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'));
119 $m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '[A-Za-z0-9_-]+'));
121 foreach (array('requesttoken', 'accesstoken', 'userauthorization',
122 'postnotice', 'updateprofile', 'finishremotesubscribe') as $action) {
123 $m->connect('index.php?action=' . $action, array('action' => $action));
128 foreach (array('profile', 'avatar', 'password', 'openid', 'im',
129 'email', 'sms', 'twitter', 'other') as $s) {
130 $m->connect('settings/'.$s, array('action' => $s.'settings'));
135 foreach (array('group', 'people', 'notice') as $s) {
136 $m->connect('search/'.$s, array('action' => $s.'search'));
137 $m->connect('search/'.$s.'?q=:q', array('action' => $s.'search'), array('q' => '.+'));
140 $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
144 $m->connect('notice/new', array('action' => 'newnotice'));
145 $m->connect('notice/new?replyto=:replyto',
146 array('action' => 'newnotice'),
147 array('replyto' => '[A-Za-z0-9_-]+'));
148 $m->connect('notice/:notice',
149 array('action' => 'shownotice'),
150 array('notice' => '[0-9]+'));
151 $m->connect('notice/delete', array('action' => 'deletenotice'));
152 $m->connect('notice/delete/:notice',
153 array('action' => 'deletenotice'),
154 array('notice' => '[0-9]+'));
156 $m->connect('message/new', array('action' => 'newmessage'));
157 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]'));
158 $m->connect('message/:message',
159 array('action' => 'showmessage'),
160 array('message' => '[0-9]+'));
162 $m->connect('user/:id',
163 array('action' => 'userbyid'),
164 array('id' => '[0-9]+'));
166 $m->connect('tags/', array('action' => 'publictagcloud'));
167 $m->connect('tag/', array('action' => 'publictagcloud'));
168 $m->connect('tags', array('action' => 'publictagcloud'));
169 $m->connect('tag', array('action' => 'publictagcloud'));
170 $m->connect('tag/:tag/rss',
171 array('action' => 'tagrss'),
172 array('tag' => '[a-zA-Z0-9]+'));
173 $m->connect('tag/:tag',
174 array('action' => 'tag'),
175 array('tag' => '[a-zA-Z0-9]+'));
177 $m->connect('peopletag/:tag',
178 array('action' => 'peopletag'),
179 array('tag' => '[a-zA-Z0-9]+'));
181 $m->connect('featured/', array('action' => 'featured'));
182 $m->connect('featured', array('action' => 'featured'));
183 $m->connect('favorited/', array('action' => 'favorited'));
184 $m->connect('favorited', array('action' => 'favorited'));
188 $m->connect('group/new', array('action' => 'newgroup'));
190 foreach (array('edit', 'join', 'leave') as $v) {
191 $m->connect('group/:nickname/'.$v,
192 array('action' => $v.'group'),
193 array('nickname' => '[a-zA-Z0-9]+'));
196 foreach (array('members', 'logo', 'rss') as $n) {
197 $m->connect('group/:nickname/'.$n,
198 array('action' => 'group'.$n),
199 array('nickname' => '[a-zA-Z0-9]+'));
202 $m->connect('group/:id/id',
203 array('action' => 'groupbyid'),
204 array('id' => '[0-9]+'));
206 $m->connect('group/:nickname',
207 array('action' => 'showgroup'),
208 array('nickname' => '[a-zA-Z0-9]+'));
210 $m->connect('group/', array('action' => 'groups'));
211 $m->connect('group', array('action' => 'groups'));
212 $m->connect('groups/', array('action' => 'groups'));
213 $m->connect('groups', array('action' => 'groups'));
215 // Twitter-compatible API
219 $m->connect('api/statuses/:method',
220 array('action' => 'api',
221 'apiaction' => 'statuses'),
222 array('method' => '(public_timeline|friends_timeline|user_timeline|update|replies|friends|followers|featured)(\.(atom|rss|xml|json))?'));
224 $m->connect('api/statuses/:method/:argument',
225 array('action' => 'api',
226 'apiaction' => 'statuses'),
227 array('method' => '(user_timeline|friends_timeline|show|destroy|friends|followers)'));
231 $m->connect('api/users/:method/:argument',
232 array('action' => 'api',
233 'apiaction' => 'users'),
234 array('method' => 'show(\.(xml|json))?'));
236 $m->connect('api/users/:method',
237 array('action' => 'api',
238 'apiaction' => 'users'),
239 array('method' => 'show(\.(xml|json))?'));
243 foreach (array('xml', 'json') as $e) {
244 $m->connect('api/direct_messages/new.'.$e,
245 array('action' => 'api',
246 'apiaction' => 'direct_messages',
247 'method' => 'create.'.$e));
250 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
251 $m->connect('api/direct_messages.'.$e,
252 array('action' => 'api',
253 'apiaction' => 'direct_messages',
254 'method' => 'direct_messages.'.$e));
257 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
258 $m->connect('api/direct_message/sent.'.$e,
259 array('action' => 'api',
260 'apiaction' => 'direct_messages',
261 'method' => 'sent.'.$e));
264 $m->connect('api/direct_messages/destroy/:argument',
265 array('action' => 'api',
266 'apiaction' => 'direct_messages'));
270 $m->connect('api/friendships/:method/:argument',
271 array('action' => 'api',
272 'apiaction' => 'friendships'),
273 array('method' => '(create|destroy)'));
275 $m->connect('api/friendships/:method',
276 array('action' => 'api',
277 'apiaction' => 'friendships'),
278 array('method' => 'exists(\.(xml|json|rss|atom))'));
283 $m->connect('api/friends/ids/:argument',
284 array('action' => 'api',
285 'apiaction' => 'statuses',
286 'method' => 'friendsIDs'));
288 foreach (array('xml', 'json') as $e) {
289 $m->connect('api/friends/ids.'.$e,
290 array('action' => 'api',
291 'apiaction' => 'statuses',
292 'method' => 'friendsIDs.'.$e));
295 $m->connect('api/followers/ids/:argument',
296 array('action' => 'api',
297 'apiaction' => 'statuses',
298 'method' => 'followersIDs'));
300 foreach (array('xml', 'json') as $e) {
301 $m->connect('api/followers/ids.'.$e,
302 array('action' => 'api',
303 'apiaction' => 'statuses',
304 'method' => 'followersIDs.'.$e));
309 $m->connect('api/account/:method',
310 array('action' => 'api',
311 'apiaction' => 'account'));
315 $m->connect('api/favorites/:method/:argument',
316 array('action' => 'api',
317 'apiaction' => 'favorites'));
319 $m->connect('api/favorites/:argument',
320 array('action' => 'api',
321 'apiaction' => 'favorites',
322 'method' => 'favorites'));
324 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
325 $m->connect('api/favorites.'.$e,
326 array('action' => 'api',
327 'apiaction' => 'favorites',
328 'method' => 'favorites.'.$e));
333 $m->connect('api/notifications/:method/:argument',
334 array('action' => 'api',
335 'apiaction' => 'favorites'));
339 $m->connect('api/blocks/:method/:argument',
340 array('action' => 'api',
341 'apiaction' => 'blocks'));
345 $m->connect('api/help/:method',
346 array('action' => 'api',
347 'apiaction' => 'help'));
351 $m->connect('api/laconica/:method',
352 array('action' => 'api',
353 'apiaction' => 'laconica'));
358 foreach (array('json', 'atom') as $e) {
359 $m->connect('api/search.'.$e,
360 array('action' => 'twitapisearch'));
363 $m->connect('api/trends.json', array('action' => 'twitapitrends'));
367 foreach (array('subscriptions', 'subscribers',
368 'nudge', 'xrds', 'all', 'foaf',
369 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
370 $m->connect(':nickname/'.$a,
371 array('action' => $a),
372 array('nickname' => '[a-zA-Z0-9]{1,64}'));
375 foreach (array('subscriptions', 'subscribers') as $a) {
376 $m->connect(':nickname/'.$a.'/:tag',
377 array('action' => $a),
378 array('tag' => '[a-zA-Z0-9]+',
379 'nickname' => '[a-zA-Z0-9]{1,64}'));
382 foreach (array('rss', 'groups') as $a) {
383 $m->connect(':nickname/'.$a,
384 array('action' => 'user'.$a),
385 array('nickname' => '[a-zA-Z0-9]{1,64}'));
388 foreach (array('all', 'replies', 'favorites') as $a) {
389 $m->connect(':nickname/'.$a.'/rss',
390 array('action' => $a.'rss'),
391 array('nickname' => '[a-zA-Z0-9]{1,64}'));
394 $m->connect(':nickname/favorites',
395 array('action' => 'showfavorites'),
396 array('nickname' => '[a-zA-Z0-9]{1,64}'));
398 $m->connect(':nickname/avatar/:size',
399 array('action' => 'avatarbynickname'),
400 array('size' => '(original|96|48|24)',
401 'nickname' => '[a-zA-Z0-9]{1,64}'));
403 $m->connect(':nickname',
404 array('action' => 'showstream'),
405 array('nickname' => '[a-zA-Z0-9]{1,64}'));
407 Event::handle('RouterInitialized', array($m));
415 $match = $this->m->match($path);
416 } catch (Net_URL_Mapper_InvalidException $e) {
417 common_log(LOG_ERR, "Problem getting route for $path - " .
419 $cac = new ClientErrorAction("Page not found.", 404);
426 function build($action, $args=null, $params=null, $fragment=null)
428 $action_arg = array('action' => $action);
431 $args = array_merge($action_arg, $args);
436 return $this->m->generate($args, $params, $fragment);