]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/router.php
Merge branch 'testing' into 0.9.x
[quix0rs-gnu-social.git] / lib / router.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * URL routing utilities
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @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     exit(1);
32 }
33
34 require_once 'Net/URL/Mapper.php';
35
36 class StatusNet_URL_Mapper extends Net_URL_Mapper {
37
38     private static $_singleton = null;
39
40     private function __construct()
41     {
42     }
43
44     public static function getInstance($id = '__default__')
45     {
46         if (empty(self::$_singleton)) {
47             self::$_singleton = new StatusNet_URL_Mapper();
48         }
49         return self::$_singleton;
50     }
51
52     public function connect($path, $defaults = array(), $rules = array())
53     {
54         $result = null;
55         if (Event::handle('StartConnectPath', array(&$path, &$defaults, &$rules, &$result))) {
56             $result = parent::connect($path, $defaults, $rules);
57             Event::handle('EndConnectPath', array($path, $defaults, $rules, $result));
58         }
59         return $result;
60     }
61 }
62
63 /**
64  * URL Router
65  *
66  * Cheap wrapper around Net_URL_Mapper
67  *
68  * @category URL
69  * @package  StatusNet
70  * @author   Evan Prodromou <evan@status.net>
71  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
72  * @link     http://status.net/
73  */
74
75 class Router
76 {
77     var $m = null;
78     static $inst = null;
79     static $bare = array('requesttoken', 'accesstoken', 'userauthorization',
80                          'postnotice', 'updateprofile', 'finishremotesubscribe');
81
82     static function get()
83     {
84         if (!Router::$inst) {
85             Router::$inst = new Router();
86         }
87         return Router::$inst;
88     }
89
90     function __construct()
91     {
92         if (!$this->m) {
93             $this->m = $this->initialize();
94         }
95     }
96
97     function initialize()
98     {
99         $m = StatusNet_URL_Mapper::getInstance();
100
101         if (Event::handle('StartInitializeRouter', array(&$m))) {
102
103             $m->connect('robots.txt', array('action' => 'robotstxt'));
104
105             $m->connect('opensearch/people', array('action' => 'opensearch',
106                                                    'type' => 'people'));
107             $m->connect('opensearch/notice', array('action' => 'opensearch',
108                                                    'type' => 'notice'));
109
110             // docs
111
112             $m->connect('doc/:title', array('action' => 'doc'));
113
114             $m->connect('main/otp/:user_id/:token',
115                         array('action' => 'otp'),
116                         array('user_id' => '[0-9]+',
117                               'token' => '.+'));
118
119             // main stuff is repetitive
120
121             $main = array('login', 'logout', 'register', 'subscribe',
122                           'unsubscribe', 'confirmaddress', 'recoverpassword',
123                           'invite', 'favor', 'disfavor', 'sup',
124                           'block', 'unblock', 'subedit',
125                           'groupblock', 'groupunblock',
126                           'sandbox', 'unsandbox',
127                           'silence', 'unsilence',
128                           'grantrole', 'revokerole',
129                           'repeat',
130                           'deleteuser',
131                           'geocode',
132                           'version',
133                           );
134
135             foreach ($main as $a) {
136                 $m->connect('main/'.$a, array('action' => $a));
137             }
138
139             // Also need a block variant accepting ID on URL for mail links
140             $m->connect('main/block/:profileid',
141                         array('action' => 'block'),
142                         array('profileid' => '[0-9]+'));
143
144             $m->connect('main/sup/:seconds', array('action' => 'sup'),
145                         array('seconds' => '[0-9]+'));
146
147             $m->connect('main/tagother/:id', array('action' => 'tagother'));
148
149             $m->connect('main/oembed',
150                         array('action' => 'oembed'));
151
152             $m->connect('main/xrds',
153                         array('action' => 'publicxrds'));
154
155             // these take a code
156
157             foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
158                 $m->connect('main/'.$c.'/:code', array('action' => $c));
159             }
160
161             // exceptional
162
163             $m->connect('main/remote', array('action' => 'remotesubscribe'));
164             $m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '[A-Za-z0-9_-]+'));
165
166             foreach (Router::$bare as $action) {
167                 $m->connect('index.php?action=' . $action, array('action' => $action));
168             }
169
170             // settings
171
172             foreach (array('profile', 'avatar', 'password', 'im', 'oauthconnections',
173                            'oauthapps', 'email', 'sms', 'userdesign', 'other') as $s) {
174                 $m->connect('settings/'.$s, array('action' => $s.'settings'));
175             }
176
177             $m->connect('settings/oauthapps/show/:id',
178                 array('action' => 'showapplication'),
179                 array('id' => '[0-9]+')
180             );
181             $m->connect('settings/oauthapps/new',
182                 array('action' => 'newapplication')
183             );
184             $m->connect('settings/oauthapps/edit/:id',
185                 array('action' => 'editapplication'),
186                 array('id' => '[0-9]+')
187             );
188             $m->connect('settings/oauthapps/delete/:id',
189                 array('action' => 'deleteapplication'),
190                 array('id' => '[0-9]+')
191             );
192
193             // search
194
195             foreach (array('group', 'people', 'notice') as $s) {
196                 $m->connect('search/'.$s, array('action' => $s.'search'));
197                 $m->connect('search/'.$s.'?q=:q',
198                             array('action' => $s.'search'),
199                             array('q' => '.+'));
200             }
201
202             // The second of these is needed to make the link work correctly
203             // when inserted into the page. The first is needed to match the
204             // route on the way in. Seems to be another Net_URL_Mapper bug to me.
205             $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
206             $m->connect('search/notice/rss?q=:q', array('action' => 'noticesearchrss'),
207                         array('q' => '.+'));
208
209             $m->connect('attachment/:attachment',
210                         array('action' => 'attachment'),
211                         array('attachment' => '[0-9]+'));
212
213             $m->connect('attachment/:attachment/ajax',
214                         array('action' => 'attachment_ajax'),
215                         array('attachment' => '[0-9]+'));
216
217             $m->connect('attachment/:attachment/thumbnail',
218                         array('action' => 'attachment_thumbnail'),
219                         array('attachment' => '[0-9]+'));
220
221             $m->connect('notice/new', array('action' => 'newnotice'));
222             $m->connect('notice/new?replyto=:replyto',
223                         array('action' => 'newnotice'),
224                         array('replyto' => '[A-Za-z0-9_-]+'));
225             $m->connect('notice/new?replyto=:replyto&inreplyto=:inreplyto',
226                         array('action' => 'newnotice'),
227                         array('replyto' => '[A-Za-z0-9_-]+'),
228                         array('inreplyto' => '[0-9]+'));
229
230             $m->connect('notice/:notice/file',
231                         array('action' => 'file'),
232                         array('notice' => '[0-9]+'));
233
234             $m->connect('notice/:notice',
235                         array('action' => 'shownotice'),
236                         array('notice' => '[0-9]+'));
237             $m->connect('notice/delete', array('action' => 'deletenotice'));
238             $m->connect('notice/delete/:notice',
239                         array('action' => 'deletenotice'),
240                         array('notice' => '[0-9]+'));
241
242             $m->connect('bookmarklet/new', array('action' => 'bookmarklet'));
243
244             // conversation
245
246             $m->connect('conversation/:id',
247                         array('action' => 'conversation'),
248                         array('id' => '[0-9]+'));
249
250             $m->connect('message/new', array('action' => 'newmessage'));
251             $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
252             $m->connect('message/:message',
253                         array('action' => 'showmessage'),
254                         array('message' => '[0-9]+'));
255
256             $m->connect('user/:id',
257                         array('action' => 'userbyid'),
258                         array('id' => '[0-9]+'));
259
260             $m->connect('tags/', array('action' => 'publictagcloud'));
261             $m->connect('tag/', array('action' => 'publictagcloud'));
262             $m->connect('tags', array('action' => 'publictagcloud'));
263             $m->connect('tag', array('action' => 'publictagcloud'));
264             $m->connect('tag/:tag/rss',
265                         array('action' => 'tagrss'),
266                         array('tag' => '[\pL\pN_\-\.]{1,64}'));
267             $m->connect('tag/:tag',
268                         array('action' => 'tag'),
269                         array('tag' => '[\pL\pN_\-\.]{1,64}'));
270
271             $m->connect('peopletag/:tag',
272                         array('action' => 'peopletag'),
273                         array('tag' => '[a-zA-Z0-9]+'));
274
275             // groups
276
277             $m->connect('group/new', array('action' => 'newgroup'));
278
279             foreach (array('edit', 'join', 'leave') as $v) {
280                 $m->connect('group/:nickname/'.$v,
281                             array('action' => $v.'group'),
282                             array('nickname' => '[a-zA-Z0-9]+'));
283                 $m->connect('group/:id/id/'.$v,
284                             array('action' => $v.'group'),
285                             array('id' => '[0-9]+'));
286             }
287
288             foreach (array('members', 'logo', 'rss', 'designsettings') as $n) {
289                 $m->connect('group/:nickname/'.$n,
290                             array('action' => 'group'.$n),
291                             array('nickname' => '[a-zA-Z0-9]+'));
292             }
293
294             $m->connect('group/:nickname/foaf',
295                         array('action' => 'foafgroup'),
296                         array('nickname' => '[a-zA-Z0-9]+'));
297
298             $m->connect('group/:nickname/blocked',
299                         array('action' => 'blockedfromgroup'),
300                         array('nickname' => '[a-zA-Z0-9]+'));
301
302             $m->connect('group/:nickname/makeadmin',
303                         array('action' => 'makeadmin'),
304                         array('nickname' => '[a-zA-Z0-9]+'));
305
306             $m->connect('group/:id/id',
307                         array('action' => 'groupbyid'),
308                         array('id' => '[0-9]+'));
309
310             $m->connect('group/:nickname',
311                         array('action' => 'showgroup'),
312                         array('nickname' => '[a-zA-Z0-9]+'));
313
314             $m->connect('group/', array('action' => 'groups'));
315             $m->connect('group', array('action' => 'groups'));
316             $m->connect('groups/', array('action' => 'groups'));
317             $m->connect('groups', array('action' => 'groups'));
318
319             // Twitter-compatible API
320
321             // statuses API
322
323             $m->connect('api/statuses/public_timeline.:format',
324                         array('action' => 'ApiTimelinePublic',
325                               'format' => '(xml|json|rss|atom)'));
326
327             $m->connect('api/statuses/friends_timeline.:format',
328                         array('action' => 'ApiTimelineFriends',
329                               'format' => '(xml|json|rss|atom)'));
330
331             $m->connect('api/statuses/friends_timeline/:id.:format',
332                         array('action' => 'ApiTimelineFriends',
333                               'id' => '[a-zA-Z0-9]+',
334                               'format' => '(xml|json|rss|atom)'));
335
336             $m->connect('api/statuses/home_timeline.:format',
337                         array('action' => 'ApiTimelineHome',
338                               'format' => '(xml|json|rss|atom)'));
339
340             $m->connect('api/statuses/home_timeline/:id.:format',
341                         array('action' => 'ApiTimelineHome',
342                               'id' => '[a-zA-Z0-9]+',
343                               'format' => '(xml|json|rss|atom)'));
344
345             $m->connect('api/statuses/user_timeline.:format',
346                         array('action' => 'ApiTimelineUser',
347                               'format' => '(xml|json|rss|atom)'));
348
349             $m->connect('api/statuses/user_timeline/:id.:format',
350                         array('action' => 'ApiTimelineUser',
351                               'id' => '[a-zA-Z0-9]+',
352                               'format' => '(xml|json|rss|atom)'));
353
354             $m->connect('api/statuses/mentions.:format',
355                         array('action' => 'ApiTimelineMentions',
356                               'format' => '(xml|json|rss|atom)'));
357
358             $m->connect('api/statuses/mentions/:id.:format',
359                         array('action' => 'ApiTimelineMentions',
360                               'id' => '[a-zA-Z0-9]+',
361                               'format' => '(xml|json|rss|atom)'));
362
363             $m->connect('api/statuses/replies.:format',
364                         array('action' => 'ApiTimelineMentions',
365                               'format' => '(xml|json|rss|atom)'));
366
367             $m->connect('api/statuses/replies/:id.:format',
368                         array('action' => 'ApiTimelineMentions',
369                               'id' => '[a-zA-Z0-9]+',
370                               'format' => '(xml|json|rss|atom)'));
371
372             $m->connect('api/statuses/retweeted_by_me.:format',
373                         array('action' => 'ApiTimelineRetweetedByMe',
374                               'format' => '(xml|json|atom)'));
375
376             $m->connect('api/statuses/retweeted_to_me.:format',
377                         array('action' => 'ApiTimelineRetweetedToMe',
378                               'format' => '(xml|json|atom)'));
379
380             $m->connect('api/statuses/retweets_of_me.:format',
381                         array('action' => 'ApiTimelineRetweetsOfMe',
382                               'format' => '(xml|json|atom)'));
383
384             $m->connect('api/statuses/friends.:format',
385                         array('action' => 'ApiUserFriends',
386                               'format' => '(xml|json)'));
387
388             $m->connect('api/statuses/friends/:id.:format',
389                         array('action' => 'ApiUserFriends',
390                               'id' => '[a-zA-Z0-9]+',
391                               'format' => '(xml|json)'));
392
393             $m->connect('api/statuses/followers.:format',
394                         array('action' => 'ApiUserFollowers',
395                               'format' => '(xml|json)'));
396
397             $m->connect('api/statuses/followers/:id.:format',
398                         array('action' => 'ApiUserFollowers',
399                               'id' => '[a-zA-Z0-9]+',
400                               'format' => '(xml|json)'));
401
402             $m->connect('api/statuses/show.:format',
403                         array('action' => 'ApiStatusesShow',
404                               'format' => '(xml|json)'));
405
406             $m->connect('api/statuses/show/:id.:format',
407                         array('action' => 'ApiStatusesShow',
408                               'id' => '[0-9]+',
409                               'format' => '(xml|json)'));
410
411             $m->connect('api/statuses/update.:format',
412                         array('action' => 'ApiStatusesUpdate',
413                               'format' => '(xml|json)'));
414
415             $m->connect('api/statuses/destroy.:format',
416                         array('action' => 'ApiStatusesDestroy',
417                               'format' => '(xml|json)'));
418
419             $m->connect('api/statuses/destroy/:id.:format',
420                         array('action' => 'ApiStatusesDestroy',
421                               'id' => '[0-9]+',
422                               'format' => '(xml|json)'));
423
424             $m->connect('api/statuses/retweet/:id.:format',
425                         array('action' => 'ApiStatusesRetweet',
426                               'id' => '[0-9]+',
427                               'format' => '(xml|json)'));
428
429             $m->connect('api/statuses/retweets/:id.:format',
430                         array('action' => 'ApiStatusesRetweets',
431                               'id' => '[0-9]+',
432                               'format' => '(xml|json)'));
433
434             // users
435
436             $m->connect('api/users/show.:format',
437                         array('action' => 'ApiUserShow',
438                               'format' => '(xml|json)'));
439
440             $m->connect('api/users/show/:id.:format',
441                         array('action' => 'ApiUserShow',
442                               'id' => '[a-zA-Z0-9]+',
443                               'format' => '(xml|json)'));
444
445             // direct messages
446
447             $m->connect('api/direct_messages.:format',
448                         array('action' => 'ApiDirectMessage',
449                               'format' => '(xml|json|rss|atom)'));
450
451             $m->connect('api/direct_messages/sent.:format',
452                         array('action' => 'ApiDirectMessage',
453                               'format' => '(xml|json|rss|atom)',
454                               'sent' => true));
455
456             $m->connect('api/direct_messages/new.:format',
457                         array('action' => 'ApiDirectMessageNew',
458                               'format' => '(xml|json)'));
459
460             // friendships
461
462             $m->connect('api/friendships/show.:format',
463                         array('action' => 'ApiFriendshipsShow',
464                               'format' => '(xml|json)'));
465
466             $m->connect('api/friendships/exists.:format',
467                         array('action' => 'ApiFriendshipsExists',
468                               'format' => '(xml|json)'));
469
470             $m->connect('api/friendships/create.:format',
471                         array('action' => 'ApiFriendshipsCreate',
472                               'format' => '(xml|json)'));
473
474             $m->connect('api/friendships/destroy.:format',
475                         array('action' => 'ApiFriendshipsDestroy',
476                               'format' => '(xml|json)'));
477
478             $m->connect('api/friendships/create/:id.:format',
479                         array('action' => 'ApiFriendshipsCreate',
480                               'id' => '[a-zA-Z0-9]+',
481                               'format' => '(xml|json)'));
482
483             $m->connect('api/friendships/destroy/:id.:format',
484                         array('action' => 'ApiFriendshipsDestroy',
485                               'id' => '[a-zA-Z0-9]+',
486                               'format' => '(xml|json)'));
487
488             // Social graph
489
490             $m->connect('api/friends/ids/:id.:format',
491                         array('action' => 'apiuserfriends',
492                               'ids_only' => true));
493
494             $m->connect('api/followers/ids/:id.:format',
495                         array('action' => 'apiuserfollowers',
496                               'ids_only' => true));
497
498             $m->connect('api/friends/ids.:format',
499                         array('action' => 'apiuserfriends',
500                               'ids_only' => true));
501
502             $m->connect('api/followers/ids.:format',
503                         array('action' => 'apiuserfollowers',
504                               'ids_only' => true));
505
506             // account
507
508             $m->connect('api/account/verify_credentials.:format',
509                         array('action' => 'ApiAccountVerifyCredentials'));
510
511             $m->connect('api/account/update_profile.:format',
512                         array('action' => 'ApiAccountUpdateProfile'));
513
514             $m->connect('api/account/update_profile_image.:format',
515                         array('action' => 'ApiAccountUpdateProfileImage'));
516
517             $m->connect('api/account/update_profile_background_image.:format',
518                         array('action' => 'ApiAccountUpdateProfileBackgroundImage'));
519
520             $m->connect('api/account/update_profile_colors.:format',
521                         array('action' => 'ApiAccountUpdateProfileColors'));
522
523             $m->connect('api/account/update_delivery_device.:format',
524                         array('action' => 'ApiAccountUpdateDeliveryDevice'));
525
526             // special case where verify_credentials is called w/out a format
527
528             $m->connect('api/account/verify_credentials',
529                         array('action' => 'ApiAccountVerifyCredentials'));
530
531             $m->connect('api/account/rate_limit_status.:format',
532                         array('action' => 'ApiAccountRateLimitStatus'));
533
534             // favorites
535
536             $m->connect('api/favorites.:format',
537                         array('action' => 'ApiTimelineFavorites',
538                               'format' => '(xml|json|rss|atom)'));
539
540             $m->connect('api/favorites/:id.:format',
541                         array('action' => 'ApiTimelineFavorites',
542                               'id' => '[a-zA-Z0-9]+',
543                               'format' => '(xml|json|rss|atom)'));
544
545             $m->connect('api/favorites/create/:id.:format',
546                         array('action' => 'ApiFavoriteCreate',
547                               'id' => '[a-zA-Z0-9]+',
548                               'format' => '(xml|json)'));
549
550             $m->connect('api/favorites/destroy/:id.:format',
551                         array('action' => 'ApiFavoriteDestroy',
552                               'id' => '[a-zA-Z0-9]+',
553                               'format' => '(xml|json)'));
554             // blocks
555
556             $m->connect('api/blocks/create/:id.:format',
557                         array('action' => 'ApiBlockCreate',
558                               'id' => '[a-zA-Z0-9]+',
559                               'format' => '(xml|json)'));
560
561             $m->connect('api/blocks/destroy/:id.:format',
562                         array('action' => 'ApiBlockDestroy',
563                               'id' => '[a-zA-Z0-9]+',
564                               'format' => '(xml|json)'));
565             // help
566
567             $m->connect('api/help/test.:format',
568                         array('action' => 'ApiHelpTest',
569                               'format' => '(xml|json)'));
570
571             // statusnet
572
573             $m->connect('api/statusnet/version.:format',
574                         array('action' => 'ApiStatusnetVersion',
575                               'format' => '(xml|json)'));
576
577             $m->connect('api/statusnet/config.:format',
578                         array('action' => 'ApiStatusnetConfig',
579                               'format' => '(xml|json)'));
580
581             // For older methods, we provide "laconica" base action
582
583             $m->connect('api/laconica/version.:format',
584                         array('action' => 'ApiStatusnetVersion',
585                               'format' => '(xml|json)'));
586
587             $m->connect('api/laconica/config.:format',
588                         array('action' => 'ApiStatusnetConfig',
589                               'format' => '(xml|json)'));
590
591             // Groups and tags are newer than 0.8.1 so no backward-compatibility
592             // necessary
593
594             // Groups
595             //'list' has to be handled differently, as php will not allow a method to be named 'list'
596
597             $m->connect('api/statusnet/groups/timeline/:id.:format',
598                         array('action' => 'ApiTimelineGroup',
599                               'id' => '[a-zA-Z0-9]+',
600                               'format' => '(xml|json|rss|atom)'));
601
602             $m->connect('api/statusnet/groups/show.:format',
603                         array('action' => 'ApiGroupShow',
604                               'format' => '(xml|json)'));
605
606             $m->connect('api/statusnet/groups/show/:id.:format',
607                         array('action' => 'ApiGroupShow',
608                               'id' => '[a-zA-Z0-9]+',
609                               'format' => '(xml|json)'));
610
611             $m->connect('api/statusnet/groups/join.:format',
612                         array('action' => 'ApiGroupJoin',
613                               'id' => '[a-zA-Z0-9]+',
614                               'format' => '(xml|json)'));
615
616             $m->connect('api/statusnet/groups/join/:id.:format',
617                         array('action' => 'ApiGroupJoin',
618                               'format' => '(xml|json)'));
619
620             $m->connect('api/statusnet/groups/leave.:format',
621                         array('action' => 'ApiGroupLeave',
622                               'id' => '[a-zA-Z0-9]+',
623                               'format' => '(xml|json)'));
624
625             $m->connect('api/statusnet/groups/leave/:id.:format',
626                         array('action' => 'ApiGroupLeave',
627                               'format' => '(xml|json)'));
628
629             $m->connect('api/statusnet/groups/is_member.:format',
630                         array('action' => 'ApiGroupIsMember',
631                               'format' => '(xml|json)'));
632
633             $m->connect('api/statusnet/groups/list.:format',
634                         array('action' => 'ApiGroupList',
635                               'format' => '(xml|json|rss|atom)'));
636
637             $m->connect('api/statusnet/groups/list/:id.:format',
638                         array('action' => 'ApiGroupList',
639                               'id' => '[a-zA-Z0-9]+',
640                               'format' => '(xml|json|rss|atom)'));
641
642             $m->connect('api/statusnet/groups/list_all.:format',
643                         array('action' => 'ApiGroupListAll',
644                               'format' => '(xml|json|rss|atom)'));
645
646             $m->connect('api/statusnet/groups/membership.:format',
647                         array('action' => 'ApiGroupMembership',
648                               'format' => '(xml|json)'));
649
650             $m->connect('api/statusnet/groups/membership/:id.:format',
651                         array('action' => 'ApiGroupMembership',
652                               'id' => '[a-zA-Z0-9]+',
653                               'format' => '(xml|json)'));
654
655             $m->connect('api/statusnet/groups/create.:format',
656                         array('action' => 'ApiGroupCreate',
657                               'format' => '(xml|json)'));
658             // Tags
659             $m->connect('api/statusnet/tags/timeline/:tag.:format',
660                         array('action' => 'ApiTimelineTag',
661                               'format' => '(xml|json|rss|atom)'));
662
663             // media related
664             $m->connect(
665                 'api/statusnet/media/upload',
666                 array('action' => 'ApiMediaUpload')
667             );
668
669             // search
670             $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
671             $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
672             $m->connect('api/trends.json', array('action' => 'twitapitrends'));
673
674             $m->connect('api/oauth/request_token',
675                         array('action' => 'apioauthrequesttoken'));
676
677             $m->connect('api/oauth/access_token',
678                         array('action' => 'apioauthaccesstoken'));
679
680             $m->connect('api/oauth/authorize',
681                         array('action' => 'apioauthauthorize'));
682
683             // Admin
684
685             $m->connect('admin/site', array('action' => 'siteadminpanel'));
686             $m->connect('admin/design', array('action' => 'designadminpanel'));
687             $m->connect('admin/user', array('action' => 'useradminpanel'));
688                 $m->connect('admin/access', array('action' => 'accessadminpanel'));
689             $m->connect('admin/paths', array('action' => 'pathsadminpanel'));
690             $m->connect('admin/sessions', array('action' => 'sessionsadminpanel'));
691             $m->connect('admin/sitenotice', array('action' => 'sitenoticeadminpanel'));
692             $m->connect('admin/snapshot', array('action' => 'snapshotadminpanel'));
693
694             $m->connect('getfile/:filename',
695                         array('action' => 'getfile'),
696                         array('filename' => '[A-Za-z0-9._-]+'));
697
698             // In the "root"
699
700             if (common_config('singleuser', 'enabled')) {
701
702                 $user = User::siteOwner();
703
704                 if (!empty($user)) {
705                     $nickname = $user->nickname;
706                 } else {
707                     $nickname = common_config('singleuser', 'nickname');
708                     if (empty($nickname)) {
709                         throw new ServerException(_("No single user defined for single-user mode."));
710                     }
711                 }
712
713                 foreach (array('subscriptions', 'subscribers',
714                                'all', 'foaf', 'xrds',
715                                'replies', 'microsummary', 'hcard') as $a) {
716                     $m->connect($a,
717                                 array('action' => $a,
718                                       'nickname' => $nickname));
719                 }
720
721                 foreach (array('subscriptions', 'subscribers') as $a) {
722                     $m->connect($a.'/:tag',
723                                 array('action' => $a,
724                                       'nickname' => $nickname),
725                                 array('tag' => '[a-zA-Z0-9]+'));
726                 }
727
728                 foreach (array('rss', 'groups') as $a) {
729                     $m->connect($a,
730                                 array('action' => 'user'.$a,
731                                       'nickname' => $nickname));
732                 }
733
734                 foreach (array('all', 'replies', 'favorites') as $a) {
735                     $m->connect($a.'/rss',
736                                 array('action' => $a.'rss',
737                                       'nickname' => $nickname));
738                 }
739
740                 $m->connect('favorites',
741                             array('action' => 'showfavorites',
742                                   'nickname' => $nickname));
743
744                 $m->connect('avatar/:size',
745                             array('action' => 'avatarbynickname',
746                                   'nickname' => $nickname),
747                             array('size' => '(original|96|48|24)'));
748
749                 $m->connect('tag/:tag/rss',
750                             array('action' => 'userrss',
751                                   'nickname' => $nickname),
752                             array('tag' => '[\pL\pN_\-\.]{1,64}'));
753
754                 $m->connect('tag/:tag',
755                             array('action' => 'showstream',
756                                   'nickname' => $nickname),
757                             array('tag' => '[\pL\pN_\-\.]{1,64}'));
758
759                 $m->connect('rsd.xml',
760                             array('action' => 'rsd',
761                                   'nickname' => $nickname));
762
763                 $m->connect('',
764                             array('action' => 'showstream',
765                                   'nickname' => $nickname));
766
767             } else {
768
769                 $m->connect('', array('action' => 'public'));
770                 $m->connect('rss', array('action' => 'publicrss'));
771                 $m->connect('featuredrss', array('action' => 'featuredrss'));
772                 $m->connect('favoritedrss', array('action' => 'favoritedrss'));
773                 $m->connect('featured/', array('action' => 'featured'));
774                 $m->connect('featured', array('action' => 'featured'));
775                 $m->connect('favorited/', array('action' => 'favorited'));
776                 $m->connect('favorited', array('action' => 'favorited'));
777                 $m->connect('rsd.xml', array('action' => 'rsd'));
778
779                 foreach (array('subscriptions', 'subscribers',
780                                'nudge', 'all', 'foaf', 'xrds',
781                                'replies', 'inbox', 'outbox', 'microsummary', 'hcard') as $a) {
782                     $m->connect(':nickname/'.$a,
783                                 array('action' => $a),
784                                 array('nickname' => '[a-zA-Z0-9]{1,64}'));
785                 }
786
787                 foreach (array('subscriptions', 'subscribers') as $a) {
788                     $m->connect(':nickname/'.$a.'/:tag',
789                                 array('action' => $a),
790                                 array('tag' => '[a-zA-Z0-9]+',
791                                       'nickname' => '[a-zA-Z0-9]{1,64}'));
792                 }
793
794                 foreach (array('rss', 'groups') as $a) {
795                     $m->connect(':nickname/'.$a,
796                                 array('action' => 'user'.$a),
797                                 array('nickname' => '[a-zA-Z0-9]{1,64}'));
798                 }
799
800                 foreach (array('all', 'replies', 'favorites') as $a) {
801                     $m->connect(':nickname/'.$a.'/rss',
802                                 array('action' => $a.'rss'),
803                                 array('nickname' => '[a-zA-Z0-9]{1,64}'));
804                 }
805
806                 $m->connect(':nickname/favorites',
807                             array('action' => 'showfavorites'),
808                             array('nickname' => '[a-zA-Z0-9]{1,64}'));
809
810                 $m->connect(':nickname/avatar/:size',
811                             array('action' => 'avatarbynickname'),
812                             array('size' => '(original|96|48|24)',
813                                   'nickname' => '[a-zA-Z0-9]{1,64}'));
814
815                 $m->connect(':nickname/tag/:tag/rss',
816                             array('action' => 'userrss'),
817                             array('nickname' => '[a-zA-Z0-9]{1,64}'),
818                             array('tag' => '[\pL\pN_\-\.]{1,64}'));
819
820                 $m->connect(':nickname/tag/:tag',
821                             array('action' => 'showstream'),
822                             array('nickname' => '[a-zA-Z0-9]{1,64}'),
823                             array('tag' => '[\pL\pN_\-\.]{1,64}'));
824
825                 $m->connect(':nickname/rsd.xml',
826                             array('action' => 'rsd'),
827                             array('nickname' => '[a-zA-Z0-9]{1,64}'));
828
829                 $m->connect(':nickname',
830                             array('action' => 'showstream'),
831                             array('nickname' => '[a-zA-Z0-9]{1,64}'));
832             }
833
834             // user stuff
835
836             Event::handle('RouterInitialized', array($m));
837         }
838
839         return $m;
840     }
841
842     function map($path)
843     {
844         try {
845             $match = $this->m->match($path);
846         } catch (Net_URL_Mapper_InvalidException $e) {
847             common_log(LOG_ERR, "Problem getting route for $path - " .
848                        $e->getMessage());
849             $cac = new ClientErrorAction("Page not found.", 404);
850             $cac->showPage();
851         }
852
853         return $match;
854     }
855
856     function build($action, $args=null, $params=null, $fragment=null)
857     {
858         $action_arg = array('action' => $action);
859
860         if ($args) {
861             $args = array_merge($action_arg, $args);
862         } else {
863             $args = $action_arg;
864         }
865
866         $url = $this->m->generate($args, $params, $fragment);
867
868         // Due to a bug in the Net_URL_Mapper code, the returned URL may
869         // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
870         // repair that here rather than modifying the upstream code...
871
872         $qpos = strpos($url, '?');
873         if ($qpos !== false) {
874             $url = substr($url, 0, $qpos+1) .
875               str_replace('?', '&', substr($url, $qpos+1));
876         }
877         return $url;
878     }
879 }