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',
105 foreach ($main as $a) {
106 $m->connect('main/'.$a, array('action' => $a));
109 $m->connect('main/sup/:seconds', array('action' => 'sup'),
110 array('seconds' => '[0-9]+'));
112 $m->connect('main/tagother/:id', array('action' => 'tagother'));
114 $m->connect('main/oembed',
115 array('action' => 'oembed'));
117 $m->connect('main/xrds',
118 array('action' => 'publicxrds'));
122 foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
123 $m->connect('main/'.$c.'/:code', array('action' => $c));
128 $m->connect('main/remote', array('action' => 'remotesubscribe'));
129 $m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '[A-Za-z0-9_-]+'));
131 foreach (Router::$bare as $action) {
132 $m->connect('index.php?action=' . $action, array('action' => $action));
137 foreach (array('profile', 'avatar', 'password', 'im',
138 'email', 'sms', 'userdesign', 'other') as $s) {
139 $m->connect('settings/'.$s, array('action' => $s.'settings'));
144 foreach (array('group', 'people', 'notice') as $s) {
145 $m->connect('search/'.$s, array('action' => $s.'search'));
146 $m->connect('search/'.$s.'?q=:q',
147 array('action' => $s.'search'),
151 // The second of these is needed to make the link work correctly
152 // when inserted into the page. The first is needed to match the
153 // route on the way in. Seems to be another Net_URL_Mapper bug to me.
154 $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
155 $m->connect('search/notice/rss?q=:q', array('action' => 'noticesearchrss'),
158 $m->connect('attachment/:attachment',
159 array('action' => 'attachment'),
160 array('attachment' => '[0-9]+'));
162 $m->connect('attachment/:attachment/ajax',
163 array('action' => 'attachment_ajax'),
164 array('attachment' => '[0-9]+'));
166 $m->connect('attachment/:attachment/thumbnail',
167 array('action' => 'attachment_thumbnail'),
168 array('attachment' => '[0-9]+'));
170 $m->connect('notice/new', array('action' => 'newnotice'));
171 $m->connect('notice/new?replyto=:replyto',
172 array('action' => 'newnotice'),
173 array('replyto' => '[A-Za-z0-9_-]+'));
174 $m->connect('notice/new?replyto=:replyto&inreplyto=:inreplyto',
175 array('action' => 'newnotice'),
176 array('replyto' => '[A-Za-z0-9_-]+'),
177 array('inreplyto' => '[0-9]+'));
179 $m->connect('notice/:notice/file',
180 array('action' => 'file'),
181 array('notice' => '[0-9]+'));
183 $m->connect('notice/:notice',
184 array('action' => 'shownotice'),
185 array('notice' => '[0-9]+'));
186 $m->connect('notice/delete', array('action' => 'deletenotice'));
187 $m->connect('notice/delete/:notice',
188 array('action' => 'deletenotice'),
189 array('notice' => '[0-9]+'));
191 $m->connect('bookmarklet/new', array('action' => 'bookmarklet'));
195 $m->connect('conversation/:id',
196 array('action' => 'conversation'),
197 array('id' => '[0-9]+'));
199 $m->connect('message/new', array('action' => 'newmessage'));
200 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
201 $m->connect('message/:message',
202 array('action' => 'showmessage'),
203 array('message' => '[0-9]+'));
205 $m->connect('user/:id',
206 array('action' => 'userbyid'),
207 array('id' => '[0-9]+'));
209 $m->connect('tags/', array('action' => 'publictagcloud'));
210 $m->connect('tag/', array('action' => 'publictagcloud'));
211 $m->connect('tags', array('action' => 'publictagcloud'));
212 $m->connect('tag', array('action' => 'publictagcloud'));
213 $m->connect('tag/:tag/rss',
214 array('action' => 'tagrss'),
215 array('tag' => '[a-zA-Z0-9]+'));
216 $m->connect('tag/:tag',
217 array('action' => 'tag'),
218 array('tag' => '[\pL\pN_\-\.]{1,64}'));
220 $m->connect('peopletag/:tag',
221 array('action' => 'peopletag'),
222 array('tag' => '[a-zA-Z0-9]+'));
224 $m->connect('featured/', array('action' => 'featured'));
225 $m->connect('featured', array('action' => 'featured'));
226 $m->connect('favorited/', array('action' => 'favorited'));
227 $m->connect('favorited', array('action' => 'favorited'));
231 $m->connect('group/new', array('action' => 'newgroup'));
233 foreach (array('edit', 'join', 'leave') as $v) {
234 $m->connect('group/:nickname/'.$v,
235 array('action' => $v.'group'),
236 array('nickname' => '[a-zA-Z0-9]+'));
239 foreach (array('members', 'logo', 'rss', 'designsettings') as $n) {
240 $m->connect('group/:nickname/'.$n,
241 array('action' => 'group'.$n),
242 array('nickname' => '[a-zA-Z0-9]+'));
245 $m->connect('group/:nickname/foaf',
246 array('action' => 'foafgroup'),
247 array('nickname' => '[a-zA-Z0-9]+'));
249 $m->connect('group/:nickname/blocked',
250 array('action' => 'blockedfromgroup'),
251 array('nickname' => '[a-zA-Z0-9]+'));
253 $m->connect('group/:nickname/makeadmin',
254 array('action' => 'makeadmin'),
255 array('nickname' => '[a-zA-Z0-9]+'));
257 $m->connect('group/:id/id',
258 array('action' => 'groupbyid'),
259 array('id' => '[0-9]+'));
261 $m->connect('group/:nickname',
262 array('action' => 'showgroup'),
263 array('nickname' => '[a-zA-Z0-9]+'));
265 $m->connect('group/', array('action' => 'groups'));
266 $m->connect('group', array('action' => 'groups'));
267 $m->connect('groups/', array('action' => 'groups'));
268 $m->connect('groups', array('action' => 'groups'));
270 // Twitter-compatible API
274 $m->connect('api/statuses/public_timeline.:format',
275 array('action' => 'ApiTimelinePublic',
276 'format' => '(xml|json|rss|atom)'));
278 $m->connect('api/statuses/friends_timeline.:format',
279 array('action' => 'ApiTimelineFriends',
280 'format' => '(xml|json|rss|atom)'));
282 $m->connect('api/statuses/friends_timeline/:id.:format',
283 array('action' => 'ApiTimelineFriends',
284 'id' => '[a-zA-Z0-9]+',
285 'format' => '(xml|json|rss|atom)'));
287 $m->connect('api/statuses/home_timeline.:format',
288 array('action' => 'ApiTimelineHome',
289 'format' => '(xml|json|rss|atom)'));
291 $m->connect('api/statuses/home_timeline/:id.:format',
292 array('action' => 'ApiTimelineHome',
293 'id' => '[a-zA-Z0-9]+',
294 'format' => '(xml|json|rss|atom)'));
296 $m->connect('api/statuses/user_timeline.:format',
297 array('action' => 'ApiTimelineUser',
298 'format' => '(xml|json|rss|atom)'));
300 $m->connect('api/statuses/user_timeline/:id.:format',
301 array('action' => 'ApiTimelineUser',
302 'id' => '[a-zA-Z0-9]+',
303 'format' => '(xml|json|rss|atom)'));
305 $m->connect('api/statuses/mentions.:format',
306 array('action' => 'ApiTimelineMentions',
307 'format' => '(xml|json|rss|atom)'));
309 $m->connect('api/statuses/mentions/:id.:format',
310 array('action' => 'ApiTimelineMentions',
311 'id' => '[a-zA-Z0-9]+',
312 'format' => '(xml|json|rss|atom)'));
314 $m->connect('api/statuses/replies.:format',
315 array('action' => 'ApiTimelineMentions',
316 'format' => '(xml|json|rss|atom)'));
318 $m->connect('api/statuses/replies/:id.:format',
319 array('action' => 'ApiTimelineMentions',
320 'id' => '[a-zA-Z0-9]+',
321 'format' => '(xml|json|rss|atom)'));
323 $m->connect('api/statuses/retweeted_by_me.:format',
324 array('action' => 'ApiTimelineRetweetedByMe',
325 'format' => '(xml|json|atom)'));
327 $m->connect('api/statuses/retweeted_to_me.:format',
328 array('action' => 'ApiTimelineRetweetedToMe',
329 'format' => '(xml|json|atom)'));
331 $m->connect('api/statuses/retweets_of_me.:format',
332 array('action' => 'ApiTimelineRetweetsOfMe',
333 'format' => '(xml|json|atom)'));
335 $m->connect('api/statuses/friends.:format',
336 array('action' => 'ApiUserFriends',
337 'format' => '(xml|json)'));
339 $m->connect('api/statuses/friends/:id.:format',
340 array('action' => 'ApiUserFriends',
341 'id' => '[a-zA-Z0-9]+',
342 'format' => '(xml|json)'));
344 $m->connect('api/statuses/followers.:format',
345 array('action' => 'ApiUserFollowers',
346 'format' => '(xml|json)'));
348 $m->connect('api/statuses/followers/:id.:format',
349 array('action' => 'ApiUserFollowers',
350 'id' => '[a-zA-Z0-9]+',
351 'format' => '(xml|json)'));
353 $m->connect('api/statuses/show.:format',
354 array('action' => 'ApiStatusesShow',
355 'format' => '(xml|json)'));
357 $m->connect('api/statuses/show/:id.:format',
358 array('action' => 'ApiStatusesShow',
360 'format' => '(xml|json)'));
362 $m->connect('api/statuses/update.:format',
363 array('action' => 'ApiStatusesUpdate',
364 'format' => '(xml|json)'));
366 $m->connect('api/statuses/destroy.:format',
367 array('action' => 'ApiStatusesDestroy',
368 'format' => '(xml|json)'));
370 $m->connect('api/statuses/destroy/:id.:format',
371 array('action' => 'ApiStatusesDestroy',
373 'format' => '(xml|json)'));
375 $m->connect('api/statuses/retweet/:id.:format',
376 array('action' => 'ApiStatusesRetweet',
378 'format' => '(xml|json)'));
380 $m->connect('api/statuses/retweets/:id.:format',
381 array('action' => 'ApiStatusesRetweets',
383 'format' => '(xml|json)'));
387 $m->connect('api/users/show.:format',
388 array('action' => 'ApiUserShow',
389 'format' => '(xml|json)'));
391 $m->connect('api/users/show/:id.:format',
392 array('action' => 'ApiUserShow',
393 'id' => '[a-zA-Z0-9]+',
394 'format' => '(xml|json)'));
398 $m->connect('api/direct_messages.:format',
399 array('action' => 'ApiDirectMessage',
400 'format' => '(xml|json|rss|atom)'));
402 $m->connect('api/direct_messages/sent.:format',
403 array('action' => 'ApiDirectMessage',
404 'format' => '(xml|json|rss|atom)',
407 $m->connect('api/direct_messages/new.:format',
408 array('action' => 'ApiDirectMessageNew',
409 'format' => '(xml|json)'));
413 $m->connect('api/friendships/show.:format',
414 array('action' => 'ApiFriendshipsShow',
415 'format' => '(xml|json)'));
417 $m->connect('api/friendships/exists.:format',
418 array('action' => 'ApiFriendshipsExists',
419 'format' => '(xml|json)'));
421 $m->connect('api/friendships/create.:format',
422 array('action' => 'ApiFriendshipsCreate',
423 'format' => '(xml|json)'));
425 $m->connect('api/friendships/destroy.:format',
426 array('action' => 'ApiFriendshipsDestroy',
427 'format' => '(xml|json)'));
429 $m->connect('api/friendships/create/:id.:format',
430 array('action' => 'ApiFriendshipsCreate',
431 'id' => '[a-zA-Z0-9]+',
432 'format' => '(xml|json)'));
434 $m->connect('api/friendships/destroy/:id.:format',
435 array('action' => 'ApiFriendshipsDestroy',
436 'id' => '[a-zA-Z0-9]+',
437 'format' => '(xml|json)'));
441 $m->connect('api/friends/ids/:id.:format',
442 array('action' => 'apiFriends',
443 'ids_only' => true));
445 $m->connect('api/followers/ids/:id.:format',
446 array('action' => 'apiFollowers',
447 'ids_only' => true));
449 $m->connect('api/friends/ids.:format',
450 array('action' => 'apiFriends',
451 'ids_only' => true));
453 $m->connect('api/followers/ids.:format',
454 array('action' => 'apiFollowers',
455 'ids_only' => true));
459 $m->connect('api/account/verify_credentials.:format',
460 array('action' => 'ApiAccountVerifyCredentials'));
462 $m->connect('api/account/update_profile.:format',
463 array('action' => 'ApiAccountUpdateProfile'));
465 $m->connect('api/account/update_profile_image.:format',
466 array('action' => 'ApiAccountUpdateProfileImage'));
468 $m->connect('api/account/update_profile_background_image.:format',
469 array('action' => 'ApiAccountUpdateProfileBackgroundImage'));
471 $m->connect('api/account/update_profile_colors.:format',
472 array('action' => 'ApiAccountUpdateProfileColors'));
474 $m->connect('api/account/update_delivery_device.:format',
475 array('action' => 'ApiAccountUpdateDeliveryDevice'));
477 // special case where verify_credentials is called w/out a format
479 $m->connect('api/account/verify_credentials',
480 array('action' => 'ApiAccountVerifyCredentials'));
482 $m->connect('api/account/rate_limit_status.:format',
483 array('action' => 'ApiAccountRateLimitStatus'));
487 $m->connect('api/favorites.:format',
488 array('action' => 'ApiTimelineFavorites',
489 'format' => '(xml|json|rss|atom)'));
491 $m->connect('api/favorites/:id.:format',
492 array('action' => 'ApiTimelineFavorites',
493 'id' => '[a-zA-Z0-9]+',
494 'format' => '(xmljson|rss|atom)'));
496 $m->connect('api/favorites/create/:id.:format',
497 array('action' => 'ApiFavoriteCreate',
498 'id' => '[a-zA-Z0-9]+',
499 'format' => '(xml|json)'));
501 $m->connect('api/favorites/destroy/:id.:format',
502 array('action' => 'ApiFavoriteDestroy',
503 'id' => '[a-zA-Z0-9]+',
504 'format' => '(xml|json)'));
507 $m->connect('api/blocks/create/:id.:format',
508 array('action' => 'ApiBlockCreate',
509 'id' => '[a-zA-Z0-9]+',
510 'format' => '(xml|json)'));
512 $m->connect('api/blocks/destroy/:id.:format',
513 array('action' => 'ApiBlockDestroy',
514 'id' => '[a-zA-Z0-9]+',
515 'format' => '(xml|json)'));
518 $m->connect('api/help/test.:format',
519 array('action' => 'ApiHelpTest',
520 'format' => '(xml|json)'));
524 $m->connect('api/statusnet/version.:format',
525 array('action' => 'ApiStatusnetVersion',
526 'format' => '(xml|json)'));
528 $m->connect('api/statusnet/config.:format',
529 array('action' => 'ApiStatusnetConfig',
530 'format' => '(xml|json)'));
532 // For older methods, we provide "laconica" base action
534 $m->connect('api/laconica/version.:format',
535 array('action' => 'ApiStatusnetVersion',
536 'format' => '(xml|json)'));
538 $m->connect('api/laconica/config.:format',
539 array('action' => 'ApiStatusnetConfig',
540 'format' => '(xml|json)'));
542 // Groups and tags are newer than 0.8.1 so no backward-compatibility
546 //'list' has to be handled differently, as php will not allow a method to be named 'list'
548 $m->connect('api/statusnet/groups/timeline/:id.:format',
549 array('action' => 'ApiTimelineGroup',
550 'id' => '[a-zA-Z0-9]+',
551 'format' => '(xmljson|rss|atom)'));
553 $m->connect('api/statusnet/groups/show.:format',
554 array('action' => 'ApiGroupShow',
555 'format' => '(xml|json)'));
557 $m->connect('api/statusnet/groups/show/:id.:format',
558 array('action' => 'ApiGroupShow',
559 'id' => '[a-zA-Z0-9]+',
560 'format' => '(xml|json)'));
562 $m->connect('api/statusnet/groups/join.:format',
563 array('action' => 'ApiGroupJoin',
564 'id' => '[a-zA-Z0-9]+',
565 'format' => '(xml|json)'));
567 $m->connect('api/statusnet/groups/join/:id.:format',
568 array('action' => 'ApiGroupJoin',
569 'format' => '(xml|json)'));
571 $m->connect('api/statusnet/groups/leave.:format',
572 array('action' => 'ApiGroupLeave',
573 'id' => '[a-zA-Z0-9]+',
574 'format' => '(xml|json)'));
576 $m->connect('api/statusnet/groups/leave/:id.:format',
577 array('action' => 'ApiGroupLeave',
578 'format' => '(xml|json)'));
580 $m->connect('api/statusnet/groups/is_member.:format',
581 array('action' => 'ApiGroupIsMember',
582 'format' => '(xml|json)'));
584 $m->connect('api/statusnet/groups/list.:format',
585 array('action' => 'ApiGroupList',
586 'format' => '(xml|json|rss|atom)'));
588 $m->connect('api/statusnet/groups/list/:id.:format',
589 array('action' => 'ApiGroupList',
590 'id' => '[a-zA-Z0-9]+',
591 'format' => '(xml|json|rss|atom)'));
593 $m->connect('api/statusnet/groups/list_all.:format',
594 array('action' => 'ApiGroupListAll',
595 'format' => '(xml|json|rss|atom)'));
597 $m->connect('api/statusnet/groups/membership.:format',
598 array('action' => 'ApiGroupMembership',
599 'format' => '(xml|json)'));
601 $m->connect('api/statusnet/groups/membership/:id.:format',
602 array('action' => 'ApiGroupMembership',
603 'id' => '[a-zA-Z0-9]+',
604 'format' => '(xml|json)'));
606 $m->connect('api/statusnet/groups/create.:format',
607 array('action' => 'ApiGroupCreate',
608 'format' => '(xml|json)'));
610 $m->connect('api/statusnet/tags/timeline/:tag.:format',
611 array('action' => 'ApiTimelineTag',
612 'format' => '(xmljson|rss|atom)'));
615 $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
616 $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
617 $m->connect('api/trends.json', array('action' => 'twitapitrends'));
619 $m->connect('admin/site', array('action' => 'siteadminpanel'));
620 $m->connect('admin/design', array('action' => 'designadminpanel'));
621 $m->connect('admin/user', array('action' => 'useradminpanel'));
622 $m->connect('admin/paths', array('action' => 'pathsadminpanel'));
624 $m->connect('getfile/:filename',
625 array('action' => 'getfile'),
626 array('filename' => '[A-Za-z0-9._-]+'));
630 foreach (array('subscriptions', 'subscribers',
631 'nudge', 'all', 'foaf', 'xrds',
632 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
633 $m->connect(':nickname/'.$a,
634 array('action' => $a),
635 array('nickname' => '[a-zA-Z0-9]{1,64}'));
638 foreach (array('subscriptions', 'subscribers') as $a) {
639 $m->connect(':nickname/'.$a.'/:tag',
640 array('action' => $a),
641 array('tag' => '[a-zA-Z0-9]+',
642 'nickname' => '[a-zA-Z0-9]{1,64}'));
645 foreach (array('rss', 'groups') as $a) {
646 $m->connect(':nickname/'.$a,
647 array('action' => 'user'.$a),
648 array('nickname' => '[a-zA-Z0-9]{1,64}'));
651 foreach (array('all', 'replies', 'favorites') as $a) {
652 $m->connect(':nickname/'.$a.'/rss',
653 array('action' => $a.'rss'),
654 array('nickname' => '[a-zA-Z0-9]{1,64}'));
657 $m->connect(':nickname/favorites',
658 array('action' => 'showfavorites'),
659 array('nickname' => '[a-zA-Z0-9]{1,64}'));
661 $m->connect(':nickname/avatar/:size',
662 array('action' => 'avatarbynickname'),
663 array('size' => '(original|96|48|24)',
664 'nickname' => '[a-zA-Z0-9]{1,64}'));
666 $m->connect(':nickname/tag/:tag/rss',
667 array('action' => 'userrss'),
668 array('nickname' => '[a-zA-Z0-9]{1,64}'),
669 array('tag' => '[a-zA-Z0-9]+'));
671 $m->connect(':nickname/tag/:tag',
672 array('action' => 'showstream'),
673 array('nickname' => '[a-zA-Z0-9]{1,64}'),
674 array('tag' => '[a-zA-Z0-9]+'));
676 $m->connect(':nickname',
677 array('action' => 'showstream'),
678 array('nickname' => '[a-zA-Z0-9]{1,64}'));
680 Event::handle('RouterInitialized', array($m));
689 $match = $this->m->match($path);
690 } catch (Net_URL_Mapper_InvalidException $e) {
691 common_log(LOG_ERR, "Problem getting route for $path - " .
693 $cac = new ClientErrorAction("Page not found.", 404);
700 function build($action, $args=null, $params=null, $fragment=null)
702 $action_arg = array('action' => $action);
705 $args = array_merge($action_arg, $args);
710 $url = $this->m->generate($args, $params, $fragment);
712 // Due to a bug in the Net_URL_Mapper code, the returned URL may
713 // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
714 // repair that here rather than modifying the upstream code...
716 $qpos = strpos($url, '?');
717 if ($qpos !== false) {
718 $url = substr($url, 0, $qpos+1) .
719 str_replace('?', '&', substr($url, $qpos+1));