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

router.php 38.6 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('STATUSNET') && !defined('LACONICA')) {
31 32 33 34 35
    exit(1);
}

require_once 'Net/URL/Mapper.php';

36 37
class StatusNet_URL_Mapper extends Net_URL_Mapper
{
38
    private static $_singleton = null;
39
    private $_actionToPath = array();
40 41 42 43

    private function __construct()
    {
    }
44
    
45 46 47 48 49 50 51 52 53 54 55 56 57
    public static function getInstance($id = '__default__')
    {
        if (empty(self::$_singleton)) {
            self::$_singleton = new StatusNet_URL_Mapper();
        }
        return self::$_singleton;
    }

    public function connect($path, $defaults = array(), $rules = array())
    {
        $result = null;
        if (Event::handle('StartConnectPath', array(&$path, &$defaults, &$rules, &$result))) {
            $result = parent::connect($path, $defaults, $rules);
58 59 60 61 62 63 64 65
	    if (array_key_exists('action', $defaults)) {
		$action = $defaults['action'];
	    } elseif (array_key_exists('action', $rules)) {
		$action = $rules['action'];
	    } else {
		$action = null;
	    }
	    $this->_mapAction($action, $result);
66 67 68 69
            Event::handle('EndConnectPath', array($path, $defaults, $rules, $result));
        }
        return $result;
    }
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    
    protected function _mapAction($action, $path)
    {
	if (!array_key_exists($action, $this->_actionToPath)) {
	    $this->_actionToPath[$action] = array();
	}
	$this->_actionToPath[$action][] = $path;
	return;
    }
    
    public function generate($values = array(), $qstring = array(), $anchor = '')
    {
	if (!array_key_exists('action', $values)) {
	    return parent::generate($values, $qstring, $anchor);
	}
	
	$action = $values['action'];

	if (!array_key_exists($action, $this->_actionToPath)) {
	    return parent::generate($values, $qstring, $anchor);
	}
	
	$oldPaths    = $this->paths;
	$this->paths = $this->_actionToPath[$action];
	$result      = parent::generate($values, $qstring, $anchor);
	$this->paths = $oldPaths;

	return $result;
    }
99 100
}

101 102 103 104 105 106
/**
 * URL Router
 *
 * Cheap wrapper around Net_URL_Mapper
 *
 * @category URL
107
 * @package  StatusNet
108
 * @author   Evan Prodromou <evan@status.net>
109
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
110
 * @link     http://status.net/
111 112 113
 */
class Router
{
114
    var $m = null;
Evan Prodromou's avatar
Evan Prodromou committed
115
    static $inst = null;
116
    static $bare = array('requesttoken', 'accesstoken', 'userauthorization',
Zach Copley's avatar
Zach Copley committed
117
                         'postnotice', 'updateprofile', 'finishremotesubscribe');
Evan Prodromou's avatar
Evan Prodromou committed
118 119 120 121 122 123 124 125

    static function get()
    {
        if (!Router::$inst) {
            Router::$inst = new Router();
        }
        return Router::$inst;
    }
126 127 128

    function __construct()
    {
129 130
        if (empty($this->m)) {
            $k = self::cacheKey();
131 132
            $c = Cache::instance();
            $m = $c->get($k);
133 134 135 136
            if (!empty($m)) {
                $this->m = $m;
            } else {
                $this->m = $this->initialize();
137
                $c->set($k, $this->m);
138
            }
139 140 141
        }
    }

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    /**
     * Create a unique hashkey for the router.
     * 
     * 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.
     * 
     * There can still be problems with a) differences in versions of the plugins and 
     * 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.
     * 
     * @return string cache key string that should uniquely identify a router
     */
    
