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('twitter/authorization', array('action' => 'twitterauthorization'));
97 $m->connect('facebook', array('action' => 'facebookhome'));
98 $m->connect('facebook/index.php', array('action' => 'facebookhome'));
99 $m->connect('facebook/settings.php', array('action' => 'facebooksettings'));
100 $m->connect('facebook/invite.php', array('action' => 'facebookinvite'));
101 $m->connect('facebook/remove', array('action' => 'facebookremove'));
103 // main stuff is repetitive
105 $main = array('login', 'logout', 'register', 'subscribe',
106 'unsubscribe', 'confirmaddress', 'recoverpassword',
107 'invite', 'favor', 'disfavor', 'sup',
108 'block', 'unblock', 'subedit',
109 'groupblock', 'groupunblock');
111 foreach ($main as $a) {
112 $m->connect('main/'.$a, array('action' => $a));
115 $m->connect('main/sup/:seconds', array('action' => 'sup'),
116 array('seconds' => '[0-9]+'));
118 $m->connect('main/tagother/:id', array('action' => 'tagother'));
120 $m->connect('main/oembed.xml',
121 array('action' => 'api',
122 'method' => 'oembed.xml',
123 'apiaction' => 'oembed'));
125 $m->connect('main/oembed.json',
126 array('action' => 'api',
127 'method' => 'oembed.json',
128 'apiaction' => 'oembed'));
132 foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
133 $m->connect('main/'.$c.'/:code', array('action' => $c));
138 $m->connect('main/openid', array('action' => 'openidlogin'));
139 $m->connect('main/remote', array('action' => 'remotesubscribe'));
140 $m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '[A-Za-z0-9_-]+'));
142 foreach (Router::$bare as $action) {
143 $m->connect('index.php?action=' . $action, array('action' => $action));
148 foreach (array('profile', 'avatar', 'password', 'openid', 'im',
149 'email', 'sms', 'twitter', 'userdesign', 'other') as $s) {
150 $m->connect('settings/'.$s, array('action' => $s.'settings'));
155 foreach (array('group', 'people', 'notice') as $s) {
156 $m->connect('search/'.$s, array('action' => $s.'search'));
157 $m->connect('search/'.$s.'?q=:q',
158 array('action' => $s.'search'),
162 // The second of these is needed to make the link work correctly
163 // when inserted into the page. The first is needed to match the
164 // route on the way in. Seems to be another Net_URL_Mapper bug to me.
165 $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
166 $m->connect('search/notice/rss?q=:q', array('action' => 'noticesearchrss'),
169 $m->connect('attachment/:attachment',
170 array('action' => 'attachment'),
171 array('attachment' => '[0-9]+'));
173 $m->connect('attachment/:attachment/ajax',
174 array('action' => 'attachment_ajax'),
175 array('attachment' => '[0-9]+'));
177 $m->connect('attachment/:attachment/thumbnail',
178 array('action' => 'attachment_thumbnail'),
179 array('attachment' => '[0-9]+'));
181 $m->connect('notice/new', array('action' => 'newnotice'));
182 $m->connect('notice/new?replyto=:replyto',
183 array('action' => 'newnotice'),
184 array('replyto' => '[A-Za-z0-9_-]+'));
186 $m->connect('notice/:notice/file',
187 array('action' => 'file'),
188 array('notice' => '[0-9]+'));
190 $m->connect('notice/:notice',
191 array('action' => 'shownotice'),
192 array('notice' => '[0-9]+'));
193 $m->connect('notice/delete', array('action' => 'deletenotice'));
194 $m->connect('notice/delete/:notice',
195 array('action' => 'deletenotice'),
196 array('notice' => '[0-9]+'));
200 $m->connect('conversation/:id',
201 array('action' => 'conversation'),
202 array('id' => '[0-9]+'));
204 $m->connect('message/new', array('action' => 'newmessage'));
205 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
206 $m->connect('message/:message',
207 array('action' => 'showmessage'),
208 array('message' => '[0-9]+'));
210 $m->connect('user/:id',
211 array('action' => 'userbyid'),
212 array('id' => '[0-9]+'));
214 $m->connect('tags/', array('action' => 'publictagcloud'));
215 $m->connect('tag/', array('action' => 'publictagcloud'));
216 $m->connect('tags', array('action' => 'publictagcloud'));
217 $m->connect('tag', array('action' => 'publictagcloud'));
218 $m->connect('tag/:tag/rss',
219 array('action' => 'tagrss'),
220 array('tag' => '[a-zA-Z0-9]+'));
221 $m->connect('tag/:tag',
222 array('action' => 'tag'),
223 array('tag' => '[\pL\pN_\-\.]{1,64}'));
225 $m->connect('peopletag/:tag',
226 array('action' => 'peopletag'),
227 array('tag' => '[a-zA-Z0-9]+'));
229 $m->connect('featured/', array('action' => 'featured'));
230 $m->connect('featured', array('action' => 'featured'));
231 $m->connect('favorited/', array('action' => 'favorited'));
232 $m->connect('favorited', array('action' => 'favorited'));
236 $m->connect('group/new', array('action' => 'newgroup'));
238 foreach (array('edit', 'join', 'leave') as $v) {
239 $m->connect('group/:nickname/'.$v,
240 array('action' => $v.'group'),
241 array('nickname' => '[a-zA-Z0-9]+'));
244 foreach (array('members', 'logo', 'rss', 'designsettings') as $n) {
245 $m->connect('group/:nickname/'.$n,
246 array('action' => 'group'.$n),
247 array('nickname' => '[a-zA-Z0-9]+'));
250 $m->connect('group/:nickname/blocked',
251 array('action' => 'blockedfromgroup'),
252 array('nickname' => '[a-zA-Z0-9]+'));
254 $m->connect('group/:nickname/makeadmin',
255 array('action' => 'makeadmin'),
256 array('nickname' => '[a-zA-Z0-9]+'));
258 $m->connect('group/:id/id',
259 array('action' => 'groupbyid'),
260 array('id' => '[0-9]+'));
262 $m->connect('group/:nickname',
263 array('action' => 'showgroup'),
264 array('nickname' => '[a-zA-Z0-9]+'));
266 $m->connect('group/', array('action' => 'groups'));
267 $m->connect('group', array('action' => 'groups'));
268 $m->connect('groups/', array('action' => 'groups'));
269 $m->connect('groups', array('action' => 'groups'));
271 // Twitter-compatible API
275 $m->connect('api/statuses/:method',
276 array('action' => 'api',
277 'apiaction' => 'statuses'),
278 array('method' => '(public_timeline|friends_timeline|user_timeline|update|replies|mentions|show|friends|followers|featured)(\.(atom|rss|xml|json))?'));
280 $m->connect('api/statuses/:method/:argument',
281 array('action' => 'api',
282 'apiaction' => 'statuses'),
283 array('method' => '(|user_timeline|friends_timeline|replies|mentions|show|destroy|friends|followers)'));
287 $m->connect('api/users/:method/:argument',
288 array('action' => 'api',
289 'apiaction' => 'users'),
290 array('method' => 'show(\.(xml|json))?'));
292 $m->connect('api/users/:method',
293 array('action' => 'api',
294 'apiaction' => 'users'),
295 array('method' => 'show(\.(xml|json))?'));
299 foreach (array('xml', 'json') as $e) {
300 $m->connect('api/direct_messages/new.'.$e,
301 array('action' => 'api',
302 'apiaction' => 'direct_messages',
303 'method' => 'create.'.$e));
306 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
307 $m->connect('api/direct_messages.'.$e,
308 array('action' => 'api',
309 'apiaction' => 'direct_messages',
310 'method' => 'direct_messages.'.$e));
313 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
314 $m->connect('api/direct_messages/sent.'.$e,
315 array('action' => 'api',
316 'apiaction' => 'direct_messages',
317 'method' => 'sent.'.$e));
320 $m->connect('api/direct_messages/destroy/:argument',
321 array('action' => 'api',
322 'apiaction' => 'direct_messages'));
326 $m->connect('api/friendships/:method/:argument',
327 array('action' => 'api',
328 'apiaction' => 'friendships'),
329 array('method' => '(create|destroy)'));
331 $m->connect('api/friendships/:method',
332 array('action' => 'api',
333 'apiaction' => 'friendships'),
334 array('method' => '(show|exists)(\.(xml|json))'));
338 $m->connect('api/friends/ids/:argument',
339 array('action' => 'api',
340 'apiaction' => 'statuses',
341 'method' => 'friendsIDs'));
343 foreach (array('xml', 'json') as $e) {
344 $m->connect('api/friends/ids.'.$e,
345 array('action' => 'api',
346 'apiaction' => 'statuses',
347 'method' => 'friendsIDs.'.$e));
350 $m->connect('api/followers/ids/:argument',
351 array('action' => 'api',
352 'apiaction' => 'statuses',
353 'method' => 'followersIDs'));
355 foreach (array('xml', 'json') as $e) {
356 $m->connect('api/followers/ids.'.$e,
357 array('action' => 'api',
358 'apiaction' => 'statuses',
359 'method' => 'followersIDs.'.$e));
364 $m->connect('api/account/:method',
365 array('action' => 'api',
366 'apiaction' => 'account'));
370 $m->connect('api/favorites/:method/:argument',
371 array('action' => 'api',
372 'apiaction' => 'favorites',
373 array('method' => '(create|destroy)')));
375 $m->connect('api/favorites/:argument',
376 array('action' => 'api',
377 'apiaction' => 'favorites',
378 'method' => 'favorites'));
380 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
381 $m->connect('api/favorites.'.$e,
382 array('action' => 'api',
383 'apiaction' => 'favorites',
384 'method' => 'favorites.'.$e));
389 $m->connect('api/notifications/:method/:argument',
390 array('action' => 'api',
391 'apiaction' => 'favorites'));
395 $m->connect('api/blocks/:method/:argument',
396 array('action' => 'api',
397 'apiaction' => 'blocks'));
401 $m->connect('api/help/:method',
402 array('action' => 'api',
403 'apiaction' => 'help'));
407 $m->connect('api/laconica/:method',
408 array('action' => 'api',
409 'apiaction' => 'laconica'));
411 $m->connect('api/laconica/:method',
412 array('action' => 'api',
413 'apiaction' => 'laconica'));
416 $m->connect('api/laconica/groups/:method/:argument',
417 array('action' => 'api',
418 'apiaction' => 'groups'));
420 $m->connect('api/laconica/groups/:method',
421 array('action' => 'api',
422 'apiaction' => 'groups'));
425 $m->connect('api/laconica/tags/:method/:argument',
426 array('action' => 'api',
427 'apiaction' => 'tags'));
429 $m->connect('api/laconica/tags/:method',
430 array('action' => 'api',
431 'apiaction' => 'tags'));
434 $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
435 $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
436 $m->connect('api/trends.json', array('action' => 'twitapitrends'));
440 foreach (array('subscriptions', 'subscribers',
441 'nudge', 'xrds', 'all', 'foaf',
442 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
443 $m->connect(':nickname/'.$a,
444 array('action' => $a),
445 array('nickname' => '[a-zA-Z0-9]{1,64}'));
448 foreach (array('subscriptions', 'subscribers') as $a) {
449 $m->connect(':nickname/'.$a.'/:tag',
450 array('action' => $a),
451 array('tag' => '[a-zA-Z0-9]+',
452 'nickname' => '[a-zA-Z0-9]{1,64}'));
455 foreach (array('rss', 'groups') as $a) {
456 $m->connect(':nickname/'.$a,
457 array('action' => 'user'.$a),
458 array('nickname' => '[a-zA-Z0-9]{1,64}'));
461 foreach (array('all', 'replies', 'favorites') as $a) {
462 $m->connect(':nickname/'.$a.'/rss',
463 array('action' => $a.'rss'),
464 array('nickname' => '[a-zA-Z0-9]{1,64}'));
467 $m->connect(':nickname/favorites',
468 array('action' => 'showfavorites'),
469 array('nickname' => '[a-zA-Z0-9]{1,64}'));
471 $m->connect(':nickname/avatar/:size',
472 array('action' => 'avatarbynickname'),
473 array('size' => '(original|96|48|24)',
474 'nickname' => '[a-zA-Z0-9]{1,64}'));
476 $m->connect(':nickname/tag/:tag/rss',
477 array('action' => 'userrss'),
478 array('nickname' => '[a-zA-Z0-9]{1,64}'),
479 array('tag' => '[a-zA-Z0-9]+'));
481 $m->connect(':nickname/tag/:tag',
482 array('action' => 'showstream'),
483 array('nickname' => '[a-zA-Z0-9]{1,64}'),
484 array('tag' => '[a-zA-Z0-9]+'));
486 $m->connect(':nickname',
487 array('action' => 'showstream'),
488 array('nickname' => '[a-zA-Z0-9]{1,64}'));
490 Event::handle('RouterInitialized', array($m));
498 $match = $this->m->match($path);
499 } catch (Net_URL_Mapper_InvalidException $e) {
500 common_log(LOG_ERR, "Problem getting route for $path - " .
502 $cac = new ClientErrorAction("Page not found.", 404);
509 function build($action, $args=null, $params=null, $fragment=null)
511 $action_arg = array('action' => $action);
514 $args = array_merge($action_arg, $args);
519 $url = $this->m->generate($args, $params, $fragment);
521 // Due to a bug in the Net_URL_Mapper code, the returned URL may
522 // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
523 // repair that here rather than modifying the upstream code...
525 $qpos = strpos($url, '?');
526 if ($qpos !== false) {
527 $url = substr($url, 0, $qpos+1) .
528 str_replace('?', '&', substr($url, $qpos+1));