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',
102 'deleteuser', 'forward');
104 foreach ($main as $a) {
105 $m->connect('main/'.$a, array('action' => $a));
108 $m->connect('main/sup/:seconds', array('action' => 'sup'),
109 array('seconds' => '[0-9]+'));
111 $m->connect('main/tagother/:id', array('action' => 'tagother'));
113 $m->connect('main/oembed',
114 array('action' => 'oembed'));
116 $m->connect('main/xrds',
117 array('action' => 'publicxrds'));
121 foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
122 $m->connect('main/'.$c.'/:code', array('action' => $c));
127 $m->connect('main/remote', array('action' => 'remotesubscribe'));
128 $m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '[A-Za-z0-9_-]+'));
130 foreach (Router::$bare as $action) {
131 $m->connect('index.php?action=' . $action, array('action' => $action));
136 foreach (array('profile', 'avatar', 'password', 'im',
137 'email', 'sms', 'userdesign', 'other') as $s) {
138 $m->connect('settings/'.$s, array('action' => $s.'settings'));
143 foreach (array('group', 'people', 'notice') as $s) {
144 $m->connect('search/'.$s, array('action' => $s.'search'));
145 $m->connect('search/'.$s.'?q=:q',
146 array('action' => $s.'search'),
150 // The second of these is needed to make the link work correctly
151 // when inserted into the page. The first is needed to match the
152 // route on the way in. Seems to be another Net_URL_Mapper bug to me.
153 $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
154 $m->connect('search/notice/rss?q=:q', array('action' => 'noticesearchrss'),
157 $m->connect('attachment/:attachment',
158 array('action' => 'attachment'),
159 array('attachment' => '[0-9]+'));
161 $m->connect('attachment/:attachment/ajax',
162 array('action' => 'attachment_ajax'),
163 array('attachment' => '[0-9]+'));
165 $m->connect('attachment/:attachment/thumbnail',
166 array('action' => 'attachment_thumbnail'),
167 array('attachment' => '[0-9]+'));
169 $m->connect('notice/new', array('action' => 'newnotice'));
170 $m->connect('notice/new?replyto=:replyto',
171 array('action' => 'newnotice'),
172 array('replyto' => '[A-Za-z0-9_-]+'));
173 $m->connect('notice/new?replyto=:replyto&inreplyto=:inreplyto',
174 array('action' => 'newnotice'),
175 array('replyto' => '[A-Za-z0-9_-]+'),
176 array('inreplyto' => '[0-9]+'));
178 $m->connect('notice/:notice/file',
179 array('action' => 'file'),
180 array('notice' => '[0-9]+'));
182 $m->connect('notice/:notice',
183 array('action' => 'shownotice'),
184 array('notice' => '[0-9]+'));
185 $m->connect('notice/delete', array('action' => 'deletenotice'));
186 $m->connect('notice/delete/:notice',
187 array('action' => 'deletenotice'),
188 array('notice' => '[0-9]+'));
190 $m->connect('bookmarklet/new', array('action' => 'bookmarklet'));
194 $m->connect('conversation/:id',
195 array('action' => 'conversation'),
196 array('id' => '[0-9]+'));
198 $m->connect('message/new', array('action' => 'newmessage'));
199 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
200 $m->connect('message/:message',
201 array('action' => 'showmessage'),
202 array('message' => '[0-9]+'));
204 $m->connect('user/:id',
205 array('action' => 'userbyid'),
206 array('id' => '[0-9]+'));
208 $m->connect('tags/', array('action' => 'publictagcloud'));
209 $m->connect('tag/', array('action' => 'publictagcloud'));
210 $m->connect('tags', array('action' => 'publictagcloud'));
211 $m->connect('tag', array('action' => 'publictagcloud'));
212 $m->connect('tag/:tag/rss',
213 array('action' => 'tagrss'),
214 array('tag' => '[a-zA-Z0-9]+'));
215 $m->connect('tag/:tag',
216 array('action' => 'tag'),
217 array('tag' => '[\pL\pN_\-\.]{1,64}'));
219 $m->connect('peopletag/:tag',
220 array('action' => 'peopletag'),
221 array('tag' => '[a-zA-Z0-9]+'));
223 $m->connect('featured/', array('action' => 'featured'));
224 $m->connect('featured', array('action' => 'featured'));
225 $m->connect('favorited/', array('action' => 'favorited'));
226 $m->connect('favorited', array('action' => 'favorited'));
230 $m->connect('group/new', array('action' => 'newgroup'));
232 foreach (array('edit', 'join', 'leave') as $v) {
233 $m->connect('group/:nickname/'.$v,
234 array('action' => $v.'group'),
235 array('nickname' => '[a-zA-Z0-9]+'));
238 foreach (array('members', 'logo', 'rss', 'designsettings') as $n) {
239 $m->connect('group/:nickname/'.$n,
240 array('action' => 'group'.$n),
241 array('nickname' => '[a-zA-Z0-9]+'));
244 $m->connect('group/:nickname/foaf',
245 array('action' => 'foafgroup'),
246 array('nickname' => '[a-zA-Z0-9]+'));
248 $m->connect('group/:nickname/blocked',
249 array('action' => 'blockedfromgroup'),
250 array('nickname' => '[a-zA-Z0-9]+'));
252 $m->connect('group/:nickname/makeadmin',
253 array('action' => 'makeadmin'),
254 array('nickname' => '[a-zA-Z0-9]+'));
256 $m->connect('group/:id/id',
257 array('action' => 'groupbyid'),
258 array('id' => '[0-9]+'));
260 $m->connect('group/:nickname',
261 array('action' => 'showgroup'),
262 array('nickname' => '[a-zA-Z0-9]+'));
264 $m->connect('group/', array('action' => 'groups'));
265 $m->connect('group', array('action' => 'groups'));
266 $m->connect('groups/', array('action' => 'groups'));
267 $m->connect('groups', array('action' => 'groups'));
269 // Twitter-compatible API
273 $m->connect('api/statuses/public_timeline.:format',
274 array('action' => 'ApiTimelinePublic',
275 'format' => '(xml|json|rss|atom)'));
277 $m->connect('api/statuses/friends_timeline.:format',
278 array('action' => 'ApiTimelineFriends',
279 'format' => '(xml|json|rss|atom)'));
281 $m->connect('api/statuses/friends_timeline/:id.:format',
282 array('action' => 'ApiTimelineFriends',
283 'id' => '[a-zA-Z0-9]+',
284 'format' => '(xml|json|rss|atom)'));
285 $m->connect('api/statuses/home_timeline.:format',
286 array('action' => 'ApiTimelineFriends',
287 'format' => '(xml|json|rss|atom)'));
289 $m->connect('api/statuses/home_timeline/:id.:format',
290 array('action' => 'ApiTimelineFriends',
291 'id' => '[a-zA-Z0-9]+',
292 'format' => '(xml|json|rss|atom)'));
294 $m->connect('api/statuses/user_timeline.:format',
295 array('action' => 'ApiTimelineUser',
296 'format' => '(xml|json|rss|atom)'));
298 $m->connect('api/statuses/user_timeline/:id.:format',
299 array('action' => 'ApiTimelineUser',
300 'id' => '[a-zA-Z0-9]+',
301 'format' => '(xml|json|rss|atom)'));
303 $m->connect('api/statuses/mentions.:format',
304 array('action' => 'ApiTimelineMentions',
305 'format' => '(xml|json|rss|atom)'));
307 $m->connect('api/statuses/mentions/:id.:format',
308 array('action' => 'ApiTimelineMentions',
309 'id' => '[a-zA-Z0-9]+',
310 'format' => '(xml|json|rss|atom)'));
312 $m->connect('api/statuses/replies.:format',
313 array('action' => 'ApiTimelineMentions',
314 'format' => '(xml|json|rss|atom)'));
316 $m->connect('api/statuses/replies/:id.:format',
317 array('action' => 'ApiTimelineMentions',
318 'id' => '[a-zA-Z0-9]+',
319 'format' => '(xml|json|rss|atom)'));
321 $m->connect('api/statuses/friends.:format',
322 array('action' => 'ApiUserFriends',
323 'format' => '(xml|json)'));
325 $m->connect('api/statuses/friends/:id.:format',
326 array('action' => 'ApiUserFriends',
327 'id' => '[a-zA-Z0-9]+',
328 'format' => '(xml|json)'));
330 $m->connect('api/statuses/followers.:format',
331 array('action' => 'ApiUserFollowers',
332 'format' => '(xml|json)'));
334 $m->connect('api/statuses/followers/:id.:format',
335 array('action' => 'ApiUserFollowers',
336 'id' => '[a-zA-Z0-9]+',
337 'format' => '(xml|json)'));
339 $m->connect('api/statuses/show.:format',
340 array('action' => 'ApiStatusesShow',
341 'format' => '(xml|json)'));
343 $m->connect('api/statuses/show/:id.:format',
344 array('action' => 'ApiStatusesShow',
346 'format' => '(xml|json)'));
348 $m->connect('api/statuses/update.:format',
349 array('action' => 'ApiStatusesUpdate',
350 'format' => '(xml|json)'));
352 $m->connect('api/statuses/destroy.:format',
353 array('action' => 'ApiStatusesDestroy',
354 'format' => '(xml|json)'));
356 $m->connect('api/statuses/destroy/:id.:format',
357 array('action' => 'ApiStatusesDestroy',
359 'format' => '(xml|json)'));
363 $m->connect('api/users/show.:format',
364 array('action' => 'ApiUserShow',
365 'format' => '(xml|json)'));
367 $m->connect('api/users/show/:id.:format',
368 array('action' => 'ApiUserShow',
369 'id' => '[a-zA-Z0-9]+',
370 'format' => '(xml|json)'));
374 $m->connect('api/direct_messages.:format',
375 array('action' => 'ApiDirectMessage',
376 'format' => '(xml|json|rss|atom)'));
378 $m->connect('api/direct_messages/sent.:format',
379 array('action' => 'ApiDirectMessage',
380 'format' => '(xml|json|rss|atom)',
383 $m->connect('api/direct_messages/new.:format',
384 array('action' => 'ApiDirectMessageNew',
385 'format' => '(xml|json)'));
389 $m->connect('api/friendships/show.:format',
390 array('action' => 'ApiFriendshipsShow',
391 'format' => '(xml|json)'));
393 $m->connect('api/friendships/exists.:format',
394 array('action' => 'ApiFriendshipsExists',
395 'format' => '(xml|json)'));
397 $m->connect('api/friendships/create.:format',
398 array('action' => 'ApiFriendshipsCreate',
399 'format' => '(xml|json)'));
401 $m->connect('api/friendships/destroy.:format',
402 array('action' => 'ApiFriendshipsDestroy',
403 'format' => '(xml|json)'));
405 $m->connect('api/friendships/create/:id.:format',
406 array('action' => 'ApiFriendshipsCreate',
407 'id' => '[a-zA-Z0-9]+',
408 'format' => '(xml|json)'));
410 $m->connect('api/friendships/destroy/:id.:format',
411 array('action' => 'ApiFriendshipsDestroy',
412 'id' => '[a-zA-Z0-9]+',
413 'format' => '(xml|json)'));
417 $m->connect('api/friends/ids/:id.:format',
418 array('action' => 'apiFriends',
419 'ids_only' => true));
421 $m->connect('api/followers/ids/:id.:format',
422 array('action' => 'apiFollowers',
423 'ids_only' => true));
425 $m->connect('api/friends/ids.:format',
426 array('action' => 'apiFriends',
427 'ids_only' => true));
429 $m->connect('api/followers/ids.:format',
430 array('action' => 'apiFollowers',
431 'ids_only' => true));
435 $m->connect('api/account/verify_credentials.:format',
436 array('action' => 'ApiAccountVerifyCredentials'));
438 $m->connect('api/account/update_profile.:format',
439 array('action' => 'ApiAccountUpdateProfile'));
441 $m->connect('api/account/update_profile_image.:format',
442 array('action' => 'ApiAccountUpdateProfileImage'));
444 $m->connect('api/account/update_profile_background_image.:format',
445 array('action' => 'ApiAccountUpdateProfileBackgroundImage'));
447 $m->connect('api/account/update_profile_colors.:format',
448 array('action' => 'ApiAccountUpdateProfileColors'));
450 $m->connect('api/account/update_delivery_device.:format',
451 array('action' => 'ApiAccountUpdateDeliveryDevice'));
453 // special case where verify_credentials is called w/out a format
455 $m->connect('api/account/verify_credentials',
456 array('action' => 'ApiAccountVerifyCredentials'));
458 $m->connect('api/account/rate_limit_status.:format',
459 array('action' => 'ApiAccountRateLimitStatus'));
463 $m->connect('api/favorites.:format',
464 array('action' => 'ApiTimelineFavorites',
465 'format' => '(xml|json|rss|atom)'));
467 $m->connect('api/favorites/:id.:format',
468 array('action' => 'ApiTimelineFavorites',
469 'id' => '[a-zA-Z0-9]+',
470 'format' => '(xmljson|rss|atom)'));
472 $m->connect('api/favorites/create/:id.:format',
473 array('action' => 'ApiFavoriteCreate',
474 'id' => '[a-zA-Z0-9]+',
475 'format' => '(xml|json)'));
477 $m->connect('api/favorites/destroy/:id.:format',
478 array('action' => 'ApiFavoriteDestroy',
479 'id' => '[a-zA-Z0-9]+',
480 'format' => '(xml|json)'));
483 $m->connect('api/blocks/create/:id.:format',
484 array('action' => 'ApiBlockCreate',
485 'id' => '[a-zA-Z0-9]+',
486 'format' => '(xml|json)'));
488 $m->connect('api/blocks/destroy/:id.:format',
489 array('action' => 'ApiBlockDestroy',
490 'id' => '[a-zA-Z0-9]+',
491 'format' => '(xml|json)'));
494 $m->connect('api/help/test.:format',
495 array('action' => 'ApiHelpTest',
496 'format' => '(xml|json)'));
500 $m->connect('api/statusnet/version.:format',
501 array('action' => 'ApiStatusnetVersion',
502 'format' => '(xml|json)'));
504 $m->connect('api/statusnet/config.:format',
505 array('action' => 'ApiStatusnetConfig',
506 'format' => '(xml|json)'));
508 // For older methods, we provide "laconica" base action
510 $m->connect('api/laconica/version.:format',
511 array('action' => 'ApiStatusnetVersion',
512 'format' => '(xml|json)'));
514 $m->connect('api/laconica/config.:format',
515 array('action' => 'ApiStatusnetConfig',
516 'format' => '(xml|json)'));
518 // Groups and tags are newer than 0.8.1 so no backward-compatibility
522 //'list' has to be handled differently, as php will not allow a method to be named 'list'
524 $m->connect('api/statusnet/groups/timeline/:id.:format',
525 array('action' => 'ApiTimelineGroup',
526 'id' => '[a-zA-Z0-9]+',
527 'format' => '(xmljson|rss|atom)'));
529 $m->connect('api/statusnet/groups/show.:format',
530 array('action' => 'ApiGroupShow',
531 'format' => '(xml|json)'));
533 $m->connect('api/statusnet/groups/show/:id.:format',
534 array('action' => 'ApiGroupShow',
535 'id' => '[a-zA-Z0-9]+',
536 'format' => '(xml|json)'));
538 $m->connect('api/statusnet/groups/join.:format',
539 array('action' => 'ApiGroupJoin',
540 'id' => '[a-zA-Z0-9]+',
541 'format' => '(xml|json)'));
543 $m->connect('api/statusnet/groups/join/:id.:format',
544 array('action' => 'ApiGroupJoin',
545 'format' => '(xml|json)'));
547 $m->connect('api/statusnet/groups/leave.:format',
548 array('action' => 'ApiGroupLeave',
549 'id' => '[a-zA-Z0-9]+',
550 'format' => '(xml|json)'));
552 $m->connect('api/statusnet/groups/leave/:id.:format',
553 array('action' => 'ApiGroupLeave',
554 'format' => '(xml|json)'));
556 $m->connect('api/statusnet/groups/is_member.:format',
557 array('action' => 'ApiGroupIsMember',
558 'format' => '(xml|json)'));
560 $m->connect('api/statusnet/groups/list.:format',
561 array('action' => 'ApiGroupList',
562 'format' => '(xml|json|rss|atom)'));
564 $m->connect('api/statusnet/groups/list/:id.:format',
565 array('action' => 'ApiGroupList',
566 'id' => '[a-zA-Z0-9]+',
567 'format' => '(xml|json|rss|atom)'));
569 $m->connect('api/statusnet/groups/list_all.:format',
570 array('action' => 'ApiGroupListAll',
571 'format' => '(xml|json|rss|atom)'));
573 $m->connect('api/statusnet/groups/membership.:format',
574 array('action' => 'ApiGroupMembership',
575 'format' => '(xml|json)'));
577 $m->connect('api/statusnet/groups/membership/:id.:format',
578 array('action' => 'ApiGroupMembership',
579 'id' => '[a-zA-Z0-9]+',
580 'format' => '(xml|json)'));
582 $m->connect('api/statusnet/groups/create.:format',
583 array('action' => 'ApiGroupCreate',
584 'format' => '(xml|json)'));
586 $m->connect('api/statusnet/tags/timeline/:tag.:format',
587 array('action' => 'ApiTimelineTag',
588 'format' => '(xmljson|rss|atom)'));
591 $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
592 $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
593 $m->connect('api/trends.json', array('action' => 'twitapitrends'));
595 $m->connect('admin/site', array('action' => 'siteadminpanel'));
596 $m->connect('admin/design', array('action' => 'designadminpanel'));
597 $m->connect('admin/user', array('action' => 'useradminpanel'));
598 $m->connect('admin/paths', array('action' => 'pathsadminpanel'));
600 $m->connect('getfile/:filename',
601 array('action' => 'getfile'),
602 array('filename' => '[A-Za-z0-9._-]+'));
606 foreach (array('subscriptions', 'subscribers',
607 'nudge', 'all', 'foaf', 'xrds',
608 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
609 $m->connect(':nickname/'.$a,
610 array('action' => $a),
611 array('nickname' => '[a-zA-Z0-9]{1,64}'));
614 foreach (array('subscriptions', 'subscribers') as $a) {
615 $m->connect(':nickname/'.$a.'/:tag',
616 array('action' => $a),
617 array('tag' => '[a-zA-Z0-9]+',
618 'nickname' => '[a-zA-Z0-9]{1,64}'));
621 foreach (array('rss', 'groups') as $a) {
622 $m->connect(':nickname/'.$a,
623 array('action' => 'user'.$a),
624 array('nickname' => '[a-zA-Z0-9]{1,64}'));
627 foreach (array('all', 'replies', 'favorites') as $a) {
628 $m->connect(':nickname/'.$a.'/rss',
629 array('action' => $a.'rss'),
630 array('nickname' => '[a-zA-Z0-9]{1,64}'));
633 $m->connect(':nickname/favorites',
634 array('action' => 'showfavorites'),
635 array('nickname' => '[a-zA-Z0-9]{1,64}'));
637 $m->connect(':nickname/avatar/:size',
638 array('action' => 'avatarbynickname'),
639 array('size' => '(original|96|48|24)',
640 'nickname' => '[a-zA-Z0-9]{1,64}'));
642 $m->connect(':nickname/tag/:tag/rss',
643 array('action' => 'userrss'),
644 array('nickname' => '[a-zA-Z0-9]{1,64}'),
645 array('tag' => '[a-zA-Z0-9]+'));
647 $m->connect(':nickname/tag/:tag',
648 array('action' => 'showstream'),
649 array('nickname' => '[a-zA-Z0-9]{1,64}'),
650 array('tag' => '[a-zA-Z0-9]+'));
652 $m->connect(':nickname',
653 array('action' => 'showstream'),
654 array('nickname' => '[a-zA-Z0-9]{1,64}'));
656 Event::handle('RouterInitialized', array($m));
665 $match = $this->m->match($path);
666 } catch (Net_URL_Mapper_InvalidException $e) {
667 common_log(LOG_ERR, "Problem getting route for $path - " .
669 $cac = new ClientErrorAction("Page not found.", 404);
676 function build($action, $args=null, $params=null, $fragment=null)
678 $action_arg = array('action' => $action);
681 $args = array_merge($action_arg, $args);
686 $url = $this->m->generate($args, $params, $fragment);
688 // Due to a bug in the Net_URL_Mapper code, the returned URL may
689 // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
690 // repair that here rather than modifying the upstream code...
692 $qpos = strpos($url, '?');
693 if ($qpos !== false) {
694 $url = substr($url, 0, $qpos+1) .
695 str_replace('?', '&', substr($url, $qpos+1));