    static function cacheKey()
    {
        $plugins     = StatusNet::getActivePlugins();
160
        $names       = array();
161
	
162 163 164
        foreach ($plugins as $plugin) {
            $names[] = $plugin[0];
        }
165

166 167
        $names = array_unique($names);
        asort($names);
168

169
        // Unique enough.
170
	
171
        $uniq = crc32(implode(',', $names));
172
	
173
        return Cache::key('router:'.STATUSNET_VERSION.':'.$uniq);
174 175
    }
    
176 177
    function initialize()
    {
178
        $m = StatusNet_URL_Mapper::getInstance();
179

180
        if (Event::handle('StartInitializeRouter', array(&$m))) {
181

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

184 185 186 187
            $m->connect('opensearch/people', array('action' => 'opensearch',
                                                   'type' => 'people'));
            $m->connect('opensearch/notice', array('action' => 'opensearch',
                                                   'type' => 'notice'));
188

189
            // docs
190

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

193 194 195 196
            $m->connect('main/otp/:user_id/:token',
                        array('action' => 'otp'),
                        array('user_id' => '[0-9]+',
                              'token' => '.+'));
197

198
            // main stuff is repetitive
199

200 201 202 203
            $main = array('login', 'logout', 'register', 'subscribe',
                          'unsubscribe', 'confirmaddress', 'recoverpassword',
                          'invite', 'favor', 'disfavor', 'sup',
                          'block', 'unblock', 'subedit',
204 205 206
                          'groupblock', 'groupunblock',
                          'sandbox', 'unsandbox',
                          'silence', 'unsilence',
207
                          'grantrole', 'revokerole',
208
                          'repeat',
209
                          'deleteuser',
210 211 212
                          'geocode',
                          'version',
                          );
213

214 215 216
            foreach ($main as $a) {
                $m->connect('main/'.$a, array('action' => $a));
            }
Evan Prodromou's avatar
Evan Prodromou committed
217

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

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

226
            $m->connect('main/tagother/:id', array('action' => 'tagother'));
227

228 229
            $m->connect('main/oembed',
                        array('action' => 'oembed'));
230

231 232 233
            $m->connect('main/xrds',
                        array('action' => 'publicxrds'));

234
            // these take a code
235

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

240
            // exceptional
241

242
            $m->connect('main/remote', array('action' => 'remotesubscribe'));
243
            $m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '[A-Za-z0-9_-]+'));
244

245 246 247
            foreach (Router::$bare as $action) {
                $m->connect('index.php?action=' . $action, array('action' => $action));
            }
248

249 250
            // settings

251
            foreach (array('profile', 'avatar', 'password', 'im', 'oauthconnections',
252
                           'oauthapps', 'email', 'sms', 'userdesign', 'other') as $s) {
253 254
                $m->connect('settings/'.$s, array('action' => $s.'settings'));
            }
255

Evan Prodromou's avatar
Evan Prodromou committed
256 257 258 259 260 261 262 263 264 265 266
            $m->connect('settings/oauthapps/show/:id',
                array('action' => 'showapplication'),
                array('id' => '[0-9]+')
            );
            $m->connect('settings/oauthapps/new',
                array('action' => 'newapplication')
            );
            $m->connect('settings/oauthapps/edit/:id',
                array('action' => 'editapplication'),
                array('id' => '[0-9]+')
            );
267 268 269 270
            $m->connect('settings/oauthapps/delete/:id',
                array('action' => 'deleteapplication'),
                array('id' => '[0-9]+')
            );
Evan Prodromou's avatar
Evan Prodromou committed
271

272
            // search
273

274 275 276 277 278 279 280 281 282 283 284 285
            foreach (array('group', 'people', 'notice') as $s) {
                $m->connect('search/'.$s, array('action' => $s.'search'));
                $m->connect('search/'.$s.'?q=:q',
                            array('action' => $s.'search'),
                            array('q' => '.+'));
            }

            // 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', array('action' => 'noticesearchrss'));
            $m->connect('search/notice/rss?q=:q', array('action' => 'noticesearchrss'),
286
                        array('q' => '.+'));
287

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
            $m->connect('attachment/:attachment',
                        array('action' => 'attachment'),
                        array('attachment' => '[0-9]+'));

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

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

