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');
99 foreach ($main as $a) {
100 $m->connect('main/'.$a, array('action' => $a));
103 $m->connect('main/sup/:seconds', array('action' => 'sup'),
104 array('seconds' => '[0-9]+'));
106 $m->connect('main/tagother/:id', array('action' => 'tagother'));
108 $m->connect('main/oembed',
109 array('action' => 'oembed'));
111 $m->connect('main/xrds',
112 array('action' => 'publicxrds'));
116 foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
117 $m->connect('main/'.$c.'/:code', array('action' => $c));
122 $m->connect('main/remote', array('action' => 'remotesubscribe'));
123 $m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '[A-Za-z0-9_-]+'));
125 foreach (Router::$bare as $action) {
126 $m->connect('index.php?action=' . $action, array('action' => $action));
131 foreach (array('profile', 'avatar', 'password', 'im',
132 'email', 'sms', 'userdesign', 'other') as $s) {
133 $m->connect('settings/'.$s, array('action' => $s.'settings'));
138 foreach (array('group', 'people', 'notice') as $s) {
139 $m->connect('search/'.$s, array('action' => $s.'search'));
140 $m->connect('search/'.$s.'?q=:q',
141 array('action' => $s.'search'),
145 // The second of these is needed to make the link work correctly
146 // when inserted into the page. The first is needed to match the
147 // route on the way in. Seems to be another Net_URL_Mapper bug to me.
148 $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
149 $m->connect('search/notice/rss?q=:q', array('action' => 'noticesearchrss'),
152 $m->connect('attachment/:attachment',
153 array('action' => 'attachment'),
154 array('attachment' => '[0-9]+'));
156 $m->connect('attachment/:attachment/ajax',
157 array('action' => 'attachment_ajax'),
158 array('attachment' => '[0-9]+'));
160 $m->connect('attachment/:attachment/thumbnail',
161 array('action' => 'attachment_thumbnail'),
162 array('attachment' => '[0-9]+'));
164 $m->connect('notice/new', array('action' => 'newnotice'));
165 $m->connect('notice/new?replyto=:replyto',
166 array('action' => 'newnotice'),
167 array('replyto' => '[A-Za-z0-9_-]+'));
168 $m->connect('notice/new?replyto=:replyto&inreplyto=:inreplyto',
169 array('action' => 'newnotice'),
170 array('replyto' => '[A-Za-z0-9_-]+'),
171 array('inreplyto' => '[0-9]+'));
173 $m->connect('notice/:notice/file',
174 array('action' => 'file'),
175 array('notice' => '[0-9]+'));
177 $m->connect('notice/:notice',
178 array('action' => 'shownotice'),
179 array('notice' => '[0-9]+'));
180 $m->connect('notice/delete', array('action' => 'deletenotice'));
181 $m->connect('notice/delete/:notice',
182 array('action' => 'deletenotice'),
183 array('notice' => '[0-9]+'));
185 $m->connect('bookmarklet/new', array('action' => 'bookmarklet'));
189 $m->connect('conversation/:id',
190 array('action' => 'conversation'),
191 array('id' => '[0-9]+'));
193 $m->connect('message/new', array('action' => 'newmessage'));
194 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
195 $m->connect('message/:message',
196 array('action' => 'showmessage'),
197 array('message' => '[0-9]+'));
199 $m->connect('user/:id',
200 array('action' => 'userbyid'),
201 array('id' => '[0-9]+'));
203 $m->connect('tags/', array('action' => 'publictagcloud'));
204 $m->connect('tag/', array('action' => 'publictagcloud'));
205 $m->connect('tags', array('action' => 'publictagcloud'));
206 $m->connect('tag', array('action' => 'publictagcloud'));
207 $m->connect('tag/:tag/rss',
208 array('action' => 'tagrss'),
209 array('tag' => '[a-zA-Z0-9]+'));
210 $m->connect('tag/:tag',
211 array('action' => 'tag'),
212 array('tag' => '[\pL\pN_\-\.]{1,64}'));
214 $m->connect('peopletag/:tag',
215 array('action' => 'peopletag'),
216 array('tag' => '[a-zA-Z0-9]+'));
218 $m->connect('featured/', array('action' => 'featured'));
219 $m->connect('featured', array('action' => 'featured'));
220 $m->connect('favorited/', array('action' => 'favorited'));
221 $m->connect('favorited', array('action' => 'favorited'));
225 $m->connect('group/new', array('action' => 'newgroup'));
227 foreach (array('edit', 'join', 'leave') as $v) {
228 $m->connect('group/:nickname/'.$v,
229 array('action' => $v.'group'),
230 array('nickname' => '[a-zA-Z0-9]+'));
233 foreach (array('members', 'logo', 'rss', 'designsettings') as $n) {
234 $m->connect('group/:nickname/'.$n,
235 array('action' => 'group'.$n),
236 array('nickname' => '[a-zA-Z0-9]+'));
239 $m->connect('group/:nickname/foaf',
240 array('action' => 'foafgroup'),
241 array('nickname' => '[a-zA-Z0-9]+'));
243 $m->connect('group/:nickname/blocked',
244 array('action' => 'blockedfromgroup'),
245 array('nickname' => '[a-zA-Z0-9]+'));
247 $m->connect('group/:nickname/makeadmin',
248 array('action' => 'makeadmin'),
249 array('nickname' => '[a-zA-Z0-9]+'));
251 $m->connect('group/:id/id',
252 array('action' => 'groupbyid'),
253 array('id' => '[0-9]+'));
255 $m->connect('group/:nickname',
256 array('action' => 'showgroup'),
257 array('nickname' => '[a-zA-Z0-9]+'));
259 $m->connect('group/', array('action' => 'groups'));
260 $m->connect('group', array('action' => 'groups'));
261 $m->connect('groups/', array('action' => 'groups'));
262 $m->connect('groups', array('action' => 'groups'));
264 // Twitter-compatible API
268 $m->connect('api/statuses/public_timeline.:format',
269 array('action' => 'ApiTimelinePublic',
270 'format' => '(xml|json|rss|atom)'));
272 $m->connect('api/statuses/friends_timeline.:format',
273 array('action' => 'ApiTimelineFriends',
274 'format' => '(xml|json|rss|atom)'));
276 $m->connect('api/statuses/friends_timeline/:id.:format',
277 array('action' => 'ApiTimelineFriends',
278 'id' => '[a-zA-Z0-9]+',
279 'format' => '(xml|json|rss|atom)'));
280 $m->connect('api/statuses/home_timeline.:format',
281 array('action' => 'ApiTimelineFriends',
282 'format' => '(xml|json|rss|atom)'));
284 $m->connect('api/statuses/home_timeline/:id.:format',
285 array('action' => 'ApiTimelineFriends',
286 'id' => '[a-zA-Z0-9]+',
287 'format' => '(xml|json|rss|atom)'));
289 $m->connect('api/statuses/user_timeline.:format',
290 array('action' => 'ApiTimelineUser',
291 'format' => '(xml|json|rss|atom)'));
293 $m->connect('api/statuses/user_timeline/:id.:format',
294 array('action' => 'ApiTimelineUser',
295 'id' => '[a-zA-Z0-9]+',
296 'format' => '(xml|json|rss|atom)'));
298 $m->connect('api/statuses/mentions.:format',
299 array('action' => 'ApiTimelineMentions',
300 'format' => '(xml|json|rss|atom)'));
302 $m->connect('api/statuses/mentions/:id.:format',
303 array('action' => 'ApiTimelineMentions',
304 'id' => '[a-zA-Z0-9]+',
305 'format' => '(xml|json|rss|atom)'));
307 $m->connect('api/statuses/replies.:format',
308 array('action' => 'ApiTimelineMentions',
309 'format' => '(xml|json|rss|atom)'));
311 $m->connect('api/statuses/replies/:id.:format',
312 array('action' => 'ApiTimelineMentions',
313 'id' => '[a-zA-Z0-9]+',
314 'format' => '(xml|json|rss|atom)'));
316 $m->connect('api/statuses/friends.:format',
317 array('action' => 'ApiUserFriends',
318 'format' => '(xml|json)'));
320 $m->connect('api/statuses/friends/:id.:format',
321 array('action' => 'ApiUserFriends',
322 'id' => '[a-zA-Z0-9]+',
323 'format' => '(xml|json)'));
325 $m->connect('api/statuses/followers.:format',
326 array('action' => 'ApiUserFollowers',
327 'format' => '(xml|json)'));
329 $m->connect('api/statuses/followers/:id.:format',
330 array('action' => 'ApiUserFollowers',
331 'id' => '[a-zA-Z0-9]+',
332 'format' => '(xml|json)'));
334 $m->connect('api/statuses/show.:format',
335 array('action' => 'ApiStatusesShow',
336 'format' => '(xml|json)'));
338 $m->connect('api/statuses/show/:id.:format',
339 array('action' => 'ApiStatusesShow',
341 'format' => '(xml|json)'));
343 $m->connect('api/statuses/update.:format',
344 array('action' => 'ApiStatusesUpdate',
345 'format' => '(xml|json)'));
347 $m->connect('api/statuses/destroy.:format',
348 array('action' => 'ApiStatusesDestroy',
349 'format' => '(xml|json)'));
351 $m->connect('api/statuses/destroy/:id.:format',
352 array('action' => 'ApiStatusesDestroy',
354 'format' => '(xml|json)'));
358 $m->connect('api/users/show/:id.:format',
359 array('action' => 'ApiUserShow',
360 'id' => '[a-zA-Z0-9]+',
361 'format' => '(xml|json)'));
363 $m->connect('api/users/:method',
364 array('action' => 'api',
365 'apiaction' => 'users'),
366 array('method' => 'show(\.(xml|json))?'));
370 $m->connect('api/direct_messages.:format',
371 array('action' => 'ApiDirectMessage',
372 'format' => '(xml|json|rss|atom)'));
374 $m->connect('api/direct_messages/sent.:format',
375 array('action' => 'ApiDirectMessage',
376 'format' => '(xml|json|rss|atom)',
379 $m->connect('api/direct_messages/new.:format',
380 array('action' => 'ApiDirectMessageNew',
381 'format' => '(xml|json)'));
385 $m->connect('api/friendships/show.:format',
386 array('action' => 'ApiFriendshipsShow',
387 'format' => '(xml|json)'));
389 $m->connect('api/friendships/exists.:format',
390 array('action' => 'ApiFriendshipsExists',
391 'format' => '(xml|json)'));
393 $m->connect('api/friendships/create.:format',
394 array('action' => 'ApiFriendshipsCreate',
395 'format' => '(xml|json)'));
397 $m->connect('api/friendships/destroy.:format',
398 array('action' => 'ApiFriendshipsDestroy',
399 'format' => '(xml|json)'));
401 $m->connect('api/friendships/create/:id.:format',
402 array('action' => 'ApiFriendshipsCreate',
403 'id' => '[a-zA-Z0-9]+',
404 'format' => '(xml|json)'));
406 $m->connect('api/friendships/destroy/:id.:format',
407 array('action' => 'ApiFriendshipsDestroy',
408 'id' => '[a-zA-Z0-9]+',
409 'format' => '(xml|json)'));
413 $m->connect('api/friends/ids/:id.:format',
414 array('action' => 'apiFriends',
415 'ids_only' => true));
417 $m->connect('api/followers/ids/:id.:format',
418 array('action' => 'apiFollowers',
419 'ids_only' => true));
421 $m->connect('api/friends/ids.:format',
422 array('action' => 'apiFriends',
423 'ids_only' => true));
425 $m->connect('api/followers/ids.:format',
426 array('action' => 'apiFollowers',
427 'ids_only' => true));
431 $m->connect('api/account/verify_credentials.:format',
432 array('action' => 'ApiAccountVerifyCredentials'));
434 // special case where verify_credentials is called w/out a format
436 $m->connect('api/account/verify_credentials',
437 array('action' => 'ApiAccountVerifyCredentials'));
439 $m->connect('api/account/rate_limit_status.:format',
440 array('action' => 'ApiAccountRateLimitStatus'));
444 $m->connect('api/favorites.:format',
445 array('action' => 'ApiTimelineFavorites',
446 'format' => '(xml|json|rss|atom)'));
448 $m->connect('api/favorites/:id.:format',
449 array('action' => 'ApiTimelineFavorites',
450 'id' => '[a-zA-Z0-9]+',
451 'format' => '(xmljson|rss|atom)'));
453 $m->connect('api/favorites/create/:id.:format',
454 array('action' => 'ApiFavoriteCreate',
455 'id' => '[a-zA-Z0-9]+',
456 'format' => '(xml|json)'));
458 $m->connect('api/favorites/destroy/:id.:format',
459 array('action' => 'ApiFavoriteDestroy',
460 'id' => '[a-zA-Z0-9]+',
461 'format' => '(xml|json)'));
465 $m->connect('api/notifications/:method/:argument',
466 array('action' => 'api',
467 'apiaction' => 'favorites'));
471 $m->connect('api/blocks/create/:id.:format',
472 array('action' => 'ApiBlockCreate',
473 'id' => '[a-zA-Z0-9]+',
474 'format' => '(xml|json)'));
476 $m->connect('api/blocks/destroy/:id.:format',
477 array('action' => 'ApiBlockDestroy',
478 'id' => '[a-zA-Z0-9]+',
479 'format' => '(xml|json)'));
482 $m->connect('api/help/test.:format',
483 array('action' => 'ApiHelpTest',
484 'format' => '(xml|json)'));
488 $m->connect('api/statusnet/version.:format',
489 array('action' => 'ApiStatusnetVersion',
490 'format' => '(xml|json)'));
492 $m->connect('api/statusnet/config.:format',
493 array('action' => 'ApiStatusnetConfig',
494 'format' => '(xml|json)'));
496 // For older methods, we provide "laconica" base action
498 $m->connect('api/laconica/version.:format',
499 array('action' => 'ApiStatusnetVersion',
500 'format' => '(xml|json)'));
502 $m->connect('api/laconica/config.:format',
503 array('action' => 'ApiStatusnetConfig',
504 'format' => '(xml|json)'));
506 // Groups and tags are newer than 0.8.1 so no backward-compatibility
510 //'list' has to be handled differently, as php will not allow a method to be named 'list'
512 $m->connect('api/statusnet/groups/timeline/:id.:format',
513 array('action' => 'ApiTimelineGroup',
514 'id' => '[a-zA-Z0-9]+',
515 'format' => '(xmljson|rss|atom)'));
517 $m->connect('api/statusnet/groups/show.:format',
518 array('action' => 'ApiGroupShow',
519 'format' => '(xml|json)'));
521 $m->connect('api/statusnet/groups/show/:id.:format',
522 array('action' => 'ApiGroupShow',
523 'id' => '[a-zA-Z0-9]+',
524 'format' => '(xml|json)'));
526 $m->connect('api/statusnet/groups/join.:format',
527 array('action' => 'ApiGroupJoin',
528 'id' => '[a-zA-Z0-9]+',
529 'format' => '(xml|json)'));
531 $m->connect('api/statusnet/groups/join/:id.:format',
532 array('action' => 'ApiGroupJoin',
533 'format' => '(xml|json)'));
535 $m->connect('api/statusnet/groups/leave.:format',
536 array('action' => 'ApiGroupLeave',
537 'id' => '[a-zA-Z0-9]+',
538 'format' => '(xml|json)'));
540 $m->connect('api/statusnet/groups/leave/:id.:format',
541 array('action' => 'ApiGroupLeave',
542 'format' => '(xml|json)'));
544 $m->connect('api/statusnet/groups/is_member.:format',
545 array('action' => 'ApiGroupIsMember',
546 'format' => '(xml|json)'));
548 $m->connect('api/statusnet/groups/list.:format',
549 array('action' => 'ApiGroupList',
550 'format' => '(xml|json|rss|atom)'));
552 $m->connect('api/statusnet/groups/list/:id.:format',
553 array('action' => 'ApiGroupList',
554 'id' => '[a-zA-Z0-9]+',
555 'format' => '(xml|json|rss|atom)'));
557 $m->connect('api/statusnet/groups/list_all.:format',
558 array('action' => 'ApiGroupListAll',
559 'format' => '(xml|json|rss|atom)'));
561 $m->connect('api/statusnet/groups/membership.:format',
562 array('action' => 'ApiGroupMembership',
563 'format' => '(xml|json)'));
565 $m->connect('api/statusnet/groups/membership/:id.:format',
566 array('action' => 'ApiGroupMembership',
567 'id' => '[a-zA-Z0-9]+',
568 'format' => '(xml|json)'));
570 $m->connect('api/statusnet/groups/create.:format',
571 array('action' => 'ApiGroupCreate',
572 'format' => '(xml|json)'));
574 $m->connect('api/statusnet/tags/timeline/:tag.:format',
575 array('action' => 'ApiTimelineTag',
576 'format' => '(xmljson|rss|atom)'));
579 $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
580 $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
581 $m->connect('api/trends.json', array('action' => 'twitapitrends'));
585 foreach (array('subscriptions', 'subscribers',
586 'nudge', 'all', 'foaf', 'xrds',
587 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
588 $m->connect(':nickname/'.$a,
589 array('action' => $a),
590 array('nickname' => '[a-zA-Z0-9]{1,64}'));
593 foreach (array('subscriptions', 'subscribers') as $a) {
594 $m->connect(':nickname/'.$a.'/:tag',
595 array('action' => $a),
596 array('tag' => '[a-zA-Z0-9]+',
597 'nickname' => '[a-zA-Z0-9]{1,64}'));
600 foreach (array('rss', 'groups') as $a) {
601 $m->connect(':nickname/'.$a,
602 array('action' => 'user'.$a),
603 array('nickname' => '[a-zA-Z0-9]{1,64}'));
606 foreach (array('all', 'replies', 'favorites') as $a) {
607 $m->connect(':nickname/'.$a.'/rss',
608 array('action' => $a.'rss'),
609 array('nickname' => '[a-zA-Z0-9]{1,64}'));
612 $m->connect(':nickname/favorites',
613 array('action' => 'showfavorites'),
614 array('nickname' => '[a-zA-Z0-9]{1,64}'));
616 $m->connect(':nickname/avatar/:size',
617 array('action' => 'avatarbynickname'),
618 array('size' => '(original|96|48|24)',
619 'nickname' => '[a-zA-Z0-9]{1,64}'));
621 $m->connect(':nickname/tag/:tag/rss',
622 array('action' => 'userrss'),
623 array('nickname' => '[a-zA-Z0-9]{1,64}'),
624 array('tag' => '[a-zA-Z0-9]+'));
626 $m->connect(':nickname/tag/:tag',
627 array('action' => 'showstream'),
628 array('nickname' => '[a-zA-Z0-9]{1,64}'),
629 array('tag' => '[a-zA-Z0-9]+'));
631 $m->connect(':nickname',
632 array('action' => 'showstream'),
633 array('nickname' => '[a-zA-Z0-9]{1,64}'));
635 Event::handle('RouterInitialized', array($m));
644 $match = $this->m->match($path);
645 } catch (Net_URL_Mapper_InvalidException $e) {
646 common_log(LOG_ERR, "Problem getting route for $path - " .
648 $cac = new ClientErrorAction("Page not found.", 404);
655 function build($action, $args=null, $params=null, $fragment=null)
657 $action_arg = array('action' => $action);
660 $args = array_merge($action_arg, $args);
665 $url = $this->m->generate($args, $params, $fragment);
667 // Due to a bug in the Net_URL_Mapper code, the returned URL may
668 // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
669 // repair that here rather than modifying the upstream code...
671 $qpos = strpos($url, '?');
672 if ($qpos !== false) {
673 $url = substr($url, 0, $qpos+1) .
674 str_replace('?', '&', substr($url, $qpos+1));