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