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