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'),
156 $m->connect('notice/new', array('action' => 'newnotice'));
157 $m->connect('notice/new?replyto=:replyto',
158 array('action' => 'newnotice'),
159 array('replyto' => '[A-Za-z0-9_-]+'));
160 $m->connect('notice/:notice',
161 array('action' => 'shownotice'),
162 array('notice' => '[0-9]+'));
163 $m->connect('notice/delete', array('action' => 'deletenotice'));
164 $m->connect('notice/delete/:notice',
165 array('action' => 'deletenotice'),
166 array('notice' => '[0-9]+'));
170 $m->connect('conversation/:id',
171 array('action' => 'conversation'),
172 array('id' => '[0-9]+'));
174 $m->connect('message/new', array('action' => 'newmessage'));
175 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
176 $m->connect('message/:message',
177 array('action' => 'showmessage'),
178 array('message' => '[0-9]+'));
180 $m->connect('user/:id',
181 array('action' => 'userbyid'),
182 array('id' => '[0-9]+'));
184 $m->connect('tags/', array('action' => 'publictagcloud'));
185 $m->connect('tag/', array('action' => 'publictagcloud'));
186 $m->connect('tags', array('action' => 'publictagcloud'));
187 $m->connect('tag', array('action' => 'publictagcloud'));
188 $m->connect('tag/:tag/rss',
189 array('action' => 'tagrss'),
190 array('tag' => '[a-zA-Z0-9]+'));
191 $m->connect('tag/:tag',
192 array('action' => 'tag'),
193 array('tag' => '[a-zA-Z0-9]+'));
195 $m->connect('peopletag/:tag',
196 array('action' => 'peopletag'),
197 array('tag' => '[a-zA-Z0-9]+'));
199 $m->connect('featured/', array('action' => 'featured'));
200 $m->connect('featured', array('action' => 'featured'));
201 $m->connect('favorited/', array('action' => 'favorited'));
202 $m->connect('favorited', array('action' => 'favorited'));
206 $m->connect('group/new', array('action' => 'newgroup'));
208 foreach (array('edit', 'join', 'leave') as $v) {
209 $m->connect('group/:nickname/'.$v,
210 array('action' => $v.'group'),
211 array('nickname' => '[a-zA-Z0-9]+'));
214 foreach (array('members', 'logo', 'rss') as $n) {
215 $m->connect('group/:nickname/'.$n,
216 array('action' => 'group'.$n),
217 array('nickname' => '[a-zA-Z0-9]+'));
220 $m->connect('group/:id/id',
221 array('action' => 'groupbyid'),
222 array('id' => '[0-9]+'));
224 $m->connect('group/:nickname',
225 array('action' => 'showgroup'),
226 array('nickname' => '[a-zA-Z0-9]+'));
228 $m->connect('group/', array('action' => 'groups'));
229 $m->connect('group', array('action' => 'groups'));
230 $m->connect('groups/', array('action' => 'groups'));
231 $m->connect('groups', array('action' => 'groups'));
233 // Twitter-compatible API
237 $m->connect('api/statuses/:method',
238 array('action' => 'api',
239 'apiaction' => 'statuses'),
240 array('method' => '(public_timeline|friends_timeline|user_timeline|update|replies|friends|followers|featured)(\.(atom|rss|xml|json))?'));
242 $m->connect('api/statuses/:method/:argument',
243 array('action' => 'api',
244 'apiaction' => 'statuses'),
245 array('method' => '(user_timeline|friends_timeline|replies|show|destroy|friends|followers)'));
249 $m->connect('api/users/:method/:argument',
250 array('action' => 'api',
251 'apiaction' => 'users'),
252 array('method' => 'show(\.(xml|json))?'));
254 $m->connect('api/users/:method',
255 array('action' => 'api',
256 'apiaction' => 'users'),
257 array('method' => 'show(\.(xml|json))?'));
261 foreach (array('xml', 'json') as $e) {
262 $m->connect('api/direct_messages/new.'.$e,
263 array('action' => 'api',
264 'apiaction' => 'direct_messages',
265 'method' => 'create.'.$e));
268 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
269 $m->connect('api/direct_messages.'.$e,
270 array('action' => 'api',
271 'apiaction' => 'direct_messages',
272 'method' => 'direct_messages.'.$e));
275 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
276 $m->connect('api/direct_messages/sent.'.$e,
277 array('action' => 'api',
278 'apiaction' => 'direct_messages',
279 'method' => 'sent.'.$e));
282 $m->connect('api/direct_messages/destroy/:argument',
283 array('action' => 'api',
284 'apiaction' => 'direct_messages'));
288 $m->connect('api/friendships/:method/:argument',
289 array('action' => 'api',
290 'apiaction' => 'friendships'),
291 array('method' => '(create|destroy)'));
293 $m->connect('api/friendships/:method',
294 array('action' => 'api',
295 'apiaction' => 'friendships'),
296 array('method' => 'exists(\.(xml|json))'));
300 $m->connect('api/friends/ids/:argument',
301 array('action' => 'api',
302 'apiaction' => 'statuses',
303 'method' => 'friendsIDs'));
305 foreach (array('xml', 'json') as $e) {
306 $m->connect('api/friends/ids.'.$e,
307 array('action' => 'api',
308 'apiaction' => 'statuses',
309 'method' => 'friendsIDs.'.$e));
312 $m->connect('api/followers/ids/:argument',
313 array('action' => 'api',
314 'apiaction' => 'statuses',
315 'method' => 'followersIDs'));
317 foreach (array('xml', 'json') as $e) {
318 $m->connect('api/followers/ids.'.$e,
319 array('action' => 'api',
320 'apiaction' => 'statuses',
321 'method' => 'followersIDs.'.$e));
326 $m->connect('api/account/:method',
327 array('action' => 'api',
328 'apiaction' => 'account'));
332 $m->connect('api/favorites/:method/:argument',
333 array('action' => 'api',
334 'apiaction' => 'favorites'));
336 $m->connect('api/favorites/:argument',
337 array('action' => 'api',
338 'apiaction' => 'favorites',
339 'method' => 'favorites'));
341 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
342 $m->connect('api/favorites.'.$e,
343 array('action' => 'api',
344 'apiaction' => 'favorites',
345 'method' => 'favorites.'.$e));
350 $m->connect('api/notifications/:method/:argument',
351 array('action' => 'api',
352 'apiaction' => 'favorites'));
356 $m->connect('api/blocks/:method/:argument',
357 array('action' => 'api',
358 'apiaction' => 'blocks'));
362 $m->connect('api/help/:method',
363 array('action' => 'api',
364 'apiaction' => 'help'));
368 $m->connect('api/laconica/:method',
369 array('action' => 'api',
370 'apiaction' => 'laconica'));
373 $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
374 $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
375 $m->connect('api/trends.json', array('action' => 'twitapitrends'));
379 foreach (array('subscriptions', 'subscribers',
380 'nudge', 'xrds', 'all', 'foaf',
381 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
382 $m->connect(':nickname/'.$a,
383 array('action' => $a),
384 array('nickname' => '[a-zA-Z0-9]{1,64}'));
387 foreach (array('subscriptions', 'subscribers') as $a) {
388 $m->connect(':nickname/'.$a.'/:tag',
389 array('action' => $a),
390 array('tag' => '[a-zA-Z0-9]+',
391 'nickname' => '[a-zA-Z0-9]{1,64}'));
394 foreach (array('rss', 'groups') as $a) {
395 $m->connect(':nickname/'.$a,
396 array('action' => 'user'.$a),
397 array('nickname' => '[a-zA-Z0-9]{1,64}'));
400 foreach (array('all', 'replies', 'favorites') as $a) {
401 $m->connect(':nickname/'.$a.'/rss',
402 array('action' => $a.'rss'),
403 array('nickname' => '[a-zA-Z0-9]{1,64}'));
406 $m->connect(':nickname/favorites',
407 array('action' => 'showfavorites'),
408 array('nickname' => '[a-zA-Z0-9]{1,64}'));
410 $m->connect(':nickname/avatar/:size',
411 array('action' => 'avatarbynickname'),
412 array('size' => '(original|96|48|24)',
413 'nickname' => '[a-zA-Z0-9]{1,64}'));
415 $m->connect(':nickname',
416 array('action' => 'showstream'),
417 array('nickname' => '[a-zA-Z0-9]{1,64}'));
419 Event::handle('RouterInitialized', array($m));
427 $match = $this->m->match($path);
428 } catch (Net_URL_Mapper_InvalidException $e) {
429 common_log(LOG_ERR, "Problem getting route for $path - " .
431 $cac = new ClientErrorAction("Page not found.", 404);
438 function build($action, $args=null, $params=null, $fragment=null)
440 $action_arg = array('action' => $action);
443 $args = array_merge($action_arg, $args);
448 $url = $this->m->generate($args, $params, $fragment);
450 // Due to a bug in the Net_URL_Mapper code, the returned URL may
451 // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
452 // repair that here rather than modifying the upstream code...
454 $qpos = strpos($url, '?');
455 if ($qpos !== false) {
456 $url = substr($url, 0, $qpos+1) .
457 str_replace('?', '&', substr($url, $qpos+1));