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