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();
76 $m->connect('', array('action' => 'public'));
77 $m->connect('rss', array('action' => 'publicrss'));
78 $m->connect('featuredrss', array('action' => 'featuredrss'));
79 $m->connect('favoritedrss', array('action' => 'favoritedrss'));
80 $m->connect('opensearch/people', array('action' => 'opensearch',
82 $m->connect('opensearch/notice', array('action' => 'opensearch',
87 $m->connect('doc/:title', array('action' => 'doc'));
89 // main stuff is repetitive
91 $main = array('login', 'logout', 'register', 'subscribe',
92 'unsubscribe', 'confirmaddress', 'recoverpassword',
93 'invite', 'favor', 'disfavor', 'sup',
94 'block', 'unblock', 'subedit',
95 'groupblock', 'groupunblock');
97 foreach ($main as $a) {
98 $m->connect('main/'.$a, array('action' => $a));
101 $m->connect('main/sup/:seconds', array('action' => 'sup'),
102 array('seconds' => '[0-9]+'));
104 $m->connect('main/tagother/:id', array('action' => 'tagother'));
106 $m->connect('main/oembed',
107 array('action' => 'oembed'));
111 foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
112 $m->connect('main/'.$c.'/:code', array('action' => $c));
117 $m->connect('main/remote', array('action' => 'remotesubscribe'));
118 $m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '[A-Za-z0-9_-]+'));
120 foreach (Router::$bare as $action) {
121 $m->connect('index.php?action=' . $action, array('action' => $action));
126 foreach (array('profile', 'avatar', 'password', 'im',
127 'email', 'sms', 'userdesign', 'other') as $s) {
128 $m->connect('settings/'.$s, array('action' => $s.'settings'));
133 foreach (array('group', 'people', 'notice') as $s) {
134 $m->connect('search/'.$s, array('action' => $s.'search'));
135 $m->connect('search/'.$s.'?q=:q',
136 array('action' => $s.'search'),
140 // The second of these is needed to make the link work correctly
141 // when inserted into the page. The first is needed to match the
142 // route on the way in. Seems to be another Net_URL_Mapper bug to me.
143 $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
144 $m->connect('search/notice/rss?q=:q', array('action' => 'noticesearchrss'),
147 $m->connect('attachment/:attachment',
148 array('action' => 'attachment'),
149 array('attachment' => '[0-9]+'));
151 $m->connect('attachment/:attachment/ajax',
152 array('action' => 'attachment_ajax'),
153 array('attachment' => '[0-9]+'));
155 $m->connect('attachment/:attachment/thumbnail',
156 array('action' => 'attachment_thumbnail'),
157 array('attachment' => '[0-9]+'));
159 $m->connect('notice/new', array('action' => 'newnotice'));
160 $m->connect('notice/new?replyto=:replyto',
161 array('action' => 'newnotice'),
162 array('replyto' => '[A-Za-z0-9_-]+'));
163 $m->connect('notice/new?replyto=:replyto&inreplyto=:inreplyto',
164 array('action' => 'newnotice'),
165 array('replyto' => '[A-Za-z0-9_-]+'),
166 array('inreplyto' => '[0-9]+'));
168 $m->connect('notice/:notice/file',
169 array('action' => 'file'),
170 array('notice' => '[0-9]+'));
172 $m->connect('notice/:notice',
173 array('action' => 'shownotice'),
174 array('notice' => '[0-9]+'));
175 $m->connect('notice/delete', array('action' => 'deletenotice'));
176 $m->connect('notice/delete/:notice',
177 array('action' => 'deletenotice'),
178 array('notice' => '[0-9]+'));
182 $m->connect('conversation/:id',
183 array('action' => 'conversation'),
184 array('id' => '[0-9]+'));
186 $m->connect('message/new', array('action' => 'newmessage'));
187 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
188 $m->connect('message/:message',
189 array('action' => 'showmessage'),
190 array('message' => '[0-9]+'));
192 $m->connect('user/:id',
193 array('action' => 'userbyid'),
194 array('id' => '[0-9]+'));
196 $m->connect('tags/', array('action' => 'publictagcloud'));
197 $m->connect('tag/', array('action' => 'publictagcloud'));
198 $m->connect('tags', array('action' => 'publictagcloud'));
199 $m->connect('tag', array('action' => 'publictagcloud'));
200 $m->connect('tag/:tag/rss',
201 array('action' => 'tagrss'),
202 array('tag' => '[a-zA-Z0-9]+'));
203 $m->connect('tag/:tag',
204 array('action' => 'tag'),
205 array('tag' => '[\pL\pN_\-\.]{1,64}'));
207 $m->connect('peopletag/:tag',
208 array('action' => 'peopletag'),
209 array('tag' => '[a-zA-Z0-9]+'));
211 $m->connect('featured/', array('action' => 'featured'));
212 $m->connect('featured', array('action' => 'featured'));
213 $m->connect('favorited/', array('action' => 'favorited'));
214 $m->connect('favorited', array('action' => 'favorited'));
218 $m->connect('group/new', array('action' => 'newgroup'));
220 foreach (array('edit', 'join', 'leave') as $v) {
221 $m->connect('group/:nickname/'.$v,
222 array('action' => $v.'group'),
223 array('nickname' => '[a-zA-Z0-9]+'));
226 foreach (array('members', 'logo', 'rss', 'designsettings') as $n) {
227 $m->connect('group/:nickname/'.$n,
228 array('action' => 'group'.$n),
229 array('nickname' => '[a-zA-Z0-9]+'));
232 $m->connect('group/:nickname/foaf',
233 array('action' => 'foafgroup'),
234 array('nickname' => '[a-zA-Z0-9]+'));
236 $m->connect('group/:nickname/blocked',
237 array('action' => 'blockedfromgroup'),
238 array('nickname' => '[a-zA-Z0-9]+'));
240 $m->connect('group/:nickname/makeadmin',
241 array('action' => 'makeadmin'),
242 array('nickname' => '[a-zA-Z0-9]+'));
244 $m->connect('group/:id/id',
245 array('action' => 'groupbyid'),
246 array('id' => '[0-9]+'));
248 $m->connect('group/:nickname',
249 array('action' => 'showgroup'),
250 array('nickname' => '[a-zA-Z0-9]+'));
252 $m->connect('group/', array('action' => 'groups'));
253 $m->connect('group', array('action' => 'groups'));
254 $m->connect('groups/', array('action' => 'groups'));
255 $m->connect('groups', array('action' => 'groups'));
257 // Twitter-compatible API
261 $m->connect('api/statuses/public_timeline.:format',
262 array('action' => 'ApiTimelinePublic',
263 'format' => '(xml|json|rss|atom)'));
265 $m->connect('api/statuses/friends_timeline.:format',
266 array('action' => 'ApiTimelineFriends',
267 'format' => '(xml|json|rss|atom)'));
269 $m->connect('api/statuses/friends_timeline/:id.:format',
270 array('action' => 'ApiTimelineFriends',
271 'id' => '[a-zA-Z0-9]+',
272 'format' => '(xml|json|rss|atom)'));
273 $m->connect('api/statuses/home_timeline.:format',
274 array('action' => 'ApiTimelineFriends',
275 'format' => '(xml|json|rss|atom)'));
277 $m->connect('api/statuses/home_timeline/:id.:format',
278 array('action' => 'ApiTimelineFriends',
279 'id' => '[a-zA-Z0-9]+',
280 'format' => '(xml|json|rss|atom)'));
282 $m->connect('api/statuses/user_timeline.:format',
283 array('action' => 'ApiTimelineUser',
284 'format' => '(xml|json|rss|atom)'));
286 $m->connect('api/statuses/user_timeline/:id.:format',
287 array('action' => 'ApiTimelineUser',
288 'id' => '[a-zA-Z0-9]+',
289 'format' => '(xml|json|rss|atom)'));
291 $m->connect('api/statuses/mentions.:format',
292 array('action' => 'ApiTimelineMentions',
293 'format' => '(xml|json|rss|atom)'));
295 $m->connect('api/statuses/mentions/:id.:format',
296 array('action' => 'ApiTimelineMentions',
297 'id' => '[a-zA-Z0-9]+',
298 'format' => '(xml|json|rss|atom)'));
300 $m->connect('api/statuses/replies.:format',
301 array('action' => 'ApiTimelineMentions',
302 'format' => '(xml|json|rss|atom)'));
304 $m->connect('api/statuses/replies/:id.:format',
305 array('action' => 'ApiTimelineMentions',
306 'id' => '[a-zA-Z0-9]+',
307 'format' => '(xml|json|rss|atom)'));
309 $m->connect('api/statuses/friends.:format',
310 array('action' => 'ApiUserFriends',
311 'format' => '(xml|json)'));
313 $m->connect('api/statuses/friends/:id.:format',
314 array('action' => 'ApiUserFriends',
315 'id' => '[a-zA-Z0-9]+',
316 'format' => '(xml|json)'));
318 $m->connect('api/statuses/followers.:format',
319 array('action' => 'ApiUserFollowers',
320 'format' => '(xml|json)'));
322 $m->connect('api/statuses/followers/:id.:format',
323 array('action' => 'ApiUserFollowers',
324 'id' => '[a-zA-Z0-9]+',
325 'format' => '(xml|json)'));
327 $m->connect('api/statuses/show.:format',
328 array('action' => 'ApiStatusesShow',
329 'format' => '(xml|json)'));
331 $m->connect('api/statuses/show/:id.:format',
332 array('action' => 'ApiStatusesShow',
334 'format' => '(xml|json)'));
336 $m->connect('api/statuses/update.:format',
337 array('action' => 'ApiStatusesUpdate',
338 'format' => '(xml|json)'));
340 $m->connect('api/statuses/destroy.:format',
341 array('action' => 'ApiStatusesDestroy',
342 'format' => '(xml|json)'));
344 $m->connect('api/statuses/destroy/:id.:format',
345 array('action' => 'ApiStatusesDestroy',
347 'format' => '(xml|json)'));
351 $m->connect('api/users/show/:id.:format',
352 array('action' => 'ApiUserShow',
353 'id' => '[a-zA-Z0-9]+',
354 'format' => '(xml|json)'));
356 $m->connect('api/users/:method',
357 array('action' => 'api',
358 'apiaction' => 'users'),
359 array('method' => 'show(\.(xml|json))?'));
364 $m->connect('api/direct_messages.:format',
365 array('action' => 'ApiDirectMessage',
366 'format' => '(xml|json|rss|atom)'));
368 $m->connect('api/direct_messages/sent.:format',
369 array('action' => 'ApiDirectMessage',
370 'format' => '(xml|json|rss|atom)',
373 $m->connect('api/direct_messages/new.:format',
374 array('action' => 'ApiDirectMessageNew',
375 'format' => '(xml|json)'));
379 $m->connect('api/friendships/show.:format',
380 array('action' => 'ApiFriendshipsShow',
381 'format' => '(xml|json)'));
383 $m->connect('api/friendships/exists.:format',
384 array('action' => 'ApiFriendshipsExists',
385 'format' => '(xml|json)'));
387 $m->connect('api/friendships/create.:format',
388 array('action' => 'ApiFriendshipsCreate',
389 'format' => '(xml|json)'));
391 $m->connect('api/friendships/destroy.:format',
392 array('action' => 'ApiFriendshipsDestroy',
393 'format' => '(xml|json)'));
395 $m->connect('api/friendships/create/:id.:format',
396 array('action' => 'ApiFriendshipsCreate',
397 'id' => '[a-zA-Z0-9]+',
398 'format' => '(xml|json)'));
400 $m->connect('api/friendships/destroy/:id.:format',
401 array('action' => 'ApiFriendshipsDestroy',
402 'id' => '[a-zA-Z0-9]+',
403 'format' => '(xml|json)'));
407 $m->connect('api/friends/ids/:id.:format',
408 array('action' => 'apiFriends',
409 'ids_only' => true));
411 $m->connect('api/followers/ids/:id.:format',
412 array('action' => 'apiFollowers',
413 'ids_only' => true));
415 $m->connect('api/friends/ids.:format',
416 array('action' => 'apiFriends',
417 'ids_only' => true));
419 $m->connect('api/followers/ids.:format',
420 array('action' => 'apiFollowers',
421 'ids_only' => true));
425 $m->connect('api/account/verify_credentials.:format',
426 array('action' => 'ApiAccountVerifyCredentials'));
428 // special case where verify_credentials is called w/out a format
430 $m->connect('api/account/verify_credentials',
431 array('action' => 'ApiAccountVerifyCredentials'));
433 $m->connect('api/account/rate_limit_status.:format',
434 array('action' => 'ApiAccountRateLimitStatus'));
438 $m->connect('api/favorites.:format',
439 array('action' => 'ApiTimelineFavorites',
440 'format' => '(xml|json|rss|atom)'));
442 $m->connect('api/favorites/:id.:format',
443 array('action' => 'ApiTimelineFavorites',
444 'id' => '[a-zA-Z0-9]+',
445 'format' => '(xmljson|rss|atom)'));
447 $m->connect('api/favorites/create/:id.:format',
448 array('action' => 'ApiFavoriteCreate',
449 'id' => '[a-zA-Z0-9]+',
450 'format' => '(xml|json)'));
452 $m->connect('api/favorites/destroy/:id.:format',
453 array('action' => 'ApiFavoriteDestroy',
454 'id' => '[a-zA-Z0-9]+',
455 'format' => '(xml|json)'));
459 $m->connect('api/notifications/:method/:argument',
460 array('action' => 'api',
461 'apiaction' => 'favorites'));
465 $m->connect('api/blocks/create/:id.:format',
466 array('action' => 'ApiBlockCreate',
467 'id' => '[a-zA-Z0-9]+',
468 'format' => '(xml|json)'));
470 $m->connect('api/blocks/destroy/:id.:format',
471 array('action' => 'ApiBlockDestroy',
472 'id' => '[a-zA-Z0-9]+',
473 'format' => '(xml|json)'));
476 $m->connect('api/help/test.:format',
477 array('action' => 'ApiHelpTest',
478 'format' => '(xml|json)'));
482 $m->connect('api/statusnet/version.:format',
483 array('action' => 'ApiStatusnetVersion',
484 'format' => '(xml|json)'));
486 $m->connect('api/statusnet/config.:format',
487 array('action' => 'ApiStatusnetConfig',
488 'format' => '(xml|json)'));
490 // For older methods, we provide "laconica" base action
492 $m->connect('api/laconica/version.:format',
493 array('action' => 'ApiStatusnetVersion',
494 'format' => '(xml|json)'));
496 $m->connect('api/laconica/config.:format',
497 array('action' => 'ApiStatusnetConfig',
498 'format' => '(xml|json)'));
500 // Groups and tags are newer than 0.8.1 so no backward-compatibility
504 //'list' has to be handled differently, as php will not allow a method to be named 'list'
506 $m->connect('api/statusnet/groups/timeline/:id.:format',
507 array('action' => 'ApiTimelineGroup',
508 'id' => '[a-zA-Z0-9]+',
509 'format' => '(xmljson|rss|atom)'));
511 $m->connect('api/statusnet/groups/show.:format',
512 array('action' => 'ApiGroupShow',
513 'format' => '(xml|json)'));
515 $m->connect('api/statusnet/groups/show/:id.:format',
516 array('action' => 'ApiGroupShow',
517 'id' => '[a-zA-Z0-9]+',
518 'format' => '(xml|json)'));
520 $m->connect('api/statusnet/groups/join.:format',
521 array('action' => 'ApiGroupJoin',
522 'id' => '[a-zA-Z0-9]+',
523 'format' => '(xml|json)'));
525 $m->connect('api/statusnet/groups/join/:id.:format',
526 array('action' => 'ApiGroupJoin',
527 'format' => '(xml|json)'));
529 $m->connect('api/statusnet/groups/leave.:format',
530 array('action' => 'ApiGroupLeave',
531 'id' => '[a-zA-Z0-9]+',
532 'format' => '(xml|json)'));
534 $m->connect('api/statusnet/groups/leave/:id.:format',
535 array('action' => 'ApiGroupLeave',
536 'format' => '(xml|json)'));
538 $m->connect('api/statusnet/groups/is_member.:format',
539 array('action' => 'ApiGroupIsMember',
540 'format' => '(xml|json)'));
542 $m->connect('api/statusnet/groups/list.:format',
543 array('action' => 'ApiGroupList',
544 'format' => '(xml|json|rss|atom)'));
546 $m->connect('api/statusnet/groups/list/:id.:format',
547 array('action' => 'ApiGroupList',
548 'id' => '[a-zA-Z0-9]+',
549 'format' => '(xml|json|rss|atom)'));
551 $m->connect('api/statusnet/groups/list_all.:format',
552 array('action' => 'ApiGroupListAll',
553 'format' => '(xml|json|rss|atom)'));
555 $m->connect('api/statusnet/groups/membership.:format',
556 array('action' => 'ApiGroupMembership',
557 'format' => '(xml|json)'));
559 $m->connect('api/statusnet/groups/membership/:id.:format',
560 array('action' => 'ApiGroupMembership',
561 'id' => '[a-zA-Z0-9]+',
562 'format' => '(xml|json)'));
564 $m->connect('api/statusnet/groups/create.:format',
565 array('action' => 'ApiGroupCreate',
566 'format' => '(xml|json)'));
568 $m->connect('api/statusnet/tags/timeline/:tag.:format',
569 array('action' => 'ApiTimelineTag',
570 'format' => '(xmljson|rss|atom)'));
573 $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
574 $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
575 $m->connect('api/trends.json', array('action' => 'twitapitrends'));
579 foreach (array('subscriptions', 'subscribers',
580 'nudge', 'all', 'foaf', 'xrds',
581 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
582 $m->connect(':nickname/'.$a,
583 array('action' => $a),
584 array('nickname' => '[a-zA-Z0-9]{1,64}'));
587 foreach (array('subscriptions', 'subscribers') as $a) {
588 $m->connect(':nickname/'.$a.'/:tag',
589 array('action' => $a),
590 array('tag' => '[a-zA-Z0-9]+',
591 'nickname' => '[a-zA-Z0-9]{1,64}'));
594 foreach (array('rss', 'groups') as $a) {
595 $m->connect(':nickname/'.$a,
596 array('action' => 'user'.$a),
597 array('nickname' => '[a-zA-Z0-9]{1,64}'));
600 foreach (array('all', 'replies', 'favorites') as $a) {
601 $m->connect(':nickname/'.$a.'/rss',
602 array('action' => $a.'rss'),
603 array('nickname' => '[a-zA-Z0-9]{1,64}'));
606 $m->connect(':nickname/favorites',
607 array('action' => 'showfavorites'),
608 array('nickname' => '[a-zA-Z0-9]{1,64}'));
610 $m->connect(':nickname/avatar/:size',
611 array('action' => 'avatarbynickname'),
612 array('size' => '(original|96|48|24)',
613 'nickname' => '[a-zA-Z0-9]{1,64}'));
615 $m->connect(':nickname/tag/:tag/rss',
616 array('action' => 'userrss'),
617 array('nickname' => '[a-zA-Z0-9]{1,64}'),
618 array('tag' => '[a-zA-Z0-9]+'));
620 $m->connect(':nickname/tag/:tag',
621 array('action' => 'showstream'),
622 array('nickname' => '[a-zA-Z0-9]{1,64}'),
623 array('tag' => '[a-zA-Z0-9]+'));
625 $m->connect(':nickname',
626 array('action' => 'showstream'),
627 array('nickname' => '[a-zA-Z0-9]{1,64}'));
629 Event::handle('RouterInitialized', array($m));
637 $match = $this->m->match($path);
638 } catch (Net_URL_Mapper_InvalidException $e) {
639 common_log(LOG_ERR, "Problem getting route for $path - " .
641 $cac = new ClientErrorAction("Page not found.", 404);
648 function build($action, $args=null, $params=null, $fragment=null)
650 $action_arg = array('action' => $action);
653 $args = array_merge($action_arg, $args);
658 $url = $this->m->generate($args, $params, $fragment);
660 // Due to a bug in the Net_URL_Mapper code, the returned URL may
661 // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
662 // repair that here rather than modifying the upstream code...
664 $qpos = strpos($url, '?');
665 if ($qpos !== false) {
666 $url = substr($url, 0, $qpos+1) .
667 str_replace('?', '&', substr($url, $qpos+1));