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)'));
365 $m->connect('api/direct_messages.:format',
366 array('action' => 'ApiDirectMessage',
367 'format' => '(xml|json|rss|atom)'));
369 $m->connect('api/direct_messages/sent.:format',
370 array('action' => 'ApiDirectMessage',
371 'format' => '(xml|json|rss|atom)',
374 $m->connect('api/direct_messages/new.:format',
375 array('action' => 'ApiDirectMessageNew',
376 'format' => '(xml|json)'));
380 $m->connect('api/friendships/show.:format',
381 array('action' => 'ApiFriendshipsShow',
382 'format' => '(xml|json)'));
384 $m->connect('api/friendships/exists.:format',
385 array('action' => 'ApiFriendshipsExists',
386 'format' => '(xml|json)'));
388 $m->connect('api/friendships/create.:format',
389 array('action' => 'ApiFriendshipsCreate',
390 'format' => '(xml|json)'));
392 $m->connect('api/friendships/destroy.:format',
393 array('action' => 'ApiFriendshipsDestroy',
394 'format' => '(xml|json)'));
396 $m->connect('api/friendships/create/:id.:format',
397 array('action' => 'ApiFriendshipsCreate',
398 'id' => '[a-zA-Z0-9]+',
399 'format' => '(xml|json)'));
401 $m->connect('api/friendships/destroy/:id.:format',
402 array('action' => 'ApiFriendshipsDestroy',
403 'id' => '[a-zA-Z0-9]+',
404 'format' => '(xml|json)'));
408 $m->connect('api/friends/ids/:id.:format',
409 array('action' => 'apiFriends',
410 'ids_only' => true));
412 $m->connect('api/followers/ids/:id.:format',
413 array('action' => 'apiFollowers',
414 'ids_only' => true));
416 $m->connect('api/friends/ids.:format',
417 array('action' => 'apiFriends',
418 'ids_only' => true));
420 $m->connect('api/followers/ids.:format',
421 array('action' => 'apiFollowers',
422 'ids_only' => true));
426 $m->connect('api/account/verify_credentials.:format',
427 array('action' => 'ApiAccountVerifyCredentials'));
429 // special case where verify_credentials is called w/out a format
431 $m->connect('api/account/verify_credentials',
432 array('action' => 'ApiAccountVerifyCredentials'));
434 $m->connect('api/account/rate_limit_status.:format',
435 array('action' => 'ApiAccountRateLimitStatus'));
439 $m->connect('api/favorites.:format',
440 array('action' => 'ApiTimelineFavorites',
441 'format' => '(xml|json|rss|atom)'));
443 $m->connect('api/favorites/:id.:format',
444 array('action' => 'ApiTimelineFavorites',
445 'id' => '[a-zA-Z0-9]+',
446 'format' => '(xmljson|rss|atom)'));
448 $m->connect('api/favorites/create/:id.:format',
449 array('action' => 'ApiFavoriteCreate',
450 'id' => '[a-zA-Z0-9]+',
451 'format' => '(xml|json)'));
453 $m->connect('api/favorites/destroy/:id.:format',
454 array('action' => 'ApiFavoriteDestroy',
455 'id' => '[a-zA-Z0-9]+',
456 'format' => '(xml|json)'));
459 $m->connect('api/blocks/create/:id.:format',
460 array('action' => 'ApiBlockCreate',
461 'id' => '[a-zA-Z0-9]+',
462 'format' => '(xml|json)'));
464 $m->connect('api/blocks/destroy/:id.:format',
465 array('action' => 'ApiBlockDestroy',
466 'id' => '[a-zA-Z0-9]+',
467 'format' => '(xml|json)'));
470 $m->connect('api/help/test.:format',
471 array('action' => 'ApiHelpTest',
472 'format' => '(xml|json)'));
476 $m->connect('api/statusnet/version.:format',
477 array('action' => 'ApiStatusnetVersion',
478 'format' => '(xml|json)'));
480 $m->connect('api/statusnet/config.:format',
481 array('action' => 'ApiStatusnetConfig',
482 'format' => '(xml|json)'));
484 // For older methods, we provide "laconica" base action
486 $m->connect('api/laconica/version.:format',
487 array('action' => 'ApiStatusnetVersion',
488 'format' => '(xml|json)'));
490 $m->connect('api/laconica/config.:format',
491 array('action' => 'ApiStatusnetConfig',
492 'format' => '(xml|json)'));
494 // Groups and tags are newer than 0.8.1 so no backward-compatibility
498 //'list' has to be handled differently, as php will not allow a method to be named 'list'
500 $m->connect('api/statusnet/groups/timeline/:id.:format',
501 array('action' => 'ApiTimelineGroup',
502 'id' => '[a-zA-Z0-9]+',
503 'format' => '(xmljson|rss|atom)'));
505 $m->connect('api/statusnet/groups/show.:format',
506 array('action' => 'ApiGroupShow',
507 'format' => '(xml|json)'));
509 $m->connect('api/statusnet/groups/show/:id.:format',
510 array('action' => 'ApiGroupShow',
511 'id' => '[a-zA-Z0-9]+',
512 'format' => '(xml|json)'));
514 $m->connect('api/statusnet/groups/join.:format',
515 array('action' => 'ApiGroupJoin',
516 'id' => '[a-zA-Z0-9]+',
517 'format' => '(xml|json)'));
519 $m->connect('api/statusnet/groups/join/:id.:format',
520 array('action' => 'ApiGroupJoin',
521 'format' => '(xml|json)'));
523 $m->connect('api/statusnet/groups/leave.:format',
524 array('action' => 'ApiGroupLeave',
525 'id' => '[a-zA-Z0-9]+',
526 'format' => '(xml|json)'));
528 $m->connect('api/statusnet/groups/leave/:id.:format',
529 array('action' => 'ApiGroupLeave',
530 'format' => '(xml|json)'));
532 $m->connect('api/statusnet/groups/is_member.:format',
533 array('action' => 'ApiGroupIsMember',
534 'format' => '(xml|json)'));
536 $m->connect('api/statusnet/groups/list.:format',
537 array('action' => 'ApiGroupList',
538 'format' => '(xml|json|rss|atom)'));
540 $m->connect('api/statusnet/groups/list/:id.:format',
541 array('action' => 'ApiGroupList',
542 'id' => '[a-zA-Z0-9]+',
543 'format' => '(xml|json|rss|atom)'));
545 $m->connect('api/statusnet/groups/list_all.:format',
546 array('action' => 'ApiGroupListAll',
547 'format' => '(xml|json|rss|atom)'));
549 $m->connect('api/statusnet/groups/membership.:format',
550 array('action' => 'ApiGroupMembership',
551 'format' => '(xml|json)'));
553 $m->connect('api/statusnet/groups/membership/:id.:format',
554 array('action' => 'ApiGroupMembership',
555 'id' => '[a-zA-Z0-9]+',
556 'format' => '(xml|json)'));
558 $m->connect('api/statusnet/groups/create.:format',
559 array('action' => 'ApiGroupCreate',
560 'format' => '(xml|json)'));
562 $m->connect('api/statusnet/tags/timeline/:tag.:format',
563 array('action' => 'ApiTimelineTag',
564 'format' => '(xmljson|rss|atom)'));
567 $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
568 $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
569 $m->connect('api/trends.json', array('action' => 'twitapitrends'));
571 $m->connect('getfile/:filename',
572 array('action' => 'getfile'),
573 array('filename' => '[A-Za-z0-9._-]+'));
577 foreach (array('subscriptions', 'subscribers',
578 'nudge', 'all', 'foaf', 'xrds',
579 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
580 $m->connect(':nickname/'.$a,
581 array('action' => $a),
582 array('nickname' => '[a-zA-Z0-9]{1,64}'));
585 foreach (array('subscriptions', 'subscribers') as $a) {
586 $m->connect(':nickname/'.$a.'/:tag',
587 array('action' => $a),
588 array('tag' => '[a-zA-Z0-9]+',
589 'nickname' => '[a-zA-Z0-9]{1,64}'));
592 foreach (array('rss', 'groups') as $a) {
593 $m->connect(':nickname/'.$a,
594 array('action' => 'user'.$a),
595 array('nickname' => '[a-zA-Z0-9]{1,64}'));
598 foreach (array('all', 'replies', 'favorites') as $a) {
599 $m->connect(':nickname/'.$a.'/rss',
600 array('action' => $a.'rss'),
601 array('nickname' => '[a-zA-Z0-9]{1,64}'));
604 $m->connect(':nickname/favorites',
605 array('action' => 'showfavorites'),
606 array('nickname' => '[a-zA-Z0-9]{1,64}'));
608 $m->connect(':nickname/avatar/:size',
609 array('action' => 'avatarbynickname'),
610 array('size' => '(original|96|48|24)',
611 'nickname' => '[a-zA-Z0-9]{1,64}'));
613 $m->connect(':nickname/tag/:tag/rss',
614 array('action' => 'userrss'),
615 array('nickname' => '[a-zA-Z0-9]{1,64}'),
616 array('tag' => '[a-zA-Z0-9]+'));
618 $m->connect(':nickname/tag/:tag',
619 array('action' => 'showstream'),
620 array('nickname' => '[a-zA-Z0-9]{1,64}'),
621 array('tag' => '[a-zA-Z0-9]+'));
623 $m->connect(':nickname',
624 array('action' => 'showstream'),
625 array('nickname' => '[a-zA-Z0-9]{1,64}'));
627 Event::handle('RouterInitialized', array($m));
636 $match = $this->m->match($path);
637 } catch (Net_URL_Mapper_InvalidException $e) {
638 common_log(LOG_ERR, "Problem getting route for $path - " .
640 $cac = new ClientErrorAction("Page not found.", 404);
647 function build($action, $args=null, $params=null, $fragment=null)
649 $action_arg = array('action' => $action);
652 $args = array_merge($action_arg, $args);
657 $url = $this->m->generate($args, $params, $fragment);
659 // Due to a bug in the Net_URL_Mapper code, the returned URL may
660 // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
661 // repair that here rather than modifying the upstream code...
663 $qpos = strpos($url, '?');
664 if ($qpos !== false) {
665 $url = substr($url, 0, $qpos+1) .
666 str_replace('?', '&', substr($url, $qpos+1));