]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/router.php
define LACONICA and accept LACONICA for backwards compatibility
[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
179         $m->connect('notice/:notice/file',
180             array('action' => 'file'),
181             array('notice' => '[0-9]+'));
182
183         $m->connect('notice/:notice',
184                     array('action' => 'shownotice'),
185                     array('notice' => '[0-9]+'));
186         $m->connect('notice/delete', array('action' => 'deletenotice'));
187         $m->connect('notice/delete/:notice',
188                     array('action' => 'deletenotice'),
189                     array('notice' => '[0-9]+'));
190
191         // conversation
192
193         $m->connect('conversation/:id',
194                     array('action' => 'conversation'),
195                     array('id' => '[0-9]+'));
196
197         $m->connect('message/new', array('action' => 'newmessage'));
198         $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
199         $m->connect('message/:message',
200                     array('action' => 'showmessage'),
201                     array('message' => '[0-9]+'));
202
203         $m->connect('user/:id',
204                     array('action' => 'userbyid'),
205                     array('id' => '[0-9]+'));
206
207         $m->connect('tags/', array('action' => 'publictagcloud'));
208         $m->connect('tag/', array('action' => 'publictagcloud'));
209         $m->connect('tags', array('action' => 'publictagcloud'));
210         $m->connect('tag', array('action' => 'publictagcloud'));
211         $m->connect('tag/:tag/rss',
212                     array('action' => 'tagrss'),
213                     array('tag' => '[a-zA-Z0-9]+'));
214         $m->connect('tag/:tag',
215                     array('action' => 'tag'),
216                     array('tag' => '[\pL\pN_\-\.]{1,64}'));
217
218         $m->connect('peopletag/:tag',
219                     array('action' => 'peopletag'),
220                     array('tag' => '[a-zA-Z0-9]+'));
221
222         $m->connect('featured/', array('action' => 'featured'));
223         $m->connect('featured', array('action' => 'featured'));
224         $m->connect('favorited/', array('action' => 'favorited'));
225         $m->connect('favorited', array('action' => 'favorited'));
226
227         // groups
228
229         $m->connect('group/new', array('action' => 'newgroup'));
230
231         foreach (array('edit', 'join', 'leave') as $v) {
232             $m->connect('group/:nickname/'.$v,
233                         array('action' => $v.'group'),
234                         array('nickname' => '[a-zA-Z0-9]+'));
235         }
236
237         foreach (array('members', 'logo', 'rss', 'designsettings') as $n) {
238             $m->connect('group/:nickname/'.$n,
239                         array('action' => 'group'.$n),
240                         array('nickname' => '[a-zA-Z0-9]+'));
241         }
242
243         $m->connect('group/:nickname/blocked',
244                     array('action' => 'blockedfromgroup'),
245                     array('nickname' => '[a-zA-Z0-9]+'));
246
247         $m->connect('group/:nickname/makeadmin',
248                     array('action' => 'makeadmin'),
249                     array('nickname' => '[a-zA-Z0-9]+'));
250
251         $m->connect('group/:id/id',
252                     array('action' => 'groupbyid'),
253                     array('id' => '[0-9]+'));
254
255         $m->connect('group/:nickname',
256                     array('action' => 'showgroup'),
257                     array('nickname' => '[a-zA-Z0-9]+'));
258
259         $m->connect('group/', array('action' => 'groups'));
260         $m->connect('group', array('action' => 'groups'));
261         $m->connect('groups/', array('action' => 'groups'));
262         $m->connect('groups', array('action' => 'groups'));
263
264         // Twitter-compatible API
265
266         // statuses API
267
268         $m->connect('api/statuses/:method',
269                     array('action' => 'api',
270                           'apiaction' => 'statuses'),
271                     array('method' => '(public_timeline|friends_timeline|user_timeline|update|replies|mentions|show|friends|followers|featured)(\.(atom|rss|xml|json))?'));
272
273         $m->connect('api/statuses/:method/:argument',
274                     array('action' => 'api',
275                           'apiaction' => 'statuses'),
276                     array('method' => '(|user_timeline|friends_timeline|replies|mentions|show|destroy|friends|followers)'));
277
278         // users
279
280         $m->connect('api/users/:method/:argument',
281                     array('action' => 'api',
282                           'apiaction' => 'users'),
283                     array('method' => 'show(\.(xml|json))?'));
284
285         $m->connect('api/users/:method',
286                     array('action' => 'api',
287                           'apiaction' => 'users'),
288                     array('method' => 'show(\.(xml|json))?'));
289
290         // direct messages
291
292         foreach (array('xml', 'json') as $e) {
293             $m->connect('api/direct_messages/new.'.$e,
294                         array('action' => 'api',
295                               'apiaction' => 'direct_messages',
296                               'method' => 'create.'.$e));
297         }
298
299         foreach (array('xml', 'json', 'rss', 'atom') as $e) {
300             $m->connect('api/direct_messages.'.$e,
301                         array('action' => 'api',
302                               'apiaction' => 'direct_messages',
303                               'method' => 'direct_messages.'.$e));
304         }
305
306         foreach (array('xml', 'json', 'rss', 'atom') as $e) {
307             $m->connect('api/direct_messages/sent.'.$e,
308                         array('action' => 'api',
309                               'apiaction' => 'direct_messages',
310                               'method' => 'sent.'.$e));
311         }
312
313         $m->connect('api/direct_messages/destroy/:argument',
314                     array('action' => 'api',
315                           'apiaction' => 'direct_messages'));
316
317         // friendships
318
319         $m->connect('api/friendships/:method/:argument',
320                     array('action' => 'api',
321                           'apiaction' => 'friendships'),
322                     array('method' => '(create|destroy)'));
323
324         $m->connect('api/friendships/:method',
325                     array('action' => 'api',
326                           'apiaction' => 'friendships'),
327                     array('method' => '(show|exists)(\.(xml|json))'));
328
329         // Social graph
330
331         $m->connect('api/friends/ids/:argument',
332                     array('action' => 'api',
333                           'apiaction' => 'statuses',
334                           'method' => 'friendsIDs'));
335
336         foreach (array('xml', 'json') as $e) {
337             $m->connect('api/friends/ids.'.$e,
338                         array('action' => 'api',
339                               'apiaction' => 'statuses',
340                               'method' => 'friendsIDs.'.$e));
341         }
342
343         $m->connect('api/followers/ids/:argument',
344                     array('action' => 'api',
345                           'apiaction' => 'statuses',
346                           'method' => 'followersIDs'));
347
348         foreach (array('xml', 'json') as $e) {
349             $m->connect('api/followers/ids.'.$e,
350                         array('action' => 'api',
351                               'apiaction' => 'statuses',
352                               'method' => 'followersIDs.'.$e));
353         }
354
355         // account
356
357         $m->connect('api/account/:method',
358                     array('action' => 'api',
359                           'apiaction' => 'account'));
360
361         // favorites
362
363         $m->connect('api/favorites/:method/:argument',
364                     array('action' => 'api',
365                           'apiaction' => 'favorites',
366                           array('method' => '(create|destroy)')));
367
368         $m->connect('api/favorites/:argument',
369                     array('action' => 'api',
370                           'apiaction' => 'favorites',
371                           'method' => 'favorites'));
372
373         foreach (array('xml', 'json', 'rss', 'atom') as $e) {
374             $m->connect('api/favorites.'.$e,
375                         array('action' => 'api',
376                               'apiaction' => 'favorites',
377                               'method' => 'favorites.'.$e));
378         }
379
380         // notifications
381
382         $m->connect('api/notifications/:method/:argument',
383                     array('action' => 'api',
384                           'apiaction' => 'favorites'));
385
386         // blocks
387
388         $m->connect('api/blocks/:method/:argument',
389                     array('action' => 'api',
390                           'apiaction' => 'blocks'));
391
392         // help
393
394         $m->connect('api/help/:method',
395                     array('action' => 'api',
396                           'apiaction' => 'help'));
397
398         // statusnet
399
400         $m->connect('api/statusnet/:method',
401                     array('action' => 'api',
402                           'apiaction' => 'statusnet'));
403
404         $m->connect('api/statusnet/:method',
405                     array('action' => 'api',
406                           'apiaction' => 'statusnet'));
407
408         // Groups
409         //'list' has to be handled differently, as php will not allow a method to be named 'list'
410         $m->connect('api/statusnet/groups/list/:argument',
411                     array('action' => 'api',
412                           'method' => 'list_groups',
413                           'apiaction' => 'groups'));
414         foreach (array('xml', 'json', 'rss', 'atom') as $e) {
415             $m->connect('api/statusnet/groups/list.' . $e,
416                     array('action' => 'api',
417                           'method' => 'list_groups.' . $e,
418                           'apiaction' => 'groups'));
419         }
420
421         $m->connect('api/statusnet/groups/:method',
422                     array('action' => 'api',
423                           'apiaction' => 'statuses'),
424                     array('method' => '(list_all|)(\.(atom|rss|xml|json))?'));
425
426         $m->connect('api/statuses/:method/:argument',
427                     array('action' => 'api',
428                           'apiaction' => 'statuses'),
429                     array('method' => '(|user_timeline|friends_timeline|replies|mentions|show|destroy|friends|followers)'));
430
431         $m->connect('api/statusnet/groups/:method/:argument',
432                     array('action' => 'api',
433                           'apiaction' => 'groups'));
434
435         $m->connect('api/statusnet/groups/:method',
436                     array('action' => 'api',
437                           'apiaction' => 'groups'));
438
439         // Tags
440         $m->connect('api/statusnet/tags/:method/:argument',
441                     array('action' => 'api',
442                           'apiaction' => 'tags'));
443
444         $m->connect('api/statusnet/tags/:method',
445                     array('action' => 'api',
446                           'apiaction' => 'tags'));
447
448         // search
449         $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
450         $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
451         $m->connect('api/trends.json', array('action' => 'twitapitrends'));
452
453         // user stuff
454
455         foreach (array('subscriptions', 'subscribers',
456                        'nudge', 'xrds', 'all', 'foaf',
457                        'replies', 'inbox', 'outbox', 'microsummary') as $a) {
458             $m->connect(':nickname/'.$a,
459                         array('action' => $a),
460                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
461         }
462
463         foreach (array('subscriptions', 'subscribers') as $a) {
464             $m->connect(':nickname/'.$a.'/:tag',
465                         array('action' => $a),
466                         array('tag' => '[a-zA-Z0-9]+',
467                               'nickname' => '[a-zA-Z0-9]{1,64}'));
468         }
469
470         foreach (array('rss', 'groups') as $a) {
471             $m->connect(':nickname/'.$a,
472                         array('action' => 'user'.$a),
473                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
474         }
475
476         foreach (array('all', 'replies', 'favorites') as $a) {
477             $m->connect(':nickname/'.$a.'/rss',
478                         array('action' => $a.'rss'),
479                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
480         }
481
482         $m->connect(':nickname/favorites',
483                     array('action' => 'showfavorites'),
484                     array('nickname' => '[a-zA-Z0-9]{1,64}'));
485
486         $m->connect(':nickname/avatar/:size',
487                     array('action' => 'avatarbynickname'),
488                     array('size' => '(original|96|48|24)',
489                           'nickname' => '[a-zA-Z0-9]{1,64}'));
490
491         $m->connect(':nickname/tag/:tag/rss',
492             array('action' => 'userrss'),
493             array('nickname' => '[a-zA-Z0-9]{1,64}'),
494             array('tag' => '[a-zA-Z0-9]+'));
495
496         $m->connect(':nickname/tag/:tag',
497                     array('action' => 'showstream'),
498                     array('nickname' => '[a-zA-Z0-9]{1,64}'),
499                     array('tag' => '[a-zA-Z0-9]+'));
500
501         $m->connect(':nickname',
502                     array('action' => 'showstream'),
503                     array('nickname' => '[a-zA-Z0-9]{1,64}'));
504
505         Event::handle('RouterInitialized', array($m));
506
507         return $m;
508     }
509
510     function map($path)
511     {
512         try {
513             $match = $this->m->match($path);
514         } catch (Net_URL_Mapper_InvalidException $e) {
515             common_log(LOG_ERR, "Problem getting route for $path - " .
516                        $e->getMessage());
517             $cac = new ClientErrorAction("Page not found.", 404);
518             $cac->showPage();
519         }
520
521         return $match;
522     }
523
524     function build($action, $args=null, $params=null, $fragment=null)
525     {
526         $action_arg = array('action' => $action);
527
528         if ($args) {
529             $args = array_merge($action_arg, $args);
530         } else {
531             $args = $action_arg;
532         }
533
534         $url = $this->m->generate($args, $params, $fragment);
535
536         // Due to a bug in the Net_URL_Mapper code, the returned URL may
537         // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
538         // repair that here rather than modifying the upstream code...
539
540         $qpos = strpos($url, '?');
541         if ($qpos !== false) {
542             $url = substr($url, 0, $qpos+1) .
543               str_replace('?', '&', substr($url, $qpos+1));
544         }
545         return $url;
546     }
547 }