]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/router.php
Merge branch '0.8.x' of git://gitorious.org/~brion/statusnet/brion-fixes into 0.8.x
[quix0rs-gnu-social.git] / lib / router.php
1 <?php
2 /**
3  * StatusNet, 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   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET') && !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  StatusNet
43  * @author   Evan Prodromou <evan@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
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         // Twitter
92
93         $m->connect('twitter/authorization', array('action' => 'twitterauthorization'));
94
95         // facebook
96
97         $m->connect('facebook', array('action' => 'facebookhome'));
98         $m->connect('facebook/index.php', array('action' => 'facebookhome'));
99         $m->connect('facebook/settings.php', array('action' => 'facebooksettings'));
100         $m->connect('facebook/invite.php', array('action' => 'facebookinvite'));
101         $m->connect('facebook/remove', array('action' => 'facebookremove'));
102
103         // main stuff is repetitive
104
105         $main = array('login', 'logout', 'register', 'subscribe',
106                       'unsubscribe', 'confirmaddress', 'recoverpassword',
107                       'invite', 'favor', 'disfavor', 'sup',
108                       'block', 'unblock', 'subedit',
109                       'groupblock', 'groupunblock');
110
111         foreach ($main as $a) {
112             $m->connect('main/'.$a, array('action' => $a));
113         }
114
115         $m->connect('main/sup/:seconds', array('action' => 'sup'),
116                     array('seconds' => '[0-9]+'));
117
118         $m->connect('main/tagother/:id', array('action' => 'tagother'));
119
120         $m->connect('main/oembed',
121                     array('action' => 'oembed'));
122
123         // these take a code
124
125         foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
126             $m->connect('main/'.$c.'/:code', array('action' => $c));
127         }
128
129         // exceptional
130
131         $m->connect('main/openid', array('action' => 'openidlogin'));
132         $m->connect('main/remote', array('action' => 'remotesubscribe'));
133         $m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '[A-Za-z0-9_-]+'));
134
135         foreach (Router::$bare as $action) {
136             $m->connect('index.php?action=' . $action, array('action' => $action));
137         }
138
139         // settings
140
141         foreach (array('profile', 'avatar', 'password', 'openid', 'im',
142                        'email', 'sms', 'twitter', 'userdesign', 'other') as $s) {
143             $m->connect('settings/'.$s, array('action' => $s.'settings'));
144         }
145
146         // search
147
148         foreach (array('group', 'people', 'notice') as $s) {
149             $m->connect('search/'.$s, array('action' => $s.'search'));
150             $m->connect('search/'.$s.'?q=:q',
151                         array('action' => $s.'search'),
152                         array('q' => '.+'));
153         }
154
155         // The second of these is needed to make the link work correctly
156         // when inserted into the page. The first is needed to match the
157         // route on the way in. Seems to be another Net_URL_Mapper bug to me.
158         $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
159         $m->connect('search/notice/rss?q=:q', array('action' => 'noticesearchrss'),
160                     array('q' => '.+'));
161
162         $m->connect('attachment/:attachment',
163                     array('action' => 'attachment'),
164                     array('attachment' => '[0-9]+'));
165
166         $m->connect('attachment/:attachment/ajax',
167                     array('action' => 'attachment_ajax'),
168                     array('attachment' => '[0-9]+'));
169
170         $m->connect('attachment/:attachment/thumbnail',
171                     array('action' => 'attachment_thumbnail'),
172                     array('attachment' => '[0-9]+'));
173
174         $m->connect('notice/new', array('action' => 'newnotice'));
175         $m->connect('notice/new?replyto=:replyto',
176                     array('action' => 'newnotice'),
177                     array('replyto' => '[A-Za-z0-9_-]+'));
178         $m->connect('notice/new?replyto=:replyto&inreplyto=:inreplyto',
179                     array('action' => 'newnotice'),
180                     array('replyto' => '[A-Za-z0-9_-]+'),
181                     array('inreplyto' => '[0-9]+'));
182
183         $m->connect('notice/:notice/file',
184             array('action' => 'file'),
185             array('notice' => '[0-9]+'));
186
187         $m->connect('notice/:notice',
188                     array('action' => 'shownotice'),
189                     array('notice' => '[0-9]+'));
190         $m->connect('notice/delete', array('action' => 'deletenotice'));
191         $m->connect('notice/delete/:notice',
192                     array('action' => 'deletenotice'),
193                     array('notice' => '[0-9]+'));
194
195         // conversation
196
197         $m->connect('conversation/:id',
198                     array('action' => 'conversation'),
199                     array('id' => '[0-9]+'));
200
201         $m->connect('message/new', array('action' => 'newmessage'));
202         $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
203         $m->connect('message/:message',
204                     array('action' => 'showmessage'),
205                     array('message' => '[0-9]+'));
206
207         $m->connect('user/:id',
208                     array('action' => 'userbyid'),
209                     array('id' => '[0-9]+'));
210
211         $m->connect('tags/', array('action' => 'publictagcloud'));
212         $m->connect('tag/', array('action' => 'publictagcloud'));
213         $m->connect('tags', array('action' => 'publictagcloud'));
214         $m->connect('tag', array('action' => 'publictagcloud'));
215         $m->connect('tag/:tag/rss',
216                     array('action' => 'tagrss'),
217                     array('tag' => '[a-zA-Z0-9]+'));
218         $m->connect('tag/:tag',
219                     array('action' => 'tag'),
220                     array('tag' => '[\pL\pN_\-\.]{1,64}'));
221
222         $m->connect('peopletag/:tag',
223                     array('action' => 'peopletag'),
224                     array('tag' => '[a-zA-Z0-9]+'));
225
226         $m->connect('featured/', array('action' => 'featured'));
227         $m->connect('featured', array('action' => 'featured'));
228         $m->connect('favorited/', array('action' => 'favorited'));
229         $m->connect('favorited', array('action' => 'favorited'));
230
231         // groups
232
233         $m->connect('group/new', array('action' => 'newgroup'));
234
235         foreach (array('edit', 'join', 'leave') as $v) {
236             $m->connect('group/:nickname/'.$v,
237                         array('action' => $v.'group'),
238                         array('nickname' => '[a-zA-Z0-9]+'));
239         }
240
241         foreach (array('members', 'logo', 'rss', 'designsettings') as $n) {
242             $m->connect('group/:nickname/'.$n,
243                         array('action' => 'group'.$n),
244                         array('nickname' => '[a-zA-Z0-9]+'));
245         }
246
247         $m->connect('group/:nickname/blocked',
248                     array('action' => 'blockedfromgroup'),
249                     array('nickname' => '[a-zA-Z0-9]+'));
250
251         $m->connect('group/:nickname/makeadmin',
252                     array('action' => 'makeadmin'),
253                     array('nickname' => '[a-zA-Z0-9]+'));
254
255         $m->connect('group/:id/id',
256                     array('action' => 'groupbyid'),
257                     array('id' => '[0-9]+'));
258
259         $m->connect('group/:nickname',
260                     array('action' => 'showgroup'),
261                     array('nickname' => '[a-zA-Z0-9]+'));
262
263         $m->connect('group/', array('action' => 'groups'));
264         $m->connect('group', array('action' => 'groups'));
265         $m->connect('groups/', array('action' => 'groups'));
266         $m->connect('groups', array('action' => 'groups'));
267
268         // Twitter-compatible API
269
270         // statuses API
271
272         $m->connect('api/statuses/:method',
273                     array('action' => 'api',
274                           'apiaction' => 'statuses'),
275                     array('method' => '(public_timeline|home_timeline|friends_timeline|user_timeline|update|replies|mentions|show|friends|followers|featured)(\.(atom|rss|xml|json))?'));
276
277         $m->connect('api/statuses/:method/:argument',
278                     array('action' => 'api',
279                           'apiaction' => 'statuses'),
280                     array('method' => '(user_timeline|home_timeline|friends_timeline|replies|mentions|show|destroy|friends|followers)'));
281
282         // users
283
284         $m->connect('api/users/:method/:argument',
285                     array('action' => 'api',
286                           'apiaction' => 'users'),
287                     array('method' => 'show(\.(xml|json))?'));
288
289         $m->connect('api/users/:method',
290                     array('action' => 'api',
291                           'apiaction' => 'users'),
292                     array('method' => 'show(\.(xml|json))?'));
293
294         // direct messages
295
296         foreach (array('xml', 'json') as $e) {
297             $m->connect('api/direct_messages/new.'.$e,
298                         array('action' => 'api',
299                               'apiaction' => 'direct_messages',
300                               'method' => 'create.'.$e));
301         }
302
303         foreach (array('xml', 'json', 'rss', 'atom') as $e) {
304             $m->connect('api/direct_messages.'.$e,
305                         array('action' => 'api',
306                               'apiaction' => 'direct_messages',
307                               'method' => 'direct_messages.'.$e));
308         }
309
310         foreach (array('xml', 'json', 'rss', 'atom') as $e) {
311             $m->connect('api/direct_messages/sent.'.$e,
312                         array('action' => 'api',
313                               'apiaction' => 'direct_messages',
314                               'method' => 'sent.'.$e));
315         }
316
317         $m->connect('api/direct_messages/destroy/:argument',
318                     array('action' => 'api',
319                           'apiaction' => 'direct_messages'));
320
321         // friendships
322
323         $m->connect('api/friendships/:method/:argument',
324                     array('action' => 'api',
325                           'apiaction' => 'friendships'),
326                     array('method' => '(create|destroy)'));
327
328         $m->connect('api/friendships/:method',
329                     array('action' => 'api',
330                           'apiaction' => 'friendships'),
331                     array('method' => '(show|exists)(\.(xml|json))'));
332
333         // Social graph
334
335         $m->connect('api/friends/ids/:argument',
336                     array('action' => 'api',
337                           'apiaction' => 'statuses',
338                           'method' => 'friendsIDs'));
339
340         foreach (array('xml', 'json') as $e) {
341             $m->connect('api/friends/ids.'.$e,
342                         array('action' => 'api',
343                               'apiaction' => 'statuses',
344                               'method' => 'friendsIDs.'.$e));
345         }
346
347         $m->connect('api/followers/ids/:argument',
348                     array('action' => 'api',
349                           'apiaction' => 'statuses',
350                           'method' => 'followersIDs'));
351
352         foreach (array('xml', 'json') as $e) {
353             $m->connect('api/followers/ids.'.$e,
354                         array('action' => 'api',
355                               'apiaction' => 'statuses',
356                               'method' => 'followersIDs.'.$e));
357         }
358
359         // account
360
361         $m->connect('api/account/:method',
362                     array('action' => 'api',
363                           'apiaction' => 'account'));
364
365         // favorites
366
367         $m->connect('api/favorites/:method/:argument',
368                     array('action' => 'api',
369                           'apiaction' => 'favorites',
370                           array('method' => '(create|destroy)')));
371
372         $m->connect('api/favorites/:argument',
373                     array('action' => 'api',
374                           'apiaction' => 'favorites',
375                           'method' => 'favorites'));
376
377         foreach (array('xml', 'json', 'rss', 'atom') as $e) {
378             $m->connect('api/favorites.'.$e,
379                         array('action' => 'api',
380                               'apiaction' => 'favorites',
381                               'method' => 'favorites.'.$e));
382         }
383
384         // notifications
385
386         $m->connect('api/notifications/:method/:argument',
387                     array('action' => 'api',
388                           'apiaction' => 'favorites'));
389
390         // blocks
391
392         $m->connect('api/blocks/:method/:argument',
393                     array('action' => 'api',
394                           'apiaction' => 'blocks'));
395
396         // help
397
398         $m->connect('api/help/:method',
399                     array('action' => 'api',
400                           'apiaction' => 'help'));
401
402         // statusnet
403
404         $m->connect('api/statusnet/:method',
405                     array('action' => 'api',
406                           'apiaction' => 'statusnet'));
407
408         // For older methods, we provide "laconica" base action
409
410         $m->connect('api/laconica/:method',
411                     array('action' => 'api',
412                           'apiaction' => 'statusnet'));
413
414         // Groups and tags are newer than 0.8.1 so no backward-compatibility
415         // necessary
416
417         // Groups
418         //'list' has to be handled differently, as php will not allow a method to be named 'list'
419         $m->connect('api/statusnet/groups/list/:argument',
420                     array('action' => 'api',
421                           'method' => 'list_groups',
422                           'apiaction' => 'groups'));
423
424         foreach (array('xml', 'json', 'rss', 'atom') as $e) {
425             $m->connect('api/statusnet/groups/list.' . $e,
426                     array('action' => 'api',
427                           'method' => 'list_groups.' . $e,
428                           'apiaction' => 'groups'));
429         }
430
431         $m->connect('api/statusnet/groups/:method',
432                     array('action' => 'api',
433                           'apiaction' => 'statuses'),
434                     array('method' => '(list_all|)(\.(atom|rss|xml|json))?'));
435
436         $m->connect('api/statuses/:method/:argument',
437                     array('action' => 'api',
438                           'apiaction' => 'statuses'),
439                     array('method' => '(user_timeline|home_timeline|friends_timeline|replies|mentions|show|destroy|friends|followers)'));
440
441         $m->connect('api/statusnet/groups/:method/:argument',
442                     array('action' => 'api',
443                           'apiaction' => 'groups'));
444
445         $m->connect('api/statusnet/groups/:method',
446                     array('action' => 'api',
447                           'apiaction' => 'groups'));
448
449         // Tags
450         $m->connect('api/statusnet/tags/:method/:argument',
451                     array('action' => 'api',
452                           'apiaction' => 'tags'));
453
454         $m->connect('api/statusnet/tags/:method',
455                     array('action' => 'api',
456                           'apiaction' => 'tags'));
457
458         // search
459         $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
460         $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
461         $m->connect('api/trends.json', array('action' => 'twitapitrends'));
462
463         // user stuff
464
465         foreach (array('subscriptions', 'subscribers',
466                        'nudge', 'xrds', 'all', 'foaf',
467                        'replies', 'inbox', 'outbox', 'microsummary') as $a) {
468             $m->connect(':nickname/'.$a,
469                         array('action' => $a),
470                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
471         }
472
473         foreach (array('subscriptions', 'subscribers') as $a) {
474             $m->connect(':nickname/'.$a.'/:tag',
475                         array('action' => $a),
476                         array('tag' => '[a-zA-Z0-9]+',
477                               'nickname' => '[a-zA-Z0-9]{1,64}'));
478         }
479
480         foreach (array('rss', 'groups') as $a) {
481             $m->connect(':nickname/'.$a,
482                         array('action' => 'user'.$a),
483                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
484         }
485
486         foreach (array('all', 'replies', 'favorites') as $a) {
487             $m->connect(':nickname/'.$a.'/rss',
488                         array('action' => $a.'rss'),
489                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
490         }
491
492         $m->connect(':nickname/favorites',
493                     array('action' => 'showfavorites'),
494                     array('nickname' => '[a-zA-Z0-9]{1,64}'));
495
496         $m->connect(':nickname/avatar/:size',
497                     array('action' => 'avatarbynickname'),
498                     array('size' => '(original|96|48|24)',
499                           'nickname' => '[a-zA-Z0-9]{1,64}'));
500
501         $m->connect(':nickname/tag/:tag/rss',
502             array('action' => 'userrss'),
503             array('nickname' => '[a-zA-Z0-9]{1,64}'),
504             array('tag' => '[a-zA-Z0-9]+'));
505
506         $m->connect(':nickname/tag/:tag',
507                     array('action' => 'showstream'),
508                     array('nickname' => '[a-zA-Z0-9]{1,64}'),
509                     array('tag' => '[a-zA-Z0-9]+'));
510
511         $m->connect(':nickname',
512                     array('action' => 'showstream'),
513                     array('nickname' => '[a-zA-Z0-9]{1,64}'));
514
515         Event::handle('RouterInitialized', array($m));
516
517         return $m;
518     }
519
520     function map($path)
521     {
522         try {
523             $match = $this->m->match($path);
524         } catch (Net_URL_Mapper_InvalidException $e) {
525             common_log(LOG_ERR, "Problem getting route for $path - " .
526                        $e->getMessage());
527             $cac = new ClientErrorAction("Page not found.", 404);
528             $cac->showPage();
529         }
530
531         return $match;
532     }
533
534     function build($action, $args=null, $params=null, $fragment=null)
535     {
536         $action_arg = array('action' => $action);
537
538         if ($args) {
539             $args = array_merge($action_arg, $args);
540         } else {
541             $args = $action_arg;
542         }
543
544         $url = $this->m->generate($args, $params, $fragment);
545
546         // Due to a bug in the Net_URL_Mapper code, the returned URL may
547         // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
548         // repair that here rather than modifying the upstream code...
549
550         $qpos = strpos($url, '?');
551         if ($qpos !== false) {
552             $url = substr($url, 0, $qpos+1) .
553               str_replace('?', '&', substr($url, $qpos+1));
554         }
555         return $url;
556     }
557 }