            $m->connect('notice/new', array('action' => 'newnotice'));
            $m->connect('notice/new?replyto=:replyto',
                        array('action' => 'newnotice'),
303
                        array('replyto' => '[A-Za-z0-9_-]+'));
304 305
            $m->connect('notice/new?replyto=:replyto&inreplyto=:inreplyto',
                        array('action' => 'newnotice'),
306
                        array('replyto' => '[A-Za-z0-9_-]+'),
307 308 309 310 311 312 313 314 315 316 317 318 319 320
                        array('inreplyto' => '[0-9]+'));

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

            $m->connect('notice/:notice',
                        array('action' => 'shownotice'),
                        array('notice' => '[0-9]+'));
            $m->connect('notice/delete', array('action' => 'deletenotice'));
            $m->connect('notice/delete/:notice',
                        array('action' => 'deletenotice'),
                        array('notice' => '[0-9]+'));

321 322
            $m->connect('bookmarklet/new', array('action' => 'bookmarklet'));

323 324 325 326 327 328 329
            // conversation

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

            $m->connect('message/new', array('action' => 'newmessage'));
330
            $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
331 332 333 334 335 336 337 338 339 340 341 342 343 344
            $m->connect('message/:message',
                        array('action' => 'showmessage'),
                        array('message' => '[0-9]+'));

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

            $m->connect('tags/', array('action' => 'publictagcloud'));
            $m->connect('tag/', array('action' => 'publictagcloud'));
            $m->connect('tags', array('action' => 'publictagcloud'));
            $m->connect('tag', array('action' => 'publictagcloud'));
            $m->connect('tag/:tag/rss',
                        array('action' => 'tagrss'),
345
                        array('tag' => '[\pL\pN_\-\.]{1,64}'));
346 347 348 349 350 351 352 353 354 355 356 357
            $m->connect('tag/:tag',
                        array('action' => 'tag'),
                        array('tag' => '[\pL\pN_\-\.]{1,64}'));

            $m->connect('peopletag/:tag',
                        array('action' => 'peopletag'),
                        array('tag' => '[a-zA-Z0-9]+'));

            // groups

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

358
            foreach (array('edit', 'join', 'leave', 'delete') as $v) {
359 360 361
                $m->connect('group/:nickname/'.$v,
                            array('action' => $v.'group'),
                            array('nickname' => '[a-zA-Z0-9]+'));
362 363 364
                $m->connect('group/:id/id/'.$v,
                            array('action' => $v.'group'),
                            array('id' => '[0-9]+'));
365 366 367 368 369 370 371 372 373 374
            }

            foreach (array('members', 'logo', 'rss', 'designsettings') as $n) {
                $m->connect('group/:nickname/'.$n,
                            array('action' => 'group'.$n),
                            array('nickname' => '[a-zA-Z0-9]+'));
            }

            $m->connect('group/:nickname/foaf',
                        array('action' => 'foafgroup'),
375 376
                        array('nickname' => '[a-zA-Z0-9]+'));

377 378
            $m->connect('group/:nickname/blocked',
                        array('action' => 'blockedfromgroup'),
379
                        array('nickname' => '[a-zA-Z0-9]+'));
380

381 382 383
            $m->connect('group/:nickname/makeadmin',
                        array('action' => 'makeadmin'),
                        array('nickname' => '[a-zA-Z0-9]+'));
384

385 386 387
            $m->connect('group/:id/id',
                        array('action' => 'groupbyid'),
                        array('id' => '[0-9]+'));
388

389 390 391
            $m->connect('group/:nickname',
                        array('action' => 'showgroup'),
                        array('nickname' => '[a-zA-Z0-9]+'));
392

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
            $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

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

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

            $m->connect('api/statuses/friends_timeline/:id.:format',
                        array('action' => 'ApiTimelineFriends',
412
                              'id' => '[a-zA-Z0-9]+',
413
                              'format' => '(xml|json|rss|atom)'));
414

415
            $m->connect('api/statuses/home_timeline.:format',
416
                        array('action' => 'ApiTimelineHome',
417 418 419
                              'format' => '(xml|json|rss|atom)'));

