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