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