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'));
122 foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
123 $m->connect('main/'.$c.'/:code', array('action' => $c));
128 $m->connect('main/openid', array('action' => 'openidlogin'));
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', 'openid', '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_-]+'));
176 $m->connect('notice/:notice/file',
177 array('action' => 'file'),
178 array('notice' => '[0-9]+'));
180 $m->connect('notice/:notice',
181 array('action' => 'shownotice'),
182 array('notice' => '[0-9]+'));
183 $m->connect('notice/delete', array('action' => 'deletenotice'));
184 $m->connect('notice/delete/:notice',
185 array('action' => 'deletenotice'),
186 array('notice' => '[0-9]+'));
190 $m->connect('conversation/:id',
191 array('action' => 'conversation'),
192 array('id' => '[0-9]+'));
194 $m->connect('message/new', array('action' => 'newmessage'));
195 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
196 $m->connect('message/:message',
197 array('action' => 'showmessage'),
198 array('message' => '[0-9]+'));
200 $m->connect('user/:id',
201 array('action' => 'userbyid'),
202 array('id' => '[0-9]+'));
204 $m->connect('tags/', array('action' => 'publictagcloud'));
205 $m->connect('tag/', array('action' => 'publictagcloud'));
206 $m->connect('tags', array('action' => 'publictagcloud'));
207 $m->connect('tag', array('action' => 'publictagcloud'));
208 $m->connect('tag/:tag/rss',
209 array('action' => 'tagrss'),
210 array('tag' => '[a-zA-Z0-9]+'));
211 $m->connect('tag/:tag',
212 array('action' => 'tag'),
213 array('tag' => '[\pL\pN_\-\.]{1,64}'));
215 $m->connect('peopletag/:tag',
216 array('action' => 'peopletag'),
217 array('tag' => '[a-zA-Z0-9]+'));
219 $m->connect('featured/', array('action' => 'featured'));
220 $m->connect('featured', array('action' => 'featured'));
221 $m->connect('favorited/', array('action' => 'favorited'));
222 $m->connect('favorited', array('action' => 'favorited'));
226 $m->connect('group/new', array('action' => 'newgroup'));
228 foreach (array('edit', 'join', 'leave') as $v) {
229 $m->connect('group/:nickname/'.$v,
230 array('action' => $v.'group'),
231 array('nickname' => '[a-zA-Z0-9]+'));
234 foreach (array('members', 'logo', 'rss', 'designsettings') as $n) {
235 $m->connect('group/:nickname/'.$n,
236 array('action' => 'group'.$n),
237 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/:method',
266 array('action' => 'api',
267 'apiaction' => 'statuses'),
268 array('method' => '(public_timeline|friends_timeline|user_timeline|update|replies|mentions|show|friends|followers|featured)(\.(atom|rss|xml|json))?'));
270 $m->connect('api/statuses/:method/:argument',
271 array('action' => 'api',
272 'apiaction' => 'statuses'),
273 array('method' => '(|user_timeline|friends_timeline|replies|mentions|show|destroy|friends|followers)'));
277 $m->connect('api/users/:method/:argument',
278 array('action' => 'api',
279 'apiaction' => 'users'),
280 array('method' => 'show(\.(xml|json))?'));
282 $m->connect('api/users/:method',
283 array('action' => 'api',
284 'apiaction' => 'users'),
285 array('method' => 'show(\.(xml|json))?'));
289 foreach (array('xml', 'json') as $e) {
290 $m->connect('api/direct_messages/new.'.$e,
291 array('action' => 'api',
292 'apiaction' => 'direct_messages',
293 'method' => 'create.'.$e));
296 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
297 $m->connect('api/direct_messages.'.$e,
298 array('action' => 'api',
299 'apiaction' => 'direct_messages',
300 'method' => 'direct_messages.'.$e));
303 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
304 $m->connect('api/direct_messages/sent.'.$e,
305 array('action' => 'api',
306 'apiaction' => 'direct_messages',
307 'method' => 'sent.'.$e));
310 $m->connect('api/direct_messages/destroy/:argument',
311 array('action' => 'api',
312 'apiaction' => 'direct_messages'));
316 $m->connect('api/friendships/:method/:argument',
317 array('action' => 'api',
318 'apiaction' => 'friendships'),
319 array('method' => '(create|destroy)'));
321 $m->connect('api/friendships/:method',
322 array('action' => 'api',
323 'apiaction' => 'friendships'),
324 array('method' => '(show|exists)(\.(xml|json))'));
328 $m->connect('api/friends/ids/:argument',
329 array('action' => 'api',
330 'apiaction' => 'statuses',
331 'method' => 'friendsIDs'));
333 foreach (array('xml', 'json') as $e) {
334 $m->connect('api/friends/ids.'.$e,
335 array('action' => 'api',
336 'apiaction' => 'statuses',
337 'method' => 'friendsIDs.'.$e));
340 $m->connect('api/followers/ids/:argument',
341 array('action' => 'api',
342 'apiaction' => 'statuses',
343 'method' => 'followersIDs'));
345 foreach (array('xml', 'json') as $e) {
346 $m->connect('api/followers/ids.'.$e,
347 array('action' => 'api',
348 'apiaction' => 'statuses',
349 'method' => 'followersIDs.'.$e));
354 $m->connect('api/account/:method',
355 array('action' => 'api',
356 'apiaction' => 'account'));
360 $m->connect('api/favorites/:method/:argument',
361 array('action' => 'api',
362 'apiaction' => 'favorites',
363 array('method' => '(create|destroy)')));
365 $m->connect('api/favorites/:argument',
366 array('action' => 'api',
367 'apiaction' => 'favorites',
368 'method' => 'favorites'));
370 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
371 $m->connect('api/favorites.'.$e,
372 array('action' => 'api',
373 'apiaction' => 'favorites',
374 'method' => 'favorites.'.$e));
379 $m->connect('api/notifications/:method/:argument',
380 array('action' => 'api',
381 'apiaction' => 'favorites'));
385 $m->connect('api/blocks/:method/:argument',
386 array('action' => 'api',
387 'apiaction' => 'blocks'));
391 $m->connect('api/help/:method',
392 array('action' => 'api',
393 'apiaction' => 'help'));
397 $m->connect('api/laconica/:method',
398 array('action' => 'api',
399 'apiaction' => 'laconica'));
401 $m->connect('api/laconica/:method',
402 array('action' => 'api',
403 'apiaction' => 'laconica'));
406 $m->connect('api/laconica/groups/:method/:argument',
407 array('action' => 'api',
408 'apiaction' => 'groups'));
410 $m->connect('api/laconica/groups/:method',
411 array('action' => 'api',
412 'apiaction' => 'groups'));
415 $m->connect('api/laconica/tags/:method/:argument',
416 array('action' => 'api',
417 'apiaction' => 'tags'));
419 $m->connect('api/laconica/tags/:method',
420 array('action' => 'api',
421 'apiaction' => 'tags'));
424 $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
425 $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
426 $m->connect('api/trends.json', array('action' => 'twitapitrends'));
430 foreach (array('subscriptions', 'subscribers',
431 'nudge', 'xrds', 'all', 'foaf',
432 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
433 $m->connect(':nickname/'.$a,
434 array('action' => $a),
435 array('nickname' => '[a-zA-Z0-9]{1,64}'));
438 foreach (array('subscriptions', 'subscribers') as $a) {
439 $m->connect(':nickname/'.$a.'/:tag',
440 array('action' => $a),
441 array('tag' => '[a-zA-Z0-9]+',
442 'nickname' => '[a-zA-Z0-9]{1,64}'));
445 foreach (array('rss', 'groups') as $a) {
446 $m->connect(':nickname/'.$a,
447 array('action' => 'user'.$a),
448 array('nickname' => '[a-zA-Z0-9]{1,64}'));
451 foreach (array('all', 'replies', 'favorites') as $a) {
452 $m->connect(':nickname/'.$a.'/rss',
453 array('action' => $a.'rss'),
454 array('nickname' => '[a-zA-Z0-9]{1,64}'));
457 $m->connect(':nickname/favorites',
458 array('action' => 'showfavorites'),
459 array('nickname' => '[a-zA-Z0-9]{1,64}'));
461 $m->connect(':nickname/avatar/:size',
462 array('action' => 'avatarbynickname'),
463 array('size' => '(original|96|48|24)',
464 'nickname' => '[a-zA-Z0-9]{1,64}'));
466 $m->connect(':nickname/tag/:tag/rss',
467 array('action' => 'userrss'),
468 array('nickname' => '[a-zA-Z0-9]{1,64}'),
469 array('tag' => '[a-zA-Z0-9]+'));
471 $m->connect(':nickname/tag/:tag',
472 array('action' => 'showstream'),
473 array('nickname' => '[a-zA-Z0-9]{1,64}'),
474 array('tag' => '[a-zA-Z0-9]+'));
476 $m->connect(':nickname',
477 array('action' => 'showstream'),
478 array('nickname' => '[a-zA-Z0-9]{1,64}'));
480 Event::handle('RouterInitialized', array($m));
482 $m->connect('main/:method',
483 array('action' => 'api',
484 'method' => 'oembed(.xml|.json)?',
485 'apiaction' => 'oembed'));
493 $match = $this->m->match($path);
494 } catch (Net_URL_Mapper_InvalidException $e) {
495 common_log(LOG_ERR, "Problem getting route for $path - " .
497 $cac = new ClientErrorAction("Page not found.", 404);
504 function build($action, $args=null, $params=null, $fragment=null)
506 $action_arg = array('action' => $action);
509 $args = array_merge($action_arg, $args);
514 $url = $this->m->generate($args, $params, $fragment);
516 // Due to a bug in the Net_URL_Mapper code, the returned URL may
517 // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
518 // repair that here rather than modifying the upstream code...
520 $qpos = strpos($url, '?');
521 if ($qpos !== false) {
522 $url = substr($url, 0, $qpos+1) .
523 str_replace('?', '&', substr($url, $qpos+1));