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