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));
134 foreach (array('profile', 'avatar', 'password', 'openid', 'im',
135 'email', 'sms', 'twitter', 'userdesign', 'other') as $s) {
136 $m->connect('settings/'.$s, array('action' => $s.'settings'));
141 foreach (array('group', 'people', 'notice') as $s) {
142 $m->connect('search/'.$s, array('action' => $s.'search'));
143 $m->connect('search/'.$s.'?q=:q',
144 array('action' => $s.'search'),
148 // The second of these is needed to make the link work correctly
149 // when inserted into the page. The first is needed to match the
150 // route on the way in. Seems to be another Net_URL_Mapper bug to me.
151 $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
152 $m->connect('search/notice/rss?q=:q', array('action' => 'noticesearchrss'),
155 $m->connect('attachment/:attachment/ajax',
156 array('action' => 'attachment_ajax'),
157 array('attachment' => '[0-9]+'));
159 $m->connect('attachment/:attachment/thumbnail',
160 array('action' => 'attachment_thumbnail'),
161 array('attachment' => '[0-9]+'));
163 $m->connect('notice/new', array('action' => 'newnotice'));
164 $m->connect('notice/new?replyto=:replyto',
165 array('action' => 'newnotice'),
166 array('replyto' => '[A-Za-z0-9_-]+'));
168 $m->connect('notice/:notice/file',
169 array('action' => 'file'),
170 array('notice' => '[0-9]+'));
172 $m->connect('notice/:notice',
173 array('action' => 'shownotice'),
174 array('notice' => '[0-9]+'));
175 $m->connect('notice/delete', array('action' => 'deletenotice'));
176 $m->connect('notice/delete/:notice',
177 array('action' => 'deletenotice'),
178 array('notice' => '[0-9]+'));
182 $m->connect('conversation/:id',
183 array('action' => 'conversation'),
184 array('id' => '[0-9]+'));
186 $m->connect('message/new', array('action' => 'newmessage'));
187 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
188 $m->connect('message/:message',
189 array('action' => 'showmessage'),
190 array('message' => '[0-9]+'));
192 $m->connect('user/:id',
193 array('action' => 'userbyid'),
194 array('id' => '[0-9]+'));
196 $m->connect('tags/', array('action' => 'publictagcloud'));
197 $m->connect('tag/', array('action' => 'publictagcloud'));
198 $m->connect('tags', array('action' => 'publictagcloud'));
199 $m->connect('tag', array('action' => 'publictagcloud'));
200 $m->connect('tag/:tag/rss',
201 array('action' => 'tagrss'),
202 array('tag' => '[a-zA-Z0-9]+'));
203 $m->connect('tag/:tag',
204 array('action' => 'tag'),
205 array('tag' => '[a-zA-Z0-9]+'));
207 $m->connect('peopletag/:tag',
208 array('action' => 'peopletag'),
209 array('tag' => '[a-zA-Z0-9]+'));
211 $m->connect('featured/', array('action' => 'featured'));
212 $m->connect('featured', array('action' => 'featured'));
213 $m->connect('favorited/', array('action' => 'favorited'));
214 $m->connect('favorited', array('action' => 'favorited'));
218 $m->connect('group/new', array('action' => 'newgroup'));
220 foreach (array('edit', 'join', 'leave') as $v) {
221 $m->connect('group/:nickname/'.$v,
222 array('action' => $v.'group'),
223 array('nickname' => '[a-zA-Z0-9]+'));
226 foreach (array('members', 'logo', 'rss', 'designsettings') as $n) {
227 $m->connect('group/:nickname/'.$n,
228 array('action' => 'group'.$n),
229 array('nickname' => '[a-zA-Z0-9]+'));
232 $m->connect('group/:nickname/blocked',
233 array('action' => 'blockedfromgroup'),
234 array('nickname' => '[a-zA-Z0-9]+'));
236 $m->connect('group/:nickname/makeadmin',
237 array('action' => 'makeadmin'),
238 array('nickname' => '[a-zA-Z0-9]+'));
240 $m->connect('group/:id/id',
241 array('action' => 'groupbyid'),
242 array('id' => '[0-9]+'));
244 $m->connect('group/:nickname',
245 array('action' => 'showgroup'),
246 array('nickname' => '[a-zA-Z0-9]+'));
248 $m->connect('group/', array('action' => 'groups'));
249 $m->connect('group', array('action' => 'groups'));
250 $m->connect('groups/', array('action' => 'groups'));
251 $m->connect('groups', array('action' => 'groups'));
253 // Twitter-compatible API
257 $m->connect('api/statuses/:method',
258 array('action' => 'api',
259 'apiaction' => 'statuses'),
260 array('method' => '(public_timeline|friends_timeline|user_timeline|update|replies|mentions|friends|followers|featured)(\.(atom|rss|xml|json))?'));
262 $m->connect('api/statuses/:method/:argument',
263 array('action' => 'api',
264 'apiaction' => 'statuses'),
265 array('method' => '(user_timeline|friends_timeline|replies|mentions|show|destroy|friends|followers)'));
269 $m->connect('api/users/:method/:argument',
270 array('action' => 'api',
271 'apiaction' => 'users'),
272 array('method' => 'show(\.(xml|json))?'));
274 $m->connect('api/users/:method',
275 array('action' => 'api',
276 'apiaction' => 'users'),
277 array('method' => 'show(\.(xml|json))?'));
281 foreach (array('xml', 'json') as $e) {
282 $m->connect('api/direct_messages/new.'.$e,
283 array('action' => 'api',
284 'apiaction' => 'direct_messages',
285 'method' => 'create.'.$e));
288 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
289 $m->connect('api/direct_messages.'.$e,
290 array('action' => 'api',
291 'apiaction' => 'direct_messages',
292 'method' => 'direct_messages.'.$e));
295 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
296 $m->connect('api/direct_messages/sent.'.$e,
297 array('action' => 'api',
298 'apiaction' => 'direct_messages',
299 'method' => 'sent.'.$e));
302 $m->connect('api/direct_messages/destroy/:argument',
303 array('action' => 'api',
304 'apiaction' => 'direct_messages'));
308 $m->connect('api/friendships/:method/:argument',
309 array('action' => 'api',
310 'apiaction' => 'friendships'),
311 array('method' => '(create|destroy)'));
313 $m->connect('api/friendships/:method',
314 array('action' => 'api',
315 'apiaction' => 'friendships'),
316 array('method' => 'exists(\.(xml|json))'));
320 $m->connect('api/friends/ids/:argument',
321 array('action' => 'api',
322 'apiaction' => 'statuses',
323 'method' => 'friendsIDs'));
325 foreach (array('xml', 'json') as $e) {
326 $m->connect('api/friends/ids.'.$e,
327 array('action' => 'api',
328 'apiaction' => 'statuses',
329 'method' => 'friendsIDs.'.$e));
332 $m->connect('api/followers/ids/:argument',
333 array('action' => 'api',
334 'apiaction' => 'statuses',
335 'method' => 'followersIDs'));
337 foreach (array('xml', 'json') as $e) {
338 $m->connect('api/followers/ids.'.$e,
339 array('action' => 'api',
340 'apiaction' => 'statuses',
341 'method' => 'followersIDs.'.$e));
346 $m->connect('api/account/:method',
347 array('action' => 'api',
348 'apiaction' => 'account'));
352 $m->connect('api/favorites/:method/:argument',
353 array('action' => 'api',
354 'apiaction' => 'favorites',
355 array('method' => '(create|destroy)')));
357 $m->connect('api/favorites/:argument',
358 array('action' => 'api',
359 'apiaction' => 'favorites',
360 'method' => 'favorites'));
362 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
363 $m->connect('api/favorites.'.$e,
364 array('action' => 'api',
365 'apiaction' => 'favorites',
366 'method' => 'favorites.'.$e));
371 $m->connect('api/notifications/:method/:argument',
372 array('action' => 'api',
373 'apiaction' => 'favorites'));
377 $m->connect('api/blocks/:method/:argument',
378 array('action' => 'api',
379 'apiaction' => 'blocks'));
383 $m->connect('api/help/:method',
384 array('action' => 'api',
385 'apiaction' => 'help'));
389 $m->connect('api/laconica/:method',
390 array('action' => 'api',
391 'apiaction' => 'laconica'));
394 $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
395 $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
396 $m->connect('api/trends.json', array('action' => 'twitapitrends'));
400 foreach (array('subscriptions', 'subscribers',
401 'nudge', 'xrds', 'all', 'foaf',
402 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
403 $m->connect(':nickname/'.$a,
404 array('action' => $a),
405 array('nickname' => '[a-zA-Z0-9]{1,64}'));
408 foreach (array('subscriptions', 'subscribers') as $a) {
409 $m->connect(':nickname/'.$a.'/:tag',
410 array('action' => $a),
411 array('tag' => '[a-zA-Z0-9]+',
412 'nickname' => '[a-zA-Z0-9]{1,64}'));
415 foreach (array('rss', 'groups') as $a) {
416 $m->connect(':nickname/'.$a,
417 array('action' => 'user'.$a),
418 array('nickname' => '[a-zA-Z0-9]{1,64}'));
421 foreach (array('all', 'replies', 'favorites') as $a) {
422 $m->connect(':nickname/'.$a.'/rss',
423 array('action' => $a.'rss'),
424 array('nickname' => '[a-zA-Z0-9]{1,64}'));
427 $m->connect(':nickname/favorites',
428 array('action' => 'showfavorites'),
429 array('nickname' => '[a-zA-Z0-9]{1,64}'));
431 $m->connect(':nickname/avatar/:size',
432 array('action' => 'avatarbynickname'),
433 array('size' => '(original|96|48|24)',
434 'nickname' => '[a-zA-Z0-9]{1,64}'));
436 $m->connect(':nickname/tag/:tag/rss',
437 array('action' => 'userrss'),
438 array('nickname' => '[a-zA-Z0-9]{1,64}'),
439 array('tag' => '[a-zA-Z0-9]+'));
441 $m->connect(':nickname/tag/:tag',
442 array('action' => 'showstream'),
443 array('nickname' => '[a-zA-Z0-9]{1,64}'),
444 array('tag' => '[a-zA-Z0-9]+'));
446 $m->connect(':nickname',
447 array('action' => 'showstream'),
448 array('nickname' => '[a-zA-Z0-9]{1,64}'));
450 Event::handle('RouterInitialized', array($m));
458 $match = $this->m->match($path);
459 } catch (Net_URL_Mapper_InvalidException $e) {
460 common_log(LOG_ERR, "Problem getting route for $path - " .
462 $cac = new ClientErrorAction("Page not found.", 404);
469 function build($action, $args=null, $params=null, $fragment=null)
471 $action_arg = array('action' => $action);
474 $args = array_merge($action_arg, $args);
479 $url = $this->m->generate($args, $params, $fragment);
481 // Due to a bug in the Net_URL_Mapper code, the returned URL may
482 // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
483 // repair that here rather than modifying the upstream code...
485 $qpos = strpos($url, '?');
486 if ($qpos !== false) {
487 $url = substr($url, 0, $qpos+1) .
488 str_replace('?', '&', substr($url, $qpos+1));