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