]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/router.php
Merge branch 'master' of git@gitorious.org:statusnet/mainline
[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 /**
37  * URL Router
38  *
39  * Cheap wrapper around Net_URL_Mapper
40  *
41  * @category URL
42  * @package  StatusNet
43  * @author   Evan Prodromou <evan@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
46  */
47
48 class Router
49 {
50     var $m = null;
51     static $inst = null;
52     static $bare = array('requesttoken', 'accesstoken', 'userauthorization',
53                          'postnotice', 'updateprofile', 'finishremotesubscribe');
54
55     static function get()
56     {
57         if (!Router::$inst) {
58             Router::$inst = new Router();
59         }
60         return Router::$inst;
61     }
62
63     function __construct()
64     {
65         if (!$this->m) {
66             $this->m = $this->initialize();
67         }
68     }
69
70     function initialize()
71     {
72         $m = Net_URL_Mapper::getInstance();
73
74         if (Event::handle('StartInitializeRouter', array(&$m))) {
75
76             // In the "root"
77
78             $m->connect('', array('action' => 'public'));
79             $m->connect('rss', array('action' => 'publicrss'));
80             $m->connect('featuredrss', array('action' => 'featuredrss'));
81             $m->connect('favoritedrss', array('action' => 'favoritedrss'));
82             $m->connect('opensearch/people', array('action' => 'opensearch',
83                                                    'type' => 'people'));
84             $m->connect('opensearch/notice', array('action' => 'opensearch',
85                                                    'type' => 'notice'));
86
87             // docs
88
89             $m->connect('doc/:title', array('action' => 'doc'));
90
91             // main stuff is repetitive
92
93             $main = array('login', 'logout', 'register', 'subscribe',
94                           'unsubscribe', 'confirmaddress', 'recoverpassword',
95                           'invite', 'favor', 'disfavor', 'sup',
96                           'block', 'unblock', 'subedit',
97                           'groupblock', 'groupunblock',
98                           'sandbox', 'unsandbox',
99                           'silence', 'unsilence',
100                           'deleteuser');
101
102             foreach ($main as $a) {
103                 $m->connect('main/'.$a, array('action' => $a));
104             }
105
106             $m->connect('main/sup/:seconds', array('action' => 'sup'),
107                         array('seconds' => '[0-9]+'));
108
109             $m->connect('main/tagother/:id', array('action' => 'tagother'));
110
111             $m->connect('main/oembed',
112                         array('action' => 'oembed'));
113
114             $m->connect('main/xrds',
115                         array('action' => 'publicxrds'));
116
117             // these take a code
118
119             foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
120                 $m->connect('main/'.$c.'/:code', array('action' => $c));
121             }
122
123             // exceptional
124
125             $m->connect('main/remote', array('action' => 'remotesubscribe'));
126             $m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '[A-Za-z0-9_-]+'));
127
128             foreach (Router::$bare as $action) {
129                 $m->connect('index.php?action=' . $action, array('action' => $action));
130             }
131
132             // settings
133
134             foreach (array('profile', 'avatar', 'password', 'im',
135                            'email', 'sms', 'userdesign', 'other') as $s) {
136                 $m->connect('settings/'.$s, array('action' => $s.'settings'));
137             }
138
139             // search
140
141             foreach (array('group', 'people', 'notice') as $s) {
142                 $m->connect('search/'.$s, array('action' => $s.'search'));
143                 $m->connect('search/'.$s.'?q=:q',
144                             array('action' => $s.'search'),
145                             array('q' => '.+'));
146             }
147
148             // The second of these is needed to make the link work correctly
149             // when inserted into the page. The first is needed to match the
150             // route on the way in. Seems to be another Net_URL_Mapper bug to me.
151             $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
152             $m->connect('search/notice/rss?q=:q', array('action' => 'noticesearchrss'),
153                         array('q' => '.+'));
154
155             $m->connect('attachment/:attachment',
156                         array('action' => 'attachment'),
157                         array('attachment' => '[0-9]+'));
158
159             $m->connect('attachment/:attachment/ajax',
160                         array('action' => 'attachment_ajax'),
161                         array('attachment' => '[0-9]+'));
162
163             $m->connect('attachment/:attachment/thumbnail',
164                         array('action' => 'attachment_thumbnail'),
165                         array('attachment' => '[0-9]+'));
166
167             $m->connect('notice/new', array('action' => 'newnotice'));
168             $m->connect('notice/new?replyto=:replyto',
169                         array('action' => 'newnotice'),
170                         array('replyto' => '[A-Za-z0-9_-]+'));
171             $m->connect('notice/new?replyto=:replyto&inreplyto=:inreplyto',
172                         array('action' => 'newnotice'),
173                         array('replyto' => '[A-Za-z0-9_-]+'),
174                         array('inreplyto' => '[0-9]+'));
175
176             $m->connect('notice/:notice/file',
177                         array('action' => 'file'),
178                         array('notice' => '[0-9]+'));
179
180             $m->connect('notice/:notice',
181                         array('action' => 'shownotice'),
182                         array('notice' => '[0-9]+'));
183             $m->connect('notice/delete', array('action' => 'deletenotice'));
184             $m->connect('notice/delete/:notice',
185                         array('action' => 'deletenotice'),
186                         array('notice' => '[0-9]+'));
187
188             $m->connect('bookmarklet/new', array('action' => 'bookmarklet'));
189
190             // conversation
191
192             $m->connect('conversation/:id',
193                         array('action' => 'conversation'),
194                         array('id' => '[0-9]+'));
195
196             $m->connect('message/new', array('action' => 'newmessage'));
197             $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
198             $m->connect('message/:message',
199                         array('action' => 'showmessage'),
200                         array('message' => '[0-9]+'));
201
202             $m->connect('user/:id',
203                         array('action' => 'userbyid'),
204                         array('id' => '[0-9]+'));
205
206             $m->connect('tags/', array('action' => 'publictagcloud'));
207             $m->connect('tag/', array('action' => 'publictagcloud'));
208             $m->connect('tags', array('action' => 'publictagcloud'));
209             $m->connect('tag', array('action' => 'publictagcloud'));
210             $m->connect('tag/:tag/rss',
211                         array('action' => 'tagrss'),
212                         array('tag' => '[a-zA-Z0-9]+'));
213             $m->connect('tag/:tag',
214                         array('action' => 'tag'),
215                         array('tag' => '[\pL\pN_\-\.]{1,64}'));
216
217             $m->connect('peopletag/:tag',
218                         array('action' => 'peopletag'),
219                         array('tag' => '[a-zA-Z0-9]+'));
220
221             $m->connect('featured/', array('action' => 'featured'));
222             $m->connect('featured', array('action' => 'featured'));
223             $m->connect('favorited/', array('action' => 'favorited'));
224             $m->connect('favorited', array('action' => 'favorited'));
225
226             // groups
227
228             $m->connect('group/new', array('action' => 'newgroup'));
229
230             foreach (array('edit', 'join', 'leave') as $v) {
231                 $m->connect('group/:nickname/'.$v,
232                             array('action' => $v.'group'),
233                             array('nickname' => '[a-zA-Z0-9]+'));
234             }
235
236             foreach (array('members', 'logo', 'rss', 'designsettings') as $n) {
237                 $m->connect('group/:nickname/'.$n,
238                             array('action' => 'group'.$n),
239                             array('nickname' => '[a-zA-Z0-9]+'));
240             }
241
242             $m->connect('group/:nickname/foaf',
243                         array('action' => 'foafgroup'),
244                         array('nickname' => '[a-zA-Z0-9]+'));
245
246             $m->connect('group/:nickname/blocked',
247                         array('action' => 'blockedfromgroup'),
248                         array('nickname' => '[a-zA-Z0-9]+'));
249
250             $m->connect('group/:nickname/makeadmin',
251                         array('action' => 'makeadmin'),
252                         array('nickname' => '[a-zA-Z0-9]+'));
253
254             $m->connect('group/:id/id',
255                         array('action' => 'groupbyid'),
256                         array('id' => '[0-9]+'));
257
258             $m->connect('group/:nickname',
259                         array('action' => 'showgroup'),
260                         array('nickname' => '[a-zA-Z0-9]+'));
261
262             $m->connect('group/', array('action' => 'groups'));
263             $m->connect('group', array('action' => 'groups'));
264             $m->connect('groups/', array('action' => 'groups'));
265             $m->connect('groups', array('action' => 'groups'));
266
267             // Twitter-compatible API
268
269             // statuses API
270
271             $m->connect('api/statuses/public_timeline.:format',
272                         array('action' => 'ApiTimelinePublic',
273                               'format' => '(xml|json|rss|atom)'));
274
275             $m->connect('api/statuses/friends_timeline.:format',
276                         array('action' => 'ApiTimelineFriends',
277                               'format' => '(xml|json|rss|atom)'));
278
279             $m->connect('api/statuses/friends_timeline/:id.:format',
280                         array('action' => 'ApiTimelineFriends',
281                               'id' => '[a-zA-Z0-9]+',
282                               'format' => '(xml|json|rss|atom)'));
283             $m->connect('api/statuses/home_timeline.:format',
284                         array('action' => 'ApiTimelineFriends',
285                               'format' => '(xml|json|rss|atom)'));
286
287             $m->connect('api/statuses/home_timeline/:id.:format',
288                         array('action' => 'ApiTimelineFriends',
289                               'id' => '[a-zA-Z0-9]+',
290                               'format' => '(xml|json|rss|atom)'));
291
292             $m->connect('api/statuses/user_timeline.:format',
293                         array('action' => 'ApiTimelineUser',
294                               'format' => '(xml|json|rss|atom)'));
295
296             $m->connect('api/statuses/user_timeline/:id.:format',
297                         array('action' => 'ApiTimelineUser',
298                               'id' => '[a-zA-Z0-9]+',
299                               'format' => '(xml|json|rss|atom)'));
300
301             $m->connect('api/statuses/mentions.:format',
302                         array('action' => 'ApiTimelineMentions',
303                               'format' => '(xml|json|rss|atom)'));
304
305             $m->connect('api/statuses/mentions/:id.:format',
306                         array('action' => 'ApiTimelineMentions',
307                               'id' => '[a-zA-Z0-9]+',
308                               'format' => '(xml|json|rss|atom)'));
309
310             $m->connect('api/statuses/replies.:format',
311                         array('action' => 'ApiTimelineMentions',
312                               'format' => '(xml|json|rss|atom)'));
313
314             $m->connect('api/statuses/replies/:id.:format',
315                         array('action' => 'ApiTimelineMentions',
316                               'id' => '[a-zA-Z0-9]+',
317                               'format' => '(xml|json|rss|atom)'));
318
319             $m->connect('api/statuses/friends.:format',
320                         array('action' => 'ApiUserFriends',
321                               'format' => '(xml|json)'));
322
323             $m->connect('api/statuses/friends/:id.:format',
324                         array('action' => 'ApiUserFriends',
325                               'id' => '[a-zA-Z0-9]+',
326                               'format' => '(xml|json)'));
327
328             $m->connect('api/statuses/followers.:format',
329                         array('action' => 'ApiUserFollowers',
330                               'format' => '(xml|json)'));
331
332             $m->connect('api/statuses/followers/:id.:format',
333                         array('action' => 'ApiUserFollowers',
334                               'id' => '[a-zA-Z0-9]+',
335                               'format' => '(xml|json)'));
336
337             $m->connect('api/statuses/show.:format',
338                         array('action' => 'ApiStatusesShow',
339                               'format' => '(xml|json)'));
340
341             $m->connect('api/statuses/show/:id.:format',
342                         array('action' => 'ApiStatusesShow',
343                               'id' => '[0-9]+',
344                               'format' => '(xml|json)'));
345
346             $m->connect('api/statuses/update.:format',
347                         array('action' => 'ApiStatusesUpdate',
348                               'format' => '(xml|json)'));
349
350             $m->connect('api/statuses/destroy.:format',
351                         array('action' => 'ApiStatusesDestroy',
352                               'format' => '(xml|json)'));
353
354             $m->connect('api/statuses/destroy/:id.:format',
355                         array('action' => 'ApiStatusesDestroy',
356                               'id' => '[0-9]+',
357                               'format' => '(xml|json)'));
358
359             // users
360
361             $m->connect('api/users/show/:id.:format',
362                         array('action' => 'ApiUserShow',
363                               'id' => '[a-zA-Z0-9]+',
364                               'format' => '(xml|json)'));
365
366             // direct messages
367
368             $m->connect('api/direct_messages.:format',
369                         array('action' => 'ApiDirectMessage',
370                               'format' => '(xml|json|rss|atom)'));
371
372             $m->connect('api/direct_messages/sent.:format',
373                         array('action' => 'ApiDirectMessage',
374                               'format' => '(xml|json|rss|atom)',
375                               'sent' => true));
376
377             $m->connect('api/direct_messages/new.:format',
378                         array('action' => 'ApiDirectMessageNew',
379                               'format' => '(xml|json)'));
380
381             // friendships
382
383             $m->connect('api/friendships/show.:format',
384                         array('action' => 'ApiFriendshipsShow',
385                               'format' => '(xml|json)'));
386
387             $m->connect('api/friendships/exists.:format',
388                         array('action' => 'ApiFriendshipsExists',
389                               'format' => '(xml|json)'));
390
391             $m->connect('api/friendships/create.:format',
392                         array('action' => 'ApiFriendshipsCreate',
393                               'format' => '(xml|json)'));
394
395             $m->connect('api/friendships/destroy.:format',
396                         array('action' => 'ApiFriendshipsDestroy',
397                               'format' => '(xml|json)'));
398
399             $m->connect('api/friendships/create/:id.:format',
400                         array('action' => 'ApiFriendshipsCreate',
401                               'id' => '[a-zA-Z0-9]+',
402                               'format' => '(xml|json)'));
403
404             $m->connect('api/friendships/destroy/:id.:format',
405                         array('action' => 'ApiFriendshipsDestroy',
406                               'id' => '[a-zA-Z0-9]+',
407                               'format' => '(xml|json)'));
408
409             // Social graph
410
411             $m->connect('api/friends/ids/:id.:format',
412                         array('action' => 'apiFriends',
413                               'ids_only' => true));
414
415             $m->connect('api/followers/ids/:id.:format',
416                         array('action' => 'apiFollowers',
417                               'ids_only' => true));
418
419             $m->connect('api/friends/ids.:format',
420                         array('action' => 'apiFriends',
421                               'ids_only' => true));
422
423             $m->connect('api/followers/ids.:format',
424                         array('action' => 'apiFollowers',
425                               'ids_only' => true));
426
427             // account
428
429             $m->connect('api/account/verify_credentials.:format',
430                         array('action' => 'ApiAccountVerifyCredentials'));
431
432             $m->connect('api/account/update_profile.:format',
433                         array('action' => 'ApiAccountUpdateProfile'));
434
435             $m->connect('api/account/update_profile_image.:format',
436                         array('action' => 'ApiAccountUpdateProfileImage'));
437
438             $m->connect('api/account/update_profile_background_image.:format',
439                         array('action' => 'ApiAccountUpdateProfileBackgroundImage'));
440
441             $m->connect('api/account/update_profile_colors.:format',
442                         array('action' => 'ApiAccountUpdateProfileColors'));
443
444             $m->connect('api/account/update_delivery_device.:format',
445                         array('action' => 'ApiAccountUpdateDeliveryDevice'));
446
447             // special case where verify_credentials is called w/out a format
448
449             $m->connect('api/account/verify_credentials',
450                         array('action' => 'ApiAccountVerifyCredentials'));
451
452             $m->connect('api/account/rate_limit_status.:format',
453                         array('action' => 'ApiAccountRateLimitStatus'));
454
455             // favorites
456
457             $m->connect('api/favorites.:format',
458                         array('action' => 'ApiTimelineFavorites',
459                               'format' => '(xml|json|rss|atom)'));
460
461             $m->connect('api/favorites/:id.:format',
462                         array('action' => 'ApiTimelineFavorites',
463                               'id' => '[a-zA-Z0-9]+',
464                               'format' => '(xmljson|rss|atom)'));
465
466             $m->connect('api/favorites/create/:id.:format',
467                         array('action' => 'ApiFavoriteCreate',
468                               'id' => '[a-zA-Z0-9]+',
469                               'format' => '(xml|json)'));
470
471             $m->connect('api/favorites/destroy/:id.:format',
472                         array('action' => 'ApiFavoriteDestroy',
473                               'id' => '[a-zA-Z0-9]+',
474                               'format' => '(xml|json)'));
475             // blocks
476
477             $m->connect('api/blocks/create/:id.:format',
478                         array('action' => 'ApiBlockCreate',
479                               'id' => '[a-zA-Z0-9]+',
480                               'format' => '(xml|json)'));
481
482             $m->connect('api/blocks/destroy/:id.:format',
483                         array('action' => 'ApiBlockDestroy',
484                               'id' => '[a-zA-Z0-9]+',
485                               'format' => '(xml|json)'));
486             // help
487
488             $m->connect('api/help/test.:format',
489                         array('action' => 'ApiHelpTest',
490                               'format' => '(xml|json)'));
491
492             // statusnet
493
494             $m->connect('api/statusnet/version.:format',
495                         array('action' => 'ApiStatusnetVersion',
496                               'format' => '(xml|json)'));
497
498             $m->connect('api/statusnet/config.:format',
499                         array('action' => 'ApiStatusnetConfig',
500                               'format' => '(xml|json)'));
501
502             // For older methods, we provide "laconica" base action
503
504             $m->connect('api/laconica/version.:format',
505                         array('action' => 'ApiStatusnetVersion',
506                               'format' => '(xml|json)'));
507
508             $m->connect('api/laconica/config.:format',
509                         array('action' => 'ApiStatusnetConfig',
510                               'format' => '(xml|json)'));
511
512             // Groups and tags are newer than 0.8.1 so no backward-compatibility
513             // necessary
514
515             // Groups
516             //'list' has to be handled differently, as php will not allow a method to be named 'list'
517
518             $m->connect('api/statusnet/groups/timeline/:id.:format',
519                         array('action' => 'ApiTimelineGroup',
520                               'id' => '[a-zA-Z0-9]+',
521                               'format' => '(xmljson|rss|atom)'));
522
523             $m->connect('api/statusnet/groups/show.:format',
524                         array('action' => 'ApiGroupShow',
525                               'format' => '(xml|json)'));
526
527             $m->connect('api/statusnet/groups/show/:id.:format',
528                         array('action' => 'ApiGroupShow',
529                               'id' => '[a-zA-Z0-9]+',
530                               'format' => '(xml|json)'));
531
532             $m->connect('api/statusnet/groups/join.:format',
533                         array('action' => 'ApiGroupJoin',
534                               'id' => '[a-zA-Z0-9]+',
535                               'format' => '(xml|json)'));
536
537             $m->connect('api/statusnet/groups/join/:id.:format',
538                         array('action' => 'ApiGroupJoin',
539                               'format' => '(xml|json)'));
540
541             $m->connect('api/statusnet/groups/leave.:format',
542                         array('action' => 'ApiGroupLeave',
543                               'id' => '[a-zA-Z0-9]+',
544                               'format' => '(xml|json)'));
545
546             $m->connect('api/statusnet/groups/leave/:id.:format',
547                         array('action' => 'ApiGroupLeave',
548                               'format' => '(xml|json)'));
549
550             $m->connect('api/statusnet/groups/is_member.:format',
551                         array('action' => 'ApiGroupIsMember',
552                               'format' => '(xml|json)'));
553
554             $m->connect('api/statusnet/groups/list.:format',
555                         array('action' => 'ApiGroupList',
556                               'format' => '(xml|json|rss|atom)'));
557
558             $m->connect('api/statusnet/groups/list/:id.:format',
559                         array('action' => 'ApiGroupList',
560                               'id' => '[a-zA-Z0-9]+',
561                               'format' => '(xml|json|rss|atom)'));
562
563             $m->connect('api/statusnet/groups/list_all.:format',
564                         array('action' => 'ApiGroupListAll',
565                               'format' => '(xml|json|rss|atom)'));
566
567             $m->connect('api/statusnet/groups/membership.:format',
568                         array('action' => 'ApiGroupMembership',
569                               'format' => '(xml|json)'));
570
571             $m->connect('api/statusnet/groups/membership/:id.:format',
572                         array('action' => 'ApiGroupMembership',
573                               'id' => '[a-zA-Z0-9]+',
574                               'format' => '(xml|json)'));
575
576             $m->connect('api/statusnet/groups/create.:format',
577                         array('action' => 'ApiGroupCreate',
578                               'format' => '(xml|json)'));
579             // Tags
580             $m->connect('api/statusnet/tags/timeline/:tag.:format',
581                         array('action' => 'ApiTimelineTag',
582                               'format' => '(xmljson|rss|atom)'));
583
584             // search
585             $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
586             $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
587             $m->connect('api/trends.json', array('action' => 'twitapitrends'));
588
589             $m->connect('admin/site', array('action' => 'siteadminpanel'));
590             $m->connect('admin/design', array('action' => 'designadminpanel'));
591             $m->connect('admin/user', array('action' => 'useradminpanel'));
592             $m->connect('admin/paths', array('action' => 'pathsadminpanel'));
593
594             $m->connect('getfile/:filename',
595                         array('action' => 'getfile'),
596                         array('filename' => '[A-Za-z0-9._-]+'));
597
598             // user stuff
599
600             foreach (array('subscriptions', 'subscribers',
601                            'nudge', 'all', 'foaf', 'xrds',
602                            'replies', 'inbox', 'outbox', 'microsummary') as $a) {
603                 $m->connect(':nickname/'.$a,
604                             array('action' => $a),
605                             array('nickname' => '[a-zA-Z0-9]{1,64}'));
606             }
607
608             foreach (array('subscriptions', 'subscribers') as $a) {
609                 $m->connect(':nickname/'.$a.'/:tag',
610                             array('action' => $a),
611                             array('tag' => '[a-zA-Z0-9]+',
612                                   'nickname' => '[a-zA-Z0-9]{1,64}'));
613             }
614
615             foreach (array('rss', 'groups') as $a) {
616                 $m->connect(':nickname/'.$a,
617                             array('action' => 'user'.$a),
618                             array('nickname' => '[a-zA-Z0-9]{1,64}'));
619             }
620
621             foreach (array('all', 'replies', 'favorites') as $a) {
622                 $m->connect(':nickname/'.$a.'/rss',
623                             array('action' => $a.'rss'),
624                             array('nickname' => '[a-zA-Z0-9]{1,64}'));
625             }
626
627             $m->connect(':nickname/favorites',
628                         array('action' => 'showfavorites'),
629                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
630
631             $m->connect(':nickname/avatar/:size',
632                         array('action' => 'avatarbynickname'),
633                         array('size' => '(original|96|48|24)',
634                               'nickname' => '[a-zA-Z0-9]{1,64}'));
635
636             $m->connect(':nickname/tag/:tag/rss',
637                         array('action' => 'userrss'),
638                         array('nickname' => '[a-zA-Z0-9]{1,64}'),
639                         array('tag' => '[a-zA-Z0-9]+'));
640
641             $m->connect(':nickname/tag/:tag',
642                         array('action' => 'showstream'),
643                         array('nickname' => '[a-zA-Z0-9]{1,64}'),
644                         array('tag' => '[a-zA-Z0-9]+'));
645
646             $m->connect(':nickname',
647                         array('action' => 'showstream'),
648                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
649
650             Event::handle('RouterInitialized', array($m));
651         }
652
653         return $m;
654     }
655
656     function map($path)
657     {
658         try {
659             $match = $this->m->match($path);
660         } catch (Net_URL_Mapper_InvalidException $e) {
661             common_log(LOG_ERR, "Problem getting route for $path - " .
662                        $e->getMessage());
663             $cac = new ClientErrorAction("Page not found.", 404);
664             $cac->showPage();
665         }
666
667         return $match;
668     }
669
670     function build($action, $args=null, $params=null, $fragment=null)
671     {
672         $action_arg = array('action' => $action);
673
674         if ($args) {
675             $args = array_merge($action_arg, $args);
676         } else {
677             $args = $action_arg;
678         }
679
680         $url = $this->m->generate($args, $params, $fragment);
681
682         // Due to a bug in the Net_URL_Mapper code, the returned URL may
683         // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
684         // repair that here rather than modifying the upstream code...
685
686         $qpos = strpos($url, '?');
687         if ($qpos !== false) {
688             $url = substr($url, 0, $qpos+1) .
689               str_replace('?', '&', substr($url, $qpos+1));
690         }
691         return $url;
692     }
693 }