            $m->connect('api/statuses/home_timeline/:id.:format',
420
                        array('action' => 'ApiTimelineHome',
421
                              'id' => '[a-zA-Z0-9]+',
422
                              'format' => '(xml|json|rss|atom)'));
423

424 425 426
            $m->connect('api/statuses/user_timeline.:format',
                        array('action' => 'ApiTimelineUser',
                              'format' => '(xml|json|rss|atom)'));
427

428 429
            $m->connect('api/statuses/user_timeline/:id.:format',
                        array('action' => 'ApiTimelineUser',
430
                              'id' => '[a-zA-Z0-9]+',
431 432 433 434 435 436 437 438
                              'format' => '(xml|json|rss|atom)'));

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

            $m->connect('api/statuses/mentions/:id.:format',
                        array('action' => 'ApiTimelineMentions',
439
                              'id' => '[a-zA-Z0-9]+',
440 441 442 443 444 445 446 447
                              'format' => '(xml|json|rss|atom)'));

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

            $m->connect('api/statuses/replies/:id.:format',
                        array('action' => 'ApiTimelineMentions',
448
                              'id' => '[a-zA-Z0-9]+',
449 450
                              'format' => '(xml|json|rss|atom)'));

451 452 453 454
            $m->connect('api/statuses/retweeted_by_me.:format',
                        array('action' => 'ApiTimelineRetweetedByMe',
                              'format' => '(xml|json|atom)'));

455 456 457 458
            $m->connect('api/statuses/retweeted_to_me.:format',
                        array('action' => 'ApiTimelineRetweetedToMe',
                              'format' => '(xml|json|atom)'));

459 460 461 462
            $m->connect('api/statuses/retweets_of_me.:format',
                        array('action' => 'ApiTimelineRetweetsOfMe',
                              'format' => '(xml|json|atom)'));

463 464 465 466 467 468
            $m->connect('api/statuses/friends.:format',
                        array('action' => 'ApiUserFriends',
                              'format' => '(xml|json)'));

            $m->connect('api/statuses/friends/:id.:format',
                        array('action' => 'ApiUserFriends',
469
                              'id' => '[a-zA-Z0-9]+',
470
                              'format' => '(xml|json)'));
471

472 473 474
            $m->connect('api/statuses/followers.:format',
                        array('action' => 'ApiUserFollowers',
                              'format' => '(xml|json)'));
475

476 477
            $m->connect('api/statuses/followers/:id.:format',
                        array('action' => 'ApiUserFollowers',
478
                              'id' => '[a-zA-Z0-9]+',
479
                              'format' => '(xml|json)'));
480

481 482 483
            $m->connect('api/statuses/show.:format',
                        array('action' => 'ApiStatusesShow',
                              'format' => '(xml|json)'));
484

485 486 487 488
            $m->connect('api/statuses/show/:id.:format',
                        array('action' => 'ApiStatusesShow',
                              'id' => '[0-9]+',
                              'format' => '(xml|json)'));
489

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

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

498 499 500
            $m->connect('api/statuses/destroy/:id.:format',
                        array('action' => 'ApiStatusesDestroy',
                              'id' => '[0-9]+',
501 502 503 504 505
                              'format' => '(xml|json)'));

            $m->connect('api/statuses/retweet/:id.:format',
                        array('action' => 'ApiStatusesRetweet',
                              'id' => '[0-9]+',
506 507 508 509 510
                              'format' => '(xml|json)'));

