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