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/tagother/:id', array('action' => 'tagother'));
114 foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
115 $m->connect('main/'.$c.'/:code', array('action' => $c));
120 $m->connect('main/openid', array('action' => 'openidlogin'));
121 $m->connect('main/remote', array('action' => 'remotesubscribe'));
122 $m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '[A-Za-z0-9_-]+'));
124 foreach (Router::$bare as $action) {
125 $m->connect('index.php?action=' . $action, array('action' => $action));
130 foreach (array('profile', 'avatar', 'password', 'openid', 'im',
131 'email', 'sms', 'twitter', 'other') as $s) {
132 $m->connect('settings/'.$s, array('action' => $s.'settings'));
137 foreach (array('group', 'people', 'notice') as $s) {
138 $m->connect('search/'.$s, array('action' => $s.'search'));
139 $m->connect('search/'.$s.'?q=:q', array('action' => $s.'search'), array('q' => '.+'));
142 $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
146 $m->connect('notice/new', array('action' => 'newnotice'));
147 $m->connect('notice/new?replyto=:replyto',
148 array('action' => 'newnotice'),
149 array('replyto' => '[A-Za-z0-9_-]+'));
150 $m->connect('notice/:notice',
151 array('action' => 'shownotice'),
152 array('notice' => '[0-9]+'));
153 $m->connect('notice/delete', array('action' => 'deletenotice'));
154 $m->connect('notice/delete/:notice',
155 array('action' => 'deletenotice'),
156 array('notice' => '[0-9]+'));
160 $m->connect('conversation/:id',
161 array('action' => 'conversation'),
162 array('id' => '[0-9]+'));
164 $m->connect('message/new', array('action' => 'newmessage'));
165 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]'));
166 $m->connect('message/:message',
167 array('action' => 'showmessage'),
168 array('message' => '[0-9]+'));
170 $m->connect('user/:id',
171 array('action' => 'userbyid'),
172 array('id' => '[0-9]+'));
174 $m->connect('tags/', array('action' => 'publictagcloud'));
175 $m->connect('tag/', array('action' => 'publictagcloud'));
176 $m->connect('tags', array('action' => 'publictagcloud'));
177 $m->connect('tag', array('action' => 'publictagcloud'));
178 $m->connect('tag/:tag/rss',
179 array('action' => 'tagrss'),
180 array('tag' => '[a-zA-Z0-9]+'));
181 $m->connect('tag/:tag',
182 array('action' => 'tag'),
183 array('tag' => '[a-zA-Z0-9]+'));
185 $m->connect('peopletag/:tag',
186 array('action' => 'peopletag'),
187 array('tag' => '[a-zA-Z0-9]+'));
189 $m->connect('featured/', array('action' => 'featured'));
190 $m->connect('featured', array('action' => 'featured'));
191 $m->connect('favorited/', array('action' => 'favorited'));
192 $m->connect('favorited', array('action' => 'favorited'));
196 $m->connect('group/new', array('action' => 'newgroup'));
198 foreach (array('edit', 'join', 'leave') as $v) {
199 $m->connect('group/:nickname/'.$v,
200 array('action' => $v.'group'),
201 array('nickname' => '[a-zA-Z0-9]+'));
204 foreach (array('members', 'logo', 'rss') as $n) {
205 $m->connect('group/:nickname/'.$n,
206 array('action' => 'group'.$n),
207 array('nickname' => '[a-zA-Z0-9]+'));
210 $m->connect('group/:id/id',
211 array('action' => 'groupbyid'),
212 array('id' => '[0-9]+'));
214 $m->connect('group/:nickname',
215 array('action' => 'showgroup'),
216 array('nickname' => '[a-zA-Z0-9]+'));
218 $m->connect('group/', array('action' => 'groups'));
219 $m->connect('group', array('action' => 'groups'));
220 $m->connect('groups/', array('action' => 'groups'));
221 $m->connect('groups', array('action' => 'groups'));
223 // Twitter-compatible API
227 $m->connect('api/statuses/:method',
228 array('action' => 'api',
229 'apiaction' => 'statuses'),
230 array('method' => '(public_timeline|friends_timeline|user_timeline|update|replies|friends|followers|featured)(\.(atom|rss|xml|json))?'));
232 $m->connect('api/statuses/:method/:argument',
233 array('action' => 'api',
234 'apiaction' => 'statuses'),
235 array('method' => '(user_timeline|friends_timeline|replies|show|destroy|friends|followers)'));
239 $m->connect('api/users/:method/:argument',
240 array('action' => 'api',
241 'apiaction' => 'users'),
242 array('method' => 'show(\.(xml|json))?'));
244 $m->connect('api/users/:method',
245 array('action' => 'api',
246 'apiaction' => 'users'),
247 array('method' => 'show(\.(xml|json))?'));
251 foreach (array('xml', 'json') as $e) {
252 $m->connect('api/direct_messages/new.'.$e,
253 array('action' => 'api',
254 'apiaction' => 'direct_messages',
255 'method' => 'create.'.$e));
258 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
259 $m->connect('api/direct_messages.'.$e,
260 array('action' => 'api',
261 'apiaction' => 'direct_messages',
262 'method' => 'direct_messages.'.$e));
265 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
266 $m->connect('api/direct_messages/sent.'.$e,
267 array('action' => 'api',
268 'apiaction' => 'direct_messages',
269 'method' => 'sent.'.$e));
272 $m->connect('api/direct_messages/destroy/:argument',
273 array('action' => 'api',
274 'apiaction' => 'direct_messages'));
278 $m->connect('api/friendships/:method/:argument',
279 array('action' => 'api',
280 'apiaction' => 'friendships'),
281 array('method' => '(create|destroy)'));
283 $m->connect('api/friendships/:method',
284 array('action' => 'api',
285 'apiaction' => 'friendships'),
286 array('method' => 'exists(\.(xml|json))'));
290 $m->connect('api/friends/ids/:argument',
291 array('action' => 'api',
292 'apiaction' => 'statuses',
293 'method' => 'friendsIDs'));
295 foreach (array('xml', 'json') as $e) {
296 $m->connect('api/friends/ids.'.$e,
297 array('action' => 'api',
298 'apiaction' => 'statuses',
299 'method' => 'friendsIDs.'.$e));
302 $m->connect('api/followers/ids/:argument',
303 array('action' => 'api',
304 'apiaction' => 'statuses',
305 'method' => 'followersIDs'));
307 foreach (array('xml', 'json') as $e) {
308 $m->connect('api/followers/ids.'.$e,
309 array('action' => 'api',
310 'apiaction' => 'statuses',
311 'method' => 'followersIDs.'.$e));
316 $m->connect('api/account/:method',
317 array('action' => 'api',
318 'apiaction' => 'account'));
322 $m->connect('api/favorites/:method/:argument',
323 array('action' => 'api',
324 'apiaction' => 'favorites'));
326 $m->connect('api/favorites/:argument',
327 array('action' => 'api',
328 'apiaction' => 'favorites',
329 'method' => 'favorites'));
331 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
332 $m->connect('api/favorites.'.$e,
333 array('action' => 'api',
334 'apiaction' => 'favorites',
335 'method' => 'favorites.'.$e));
340 $m->connect('api/notifications/:method/:argument',
341 array('action' => 'api',
342 'apiaction' => 'favorites'));
346 $m->connect('api/blocks/:method/:argument',
347 array('action' => 'api',
348 'apiaction' => 'blocks'));
352 $m->connect('api/help/:method',
353 array('action' => 'api',
354 'apiaction' => 'help'));
358 $m->connect('api/laconica/:method',
359 array('action' => 'api',
360 'apiaction' => 'laconica'));
363 $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
364 $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
365 $m->connect('api/trends.json', array('action' => 'twitapitrends'));
369 foreach (array('subscriptions', 'subscribers',
370 'nudge', 'xrds', 'all', 'foaf',
371 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
372 $m->connect(':nickname/'.$a,
373 array('action' => $a),
374 array('nickname' => '[a-zA-Z0-9]{1,64}'));
377 foreach (array('subscriptions', 'subscribers') as $a) {
378 $m->connect(':nickname/'.$a.'/:tag',
379 array('action' => $a),
380 array('tag' => '[a-zA-Z0-9]+',
381 'nickname' => '[a-zA-Z0-9]{1,64}'));
384 foreach (array('rss', 'groups') as $a) {
385 $m->connect(':nickname/'.$a,
386 array('action' => 'user'.$a),
387 array('nickname' => '[a-zA-Z0-9]{1,64}'));
390 foreach (array('all', 'replies', 'favorites') as $a) {
391 $m->connect(':nickname/'.$a.'/rss',
392 array('action' => $a.'rss'),
393 array('nickname' => '[a-zA-Z0-9]{1,64}'));
396 $m->connect(':nickname/favorites',
397 array('action' => 'showfavorites'),
398 array('nickname' => '[a-zA-Z0-9]{1,64}'));
400 $m->connect(':nickname/avatar/:size',
401 array('action' => 'avatarbynickname'),
402 array('size' => '(original|96|48|24)',
403 'nickname' => '[a-zA-Z0-9]{1,64}'));
405 $m->connect(':nickname',
406 array('action' => 'showstream'),
407 array('nickname' => '[a-zA-Z0-9]{1,64}'));
409 Event::handle('RouterInitialized', array($m));
417 $match = $this->m->match($path);
418 } catch (Net_URL_Mapper_InvalidException $e) {
419 common_log(LOG_ERR, "Problem getting route for $path - " .
421 $cac = new ClientErrorAction("Page not found.", 404);
428 function build($action, $args=null, $params=null, $fragment=null)
430 $action_arg = array('action' => $action);
433 $args = array_merge($action_arg, $args);
438 return $this->m->generate($args, $params, $fragment);