3 * StatusNet, the distributed open-source microblogging tool
5 * URL routing utilities
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.
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.
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/>.
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/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
34 require_once 'Net/URL/Mapper.php';
39 * Cheap wrapper around Net_URL_Mapper
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/
52 static $bare = array('requesttoken', 'accesstoken', 'userauthorization',
53 'postnotice', 'updateprofile', 'finishremotesubscribe');
58 Router::$inst = new Router();
63 function __construct()
66 $this->m = $this->initialize();
72 $m = Net_URL_Mapper::getInstance();
74 if (Event::handle('StartInitializeRouter', array(&$m))) {
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',
84 $m->connect('opensearch/notice', array('action' => 'opensearch',
89 $m->connect('doc/:title', array('action' => 'doc'));
91 $m->connect('main/login?user_id=:user_id&token=:token', array('action'=>'login'), array('user_id'=> '[0-9]+', 'token'=>'.+'));
93 // main stuff is repetitive
95 $main = array('login', 'logout', 'register', 'subscribe',
96 'unsubscribe', 'confirmaddress', 'recoverpassword',
97 'invite', 'favor', 'disfavor', 'sup',
98 'block', 'unblock', 'subedit',
99 'groupblock', 'groupunblock',
100 'sandbox', 'unsandbox',
101 'silence', 'unsilence',
106 foreach ($main as $a) {
107 $m->connect('main/'.$a, array('action' => $a));
110 $m->connect('main/sup/:seconds', array('action' => 'sup'),
111 array('seconds' => '[0-9]+'));
113 $m->connect('main/tagother/:id', array('action' => 'tagother'));
115 $m->connect('main/oembed',
116 array('action' => 'oembed'));
118 $m->connect('main/xrds',
119 array('action' => 'publicxrds'));
123 foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
124 $m->connect('main/'.$c.'/:code', array('action' => $c));
129 $m->connect('main/remote', array('action' => 'remotesubscribe'));
130 $m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '[A-Za-z0-9_-]+'));
132 foreach (Router::$bare as $action) {
133 $m->connect('index.php?action=' . $action, array('action' => $action));
138 foreach (array('profile', 'avatar', 'password', 'im',
139 'email', 'sms', 'userdesign', 'other') as $s) {
140 $m->connect('settings/'.$s, array('action' => $s.'settings'));
145 foreach (array('group', 'people', 'notice') as $s) {
146 $m->connect('search/'.$s, array('action' => $s.'search'));
147 $m->connect('search/'.$s.'?q=:q',
148 array('action' => $s.'search'),
152 // The second of these is needed to make the link work correctly
153 // when inserted into the page. The first is needed to match the
154 // route on the way in. Seems to be another Net_URL_Mapper bug to me.
155 $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
156 $m->connect('search/notice/rss?q=:q', array('action' => 'noticesearchrss'),
159 $m->connect('attachment/:attachment',
160 array('action' => 'attachment'),
161 array('attachment' => '[0-9]+'));
163 $m->connect('attachment/:attachment/ajax',
164 array('action' => 'attachment_ajax'),
165 array('attachment' => '[0-9]+'));
167 $m->connect('attachment/:attachment/thumbnail',
168 array('action' => 'attachment_thumbnail'),
169 array('attachment' => '[0-9]+'));
171 $m->connect('notice/new', array('action' => 'newnotice'));
172 $m->connect('notice/new?replyto=:replyto',
173 array('action' => 'newnotice'),
174 array('replyto' => '[A-Za-z0-9_-]+'));
175 $m->connect('notice/new?replyto=:replyto&inreplyto=:inreplyto',
176 array('action' => 'newnotice'),
177 array('replyto' => '[A-Za-z0-9_-]+'),
178 array('inreplyto' => '[0-9]+'));
180 $m->connect('notice/:notice/file',
181 array('action' => 'file'),
182 array('notice' => '[0-9]+'));
184 $m->connect('notice/:notice',
185 array('action' => 'shownotice'),
186 array('notice' => '[0-9]+'));
187 $m->connect('notice/delete', array('action' => 'deletenotice'));
188 $m->connect('notice/delete/:notice',
189 array('action' => 'deletenotice'),
190 array('notice' => '[0-9]+'));
192 $m->connect('bookmarklet/new', array('action' => 'bookmarklet'));
196 $m->connect('conversation/:id',
197 array('action' => 'conversation'),
198 array('id' => '[0-9]+'));
200 $m->connect('message/new', array('action' => 'newmessage'));
201 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
202 $m->connect('message/:message',
203 array('action' => 'showmessage'),
204 array('message' => '[0-9]+'));
206 $m->connect('user/:id',
207 array('action' => 'userbyid'),
208 array('id' => '[0-9]+'));
210 $m->connect('tags/', array('action' => 'publictagcloud'));
211 $m->connect('tag/', array('action' => 'publictagcloud'));
212 $m->connect('tags', array('action' => 'publictagcloud'));
213 $m->connect('tag', array('action' => 'publictagcloud'));
214 $m->connect('tag/:tag/rss',
215 array('action' => 'tagrss'),
216 array('tag' => '[a-zA-Z0-9]+'));
217 $m->connect('tag/:tag',
218 array('action' => 'tag'),
219 array('tag' => '[\pL\pN_\-\.]{1,64}'));
221 $m->connect('peopletag/:tag',
222 array('action' => 'peopletag'),
223 array('tag' => '[a-zA-Z0-9]+'));
225 $m->connect('featured/', array('action' => 'featured'));
226 $m->connect('featured', array('action' => 'featured'));
227 $m->connect('favorited/', array('action' => 'favorited'));
228 $m->connect('favorited', array('action' => 'favorited'));
232 $m->connect('group/new', array('action' => 'newgroup'));
234 foreach (array('edit', 'join', 'leave') as $v) {
235 $m->connect('group/:nickname/'.$v,
236 array('action' => $v.'group'),
237 array('nickname' => '[a-zA-Z0-9]+'));
240 foreach (array('members', 'logo', 'rss', 'designsettings') as $n) {
241 $m->connect('group/:nickname/'.$n,
242 array('action' => 'group'.$n),
243 array('nickname' => '[a-zA-Z0-9]+'));
246 $m->connect('group/:nickname/foaf',
247 array('action' => 'foafgroup'),
248 array('nickname' => '[a-zA-Z0-9]+'));
250 $m->connect('group/:nickname/blocked',
251 array('action' => 'blockedfromgroup'),
252 array('nickname' => '[a-zA-Z0-9]+'));
254 $m->connect('group/:nickname/makeadmin',
255 array('action' => 'makeadmin'),
256 array('nickname' => '[a-zA-Z0-9]+'));
258 $m->connect('group/:id/id',
259 array('action' => 'groupbyid'),
260 array('id' => '[0-9]+'));
262 $m->connect('group/:nickname',
263 array('action' => 'showgroup'),
264 array('nickname' => '[a-zA-Z0-9]+'));
266 $m->connect('group/', array('action' => 'groups'));
267 $m->connect('group', array('action' => 'groups'));
268 $m->connect('groups/', array('action' => 'groups'));
269 $m->connect('groups', array('action' => 'groups'));
271 // Twitter-compatible API
275 $m->connect('api/statuses/public_timeline.:format',
276 array('action' => 'ApiTimelinePublic',
277 'format' => '(xml|json|rss|atom)'));
279 $m->connect('api/statuses/friends_timeline.:format',
280 array('action' => 'ApiTimelineFriends',
281 'format' => '(xml|json|rss|atom)'));
283 $m->connect('api/statuses/friends_timeline/:id.:format',
284 array('action' => 'ApiTimelineFriends',
285 'id' => '[a-zA-Z0-9]+',
286 'format' => '(xml|json|rss|atom)'));
288 $m->connect('api/statuses/home_timeline.:format',
289 array('action' => 'ApiTimelineHome',
290 'format' => '(xml|json|rss|atom)'));
292 $m->connect('api/statuses/home_timeline/:id.:format',
293 array('action' => 'ApiTimelineHome',
294 'id' => '[a-zA-Z0-9]+',
295 'format' => '(xml|json|rss|atom)'));
297 $m->connect('api/statuses/user_timeline.:format',
298 array('action' => 'ApiTimelineUser',
299 'format' => '(xml|json|rss|atom)'));
301 $m->connect('api/statuses/user_timeline/:id.:format',
302 array('action' => 'ApiTimelineUser',
303 'id' => '[a-zA-Z0-9]+',
304 'format' => '(xml|json|rss|atom)'));
306 $m->connect('api/statuses/mentions.:format',
307 array('action' => 'ApiTimelineMentions',
308 'format' => '(xml|json|rss|atom)'));
310 $m->connect('api/statuses/mentions/:id.:format',
311 array('action' => 'ApiTimelineMentions',
312 'id' => '[a-zA-Z0-9]+',
313 'format' => '(xml|json|rss|atom)'));
315 $m->connect('api/statuses/replies.:format',
316 array('action' => 'ApiTimelineMentions',
317 'format' => '(xml|json|rss|atom)'));
319 $m->connect('api/statuses/replies/:id.:format',
320 array('action' => 'ApiTimelineMentions',
321 'id' => '[a-zA-Z0-9]+',
322 'format' => '(xml|json|rss|atom)'));
324 $m->connect('api/statuses/retweeted_by_me.:format',
325 array('action' => 'ApiTimelineRetweetedByMe',
326 'format' => '(xml|json|atom)'));
328 $m->connect('api/statuses/retweeted_to_me.:format',
329 array('action' => 'ApiTimelineRetweetedToMe',
330 'format' => '(xml|json|atom)'));
332 $m->connect('api/statuses/retweets_of_me.:format',
333 array('action' => 'ApiTimelineRetweetsOfMe',
334 'format' => '(xml|json|atom)'));
336 $m->connect('api/statuses/friends.:format',
337 array('action' => 'ApiUserFriends',
338 'format' => '(xml|json)'));
340 $m->connect('api/statuses/friends/:id.:format',
341 array('action' => 'ApiUserFriends',
342 'id' => '[a-zA-Z0-9]+',
343 'format' => '(xml|json)'));
345 $m->connect('api/statuses/followers.:format',
346 array('action' => 'ApiUserFollowers',
347 'format' => '(xml|json)'));
349 $m->connect('api/statuses/followers/:id.:format',
350 array('action' => 'ApiUserFollowers',
351 'id' => '[a-zA-Z0-9]+',
352 'format' => '(xml|json)'));
354 $m->connect('api/statuses/show.:format',
355 array('action' => 'ApiStatusesShow',
356 'format' => '(xml|json)'));
358 $m->connect('api/statuses/show/:id.:format',
359 array('action' => 'ApiStatusesShow',
361 'format' => '(xml|json)'));
363 $m->connect('api/statuses/update.:format',
364 array('action' => 'ApiStatusesUpdate',
365 'format' => '(xml|json)'));
367 $m->connect('api/statuses/destroy.:format',
368 array('action' => 'ApiStatusesDestroy',
369 'format' => '(xml|json)'));
371 $m->connect('api/statuses/destroy/:id.:format',
372 array('action' => 'ApiStatusesDestroy',
374 'format' => '(xml|json)'));
376 $m->connect('api/statuses/retweet/:id.:format',
377 array('action' => 'ApiStatusesRetweet',
379 'format' => '(xml|json)'));
381 $m->connect('api/statuses/retweets/:id.:format',
382 array('action' => 'ApiStatusesRetweets',
384 'format' => '(xml|json)'));
388 $m->connect('api/users/show.:format',
389 array('action' => 'ApiUserShow',
390 'format' => '(xml|json)'));
392 $m->connect('api/users/show/:id.:format',
393 array('action' => 'ApiUserShow',
394 'id' => '[a-zA-Z0-9]+',
395 'format' => '(xml|json)'));
399 $m->connect('api/direct_messages.:format',
400 array('action' => 'ApiDirectMessage',
401 'format' => '(xml|json|rss|atom)'));
403 $m->connect('api/direct_messages/sent.:format',
404 array('action' => 'ApiDirectMessage',
405 'format' => '(xml|json|rss|atom)',
408 $m->connect('api/direct_messages/new.:format',
409 array('action' => 'ApiDirectMessageNew',
410 'format' => '(xml|json)'));
414 $m->connect('api/friendships/show.:format',
415 array('action' => 'ApiFriendshipsShow',
416 'format' => '(xml|json)'));
418 $m->connect('api/friendships/exists.:format',
419 array('action' => 'ApiFriendshipsExists',
420 'format' => '(xml|json)'));
422 $m->connect('api/friendships/create.:format',
423 array('action' => 'ApiFriendshipsCreate',
424 'format' => '(xml|json)'));
426 $m->connect('api/friendships/destroy.:format',
427 array('action' => 'ApiFriendshipsDestroy',
428 'format' => '(xml|json)'));
430 $m->connect('api/friendships/create/:id.:format',
431 array('action' => 'ApiFriendshipsCreate',
432 'id' => '[a-zA-Z0-9]+',
433 'format' => '(xml|json)'));
435 $m->connect('api/friendships/destroy/:id.:format',
436 array('action' => 'ApiFriendshipsDestroy',
437 'id' => '[a-zA-Z0-9]+',
438 'format' => '(xml|json)'));
442 $m->connect('api/friends/ids/:id.:format',
443 array('action' => 'apiFriends',
444 'ids_only' => true));
446 $m->connect('api/followers/ids/:id.:format',
447 array('action' => 'apiFollowers',
448 'ids_only' => true));
450 $m->connect('api/friends/ids.:format',
451 array('action' => 'apiFriends',
452 'ids_only' => true));
454 $m->connect('api/followers/ids.:format',
455 array('action' => 'apiFollowers',
456 'ids_only' => true));
460 $m->connect('api/account/verify_credentials.:format',
461 array('action' => 'ApiAccountVerifyCredentials'));
463 $m->connect('api/account/update_profile.:format',
464 array('action' => 'ApiAccountUpdateProfile'));
466 $m->connect('api/account/update_profile_image.:format',
467 array('action' => 'ApiAccountUpdateProfileImage'));
469 $m->connect('api/account/update_profile_background_image.:format',
470 array('action' => 'ApiAccountUpdateProfileBackgroundImage'));
472 $m->connect('api/account/update_profile_colors.:format',
473 array('action' => 'ApiAccountUpdateProfileColors'));
475 $m->connect('api/account/update_delivery_device.:format',
476 array('action' => 'ApiAccountUpdateDeliveryDevice'));
478 // special case where verify_credentials is called w/out a format
480 $m->connect('api/account/verify_credentials',
481 array('action' => 'ApiAccountVerifyCredentials'));
483 $m->connect('api/account/rate_limit_status.:format',
484 array('action' => 'ApiAccountRateLimitStatus'));
488 $m->connect('api/favorites.:format',
489 array('action' => 'ApiTimelineFavorites',
490 'format' => '(xml|json|rss|atom)'));
492 $m->connect('api/favorites/:id.:format',
493 array('action' => 'ApiTimelineFavorites',
494 'id' => '[a-zA-Z0-9]+',
495 'format' => '(xmljson|rss|atom)'));
497 $m->connect('api/favorites/create/:id.:format',
498 array('action' => 'ApiFavoriteCreate',
499 'id' => '[a-zA-Z0-9]+',
500 'format' => '(xml|json)'));
502 $m->connect('api/favorites/destroy/:id.:format',
503 array('action' => 'ApiFavoriteDestroy',
504 'id' => '[a-zA-Z0-9]+',
505 'format' => '(xml|json)'));
508 $m->connect('api/blocks/create/:id.:format',
509 array('action' => 'ApiBlockCreate',
510 'id' => '[a-zA-Z0-9]+',
511 'format' => '(xml|json)'));
513 $m->connect('api/blocks/destroy/:id.:format',
514 array('action' => 'ApiBlockDestroy',
515 'id' => '[a-zA-Z0-9]+',
516 'format' => '(xml|json)'));
519 $m->connect('api/help/test.:format',
520 array('action' => 'ApiHelpTest',
521 'format' => '(xml|json)'));
525 $m->connect('api/statusnet/version.:format',
526 array('action' => 'ApiStatusnetVersion',
527 'format' => '(xml|json)'));
529 $m->connect('api/statusnet/config.:format',
530 array('action' => 'ApiStatusnetConfig',
531 'format' => '(xml|json)'));
533 // For older methods, we provide "laconica" base action
535 $m->connect('api/laconica/version.:format',
536 array('action' => 'ApiStatusnetVersion',
537 'format' => '(xml|json)'));
539 $m->connect('api/laconica/config.:format',
540 array('action' => 'ApiStatusnetConfig',
541 'format' => '(xml|json)'));
543 // Groups and tags are newer than 0.8.1 so no backward-compatibility
547 //'list' has to be handled differently, as php will not allow a method to be named 'list'
549 $m->connect('api/statusnet/groups/timeline/:id.:format',
550 array('action' => 'ApiTimelineGroup',
551 'id' => '[a-zA-Z0-9]+',
552 'format' => '(xmljson|rss|atom)'));
554 $m->connect('api/statusnet/groups/show.:format',
555 array('action' => 'ApiGroupShow',
556 'format' => '(xml|json)'));
558 $m->connect('api/statusnet/groups/show/:id.:format',
559 array('action' => 'ApiGroupShow',
560 'id' => '[a-zA-Z0-9]+',
561 'format' => '(xml|json)'));
563 $m->connect('api/statusnet/groups/join.:format',
564 array('action' => 'ApiGroupJoin',
565 'id' => '[a-zA-Z0-9]+',
566 'format' => '(xml|json)'));
568 $m->connect('api/statusnet/groups/join/:id.:format',
569 array('action' => 'ApiGroupJoin',
570 'format' => '(xml|json)'));
572 $m->connect('api/statusnet/groups/leave.:format',
573 array('action' => 'ApiGroupLeave',
574 'id' => '[a-zA-Z0-9]+',
575 'format' => '(xml|json)'));
577 $m->connect('api/statusnet/groups/leave/:id.:format',
578 array('action' => 'ApiGroupLeave',
579 'format' => '(xml|json)'));
581 $m->connect('api/statusnet/groups/is_member.:format',
582 array('action' => 'ApiGroupIsMember',
583 'format' => '(xml|json)'));
585 $m->connect('api/statusnet/groups/list.:format',
586 array('action' => 'ApiGroupList',
587 'format' => '(xml|json|rss|atom)'));
589 $m->connect('api/statusnet/groups/list/:id.:format',
590 array('action' => 'ApiGroupList',
591 'id' => '[a-zA-Z0-9]+',
592 'format' => '(xml|json|rss|atom)'));
594 $m->connect('api/statusnet/groups/list_all.:format',
595 array('action' => 'ApiGroupListAll',
596 'format' => '(xml|json|rss|atom)'));
598 $m->connect('api/statusnet/groups/membership.:format',
599 array('action' => 'ApiGroupMembership',
600 'format' => '(xml|json)'));
602 $m->connect('api/statusnet/groups/membership/:id.:format',
603 array('action' => 'ApiGroupMembership',
604 'id' => '[a-zA-Z0-9]+',
605 'format' => '(xml|json)'));
607 $m->connect('api/statusnet/groups/create.:format',
608 array('action' => 'ApiGroupCreate',
609 'format' => '(xml|json)'));
611 $m->connect('api/statusnet/tags/timeline/:tag.:format',
612 array('action' => 'ApiTimelineTag',
613 'format' => '(xmljson|rss|atom)'));
616 $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
617 $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
618 $m->connect('api/trends.json', array('action' => 'twitapitrends'));
620 $m->connect('admin/site', array('action' => 'siteadminpanel'));
621 $m->connect('admin/design', array('action' => 'designadminpanel'));
622 $m->connect('admin/user', array('action' => 'useradminpanel'));
623 $m->connect('admin/paths', array('action' => 'pathsadminpanel'));
625 $m->connect('getfile/:filename',
626 array('action' => 'getfile'),
627 array('filename' => '[A-Za-z0-9._-]+'));
631 foreach (array('subscriptions', 'subscribers',
632 'nudge', 'all', 'foaf', 'xrds',
633 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
634 $m->connect(':nickname/'.$a,
635 array('action' => $a),
636 array('nickname' => '[a-zA-Z0-9]{1,64}'));
639 foreach (array('subscriptions', 'subscribers') as $a) {
640 $m->connect(':nickname/'.$a.'/:tag',
641 array('action' => $a),
642 array('tag' => '[a-zA-Z0-9]+',
643 'nickname' => '[a-zA-Z0-9]{1,64}'));
646 foreach (array('rss', 'groups') as $a) {
647 $m->connect(':nickname/'.$a,
648 array('action' => 'user'.$a),
649 array('nickname' => '[a-zA-Z0-9]{1,64}'));
652 foreach (array('all', 'replies', 'favorites') as $a) {
653 $m->connect(':nickname/'.$a.'/rss',
654 array('action' => $a.'rss'),
655 array('nickname' => '[a-zA-Z0-9]{1,64}'));
658 $m->connect(':nickname/favorites',
659 array('action' => 'showfavorites'),
660 array('nickname' => '[a-zA-Z0-9]{1,64}'));
662 $m->connect(':nickname/avatar/:size',
663 array('action' => 'avatarbynickname'),
664 array('size' => '(original|96|48|24)',
665 'nickname' => '[a-zA-Z0-9]{1,64}'));
667 $m->connect(':nickname/tag/:tag/rss',
668 array('action' => 'userrss'),
669 array('nickname' => '[a-zA-Z0-9]{1,64}'),
670 array('tag' => '[a-zA-Z0-9]+'));
672 $m->connect(':nickname/tag/:tag',
673 array('action' => 'showstream'),
674 array('nickname' => '[a-zA-Z0-9]{1,64}'),
675 array('tag' => '[a-zA-Z0-9]+'));
677 $m->connect(':nickname',
678 array('action' => 'showstream'),
679 array('nickname' => '[a-zA-Z0-9]{1,64}'));
681 Event::handle('RouterInitialized', array($m));
690 $match = $this->m->match($path);
691 } catch (Net_URL_Mapper_InvalidException $e) {
692 common_log(LOG_ERR, "Problem getting route for $path - " .
694 $cac = new ClientErrorAction("Page not found.", 404);
701 function build($action, $args=null, $params=null, $fragment=null)
703 $action_arg = array('action' => $action);
706 $args = array_merge($action_arg, $args);
711 $url = $this->m->generate($args, $params, $fragment);
713 // Due to a bug in the Net_URL_Mapper code, the returned URL may
714 // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
715 // repair that here rather than modifying the upstream code...
717 $qpos = strpos($url, '?');
718 if ($qpos !== false) {
719 $url = substr($url, 0, $qpos+1) .
720 str_replace('?', '&', substr($url, $qpos+1));