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();
71 function 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', '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', array('action' => $s.'search'),array('q' => '.+'));
145 // The second of these is needed to make the link work correctly
146 // when inserted into the page. The first is needed to match the
147 // route on the way in. Seems to be another Net_URL_Mapper bug to me.
148 $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
149 $m->connect('search/notice/rss?q=:q', array('action' => 'noticesearchrss'),array('q' => '.+'));
153 $m->connect('notice/new', array('action' => 'newnotice'));
154 $m->connect('notice/new?replyto=:replyto',
155 array('action' => 'newnotice'),
156 array('replyto' => '[A-Za-z0-9_-]+'));
157 $m->connect('notice/:notice',
158 array('action' => 'shownotice'),
159 array('notice' => '[0-9]+'));
160 $m->connect('notice/delete', array('action' => 'deletenotice'));
161 $m->connect('notice/delete/:notice',
162 array('action' => 'deletenotice'),
163 array('notice' => '[0-9]+'));
165 $m->connect('message/new', array('action' => 'newmessage'));
166 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
167 $m->connect('message/:message',
168 array('action' => 'showmessage'),
169 array('message' => '[0-9]+'));
171 $m->connect('user/:id',
172 array('action' => 'userbyid'),
173 array('id' => '[0-9]+'));
175 $m->connect('tags/', array('action' => 'publictagcloud'));
176 $m->connect('tag/', array('action' => 'publictagcloud'));
177 $m->connect('tags', array('action' => 'publictagcloud'));
178 $m->connect('tag', array('action' => 'publictagcloud'));
179 $m->connect('tag/:tag/rss',
180 array('action' => 'tagrss'),
181 array('tag' => '[a-zA-Z0-9]+'));
182 $m->connect('tag/:tag',
183 array('action' => 'tag'),
184 array('tag' => '[a-zA-Z0-9]+'));
186 $m->connect('peopletag/:tag',
187 array('action' => 'peopletag'),
188 array('tag' => '[a-zA-Z0-9]+'));
190 $m->connect('featured/', array('action' => 'featured'));
191 $m->connect('featured', array('action' => 'featured'));
192 $m->connect('favorited/', array('action' => 'favorited'));
193 $m->connect('favorited', array('action' => 'favorited'));
197 $m->connect('group/new', array('action' => 'newgroup'));
199 foreach (array('edit', 'join', 'leave') as $v) {
200 $m->connect('group/:nickname/'.$v,
201 array('action' => $v.'group'),
202 array('nickname' => '[a-zA-Z0-9]+'));
205 foreach (array('members', 'logo', 'rss') as $n) {
206 $m->connect('group/:nickname/'.$n,
207 array('action' => 'group'.$n),
208 array('nickname' => '[a-zA-Z0-9]+'));
211 $m->connect('group/:id/id',
212 array('action' => 'groupbyid'),
213 array('id' => '[0-9]+'));
215 $m->connect('group/:nickname',
216 array('action' => 'showgroup'),
217 array('nickname' => '[a-zA-Z0-9]+'));
219 $m->connect('group/', array('action' => 'groups'));
220 $m->connect('group', array('action' => 'groups'));
221 $m->connect('groups/', array('action' => 'groups'));
222 $m->connect('groups', array('action' => 'groups'));
224 // Twitter-compatible API
228 $m->connect('api/statuses/:method',
229 array('action' => 'api',
230 'apiaction' => 'statuses'),
231 array('method' => '(public_timeline|friends_timeline|user_timeline|update|replies|friends|followers|featured)(\.(atom|rss|xml|json))?'));
233 $m->connect('api/statuses/:method/:argument',
234 array('action' => 'api',
235 'apiaction' => 'statuses'),
236 array('method' => '(user_timeline|friends_timeline|replies|show|destroy|friends|followers)'));
240 $m->connect('api/users/:method/:argument',
241 array('action' => 'api',
242 'apiaction' => 'users'),
243 array('method' => 'show(\.(xml|json))?'));
245 $m->connect('api/users/:method',
246 array('action' => 'api',
247 'apiaction' => 'users'),
248 array('method' => 'show(\.(xml|json))?'));
252 foreach (array('xml', 'json') as $e) {
253 $m->connect('api/direct_messages/new.'.$e,
254 array('action' => 'api',
255 'apiaction' => 'direct_messages',
256 'method' => 'create.'.$e));
259 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
260 $m->connect('api/direct_messages.'.$e,
261 array('action' => 'api',
262 'apiaction' => 'direct_messages',
263 'method' => 'direct_messages.'.$e));
266 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
267 $m->connect('api/direct_messages/sent.'.$e,
268 array('action' => 'api',
269 'apiaction' => 'direct_messages',
270 'method' => 'sent.'.$e));
273 $m->connect('api/direct_messages/destroy/:argument',
274 array('action' => 'api',
275 'apiaction' => 'direct_messages'));
279 $m->connect('api/friendships/:method/:argument',
280 array('action' => 'api',
281 'apiaction' => 'friendships'),
282 array('method' => '(create|destroy)'));
284 $m->connect('api/friendships/:method',
285 array('action' => 'api',
286 'apiaction' => 'friendships'),
287 array('method' => 'exists(\.(xml|json))'));
291 $m->connect('api/friends/ids/:argument',
292 array('action' => 'api',
293 'apiaction' => 'statuses',
294 'method' => 'friendsIDs'));
296 foreach (array('xml', 'json') as $e) {
297 $m->connect('api/friends/ids.'.$e,
298 array('action' => 'api',
299 'apiaction' => 'statuses',
300 'method' => 'friendsIDs.'.$e));
303 $m->connect('api/followers/ids/:argument',
304 array('action' => 'api',
305 'apiaction' => 'statuses',
306 'method' => 'followersIDs'));
308 foreach (array('xml', 'json') as $e) {
309 $m->connect('api/followers/ids.'.$e,
310 array('action' => 'api',
311 'apiaction' => 'statuses',
312 'method' => 'followersIDs.'.$e));
317 $m->connect('api/account/:method',
318 array('action' => 'api',
319 'apiaction' => 'account'));
323 $m->connect('api/favorites/:method/:argument',
324 array('action' => 'api',
325 'apiaction' => 'favorites'));
327 $m->connect('api/favorites/:argument',
328 array('action' => 'api',
329 'apiaction' => 'favorites',
330 'method' => 'favorites'));
332 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
333 $m->connect('api/favorites.'.$e,
334 array('action' => 'api',
335 'apiaction' => 'favorites',
336 'method' => 'favorites.'.$e));
341 $m->connect('api/notifications/:method/:argument',
342 array('action' => 'api',
343 'apiaction' => 'favorites'));
347 $m->connect('api/blocks/:method/:argument',
348 array('action' => 'api',
349 'apiaction' => 'blocks'));
353 $m->connect('api/help/:method',
354 array('action' => 'api',
355 'apiaction' => 'help'));
359 $m->connect('api/laconica/:method',
360 array('action' => 'api',
361 'apiaction' => 'laconica'));
364 $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
365 $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
366 $m->connect('api/trends.json', array('action' => 'twitapitrends'));
370 foreach (array('subscriptions', 'subscribers',
371 'nudge', 'xrds', 'all', 'foaf',
372 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
373 $m->connect(':nickname/'.$a,
374 array('action' => $a),
375 array('nickname' => '[a-zA-Z0-9]{1,64}'));
378 foreach (array('subscriptions', 'subscribers') as $a) {
379 $m->connect(':nickname/'.$a.'/:tag',
380 array('action' => $a),
381 array('tag' => '[a-zA-Z0-9]+',
382 'nickname' => '[a-zA-Z0-9]{1,64}'));
385 foreach (array('rss', 'groups') as $a) {
386 $m->connect(':nickname/'.$a,
387 array('action' => 'user'.$a),
388 array('nickname' => '[a-zA-Z0-9]{1,64}'));
391 foreach (array('all', 'replies', 'favorites') as $a) {
392 $m->connect(':nickname/'.$a.'/rss',
393 array('action' => $a.'rss'),
394 array('nickname' => '[a-zA-Z0-9]{1,64}'));
397 $m->connect(':nickname/favorites',
398 array('action' => 'showfavorites'),
399 array('nickname' => '[a-zA-Z0-9]{1,64}'));
401 $m->connect(':nickname/avatar/:size',
402 array('action' => 'avatarbynickname'),
403 array('size' => '(original|96|48|24)',
404 'nickname' => '[a-zA-Z0-9]{1,64}'));
406 $m->connect(':nickname',
407 array('action' => 'showstream'),
408 array('nickname' => '[a-zA-Z0-9]{1,64}'));
410 Event::handle('RouterInitialized', array($m));
418 $match = $this->m->match($path);
419 } catch (Net_URL_Mapper_InvalidException $e) {
420 common_log(LOG_ERR, "Problem getting route for $path - " .
422 $cac = new ClientErrorAction("Page not found.", 404);
429 function build($action, $args=null, $params=null, $fragment=null)
431 $action_arg = array('action' => $action);
434 $args = array_merge($action_arg, $args);
439 $url = $this->m->generate($args, $params, $fragment);
441 // Due to a bug in the Net_URL_Mapper code, the returned URL may
442 // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
443 // repair that here rather than modifying the upstream code...
444 $qpos = strpos($url,'?');
445 if ($qpos !== false) {
446 $url = substr($url, 0, $qpos+1) .
447 str_replace('?', '&', substr($url, $qpos+1));