]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/router.php
Merge branch '0.8.x' of git@gitorious.org:laconica/dev into 0.8.x
[quix0rs-gnu-social.git] / lib / router.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * URL routing utilities
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  URL
23  * @package   Laconica
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/
28  */
29
30 if (!defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once 'Net/URL/Mapper.php';
35
36 /**
37  * URL Router
38  *
39  * Cheap wrapper around Net_URL_Mapper
40  *
41  * @category URL
42  * @package  Laconica
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/
46  */
47
48 class Router
49 {
50     var $m = null;
51     static $inst = null;
52     static $bare = array('requesttoken', 'accesstoken', 'userauthorization',
53                          'postnotice', 'updateprofile', 'finishremotesubscribe',
54                          'finishopenidlogin', 'finishaddopenid');
55
56     static function get()
57     {
58         if (!Router::$inst) {
59             Router::$inst = new Router();
60         }
61         return Router::$inst;
62     }
63
64     function __construct()
65     {
66         if (!$this->m) {
67             $this->m = $this->initialize();
68         }
69     }
70
71     function initialize()
72     {
73         $m = Net_URL_Mapper::getInstance();
74
75         // In the "root"
76
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',
83                                                'type' => 'people'));
84         $m->connect('opensearch/notice', array('action' => 'opensearch',
85                                                'type' => 'notice'));
86
87         // docs
88
89         $m->connect('doc/:title', array('action' => 'doc'));
90
91         // facebook
92
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'));
98
99         // main stuff is repetitive
100
101         $main = array('login', 'logout', 'register', 'subscribe',
102                       'unsubscribe', 'confirmaddress', 'recoverpassword',
103                       'invite', 'favor', 'disfavor', 'sup',
104                       'block', 'unblock', 'subedit',
105                       'groupblock', 'groupunblock');
106
107         foreach ($main as $a) {
108             $m->connect('main/'.$a, array('action' => $a));
109         }
110
111         $m->connect('main/sup/:seconds', array('action' => 'sup'),
112                     array('seconds' => '[0-9]+'));
113
114         $m->connect('main/tagother/:id', array('action' => 'tagother'));
115
116         // these take a code
117
118         foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
119             $m->connect('main/'.$c.'/:code', array('action' => $c));
120         }
121
122         // exceptional
123
124         $m->connect('main/openid', array('action' => 'openidlogin'));
125         $m->connect('main/remote', array('action' => 'remotesubscribe'));
126         $m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '[A-Za-z0-9_-]+'));
127
128         foreach (Router::$bare as $action) {
129             $m->connect('index.php?action=' . $action, array('action' => $action));
130         }
131
132         // settings
133
134         foreach (array('profile', 'avatar', 'password', 'openid', 'im',
135                        'email', 'sms', 'twitter', 'design', 'other') as $s) {
136             $m->connect('settings/'.$s, array('action' => $s.'settings'));
137         }
138
139         // search
140
141         foreach (array('group', 'people', 'notice') as $s) {
142             $m->connect('search/'.$s, array('action' => $s.'search'));
143             $m->connect('search/'.$s.'?q=:q',
144                         array('action' => $s.'search'),
145                         array('q' => '.+'));
146         }
147
148         // The second of these is needed to make the link work correctly
149         // when inserted into the page. The first is needed to match the
150         // route on the way in. Seems to be another Net_URL_Mapper bug to me.
151         $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
152         $m->connect('search/notice/rss?q=:q', array('action' => 'noticesearchrss'),
153                     array('q' => '.+'));
154
155         $m->connect('attachment/:attachment/ajax',
156                     array('action' => 'attachment_ajax'),
157                     array('attachment' => '[0-9]+'));
158
159         $m->connect('attachment/:attachment/thumbnail',
160                     array('action' => 'attachment_thumbnail'),
161                     array('attachment' => '[0-9]+'));
162
163         $m->connect('notice/new', array('action' => 'newnotice'));
164         $m->connect('notice/new?replyto=:replyto',
165                     array('action' => 'newnotice'),
166                     array('replyto' => '[A-Za-z0-9_-]+'));
167
168         $m->connect('notice/:notice/file',
169             array('action' => 'file'),
170             array('notice' => '[0-9]+'));
171
172         $m->connect('notice/:notice',
173                     array('action' => 'shownotice'),
174                     array('notice' => '[0-9]+'));
175         $m->connect('notice/delete', array('action' => 'deletenotice'));
176         $m->connect('notice/delete/:notice',
177                     array('action' => 'deletenotice'),
178                     array('notice' => '[0-9]+'));
179
180         // conversation
181
182         $m->connect('conversation/:id',
183                     array('action' => 'conversation'),
184                     array('id' => '[0-9]+'));
185
186         $m->connect('message/new', array('action' => 'newmessage'));
187         $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
188         $m->connect('message/:message',
189                     array('action' => 'showmessage'),
190                     array('message' => '[0-9]+'));
191
192         $m->connect('user/:id',
193                     array('action' => 'userbyid'),
194                     array('id' => '[0-9]+'));
195
196         $m->connect('tags/', array('action' => 'publictagcloud'));
197         $m->connect('tag/', array('action' => 'publictagcloud'));
198         $m->connect('tags', array('action' => 'publictagcloud'));
199         $m->connect('tag', array('action' => 'publictagcloud'));
200         $m->connect('tag/:tag/rss',
201                     array('action' => 'tagrss'),
202                     array('tag' => '[a-zA-Z0-9]+'));
203         $m->connect('tag/:tag',
204                     array('action' => 'tag'),
205                     array('tag' => '[a-zA-Z0-9]+'));
206
207         $m->connect('peopletag/:tag',
208                     array('action' => 'peopletag'),
209                     array('tag' => '[a-zA-Z0-9]+'));
210
211         $m->connect('featured/', array('action' => 'featured'));
212         $m->connect('featured', array('action' => 'featured'));
213         $m->connect('favorited/', array('action' => 'favorited'));
214         $m->connect('favorited', array('action' => 'favorited'));
215
216         // groups
217
218         $m->connect('group/new', array('action' => 'newgroup'));
219
220         foreach (array('edit', 'join', 'leave') as $v) {
221             $m->connect('group/:nickname/'.$v,
222                         array('action' => $v.'group'),
223                         array('nickname' => '[a-zA-Z0-9]+'));
224         }
225
226         foreach (array('members', 'logo', 'rss') as $n) {
227             $m->connect('group/:nickname/'.$n,
228                         array('action' => 'group'.$n),
229                         array('nickname' => '[a-zA-Z0-9]+'));
230         }
231
232         $m->connect('group/:nickname/blocked',
233                     array('action' => 'blockedfromgroup'),
234                     array('nickname' => '[a-zA-Z0-9]+'));
235
236         $m->connect('group/:id/id',
237                     array('action' => 'groupbyid'),
238                     array('id' => '[0-9]+'));
239
240         $m->connect('group/:nickname',
241                     array('action' => 'showgroup'),
242                     array('nickname' => '[a-zA-Z0-9]+'));
243
244         $m->connect('group/', array('action' => 'groups'));
245         $m->connect('group', array('action' => 'groups'));
246         $m->connect('groups/', array('action' => 'groups'));
247         $m->connect('groups', array('action' => 'groups'));
248
249         // Twitter-compatible API
250
251         // statuses API
252
253         $m->connect('api/statuses/:method',
254                     array('action' => 'api',
255                           'apiaction' => 'statuses'),
256                     array('method' => '(public_timeline|friends_timeline|user_timeline|update|replies|mentions|friends|followers|featured)(\.(atom|rss|xml|json))?'));
257
258         $m->connect('api/statuses/:method/:argument',
259                     array('action' => 'api',
260                           'apiaction' => 'statuses'),
261                     array('method' => '(user_timeline|friends_timeline|replies|mentions|show|destroy|friends|followers)'));
262
263         // users
264
265         $m->connect('api/users/:method/:argument',
266                     array('action' => 'api',
267                           'apiaction' => 'users'),
268                     array('method' => 'show(\.(xml|json))?'));
269
270         $m->connect('api/users/:method',
271                     array('action' => 'api',
272                           'apiaction' => 'users'),
273                     array('method' => 'show(\.(xml|json))?'));
274
275         // direct messages
276
277         foreach (array('xml', 'json') as $e) {
278             $m->connect('api/direct_messages/new.'.$e,
279                         array('action' => 'api',
280                               'apiaction' => 'direct_messages',
281                               'method' => 'create.'.$e));
282         }
283
284         foreach (array('xml', 'json', 'rss', 'atom') as $e) {
285             $m->connect('api/direct_messages.'.$e,
286                         array('action' => 'api',
287                               'apiaction' => 'direct_messages',
288                               'method' => 'direct_messages.'.$e));
289         }
290
291         foreach (array('xml', 'json', 'rss', 'atom') as $e) {
292             $m->connect('api/direct_messages/sent.'.$e,
293                         array('action' => 'api',
294                               'apiaction' => 'direct_messages',
295                               'method' => 'sent.'.$e));
296         }
297
298         $m->connect('api/direct_messages/destroy/:argument',
299                     array('action' => 'api',
300                           'apiaction' => 'direct_messages'));
301
302         // friendships
303
304         $m->connect('api/friendships/:method/:argument',
305                     array('action' => 'api',
306                           'apiaction' => 'friendships'),
307                     array('method' => '(create|destroy)'));
308
309         $m->connect('api/friendships/:method',
310                     array('action' => 'api',
311                           'apiaction' => 'friendships'),
312                     array('method' => 'exists(\.(xml|json))'));
313
314         // Social graph
315
316         $m->connect('api/friends/ids/:argument',
317                     array('action' => 'api',
318                           'apiaction' => 'statuses',
319                           'method' => 'friendsIDs'));
320
321         foreach (array('xml', 'json') as $e) {
322             $m->connect('api/friends/ids.'.$e,
323                         array('action' => 'api',
324                               'apiaction' => 'statuses',
325                               'method' => 'friendsIDs.'.$e));
326         }
327
328         $m->connect('api/followers/ids/:argument',
329                     array('action' => 'api',
330                           'apiaction' => 'statuses',
331                           'method' => 'followersIDs'));
332
333         foreach (array('xml', 'json') as $e) {
334             $m->connect('api/followers/ids.'.$e,
335                         array('action' => 'api',
336                               'apiaction' => 'statuses',
337                               'method' => 'followersIDs.'.$e));
338         }
339
340         // account
341
342         $m->connect('api/account/:method',
343                     array('action' => 'api',
344                           'apiaction' => 'account'));
345
346         // favorites
347
348         $m->connect('api/favorites/:method/:argument',
349                     array('action' => 'api',
350                           'apiaction' => 'favorites'));
351
352         $m->connect('api/favorites/:argument',
353                     array('action' => 'api',
354                           'apiaction' => 'favorites',
355                           'method' => 'favorites'));
356
357         foreach (array('xml', 'json', 'rss', 'atom') as $e) {
358             $m->connect('api/favorites.'.$e,
359                         array('action' => 'api',
360                               'apiaction' => 'favorites',
361                               'method' => 'favorites.'.$e));
362         }
363
364         // notifications
365
366         $m->connect('api/notifications/:method/:argument',
367                     array('action' => 'api',
368                           'apiaction' => 'favorites'));
369
370         // blocks
371
372         $m->connect('api/blocks/:method/:argument',
373                     array('action' => 'api',
374                           'apiaction' => 'blocks'));
375
376         // help
377
378         $m->connect('api/help/:method',
379                     array('action' => 'api',
380                           'apiaction' => 'help'));
381
382         // laconica
383
384         $m->connect('api/laconica/:method',
385                     array('action' => 'api',
386                           'apiaction' => 'laconica'));
387
388         // search
389         $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
390         $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
391         $m->connect('api/trends.json', array('action' => 'twitapitrends'));
392
393         // user stuff
394
395         foreach (array('subscriptions', 'subscribers',
396                        'nudge', 'xrds', 'all', 'foaf',
397                        'replies', 'inbox', 'outbox', 'microsummary') as $a) {
398             $m->connect(':nickname/'.$a,
399                         array('action' => $a),
400                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
401         }
402
403         foreach (array('subscriptions', 'subscribers') as $a) {
404             $m->connect(':nickname/'.$a.'/:tag',
405                         array('action' => $a),
406                         array('tag' => '[a-zA-Z0-9]+',
407                               'nickname' => '[a-zA-Z0-9]{1,64}'));
408         }
409
410         foreach (array('rss', 'groups') as $a) {
411             $m->connect(':nickname/'.$a,
412                         array('action' => 'user'.$a),
413                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
414         }
415
416         foreach (array('all', 'replies', 'favorites') as $a) {
417             $m->connect(':nickname/'.$a.'/rss',
418                         array('action' => $a.'rss'),
419                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
420         }
421
422         $m->connect(':nickname/favorites',
423                     array('action' => 'showfavorites'),
424                     array('nickname' => '[a-zA-Z0-9]{1,64}'));
425
426         $m->connect(':nickname/avatar/:size',
427                     array('action' => 'avatarbynickname'),
428                     array('size' => '(original|96|48|24)',
429                           'nickname' => '[a-zA-Z0-9]{1,64}'));
430
431         $m->connect(':nickname/tag/:tag/rss',
432             array('action' => 'userrss'),
433             array('nickname' => '[a-zA-Z0-9]{1,64}'),
434             array('tag' => '[a-zA-Z0-9]+'));
435
436         $m->connect(':nickname/tag/:tag',
437                     array('action' => 'showstream'),
438                     array('nickname' => '[a-zA-Z0-9]{1,64}'),
439                     array('tag' => '[a-zA-Z0-9]+'));
440
441         $m->connect(':nickname',
442                     array('action' => 'showstream'),
443                     array('nickname' => '[a-zA-Z0-9]{1,64}'));
444
445         Event::handle('RouterInitialized', array($m));
446
447         return $m;
448     }
449
450     function map($path)
451     {
452         try {
453             $match = $this->m->match($path);
454         } catch (Net_URL_Mapper_InvalidException $e) {
455             common_log(LOG_ERR, "Problem getting route for $path - " .
456                        $e->getMessage());
457             $cac = new ClientErrorAction("Page not found.", 404);
458             $cac->showPage();
459         }
460
461         return $match;
462     }
463
464     function build($action, $args=null, $params=null, $fragment=null)
465     {
466         $action_arg = array('action' => $action);
467
468         if ($args) {
469             $args = array_merge($action_arg, $args);
470         } else {
471             $args = $action_arg;
472         }
473
474         $url = $this->m->generate($args, $params, $fragment);
475
476         // Due to a bug in the Net_URL_Mapper code, the returned URL may
477         // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
478         // repair that here rather than modifying the upstream code...
479
480         $qpos = strpos($url, '?');
481         if ($qpos !== false) {
482             $url = substr($url, 0, $qpos+1) .
483               str_replace('?', '&', substr($url, $qpos+1));
484         }
485         return $url;
486     }
487 }