git.gnu.io has moved to IP address 209.51.188.249 -- please double check where you are logging in.

router.php 47.4 KB
Newer Older
1 2
<?php
/**
3
 * StatusNet, the distributed open-source microblogging tool
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * URL routing utilities
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  URL
23
 * @package   StatusNet
24
 * @author    Evan Prodromou <evan@status.net>
25
 * @copyright 2009 StatusNet, Inc.
26
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27
 * @link      http://status.net/
28 29
 */

30
if (!defined('GNUSOCIAL')) { exit(1); }
31 32 33 34 35 36 37

/**
 * URL Router
 *
 * Cheap wrapper around Net_URL_Mapper
 *
 * @category URL
38
 * @package  StatusNet
39
 * @author   Evan Prodromou <evan@status.net>
40
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41
 * @link     http://status.net/
42 43 44
 */
class Router
{
45
    var $m = null;
Evan Prodromou's avatar
Evan Prodromou committed
46 47
    static $inst = null;

48 49
    const REGEX_TAG = '[^\/]+'; // [\pL\pN_\-\.]{1,64} better if we can do unicode regexes

Evan Prodromou's avatar
Evan Prodromou committed
50 51 52 53 54 55 56
    static function get()
    {
        if (!Router::$inst) {
            Router::$inst = new Router();
        }
        return Router::$inst;
    }
57

58 59 60 61 62 63 64 65 66
    /**
     * Clear the global singleton instance for this class.
     * Needed to ensure reset when switching site configurations.
     */
    static function clear()
    {
        Router::$inst = null;
    }

67 68
    function __construct()
    {
69
        if (empty($this->m)) {
70
            $this->m = $this->initialize();
71 72 73
        }
    }

74 75
    /**
     * Create a unique hashkey for the router.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
76
     *
77 78 79 80
     * The router's url map can change based on the version of the software
     * you're running and the plugins that are enabled. To avoid having bad routes
     * get stuck in the cache, the key includes a list of plugins and the software
     * version.
81 82
     * 
    * There can still be problems with a) differences in versions of the plugins and
83 84
     * b) people running code between official versions, but these tend to be more
     * sophisticated users who can grok what's going on and clear their caches.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
85
     *
86 87
     * @return string cache key string that should uniquely identify a router
     */
Siebrand Mazeland's avatar
Siebrand Mazeland committed
88

89 90
    static function cacheKey()
    {
91 92 93 94 95 96 97 98 99 100
        $parts = array('router');

        // Many router paths depend on this setting.
        if (common_config('singleuser', 'enabled')) {
            $parts[] = '1user';
        } else {
            $parts[] = 'multi';
        }

        return Cache::codeKey(implode(':', $parts));
101
    }
Siebrand Mazeland's avatar
Siebrand Mazeland committed
102

103 104
    function initialize()
    {
105
        $m = new URLMapper();
106

107
        if (Event::handle('StartInitializeRouter', array(&$m))) {
108

109 110 111 112 113
            // top of the menu hierarchy, sometimes "Home"
            $m->connect('', array('action' => 'top'));

            // public endpoints

114 115
            $m->connect('robots.txt', array('action' => 'robotstxt'));

116 117 118 119
            $m->connect('opensearch/people', array('action' => 'opensearch',
                                                   'type' => 'people'));
            $m->connect('opensearch/notice', array('action' => 'opensearch',
                                                   'type' => 'notice'));
120

121
            // docs
122

123
            $m->connect('doc/:title', array('action' => 'doc'));
124

125 126 127 128
            $m->connect('main/otp/:user_id/:token',
                        array('action' => 'otp'),
                        array('user_id' => '[0-9]+',
                              'token' => '.+'));
129

130 131 132 133 134 135 136 137 138 139 140 141 142 143
            // these take a code; before the main part

            foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
                $m->connect('main/'.$c.'/:code', array('action' => $c));
            }

            // Also need a block variant accepting ID on URL for mail links
            $m->connect('main/block/:profileid',
                        array('action' => 'block'),
                        array('profileid' => '[0-9]+'));

            $m->connect('main/sup/:seconds', array('action' => 'sup'),
                        array('seconds' => '[0-9]+'));

144
            // main stuff is repetitive
145

146
            $main = array('login', 'logout', 'register', 'subscribe',
147
                          'unsubscribe', 'cancelsubscription', 'approvesub',
148
                          'confirmaddress', 'recoverpassword',
149
                          'invite', 'sup',
150
                          'block', 'unblock', 'subedit',
151 152 153
                          'groupblock', 'groupunblock',
                          'sandbox', 'unsandbox',
                          'silence', 'unsilence',
154
                          'grantrole', 'revokerole',
155
                          'deleteuser',
156 157
                          'geocode',
                          'version',
158
                          'backupaccount',
159
                          'deleteaccount',
160
                          'restoreaccount',
161
                          'top',
162
                          'public',
163
            );
164

165 166 167
            foreach ($main as $a) {
                $m->connect('main/'.$a, array('action' => $a));
            }
Evan Prodromou's avatar
Evan Prodromou committed
168

169 170
            $m->connect('main/all', array('action' => 'networkpublic'));

Shashi Gowda's avatar
Shashi Gowda committed
171 172
            $m->connect('main/tagprofile/:id', array('action' => 'tagprofile'),
                                               array('id' => '[0-9]+'));
173

174 175
            $m->connect('main/tagprofile', array('action' => 'tagprofile'));

176 177 178
            $m->connect('main/xrds',
                        array('action' => 'publicxrds'));

179 180
            // settings

181
            foreach (array('profile', 'avatar', 'password', 'im', 'oauthconnections',
182
                           'oauthapps', 'email', 'sms', 'url') as $s) {
183 184
                $m->connect('settings/'.$s, array('action' => $s.'settings'));
            }
185

186 187 188 189
            if (common_config('oldschool', 'enabled')) {
                $m->connect('settings/oldschool', array('action' => 'oldschoolsettings'));
            }

Evan Prodromou's avatar
Evan Prodromou committed
190
            $m->connect('settings/oauthapps/show/:id',
191 192
                        array('action' => 'showapplication'),
                        array('id' => '[0-9]+')
Evan Prodromou's avatar
Evan Prodromou committed
193 194
            );
            $m->connect('settings/oauthapps/new',
195
                        array('action' => 'newapplication')
Evan Prodromou's avatar
Evan Prodromou committed
196 197
            );
            $m->connect('settings/oauthapps/edit/:id',
198 199
                        array('action' => 'editapplication'),
                        array('id' => '[0-9]+')
Evan Prodromou's avatar
Evan Prodromou committed
200
            );
201
            $m->connect('settings/oauthapps/delete/:id',
202 203
                        array('action' => 'deleteapplication'),
                        array('id' => '[0-9]+')
204
            );
Evan Prodromou's avatar
Evan Prodromou committed
205

206
            // search
207

208 209 210 211
            foreach (array('group', 'people', 'notice') as $s) {
                $m->connect('search/'.$s.'?q=:q',
                            array('action' => $s.'search'),
                            array('q' => '.+'));
212
                $m->connect('search/'.$s, array('action' => $s.'search'));
213 214 215 216 217 218
            }

            // The second of these is needed to make the link work correctly
            // when inserted into the page. The first is needed to match the
            // route on the way in. Seems to be another Net_URL_Mapper bug to me.
            $m->connect('search/notice/rss?q=:q', array('action' => 'noticesearchrss'),
219
                        array('q' => '.+'));
220
            $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
221

222 223 224 225
            $m->connect('attachment/:attachment',
                        array('action' => 'attachment'),
                        array('attachment' => '[0-9]+'));

226 227 228 229
            $m->connect('attachment/:attachment/download',
                        array('action' => 'attachment_download'),
                        array('attachment' => '[0-9]+'));

230 231 232 233 234 235
            $m->connect('attachment/:attachment/thumbnail',
                        array('action' => 'attachment_thumbnail'),
                        array('attachment' => '[0-9]+'));

            $m->connect('notice/new?replyto=:replyto&inreplyto=:inreplyto',
                        array('action' => 'newnotice'),
236
                        array('replyto' => Nickname::DISPLAY_FMT),
237 238
                        array('inreplyto' => '[0-9]+'));

239 240 241 242 243 244
            $m->connect('notice/new?replyto=:replyto',
                        array('action' => 'newnotice'),
                        array('replyto' => Nickname::DISPLAY_FMT));

            $m->connect('notice/new', array('action' => 'newnotice'));

245 246 247
            $m->connect('notice/:notice',
                        array('action' => 'shownotice'),
                        array('notice' => '[0-9]+'));
248

249
            $m->connect('notice/:notice/delete',
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
                        array('action' => 'deletenotice'),
                        array('notice' => '[0-9]+'));

            // conversation

            $m->connect('conversation/:id',
                        array('action' => 'conversation'),
                        array('id' => '[0-9]+'));

            $m->connect('user/:id',
                        array('action' => 'userbyid'),
                        array('id' => '[0-9]+'));

            $m->connect('tag/:tag/rss',
                        array('action' => 'tagrss'),
265
                        array('tag' => self::REGEX_TAG));
266 267
            $m->connect('tag/:tag',
                        array('action' => 'tag'),
268
                        array('tag' => self::REGEX_TAG));
269 270 271 272 273

            // groups

            $m->connect('group/new', array('action' => 'newgroup'));

274
            foreach (array('edit', 'join', 'leave', 'delete', 'cancel', 'approve') as $v) {
275 276
                $m->connect('group/:nickname/'.$v,
                            array('action' => $v.'group'),
277
                            array('nickname' => Nickname::DISPLAY_FMT));
278 279 280
                $m->connect('group/:id/id/'.$v,
                            array('action' => $v.'group'),
                            array('id' => '[0-9]+'));
281 282
            }

283
            foreach (array('members', 'logo', 'rss') as $n) {
284 285
                $m->connect('group/:nickname/'.$n,
                            array('action' => 'group'.$n),
286
                            array('nickname' => Nickname::DISPLAY_FMT));
287 288 289 290
            }

            $m->connect('group/:nickname/foaf',
                        array('action' => 'foafgroup'),
291
                        array('nickname' => Nickname::DISPLAY_FMT));
292

293 294
            $m->connect('group/:nickname/blocked',
                        array('action' => 'blockedfromgroup'),
295
                        array('nickname' => Nickname::DISPLAY_FMT));
296

297 298
            $m->connect('group/:nickname/makeadmin',
                        array('action' => 'makeadmin'),
299
                        array('nickname' => Nickname::DISPLAY_FMT));
300

301 302 303
            $m->connect('group/:nickname/members/pending',
                        array('action' => 'groupqueue'),
                        array('nickname' => Nickname::DISPLAY_FMT));
304

305 306 307
            $m->connect('group/:id/id',
                        array('action' => 'groupbyid'),
                        array('id' => '[0-9]+'));
308

309 310
            $m->connect('group/:nickname',
                        array('action' => 'showgroup'),
311
                        array('nickname' => Nickname::DISPLAY_FMT));
312

313 314 315 316
            $m->connect('group/:nickname/',
                        array('action' => 'showgroup'),
                        array('nickname' => Nickname::DISPLAY_FMT));

317 318 319 320 321 322 323 324 325
            $m->connect('group/', array('action' => 'groups'));
            $m->connect('group', array('action' => 'groups'));
            $m->connect('groups/', array('action' => 'groups'));
            $m->connect('groups', array('action' => 'groups'));

            // Twitter-compatible API

            // statuses API

326 327 328 329 330
            $m->connect('api',
                        array('action' => 'Redirect',
                              'nextAction' => 'doc',
                              'args' => array('title' => 'api')));

331 332
            $m->connect('api/statuses/public_timeline.:format',
                        array('action' => 'ApiTimelinePublic',
333
                              'format' => '(xml|json|rss|atom|as)'));
334

335 336 337 338 339
            // this is not part of the Twitter API. Also may require authentication depending on server config!
            $m->connect('api/statuses/networkpublic_timeline.:format',
                        array('action' => 'ApiTimelineNetworkPublic',
                              'format' => '(xml|json|rss|atom|as)'));

340 341
            $m->connect('api/statuses/friends_timeline/:id.:format',
                        array('action' => 'ApiTimelineFriends',
342
                              'id' => Nickname::INPUT_FMT,
343
                              'format' => '(xml|json|rss|atom|as)'));
344

345 346
            $m->connect('api/statuses/friends_timeline.:format',
                        array('action' => 'ApiTimelineFriends',
347
                              'format' => '(xml|json|rss|atom|as)'));
348 349

            $m->connect('api/statuses/home_timeline/:id.:format',
350
                        array('action' => 'ApiTimelineHome',
351
                              'id' => Nickname::INPUT_FMT,
352
                              'format' => '(xml|json|rss|atom|as)'));
353

354 355
            $m->connect('api/statuses/home_timeline.:format',
                        array('action' => 'ApiTimelineHome',
356
                              'format' => '(xml|json|rss|atom|as)'));
357

358 359
            $m->connect('api/statuses/user_timeline/:id.:format',
                        array('action' => 'ApiTimelineUser',
360
                              'id' => Nickname::INPUT_FMT,
361
                              'format' => '(xml|json|rss|atom|as)'));
362

363 364
            $m->connect('api/statuses/user_timeline.:format',
                        array('action' => 'ApiTimelineUser',
365
                              'format' => '(xml|json|rss|atom|as)'));
366 367 368

            $m->connect('api/statuses/mentions/:id.:format',
                        array('action' => 'ApiTimelineMentions',
369
                              'id' => Nickname::INPUT_FMT,
370
                              'format' => '(xml|json|rss|atom|as)'));
371

372
            $m->connect('api/statuses/mentions.:format',
373
                        array('action' => 'ApiTimelineMentions',
374
                              'format' => '(xml|json|rss|atom|as)'));
375 376 377

            $m->connect('api/statuses/replies/:id.:format',
                        array('action' => 'ApiTimelineMentions',
378
                              'id' => Nickname::INPUT_FMT,
379
                              'format' => '(xml|json|rss|atom|as)'));
380

381 382 383
            $m->connect('api/statuses/replies.:format',
                        array('action' => 'ApiTimelineMentions',
                              'format' => '(xml|json|rss|atom|as)'));
384 385 386 387 388 389 390 391 392
 
            $m->connect('api/statuses/mentions_timeline/:id.:format',
                        array('action' => 'ApiTimelineMentions',
                              'id' => Nickname::INPUT_FMT,
                              'format' => '(xml|json|rss|atom|as)'));

            $m->connect('api/statuses/mentions_timeline.:format',
                        array('action' => 'ApiTimelineMentions',
                              'format' => '(xml|json|rss|atom|as)'));
393

394 395
            $m->connect('api/statuses/friends/:id.:format',
                        array('action' => 'ApiUserFriends',
396
                              'id' => Nickname::INPUT_FMT,
397
                              'format' => '(xml|json)'));
398

399 400
            $m->connect('api/statuses/friends.:format',
                        array('action' => 'ApiUserFriends',
401
                              'format' => '(xml|json)'));
402

403 404
            $m->connect('api/statuses/followers/:id.:format',
                        array('action' => 'ApiUserFollowers',
405
                              'id' => Nickname::INPUT_FMT,
406
                              'format' => '(xml|json)'));
407

408 409 410
            $m->connect('api/statuses/followers.:format',
                        array('action' => 'ApiUserFollowers',
                              'format' => '(xml|json)'));
411

412 413 414
            $m->connect('api/statuses/show/:id.:format',
                        array('action' => 'ApiStatusesShow',
                              'id' => '[0-9]+',
415
                              'format' => '(xml|json|atom)'));
416

417 418 419 420
            $m->connect('api/statuses/show.:format',
                        array('action' => 'ApiStatusesShow',
                              'format' => '(xml|json|atom)'));

421 422
            $m->connect('api/statuses/update.:format',
                        array('action' => 'ApiStatusesUpdate',
423
                              'format' => '(xml|json|atom)'));
424

425
            $m->connect('api/statuses/destroy/:id.:format',
426
                        array('action' => 'ApiStatusesDestroy',
427
                              'id' => '[0-9]+',
428
                              'format' => '(xml|json)'));
429

430
            $m->connect('api/statuses/destroy.:format',
431
                        array('action' => 'ApiStatusesDestroy',
432 433
                              'format' => '(xml|json)'));

434 435
            // START qvitter API additions
            
436 437 438 439
            $m->connect('api/attachment/:id.:format',
                        array('action' => 'ApiAttachment',
                              'id' => '[0-9]+',
                              'format' => '(xml|json)'));
440
            
441 442 443
            $m->connect('api/checkhub.:format',
                        array('action' => 'ApiCheckHub',
                              'format' => '(xml|json)'));
444
            
445 446 447
            $m->connect('api/externalprofile/show.:format',
                        array('action' => 'ApiExternalProfileShow',
                              'format' => '(xml|json)'));
448 449

            $m->connect('api/statusnet/groups/admins/:id.:format',
450 451 452
                        array('action' => 'ApiGroupAdmins',
                              'id' => Nickname::INPUT_FMT,
                              'format' => '(xml|json)'));
453
            
454 455 456
            $m->connect('api/account/update_link_color.:format',
                        array('action' => 'ApiAccountUpdateLinkColor',
                              'format' => '(xml|json)'));
457
                
458 459 460
            $m->connect('api/account/update_background_color.:format',
                        array('action' => 'ApiAccountUpdateBackgroundColor',
                              'format' => '(xml|json)'));
461

462 463 464
            $m->connect('api/account/register.:format',
                        array('action' => 'ApiAccountRegister',
                              'format' => '(xml|json)'));
465
            
466 467 468
            $m->connect('api/check_nickname.:format',
                        array('action' => 'ApiCheckNickname',
                              'format' => '(xml|json)'));
469 470 471

            // END qvitter API additions

472
            // users
473

474
            $m->connect('api/users/show/:id.:format',
475
                        array('action' => 'ApiUserShow',
476
                              'id' => Nickname::INPUT_FMT,
477 478
                              'format' => '(xml|json)'));

479
            $m->connect('api/users/show.:format',
480 481
                        array('action' => 'ApiUserShow',
                              'format' => '(xml|json)'));
482

483 484 485 486 487
            $m->connect('api/users/profile_image/:screen_name.:format',
                        array('action' => 'ApiUserProfileImage',
                              'screen_name' => Nickname::DISPLAY_FMT,
                              'format' => '(xml|json)'));

488
            // friendships
489

490 491 492
            $m->connect('api/friendships/show.:format',
                        array('action' => 'ApiFriendshipsShow',
                              'format' => '(xml|json)'));
493

494 495 496
            $m->connect('api/friendships/exists.:format',
                        array('action' => 'ApiFriendshipsExists',
                              'format' => '(xml|json)'));
497

498
            $m->connect('api/friendships/create/:id.:format',
499
                        array('action' => 'ApiFriendshipsCreate',
500
                              'id' => Nickname::INPUT_FMT,
501
                              'format' => '(xml|json)'));
502

503
            $m->connect('api/friendships/create.:format',
504 505
                        array('action' => 'ApiFriendshipsCreate',
                              'format' => '(xml|json)'));
506

507 508
            $m->connect('api/friendships/destroy/:id.:format',
                        array('action' => 'ApiFriendshipsDestroy',
509
                              'id' => Nickname::INPUT_FMT,
510
                              'format' => '(xml|json)'));
511

512 513 514 515
            $m->connect('api/friendships/destroy.:format',
                        array('action' => 'ApiFriendshipsDestroy',
                              'format' => '(xml|json)'));

516
            // Social graph
517

518
            $m->connect('api/friends/ids/:id.:format',
519
                        array('action' => 'ApiUserFriends',
520
                              'ids_only' => true));
521

522
            $m->connect('api/followers/ids/:id.:format',
523
                        array('action' => 'ApiUserFollowers',
524
                              'ids_only' => true));
525

526
            $m->connect('api/friends/ids.:format',
527
                        array('action' => 'ApiUserFriends',
528
                              'ids_only' => true));
529

530
            $m->connect('api/followers/ids.:format',
531
                        array('action' => 'ApiUserFollowers',
532
                              'ids_only' => true));
533

534
            // account
535

536 537
            $m->connect('api/account/verify_credentials.:format',
                        array('action' => 'ApiAccountVerifyCredentials'));
538

539 540 541
            $m->connect('api/account/update_profile.:format',
                        array('action' => 'ApiAccountUpdateProfile'));

542 543 544
            $m->connect('api/account/update_profile_image.:format',
                        array('action' => 'ApiAccountUpdateProfileImage'));

545 546 547
            $m->connect('api/account/update_delivery_device.:format',
                        array('action' => 'ApiAccountUpdateDeliveryDevice'));

548
            // special case where verify_credentials is called w/out a format
549

550 551
            $m->connect('api/account/verify_credentials',
                        array('action' => 'ApiAccountVerifyCredentials'));
552

553 554
            $m->connect('api/account/rate_limit_status.:format',
                        array('action' => 'ApiAccountRateLimitStatus'));
555

556
            // blocks
557

558 559
            $m->connect('api/blocks/create/:id.:format',
                        array('action' => 'ApiBlockCreate',
560
                              'id' => Nickname::INPUT_FMT,
561
                              'format' => '(xml|json)'));
562

563 564
            $m->connect('api/blocks/create.:format',
                        array('action' => 'ApiBlockCreate',
565 566
                              'format' => '(xml|json)'));

567 568
            $m->connect('api/blocks/destroy/:id.:format',
                        array('action' => 'ApiBlockDestroy',
569
                              'id' => Nickname::INPUT_FMT,
570
                              'format' => '(xml|json)'));
571 572 573 574 575

            $m->connect('api/blocks/destroy.:format',
                        array('action' => 'ApiBlockDestroy',
                              'format' => '(xml|json)'));

576
            // help
577

578 579 580
            $m->connect('api/help/test.:format',
                        array('action' => 'ApiHelpTest',
                              'format' => '(xml|json)'));
581

582
            // statusnet
583

584
            $m->connect('api/statusnet/version.:format',
585
                        array('action' => 'ApiGNUsocialVersion',
586
                              'format' => '(xml|json)'));
587

588
            $m->connect('api/statusnet/config.:format',
589
                        array('action' => 'ApiGNUsocialConfig',
590
                              'format' => '(xml|json)'));
591

592
            // For our current software name, we provide "gnusocial" base action
593

594 595
            $m->connect('api/gnusocial/version.:format',
                        array('action' => 'ApiGNUsocialVersion',
596 597
                              'format' => '(xml|json)'));

598 599
            $m->connect('api/gnusocial/config.:format',
                        array('action' => 'ApiGNUsocialConfig',
600
                              'format' => '(xml|json)'));
601

602 603 604 605 606 607 608 609
            // Groups and tags are newer than 0.8.1 so no backward-compatibility
            // necessary

            // Groups
            //'list' has to be handled differently, as php will not allow a method to be named 'list'

            $m->connect('api/statusnet/groups/timeline/:id.:format',
                        array('action' => 'ApiTimelineGroup',
610
                              'id' => Nickname::INPUT_FMT,
611
                              'format' => '(xml|json|rss|atom|as)'));
612 613 614

            $m->connect('api/statusnet/groups/show/:id.:format',
                        array('action' => 'ApiGroupShow',
615
                              'id' => Nickname::INPUT_FMT,
616 617
                              'format' => '(xml|json)'));

618 619
            $m->connect('api/statusnet/groups/show.:format',
                        array('action' => 'ApiGroupShow',
620 621 622 623
                              'format' => '(xml|json)'));

            $m->connect('api/statusnet/groups/join/:id.:format',
                        array('action' => 'ApiGroupJoin',
mmn's avatar
mmn committed
624
                              'id' => Nickname::INPUT_FMT,
625 626
                              'format' => '(xml|json)'));

627 628
            $m->connect('api/statusnet/groups/join.:format',
                        array('action' => 'ApiGroupJoin',
629 630 631 632 633 634
                              'format' => '(xml|json)'));

            $m->connect('api/statusnet/groups/leave/:id.:format',
                        array('action' => 'ApiGroupLeave',
                              'format' => '(xml|json)'));

635 636 637 638 639
            $m->connect('api/statusnet/groups/leave.:format',
                        array('action' => 'ApiGroupLeave',
                              'id' => Nickname::INPUT_FMT,
                              'format' => '(xml|json)'));

640 641 642 643
            $m->connect('api/statusnet/groups/is_member.:format',
                        array('action' => 'ApiGroupIsMember',
                              'format' => '(xml|json)'));

644
            $m->connect('api/statusnet/groups/list/:id.:format',
645
                        array('action' => 'ApiGroupList',
646
                              'id' => Nickname::INPUT_FMT,
647 648
                              'format' => '(xml|json|rss|atom)'));

649
            $m->connect('api/statusnet/groups/list.:format',
650 651 652 653 654 655 656
                        array('action' => 'ApiGroupList',
                              'format' => '(xml|json|rss|atom)'));

            $m->connect('api/statusnet/groups/list_all.:format',
                        array('action' => 'ApiGroupListAll',
                              'format' => '(xml|json|rss|atom)'));

657
            $m->connect('api/statusnet/groups/membership/:id.:format',
658
                        array('action' => 'ApiGroupMembership',
659
                              'id' => Nickname::INPUT_FMT,
660 661
                              'format' => '(xml|json)'));

662
            $m->connect('api/statusnet/groups/membership.:format',
663 664 665 666 667 668
                        array('action' => 'ApiGroupMembership',
                              'format' => '(xml|json)'));

            $m->connect('api/statusnet/groups/create.:format',
                        array('action' => 'ApiGroupCreate',
                              'format' => '(xml|json)'));
669 670 671 672 673

            $m->connect('api/statusnet/groups/update/:id.:format',
                        array('action' => 'ApiGroupProfileUpdate',
                              'id' => '[a-zA-Z0-9]+',
                              'format' => '(xml|json)'));
Evan Prodromou's avatar
Evan Prodromou committed
674 675 676 677 678
                              
            $m->connect('api/statusnet/conversation/:id.:format',
                        array('action' => 'apiconversation',
                              'id' => '[0-9]+',
                              'format' => '(xml|json|rss|atom|as)'));
679

680
            // Lists (people tags)
681 682 683
            $m->connect('api/lists/list.:format',
                        array('action' => 'ApiListSubscriptions',
                              'format' => '(xml|json)'));
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702

            $m->connect('api/lists/memberships.:format',
                        array('action' => 'ApiListMemberships',
                              'format' => '(xml|json)'));

            $m->connect('api/:user/lists/memberships.:format',
                        array('action' => 'ApiListMemberships',
                              'user' => '[a-zA-Z0-9]+',
                              'format' => '(xml|json)'));

            $m->connect('api/lists/subscriptions.:format',
                        array('action' => 'ApiListSubscriptions',
                              'format' => '(xml|json)'));

            $m->connect('api/:user/lists/subscriptions.:format',
                        array('action' => 'ApiListSubscriptions',
                              'user' => '[a-zA-Z0-9]+',
                              'format' => '(xml|json)'));

703
            $m->connect('api/lists.:format',
704 705 706 707 708 709 710 711 712
                        array('action' => 'ApiLists',
                              'format' => '(xml|json)'));

            $m->connect('api/:user/lists/:id.:format',
                        array('action' => 'ApiList',
                              'user' => '[a-zA-Z0-9]+',
                              'id' => '[a-zA-Z0-9]+',
                              'format' => '(xml|json)'));

713 714 715 716 717
            $m->connect('api/:user/lists.:format',
                        array('action' => 'ApiLists',
                              'user' => '[a-zA-Z0-9]+',
                              'format' => '(xml|json)'));

718 719 720 721 722 723
            $m->connect('api/:user/lists/:id/statuses.:format',
                        array('action' => 'ApiTimelineList',
                              'user' => '[a-zA-Z0-9]+',
                              'id' => '[a-zA-Z0-9]+',
                              'format' => '(xml|json|rss|atom)'));

724 725
            $m->connect('api/:user/:list_id/members/:id.:format',
                        array('action' => 'ApiListMember',
726 727
                              'user' => '[a-zA-Z0-9]+',
                              'list_id' => '[a-zA-Z0-9]+',
728
                              'id' => '[a-zA-Z0-9]+',
729 730
                              'format' => '(xml|json)'));

731 732
            $m->connect('api/:user/:list_id/members.:format',
                        array('action' => 'ApiListMembers',
733 734 735 736
                              'user' => '[a-zA-Z0-9]+',
                              'list_id' => '[a-zA-Z0-9]+',
                              'format' => '(xml|json)'));

737 738
            $m->connect('api/:user/:list_id/subscribers/:id.:format',
                        array('action' => 'ApiListSubscriber',
739 740 741 742 743
                              'user' => '[a-zA-Z0-9]+',
                              'list_id' => '[a-zA-Z0-9]+',
                              'id' => '[a-zA-Z0-9]+',
                              'format' => '(xml|json)'));

744 745
            $m->connect('api/:user/:list_id/subscribers.:format',
                        array('action' => 'ApiListSubscribers',
746 747 748 749
                              'user' => '[a-zA-Z0-9]+',
                              'list_id' => '[a-zA-Z0-9]+',
                              'format' => '(xml|json)'));

750 751 752
            // Tags
            $m->connect('api/statusnet/tags/timeline/:tag.:format',
                        array('action' => 'ApiTimelineTag',
753
                              'tag'    => self::REGEX_TAG,
754
                              'format' => '(xml|json|rss|atom|as)'));
755

756 757 758 759 760
            // media related
            $m->connect(
                'api/statusnet/media/upload',
                array('action' => 'ApiMediaUpload')
            );
761 762 763 764
            $m->connect(
                'api/statuses/update_with_media.json',
                array('action' => 'ApiMediaUpload')
            );
765 766 767 768 769 770 771
            // Twitter Media upload API v1.1
            $m->connect(
                'api/media/upload.:format',
                array('action' => 'ApiMediaUpload',
                      'format' => '(xml|json)',
                      )
            );
772

773
            // search
774 775 776
            $m->connect('api/search.atom', array('action' => 'ApiSearchAtom'));
            $m->connect('api/search.json', array('action' => 'ApiSearchJSON'));
            $m->connect('api/trends.json', array('action' => 'ApiTrends'));
777

Evan Prodromou's avatar
Evan Prodromou committed
778
            $m->connect('api/oauth/request_token',
779
                        array('action' => 'ApiOAuthRequestToken'));
Evan Prodromou's avatar
Evan Prodromou committed
780 781

            $m->connect('api/oauth/access_token',
782
                        array('action' => 'ApiOAuthAccessToken'));
Evan Prodromou's avatar
Evan Prodromou committed
783 784

            $m->connect('api/oauth/authorize',
785
                        array('action' => 'ApiOAuthAuthorize'));
Evan Prodromou's avatar
Evan Prodromou committed
786 787 788

            // Admin

789 790
            $m->connect('panel/site', array('action' => 'siteadminpanel'));
            $m->connect('panel/user', array('action' => 'useradminpanel'));
791
            $m->connect('panel/access', array('action' => 'accessadminpanel'));
792 793 794 795 796 797 798
            $m->connect('panel/paths', array('action' => 'pathsadminpanel'));
            $m->connect('panel/sessions', array('action' => 'sessionsadminpanel'));
            $m->connect('panel/sitenotice', array('action' => 'sitenoticeadminpanel'));
            $m->connect('panel/license', array('action' => 'licenseadminpanel'));

            $m->connect('panel/plugins', array('action' => 'pluginsadminpanel'));
            $m->connect('panel/plugins/enable/:plugin',
799 800
                        array('action' => 'pluginenable'),
                        array('plugin' => '[A-Za-z0-9_]+'));
801
            $m->connect('panel/plugins/disable/:plugin',
802 803
                        array('action' => 'plugindisable'),
                        array('plugin' => '[A-Za-z0-9_]+'));
804

805 806 807 808
            $m->connect('getfile/:filename',
                        array('action' => 'getfile'),
                        array('filename' => '[A-Za-z0-9._-]+'));

809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
            // Common people-tag stuff

            $m->connect('peopletag/:tag', array('action' => 'peopletag',
                                                'tag'    => self::REGEX_TAG));

            $m->connect('selftag/:tag', array('action' => 'selftag',
                                              'tag'    => self::REGEX_TAG));

            $m->connect('main/addpeopletag', array('action' => 'addpeopletag'));

            $m->connect('main/removepeopletag', array('action' => 'removepeopletag'));

            $m->connect('main/profilecompletion', array('action' => 'profilecompletion'));

            $m->connect('main/peopletagautocomplete', array('action' => 'peopletagautocomplete'));

Evan Prodromou's avatar
Evan Prodromou committed
825
            // In the "root"
826

Evan Prodromou's avatar
Evan Prodromou committed
827 828
            if (common_config('singleuser', 'enabled')) {

829
                $nickname = User::singleUserNickname();
Evan Prodromou's avatar
Evan Prodromou committed
830 831

                foreach (array('subscriptions', 'subscribers',
832
                               'all', 'foaf', 'replies',
833
                               ) as $a) {
Evan Prodromou's avatar
Evan Prodromou committed
834 835 836 837 838 839 840 841 842
                    $m->connect($a,
                                array('action' => $a,
                                      'nickname' => $nickname));
                }

                foreach (array('subscriptions', 'subscribers') as $a) {
                    $m->connect($a.'/:tag',
                                array('action' => $a,
                                      'nickname' => $nickname),
843
                                array('tag' => self::REGEX_TAG));
Evan Prodromou's avatar
Evan Prodromou committed
844 845
                }

846 847 848 849
                $m->connect('subscribers/pending',
                            array('action' => 'subqueue',
                                  'nickname' => $nickname));

Evan Prodromou's avatar
Evan Prodromou committed
850 851 852 853 854 855
                foreach (array('rss', 'groups') as $a) {
                    $m->connect($a,
                                array('action' => 'user'.$a,
                                      'nickname' => $nickname));
                }

856
                foreach (array('all', 'replies') as $a) {
Evan Prodromou's avatar
Evan Prodromou committed
857 858 859 860 861
                    $m->connect($a.'/rss',
                                array('action' => $a.'rss',
                                      'nickname' => $nickname));
                }

862 863 864
                $m->connect('avatar',
                            array('action' => 'avatarbynickname',
                                  'nickname' => $nickname));
Evan Prodromou's avatar
Evan Prodromou committed
865 866 867
                $m->connect('avatar/:size',
                            array('action' => 'avatarbynickname',
                                  'nickname' => $nickname),
868
                            array('size' => '(|original|\d+)'));
Evan Prodromou's avatar
Evan Prodromou committed
869 870 871 872

                $m->connect('tag/:tag/rss',
                            array('action' => 'userrss',
                                  'nickname' => $nickname),
873
                            array('tag' => self::REGEX_TAG));
Evan Prodromou's avatar
Evan Prodromou committed
874 875 876 877

                $m->connect('tag/:tag',
                            array('action' => 'showstream',
                                  'nickname' => $nickname),
878
                            array('tag' => self::REGEX_TAG));
Evan Prodromou's avatar
Evan Prodromou committed
879

880 881 882 883
                $m->connect('rsd.xml',
                            array('action' => 'rsd',
                                  'nickname' => $nickname));

884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
                // peopletags

                $m->connect('peopletags',
                            array('action' => 'peopletagsbyuser'));

                $m->connect('peopletags/private',
                            array('action' => 'peopletagsbyuser',
                                  'private' => 1));

                $m->connect('peopletags/public',
                            array('action' => 'peopletagsbyuser',
                                  'public' => 1));

                $m->connect('othertags',
                            array('action' => 'peopletagsforuser'));

                $m->connect('peopletagsubscriptions',
                            array('action' => 'peopletagsubscriptions'));

                $m->connect('all/:tag/subscribers',
                            array('action' => 'peopletagsubscribers',
                                  'tag' => self::REGEX_TAG));

                $m->connect('all/:tag/tagged',
                                array('action' => 'peopletagged',
                                      'tag' => self::REGEX_TAG));

                $m->connect('all/:tag/edit',
                                array('action' => 'editpeopletag',
                                      'tag' => self::REGEX_TAG));

                foreach(array('subscribe', 'unsubscribe') as $v) {
                    $m->connect('peopletag/:id/'.$v,
                                    array('action' => $v.'peopletag',
                                          'id' => '[0-9]{1,64}'));
                }
                $m->connect('user/:tagger_id/profiletag/:id/id',
                                array('action' => 'profiletagbyid',
                                      'tagger_id' => '[0-9]+',
                                      'id' => '[0-9]+'));

                $m->connect('all/:tag',
                                array('action' => 'showprofiletag',
927
                                      'nickname' => $nickname,
928 929 930 931 932 933 934
                                      'tag' => self::REGEX_TAG));

                foreach (array('subscriptions', 'subscribers') as $a) {
                    $m->connect($a.'/:tag',
                                array('action' => $a),
                                array('tag' => self::REGEX_TAG));
                }
935
            }
Evan Prodromou's avatar
Evan Prodromou committed
936

937 938 939 940 941 942 943 944
            $m->connect('rss', array('action' => 'publicrss'));
            $m->connect('featuredrss', array('action' => 'featuredrss'));
            $m->connect('featured/', array('action' => 'featured'));
            $m->connect('featured', array('action' => 'featured'));
            $m->connect('rsd.xml', array('action' => 'rsd'));

            foreach (array('subscriptions', 'subscribers',
                           'nudge', 'all', 'foaf', 'replies',
945
                           'inbox', 'outbox') as $a) {
946 947
                $m->connect(':nickname/'.$a,
                            array('action' => $a),
948
                            array('nickname' => Nickname::DISPLAY_FMT));
949 950 951 952
            }
            $m->connect(':nickname/subscribers/pending',
                        array('action' => 'subqueue'),
                        array('nickname' => Nickname::DISPLAY_FMT));
Evan Prodromou's avatar
Evan Prodromou committed
953

954 955 956 957 958 959 960
            // some targeted RSS 1.0 actions (extends TargetedRss10Action)
            foreach (array('all', 'replies') as $a) {
                $m->connect(':nickname/'.$a.'/rss',
                            array('action' => $a.'rss'),
                            array('nickname' => Nickname::DISPLAY_FMT));
            }

961
            // people tags
Shashi Gowda's avatar
Shashi Gowda committed
962

963 964 965
            $m->connect(':nickname/peopletags',
                            array('action' => 'peopletagsbyuser',
                                  'nickname' => Nickname::DISPLAY_FMT));
Shashi Gowda's avatar
Shashi Gowda committed
966

967 968 969 970
            $m->connect(':nickname/peopletags/private',
                            array('action' => 'peopletagsbyuser',
                                  'nickname' => Nickname::DISPLAY_FMT,
                                  'private' => 1));
Shashi Gowda's avatar
Shashi Gowda committed
971

972 973 974 975
            $m->connect(':nickname/peopletags/public',
                            array('action' => 'peopletagsbyuser',
                                  'nickname' => Nickname::DISPLAY_FMT,
                                  'public' => 1));
Shashi Gowda's avatar
Shashi Gowda committed
976

977 978 979
            $m->connect(':nickname/othertags',
                            array('action' => 'peopletagsforuser',
                                  'nickname' => Nickname::DISPLAY_FMT));
Shashi Gowda's avatar
Shashi Gowda committed
980

981 982 983
            $m->connect(':nickname/peopletagsubscriptions',
                            array('action' => 'peopletagsubscriptions',
                                  'nickname' => Nickname::DISPLAY_FMT));
Shashi Gowda's avatar
Shashi Gowda committed
984

985 986 987 988
            $m->connect(':tagger/all/:tag/subscribers',
                            array('action' => 'peopletagsubscribers',
                                  'tagger' => Nickname::DISPLAY_FMT,
                                  'tag' => self::REGEX_TAG));
Shashi Gowda's avatar
Shashi Gowda committed
989

990 991 992 993
            $m->connect(':tagger/all/:tag/tagged',
                            array('action' => 'peopletagged',
                                  'tagger' => Nickname::DISPLAY_FMT,
                                  'tag' => self::REGEX_TAG));
Shashi Gowda's avatar
Shashi Gowda committed
994

995 996 997 998
            $m->connect(':tagger/all/:tag/edit',
                            array('action' => 'editpeopletag',
                                  'tagger' => Nickname::DISPLAY_FMT,
                                  'tag' => self::REGEX_TAG));
Shashi Gowda's avatar
Shashi Gowda committed
999

1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
            foreach(array('subscribe', 'unsubscribe') as $v) {
                $m->connect('peopletag/:id/'.$v,
                                array('action' => $v.'peopletag',
                                      'id' => '[0-9]{1,64}'));
            }
            $m->connect('user/:tagger_id/profiletag/:id/id',
                            array('action' => 'profiletagbyid',
                                  'tagger_id' => '[0-9]+',
                                  'id' => '[0-9]+'));

1010 1011 1012
            $m->connect(':nickname/all/:tag',
                            array('action' => 'showprofiletag'),
                            array('nickname' => Nickname::DISPLAY_FMT,
1013
                                  'tag' => self::REGEX_TAG));
Evan Prodromou's avatar
Evan Prodromou committed
1014

1015 1016 1017 1018 1019 1020
            foreach (array('subscriptions', 'subscribers') as $a) {
                $m->connect(':nickname/'.$a.'/:tag',
                            array('action' => $a),
                            array('tag' => self::REGEX_TAG,
                                  'nickname' => Nickname::DISPLAY_FMT));
            }
Evan Prodromou's avatar
Evan Prodromou committed
1021

1022 1023 1024 1025 1026
            foreach (array('rss', 'groups') as $a) {
                $m->connect(':nickname/'.$a,
                            array('action' => 'user'.$a),
                            array('nickname' => Nickname::DISPLAY_FMT));
            }
Evan Prodromou's avatar
Evan Prodromou committed
1027

1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
            $m->connect(':nickname/avatar',
                        array('action' => 'avatarbynickname'),
                        array('nickname' => Nickname::DISPLAY_FMT));
            $m->connect(':nickname/avatar/:size',
                        array('action' => 'avatarbynickname'),
                        array('size' => '(|original|\d+)',
                              'nickname' => Nickname::DISPLAY_FMT));

            $m->connect(':nickname/tag/:tag/rss',
                        array('action' => 'userrss'),
                        array('nickname' => Nickname::DISPLAY_FMT),
                        array('tag' => self::REGEX_TAG));
Evan Prodromou's avatar
Evan Prodromou committed
1040

1041 1042 1043 1044
            $m->connect(':nickname/tag/:tag',
                        array('action' => 'showstream'),
                        array('nickname' => Nickname::DISPLAY_FMT),
                        array('tag' => self::REGEX_TAG));
1045

1046 1047 1048
            $m->connect(':nickname/rsd.xml',
                        array('action' => 'rsd'),
                        array('nickname' => Nickname::DISPLAY_FMT));
1049

1050 1051 1052
            $m->connect(':nickname',
                        array('action' => 'showstream'),
                        array('nickname' => Nickname::DISPLAY_FMT));
1053

1054 1055 1056
            $m->connect(':nickname/',
                        array('action' => 'showstream'),
                        array('nickname' => Nickname::DISPLAY_FMT));
1057

1058 1059 1060
            // AtomPub API

            $m->connect('api/statusnet/app/service/:id.xml',
1061 1062
                        array('action' => 'ApiAtomService'),
                        array('id' => Nickname::DISPLAY_FMT));
1063 1064 1065 1066 1067 1068 1069 1070 1071

            $m->connect('api/statusnet/app/service.xml',
                        array('action' => 'ApiAtomService'));

            $m->connect('api/statusnet/app/subscriptions/:subscriber/:subscribed.atom',
                        array('action' => 'AtomPubShowSubscription'),
                        array('subscriber' => '[0-9]+',
                              'subscribed' => '[0-9]+'));

1072 1073 1074 1075
            $m->connect('api/statusnet/app/subscriptions/:subscriber.atom',
                        array('action' => 'AtomPubSubscriptionFeed'),
                        array('subscriber' => '[0-9]+'));

1076 1077 1078 1079 1080
            $m->connect('api/statusnet/app/memberships/:profile/:group.atom',
                        array('action' => 'AtomPubShowMembership'),
                        array('profile' => '[0-9]+',
                              'group' => '[0-9]+'));

1081
            $m->connect('api/statusnet/app/memberships/:profile.atom',
1082 1083 1084
                        array('action' => 'AtomPubMembershipFeed'),
                        array('profile' => '[0-9]+'));

1085 1086 1087 1088 1089 1090
            // URL shortening

            $m->connect('url/:id',
                        array('action' => 'redirecturl',
                              'id' => '[0-9]+'));

Evan Prodromou's avatar
Evan Prodromou committed
1091
            // user stuff
1092

1093 1094
            Event::handle('RouterInitialized', array($m));
        }
1095

1096
        return $m;
1097 1098 1099 1100
    }

    function map($path)
    {
1101
        try {
1102 1103 1104
            return $this->m->match($path);
        } catch (NoRouteMapException $e) {
            common_debug($e->getMessage());
1105
            // TRANS: Client error on action trying to visit a non-existing page.
1106
            throw new ClientException(_('Page not found.'), 404);
1107
        }
1108 1109
    }

