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'));
91 $m->connect('twitter/authorization', array('action' => 'twitterauthorization'));
95 $m->connect('facebook', array('action' => 'facebookhome'));
96 $m->connect('facebook/index.php', array('action' => 'facebookhome'));
97 $m->connect('facebook/settings.php', array('action' => 'facebooksettings'));
98 $m->connect('facebook/invite.php', array('action' => 'facebookinvite'));
99 $m->connect('facebook/remove', array('action' => 'facebookremove'));
101 // main stuff is repetitive
103 $main = array('login', 'logout', 'register', 'subscribe',
104 'unsubscribe', 'confirmaddress', 'recoverpassword',
105 'invite', 'favor', 'disfavor', 'sup',
106 'block', 'unblock', 'subedit',
107 'groupblock', 'groupunblock');
109 foreach ($main as $a) {
110 $m->connect('main/'.$a, array('action' => $a));
113 $m->connect('main/sup/:seconds', array('action' => 'sup'),
114 array('seconds' => '[0-9]+'));
116 $m->connect('main/tagother/:id', array('action' => 'tagother'));
118 $m->connect('main/oembed',
119 array('action' => 'oembed'));
123 foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
124 $m->connect('main/'.$c.'/:code', array('action' => $c));
129 $m->connect('main/remote', array('action' => 'remotesubscribe'));
130 $m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '[A-Za-z0-9_-]+'));
132 foreach (Router::$bare as $action) {
133 $m->connect('index.php?action=' . $action, array('action' => $action));
138 foreach (array('profile', 'avatar', 'password', 'im',
139 'email', 'sms', 'twitter', 'userdesign', 'other') as $s) {
140 $m->connect('settings/'.$s, array('action' => $s.'settings'));
145 foreach (array('group', 'people', 'notice') as $s) {
146 $m->connect('search/'.$s, array('action' => $s.'search'));
147 $m->connect('search/'.$s.'?q=:q',
148 array('action' => $s.'search'),
152 // The second of these is needed to make the link work correctly
153 // when inserted into the page. The first is needed to match the
154 // route on the way in. Seems to be another Net_URL_Mapper bug to me.
155 $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
156 $m->connect('search/notice/rss?q=:q', array('action' => 'noticesearchrss'),
159 $m->connect('attachment/:attachment',
160 array('action' => 'attachment'),
161 array('attachment' => '[0-9]+'));
163 $m->connect('attachment/:attachment/ajax',
164 array('action' => 'attachment_ajax'),
165 array('attachment' => '[0-9]+'));
167 $m->connect('attachment/:attachment/thumbnail',
168 array('action' => 'attachment_thumbnail'),
169 array('attachment' => '[0-9]+'));
171 $m->connect('notice/new', array('action' => 'newnotice'));
172 $m->connect('notice/new?replyto=:replyto',
173 array('action' => 'newnotice'),
174 array('replyto' => '[A-Za-z0-9_-]+'));
175 $m->connect('notice/new?replyto=:replyto&inreplyto=:inreplyto',
176 array('action' => 'newnotice'),
177 array('replyto' => '[A-Za-z0-9_-]+'),
178 array('inreplyto' => '[0-9]+'));
180 $m->connect('notice/:notice/file',
181 array('action' => 'file'),
182 array('notice' => '[0-9]+'));
184 $m->connect('notice/:notice',
185 array('action' => 'shownotice'),
186 array('notice' => '[0-9]+'));
187 $m->connect('notice/delete', array('action' => 'deletenotice'));
188 $m->connect('notice/delete/:notice',
189 array('action' => 'deletenotice'),
190 array('notice' => '[0-9]+'));
194 $m->connect('conversation/:id',
195 array('action' => 'conversation'),
196 array('id' => '[0-9]+'));
198 $m->connect('message/new', array('action' => 'newmessage'));
199 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
200 $m->connect('message/:message',
201 array('action' => 'showmessage'),
202 array('message' => '[0-9]+'));
204 $m->connect('user/:id',
205 array('action' => 'userbyid'),
206 array('id' => '[0-9]+'));
208 $m->connect('tags/', array('action' => 'publictagcloud'));
209 $m->connect('tag/', array('action' => 'publictagcloud'));
210 $m->connect('tags', array('action' => 'publictagcloud'));
211 $m->connect('tag', array('action' => 'publictagcloud'));
212 $m->connect('tag/:tag/rss',
213 array('action' => 'tagrss'),
214 array('tag' => '[a-zA-Z0-9]+'));
215 $m->connect('tag/:tag',
216 array('action' => 'tag'),
217 array('tag' => '[\pL\pN_\-\.]{1,64}'));
219 $m->connect('peopletag/:tag',
220 array('action' => 'peopletag'),
221 array('tag' => '[a-zA-Z0-9]+'));
223 $m->connect('featured/', array('action' => 'featured'));
224 $m->connect('featured', array('action' => 'featured'));
225 $m->connect('favorited/', array('action' => 'favorited'));
226 $m->connect('favorited', array('action' => 'favorited'));
230 $m->connect('group/new', array('action' => 'newgroup'));
232 foreach (array('edit', 'join', 'leave') as $v) {
233 $m->connect('group/:nickname/'.$v,
234 array('action' => $v.'group'),
235 array('nickname' => '[a-zA-Z0-9]+'));
238 foreach (array('members', 'logo', 'rss', 'designsettings') as $n) {
239 $m->connect('group/:nickname/'.$n,
240 array('action' => 'group'.$n),
241 array('nickname' => '[a-zA-Z0-9]+'));
244 $m->connect('group/:nickname/blocked',
245 array('action' => 'blockedfromgroup'),
246 array('nickname' => '[a-zA-Z0-9]+'));
248 $m->connect('group/:nickname/makeadmin',
249 array('action' => 'makeadmin'),
250 array('nickname' => '[a-zA-Z0-9]+'));
252 $m->connect('group/:id/id',
253 array('action' => 'groupbyid'),
254 array('id' => '[0-9]+'));
256 $m->connect('group/:nickname',
257 array('action' => 'showgroup'),
258 array('nickname' => '[a-zA-Z0-9]+'));
260 $m->connect('group/', array('action' => 'groups'));
261 $m->connect('group', array('action' => 'groups'));
262 $m->connect('groups/', array('action' => 'groups'));
263 $m->connect('groups', array('action' => 'groups'));
265 // Twitter-compatible API
269 $m->connect('api/statuses/public_timeline.:format',
270 array('action' => 'ApiPublicTimeline',
271 'format' => '(xml|json|rss|atom)'));
273 $m->connect('api/statuses/friends_timeline.:format',
274 array('action' => 'ApiFriendsTimeline',
275 'format' => '(xml|json|rss|atom)'));
277 $m->connect('api/statuses/friends_timeline/:id.:format',
278 array('action' => 'ApiFriendsTimeline',
279 'id' => '[a-zA-Z0-9]+',
280 'format' => '(xml|json|rss|atom)'));
281 $m->connect('api/statuses/home_timeline.:format',
282 array('action' => 'ApiFriendsTimeline',
283 'format' => '(xml|json|rss|atom)'));
285 $m->connect('api/statuses/home_timeline/:id.:format',
286 array('action' => 'ApiFriendsTimeline',
287 'id' => '[a-zA-Z0-9]+',
288 'format' => '(xml|json|rss|atom)'));
290 $m->connect('api/statuses/user_timeline.:format',
291 array('action' => 'ApiUserTimeline',
292 'format' => '(xml|json|rss|atom)'));
294 $m->connect('api/statuses/user_timeline/:id.:format',
295 array('action' => 'ApiUserTimeline',
296 'id' => '[a-zA-Z0-9]+',
297 'format' => '(xml|json|rss|atom)'));
299 $m->connect('api/statuses/mentions.:format',
300 array('action' => 'ApiMentions',
301 'format' => '(xml|json|rss|atom)'));
303 $m->connect('api/statuses/mentions/:id.:format',
304 array('action' => 'ApiMentions',
305 'id' => '[a-zA-Z0-9]+',
306 'format' => '(xml|json|rss|atom)'));
308 $m->connect('api/statuses/replies.:format',
309 array('action' => 'ApiMentions',
310 'format' => '(xml|json|rss|atom)'));
312 $m->connect('api/statuses/replies/:id.:format',
313 array('action' => 'ApiMentions',
314 'id' => '[a-zA-Z0-9]+',
315 'format' => '(xml|json|rss|atom)'));
317 $m->connect('api/statuses/friends.:format',
318 array('action' => 'ApiFriends',
319 'format' => '(xml|json)'));
321 $m->connect('api/statuses/friends/:id.:format',
322 array('action' => 'ApiFriends',
323 'id' => '[a-zA-Z0-9]+',
324 'format' => '(xml|json)'));
326 $m->connect('api/statuses/followers.:format',
327 array('action' => 'ApiFollowers',
328 'format' => '(xml|json)'));
330 $m->connect('api/statuses/followers/:id.:format',
331 array('action' => 'ApiFollowers',
332 'id' => '[a-zA-Z0-9]+',
333 'format' => '(xml|json)'));
335 $m->connect('api/statuses/show.:format',
336 array('action' => 'ApiShow',
337 'format' => '(xml|json)'));
339 $m->connect('api/statuses/show/:id.:format',
340 array('action' => 'ApiShow',
341 'id' => '[a-zA-Z0-9]+',
342 'format' => '(xml|json)'));
344 $m->connect('api/statuses/update.:format',
345 array('action' => 'ApiUpdate',
346 'format' => '(xml|json)'));
348 $m->connect('api/statuses/:method/:argument',
349 array('action' => 'api',
350 'apiaction' => 'statuses'),
351 array('method' => 'destroy'));
355 $m->connect('api/users/:method/:argument',
356 array('action' => 'api',
357 'apiaction' => 'users'),
358 array('method' => 'show(\.(xml|json))?'));
360 $m->connect('api/users/:method',
361 array('action' => 'api',
362 'apiaction' => 'users'),
363 array('method' => 'show(\.(xml|json))?'));
367 foreach (array('xml', 'json') as $e) {
368 $m->connect('api/direct_messages/new.'.$e,
369 array('action' => 'api',
370 'apiaction' => 'direct_messages',
371 'method' => 'create.'.$e));
374 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
375 $m->connect('api/direct_messages.'.$e,
376 array('action' => 'api',
377 'apiaction' => 'direct_messages',
378 'method' => 'direct_messages.'.$e));
381 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
382 $m->connect('api/direct_messages/sent.'.$e,
383 array('action' => 'api',
384 'apiaction' => 'direct_messages',
385 'method' => 'sent.'.$e));
388 $m->connect('api/direct_messages/destroy/:argument',
389 array('action' => 'api',
390 'apiaction' => 'direct_messages'));
394 $m->connect('api/friendships/:method/:argument',
395 array('action' => 'api',
396 'apiaction' => 'friendships'),
397 array('method' => '(create|destroy)'));
399 $m->connect('api/friendships/:method',
400 array('action' => 'api',
401 'apiaction' => 'friendships'),
402 array('method' => '(show|exists)(\.(xml|json))'));
406 $m->connect('api/friends/ids/:id.:format',
407 array('action' => 'apiFriends',
408 'ids_only' => true));
410 $m->connect('api/followers/ids/:id.:format',
411 array('action' => 'apiFollowers',
412 'ids_only' => true));
414 $m->connect('api/friends/ids.:format',
415 array('action' => 'apiFriends',
416 'ids_only' => true));
418 $m->connect('api/followers/ids.:format',
419 array('action' => 'apiFollowers',
420 'ids_only' => true));
424 $m->connect('api/account/:method',
425 array('action' => 'api',
426 'apiaction' => 'account'));
430 $m->connect('api/favorites/:method/:argument',
431 array('action' => 'api',
432 'apiaction' => 'favorites',
433 array('method' => '(create|destroy)')));
435 $m->connect('api/favorites/:argument',
436 array('action' => 'api',
437 'apiaction' => 'favorites',
438 'method' => 'favorites'));
440 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
441 $m->connect('api/favorites.'.$e,
442 array('action' => 'api',
443 'apiaction' => 'favorites',
444 'method' => 'favorites.'.$e));
449 $m->connect('api/notifications/:method/:argument',
450 array('action' => 'api',
451 'apiaction' => 'favorites'));
455 $m->connect('api/blocks/:method/:argument',
456 array('action' => 'api',
457 'apiaction' => 'blocks'));
461 $m->connect('api/help/:method',
462 array('action' => 'api',
463 'apiaction' => 'help'));
467 $m->connect('api/statusnet/:method',
468 array('action' => 'api',
469 'apiaction' => 'statusnet'));
471 // For older methods, we provide "laconica" base action
473 $m->connect('api/laconica/:method',
474 array('action' => 'api',
475 'apiaction' => 'statusnet'));
477 // Groups and tags are newer than 0.8.1 so no backward-compatibility
481 //'list' has to be handled differently, as php will not allow a method to be named 'list'
482 $m->connect('api/statusnet/groups/list/:argument',
483 array('action' => 'api',
484 'method' => 'list_groups',
485 'apiaction' => 'groups'));
487 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
488 $m->connect('api/statusnet/groups/list.' . $e,
489 array('action' => 'api',
490 'method' => 'list_groups.' . $e,
491 'apiaction' => 'groups'));
494 $m->connect('api/statusnet/groups/:method',
495 array('action' => 'api',
496 'apiaction' => 'statuses'),
497 array('method' => '(list_all|)(\.(atom|rss|xml|json))?'));
499 $m->connect('api/statuses/:method/:argument',
500 array('action' => 'api',
501 'apiaction' => 'statuses'),
502 array('method' => '(user_timeline|home_timeline|friends_timeline|replies|mentions|show|destroy|friends|followers)'));
504 $m->connect('api/statusnet/groups/:method/:argument',
505 array('action' => 'api',
506 'apiaction' => 'groups'));
508 $m->connect('api/statusnet/groups/:method',
509 array('action' => 'api',
510 'apiaction' => 'groups'));
513 $m->connect('api/statusnet/tags/:method/:argument',
514 array('action' => 'api',
515 'apiaction' => 'tags'));
517 $m->connect('api/statusnet/tags/:method',
518 array('action' => 'api',
519 'apiaction' => 'tags'));
522 $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
523 $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
524 $m->connect('api/trends.json', array('action' => 'twitapitrends'));
528 foreach (array('subscriptions', 'subscribers',
529 'nudge', 'all', 'foaf', 'xrds',
530 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
531 $m->connect(':nickname/'.$a,
532 array('action' => $a),
533 array('nickname' => '[a-zA-Z0-9]{1,64}'));
536 foreach (array('subscriptions', 'subscribers') as $a) {
537 $m->connect(':nickname/'.$a.'/:tag',
538 array('action' => $a),
539 array('tag' => '[a-zA-Z0-9]+',
540 'nickname' => '[a-zA-Z0-9]{1,64}'));
543 foreach (array('rss', 'groups') as $a) {
544 $m->connect(':nickname/'.$a,
545 array('action' => 'user'.$a),
546 array('nickname' => '[a-zA-Z0-9]{1,64}'));
549 foreach (array('all', 'replies', 'favorites') as $a) {
550 $m->connect(':nickname/'.$a.'/rss',
551 array('action' => $a.'rss'),
552 array('nickname' => '[a-zA-Z0-9]{1,64}'));
555 $m->connect(':nickname/favorites',
556 array('action' => 'showfavorites'),
557 array('nickname' => '[a-zA-Z0-9]{1,64}'));
559 $m->connect(':nickname/avatar/:size',
560 array('action' => 'avatarbynickname'),
561 array('size' => '(original|96|48|24)',
562 'nickname' => '[a-zA-Z0-9]{1,64}'));
564 $m->connect(':nickname/tag/:tag/rss',
565 array('action' => 'userrss'),
566 array('nickname' => '[a-zA-Z0-9]{1,64}'),
567 array('tag' => '[a-zA-Z0-9]+'));
569 $m->connect(':nickname/tag/:tag',
570 array('action' => 'showstream'),
571 array('nickname' => '[a-zA-Z0-9]{1,64}'),
572 array('tag' => '[a-zA-Z0-9]+'));
574 $m->connect(':nickname',
575 array('action' => 'showstream'),
576 array('nickname' => '[a-zA-Z0-9]{1,64}'));
578 Event::handle('RouterInitialized', array($m));
586 $match = $this->m->match($path);
587 } catch (Net_URL_Mapper_InvalidException $e) {
588 common_log(LOG_ERR, "Problem getting route for $path - " .
590 $cac = new ClientErrorAction("Page not found.", 404);
597 function build($action, $args=null, $params=null, $fragment=null)
599 $action_arg = array('action' => $action);
602 $args = array_merge($action_arg, $args);
607 $url = $this->m->generate($args, $params, $fragment);
609 // Due to a bug in the Net_URL_Mapper code, the returned URL may
610 // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
611 // repair that here rather than modifying the upstream code...
613 $qpos = strpos($url, '?');
614 if ($qpos !== false) {
615 $url = substr($url, 0, $qpos+1) .
616 str_replace('?', '&', substr($url, $qpos+1));