3 * Laconica, 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@controlyourself.ca>
25 * @copyright 2009 Control Yourself, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://laconi.ca/
30 if (!defined('LACONICA')) {
34 require_once 'Net/URL/Mapper.php';
39 * Cheap wrapper around Net_URL_Mapper
43 * @author Evan Prodromou <evan@controlyourself.ca>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://laconi.ca/
52 static $bare = array('requesttoken', 'accesstoken', 'userauthorization',
53 'postnotice', 'updateprofile', 'finishremotesubscribe',
54 'finishopenidlogin', 'finishaddopenid');
59 Router::$inst = new Router();
64 function __construct()
67 $this->m = $this->initialize();
73 $m = Net_URL_Mapper::getInstance();
77 $m->connect('', array('action' => 'public'));
78 $m->connect('rss', array('action' => 'publicrss'));
79 $m->connect('xrds', array('action' => 'publicxrds'));
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'));
93 $m->connect('facebook', array('action' => 'facebookhome'));
94 $m->connect('facebook/index.php', array('action' => 'facebookhome'));
95 $m->connect('facebook/settings.php', array('action' => 'facebooksettings'));
96 $m->connect('facebook/invite.php', array('action' => 'facebookinvite'));
97 $m->connect('facebook/remove', array('action' => 'facebookremove'));
99 // main stuff is repetitive
101 $main = array('login', 'logout', 'register', 'subscribe',
102 'unsubscribe', 'confirmaddress', 'recoverpassword',
103 'invite', 'favor', 'disfavor', 'sup',
104 'block', 'unblock', 'subedit',
105 'groupblock', 'groupunblock');
107 foreach ($main as $a) {
108 $m->connect('main/'.$a, array('action' => $a));
111 $m->connect('main/sup/:seconds', array('action' => 'sup'),
112 array('seconds' => '[0-9]+'));
114 $m->connect('main/tagother/:id', array('action' => 'tagother'));
118 foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
119 $m->connect('main/'.$c.'/:code', array('action' => $c));
124 $m->connect('main/openid', array('action' => 'openidlogin'));
125 $m->connect('main/remote', array('action' => 'remotesubscribe'));
126 $m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '[A-Za-z0-9_-]+'));
128 foreach (Router::$bare as $action) {
129 $m->connect('index.php?action=' . $action, array('action' => $action));
132 $m->connect('main/:method',
133 array('action' => 'api',
134 'method' => 'oembed(.xml|.json)?',
135 'apiaction' => 'oembed'));
139 foreach (array('profile', 'avatar', 'password', 'openid', 'im',
140 'email', 'sms', 'twitter', 'userdesign', 'other') as $s) {
141 $m->connect('settings/'.$s, array('action' => $s.'settings'));
146 foreach (array('group', 'people', 'notice') as $s) {
147 $m->connect('search/'.$s, array('action' => $s.'search'));
148 $m->connect('search/'.$s.'?q=:q',
149 array('action' => $s.'search'),
153 // The second of these is needed to make the link work correctly
154 // when inserted into the page. The first is needed to match the
155 // route on the way in. Seems to be another Net_URL_Mapper bug to me.
156 $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
157 $m->connect('search/notice/rss?q=:q', array('action' => 'noticesearchrss'),
160 $m->connect('attachment/:attachment',
161 array('action' => 'attachment'),
162 array('attachment' => '[0-9]+'));
164 $m->connect('attachment/:attachment/ajax',
165 array('action' => 'attachment_ajax'),
166 array('attachment' => '[0-9]+'));
168 $m->connect('attachment/:attachment/thumbnail',
169 array('action' => 'attachment_thumbnail'),
170 array('attachment' => '[0-9]+'));
172 $m->connect('notice/new', array('action' => 'newnotice'));
173 $m->connect('notice/new?replyto=:replyto',
174 array('action' => 'newnotice'),
175 array('replyto' => '[A-Za-z0-9_-]+'));
177 $m->connect('notice/:notice/file',
178 array('action' => 'file'),
179 array('notice' => '[0-9]+'));
181 $m->connect('notice/:notice',
182 array('action' => 'shownotice'),
183 array('notice' => '[0-9]+'));
184 $m->connect('notice/delete', array('action' => 'deletenotice'));
185 $m->connect('notice/delete/:notice',
186 array('action' => 'deletenotice'),
187 array('notice' => '[0-9]+'));
191 $m->connect('conversation/:id',
192 array('action' => 'conversation'),
193 array('id' => '[0-9]+'));
195 $m->connect('message/new', array('action' => 'newmessage'));
196 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
197 $m->connect('message/:message',
198 array('action' => 'showmessage'),
199 array('message' => '[0-9]+'));
201 $m->connect('user/:id',
202 array('action' => 'userbyid'),
203 array('id' => '[0-9]+'));
205 $m->connect('tags/', array('action' => 'publictagcloud'));
206 $m->connect('tag/', array('action' => 'publictagcloud'));
207 $m->connect('tags', array('action' => 'publictagcloud'));
208 $m->connect('tag', array('action' => 'publictagcloud'));
209 $m->connect('tag/:tag/rss',
210 array('action' => 'tagrss'),
211 array('tag' => '[a-zA-Z0-9]+'));
212 $m->connect('tag/:tag',
213 array('action' => 'tag'),
214 array('tag' => '[a-zA-Z0-9]+'));
216 $m->connect('peopletag/:tag',
217 array('action' => 'peopletag'),
218 array('tag' => '[a-zA-Z0-9]+'));
220 $m->connect('featured/', array('action' => 'featured'));
221 $m->connect('featured', array('action' => 'featured'));
222 $m->connect('favorited/', array('action' => 'favorited'));
223 $m->connect('favorited', array('action' => 'favorited'));
227 $m->connect('group/new', array('action' => 'newgroup'));
229 foreach (array('edit', 'join', 'leave') as $v) {
230 $m->connect('group/:nickname/'.$v,
231 array('action' => $v.'group'),
232 array('nickname' => '[a-zA-Z0-9]+'));
235 foreach (array('members', 'logo', 'rss', 'designsettings') as $n) {
236 $m->connect('group/:nickname/'.$n,
237 array('action' => 'group'.$n),
238 array('nickname' => '[a-zA-Z0-9]+'));
241 $m->connect('group/:nickname/blocked',
242 array('action' => 'blockedfromgroup'),
243 array('nickname' => '[a-zA-Z0-9]+'));
245 $m->connect('group/:nickname/makeadmin',
246 array('action' => 'makeadmin'),
247 array('nickname' => '[a-zA-Z0-9]+'));
249 $m->connect('group/:id/id',
250 array('action' => 'groupbyid'),
251 array('id' => '[0-9]+'));
253 $m->connect('group/:nickname',
254 array('action' => 'showgroup'),
255 array('nickname' => '[a-zA-Z0-9]+'));
257 $m->connect('group/', array('action' => 'groups'));
258 $m->connect('group', array('action' => 'groups'));
259 $m->connect('groups/', array('action' => 'groups'));
260 $m->connect('groups', array('action' => 'groups'));
262 // Twitter-compatible API
266 $m->connect('api/statuses/:method',
267 array('action' => 'api',
268 'apiaction' => 'statuses'),
269 array('method' => '(public_timeline|friends_timeline|user_timeline|update|replies|mentions|show|friends|followers|featured)(\.(atom|rss|xml|json))?'));
271 $m->connect('api/statuses/:method/:argument',
272 array('action' => 'api',
273 'apiaction' => 'statuses'),
274 array('method' => '(|user_timeline|friends_timeline|replies|mentions|show|destroy|friends|followers)'));
278 $m->connect('api/users/:method/:argument',
279 array('action' => 'api',
280 'apiaction' => 'users'),
281 array('method' => 'show(\.(xml|json))?'));
283 $m->connect('api/users/:method',
284 array('action' => 'api',
285 'apiaction' => 'users'),
286 array('method' => 'show(\.(xml|json))?'));
290 foreach (array('xml', 'json') as $e) {
291 $m->connect('api/direct_messages/new.'.$e,
292 array('action' => 'api',
293 'apiaction' => 'direct_messages',
294 'method' => 'create.'.$e));
297 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
298 $m->connect('api/direct_messages.'.$e,
299 array('action' => 'api',
300 'apiaction' => 'direct_messages',
301 'method' => 'direct_messages.'.$e));
304 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
305 $m->connect('api/direct_messages/sent.'.$e,
306 array('action' => 'api',
307 'apiaction' => 'direct_messages',
308 'method' => 'sent.'.$e));
311 $m->connect('api/direct_messages/destroy/:argument',
312 array('action' => 'api',
313 'apiaction' => 'direct_messages'));
317 $m->connect('api/friendships/:method/:argument',
318 array('action' => 'api',
319 'apiaction' => 'friendships'),
320 array('method' => '(create|destroy)'));
322 $m->connect('api/friendships/:method',
323 array('action' => 'api',
324 'apiaction' => 'friendships'),
325 array('method' => '(show|exists)(\.(xml|json))'));
329 $m->connect('api/friends/ids/:argument',
330 array('action' => 'api',
331 'apiaction' => 'statuses',
332 'method' => 'friendsIDs'));
334 foreach (array('xml', 'json') as $e) {
335 $m->connect('api/friends/ids.'.$e,
336 array('action' => 'api',
337 'apiaction' => 'statuses',
338 'method' => 'friendsIDs.'.$e));
341 $m->connect('api/followers/ids/:argument',
342 array('action' => 'api',
343 'apiaction' => 'statuses',
344 'method' => 'followersIDs'));
346 foreach (array('xml', 'json') as $e) {
347 $m->connect('api/followers/ids.'.$e,
348 array('action' => 'api',
349 'apiaction' => 'statuses',
350 'method' => 'followersIDs.'.$e));
355 $m->connect('api/account/:method',
356 array('action' => 'api',
357 'apiaction' => 'account'));
361 $m->connect('api/favorites/:method/:argument',
362 array('action' => 'api',
363 'apiaction' => 'favorites',
364 array('method' => '(create|destroy)')));
366 $m->connect('api/favorites/:argument',
367 array('action' => 'api',
368 'apiaction' => 'favorites',
369 'method' => 'favorites'));
371 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
372 $m->connect('api/favorites.'.$e,
373 array('action' => 'api',
374 'apiaction' => 'favorites',
375 'method' => 'favorites.'.$e));
380 $m->connect('api/notifications/:method/:argument',
381 array('action' => 'api',
382 'apiaction' => 'favorites'));
386 $m->connect('api/blocks/:method/:argument',
387 array('action' => 'api',
388 'apiaction' => 'blocks'));
392 $m->connect('api/help/:method',
393 array('action' => 'api',
394 'apiaction' => 'help'));
398 $m->connect('api/laconica/:method',
399 array('action' => 'api',
400 'apiaction' => 'laconica'));
402 $m->connect('api/laconica/:method',
403 array('action' => 'api',
404 'apiaction' => 'laconica'));
407 $m->connect('api/laconica/groups/:method/:argument',
408 array('action' => 'api',
409 'apiaction' => 'groups'));
411 $m->connect('api/laconica/groups/:method',
412 array('action' => 'api',
413 'apiaction' => 'groups'));
416 $m->connect('api/laconica/tags/:method/:argument',
417 array('action' => 'api',
418 'apiaction' => 'tags'));
420 $m->connect('api/laconica/tags/:method',
421 array('action' => 'api',
422 'apiaction' => 'tags'));
425 $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
426 $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
427 $m->connect('api/trends.json', array('action' => 'twitapitrends'));
431 foreach (array('subscriptions', 'subscribers',
432 'nudge', 'xrds', 'all', 'foaf',
433 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
434 $m->connect(':nickname/'.$a,
435 array('action' => $a),
436 array('nickname' => '[a-zA-Z0-9]{1,64}'));
439 foreach (array('subscriptions', 'subscribers') as $a) {
440 $m->connect(':nickname/'.$a.'/:tag',
441 array('action' => $a),
442 array('tag' => '[a-zA-Z0-9]+',
443 'nickname' => '[a-zA-Z0-9]{1,64}'));
446 foreach (array('rss', 'groups') as $a) {
447 $m->connect(':nickname/'.$a,
448 array('action' => 'user'.$a),
449 array('nickname' => '[a-zA-Z0-9]{1,64}'));
452 foreach (array('all', 'replies', 'favorites') as $a) {
453 $m->connect(':nickname/'.$a.'/rss',
454 array('action' => $a.'rss'),
455 array('nickname' => '[a-zA-Z0-9]{1,64}'));
458 $m->connect(':nickname/favorites',
459 array('action' => 'showfavorites'),
460 array('nickname' => '[a-zA-Z0-9]{1,64}'));
462 $m->connect(':nickname/avatar/:size',
463 array('action' => 'avatarbynickname'),
464 array('size' => '(original|96|48|24)',
465 'nickname' => '[a-zA-Z0-9]{1,64}'));
467 $m->connect(':nickname/tag/:tag/rss',
468 array('action' => 'userrss'),
469 array('nickname' => '[a-zA-Z0-9]{1,64}'),
470 array('tag' => '[a-zA-Z0-9]+'));
472 $m->connect(':nickname/tag/:tag',
473 array('action' => 'showstream'),
474 array('nickname' => '[a-zA-Z0-9]{1,64}'),
475 array('tag' => '[a-zA-Z0-9]+'));
477 $m->connect(':nickname',
478 array('action' => 'showstream'),
479 array('nickname' => '[a-zA-Z0-9]{1,64}'));
481 Event::handle('RouterInitialized', array($m));
489 $match = $this->m->match($path);
490 } catch (Net_URL_Mapper_InvalidException $e) {
491 common_log(LOG_ERR, "Problem getting route for $path - " .
493 $cac = new ClientErrorAction("Page not found.", 404);
500 function build($action, $args=null, $params=null, $fragment=null)
502 $action_arg = array('action' => $action);
505 $args = array_merge($action_arg, $args);
510 $url = $this->m->generate($args, $params, $fragment);
512 // Due to a bug in the Net_URL_Mapper code, the returned URL may
513 // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
514 // repair that here rather than modifying the upstream code...
516 $qpos = strpos($url, '?');
517 if ($qpos !== false) {
518 $url = substr($url, 0, $qpos+1) .
519 str_replace('?', '&', substr($url, $qpos+1));