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',
106 foreach ($main as $a) {
107 $m->connect('main/'.$a, array('action' => $a));
110 $m->connect('main/sup/:seconds', array('action' => 'sup'),
111 array('seconds' => '[0-9]+'));
113 $m->connect('main/tagother/:id', array('action' => 'tagother'));
117 foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
118 $m->connect('main/'.$c.'/:code', array('action' => $c));
123 $m->connect('main/openid', array('action' => 'openidlogin'));
124 $m->connect('main/remote', array('action' => 'remotesubscribe'));
125 $m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '[A-Za-z0-9_-]+'));
127 foreach (Router::$bare as $action) {
128 $m->connect('index.php?action=' . $action, array('action' => $action));
133 foreach (array('profile', 'avatar', 'password', 'openid', 'im',
134 'email', 'sms', 'twitter', 'design', 'other') as $s) {
135 $m->connect('settings/'.$s, array('action' => $s.'settings'));
140 foreach (array('group', 'people', 'notice') as $s) {
141 $m->connect('search/'.$s, array('action' => $s.'search'));
142 $m->connect('search/'.$s.'?q=:q',
143 array('action' => $s.'search'),
147 // The second of these is needed to make the link work correctly
148 // when inserted into the page. The first is needed to match the
149 // route on the way in. Seems to be another Net_URL_Mapper bug to me.
150 $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
151 $m->connect('search/notice/rss?q=:q', array('action' => 'noticesearchrss'),
155 $m->connect('attachment/ajax_by_url/*url',
156 array('action' => 'attachment_ajax'));
158 $m->connect('attachment/:attachment/ajax',
159 array('action' => 'attachment_ajax'),
160 array('attachment' => '[0-9]+'));
164 not used right now, will revisit later
165 $m->connect('attachment/:attachment',
166 array('action' => 'attachment'),
167 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/attachments/ajax',
177 array('action' => 'attachments_ajax'),
178 array('notice' => '[0-9]+'));
179 $m->connect('notice/:notice/attachments',
180 array('action' => 'attachments'),
181 array('notice' => '[0-9]+'));
183 $m->connect('notice/:notice',
184 array('action' => 'shownotice'),
185 array('notice' => '[0-9]+'));
186 $m->connect('notice/delete', array('action' => 'deletenotice'));
187 $m->connect('notice/delete/:notice',
188 array('action' => 'deletenotice'),
189 array('notice' => '[0-9]+'));
193 $m->connect('conversation/:id',
194 array('action' => 'conversation'),
195 array('id' => '[0-9]+'));
197 $m->connect('message/new', array('action' => 'newmessage'));
198 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
199 $m->connect('message/:message',
200 array('action' => 'showmessage'),
201 array('message' => '[0-9]+'));
203 $m->connect('user/:id',
204 array('action' => 'userbyid'),
205 array('id' => '[0-9]+'));
207 $m->connect('tags/', array('action' => 'publictagcloud'));
208 $m->connect('tag/', array('action' => 'publictagcloud'));
209 $m->connect('tags', array('action' => 'publictagcloud'));
210 $m->connect('tag', array('action' => 'publictagcloud'));
211 $m->connect('tag/:tag/rss',
212 array('action' => 'tagrss'),
213 array('tag' => '[a-zA-Z0-9]+'));
214 $m->connect('tag/:tag',
215 array('action' => 'tag'),
216 array('tag' => '[a-zA-Z0-9]+'));
218 $m->connect('peopletag/:tag',
219 array('action' => 'peopletag'),
220 array('tag' => '[a-zA-Z0-9]+'));
222 $m->connect('featured/', array('action' => 'featured'));
223 $m->connect('featured', array('action' => 'featured'));
224 $m->connect('favorited/', array('action' => 'favorited'));
225 $m->connect('favorited', array('action' => 'favorited'));
229 $m->connect('group/new', array('action' => 'newgroup'));
231 foreach (array('edit', 'join', 'leave') as $v) {
232 $m->connect('group/:nickname/'.$v,
233 array('action' => $v.'group'),
234 array('nickname' => '[a-zA-Z0-9]+'));
237 foreach (array('members', 'logo', 'rss') as $n) {
238 $m->connect('group/:nickname/'.$n,
239 array('action' => 'group'.$n),
240 array('nickname' => '[a-zA-Z0-9]+'));
243 $m->connect('group/:id/id',
244 array('action' => 'groupbyid'),
245 array('id' => '[0-9]+'));
247 $m->connect('group/:nickname',
248 array('action' => 'showgroup'),
249 array('nickname' => '[a-zA-Z0-9]+'));
251 $m->connect('group/', array('action' => 'groups'));
252 $m->connect('group', array('action' => 'groups'));
253 $m->connect('groups/', array('action' => 'groups'));
254 $m->connect('groups', array('action' => 'groups'));
256 // Twitter-compatible API
260 $m->connect('api/statuses/:method',
261 array('action' => 'api',
262 'apiaction' => 'statuses'),
263 array('method' => '(public_timeline|friends_timeline|user_timeline|update|replies|mentions|friends|followers|featured)(\.(atom|rss|xml|json))?'));
265 $m->connect('api/statuses/:method/:argument',
266 array('action' => 'api',
267 'apiaction' => 'statuses'),
268 array('method' => '(user_timeline|friends_timeline|replies|mentions|show|destroy|friends|followers)'));
272 $m->connect('api/users/:method/:argument',
273 array('action' => 'api',
274 'apiaction' => 'users'),
275 array('method' => 'show(\.(xml|json))?'));
277 $m->connect('api/users/:method',
278 array('action' => 'api',
279 'apiaction' => 'users'),
280 array('method' => 'show(\.(xml|json))?'));
284 foreach (array('xml', 'json') as $e) {
285 $m->connect('api/direct_messages/new.'.$e,
286 array('action' => 'api',
287 'apiaction' => 'direct_messages',
288 'method' => 'create.'.$e));
291 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
292 $m->connect('api/direct_messages.'.$e,
293 array('action' => 'api',
294 'apiaction' => 'direct_messages',
295 'method' => 'direct_messages.'.$e));
298 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
299 $m->connect('api/direct_messages/sent.'.$e,
300 array('action' => 'api',
301 'apiaction' => 'direct_messages',
302 'method' => 'sent.'.$e));
305 $m->connect('api/direct_messages/destroy/:argument',
306 array('action' => 'api',
307 'apiaction' => 'direct_messages'));
311 $m->connect('api/friendships/:method/:argument',
312 array('action' => 'api',
313 'apiaction' => 'friendships'),
314 array('method' => '(create|destroy)'));
316 $m->connect('api/friendships/:method',
317 array('action' => 'api',
318 'apiaction' => 'friendships'),
319 array('method' => 'exists(\.(xml|json))'));
323 $m->connect('api/friends/ids/:argument',
324 array('action' => 'api',
325 'apiaction' => 'statuses',
326 'method' => 'friendsIDs'));
328 foreach (array('xml', 'json') as $e) {
329 $m->connect('api/friends/ids.'.$e,
330 array('action' => 'api',
331 'apiaction' => 'statuses',
332 'method' => 'friendsIDs.'.$e));
335 $m->connect('api/followers/ids/:argument',
336 array('action' => 'api',
337 'apiaction' => 'statuses',
338 'method' => 'followersIDs'));
340 foreach (array('xml', 'json') as $e) {
341 $m->connect('api/followers/ids.'.$e,
342 array('action' => 'api',
343 'apiaction' => 'statuses',
344 'method' => 'followersIDs.'.$e));
349 $m->connect('api/account/:method',
350 array('action' => 'api',
351 'apiaction' => 'account'));
355 $m->connect('api/favorites/:method/:argument',
356 array('action' => 'api',
357 'apiaction' => 'favorites'));
359 $m->connect('api/favorites/:argument',
360 array('action' => 'api',
361 'apiaction' => 'favorites',
362 'method' => 'favorites'));
364 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
365 $m->connect('api/favorites.'.$e,
366 array('action' => 'api',
367 'apiaction' => 'favorites',
368 'method' => 'favorites.'.$e));
373 $m->connect('api/notifications/:method/:argument',
374 array('action' => 'api',
375 'apiaction' => 'favorites'));
379 $m->connect('api/blocks/:method/:argument',
380 array('action' => 'api',
381 'apiaction' => 'blocks'));
385 $m->connect('api/help/:method',
386 array('action' => 'api',
387 'apiaction' => 'help'));
391 $m->connect('api/laconica/:method',
392 array('action' => 'api',
393 'apiaction' => 'laconica'));
396 $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
397 $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
398 $m->connect('api/trends.json', array('action' => 'twitapitrends'));
402 foreach (array('subscriptions', 'subscribers',
403 'nudge', 'xrds', 'all', 'foaf',
404 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
405 $m->connect(':nickname/'.$a,
406 array('action' => $a),
407 array('nickname' => '[a-zA-Z0-9]{1,64}'));
410 foreach (array('subscriptions', 'subscribers') as $a) {
411 $m->connect(':nickname/'.$a.'/:tag',
412 array('action' => $a),
413 array('tag' => '[a-zA-Z0-9]+',
414 'nickname' => '[a-zA-Z0-9]{1,64}'));
417 foreach (array('rss', 'groups') as $a) {
418 $m->connect(':nickname/'.$a,
419 array('action' => 'user'.$a),
420 array('nickname' => '[a-zA-Z0-9]{1,64}'));
423 foreach (array('all', 'replies', 'favorites') as $a) {
424 $m->connect(':nickname/'.$a.'/rss',
425 array('action' => $a.'rss'),
426 array('nickname' => '[a-zA-Z0-9]{1,64}'));
429 $m->connect(':nickname/favorites',
430 array('action' => 'showfavorites'),
431 array('nickname' => '[a-zA-Z0-9]{1,64}'));
433 $m->connect(':nickname/avatar/:size',
434 array('action' => 'avatarbynickname'),
435 array('size' => '(original|96|48|24)',
436 'nickname' => '[a-zA-Z0-9]{1,64}'));
438 $m->connect(':nickname/tag/:tag/rss',
439 array('action' => 'userrss'),
440 array('nickname' => '[a-zA-Z0-9]{1,64}'),
441 array('tag' => '[a-zA-Z0-9]+'));
443 $m->connect(':nickname/tag/:tag',
444 array('action' => 'showstream'),
445 array('nickname' => '[a-zA-Z0-9]{1,64}'),
446 array('tag' => '[a-zA-Z0-9]+'));
448 $m->connect(':nickname',
449 array('action' => 'showstream'),
450 array('nickname' => '[a-zA-Z0-9]{1,64}'));
452 Event::handle('RouterInitialized', array($m));
460 $match = $this->m->match($path);
461 } catch (Net_URL_Mapper_InvalidException $e) {
462 common_log(LOG_ERR, "Problem getting route for $path - " .
464 $cac = new ClientErrorAction("Page not found.", 404);
471 function build($action, $args=null, $params=null, $fragment=null)
473 $action_arg = array('action' => $action);
476 $args = array_merge($action_arg, $args);
481 $url = $this->m->generate($args, $params, $fragment);
483 // Due to a bug in the Net_URL_Mapper code, the returned URL may
484 // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
485 // repair that here rather than modifying the upstream code...
487 $qpos = strpos($url, '?');
488 if ($qpos !== false) {
489 $url = substr($url, 0, $qpos+1) .
490 str_replace('?', '&', substr($url, $qpos+1));