            $m->connect('api/statuses/retweets/:id.:format',
                        array('action' => 'ApiStatusesRetweets',
                              'id' => '[0-9]+',
511
                              'format' => '(xml|json)'));
512

513
            // users
514

515 516 517 518
            $m->connect('api/users/show.:format',
                        array('action' => 'ApiUserShow',
                              'format' => '(xml|json)'));

519 520
            $m->connect('api/users/show/:id.:format',
                        array('action' => 'ApiUserShow',
521
                              'id' => '[a-zA-Z0-9]+',
522
                              'format' => '(xml|json)'));
523

524
            // direct messages
525

526 527 528
            $m->connect('api/direct_messages.:format',
                        array('action' => 'ApiDirectMessage',
                              'format' => '(xml|json|rss|atom)'));
529

530 531 532 533
            $m->connect('api/direct_messages/sent.:format',
                        array('action' => 'ApiDirectMessage',
                              'format' => '(xml|json|rss|atom)',
                              'sent' => true));
534

535 536 537
            $m->connect('api/direct_messages/new.:format',
                        array('action' => 'ApiDirectMessageNew',
                              'format' => '(xml|json)'));
538

539
            // friendships
540

541 542 543
            $m->connect('api/friendships/show.:format',
                        array('action' => 'ApiFriendshipsShow',
                              'format' => '(xml|json)'));
544

545 546 547
            $m->connect('api/friendships/exists.:format',
                        array('action' => 'ApiFriendshipsExists',
                              'format' => '(xml|json)'));
548

549 550 551
            $m->connect('api/friendships/create.:format',
                        array('action' => 'ApiFriendshipsCreate',
                              'format' => '(xml|json)'));
552

553 554 555
            $m->connect('api/friendships/destroy.:format',
                        array('action' => 'ApiFriendshipsDestroy',
                              'format' => '(xml|json)'));
556

557 558
            $m->connect('api/friendships/create/:id.:format',
                        array('action' => 'ApiFriendshipsCreate',
559
                              'id' => '[a-zA-Z0-9]+',
560
                              'format' => '(xml|json)'));
561

562 563
            $m->connect('api/friendships/destroy/:id.:format',
                        array('action' => 'ApiFriendshipsDestroy',
564
                              'id' => '[a-zA-Z0-9]+',
565
                              'format' => '(xml|json)'));
566

567
            // Social graph
568

569
            $m->connect('api/friends/ids/:id.:format',
570
                        array('action' => 'ApiUserFriends',
571
                              'ids_only' => true));
572

573
            $m->connect('api/followers/ids/:id.:format',
574
                        array('action' => 'ApiUserFollowers',
575
                              'ids_only' => true));
576

577
            $m->connect('api/friends/ids.:format',
578
                        array('action' => 'ApiUserFriends',
579
                              'ids_only' => true));
580

581
            $m->connect('api/followers/ids.:format',
582
                        array('action' => 'ApiUserFollowers',
583
                              'ids_only' => true));
584

585
            // account
586

587 588
            $m->connect('api/account/verify_credentials.:format',
                        array('action' => 'ApiAccountVerifyCredentials'));
589

590 591 592
            $m->connect('api/account/update_profile.:format',
                        array('action' => 'ApiAccountUpdateProfile'));

593 594 595
            $m->connect('api/account/update_profile_image.:format',
                        array('action' => 'ApiAccountUpdateProfileImage'));

596 597 598
            $m->connect('api/account/update_profile_background_image.:format',
                        array('action' => 'ApiAccountUpdateProfileBackgroundImage'));

599 600 601
            $m->connect('api/account/update_profile_colors.:format',
                        array('action' => 'ApiAccountUpdateProfileColors'));

602 603 604
            $m->connect('api/account/update_delivery_device.:format',
                        array('action' => 'ApiAccountUpdateDeliveryDevice'));

605
            // special case where verify_credentials is called w/out a format
606

607 608
            $m->connect('api/account/verify_credentials',
                        array('action' => 'ApiAccountVerifyCredentials'));
609

610 611
            $m->connect('api/account/rate_limit_status.:format',
                        array('action' => 'ApiAccountRateLimitStatus'));
612

613
            // favorites
614

615 616 617
            $m->connect('api/favorites.:format',
                        array('action' => 'ApiTimelineFavorites',
                              'format' => '(xml|json|rss|atom)'));
618

619 620
            $m->connect('api/favorites/:id.:format',
                        array('action' => 'ApiTimelineFavorites',
621
                              'id' => '[a-zA-Z0-9]+',
622
                              'format' => '(xml|json|rss|atom)'));
623

624 625
            $m->connect('api/favorites/create/:id.:format',
                        array('action' => 'ApiFavoriteCreate',
626
                              'id' => '[a-zA-Z0-9]+',
627
                              'format' => '(xml|json)'));
628

629 630
            $m->connect('api/favorites/destroy/:id.:format',
                        array('action' => 'ApiFavoriteDestroy',
631
                              'id' => '[a-zA-Z0-9]+',
632 633
                              'format' => '(xml|json)'));
            // blocks
634

635 636 637 638
            $m->connect('api/blocks/create.:format',
                        array('action' => 'ApiBlockCreate',
                              'format' => '(xml|json)'));

639 640
            $m->connect('api/blocks/create/:id.:format',
                        array('action' => 'ApiBlockCreate',
641
                              'id' => '[a-zA-Z0-9]+',
642
                              'format' => '(xml|json)'));
643

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

648 649
            $m->connect('api/blocks/destroy/:id.:format',
                        array('action' => 'ApiBlockDestroy',
650
                              'id' => '[a-zA-Z0-9]+',
651 652
                              'format' => '(xml|json)'));
            // help
653

654 655 656
            $m->connect('api/help/test.:format',
                        array('action' => 'ApiHelpTest',
                              'format' => '(xml|json)'));
657

658
            // statusnet
659

660 661 662
            $m->connect('api/statusnet/version.:format',
                        array('action' => 'ApiStatusnetVersion',
                              'format' => '(xml|json)'));
663

664 665 666
            $m->connect('api/statusnet/config.:format',
                        array('action' => 'ApiStatusnetConfig',
                              'format' => '(xml|json)'));
667

668 669 670 671 672 673 674 675 676
            // For older methods, we provide "laconica" base action

