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'),
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',
168 array('action' => 'shownotice'),
169 array('notice' => '[0-9]+'));
170 $m->connect('notice/delete', array('action' => 'deletenotice'));
171 $m->connect('notice/delete/:notice',
172 array('action' => 'deletenotice'),
173 array('notice' => '[0-9]+'));
177 $m->connect('conversation/:id',
178 array('action' => 'conversation'),
179 array('id' => '[0-9]+'));
181 $m->connect('message/new', array('action' => 'newmessage'));
182 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
183 $m->connect('message/:message',
184 array('action' => 'showmessage'),
185 array('message' => '[0-9]+'));
187 $m->connect('user/:id',
188 array('action' => 'userbyid'),
189 array('id' => '[0-9]+'));
191 $m->connect('tags/', array('action' => 'publictagcloud'));
192 $m->connect('tag/', array('action' => 'publictagcloud'));
193 $m->connect('tags', array('action' => 'publictagcloud'));
194 $m->connect('tag', array('action' => 'publictagcloud'));
195 $m->connect('tag/:tag/rss',
196 array('action' => 'tagrss'),
197 array('tag' => '[a-zA-Z0-9]+'));
198 $m->connect('tag/:tag',
199 array('action' => 'tag'),
200 array('tag' => '[a-zA-Z0-9]+'));
202 $m->connect('peopletag/:tag',
203 array('action' => 'peopletag'),
204 array('tag' => '[a-zA-Z0-9]+'));
206 $m->connect('featured/', array('action' => 'featured'));
207 $m->connect('featured', array('action' => 'featured'));
208 $m->connect('favorited/', array('action' => 'favorited'));
209 $m->connect('favorited', array('action' => 'favorited'));
213 $m->connect('group/new', array('action' => 'newgroup'));
215 foreach (array('edit', 'join', 'leave') as $v) {
216 $m->connect('group/:nickname/'.$v,
217 array('action' => $v.'group'),
218 array('nickname' => '[a-zA-Z0-9]+'));
221 foreach (array('members', 'logo', 'rss') as $n) {
222 $m->connect('group/:nickname/'.$n,
223 array('action' => 'group'.$n),
224 array('nickname' => '[a-zA-Z0-9]+'));
227 $m->connect('group/:id/id',
228 array('action' => 'groupbyid'),
229 array('id' => '[0-9]+'));
231 $m->connect('group/:nickname',
232 array('action' => 'showgroup'),
233 array('nickname' => '[a-zA-Z0-9]+'));
235 $m->connect('group/', array('action' => 'groups'));
236 $m->connect('group', array('action' => 'groups'));
237 $m->connect('groups/', array('action' => 'groups'));
238 $m->connect('groups', array('action' => 'groups'));
240 // Twitter-compatible API
244 $m->connect('api/statuses/:method',
245 array('action' => 'api',
246 'apiaction' => 'statuses'),
247 array('method' => '(public_timeline|friends_timeline|user_timeline|update|replies|mentions|friends|followers|featured)(\.(atom|rss|xml|json))?'));
249 $m->connect('api/statuses/:method/:argument',
250 array('action' => 'api',
251 'apiaction' => 'statuses'),
252 array('method' => '(user_timeline|friends_timeline|replies|mentions|show|destroy|friends|followers)'));
256 $m->connect('api/users/:method/:argument',
257 array('action' => 'api',
258 'apiaction' => 'users'),
259 array('method' => 'show(\.(xml|json))?'));
261 $m->connect('api/users/:method',
262 array('action' => 'api',
263 'apiaction' => 'users'),
264 array('method' => 'show(\.(xml|json))?'));
268 foreach (array('xml', 'json') as $e) {
269 $m->connect('api/direct_messages/new.'.$e,
270 array('action' => 'api',
271 'apiaction' => 'direct_messages',
272 'method' => 'create.'.$e));
275 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
276 $m->connect('api/direct_messages.'.$e,
277 array('action' => 'api',
278 'apiaction' => 'direct_messages',
279 'method' => 'direct_messages.'.$e));
282 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
283 $m->connect('api/direct_messages/sent.'.$e,
284 array('action' => 'api',
285 'apiaction' => 'direct_messages',
286 'method' => 'sent.'.$e));
289 $m->connect('api/direct_messages/destroy/:argument',
290 array('action' => 'api',
291 'apiaction' => 'direct_messages'));
295 $m->connect('api/friendships/:method/:argument',
296 array('action' => 'api',
297 'apiaction' => 'friendships'),
298 array('method' => '(create|destroy)'));
300 $m->connect('api/friendships/:method',
301 array('action' => 'api',
302 'apiaction' => 'friendships'),
303 array('method' => 'exists(\.(xml|json))'));
307 $m->connect('api/friends/ids/:argument',
308 array('action' => 'api',
309 'apiaction' => 'statuses',
310 'method' => 'friendsIDs'));
312 foreach (array('xml', 'json') as $e) {
313 $m->connect('api/friends/ids.'.$e,
314 array('action' => 'api',
315 'apiaction' => 'statuses',
316 'method' => 'friendsIDs.'.$e));
319 $m->connect('api/followers/ids/:argument',
320 array('action' => 'api',
321 'apiaction' => 'statuses',
322 'method' => 'followersIDs'));
324 foreach (array('xml', 'json') as $e) {
325 $m->connect('api/followers/ids.'.$e,
326 array('action' => 'api',
327 'apiaction' => 'statuses',
328 'method' => 'followersIDs.'.$e));
333 $m->connect('api/account/:method',
334 array('action' => 'api',
335 'apiaction' => 'account'));
339 $m->connect('api/favorites/:method/:argument',
340 array('action' => 'api',
341 'apiaction' => 'favorites'));
343 $m->connect('api/favorites/:argument',
344 array('action' => 'api',
345 'apiaction' => 'favorites',
346 'method' => 'favorites'));
348 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
349 $m->connect('api/favorites.'.$e,
350 array('action' => 'api',
351 'apiaction' => 'favorites',
352 'method' => 'favorites.'.$e));
357 $m->connect('api/notifications/:method/:argument',
358 array('action' => 'api',
359 'apiaction' => 'favorites'));
363 $m->connect('api/blocks/:method/:argument',
364 array('action' => 'api',
365 'apiaction' => 'blocks'));
369 $m->connect('api/help/:method',
370 array('action' => 'api',
371 'apiaction' => 'help'));
375 $m->connect('api/laconica/:method',
376 array('action' => 'api',
377 'apiaction' => 'laconica'));
380 $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
381 $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
382 $m->connect('api/trends.json', array('action' => 'twitapitrends'));
386 foreach (array('subscriptions', 'subscribers',
387 'nudge', 'xrds', 'all', 'foaf',
388 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
389 $m->connect(':nickname/'.$a,
390 array('action' => $a),
391 array('nickname' => '[a-zA-Z0-9]{1,64}'));
394 foreach (array('subscriptions', 'subscribers') as $a) {
395 $m->connect(':nickname/'.$a.'/:tag',
396 array('action' => $a),
397 array('tag' => '[a-zA-Z0-9]+',
398 'nickname' => '[a-zA-Z0-9]{1,64}'));
401 foreach (array('rss', 'groups') as $a) {
402 $m->connect(':nickname/'.$a,
403 array('action' => 'user'.$a),
404 array('nickname' => '[a-zA-Z0-9]{1,64}'));
407 foreach (array('all', 'replies', 'favorites') as $a) {
408 $m->connect(':nickname/'.$a.'/rss',
409 array('action' => $a.'rss'),
410 array('nickname' => '[a-zA-Z0-9]{1,64}'));
413 $m->connect(':nickname/favorites',
414 array('action' => 'showfavorites'),
415 array('nickname' => '[a-zA-Z0-9]{1,64}'));
417 $m->connect(':nickname/avatar/:size',
418 array('action' => 'avatarbynickname'),
419 array('size' => '(original|96|48|24)',
420 'nickname' => '[a-zA-Z0-9]{1,64}'));
422 $m->connect(':nickname/tag/:tag/rss',
423 array('action' => 'userrss'),
424 array('nickname' => '[a-zA-Z0-9]{1,64}'),
425 array('tag' => '[a-zA-Z0-9]+'));
427 $m->connect(':nickname/tag/:tag',
428 array('action' => 'showstream'),
429 array('nickname' => '[a-zA-Z0-9]{1,64}'),
430 array('tag' => '[a-zA-Z0-9]+'));
432 $m->connect(':nickname',
433 array('action' => 'showstream'),
434 array('nickname' => '[a-zA-Z0-9]{1,64}'));
436 Event::handle('RouterInitialized', array($m));
444 $match = $this->m->match($path);
445 } catch (Net_URL_Mapper_InvalidException $e) {
446 common_log(LOG_ERR, "Problem getting route for $path - " .
448 $cac = new ClientErrorAction("Page not found.", 404);
455 function build($action, $args=null, $params=null, $fragment=null)
457 $action_arg = array('action' => $action);
460 $args = array_merge($action_arg, $args);
465 $url = $this->m->generate($args, $params, $fragment);
467 // Due to a bug in the Net_URL_Mapper code, the returned URL may
468 // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
469 // repair that here rather than modifying the upstream code...
471 $qpos = strpos($url, '?');
472 if ($qpos !== false) {
473 $url = substr($url, 0, $qpos+1) .
474 str_replace('?', '&', substr($url, $qpos+1));