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