            $m->connect('api/laconica/version.:format',
                        array('action' => 'ApiStatusnetVersion',
                              'format' => '(xml|json)'));

            $m->connect('api/laconica/config.:format',
                        array('action' => 'ApiStatusnetConfig',
                              'format' => '(xml|json)'));
677

678 679 680 681 682 683 684 685 686
            // 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',
                              'id' => '[a-zA-Z0-9]+',
687
                              'format' => '(xml|json|rss|atom)'));
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747

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

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

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

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

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

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

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

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

            $m->connect('api/statusnet/groups/list/:id.:format',
                        array('action' => 'ApiGroupList',
                              'id' => '[a-zA-Z0-9]+',
                              'format' => '(xml|json|rss|atom)'));

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

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

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

            $m->connect('api/statusnet/groups/create.:format',
                        array('action' => 'ApiGroupCreate',
                              'format' => '(xml|json)'));
            // Tags
            $m->connect('api/statusnet/tags/timeline/:tag.:format',
                        array('action' => 'ApiTimelineTag',
748
                              'format' => '(xml|json|rss|atom)'));
749

750 751 752 753 754 755
            // media related
            $m->connect(
                'api/statusnet/media/upload',
                array('action' => 'ApiMediaUpload')
            );

756
            // search
757 758 759
            $m->connect('api/search.atom', array('action' => 'ApiSearchAtom'));
            $m->connect('api/search.json', array('action' => 'ApiSearchJSON'));
            $m->connect('api/trends.json', array('action' => 'ApiTrends'));
760

Evan Prodromou's avatar
Evan Prodromou committed
761
            $m->connect('api/oauth/request_token',
762
                        array('action' => 'ApiOauthRequestToken'));
Evan Prodromou's avatar
Evan Prodromou committed
763 764

            $m->connect('api/oauth/access_token',
765
                        array('action' => 'ApiOauthAccessToken'));
Evan Prodromou's avatar
Evan Prodromou committed
766 767

            $m->connect('api/oauth/authorize',
768
                        array('action' => 'ApiOauthAuthorize'));
Evan Prodromou's avatar
Evan Prodromou committed
769 770 771

            // Admin

Evan Prodromou's avatar
Evan Prodromou committed
772
            $m->connect('admin/site', array('action' => 'siteadminpanel'));
773
            $m->connect('admin/design', array('action' => 'designadminpanel'));
Zach Copley's avatar
Zach Copley committed
774
            $m->connect('admin/user', array('action' => 'useradminpanel'));
775
	        $m->connect('admin/access', array('action' => 'accessadminpanel'));
