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'));
113 foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
114 $m->connect('main/'.$c.'/:code', array('action' => $c));
119 $m->connect('main/remote', array('action' => 'remotesubscribe'));
120 $m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '[A-Za-z0-9_-]+'));
122 foreach (Router::$bare as $action) {
123 $m->connect('index.php?action=' . $action, array('action' => $action));
128 foreach (array('profile', 'avatar', 'password', 'im',
129 'email', 'sms', 'userdesign', 'other') as $s) {
130 $m->connect('settings/'.$s, array('action' => $s.'settings'));
135 foreach (array('group', 'people', 'notice') as $s) {
136 $m->connect('search/'.$s, array('action' => $s.'search'));
137 $m->connect('search/'.$s.'?q=:q',
138 array('action' => $s.'search'),
142 // The second of these is needed to make the link work correctly
143 // when inserted into the page. The first is needed to match the
144 // route on the way in. Seems to be another Net_URL_Mapper bug to me.
145 $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
146 $m->connect('search/notice/rss?q=:q', array('action' => 'noticesearchrss'),
149 $m->connect('attachment/:attachment',
150 array('action' => 'attachment'),
151 array('attachment' => '[0-9]+'));
153 $m->connect('attachment/:attachment/ajax',
154 array('action' => 'attachment_ajax'),
155 array('attachment' => '[0-9]+'));
157 $m->connect('attachment/:attachment/thumbnail',
158 array('action' => 'attachment_thumbnail'),
159 array('attachment' => '[0-9]+'));
161 $m->connect('notice/new', array('action' => 'newnotice'));
162 $m->connect('notice/new?replyto=:replyto',
163 array('action' => 'newnotice'),
164 array('replyto' => '[A-Za-z0-9_-]+'));
165 $m->connect('notice/new?replyto=:replyto&inreplyto=:inreplyto',
166 array('action' => 'newnotice'),
167 array('replyto' => '[A-Za-z0-9_-]+'),
168 array('inreplyto' => '[0-9]+'));
170 $m->connect('notice/:notice/file',
171 array('action' => 'file'),
172 array('notice' => '[0-9]+'));
174 $m->connect('notice/:notice',
175 array('action' => 'shownotice'),
176 array('notice' => '[0-9]+'));
177 $m->connect('notice/delete', array('action' => 'deletenotice'));
178 $m->connect('notice/delete/:notice',
179 array('action' => 'deletenotice'),
180 array('notice' => '[0-9]+'));
182 $m->connect('bookmarklet/new', array('action' => 'bookmarklet'));
186 $m->connect('conversation/:id',
187 array('action' => 'conversation'),
188 array('id' => '[0-9]+'));
190 $m->connect('message/new', array('action' => 'newmessage'));
191 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
192 $m->connect('message/:message',
193 array('action' => 'showmessage'),
194 array('message' => '[0-9]+'));
196 $m->connect('user/:id',
197 array('action' => 'userbyid'),
198 array('id' => '[0-9]+'));
200 $m->connect('tags/', array('action' => 'publictagcloud'));
201 $m->connect('tag/', array('action' => 'publictagcloud'));
202 $m->connect('tags', array('action' => 'publictagcloud'));
203 $m->connect('tag', array('action' => 'publictagcloud'));
204 $m->connect('tag/:tag/rss',
205 array('action' => 'tagrss'),
206 array('tag' => '[a-zA-Z0-9]+'));
207 $m->connect('tag/:tag',
208 array('action' => 'tag'),
209 array('tag' => '[\pL\pN_\-\.]{1,64}'));
211 $m->connect('peopletag/:tag',
212 array('action' => 'peopletag'),
213 array('tag' => '[a-zA-Z0-9]+'));
215 $m->connect('featured/', array('action' => 'featured'));
216 $m->connect('featured', array('action' => 'featured'));
217 $m->connect('favorited/', array('action' => 'favorited'));
218 $m->connect('favorited', array('action' => 'favorited'));
222 $m->connect('group/new', array('action' => 'newgroup'));
224 foreach (array('edit', 'join', 'leave') as $v) {
225 $m->connect('group/:nickname/'.$v,
226 array('action' => $v.'group'),
227 array('nickname' => '[a-zA-Z0-9]+'));
230 foreach (array('members', 'logo', 'rss', 'designsettings') as $n) {
231 $m->connect('group/:nickname/'.$n,
232 array('action' => 'group'.$n),
233 array('nickname' => '[a-zA-Z0-9]+'));
236 $m->connect('group/:nickname/foaf',
237 array('action' => 'foafgroup'),
238 array('nickname' => '[a-zA-Z0-9]+'));
240 $m->connect('group/:nickname/blocked',
241 array('action' => 'blockedfromgroup'),
242 array('nickname' => '[a-zA-Z0-9]+'));
244 $m->connect('group/:nickname/makeadmin',
245 array('action' => 'makeadmin'),
246 array('nickname' => '[a-zA-Z0-9]+'));
248 $m->connect('group/:id/id',
249 array('action' => 'groupbyid'),
250 array('id' => '[0-9]+'));
252 $m->connect('group/:nickname',
253 array('action' => 'showgroup'),
254 array('nickname' => '[a-zA-Z0-9]+'));
256 $m->connect('group/', array('action' => 'groups'));
257 $m->connect('group', array('action' => 'groups'));
258 $m->connect('groups/', array('action' => 'groups'));
259 $m->connect('groups', array('action' => 'groups'));
261 // Twitter-compatible API
265 $m->connect('api/statuses/public_timeline.:format',
266 array('action' => 'ApiTimelinePublic',
267 'format' => '(xml|json|rss|atom)'));
269 $m->connect('api/statuses/friends_timeline.:format',
270 array('action' => 'ApiTimelineFriends',
271 'format' => '(xml|json|rss|atom)'));
273 $m->connect('api/statuses/friends_timeline/:id.:format',
274 array('action' => 'ApiTimelineFriends',
275 'id' => '[a-zA-Z0-9]+',
276 'format' => '(xml|json|rss|atom)'));
277 $m->connect('api/statuses/home_timeline.:format',
278 array('action' => 'ApiTimelineFriends',
279 'format' => '(xml|json|rss|atom)'));
281 $m->connect('api/statuses/home_timeline/:id.:format',
282 array('action' => 'ApiTimelineFriends',
283 'id' => '[a-zA-Z0-9]+',
284 'format' => '(xml|json|rss|atom)'));
286 $m->connect('api/statuses/user_timeline.:format',
287 array('action' => 'ApiTimelineUser',
288 'format' => '(xml|json|rss|atom)'));
290 $m->connect('api/statuses/user_timeline/:id.:format',
291 array('action' => 'ApiTimelineUser',
292 'id' => '[a-zA-Z0-9]+',
293 'format' => '(xml|json|rss|atom)'));
295 $m->connect('api/statuses/mentions.:format',
296 array('action' => 'ApiTimelineMentions',
297 'format' => '(xml|json|rss|atom)'));
299 $m->connect('api/statuses/mentions/:id.:format',
300 array('action' => 'ApiTimelineMentions',
301 'id' => '[a-zA-Z0-9]+',
302 'format' => '(xml|json|rss|atom)'));
304 $m->connect('api/statuses/replies.:format',
305 array('action' => 'ApiTimelineMentions',
306 'format' => '(xml|json|rss|atom)'));
308 $m->connect('api/statuses/replies/:id.:format',
309 array('action' => 'ApiTimelineMentions',
310 'id' => '[a-zA-Z0-9]+',
311 'format' => '(xml|json|rss|atom)'));
313 $m->connect('api/statuses/friends.:format',
314 array('action' => 'ApiUserFriends',
315 'format' => '(xml|json)'));
317 $m->connect('api/statuses/friends/:id.:format',
318 array('action' => 'ApiUserFriends',
319 'id' => '[a-zA-Z0-9]+',
320 'format' => '(xml|json)'));
322 $m->connect('api/statuses/followers.:format',
323 array('action' => 'ApiUserFollowers',
324 'format' => '(xml|json)'));
326 $m->connect('api/statuses/followers/:id.:format',
327 array('action' => 'ApiUserFollowers',
328 'id' => '[a-zA-Z0-9]+',
329 'format' => '(xml|json)'));
331 $m->connect('api/statuses/show.:format',
332 array('action' => 'ApiStatusesShow',
333 'format' => '(xml|json)'));
335 $m->connect('api/statuses/show/:id.:format',
336 array('action' => 'ApiStatusesShow',
338 'format' => '(xml|json)'));
340 $m->connect('api/statuses/update.:format',
341 array('action' => 'ApiStatusesUpdate',
342 'format' => '(xml|json)'));
344 $m->connect('api/statuses/destroy.:format',
345 array('action' => 'ApiStatusesDestroy',
346 'format' => '(xml|json)'));
348 $m->connect('api/statuses/destroy/:id.:format',
349 array('action' => 'ApiStatusesDestroy',
351 'format' => '(xml|json)'));
355 $m->connect('api/users/show/:id.:format',
356 array('action' => 'ApiUserShow',
357 'id' => '[a-zA-Z0-9]+',
358 'format' => '(xml|json)'));
360 $m->connect('api/users/:method',
361 array('action' => 'api',
362 'apiaction' => 'users'),
363 array('method' => 'show(\.(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' => '[a-zA-Z0-9]+',
401 'format' => '(xml|json)'));
403 $m->connect('api/friendships/destroy/:id.:format',
404 array('action' => 'ApiFriendshipsDestroy',
405 'id' => '[a-zA-Z0-9]+',
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 // special case where verify_credentials is called w/out a format
433 $m->connect('api/account/verify_credentials',
434 array('action' => 'ApiAccountVerifyCredentials'));
436 $m->connect('api/account/rate_limit_status.:format',
437 array('action' => 'ApiAccountRateLimitStatus'));
441 $m->connect('api/favorites.:format',
442 array('action' => 'ApiTimelineFavorites',
443 'format' => '(xml|json|rss|atom)'));
445 $m->connect('api/favorites/:id.:format',
446 array('action' => 'ApiTimelineFavorites',
447 'id' => '[a-zA-Z0-9]+',
448 'format' => '(xmljson|rss|atom)'));
450 $m->connect('api/favorites/create/:id.:format',
451 array('action' => 'ApiFavoriteCreate',
452 'id' => '[a-zA-Z0-9]+',
453 'format' => '(xml|json)'));
455 $m->connect('api/favorites/destroy/:id.:format',
456 array('action' => 'ApiFavoriteDestroy',
457 'id' => '[a-zA-Z0-9]+',
458 'format' => '(xml|json)'));
462 $m->connect('api/notifications/:method/:argument',
463 array('action' => 'api',
464 'apiaction' => 'favorites'));
468 $m->connect('api/blocks/create/:id.:format',
469 array('action' => 'ApiBlockCreate',
470 'id' => '[a-zA-Z0-9]+',
471 'format' => '(xml|json)'));
473 $m->connect('api/blocks/destroy/:id.:format',
474 array('action' => 'ApiBlockDestroy',
475 'id' => '[a-zA-Z0-9]+',
476 'format' => '(xml|json)'));
479 $m->connect('api/help/test.:format',
480 array('action' => 'ApiHelpTest',
481 'format' => '(xml|json)'));
485 $m->connect('api/statusnet/version.:format',
486 array('action' => 'ApiStatusnetVersion',
487 'format' => '(xml|json)'));
489 $m->connect('api/statusnet/config.:format',
490 array('action' => 'ApiStatusnetConfig',
491 'format' => '(xml|json)'));
493 // For older methods, we provide "laconica" base action
495 $m->connect('api/laconica/version.:format',
496 array('action' => 'ApiStatusnetVersion',
497 'format' => '(xml|json)'));
499 $m->connect('api/laconica/config.:format',
500 array('action' => 'ApiStatusnetConfig',
501 'format' => '(xml|json)'));
503 // Groups and tags are newer than 0.8.1 so no backward-compatibility
507 //'list' has to be handled differently, as php will not allow a method to be named 'list'
509 $m->connect('api/statusnet/groups/timeline/:id.:format',
510 array('action' => 'ApiTimelineGroup',
511 'id' => '[a-zA-Z0-9]+',
512 'format' => '(xmljson|rss|atom)'));
514 $m->connect('api/statusnet/groups/show.:format',
515 array('action' => 'ApiGroupShow',
516 'format' => '(xml|json)'));
518 $m->connect('api/statusnet/groups/show/:id.:format',
519 array('action' => 'ApiGroupShow',
520 'id' => '[a-zA-Z0-9]+',
521 'format' => '(xml|json)'));
523 $m->connect('api/statusnet/groups/join.:format',
524 array('action' => 'ApiGroupJoin',
525 'id' => '[a-zA-Z0-9]+',
526 'format' => '(xml|json)'));
528 $m->connect('api/statusnet/groups/join/:id.:format',
529 array('action' => 'ApiGroupJoin',
530 'format' => '(xml|json)'));
532 $m->connect('api/statusnet/groups/leave.:format',
533 array('action' => 'ApiGroupLeave',
534 'id' => '[a-zA-Z0-9]+',
535 'format' => '(xml|json)'));
537 $m->connect('api/statusnet/groups/leave/:id.:format',
538 array('action' => 'ApiGroupLeave',
539 'format' => '(xml|json)'));
541 $m->connect('api/statusnet/groups/is_member.:format',
542 array('action' => 'ApiGroupIsMember',
543 'format' => '(xml|json)'));
545 $m->connect('api/statusnet/groups/list.:format',
546 array('action' => 'ApiGroupList',
547 'format' => '(xml|json|rss|atom)'));
549 $m->connect('api/statusnet/groups/list/:id.:format',
550 array('action' => 'ApiGroupList',
551 'id' => '[a-zA-Z0-9]+',
552 'format' => '(xml|json|rss|atom)'));
554 $m->connect('api/statusnet/groups/list_all.:format',
555 array('action' => 'ApiGroupListAll',
556 'format' => '(xml|json|rss|atom)'));
558 $m->connect('api/statusnet/groups/membership.:format',
559 array('action' => 'ApiGroupMembership',
560 'format' => '(xml|json)'));
562 $m->connect('api/statusnet/groups/membership/:id.:format',
563 array('action' => 'ApiGroupMembership',
564 'id' => '[a-zA-Z0-9]+',
565 'format' => '(xml|json)'));
567 $m->connect('api/statusnet/groups/create.:format',
568 array('action' => 'ApiGroupCreate',
569 'format' => '(xml|json)'));
571 $m->connect('api/statusnet/tags/timeline/:tag.:format',
572 array('action' => 'ApiTimelineTag',
573 'format' => '(xmljson|rss|atom)'));
576 $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
577 $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
578 $m->connect('api/trends.json', array('action' => 'twitapitrends'));
582 foreach (array('subscriptions', 'subscribers',
583 'nudge', 'all', 'foaf', 'xrds',
584 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
585 $m->connect(':nickname/'.$a,
586 array('action' => $a),
587 array('nickname' => '[a-zA-Z0-9]{1,64}'));
590 foreach (array('subscriptions', 'subscribers') as $a) {
591 $m->connect(':nickname/'.$a.'/:tag',
592 array('action' => $a),
593 array('tag' => '[a-zA-Z0-9]+',
594 'nickname' => '[a-zA-Z0-9]{1,64}'));
597 foreach (array('rss', 'groups') as $a) {
598 $m->connect(':nickname/'.$a,
599 array('action' => 'user'.$a),
600 array('nickname' => '[a-zA-Z0-9]{1,64}'));
603 foreach (array('all', 'replies', 'favorites') as $a) {
604 $m->connect(':nickname/'.$a.'/rss',
605 array('action' => $a.'rss'),
606 array('nickname' => '[a-zA-Z0-9]{1,64}'));
609 $m->connect(':nickname/favorites',
610 array('action' => 'showfavorites'),
611 array('nickname' => '[a-zA-Z0-9]{1,64}'));
613 $m->connect(':nickname/avatar/:size',
614 array('action' => 'avatarbynickname'),
615 array('size' => '(original|96|48|24)',
616 'nickname' => '[a-zA-Z0-9]{1,64}'));
618 $m->connect(':nickname/tag/:tag/rss',
619 array('action' => 'userrss'),
620 array('nickname' => '[a-zA-Z0-9]{1,64}'),
621 array('tag' => '[a-zA-Z0-9]+'));
623 $m->connect(':nickname/tag/:tag',
624 array('action' => 'showstream'),
625 array('nickname' => '[a-zA-Z0-9]{1,64}'),
626 array('tag' => '[a-zA-Z0-9]+'));
628 $m->connect(':nickname',
629 array('action' => 'showstream'),
630 array('nickname' => '[a-zA-Z0-9]{1,64}'));
632 Event::handle('RouterInitialized', array($m));
641 $match = $this->m->match($path);
642 } catch (Net_URL_Mapper_InvalidException $e) {
643 common_log(LOG_ERR, "Problem getting route for $path - " .
645 $cac = new ClientErrorAction("Page not found.", 404);
652 function build($action, $args=null, $params=null, $fragment=null)
654 $action_arg = array('action' => $action);
657 $args = array_merge($action_arg, $args);
662 $url = $this->m->generate($args, $params, $fragment);
664 // Due to a bug in the Net_URL_Mapper code, the returned URL may
665 // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
666 // repair that here rather than modifying the upstream code...
668 $qpos = strpos($url, '?');
669 if ($qpos !== false) {
670 $url = substr($url, 0, $qpos+1) .
671 str_replace('?', '&', substr($url, $qpos+1));