1110
    function build($action, $args=null, $params=null, $fragment=null)
1111 1112 1113 1114
    {
        $action_arg = array('action' => $action);

        if ($args) {
Evan Prodromou's avatar
Evan Prodromou committed
1115
            $args = array_merge($action_arg, $args);
1116 1117 1118 1119
        } else {
            $args = $action_arg;
        }

1120
        $url = $this->m->generate($args, $params, $fragment);
1121 1122 1123
        // Due to a bug in the Net_URL_Mapper code, the returned URL may
        // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
        // repair that here rather than modifying the upstream code...
1124 1125

        $qpos = strpos($url, '?');
1126 1127
        if ($qpos !== false) {
            $url = substr($url, 0, $qpos+1) .
1128
                str_replace('?', '&', substr($url, $qpos+1));
1129 1130 1131 1132 1133 1134

            // @fixme this is a hacky workaround for http_build_query in the
            // lower-level code and bad configs that set the default separator
            // to &amp; instead of &. Encoded &s in parameters will not be
            // affected.
            $url = substr($url, 0, $qpos+1) .
1135
                str_replace('&amp;', '&', substr($url, $qpos+1));
1136

1137
        }
1138

1139
        return $url;
1140
    }
Adrian Lang's avatar
Adrian Lang committed
1141
}