776
            $m->connect('admin/paths', array('action' => 'pathsadminpanel'));
777
            $m->connect('admin/sessions', array('action' => 'sessionsadminpanel'));
778
            $m->connect('admin/sitenotice', array('action' => 'sitenoticeadminpanel'));
779
            $m->connect('admin/snapshot', array('action' => 'snapshotadminpanel'));
780 781
            $m->connect('admin/license', array('action' => 'licenseadminpanel'));

782 783 784 785
            $m->connect('getfile/:filename',
                        array('action' => 'getfile'),
                        array('filename' => '[A-Za-z0-9._-]+'));

Evan Prodromou's avatar
Evan Prodromou committed
786
            // In the "root"
787

Evan Prodromou's avatar
Evan Prodromou committed
788 789
            if (common_config('singleuser', 'enabled')) {

790
                $nickname = User::singleUserNickname();
Evan Prodromou's avatar
Evan Prodromou committed
791 792 793

                foreach (array('subscriptions', 'subscribers',
                               'all', 'foaf', 'xrds',
Evan Prodromou's avatar
Evan Prodromou committed
794
                               'replies', 'microsummary', 'hcard') as $a) {
Evan Prodromou's avatar
Evan Prodromou committed
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
                    $m->connect($a,
                                array('action' => $a,
                                      'nickname' => $nickname));
                }

                foreach (array('subscriptions', 'subscribers') as $a) {
                    $m->connect($a.'/:tag',
                                array('action' => $a,
                                      'nickname' => $nickname),
                                array('tag' => '[a-zA-Z0-9]+'));
                }

                foreach (array('rss', 'groups') as $a) {
                    $m->connect($a,
                                array('action' => 'user'.$a,
                                      'nickname' => $nickname));
                }

                foreach (array('all', 'replies', 'favorites') as $a) {
                    $m->connect($a.'/rss',
                                array('action' => $a.'rss',
                                      'nickname' => $nickname));
                }

                $m->connect('favorites',
                            array('action' => 'showfavorites',
                                  'nickname' => $nickname));

                $m->connect('avatar/:size',
                            array('action' => 'avatarbynickname',
                                  'nickname' => $nickname),
                            array('size' => '(original|96|48|24)'));

                $m->connect('tag/:tag/rss',
                            array('action' => 'userrss',
                                  'nickname' => $nickname),
831
                            array('tag' => '[\pL\pN_\-\.]{1,64}'));
Evan Prodromou's avatar
Evan Prodromou committed
832 833 834 835

                $m->connect('tag/:tag',
                            array('action' => 'showstream',
                                  'nickname' => $nickname),
836
                            array('tag' => '[\pL\pN_\-\.]{1,64}'));
Evan Prodromou's avatar
Evan Prodromou committed
837

838 839 840 841
                $m->connect('rsd.xml',
                            array('action' => 'rsd',
                                  'nickname' => $nickname));

Evan Prodromou's avatar
Evan Prodromou committed
842 843 844 845 846 847 848 849 850 851 852 853
                $m->connect('',
                            array('action' => 'showstream',
                                  'nickname' => $nickname));
            } else {
                $m->connect('', array('action' => 'public'));
                $m->connect('rss', array('action' => 'publicrss'));
                $m->connect('featuredrss', array('action' => 'featuredrss'));
                $m->connect('favoritedrss', array('action' => 'favoritedrss'));
                $m->connect('featured/', array('action' => 'featured'));
                $m->connect('featured', array('action' => 'featured'));
                $m->connect('favorited/', array('action' => 'favorited'));
                $m->connect('favorited', array('action' => 'favorited'));
854
                $m->connect('rsd.xml', array('action' => 'rsd'));
Evan Prodromou's avatar
Evan Prodromou committed
855 856 857

                foreach (array('subscriptions', 'subscribers',
                               'nudge', 'all', 'foaf', 'xrds',
Evan Prodromou's avatar
Evan Prodromou committed
858
                               'replies', 'inbox', 'outbox', 'microsummary', 'hcard') as $a) {
Evan Prodromou's avatar
Evan Prodromou committed
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
                    $m->connect(':nickname/'.$a,
                                array('action' => $a),
                                array('nickname' => '[a-zA-Z0-9]{1,64}'));
                }

                foreach (array('subscriptions', 'subscribers') as $a) {
                    $m->connect(':nickname/'.$a.'/:tag',
                                array('action' => $a),
                                array('tag' => '[a-zA-Z0-9]+',
                                      'nickname' => '[a-zA-Z0-9]{1,64}'));
                }

                foreach (array('rss', 'groups') as $a) {
                    $m->connect(':nickname/'.$a,
                                array('action' => 'user'.$a),
                                array('nickname' => '[a-zA-Z0-9]{1,64}'));
                }

                foreach (array('all', 'replies', 'favorites') as $a) {
                    $m->connect(':nickname/'.$a.'/rss',
                                array('action' => $a.'rss'),
                                array('nickname' => '[a-zA-Z0-9]{1,64}'));
                }

                $m->connect(':nickname/favorites',
                            array('action' => 'showfavorites'),
885
                            array('nickname' => '[a-zA-Z0-9]{1,64}'));
886

Evan Prodromou's avatar
Evan Prodromou committed
887 888 889
                $m->connect(':nickname/avatar/:size',
                            array('action' => 'avatarbynickname'),
                            array('size' => '(original|96|48|24)',
890
                                  'nickname' => '[a-zA-Z0-9]{1,64}'));
891

Evan Prodromou's avatar
Evan Prodromou committed
892 893 894
                $m->connect(':nickname/tag/:tag/rss',
                            array('action' => 'userrss'),
                            array('nickname' => '[a-zA-Z0-9]{1,64}'),
895
                            array('tag' => '[\pL\pN_\-\.]{1,64}'));
Evan Prodromou's avatar
Evan Prodromou committed
896 897 898 899

                $m->connect(':nickname/tag/:tag',
                            array('action' => 'showstream'),
                            array('nickname' => '[a-zA-Z0-9]{1,64}'),
900
                            array('tag' => '[\pL\pN_\-\.]{1,64}'));
901

902 903 904 905
                $m->connect(':nickname/rsd.xml',
                            array('action' => 'rsd'),
                            array('nickname' => '[a-zA-Z0-9]{1,64}'));

Evan Prodromou's avatar
Evan Prodromou committed
906 907
                $m->connect(':nickname',
                            array('action' => 'showstream'),
908
                            array('nickname' => '[a-zA-Z0-9]{1,64}'));
909 910
            }

