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',
108 foreach ($main as $a) {
109 $m->connect('main/'.$a, array('action' => $a));
112 $m->connect('main/sup/:seconds', array('action' => 'sup'),
113 array('seconds' => '[0-9]+'));
115 $m->connect('main/tagother/:id', array('action' => 'tagother'));
117 $m->connect('main/oembed',
118 array('action' => 'oembed'));
120 $m->connect('main/xrds',
121 array('action' => 'publicxrds'));
125 foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
126 $m->connect('main/'.$c.'/:code', array('action' => $c));
131 $m->connect('main/remote', array('action' => 'remotesubscribe'));
132 $m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '[A-Za-z0-9_-]+'));
134 foreach (Router::$bare as $action) {
135 $m->connect('index.php?action=' . $action, array('action' => $action));
140 foreach (array('profile', 'avatar', 'password', 'im',
141 'email', 'sms', 'userdesign', 'other') as $s) {
142 $m->connect('settings/'.$s, array('action' => $s.'settings'));
147 foreach (array('group', 'people', 'notice') as $s) {
148 $m->connect('search/'.$s, array('action' => $s.'search'));
149 $m->connect('search/'.$s.'?q=:q',
150 array('action' => $s.'search'),
154 // The second of these is needed to make the link work correctly
155 // when inserted into the page. The first is needed to match the
156 // route on the way in. Seems to be another Net_URL_Mapper bug to me.
157 $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
158 $m->connect('search/notice/rss?q=:q', array('action' => 'noticesearchrss'),
161 $m->connect('attachment/:attachment',
162 array('action' => 'attachment'),
163 array('attachment' => '[0-9]+'));
165 $m->connect('attachment/:attachment/ajax',
166 array('action' => 'attachment_ajax'),
167 array('attachment' => '[0-9]+'));
169 $m->connect('attachment/:attachment/thumbnail',
170 array('action' => 'attachment_thumbnail'),
171 array('attachment' => '[0-9]+'));
173 $m->connect('notice/new', array('action' => 'newnotice'));
174 $m->connect('notice/new?replyto=:replyto',
175 array('action' => 'newnotice'),
176 array('replyto' => '[A-Za-z0-9_-]+'));
177 $m->connect('notice/new?replyto=:replyto&inreplyto=:inreplyto',
178 array('action' => 'newnotice'),
179 array('replyto' => '[A-Za-z0-9_-]+'),
180 array('inreplyto' => '[0-9]+'));
182 $m->connect('notice/:notice/file',
183 array('action' => 'file'),
184 array('notice' => '[0-9]+'));
186 $m->connect('notice/:notice',
187 array('action' => 'shownotice'),
188 array('notice' => '[0-9]+'));
189 $m->connect('notice/delete', array('action' => 'deletenotice'));
190 $m->connect('notice/delete/:notice',
191 array('action' => 'deletenotice'),
192 array('notice' => '[0-9]+'));
194 $m->connect('bookmarklet/new', array('action' => 'bookmarklet'));
198 $m->connect('conversation/:id',
199 array('action' => 'conversation'),
200 array('id' => '[0-9]+'));
202 $m->connect('message/new', array('action' => 'newmessage'));
203 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
204 $m->connect('message/:message',
205 array('action' => 'showmessage'),
206 array('message' => '[0-9]+'));
208 $m->connect('user/:id',
209 array('action' => 'userbyid'),
210 array('id' => '[0-9]+'));
212 $m->connect('tags/', array('action' => 'publictagcloud'));
213 $m->connect('tag/', array('action' => 'publictagcloud'));
214 $m->connect('tags', array('action' => 'publictagcloud'));
215 $m->connect('tag', array('action' => 'publictagcloud'));
216 $m->connect('tag/:tag/rss',
217 array('action' => 'tagrss'),
218 array('tag' => '[a-zA-Z0-9]+'));
219 $m->connect('tag/:tag',
220 array('action' => 'tag'),
221 array('tag' => '[\pL\pN_\-\.]{1,64}'));
223 $m->connect('peopletag/:tag',
224 array('action' => 'peopletag'),
225 array('tag' => '[a-zA-Z0-9]+'));
227 $m->connect('featured/', array('action' => 'featured'));
228 $m->connect('featured', array('action' => 'featured'));
229 $m->connect('favorited/', array('action' => 'favorited'));
230 $m->connect('favorited', array('action' => 'favorited'));
234 $m->connect('group/new', array('action' => 'newgroup'));
236 foreach (array('edit', 'join', 'leave') as $v) {
237 $m->connect('group/:nickname/'.$v,
238 array('action' => $v.'group'),
239 array('nickname' => '[a-zA-Z0-9]+'));
242 foreach (array('members', 'logo', 'rss', 'designsettings') as $n) {
243 $m->connect('group/:nickname/'.$n,
244 array('action' => 'group'.$n),
245 array('nickname' => '[a-zA-Z0-9]+'));
248 $m->connect('group/:nickname/foaf',
249 array('action' => 'foafgroup'),
250 array('nickname' => '[a-zA-Z0-9]+'));
252 $m->connect('group/:nickname/blocked',
253 array('action' => 'blockedfromgroup'),
254 array('nickname' => '[a-zA-Z0-9]+'));
256 $m->connect('group/:nickname/makeadmin',
257 array('action' => 'makeadmin'),
258 array('nickname' => '[a-zA-Z0-9]+'));
260 $m->connect('group/:id/id',
261 array('action' => 'groupbyid'),
262 array('id' => '[0-9]+'));
264 $m->connect('group/:nickname',
265 array('action' => 'showgroup'),
266 array('nickname' => '[a-zA-Z0-9]+'));
268 $m->connect('group/', array('action' => 'groups'));
269 $m->connect('group', array('action' => 'groups'));
270 $m->connect('groups/', array('action' => 'groups'));
271 $m->connect('groups', array('action' => 'groups'));
273 // Twitter-compatible API
277 $m->connect('api/statuses/public_timeline.:format',
278 array('action' => 'ApiTimelinePublic',
279 'format' => '(xml|json|rss|atom)'));
281 $m->connect('api/statuses/friends_timeline.:format',
282 array('action' => 'ApiTimelineFriends',
283 'format' => '(xml|json|rss|atom)'));
285 $m->connect('api/statuses/friends_timeline/:id.:format',
286 array('action' => 'ApiTimelineFriends',
287 'id' => '[a-zA-Z0-9]+',
288 'format' => '(xml|json|rss|atom)'));
290 $m->connect('api/statuses/home_timeline.:format',
291 array('action' => 'ApiTimelineHome',
292 'format' => '(xml|json|rss|atom)'));
294 $m->connect('api/statuses/home_timeline/:id.:format',
295 array('action' => 'ApiTimelineHome',
296 'id' => '[a-zA-Z0-9]+',
297 'format' => '(xml|json|rss|atom)'));
299 $m->connect('api/statuses/user_timeline.:format',
300 array('action' => 'ApiTimelineUser',
301 'format' => '(xml|json|rss|atom)'));
303 $m->connect('api/statuses/user_timeline/:id.:format',
304 array('action' => 'ApiTimelineUser',
305 'id' => '[a-zA-Z0-9]+',
306 'format' => '(xml|json|rss|atom)'));
308 $m->connect('api/statuses/mentions.:format',
309 array('action' => 'ApiTimelineMentions',
310 'format' => '(xml|json|rss|atom)'));
312 $m->connect('api/statuses/mentions/:id.:format',
313 array('action' => 'ApiTimelineMentions',
314 'id' => '[a-zA-Z0-9]+',
315 'format' => '(xml|json|rss|atom)'));
317 $m->connect('api/statuses/replies.:format',
318 array('action' => 'ApiTimelineMentions',
319 'format' => '(xml|json|rss|atom)'));
321 $m->connect('api/statuses/replies/:id.:format',
322 array('action' => 'ApiTimelineMentions',
323 'id' => '[a-zA-Z0-9]+',
324 'format' => '(xml|json|rss|atom)'));
326 $m->connect('api/statuses/retweeted_by_me.:format',
327 array('action' => 'ApiTimelineRetweetedByMe',
328 'format' => '(xml|json|atom)'));
330 $m->connect('api/statuses/retweeted_to_me.:format',
331 array('action' => 'ApiTimelineRetweetedToMe',
332 'format' => '(xml|json|atom)'));
334 $m->connect('api/statuses/retweets_of_me.:format',
335 array('action' => 'ApiTimelineRetweetsOfMe',
336 'format' => '(xml|json|atom)'));
338 $m->connect('api/statuses/friends.:format',
339 array('action' => 'ApiUserFriends',
340 'format' => '(xml|json)'));
342 $m->connect('api/statuses/friends/:id.:format',
343 array('action' => 'ApiUserFriends',
344 'id' => '[a-zA-Z0-9]+',
345 'format' => '(xml|json)'));
347 $m->connect('api/statuses/followers.:format',
348 array('action' => 'ApiUserFollowers',
349 'format' => '(xml|json)'));
351 $m->connect('api/statuses/followers/:id.:format',
352 array('action' => 'ApiUserFollowers',
353 'id' => '[a-zA-Z0-9]+',
354 'format' => '(xml|json)'));
356 $m->connect('api/statuses/show.:format',
357 array('action' => 'ApiStatusesShow',
358 'format' => '(xml|json)'));
360 $m->connect('api/statuses/show/:id.:format',
361 array('action' => 'ApiStatusesShow',
363 'format' => '(xml|json)'));
365 $m->connect('api/statuses/update.:format',
366 array('action' => 'ApiStatusesUpdate',
367 'format' => '(xml|json)'));
369 $m->connect('api/statuses/destroy.:format',
370 array('action' => 'ApiStatusesDestroy',
371 'format' => '(xml|json)'));
373 $m->connect('api/statuses/destroy/:id.:format',
374 array('action' => 'ApiStatusesDestroy',
376 'format' => '(xml|json)'));
378 $m->connect('api/statuses/retweet/:id.:format',
379 array('action' => 'ApiStatusesRetweet',
381 'format' => '(xml|json)'));
383 $m->connect('api/statuses/retweets/:id.:format',
384 array('action' => 'ApiStatusesRetweets',
386 'format' => '(xml|json)'));
390 $m->connect('api/users/show.:format',
391 array('action' => 'ApiUserShow',
392 'format' => '(xml|json)'));
394 $m->connect('api/users/show/:id.:format',
395 array('action' => 'ApiUserShow',
396 'id' => '[a-zA-Z0-9]+',
397 'format' => '(xml|json)'));
401 $m->connect('api/direct_messages.:format',
402 array('action' => 'ApiDirectMessage',
403 'format' => '(xml|json|rss|atom)'));
405 $m->connect('api/direct_messages/sent.:format',
406 array('action' => 'ApiDirectMessage',
407 'format' => '(xml|json|rss|atom)',
410 $m->connect('api/direct_messages/new.:format',
411 array('action' => 'ApiDirectMessageNew',
412 'format' => '(xml|json)'));
416 $m->connect('api/friendships/show.:format',
417 array('action' => 'ApiFriendshipsShow',
418 'format' => '(xml|json)'));
420 $m->connect('api/friendships/exists.:format',
421 array('action' => 'ApiFriendshipsExists',
422 'format' => '(xml|json)'));
424 $m->connect('api/friendships/create.:format',
425 array('action' => 'ApiFriendshipsCreate',
426 'format' => '(xml|json)'));
428 $m->connect('api/friendships/destroy.:format',
429 array('action' => 'ApiFriendshipsDestroy',
430 'format' => '(xml|json)'));
432 $m->connect('api/friendships/create/:id.:format',
433 array('action' => 'ApiFriendshipsCreate',
434 'id' => '[a-zA-Z0-9]+',
435 'format' => '(xml|json)'));
437 $m->connect('api/friendships/destroy/:id.:format',
438 array('action' => 'ApiFriendshipsDestroy',
439 'id' => '[a-zA-Z0-9]+',
440 'format' => '(xml|json)'));
444 $m->connect('api/friends/ids/:id.:format',
445 array('action' => 'apiFriends',
446 'ids_only' => true));
448 $m->connect('api/followers/ids/:id.:format',
449 array('action' => 'apiFollowers',
450 'ids_only' => true));
452 $m->connect('api/friends/ids.:format',
453 array('action' => 'apiFriends',
454 'ids_only' => true));
456 $m->connect('api/followers/ids.:format',
457 array('action' => 'apiFollowers',
458 'ids_only' => true));
462 $m->connect('api/account/verify_credentials.:format',
463 array('action' => 'ApiAccountVerifyCredentials'));
465 $m->connect('api/account/update_profile.:format',
466 array('action' => 'ApiAccountUpdateProfile'));
468 $m->connect('api/account/update_profile_image.:format',
469 array('action' => 'ApiAccountUpdateProfileImage'));
471 $m->connect('api/account/update_profile_background_image.:format',
472 array('action' => 'ApiAccountUpdateProfileBackgroundImage'));
474 $m->connect('api/account/update_profile_colors.:format',
475 array('action' => 'ApiAccountUpdateProfileColors'));
477 $m->connect('api/account/update_delivery_device.:format',
478 array('action' => 'ApiAccountUpdateDeliveryDevice'));
480 // special case where verify_credentials is called w/out a format
482 $m->connect('api/account/verify_credentials',
483 array('action' => 'ApiAccountVerifyCredentials'));
485 $m->connect('api/account/rate_limit_status.:format',
486 array('action' => 'ApiAccountRateLimitStatus'));
490 $m->connect('api/favorites.:format',
491 array('action' => 'ApiTimelineFavorites',
492 'format' => '(xml|json|rss|atom)'));
494 $m->connect('api/favorites/:id.:format',
495 array('action' => 'ApiTimelineFavorites',
496 'id' => '[a-zA-Z0-9]+',
497 'format' => '(xmljson|rss|atom)'));
499 $m->connect('api/favorites/create/:id.:format',
500 array('action' => 'ApiFavoriteCreate',
501 'id' => '[a-zA-Z0-9]+',
502 'format' => '(xml|json)'));
504 $m->connect('api/favorites/destroy/:id.:format',
505 array('action' => 'ApiFavoriteDestroy',
506 'id' => '[a-zA-Z0-9]+',
507 'format' => '(xml|json)'));
510 $m->connect('api/blocks/create/:id.:format',
511 array('action' => 'ApiBlockCreate',
512 'id' => '[a-zA-Z0-9]+',
513 'format' => '(xml|json)'));
515 $m->connect('api/blocks/destroy/:id.:format',
516 array('action' => 'ApiBlockDestroy',
517 'id' => '[a-zA-Z0-9]+',
518 'format' => '(xml|json)'));
521 $m->connect('api/help/test.:format',
522 array('action' => 'ApiHelpTest',
523 'format' => '(xml|json)'));
527 $m->connect('api/statusnet/version.:format',
528 array('action' => 'ApiStatusnetVersion',
529 'format' => '(xml|json)'));
531 $m->connect('api/statusnet/config.:format',
532 array('action' => 'ApiStatusnetConfig',
533 'format' => '(xml|json)'));
535 // For older methods, we provide "laconica" base action
537 $m->connect('api/laconica/version.:format',
538 array('action' => 'ApiStatusnetVersion',
539 'format' => '(xml|json)'));
541 $m->connect('api/laconica/config.:format',
542 array('action' => 'ApiStatusnetConfig',
543 'format' => '(xml|json)'));
545 // Groups and tags are newer than 0.8.1 so no backward-compatibility
549 //'list' has to be handled differently, as php will not allow a method to be named 'list'
551 $m->connect('api/statusnet/groups/timeline/:id.:format',
552 array('action' => 'ApiTimelineGroup',
553 'id' => '[a-zA-Z0-9]+',
554 'format' => '(xmljson|rss|atom)'));
556 $m->connect('api/statusnet/groups/show.:format',
557 array('action' => 'ApiGroupShow',
558 'format' => '(xml|json)'));
560 $m->connect('api/statusnet/groups/show/:id.:format',
561 array('action' => 'ApiGroupShow',
562 'id' => '[a-zA-Z0-9]+',
563 'format' => '(xml|json)'));
565 $m->connect('api/statusnet/groups/join.:format',
566 array('action' => 'ApiGroupJoin',
567 'id' => '[a-zA-Z0-9]+',
568 'format' => '(xml|json)'));
570 $m->connect('api/statusnet/groups/join/:id.:format',
571 array('action' => 'ApiGroupJoin',
572 'format' => '(xml|json)'));
574 $m->connect('api/statusnet/groups/leave.:format',
575 array('action' => 'ApiGroupLeave',
576 'id' => '[a-zA-Z0-9]+',
577 'format' => '(xml|json)'));
579 $m->connect('api/statusnet/groups/leave/:id.:format',
580 array('action' => 'ApiGroupLeave',
581 'format' => '(xml|json)'));
583 $m->connect('api/statusnet/groups/is_member.:format',
584 array('action' => 'ApiGroupIsMember',
585 'format' => '(xml|json)'));
587 $m->connect('api/statusnet/groups/list.:format',
588 array('action' => 'ApiGroupList',
589 'format' => '(xml|json|rss|atom)'));
591 $m->connect('api/statusnet/groups/list/:id.:format',
592 array('action' => 'ApiGroupList',
593 'id' => '[a-zA-Z0-9]+',
594 'format' => '(xml|json|rss|atom)'));
596 $m->connect('api/statusnet/groups/list_all.:format',
597 array('action' => 'ApiGroupListAll',
598 'format' => '(xml|json|rss|atom)'));
600 $m->connect('api/statusnet/groups/membership.:format',
601 array('action' => 'ApiGroupMembership',
602 'format' => '(xml|json)'));
604 $m->connect('api/statusnet/groups/membership/:id.:format',
605 array('action' => 'ApiGroupMembership',
606 'id' => '[a-zA-Z0-9]+',
607 'format' => '(xml|json)'));
609 $m->connect('api/statusnet/groups/create.:format',
610 array('action' => 'ApiGroupCreate',
611 'format' => '(xml|json)'));
613 $m->connect('api/statusnet/tags/timeline/:tag.:format',
614 array('action' => 'ApiTimelineTag',
615 'format' => '(xmljson|rss|atom)'));
618 $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
619 $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
620 $m->connect('api/trends.json', array('action' => 'twitapitrends'));
622 $m->connect('admin/site', array('action' => 'siteadminpanel'));
623 $m->connect('admin/design', array('action' => 'designadminpanel'));
624 $m->connect('admin/user', array('action' => 'useradminpanel'));
625 $m->connect('admin/paths', array('action' => 'pathsadminpanel'));
627 $m->connect('getfile/:filename',
628 array('action' => 'getfile'),
629 array('filename' => '[A-Za-z0-9._-]+'));
633 foreach (array('subscriptions', 'subscribers',
634 'nudge', 'all', 'foaf', 'xrds',
635 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
636 $m->connect(':nickname/'.$a,
637 array('action' => $a),
638 array('nickname' => '[a-zA-Z0-9]{1,64}'));
641 foreach (array('subscriptions', 'subscribers') as $a) {
642 $m->connect(':nickname/'.$a.'/:tag',
643 array('action' => $a),
644 array('tag' => '[a-zA-Z0-9]+',
645 'nickname' => '[a-zA-Z0-9]{1,64}'));
648 foreach (array('rss', 'groups') as $a) {
649 $m->connect(':nickname/'.$a,
650 array('action' => 'user'.$a),
651 array('nickname' => '[a-zA-Z0-9]{1,64}'));
654 foreach (array('all', 'replies', 'favorites') as $a) {
655 $m->connect(':nickname/'.$a.'/rss',
656 array('action' => $a.'rss'),
657 array('nickname' => '[a-zA-Z0-9]{1,64}'));
660 $m->connect(':nickname/favorites',
661 array('action' => 'showfavorites'),
662 array('nickname' => '[a-zA-Z0-9]{1,64}'));
664 $m->connect(':nickname/avatar/:size',
665 array('action' => 'avatarbynickname'),
666 array('size' => '(original|96|48|24)',
667 'nickname' => '[a-zA-Z0-9]{1,64}'));
669 $m->connect(':nickname/tag/:tag/rss',
670 array('action' => 'userrss'),
671 array('nickname' => '[a-zA-Z0-9]{1,64}'),
672 array('tag' => '[a-zA-Z0-9]+'));
674 $m->connect(':nickname/tag/:tag',
675 array('action' => 'showstream'),
676 array('nickname' => '[a-zA-Z0-9]{1,64}'),
677 array('tag' => '[a-zA-Z0-9]+'));
679 $m->connect(':nickname',
680 array('action' => 'showstream'),
681 array('nickname' => '[a-zA-Z0-9]{1,64}'));
683 Event::handle('RouterInitialized', array($m));
692 $match = $this->m->match($path);
693 } catch (Net_URL_Mapper_InvalidException $e) {
694 common_log(LOG_ERR, "Problem getting route for $path - " .
696 $cac = new ClientErrorAction("Page not found.", 404);
703 function build($action, $args=null, $params=null, $fragment=null)
705 $action_arg = array('action' => $action);
708 $args = array_merge($action_arg, $args);
713 $url = $this->m->generate($args, $params, $fragment);
715 // Due to a bug in the Net_URL_Mapper code, the returned URL may
716 // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
717 // repair that here rather than modifying the upstream code...
719 $qpos = strpos($url, '?');
720 if ($qpos !== false) {
721 $url = substr($url, 0, $qpos+1) .
722 str_replace('?', '&', substr($url, $qpos+1));