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');
101 foreach ($main as $a) {
102 $m->connect('main/'.$a, array('action' => $a));
105 $m->connect('main/sup/:seconds', array('action' => 'sup'),
106 array('seconds' => '[0-9]+'));
108 $m->connect('main/tagother/:id', array('action' => 'tagother'));
110 $m->connect('main/oembed',
111 array('action' => 'oembed'));
113 $m->connect('main/xrds',
114 array('action' => 'publicxrds'));
118 foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
119 $m->connect('main/'.$c.'/:code', array('action' => $c));
124 $m->connect('main/remote', array('action' => 'remotesubscribe'));
125 $m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '['.NICKNAME_FMT.']+'));
127 foreach (Router::$bare as $action) {
128 $m->connect('index.php?action=' . $action, array('action' => $action));
133 foreach (array('profile', 'avatar', 'password', 'im',
134 'email', 'sms', 'userdesign', 'other') as $s) {
135 $m->connect('settings/'.$s, array('action' => $s.'settings'));
140 foreach (array('group', 'people', 'notice') as $s) {
141 $m->connect('search/'.$s, array('action' => $s.'search'));
142 $m->connect('search/'.$s.'?q=:q',
143 array('action' => $s.'search'),
147 // The second of these is needed to make the link work correctly
148 // when inserted into the page. The first is needed to match the
149 // route on the way in. Seems to be another Net_URL_Mapper bug to me.
150 $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
151 $m->connect('search/notice/rss?q=:q', array('action' => 'noticesearchrss'),
154 $m->connect('attachment/:attachment',
155 array('action' => 'attachment'),
156 array('attachment' => '[0-9]+'));
158 $m->connect('attachment/:attachment/ajax',
159 array('action' => 'attachment_ajax'),
160 array('attachment' => '[0-9]+'));
162 $m->connect('attachment/:attachment/thumbnail',
163 array('action' => 'attachment_thumbnail'),
164 array('attachment' => '[0-9]+'));
166 $m->connect('notice/new', array('action' => 'newnotice'));
167 $m->connect('notice/new?replyto=:replyto',
168 array('action' => 'newnotice'),
169 array('replyto' => '['.NICKNAME_FMT.']+'));
170 $m->connect('notice/new?replyto=:replyto&inreplyto=:inreplyto',
171 array('action' => 'newnotice'),
172 array('replyto' => '['.NICKNAME_FMT.']+'),
173 array('inreplyto' => '[0-9]+'));
175 $m->connect('notice/:notice/file',
176 array('action' => 'file'),
177 array('notice' => '[0-9]+'));
179 $m->connect('notice/:notice',
180 array('action' => 'shownotice'),
181 array('notice' => '[0-9]+'));
182 $m->connect('notice/delete', array('action' => 'deletenotice'));
183 $m->connect('notice/delete/:notice',
184 array('action' => 'deletenotice'),
185 array('notice' => '[0-9]+'));
187 $m->connect('bookmarklet/new', array('action' => 'bookmarklet'));
191 $m->connect('conversation/:id',
192 array('action' => 'conversation'),
193 array('id' => '[0-9]+'));
195 $m->connect('message/new', array('action' => 'newmessage'));
196 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '['.NICKNAME_FMT.']+'));
197 $m->connect('message/:message',
198 array('action' => 'showmessage'),
199 array('message' => '[0-9]+'));
201 $m->connect('user/:id',
202 array('action' => 'userbyid'),
203 array('id' => '[0-9]+'));
205 $m->connect('tags/', array('action' => 'publictagcloud'));
206 $m->connect('tag/', array('action' => 'publictagcloud'));
207 $m->connect('tags', array('action' => 'publictagcloud'));
208 $m->connect('tag', array('action' => 'publictagcloud'));
209 $m->connect('tag/:tag/rss',
210 array('action' => 'tagrss'),
211 array('tag' => '[a-zA-Z0-9]+'));
212 $m->connect('tag/:tag',
213 array('action' => 'tag'),
214 array('tag' => '[\pL\pN_\-\.]{1,64}'));
216 $m->connect('peopletag/:tag',
217 array('action' => 'peopletag'),
218 array('tag' => '[a-zA-Z0-9]+'));
220 $m->connect('featured/', array('action' => 'featured'));
221 $m->connect('featured', array('action' => 'featured'));
222 $m->connect('favorited/', array('action' => 'favorited'));
223 $m->connect('favorited', array('action' => 'favorited'));
227 $m->connect('group/new', array('action' => 'newgroup'));
229 foreach (array('edit', 'join', 'leave') as $v) {
230 $m->connect('group/:nickname/'.$v,
231 array('action' => $v.'group'),
232 array('nickname' => '[a-zA-Z0-9]+'));
235 foreach (array('members', 'logo', 'rss', 'designsettings') as $n) {
236 $m->connect('group/:nickname/'.$n,
237 array('action' => 'group'.$n),
238 array('nickname' => '[a-zA-Z0-9]+'));
241 $m->connect('group/:nickname/foaf',
242 array('action' => 'foafgroup'),
243 array('nickname' => '[a-zA-Z0-9]+'));
245 $m->connect('group/:nickname/blocked',
246 array('action' => 'blockedfromgroup'),
247 array('nickname' => '[a-zA-Z0-9]+'));
249 $m->connect('group/:nickname/makeadmin',
250 array('action' => 'makeadmin'),
251 array('nickname' => '[a-zA-Z0-9]+'));
253 $m->connect('group/:id/id',
254 array('action' => 'groupbyid'),
255 array('id' => '[0-9]+'));
257 $m->connect('group/:nickname',
258 array('action' => 'showgroup'),
259 array('nickname' => '[a-zA-Z0-9]+'));
261 $m->connect('group/', array('action' => 'groups'));
262 $m->connect('group', array('action' => 'groups'));
263 $m->connect('groups/', array('action' => 'groups'));
264 $m->connect('groups', array('action' => 'groups'));
266 // Twitter-compatible API
270 $m->connect('api/statuses/public_timeline.:format',
271 array('action' => 'ApiTimelinePublic',
272 'format' => '(xml|json|rss|atom)'));
274 $m->connect('api/statuses/friends_timeline.:format',
275 array('action' => 'ApiTimelineFriends',
276 'format' => '(xml|json|rss|atom)'));
278 $m->connect('api/statuses/friends_timeline/:id.:format',
279 array('action' => 'ApiTimelineFriends',
280 'id' => '['.NICKNAME_FMT.']+',
281 'format' => '(xml|json|rss|atom)'));
282 $m->connect('api/statuses/home_timeline.:format',
283 array('action' => 'ApiTimelineFriends',
284 'format' => '(xml|json|rss|atom)'));
286 $m->connect('api/statuses/home_timeline/:id.:format',
287 array('action' => 'ApiTimelineFriends',
288 'id' => '['.NICKNAME_FMT.']+',
289 'format' => '(xml|json|rss|atom)'));
291 $m->connect('api/statuses/user_timeline.:format',
292 array('action' => 'ApiTimelineUser',
293 'format' => '(xml|json|rss|atom)'));
295 $m->connect('api/statuses/user_timeline/:id.:format',
296 array('action' => 'ApiTimelineUser',
297 'id' => '['.NICKNAME_FMT.']+',
298 'format' => '(xml|json|rss|atom)'));
300 $m->connect('api/statuses/mentions.:format',
301 array('action' => 'ApiTimelineMentions',
302 'format' => '(xml|json|rss|atom)'));
304 $m->connect('api/statuses/mentions/:id.:format',
305 array('action' => 'ApiTimelineMentions',
306 'id' => '['.NICKNAME_FMT.']+',
307 'format' => '(xml|json|rss|atom)'));
309 $m->connect('api/statuses/replies.:format',
310 array('action' => 'ApiTimelineMentions',
311 'format' => '(xml|json|rss|atom)'));
313 $m->connect('api/statuses/replies/:id.:format',
314 array('action' => 'ApiTimelineMentions',
315 'id' => '['.NICKNAME_FMT.']+',
316 'format' => '(xml|json|rss|atom)'));
318 $m->connect('api/statuses/friends.:format',
319 array('action' => 'ApiUserFriends',
320 'format' => '(xml|json)'));
322 $m->connect('api/statuses/friends/:id.:format',
323 array('action' => 'ApiUserFriends',
324 'id' => '['.NICKNAME_FMT.']+',
325 'format' => '(xml|json)'));
327 $m->connect('api/statuses/followers.:format',
328 array('action' => 'ApiUserFollowers',
329 'format' => '(xml|json)'));
331 $m->connect('api/statuses/followers/:id.:format',
332 array('action' => 'ApiUserFollowers',
333 'id' => '['.NICKNAME_FMT.']+',
334 'format' => '(xml|json)'));
336 $m->connect('api/statuses/show.:format',
337 array('action' => 'ApiStatusesShow',
338 'format' => '(xml|json)'));
340 $m->connect('api/statuses/show/:id.:format',
341 array('action' => 'ApiStatusesShow',
343 'format' => '(xml|json)'));
345 $m->connect('api/statuses/update.:format',
346 array('action' => 'ApiStatusesUpdate',
347 'format' => '(xml|json)'));
349 $m->connect('api/statuses/destroy.:format',
350 array('action' => 'ApiStatusesDestroy',
351 'format' => '(xml|json)'));
353 $m->connect('api/statuses/destroy/:id.:format',
354 array('action' => 'ApiStatusesDestroy',
356 'format' => '(xml|json)'));
360 $m->connect('api/users/show/:id.:format',
361 array('action' => 'ApiUserShow',
362 'id' => '['.NICKNAME_FMT.']+',
363 'format' => '(xml|json)'));
367 $m->connect('api/direct_messages.:format',
368 array('action' => 'ApiDirectMessage',
369 'format' => '(xml|json|rss|atom)'));
371 $m->connect('api/direct_messages/sent.:format',
372 array('action' => 'ApiDirectMessage',
373 'format' => '(xml|json|rss|atom)',
376 $m->connect('api/direct_messages/new.:format',
377 array('action' => 'ApiDirectMessageNew',
378 'format' => '(xml|json)'));
382 $m->connect('api/friendships/show.:format',
383 array('action' => 'ApiFriendshipsShow',
384 'format' => '(xml|json)'));
386 $m->connect('api/friendships/exists.:format',
387 array('action' => 'ApiFriendshipsExists',
388 'format' => '(xml|json)'));
390 $m->connect('api/friendships/create.:format',
391 array('action' => 'ApiFriendshipsCreate',
392 'format' => '(xml|json)'));
394 $m->connect('api/friendships/destroy.:format',
395 array('action' => 'ApiFriendshipsDestroy',
396 'format' => '(xml|json)'));
398 $m->connect('api/friendships/create/:id.:format',
399 array('action' => 'ApiFriendshipsCreate',
400 'id' => '['.NICKNAME_FMT.']+',
401 'format' => '(xml|json)'));
403 $m->connect('api/friendships/destroy/:id.:format',
404 array('action' => 'ApiFriendshipsDestroy',
405 'id' => '['.NICKNAME_FMT.']+',
406 'format' => '(xml|json)'));
410 $m->connect('api/friends/ids/:id.:format',
411 array('action' => 'apiFriends',
412 'ids_only' => true));
414 $m->connect('api/followers/ids/:id.:format',
415 array('action' => 'apiFollowers',
416 'ids_only' => true));
418 $m->connect('api/friends/ids.:format',
419 array('action' => 'apiFriends',
420 'ids_only' => true));
422 $m->connect('api/followers/ids.:format',
423 array('action' => 'apiFollowers',
424 'ids_only' => true));
428 $m->connect('api/account/verify_credentials.:format',
429 array('action' => 'ApiAccountVerifyCredentials'));
431 $m->connect('api/account/update_profile.:format',
432 array('action' => 'ApiAccountUpdateProfile'));
434 $m->connect('api/account/update_profile_image.:format',
435 array('action' => 'ApiAccountUpdateProfileImage'));
437 $m->connect('api/account/update_profile_background_image.:format',
438 array('action' => 'ApiAccountUpdateProfileBackgroundImage'));
440 $m->connect('api/account/update_profile_colors.:format',
441 array('action' => 'ApiAccountUpdateProfileColors'));
443 $m->connect('api/account/update_delivery_device.:format',
444 array('action' => 'ApiAccountUpdateDeliveryDevice'));
446 // special case where verify_credentials is called w/out a format
448 $m->connect('api/account/verify_credentials',
449 array('action' => 'ApiAccountVerifyCredentials'));
451 $m->connect('api/account/rate_limit_status.:format',
452 array('action' => 'ApiAccountRateLimitStatus'));
456 $m->connect('api/favorites.:format',
457 array('action' => 'ApiTimelineFavorites',
458 'format' => '(xml|json|rss|atom)'));
460 $m->connect('api/favorites/:id.:format',
461 array('action' => 'ApiTimelineFavorites',
462 'id' => '['.NICKNAME_FMT.']+',
463 'format' => '(xmljson|rss|atom)'));
465 $m->connect('api/favorites/create/:id.:format',
466 array('action' => 'ApiFavoriteCreate',
467 'id' => '['.NICKNAME_FMT.']+',
468 'format' => '(xml|json)'));
470 $m->connect('api/favorites/destroy/:id.:format',
471 array('action' => 'ApiFavoriteDestroy',
472 'id' => '['.NICKNAME_FMT.']+',
473 'format' => '(xml|json)'));
476 $m->connect('api/blocks/create/:id.:format',
477 array('action' => 'ApiBlockCreate',
478 'id' => '['.NICKNAME_FMT.']+',
479 'format' => '(xml|json)'));
481 $m->connect('api/blocks/destroy/:id.:format',
482 array('action' => 'ApiBlockDestroy',
483 'id' => '['.NICKNAME_FMT.']+',
484 'format' => '(xml|json)'));
487 $m->connect('api/help/test.:format',
488 array('action' => 'ApiHelpTest',
489 'format' => '(xml|json)'));
493 $m->connect('api/statusnet/version.:format',
494 array('action' => 'ApiStatusnetVersion',
495 'format' => '(xml|json)'));
497 $m->connect('api/statusnet/config.:format',
498 array('action' => 'ApiStatusnetConfig',
499 'format' => '(xml|json)'));
501 // For older methods, we provide "laconica" base action
503 $m->connect('api/laconica/version.:format',
504 array('action' => 'ApiStatusnetVersion',
505 'format' => '(xml|json)'));
507 $m->connect('api/laconica/config.:format',
508 array('action' => 'ApiStatusnetConfig',
509 'format' => '(xml|json)'));
511 // Groups and tags are newer than 0.8.1 so no backward-compatibility
515 //'list' has to be handled differently, as php will not allow a method to be named 'list'
517 $m->connect('api/statusnet/groups/timeline/:id.:format',
518 array('action' => 'ApiTimelineGroup',
519 'id' => '[a-zA-Z0-9]+',
520 'format' => '(xmljson|rss|atom)'));
522 $m->connect('api/statusnet/groups/show.:format',
523 array('action' => 'ApiGroupShow',
524 'format' => '(xml|json)'));
526 $m->connect('api/statusnet/groups/show/:id.:format',
527 array('action' => 'ApiGroupShow',
528 'id' => '[a-zA-Z0-9]+',
529 'format' => '(xml|json)'));
531 $m->connect('api/statusnet/groups/join.:format',
532 array('action' => 'ApiGroupJoin',
533 'id' => '[a-zA-Z0-9]+',
534 'format' => '(xml|json)'));
536 $m->connect('api/statusnet/groups/join/:id.:format',
537 array('action' => 'ApiGroupJoin',
538 'format' => '(xml|json)'));
540 $m->connect('api/statusnet/groups/leave.:format',
541 array('action' => 'ApiGroupLeave',
542 'id' => '[a-zA-Z0-9]+',
543 'format' => '(xml|json)'));
545 $m->connect('api/statusnet/groups/leave/:id.:format',
546 array('action' => 'ApiGroupLeave',
547 'format' => '(xml|json)'));
549 $m->connect('api/statusnet/groups/is_member.:format',
550 array('action' => 'ApiGroupIsMember',
551 'format' => '(xml|json)'));
553 $m->connect('api/statusnet/groups/list.:format',
554 array('action' => 'ApiGroupList',
555 'format' => '(xml|json|rss|atom)'));
557 $m->connect('api/statusnet/groups/list/:id.:format',
558 array('action' => 'ApiGroupList',
559 'id' => '[a-zA-Z0-9]+',
560 'format' => '(xml|json|rss|atom)'));
562 $m->connect('api/statusnet/groups/list_all.:format',
563 array('action' => 'ApiGroupListAll',
564 'format' => '(xml|json|rss|atom)'));
566 $m->connect('api/statusnet/groups/membership.:format',
567 array('action' => 'ApiGroupMembership',
568 'format' => '(xml|json)'));
570 $m->connect('api/statusnet/groups/membership/:id.:format',
571 array('action' => 'ApiGroupMembership',
572 'id' => '[a-zA-Z0-9]+',
573 'format' => '(xml|json)'));
575 $m->connect('api/statusnet/groups/create.:format',
576 array('action' => 'ApiGroupCreate',
577 'format' => '(xml|json)'));
579 $m->connect('api/statusnet/tags/timeline/:tag.:format',
580 array('action' => 'ApiTimelineTag',
581 'format' => '(xmljson|rss|atom)'));
584 $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
585 $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
586 $m->connect('api/trends.json', array('action' => 'twitapitrends'));
588 $m->connect('admin/site', array('action' => 'siteadminpanel'));
590 $m->connect('getfile/:filename',
591 array('action' => 'getfile'),
592 array('filename' => '[A-Za-z0-9._-]+'));
596 foreach (array('subscriptions', 'subscribers',
597 'nudge', 'all', 'foaf', 'xrds',
598 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
599 $m->connect(':nickname/'.$a,
600 array('action' => $a),
601 array('nickname' => '['.NICKNAME_FMT.']{1,64}'));
604 foreach (array('subscriptions', 'subscribers') as $a) {
605 $m->connect(':nickname/'.$a.'/:tag',
606 array('action' => $a),
607 array('tag' => '[a-zA-Z0-9]+',
608 'nickname' => '['.NICKNAME_FMT.']{1,64}'));
611 foreach (array('rss', 'groups') as $a) {
612 $m->connect(':nickname/'.$a,
613 array('action' => 'user'.$a),
614 array('nickname' => '[a-zA-Z0-9]{1,64}'));
617 foreach (array('all', 'replies', 'favorites') as $a) {
618 $m->connect(':nickname/'.$a.'/rss',
619 array('action' => $a.'rss'),
620 array('nickname' => '['.NICKNAME_FMT.']{1,64}'));
623 $m->connect(':nickname/favorites',
624 array('action' => 'showfavorites'),
625 array('nickname' => '['.NICKNAME_FMT.']{1,64}'));
627 $m->connect(':nickname/avatar/:size',
628 array('action' => 'avatarbynickname'),
629 array('size' => '(original|96|48|24)',
630 'nickname' => '['.NICKNAME_FMT.']{1,64}'));
632 $m->connect(':nickname/tag/:tag/rss',
633 array('action' => 'userrss'),
634 array('nickname' => '['.NICKNAME_FMT.']{1,64}'),
635 array('tag' => '[a-zA-Z0-9]+'));
637 $m->connect(':nickname/tag/:tag',
638 array('action' => 'showstream'),
639 array('nickname' => '['.NICKNAME_FMT.']{1,64}'),
640 array('tag' => '[a-zA-Z0-9]+'));
642 $m->connect(':nickname',
643 array('action' => 'showstream'),
644 array('nickname' => '['.NICKNAME_FMT.']{1,64}'));
646 Event::handle('RouterInitialized', array($m));
655 $match = $this->m->match($path);
656 } catch (Net_URL_Mapper_InvalidException $e) {
657 common_log(LOG_ERR, "Problem getting route for $path - " .
659 $cac = new ClientErrorAction("Page not found.", 404);
666 function build($action, $args=null, $params=null, $fragment=null)
668 $action_arg = array('action' => $action);
671 $args = array_merge($action_arg, $args);
676 $url = $this->m->generate($args, $params, $fragment);
678 // Due to a bug in the Net_URL_Mapper code, the returned URL may
679 // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
680 // repair that here rather than modifying the upstream code...
682 $qpos = strpos($url, '?');
683 if ($qpos !== false) {
684 $url = substr($url, 0, $qpos+1) .
685 str_replace('?', '&', substr($url, $qpos+1));