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 // main stuff is repetitive
93 $main = array('login', 'logout', 'register', 'subscribe',
94 'unsubscribe', 'confirmaddress', 'recoverpassword',
95 'invite', 'favor', 'disfavor', 'sup',
96 'block', 'unblock', 'subedit',
97 'groupblock', 'groupunblock',
98 'sandbox', 'unsandbox',
99 'silence', 'unsilence',
102 foreach ($main as $a) {
103 $m->connect('main/'.$a, array('action' => $a));
106 $m->connect('main/sup/:seconds', array('action' => 'sup'),
107 array('seconds' => '[0-9]+'));
109 $m->connect('main/tagother/:id', array('action' => 'tagother'));
111 $m->connect('main/oembed',
112 array('action' => 'oembed'));
114 $m->connect('main/xrds',
115 array('action' => 'publicxrds'));
119 foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
120 $m->connect('main/'.$c.'/:code', array('action' => $c));
125 $m->connect('main/remote', array('action' => 'remotesubscribe'));
126 $m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '[A-Za-z0-9_-]+'));
128 foreach (Router::$bare as $action) {
129 $m->connect('index.php?action=' . $action, array('action' => $action));
134 foreach (array('profile', 'avatar', 'password', 'im',
135 'email', 'sms', 'userdesign', 'other') as $s) {
136 $m->connect('settings/'.$s, array('action' => $s.'settings'));
141 foreach (array('group', 'people', 'notice') as $s) {
142 $m->connect('search/'.$s, array('action' => $s.'search'));
143 $m->connect('search/'.$s.'?q=:q',
144 array('action' => $s.'search'),
148 // The second of these is needed to make the link work correctly
149 // when inserted into the page. The first is needed to match the
150 // route on the way in. Seems to be another Net_URL_Mapper bug to me.
151 $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
152 $m->connect('search/notice/rss?q=:q', array('action' => 'noticesearchrss'),
155 $m->connect('attachment/:attachment',
156 array('action' => 'attachment'),
157 array('attachment' => '[0-9]+'));
159 $m->connect('attachment/:attachment/ajax',
160 array('action' => 'attachment_ajax'),
161 array('attachment' => '[0-9]+'));
163 $m->connect('attachment/:attachment/thumbnail',
164 array('action' => 'attachment_thumbnail'),
165 array('attachment' => '[0-9]+'));
167 $m->connect('notice/new', array('action' => 'newnotice'));
168 $m->connect('notice/new?replyto=:replyto',
169 array('action' => 'newnotice'),
170 array('replyto' => '[A-Za-z0-9_-]+'));
171 $m->connect('notice/new?replyto=:replyto&inreplyto=:inreplyto',
172 array('action' => 'newnotice'),
173 array('replyto' => '[A-Za-z0-9_-]+'),
174 array('inreplyto' => '[0-9]+'));
176 $m->connect('notice/:notice/file',
177 array('action' => 'file'),
178 array('notice' => '[0-9]+'));
180 $m->connect('notice/:notice',
181 array('action' => 'shownotice'),
182 array('notice' => '[0-9]+'));
183 $m->connect('notice/delete', array('action' => 'deletenotice'));
184 $m->connect('notice/delete/:notice',
185 array('action' => 'deletenotice'),
186 array('notice' => '[0-9]+'));
188 $m->connect('bookmarklet/new', array('action' => 'bookmarklet'));
192 $m->connect('conversation/:id',
193 array('action' => 'conversation'),
194 array('id' => '[0-9]+'));
196 $m->connect('message/new', array('action' => 'newmessage'));
197 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
198 $m->connect('message/:message',
199 array('action' => 'showmessage'),
200 array('message' => '[0-9]+'));
202 $m->connect('user/:id',
203 array('action' => 'userbyid'),
204 array('id' => '[0-9]+'));
206 $m->connect('tags/', array('action' => 'publictagcloud'));
207 $m->connect('tag/', array('action' => 'publictagcloud'));
208 $m->connect('tags', array('action' => 'publictagcloud'));
209 $m->connect('tag', array('action' => 'publictagcloud'));
210 $m->connect('tag/:tag/rss',
211 array('action' => 'tagrss'),
212 array('tag' => '[a-zA-Z0-9]+'));
213 $m->connect('tag/:tag',
214 array('action' => 'tag'),
215 array('tag' => '[\pL\pN_\-\.]{1,64}'));
217 $m->connect('peopletag/:tag',
218 array('action' => 'peopletag'),
219 array('tag' => '[a-zA-Z0-9]+'));
221 $m->connect('featured/', array('action' => 'featured'));
222 $m->connect('featured', array('action' => 'featured'));
223 $m->connect('favorited/', array('action' => 'favorited'));
224 $m->connect('favorited', array('action' => 'favorited'));
228 $m->connect('group/new', array('action' => 'newgroup'));
230 foreach (array('edit', 'join', 'leave') as $v) {
231 $m->connect('group/:nickname/'.$v,
232 array('action' => $v.'group'),
233 array('nickname' => '[a-zA-Z0-9]+'));
236 foreach (array('members', 'logo', 'rss', 'designsettings') as $n) {
237 $m->connect('group/:nickname/'.$n,
238 array('action' => 'group'.$n),
239 array('nickname' => '[a-zA-Z0-9]+'));
242 $m->connect('group/:nickname/foaf',
243 array('action' => 'foafgroup'),
244 array('nickname' => '[a-zA-Z0-9]+'));
246 $m->connect('group/:nickname/blocked',
247 array('action' => 'blockedfromgroup'),
248 array('nickname' => '[a-zA-Z0-9]+'));
250 $m->connect('group/:nickname/makeadmin',
251 array('action' => 'makeadmin'),
252 array('nickname' => '[a-zA-Z0-9]+'));
254 $m->connect('group/:id/id',
255 array('action' => 'groupbyid'),
256 array('id' => '[0-9]+'));
258 $m->connect('group/:nickname',
259 array('action' => 'showgroup'),
260 array('nickname' => '[a-zA-Z0-9]+'));
262 $m->connect('group/', array('action' => 'groups'));
263 $m->connect('group', array('action' => 'groups'));
264 $m->connect('groups/', array('action' => 'groups'));
265 $m->connect('groups', array('action' => 'groups'));
267 // Twitter-compatible API
271 $m->connect('api/statuses/public_timeline.:format',
272 array('action' => 'ApiTimelinePublic',
273 'format' => '(xml|json|rss|atom)'));
275 $m->connect('api/statuses/friends_timeline.:format',
276 array('action' => 'ApiTimelineFriends',
277 'format' => '(xml|json|rss|atom)'));
279 $m->connect('api/statuses/friends_timeline/:id.:format',
280 array('action' => 'ApiTimelineFriends',
281 'id' => '[a-zA-Z0-9]+',
282 'format' => '(xml|json|rss|atom)'));
283 $m->connect('api/statuses/home_timeline.:format',
284 array('action' => 'ApiTimelineFriends',
285 'format' => '(xml|json|rss|atom)'));
287 $m->connect('api/statuses/home_timeline/:id.:format',
288 array('action' => 'ApiTimelineFriends',
289 'id' => '[a-zA-Z0-9]+',
290 'format' => '(xml|json|rss|atom)'));
292 $m->connect('api/statuses/user_timeline.:format',
293 array('action' => 'ApiTimelineUser',
294 'format' => '(xml|json|rss|atom)'));
296 $m->connect('api/statuses/user_timeline/:id.:format',
297 array('action' => 'ApiTimelineUser',
298 'id' => '[a-zA-Z0-9]+',
299 'format' => '(xml|json|rss|atom)'));
301 $m->connect('api/statuses/mentions.:format',
302 array('action' => 'ApiTimelineMentions',
303 'format' => '(xml|json|rss|atom)'));
305 $m->connect('api/statuses/mentions/:id.:format',
306 array('action' => 'ApiTimelineMentions',
307 'id' => '[a-zA-Z0-9]+',
308 'format' => '(xml|json|rss|atom)'));
310 $m->connect('api/statuses/replies.:format',
311 array('action' => 'ApiTimelineMentions',
312 'format' => '(xml|json|rss|atom)'));
314 $m->connect('api/statuses/replies/:id.:format',
315 array('action' => 'ApiTimelineMentions',
316 'id' => '[a-zA-Z0-9]+',
317 'format' => '(xml|json|rss|atom)'));
319 $m->connect('api/statuses/friends.:format',
320 array('action' => 'ApiUserFriends',
321 'format' => '(xml|json)'));
323 $m->connect('api/statuses/friends/:id.:format',
324 array('action' => 'ApiUserFriends',
325 'id' => '[a-zA-Z0-9]+',
326 'format' => '(xml|json)'));
328 $m->connect('api/statuses/followers.:format',
329 array('action' => 'ApiUserFollowers',
330 'format' => '(xml|json)'));
332 $m->connect('api/statuses/followers/:id.:format',
333 array('action' => 'ApiUserFollowers',
334 'id' => '[a-zA-Z0-9]+',
335 'format' => '(xml|json)'));
337 $m->connect('api/statuses/show.:format',
338 array('action' => 'ApiStatusesShow',
339 'format' => '(xml|json)'));
341 $m->connect('api/statuses/show/:id.:format',
342 array('action' => 'ApiStatusesShow',
344 'format' => '(xml|json)'));
346 $m->connect('api/statuses/update.:format',
347 array('action' => 'ApiStatusesUpdate',
348 'format' => '(xml|json)'));
350 $m->connect('api/statuses/destroy.:format',
351 array('action' => 'ApiStatusesDestroy',
352 'format' => '(xml|json)'));
354 $m->connect('api/statuses/destroy/:id.:format',
355 array('action' => 'ApiStatusesDestroy',
357 'format' => '(xml|json)'));
361 $m->connect('api/users/show/:id.:format',
362 array('action' => 'ApiUserShow',
363 'id' => '[a-zA-Z0-9]+',
364 'format' => '(xml|json)'));
368 $m->connect('api/direct_messages.:format',
369 array('action' => 'ApiDirectMessage',
370 'format' => '(xml|json|rss|atom)'));
372 $m->connect('api/direct_messages/sent.:format',
373 array('action' => 'ApiDirectMessage',
374 'format' => '(xml|json|rss|atom)',
377 $m->connect('api/direct_messages/new.:format',
378 array('action' => 'ApiDirectMessageNew',
379 'format' => '(xml|json)'));
383 $m->connect('api/friendships/show.:format',
384 array('action' => 'ApiFriendshipsShow',
385 'format' => '(xml|json)'));
387 $m->connect('api/friendships/exists.:format',
388 array('action' => 'ApiFriendshipsExists',
389 'format' => '(xml|json)'));
391 $m->connect('api/friendships/create.:format',
392 array('action' => 'ApiFriendshipsCreate',
393 'format' => '(xml|json)'));
395 $m->connect('api/friendships/destroy.:format',
396 array('action' => 'ApiFriendshipsDestroy',
397 'format' => '(xml|json)'));
399 $m->connect('api/friendships/create/:id.:format',
400 array('action' => 'ApiFriendshipsCreate',
401 'id' => '[a-zA-Z0-9]+',
402 'format' => '(xml|json)'));
404 $m->connect('api/friendships/destroy/:id.:format',
405 array('action' => 'ApiFriendshipsDestroy',
406 'id' => '[a-zA-Z0-9]+',
407 'format' => '(xml|json)'));
411 $m->connect('api/friends/ids/:id.:format',
412 array('action' => 'apiFriends',
413 'ids_only' => true));
415 $m->connect('api/followers/ids/:id.:format',
416 array('action' => 'apiFollowers',
417 'ids_only' => true));
419 $m->connect('api/friends/ids.:format',
420 array('action' => 'apiFriends',
421 'ids_only' => true));
423 $m->connect('api/followers/ids.:format',
424 array('action' => 'apiFollowers',
425 'ids_only' => true));
429 $m->connect('api/account/verify_credentials.:format',
430 array('action' => 'ApiAccountVerifyCredentials'));
432 $m->connect('api/account/update_profile.:format',
433 array('action' => 'ApiAccountUpdateProfile'));
435 $m->connect('api/account/update_profile_image.:format',
436 array('action' => 'ApiAccountUpdateProfileImage'));
438 $m->connect('api/account/update_profile_background_image.:format',
439 array('action' => 'ApiAccountUpdateProfileBackgroundImage'));
441 $m->connect('api/account/update_profile_colors.:format',
442 array('action' => 'ApiAccountUpdateProfileColors'));
444 $m->connect('api/account/update_delivery_device.:format',
445 array('action' => 'ApiAccountUpdateDeliveryDevice'));
447 // special case where verify_credentials is called w/out a format
449 $m->connect('api/account/verify_credentials',
450 array('action' => 'ApiAccountVerifyCredentials'));
452 $m->connect('api/account/rate_limit_status.:format',
453 array('action' => 'ApiAccountRateLimitStatus'));
457 $m->connect('api/favorites.:format',
458 array('action' => 'ApiTimelineFavorites',
459 'format' => '(xml|json|rss|atom)'));
461 $m->connect('api/favorites/:id.:format',
462 array('action' => 'ApiTimelineFavorites',
463 'id' => '[a-zA-Z0-9]+',
464 'format' => '(xmljson|rss|atom)'));
466 $m->connect('api/favorites/create/:id.:format',
467 array('action' => 'ApiFavoriteCreate',
468 'id' => '[a-zA-Z0-9]+',
469 'format' => '(xml|json)'));
471 $m->connect('api/favorites/destroy/:id.:format',
472 array('action' => 'ApiFavoriteDestroy',
473 'id' => '[a-zA-Z0-9]+',
474 'format' => '(xml|json)'));
477 $m->connect('api/blocks/create/:id.:format',
478 array('action' => 'ApiBlockCreate',
479 'id' => '[a-zA-Z0-9]+',
480 'format' => '(xml|json)'));
482 $m->connect('api/blocks/destroy/:id.:format',
483 array('action' => 'ApiBlockDestroy',
484 'id' => '[a-zA-Z0-9]+',
485 'format' => '(xml|json)'));
488 $m->connect('api/help/test.:format',
489 array('action' => 'ApiHelpTest',
490 'format' => '(xml|json)'));
494 $m->connect('api/statusnet/version.:format',
495 array('action' => 'ApiStatusnetVersion',
496 'format' => '(xml|json)'));
498 $m->connect('api/statusnet/config.:format',
499 array('action' => 'ApiStatusnetConfig',
500 'format' => '(xml|json)'));
502 // For older methods, we provide "laconica" base action
504 $m->connect('api/laconica/version.:format',
505 array('action' => 'ApiStatusnetVersion',
506 'format' => '(xml|json)'));
508 $m->connect('api/laconica/config.:format',
509 array('action' => 'ApiStatusnetConfig',
510 'format' => '(xml|json)'));
512 // Groups and tags are newer than 0.8.1 so no backward-compatibility
516 //'list' has to be handled differently, as php will not allow a method to be named 'list'
518 $m->connect('api/statusnet/groups/timeline/:id.:format',
519 array('action' => 'ApiTimelineGroup',
520 'id' => '[a-zA-Z0-9]+',
521 'format' => '(xmljson|rss|atom)'));
523 $m->connect('api/statusnet/groups/show.:format',
524 array('action' => 'ApiGroupShow',
525 'format' => '(xml|json)'));
527 $m->connect('api/statusnet/groups/show/:id.:format',
528 array('action' => 'ApiGroupShow',
529 'id' => '[a-zA-Z0-9]+',
530 'format' => '(xml|json)'));
532 $m->connect('api/statusnet/groups/join.:format',
533 array('action' => 'ApiGroupJoin',
534 'id' => '[a-zA-Z0-9]+',
535 'format' => '(xml|json)'));
537 $m->connect('api/statusnet/groups/join/:id.:format',
538 array('action' => 'ApiGroupJoin',
539 'format' => '(xml|json)'));
541 $m->connect('api/statusnet/groups/leave.:format',
542 array('action' => 'ApiGroupLeave',
543 'id' => '[a-zA-Z0-9]+',
544 'format' => '(xml|json)'));
546 $m->connect('api/statusnet/groups/leave/:id.:format',
547 array('action' => 'ApiGroupLeave',
548 'format' => '(xml|json)'));
550 $m->connect('api/statusnet/groups/is_member.:format',
551 array('action' => 'ApiGroupIsMember',
552 'format' => '(xml|json)'));
554 $m->connect('api/statusnet/groups/list.:format',
555 array('action' => 'ApiGroupList',
556 'format' => '(xml|json|rss|atom)'));
558 $m->connect('api/statusnet/groups/list/:id.:format',
559 array('action' => 'ApiGroupList',
560 'id' => '[a-zA-Z0-9]+',
561 'format' => '(xml|json|rss|atom)'));
563 $m->connect('api/statusnet/groups/list_all.:format',
564 array('action' => 'ApiGroupListAll',
565 'format' => '(xml|json|rss|atom)'));
567 $m->connect('api/statusnet/groups/membership.:format',
568 array('action' => 'ApiGroupMembership',
569 'format' => '(xml|json)'));
571 $m->connect('api/statusnet/groups/membership/:id.:format',
572 array('action' => 'ApiGroupMembership',
573 'id' => '[a-zA-Z0-9]+',
574 'format' => '(xml|json)'));
576 $m->connect('api/statusnet/groups/create.:format',
577 array('action' => 'ApiGroupCreate',
578 'format' => '(xml|json)'));
580 $m->connect('api/statusnet/tags/timeline/:tag.:format',
581 array('action' => 'ApiTimelineTag',
582 'format' => '(xmljson|rss|atom)'));
585 $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
586 $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
587 $m->connect('api/trends.json', array('action' => 'twitapitrends'));
589 $m->connect('admin/site', array('action' => 'siteadminpanel'));
590 $m->connect('admin/design', array('action' => 'designadminpanel'));
591 $m->connect('admin/user', array('action' => 'useradminpanel'));
592 $m->connect('admin/paths', array('action' => 'pathsadminpanel'));
594 $m->connect('getfile/:filename',
595 array('action' => 'getfile'),
596 array('filename' => '[A-Za-z0-9._-]+'));
600 foreach (array('subscriptions', 'subscribers',
601 'nudge', 'all', 'foaf', 'xrds',
602 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
603 $m->connect(':nickname/'.$a,
604 array('action' => $a),
605 array('nickname' => '[a-zA-Z0-9]{1,64}'));
608 foreach (array('subscriptions', 'subscribers') as $a) {
609 $m->connect(':nickname/'.$a.'/:tag',
610 array('action' => $a),
611 array('tag' => '[a-zA-Z0-9]+',
612 'nickname' => '[a-zA-Z0-9]{1,64}'));
615 foreach (array('rss', 'groups') as $a) {
616 $m->connect(':nickname/'.$a,
617 array('action' => 'user'.$a),
618 array('nickname' => '[a-zA-Z0-9]{1,64}'));
621 foreach (array('all', 'replies', 'favorites') as $a) {
622 $m->connect(':nickname/'.$a.'/rss',
623 array('action' => $a.'rss'),
624 array('nickname' => '[a-zA-Z0-9]{1,64}'));
627 $m->connect(':nickname/favorites',
628 array('action' => 'showfavorites'),
629 array('nickname' => '[a-zA-Z0-9]{1,64}'));
631 $m->connect(':nickname/avatar/:size',
632 array('action' => 'avatarbynickname'),
633 array('size' => '(original|96|48|24)',
634 'nickname' => '[a-zA-Z0-9]{1,64}'));
636 $m->connect(':nickname/tag/:tag/rss',
637 array('action' => 'userrss'),
638 array('nickname' => '[a-zA-Z0-9]{1,64}'),
639 array('tag' => '[a-zA-Z0-9]+'));
641 $m->connect(':nickname/tag/:tag',
642 array('action' => 'showstream'),
643 array('nickname' => '[a-zA-Z0-9]{1,64}'),
644 array('tag' => '[a-zA-Z0-9]+'));
646 $m->connect(':nickname',
647 array('action' => 'showstream'),
648 array('nickname' => '[a-zA-Z0-9]{1,64}'));
650 Event::handle('RouterInitialized', array($m));
659 $match = $this->m->match($path);
660 } catch (Net_URL_Mapper_InvalidException $e) {
661 common_log(LOG_ERR, "Problem getting route for $path - " .
663 $cac = new ClientErrorAction("Page not found.", 404);
670 function build($action, $args=null, $params=null, $fragment=null)
672 $action_arg = array('action' => $action);
675 $args = array_merge($action_arg, $args);
680 $url = $this->m->generate($args, $params, $fragment);
682 // Due to a bug in the Net_URL_Mapper code, the returned URL may
683 // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
684 // repair that here rather than modifying the upstream code...
686 $qpos = strpos($url, '?');
687 if ($qpos !== false) {
688 $url = substr($url, 0, $qpos+1) .
689 str_replace('?', '&', substr($url, $qpos+1));