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', 'groupblock');
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'),
154 $m->connect('attachment/:attachment/ajax',
155 array('action' => 'attachment_ajax'),
156 array('attachment' => '[0-9]+'));
158 $m->connect('attachment/:attachment/thumbnail',
159 array('action' => 'attachment_thumbnail'),
160 array('attachment' => '[0-9]+'));
162 $m->connect('notice/new', array('action' => 'newnotice'));
163 $m->connect('notice/new?replyto=:replyto',
164 array('action' => 'newnotice'),
165 array('replyto' => '[A-Za-z0-9_-]+'));
167 $m->connect('notice/:notice/file',
168 array('action' => 'file'),
169 array('notice' => '[0-9]+'));
171 $m->connect('notice/:notice',
172 array('action' => 'shownotice'),
173 array('notice' => '[0-9]+'));
174 $m->connect('notice/delete', array('action' => 'deletenotice'));
175 $m->connect('notice/delete/:notice',
176 array('action' => 'deletenotice'),
177 array('notice' => '[0-9]+'));
181 $m->connect('conversation/:id',
182 array('action' => 'conversation'),
183 array('id' => '[0-9]+'));
185 $m->connect('message/new', array('action' => 'newmessage'));
186 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
187 $m->connect('message/:message',
188 array('action' => 'showmessage'),
189 array('message' => '[0-9]+'));
191 $m->connect('user/:id',
192 array('action' => 'userbyid'),
193 array('id' => '[0-9]+'));
195 $m->connect('tags/', array('action' => 'publictagcloud'));
196 $m->connect('tag/', array('action' => 'publictagcloud'));
197 $m->connect('tags', array('action' => 'publictagcloud'));
198 $m->connect('tag', array('action' => 'publictagcloud'));
199 $m->connect('tag/:tag/rss',
200 array('action' => 'tagrss'),
201 array('tag' => '[a-zA-Z0-9]+'));
202 $m->connect('tag/:tag',
203 array('action' => 'tag'),
204 array('tag' => '[a-zA-Z0-9]+'));
206 $m->connect('peopletag/:tag',
207 array('action' => 'peopletag'),
208 array('tag' => '[a-zA-Z0-9]+'));
210 $m->connect('featured/', array('action' => 'featured'));
211 $m->connect('featured', array('action' => 'featured'));
212 $m->connect('favorited/', array('action' => 'favorited'));
213 $m->connect('favorited', array('action' => 'favorited'));
217 $m->connect('group/new', array('action' => 'newgroup'));
219 foreach (array('edit', 'join', 'leave') as $v) {
220 $m->connect('group/:nickname/'.$v,
221 array('action' => $v.'group'),
222 array('nickname' => '[a-zA-Z0-9]+'));
225 foreach (array('members', 'logo', 'rss') as $n) {
226 $m->connect('group/:nickname/'.$n,
227 array('action' => 'group'.$n),
228 array('nickname' => '[a-zA-Z0-9]+'));
231 $m->connect('group/:id/id',
232 array('action' => 'groupbyid'),
233 array('id' => '[0-9]+'));
235 $m->connect('group/:nickname',
236 array('action' => 'showgroup'),
237 array('nickname' => '[a-zA-Z0-9]+'));
239 $m->connect('group/', array('action' => 'groups'));
240 $m->connect('group', array('action' => 'groups'));
241 $m->connect('groups/', array('action' => 'groups'));
242 $m->connect('groups', array('action' => 'groups'));
244 // Twitter-compatible API
248 $m->connect('api/statuses/:method',
249 array('action' => 'api',
250 'apiaction' => 'statuses'),
251 array('method' => '(public_timeline|friends_timeline|user_timeline|update|replies|mentions|friends|followers|featured)(\.(atom|rss|xml|json))?'));
253 $m->connect('api/statuses/:method/:argument',
254 array('action' => 'api',
255 'apiaction' => 'statuses'),
256 array('method' => '(user_timeline|friends_timeline|replies|mentions|show|destroy|friends|followers)'));
260 $m->connect('api/users/:method/:argument',
261 array('action' => 'api',
262 'apiaction' => 'users'),
263 array('method' => 'show(\.(xml|json))?'));
265 $m->connect('api/users/:method',
266 array('action' => 'api',
267 'apiaction' => 'users'),
268 array('method' => 'show(\.(xml|json))?'));
272 foreach (array('xml', 'json') as $e) {
273 $m->connect('api/direct_messages/new.'.$e,
274 array('action' => 'api',
275 'apiaction' => 'direct_messages',
276 'method' => 'create.'.$e));
279 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
280 $m->connect('api/direct_messages.'.$e,
281 array('action' => 'api',
282 'apiaction' => 'direct_messages',
283 'method' => 'direct_messages.'.$e));
286 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
287 $m->connect('api/direct_messages/sent.'.$e,
288 array('action' => 'api',
289 'apiaction' => 'direct_messages',
290 'method' => 'sent.'.$e));
293 $m->connect('api/direct_messages/destroy/:argument',
294 array('action' => 'api',
295 'apiaction' => 'direct_messages'));
299 $m->connect('api/friendships/:method/:argument',
300 array('action' => 'api',
301 'apiaction' => 'friendships'),
302 array('method' => '(create|destroy)'));
304 $m->connect('api/friendships/:method',
305 array('action' => 'api',
306 'apiaction' => 'friendships'),
307 array('method' => 'exists(\.(xml|json))'));
311 $m->connect('api/friends/ids/:argument',
312 array('action' => 'api',
313 'apiaction' => 'statuses',
314 'method' => 'friendsIDs'));
316 foreach (array('xml', 'json') as $e) {
317 $m->connect('api/friends/ids.'.$e,
318 array('action' => 'api',
319 'apiaction' => 'statuses',
320 'method' => 'friendsIDs.'.$e));
323 $m->connect('api/followers/ids/:argument',
324 array('action' => 'api',
325 'apiaction' => 'statuses',
326 'method' => 'followersIDs'));
328 foreach (array('xml', 'json') as $e) {
329 $m->connect('api/followers/ids.'.$e,
330 array('action' => 'api',
331 'apiaction' => 'statuses',
332 'method' => 'followersIDs.'.$e));
337 $m->connect('api/account/:method',
338 array('action' => 'api',
339 'apiaction' => 'account'));
343 $m->connect('api/favorites/:method/:argument',
344 array('action' => 'api',
345 'apiaction' => 'favorites'));
347 $m->connect('api/favorites/:argument',
348 array('action' => 'api',
349 'apiaction' => 'favorites',
350 'method' => 'favorites'));
352 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
353 $m->connect('api/favorites.'.$e,
354 array('action' => 'api',
355 'apiaction' => 'favorites',
356 'method' => 'favorites.'.$e));
361 $m->connect('api/notifications/:method/:argument',
362 array('action' => 'api',
363 'apiaction' => 'favorites'));
367 $m->connect('api/blocks/:method/:argument',
368 array('action' => 'api',
369 'apiaction' => 'blocks'));
373 $m->connect('api/help/:method',
374 array('action' => 'api',
375 'apiaction' => 'help'));
379 $m->connect('api/laconica/:method',
380 array('action' => 'api',
381 'apiaction' => 'laconica'));
384 $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
385 $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
386 $m->connect('api/trends.json', array('action' => 'twitapitrends'));
390 foreach (array('subscriptions', 'subscribers',
391 'nudge', 'xrds', 'all', 'foaf',
392 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
393 $m->connect(':nickname/'.$a,
394 array('action' => $a),
395 array('nickname' => '[a-zA-Z0-9]{1,64}'));
398 foreach (array('subscriptions', 'subscribers') as $a) {
399 $m->connect(':nickname/'.$a.'/:tag',
400 array('action' => $a),
401 array('tag' => '[a-zA-Z0-9]+',
402 'nickname' => '[a-zA-Z0-9]{1,64}'));
405 foreach (array('rss', 'groups') as $a) {
406 $m->connect(':nickname/'.$a,
407 array('action' => 'user'.$a),
408 array('nickname' => '[a-zA-Z0-9]{1,64}'));
411 foreach (array('all', 'replies', 'favorites') as $a) {
412 $m->connect(':nickname/'.$a.'/rss',
413 array('action' => $a.'rss'),
414 array('nickname' => '[a-zA-Z0-9]{1,64}'));
417 $m->connect(':nickname/favorites',
418 array('action' => 'showfavorites'),
419 array('nickname' => '[a-zA-Z0-9]{1,64}'));
421 $m->connect(':nickname/avatar/:size',
422 array('action' => 'avatarbynickname'),
423 array('size' => '(original|96|48|24)',
424 'nickname' => '[a-zA-Z0-9]{1,64}'));
426 $m->connect(':nickname/tag/:tag/rss',
427 array('action' => 'userrss'),
428 array('nickname' => '[a-zA-Z0-9]{1,64}'),
429 array('tag' => '[a-zA-Z0-9]+'));
431 $m->connect(':nickname/tag/:tag',
432 array('action' => 'showstream'),
433 array('nickname' => '[a-zA-Z0-9]{1,64}'),
434 array('tag' => '[a-zA-Z0-9]+'));
436 $m->connect(':nickname',
437 array('action' => 'showstream'),
438 array('nickname' => '[a-zA-Z0-9]{1,64}'));
440 Event::handle('RouterInitialized', array($m));
448 $match = $this->m->match($path);
449 } catch (Net_URL_Mapper_InvalidException $e) {
450 common_log(LOG_ERR, "Problem getting route for $path - " .
452 $cac = new ClientErrorAction("Page not found.", 404);
459 function build($action, $args=null, $params=null, $fragment=null)
461 $action_arg = array('action' => $action);
464 $args = array_merge($action_arg, $args);
469 $url = $this->m->generate($args, $params, $fragment);
471 // Due to a bug in the Net_URL_Mapper code, the returned URL may
472 // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
473 // repair that here rather than modifying the upstream code...
475 $qpos = strpos($url, '?');
476 if ($qpos !== false) {
477 $url = substr($url, 0, $qpos+1) .
478 str_replace('?', '&', substr($url, $qpos+1));