]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/router.php
e39dc217a36fb87192a483b07852e0c2ff6941dd
[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', array('action' => $s.'search'), array('q' => '.+'));
140         }
141
142         $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
143
144         // notice
145
146         $m->connect('notice/new', array('action' => 'newnotice'));
147         $m->connect('notice/new?replyto=:replyto',
148                     array('action' => 'newnotice'),
149                     array('replyto' => '[A-Za-z0-9_-]+'));
150         $m->connect('notice/:notice',
151                     array('action' => 'shownotice'),
152                     array('notice' => '[0-9]+'));
153         $m->connect('notice/delete', array('action' => 'deletenotice'));
154         $m->connect('notice/delete/:notice',
155                     array('action' => 'deletenotice'),
156                     array('notice' => '[0-9]+'));
157
158         $m->connect('message/new', array('action' => 'newmessage'));
159         $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
160         $m->connect('message/:message',
161                     array('action' => 'showmessage'),
162                     array('message' => '[0-9]+'));
163
164         $m->connect('user/:id',
165                     array('action' => 'userbyid'),
166                     array('id' => '[0-9]+'));
167
168         $m->connect('tags/', array('action' => 'publictagcloud'));
169         $m->connect('tag/', array('action' => 'publictagcloud'));
170         $m->connect('tags', array('action' => 'publictagcloud'));
171         $m->connect('tag', array('action' => 'publictagcloud'));
172         $m->connect('tag/:tag/rss',
173                     array('action' => 'tagrss'),
174                     array('tag' => '[a-zA-Z0-9]+'));
175         $m->connect('tag/:tag',
176                     array('action' => 'tag'),
177                     array('tag' => '[a-zA-Z0-9]+'));
178
179         $m->connect('peopletag/:tag',
180                     array('action' => 'peopletag'),
181                     array('tag' => '[a-zA-Z0-9]+'));
182
183         $m->connect('featured/', array('action' => 'featured'));
184         $m->connect('featured', array('action' => 'featured'));
185         $m->connect('favorited/', array('action' => 'favorited'));
186         $m->connect('favorited', array('action' => 'favorited'));
187
188         // groups
189
190         $m->connect('group/new', array('action' => 'newgroup'));
191
192         foreach (array('edit', 'join', 'leave') as $v) {
193             $m->connect('group/:nickname/'.$v,
194                         array('action' => $v.'group'),
195                         array('nickname' => '[a-zA-Z0-9]+'));
196         }
197
198         foreach (array('members', 'logo', 'rss') as $n) {
199             $m->connect('group/:nickname/'.$n,
200                         array('action' => 'group'.$n),
201                         array('nickname' => '[a-zA-Z0-9]+'));
202         }
203
204         $m->connect('group/:id/id',
205                     array('action' => 'groupbyid'),
206                     array('id' => '[0-9]+'));
207
208         $m->connect('group/:nickname',
209                     array('action' => 'showgroup'),
210                     array('nickname' => '[a-zA-Z0-9]+'));
211
212         $m->connect('group/', array('action' => 'groups'));
213         $m->connect('group', array('action' => 'groups'));
214         $m->connect('groups/', array('action' => 'groups'));
215         $m->connect('groups', array('action' => 'groups'));
216
217         // Twitter-compatible API
218
219         // statuses API
220
221         $m->connect('api/statuses/:method',
222                     array('action' => 'api',
223                           'apiaction' => 'statuses'),
224                     array('method' => '(public_timeline|friends_timeline|user_timeline|update|replies|friends|followers|featured)(\.(atom|rss|xml|json))?'));
225
226         $m->connect('api/statuses/:method/:argument',
227                     array('action' => 'api',
228                           'apiaction' => 'statuses'),
229                     array('method' => '(user_timeline|friends_timeline|replies|show|destroy|friends|followers)'));
230
231         // users
232
233         $m->connect('api/users/:method/:argument',
234                     array('action' => 'api',
235                           'apiaction' => 'users'),
236                     array('method' => 'show(\.(xml|json))?'));
237
238         $m->connect('api/users/:method',
239                     array('action' => 'api',
240                           'apiaction' => 'users'),
241                     array('method' => 'show(\.(xml|json))?'));
242
243         // direct messages
244
245         foreach (array('xml', 'json') as $e) {
246             $m->connect('api/direct_messages/new.'.$e,
247                         array('action' => 'api',
248                               'apiaction' => 'direct_messages',
249                               'method' => 'create.'.$e));
250         }
251
252         foreach (array('xml', 'json', 'rss', 'atom') as $e) {
253             $m->connect('api/direct_messages.'.$e,
254                         array('action' => 'api',
255                               'apiaction' => 'direct_messages',
256                               'method' => 'direct_messages.'.$e));
257         }
258
259         foreach (array('xml', 'json', 'rss', 'atom') as $e) {
260             $m->connect('api/direct_messages/sent.'.$e,
261                         array('action' => 'api',
262                         'apiaction' => 'direct_messages',
263                         'method' => 'sent.'.$e));
264         }
265
266         $m->connect('api/direct_messages/destroy/:argument',
267                     array('action' => 'api',
268                           'apiaction' => 'direct_messages'));
269
270         // friendships
271
272         $m->connect('api/friendships/:method/:argument',
273                     array('action' => 'api',
274                           'apiaction' => 'friendships'),
275                     array('method' => '(create|destroy)'));
276
277         $m->connect('api/friendships/:method',
278                     array('action' => 'api',
279                           'apiaction' => 'friendships'),
280                     array('method' => 'exists(\.(xml|json))'));
281
282         // Social graph
283
284         $m->connect('api/friends/ids/:argument',
285                     array('action' => 'api',
286                           'apiaction' => 'statuses',
287                           'method' => 'friendsIDs'));
288
289         foreach (array('xml', 'json') as $e) {
290             $m->connect('api/friends/ids.'.$e,
291                         array('action' => 'api',
292                               'apiaction' => 'statuses',
293                               'method' => 'friendsIDs.'.$e));
294         }
295
296         $m->connect('api/followers/ids/:argument',
297                     array('action' => 'api',
298                           'apiaction' => 'statuses',
299                           'method' => 'followersIDs'));
300
301         foreach (array('xml', 'json') as $e) {
302             $m->connect('api/followers/ids.'.$e,
303                         array('action' => 'api',
304                               'apiaction' => 'statuses',
305                               'method' => 'followersIDs.'.$e));
306         }
307
308         // account
309
310         $m->connect('api/account/:method',
311                     array('action' => 'api',
312                           'apiaction' => 'account'));
313
314         // favorites
315
316         $m->connect('api/favorites/:method/:argument',
317                     array('action' => 'api',
318                           'apiaction' => 'favorites'));
319
320         $m->connect('api/favorites/:argument',
321                     array('action' => 'api',
322                           'apiaction' => 'favorites',
323                           'method' => 'favorites'));
324
325         foreach (array('xml', 'json', 'rss', 'atom') as $e) {
326             $m->connect('api/favorites.'.$e,
327                 array('action' => 'api',
328                       'apiaction' => 'favorites',
329                       'method' => 'favorites.'.$e));
330         }
331
332         // notifications
333
334         $m->connect('api/notifications/:method/:argument',
335                     array('action' => 'api',
336                           'apiaction' => 'favorites'));
337
338         // blocks
339
340         $m->connect('api/blocks/:method/:argument',
341                     array('action' => 'api',
342                           'apiaction' => 'blocks'));
343
344         // help
345
346         $m->connect('api/help/:method',
347                     array('action' => 'api',
348                           'apiaction' => 'help'));
349
350         // laconica
351
352         $m->connect('api/laconica/:method',
353                     array('action' => 'api',
354                           'apiaction' => 'laconica'));
355
356         // search
357         $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
358         $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
359         $m->connect('api/trends.json', array('action' => 'twitapitrends'));
360
361         // user stuff
362
363         foreach (array('subscriptions', 'subscribers',
364                        'nudge', 'xrds', 'all', 'foaf',
365                        'replies', 'inbox', 'outbox', 'microsummary') as $a) {
366             $m->connect(':nickname/'.$a,
367                         array('action' => $a),
368                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
369         }
370
371         foreach (array('subscriptions', 'subscribers') as $a) {
372             $m->connect(':nickname/'.$a.'/:tag',
373                         array('action' => $a),
374                         array('tag' => '[a-zA-Z0-9]+',
375                               'nickname' => '[a-zA-Z0-9]{1,64}'));
376         }
377
378         foreach (array('rss', 'groups') as $a) {
379             $m->connect(':nickname/'.$a,
380                         array('action' => 'user'.$a),
381                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
382         }
383
384         foreach (array('all', 'replies', 'favorites') as $a) {
385             $m->connect(':nickname/'.$a.'/rss',
386                         array('action' => $a.'rss'),
387                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
388         }
389
390         $m->connect(':nickname/favorites',
391                     array('action' => 'showfavorites'),
392                     array('nickname' => '[a-zA-Z0-9]{1,64}'));
393
394         $m->connect(':nickname/avatar/:size',
395                     array('action' => 'avatarbynickname'),
396                     array('size' => '(original|96|48|24)',
397                           'nickname' => '[a-zA-Z0-9]{1,64}'));
398
399         $m->connect(':nickname',
400                     array('action' => 'showstream'),
401                     array('nickname' => '[a-zA-Z0-9]{1,64}'));
402
403         Event::handle('RouterInitialized', array($m));
404
405         return $m;
406     }
407
408     function map($path)
409     {
410         try {
411             $match = $this->m->match($path);
412         } catch (Net_URL_Mapper_InvalidException $e) {
413             common_log(LOG_ERR, "Problem getting route for $path - " .
414                 $e->getMessage());
415             $cac = new ClientErrorAction("Page not found.", 404);
416             $cac->showPage();
417         }
418
419         return $match;
420     }
421
422     function build($action, $args=null, $params=null, $fragment=null)
423     {
424         $action_arg = array('action' => $action);
425
426         if ($args) {
427             $args = array_merge($action_arg, $args);
428         } else {
429             $args = $action_arg;
430         }
431
432         return $this->m->generate($args, $params, $fragment);
433     }
434 }