Evan Prodromou's avatar
Evan Prodromou committed
911
            // user stuff
912

913 914
            Event::handle('RouterInitialized', array($m));
        }
915

916
        return $m;
917 918 919 920
    }

    function map($path)
    {
921 922 923 924
        try {
            $match = $this->m->match($path);
        } catch (Net_URL_Mapper_InvalidException $e) {
            common_log(LOG_ERR, "Problem getting route for $path - " .
925
                       $e->getMessage());
926 927
            // TRANS: Client error on action trying to visit a non-existing page.
            $cac = new ClientErrorAction(_('Page not found.'), 404);
928 929 930 931
            $cac->showPage();
        }

        return $match;
932 933
    }

934
    function build($action, $args=null, $params=null, $fragment=null)
935 936 937 938
    {
        $action_arg = array('action' => $action);

        if ($args) {
Evan Prodromou's avatar
Evan Prodromou committed
939
            $args = array_merge($action_arg, $args);
940 941 942 943
        } else {
            $args = $action_arg;
        }

944
        $url = $this->m->generate($args, $params, $fragment);
945 946 947 948

        // 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...
949 950

        $qpos = strpos($url, '?');
951 952
        if ($qpos !== false) {
            $url = substr($url, 0, $qpos+1) .
953
              str_replace('?', '&', substr($url, $qpos+1));
954 955 956 957 958 959 960 961

            // @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) .
              str_replace('&amp;', '&', substr($url, $qpos+1));

962
        }
963

964
        return $url;
965
    }
Adrian Lang's avatar
Adrian Lang committed
966
}