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'));
122 foreach (array('profile', 'avatar', 'password', 'openid', 'im',
123 'email', 'sms', 'twitter', 'other') as $s) {
124 $m->connect('settings/'.$s, array('action' => $s.'settings'));
129 foreach (array('group', 'people', 'notice') as $s) {
130 $m->connect('search/'.$s, array('action' => $s.'search'));
133 $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
137 $m->connect('notice/new', array('action' => 'newnotice'));
138 $m->connect('notice/:notice',
139 array('action' => 'shownotice'),
140 array('notice' => '[0-9]+'));
141 $m->connect('notice/delete', array('action' => 'deletenotice'));
142 $m->connect('notice/delete/:notice',
143 array('action' => 'deletenotice'),
144 array('notice' => '[0-9]+'));
148 $m->connect('conversation/:id',
149 array('action' => 'conversation'),
150 array('id' => '[0-9]+'));
152 $m->connect('message/new', array('action' => 'newmessage'));
153 $m->connect('message/:message',
154 array('action' => 'showmessage'),
155 array('message' => '[0-9]+'));
157 $m->connect('user/:id',
158 array('action' => 'userbyid'),
159 array('id' => '[0-9]+'));
161 $m->connect('tags/', array('action' => 'publictagcloud'));
162 $m->connect('tag/', array('action' => 'publictagcloud'));
163 $m->connect('tags', array('action' => 'publictagcloud'));
164 $m->connect('tag', array('action' => 'publictagcloud'));
165 $m->connect('tag/:tag/rss',
166 array('action' => 'tagrss'),
167 array('tag' => '[a-zA-Z0-9]+'));
168 $m->connect('tag/:tag',
169 array('action' => 'tag'),
170 array('tag' => '[a-zA-Z0-9]+'));
172 $m->connect('peopletag/:tag',
173 array('action' => 'peopletag'),
174 array('tag' => '[a-zA-Z0-9]+'));
176 $m->connect('featured/', array('action' => 'featured'));
177 $m->connect('featured', array('action' => 'featured'));
178 $m->connect('favorited/', array('action' => 'favorited'));
179 $m->connect('favorited', array('action' => 'favorited'));
183 $m->connect('group/new', array('action' => 'newgroup'));
185 foreach (array('edit', 'join', 'leave') as $v) {
186 $m->connect('group/:nickname/'.$v,
187 array('action' => $v.'group'),
188 array('nickname' => '[a-zA-Z0-9]+'));
191 foreach (array('members', 'logo', 'rss') as $n) {
192 $m->connect('group/:nickname/'.$n,
193 array('action' => 'group'.$n),
194 array('nickname' => '[a-zA-Z0-9]+'));
197 $m->connect('group/:id/id',
198 array('action' => 'groupbyid'),
199 array('id' => '[0-9]+'));
201 $m->connect('group/:nickname',
202 array('action' => 'showgroup'),
203 array('nickname' => '[a-zA-Z0-9]+'));
205 $m->connect('group/', array('action' => 'groups'));
206 $m->connect('group', array('action' => 'groups'));
207 $m->connect('groups/', array('action' => 'groups'));
208 $m->connect('groups', array('action' => 'groups'));
210 // Twitter-compatible API
214 $m->connect('api/statuses/:method',
215 array('action' => 'api',
216 'apiaction' => 'statuses'),
217 array('method' => '(public_timeline|friends_timeline|user_timeline|update|replies|friends|followers|featured)(\.(atom|rss|xml|json))?'));
219 $m->connect('api/statuses/:method/:argument',
220 array('action' => 'api',
221 'apiaction' => 'statuses'),
222 array('method' => '(user_timeline|friends_timeline|show|destroy|friends|followers)'));
226 $m->connect('api/users/show/:argument',
227 array('action' => 'api',
228 'apiaction' => 'users'));
230 $m->connect('api/users/:method',
231 array('action' => 'api',
232 'apiaction' => 'users'),
233 array('method' => 'show(\.(xml|json|atom|rss))?'));
237 foreach (array('xml', 'json') as $e) {
238 $m->connect('api/direct_messages/new.'.$e,
239 array('action' => 'api',
240 'apiaction' => 'direct_messages',
241 'method' => 'create.'.$e));
244 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
245 $m->connect('api/direct_messages.'.$e,
246 array('action' => 'api',
247 'apiaction' => 'direct_messages',
248 'method' => 'direct_messages.'.$e));
251 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
252 $m->connect('api/direct_message/sent.'.$e,
253 array('action' => 'api',
254 'apiaction' => 'direct_messages',
255 'method' => 'sent.'.$e));
258 $m->connect('api/direct_messages/destroy/:argument',
259 array('action' => 'api',
260 'apiaction' => 'direct_messages'));
264 $m->connect('api/friendships/:method/:argument',
265 array('action' => 'api',
266 'apiaction' => 'friendships'),
267 array('method' => '(create|destroy)'));
269 $m->connect('api/friendships/:method',
270 array('action' => 'api',
271 'apiaction' => 'friendships'),
272 array('method' => 'exists(\.(xml|json|rss|atom))'));
276 $m->connect('api/friends/ids/:argument',
277 array('action' => 'api',
278 'apiaction' => 'statuses',
279 'method' => 'friendsIDs'));
281 foreach (array('xml', 'json') as $e) {
282 $m->connect('api/friends/ids.'.$e,
283 array('action' => 'api',
284 'apiaction' => 'statuses',
285 'method' => 'friendsIDs.'.$e));
288 $m->connect('api/followers/ids/:argument',
289 array('action' => 'api',
290 'apiaction' => 'statuses',
291 'method' => 'followersIDs'));
293 foreach (array('xml', 'json') as $e) {
294 $m->connect('api/followers/ids.'.$e,
295 array('action' => 'api',
296 'apiaction' => 'statuses',
297 'method' => 'followersIDs.'.$e));
302 $m->connect('api/account/:method',
303 array('action' => 'api',
304 'apiaction' => 'account'));
308 $m->connect('api/favorites/:method/:argument',
309 array('action' => 'api',
310 'apiaction' => 'favorites'));
312 $m->connect('api/favorites/:argument',
313 array('action' => 'api',
314 'apiaction' => 'favorites',
315 'method' => 'favorites'));
317 foreach (array('xml', 'json', 'rss', 'atom') as $e) {
318 $m->connect('api/favorites.'.$e,
319 array('action' => 'api',
320 'apiaction' => 'favorites',
321 'method' => 'favorites.'.$e));
326 $m->connect('api/notifications/:method/:argument',
327 array('action' => 'api',
328 'apiaction' => 'favorites'));
332 $m->connect('api/blocks/:method/:argument',
333 array('action' => 'api',
334 'apiaction' => 'blocks'));
338 $m->connect('api/help/:method',
339 array('action' => 'api',
340 'apiaction' => 'help'));
344 $m->connect('api/laconica/:method',
345 array('action' => 'api',
346 'apiaction' => 'laconica'));
350 foreach (array('subscriptions', 'subscribers',
351 'nudge', 'xrds', 'all', 'foaf',
352 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
353 $m->connect(':nickname/'.$a,
354 array('action' => $a),
355 array('nickname' => '[a-zA-Z0-9]{1,64}'));
358 foreach (array('subscriptions', 'subscribers') as $a) {
359 $m->connect(':nickname/'.$a.'/:tag',
360 array('action' => $a),
361 array('tag' => '[a-zA-Z0-9]+',
362 'nickname' => '[a-zA-Z0-9]{1,64}'));
365 foreach (array('rss', 'groups') as $a) {
366 $m->connect(':nickname/'.$a,
367 array('action' => 'user'.$a),
368 array('nickname' => '[a-zA-Z0-9]{1,64}'));
371 foreach (array('all', 'replies', 'favorites') as $a) {
372 $m->connect(':nickname/'.$a.'/rss',
373 array('action' => $a.'rss'),
374 array('nickname' => '[a-zA-Z0-9]{1,64}'));
377 $m->connect(':nickname/favorites',
378 array('action' => 'showfavorites'),
379 array('nickname' => '[a-zA-Z0-9]{1,64}'));
381 $m->connect(':nickname/avatar/:size',
382 array('action' => 'avatarbynickname'),
383 array('size' => '(original|96|48|24)',
384 'nickname' => '[a-zA-Z0-9]{1,64}'));
386 $m->connect(':nickname',
387 array('action' => 'showstream'),
388 array('nickname' => '[a-zA-Z0-9]{1,64}'));
396 $match = $this->m->match($path);
397 } catch (Net_URL_Mapper_InvalidException $e) {
398 common_log(LOG_ERR, "Problem getting route for $path - " .
400 $cac = new ClientErrorAction("Page not found.", 404);
407 function build($action, $args=null, $params=null, $fragment=null)
409 $action_arg = array('action' => $action);
412 $args = array_merge($action_arg, $args);
417 return $this->m->generate($args, $params, $fragment);