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/
56 Router::$inst = new Router();
61 function __construct()
64 $this->m = $this->initialize();
68 function initialize() {
70 $m = Net_URL_Mapper::getInstance();
74 $m->connect('', array('action' => 'public'));
75 $m->connect('rss', array('action' => 'publicrss'));
76 $m->connect('xrds', array('action' => 'publicxrds'));
77 $m->connect('featuredrss', array('action' => 'featuredrss'));
78 $m->connect('favoritedrss', array('action' => 'favoritedrss'));
79 $m->connect('opensearch/people', array('action' => 'opensearch',
81 $m->connect('opensearch/notice', array('action' => 'opensearch',
86 $m->connect('doc/:title', array('action' => 'doc'));
90 $m->connect('facebook', array('action' => 'facebookhome'));
91 $m->connect('facebook/index.php', array('action' => 'facebookhome'));
92 $m->connect('facebook/settings.php', array('action' => 'facebooksettings'));
93 $m->connect('facebook/invite.php', array('action' => 'facebookinvite'));
94 $m->connect('facebook/remove', array('action' => 'facebookremove'));
96 // main stuff is repetitive
98 $main = array('login', 'logout', 'register', 'subscribe',
99 'unsubscribe', 'confirmaddress', 'recoverpassword',
100 'invite', 'favor', 'disfavor', 'sup',
103 foreach ($main as $a) {
104 $m->connect('main/'.$a, array('action' => $a));
107 $m->connect('main/tagother/:id', array('action' => 'tagother'));
111 foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
112 $m->connect('main/'.$c.'/:code', array('action' => $c));
117 $m->connect('main/openid', array('action' => 'openidlogin'));
118 $m->connect('main/remote', array('action' => 'remotesubscribe'));
120 foreach (array('requesttoken', 'accesstoken', 'userauthorization',
121 'postnotice', 'updateprofile', 'finishremotesubscribe') as $action) {
122 $m->connect('index.php?action=' . $action, array('action' => $action));
127 foreach (array('profile', 'avatar', 'password', 'openid', 'im',
128 'email', 'sms', 'twitter', 'other') as $s) {
129 $m->connect('settings/'.$s, array('action' => $s.'settings'));
134 foreach (array('group', 'people', 'notice') as $s) {
135 $m->connect('search/'.$s, array('action' => $s.'search'));
136 $m->connect('search/'.$s.'?q=:q', array('action' => $s.'search'), array('q' => '.+'));
139 $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
143 $m->connect('notice/new', array('action' => 'newnotice'));
144 $m->connect('notice/new?replyto=:replyto',
145 array('action' => 'newnotice'),
146 array('replyto' => '[A-Za-z0-9_-]+'));
147 $m->connect('notice/:notice',
148 array('action' => 'shownotice'),
149 array('notice' => '[0-9]+'));
150 $m->connect('notice/delete', array('action' => 'deletenotice'));
151 $m->connect('notice/delete/:notice',
152 array('action' => 'deletenotice'),
153 array('notice' => '[0-9]+'));
155 $m->connect('message/new', array('action' => 'newmessage'));
156 $m->connect('message/:message',
157 array('action' => 'showmessage'),
158 array('message' => '[0-9]+'));
160 $m->connect('user/:id',
161 array('action' => 'userbyid'),
162 array('id' => '[0-9]+'));
164 $m->connect('tags/', array('action' => 'publictagcloud'));
165 $m->connect('tag/', array('action' => 'publictagcloud'));
166 $m->connect('tags', array('action' => 'publictagcloud'));
167 $m->connect('tag', array('action' => 'publictagcloud'));
168 $m->connect('tag/:tag/rss',
169 array('action' => 'tagrss'),
170 array('tag' => '[a-zA-Z0-9]+'));
171 $m->connect('tag/:tag',
172 array('action' => 'tag'),
173 array('tag' => '[a-zA-Z0-9]+'));
175 $m->connect('peopletag/:tag',
176 array('action' => 'peopletag'),
177 array('tag' => '[a-zA-Z0-9]+'));
179 $m->connect('featured/', array('action' => 'featured'));
180 $m->connect('featured', array('action' => 'featured'));
181 $m->connect('favorited/', array('action' => 'favorited'));
182 $m->connect('favorited', array('action' => 'favorited'));
186 $m->connect('group/new', array('action' => 'newgroup'));
188 foreach (array('edit', 'join', 'leave') as $v) {
189 $m->connect('group/:nickname/'.$v,
190 array('action' => $v.'group'),
191 array('nickname' => '[a-zA-Z0-9]+'));
194 foreach (array('members', 'logo', 'rss') as $n) {
195 $m->connect('group/:nickname/'.$n,
196 array('action' => 'group'.$n),
197 array('nickname' => '[a-zA-Z0-9]+'));
200 $m->connect('group/:id/id',
201 array('action' => 'groupbyid'),
202 array('id' => '[0-9]+'));
204 $m->connect('group/:nickname',
205 array('action' => 'showgroup'),
206 array('nickname' => '[a-zA-Z0-9]+'));
208 $m->connect('group/', array('action' => 'groups'));
209 $m->connect('group', array('action' => 'groups'));
210 $m->connect('groups/', array('action' => 'groups'));
211 $m->connect('groups', array('action' => 'groups'));
213 // Twitter-compatible API
217 $m->connect('api/statuses/:method',
218 array('action' => 'api',
219 'apiaction' => 'statuses'),
220 array('method' => '(public_timeline|friends_timeline|user_timeline|update|replies|friends|followers|featured)(\.(atom|rss|xml|json))?'));
222 $m->connect('api/statuses/:method/:argument',
223 array('action' => 'api',
224 'apiaction' => 'statuses'),
225 array('method' => '(user_timeline|friends_timeline|show|destroy|friends|followers)'));
229 $m->connect('api/users/show/:argument',
230 array('action' => 'api',
231 'apiaction' => 'users'));
233 $m->connect('api/users/:method',
234 array('action' => 'api',
235 'apiaction' => 'users'),
236 array('method' => 'show(\.(xml|json|atom|rss))?'));
240 foreach (array('xml', 'json') as $e) {
241 $m->connect('api/direct_messages/new.'.$e,
242 array('action' => 'api',
243 'apiaction' => 'direct_messages',
244 'method' => 'create.'.$e));
247 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
248 $m->connect('api/direct_messages.'.$e,
249 array('action' => 'api',
250 'apiaction' => 'direct_messages',
251 'method' => 'direct_messages.'.$e));
254 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
255 $m->connect('api/direct_message/sent.'.$e,
256 array('action' => 'api',
257 'apiaction' => 'direct_messages',
258 'method' => 'sent.'.$e));
261 $m->connect('api/direct_messages/destroy/:argument',
262 array('action' => 'api',
263 'apiaction' => 'direct_messages'));
267 $m->connect('api/friendships/:method/:argument',
268 array('action' => 'api',
269 'apiaction' => 'friendships'),
270 array('method' => '(create|destroy)'));
272 $m->connect('api/friendships/:method',
273 array('action' => 'api',
274 'apiaction' => 'friendships'),
275 array('method' => 'exists(\.(xml|json|rss|atom))'));
280 $m->connect('api/friends/ids/:argument',
281 array('action' => 'api',
282 'apiaction' => 'statuses',
283 'method' => 'friendsIDs'));
285 foreach (array('xml', 'json') as $e) {
286 $m->connect('api/friends/ids.'.$e,
287 array('action' => 'api',
288 'apiaction' => 'statuses',
289 'method' => 'friendsIDs.'.$e));
292 $m->connect('api/followers/ids/:argument',
293 array('action' => 'api',
294 'apiaction' => 'statuses',
295 'method' => 'followersIDs'));
297 foreach (array('xml', 'json') as $e) {
298 $m->connect('api/followers/ids.'.$e,
299 array('action' => 'api',
300 'apiaction' => 'statuses',
301 'method' => 'followersIDs.'.$e));
306 $m->connect('api/account/:method',
307 array('action' => 'api',
308 'apiaction' => 'account'));
312 $m->connect('api/favorites/:method/:argument',
313 array('action' => 'api',
314 'apiaction' => 'favorites'));
316 $m->connect('api/favorites/:argument',
317 array('action' => 'api',
318 'apiaction' => 'favorites',
319 'method' => 'favorites'));
321 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
322 $m->connect('api/favorites.'.$e,
323 array('action' => 'api',
324 'apiaction' => 'favorites',
325 'method' => 'favorites.'.$e));
330 $m->connect('api/notifications/:method/:argument',
331 array('action' => 'api',
332 'apiaction' => 'favorites'));
336 $m->connect('api/blocks/:method/:argument',
337 array('action' => 'api',
338 'apiaction' => 'blocks'));
342 $m->connect('api/help/:method',
343 array('action' => 'api',
344 'apiaction' => 'help'));
348 $m->connect('api/laconica/:method',
349 array('action' => 'api',
350 'apiaction' => 'laconica'));
354 foreach (array('subscriptions', 'subscribers',
355 'nudge', 'xrds', 'all', 'foaf',
356 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
357 $m->connect(':nickname/'.$a,
358 array('action' => $a),
359 array('nickname' => '[a-zA-Z0-9]{1,64}'));
362 foreach (array('subscriptions', 'subscribers') as $a) {
363 $m->connect(':nickname/'.$a.'/:tag',
364 array('action' => $a),
365 array('tag' => '[a-zA-Z0-9]+',
366 'nickname' => '[a-zA-Z0-9]{1,64}'));
369 foreach (array('rss', 'groups') as $a) {
370 $m->connect(':nickname/'.$a,
371 array('action' => 'user'.$a),
372 array('nickname' => '[a-zA-Z0-9]{1,64}'));
375 foreach (array('all', 'replies', 'favorites') as $a) {
376 $m->connect(':nickname/'.$a.'/rss',
377 array('action' => $a.'rss'),
378 array('nickname' => '[a-zA-Z0-9]{1,64}'));
381 $m->connect(':nickname/favorites',
382 array('action' => 'showfavorites'),
383 array('nickname' => '[a-zA-Z0-9]{1,64}'));
385 $m->connect(':nickname/avatar/:size',
386 array('action' => 'avatarbynickname'),
387 array('size' => '(original|96|48|24)',
388 'nickname' => '[a-zA-Z0-9]{1,64}'));
390 $m->connect(':nickname',
391 array('action' => 'showstream'),
392 array('nickname' => '[a-zA-Z0-9]{1,64}'));
400 $match = $this->m->match($path);
401 } catch (Net_URL_Mapper_InvalidException $e) {
402 common_log(LOG_ERR, "Problem getting route for $path - " .
404 $cac = new ClientErrorAction("Page not found.", 404);
411 function build($action, $args=null, $params=null, $fragment=null)
413 $action_arg = array('action' => $action);
416 $args = array_merge($action_arg, $args);
421 return $this->m->generate($args, $params, $fragment);