]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/router.php
Adds some missing routes.
[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
53     static function get()
54     {
55         if (!Router::$inst) {
56             Router::$inst = new Router();
57         }
58         return Router::$inst;
59     }
60
61     function __construct()
62     {
63         if (!$this->m) {
64             $this->m = $this->initialize();
65         }
66     }
67
68     function initialize() {
69
70         $m = Net_URL_Mapper::getInstance();
71
72         // In the "root"
73
74         $m->connect('', array('action' => 'public'));
75         $m->connect('rss', array('action' => 'publicrss'));
76         $m->connect('xrds', array('action' => 'publicxrds'));
77         $m->connect('featuredrss', array('action' => 'featuredrss'));
78         $m->connect('favoritedrss', array('action' => 'favoritedrss'));
79         $m->connect('opensearch/people', array('action' => 'opensearch',
80                                                'type' => 'people'));
81         $m->connect('opensearch/notice', array('action' => 'opensearch',
82                                                'type' => 'notice'));
83
84         // docs
85
86         $m->connect('doc/:title', array('action' => 'doc'));
87
88         // facebook
89
90         $m->connect('facebook', array('action' => 'facebookhome'));
91         $m->connect('facebook/index.php', array('action' => 'facebookhome'));
92         $m->connect('facebook/settings.php', array('action' => 'facebooksettings'));
93         $m->connect('facebook/invite.php', array('action' => 'facebookinvite'));
94         $m->connect('facebook/remove', array('action' => 'facebookremove'));
95
96         // main stuff is repetitive
97
98         $main = array('login', 'logout', 'register', 'subscribe',
99                       'unsubscribe', 'confirmaddress', 'recoverpassword',
100                       'invite', 'favor', 'disfavor', 'sup',
101                       'block');
102
103         foreach ($main as $a) {
104             $m->connect('main/'.$a, array('action' => $a));
105         }
106
107         $m->connect('main/tagother/:id', array('action' => 'tagother'));
108
109         // these take a code
110
111         foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
112             $m->connect('main/'.$c.'/:code', array('action' => $c));
113         }
114
115         // exceptional
116
117         $m->connect('main/openid', array('action' => 'openidlogin'));
118         $m->connect('main/remote', array('action' => 'remotesubscribe'));
119
120         foreach (array('requesttoken', 'accesstoken', 'userauthorization',
121                     'postnotice', 'updateprofile') as $action) {
122             $m->connect('index.php?action=' . $action, array('action' => $action));
123         }
124
125         // settings
126
127         foreach (array('profile', 'avatar', 'password', 'openid', 'im',
128                        'email', 'sms', 'twitter', 'other') as $s) {
129             $m->connect('settings/'.$s, array('action' => $s.'settings'));
130         }
131
132         // search
133
134         foreach (array('group', 'people', 'notice') as $s) {
135             $m->connect('search/'.$s, array('action' => $s.'search'));
136         }
137
138         $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
139
140         // notice
141
142         $m->connect('notice/new', array('action' => 'newnotice'));
143         $m->connect('notice/:notice',
144                     array('action' => 'shownotice'),
145                     array('notice' => '[0-9]+'));
146         $m->connect('notice/delete', array('action' => 'deletenotice'));
147         $m->connect('notice/delete/:notice',
148                     array('action' => 'deletenotice'),
149                     array('notice' => '[0-9]+'));
150
151         $m->connect('message/new', array('action' => 'newmessage'));
152         $m->connect('message/:message',
153                     array('action' => 'showmessage'),
154                     array('message' => '[0-9]+'));
155
156         $m->connect('user/:id',
157                     array('action' => 'userbyid'),
158                     array('id' => '[0-9]+'));
159
160         $m->connect('tags/', array('action' => 'publictagcloud'));
161         $m->connect('tag/', array('action' => 'publictagcloud'));
162         $m->connect('tags', array('action' => 'publictagcloud'));
163         $m->connect('tag', array('action' => 'publictagcloud'));
164         $m->connect('tag/:tag/rss',
165                     array('action' => 'tagrss'),
166                     array('tag' => '[a-zA-Z0-9]+'));
167         $m->connect('tag/:tag',
168                     array('action' => 'tag'),
169                     array('tag' => '[a-zA-Z0-9]+'));
170
171         $m->connect('peopletag/:tag',
172                     array('action' => 'peopletag'),
173                     array('tag' => '[a-zA-Z0-9]+'));
174
175         $m->connect('featured/', array('action' => 'featured'));
176         $m->connect('featured', array('action' => 'featured'));
177         $m->connect('favorited/', array('action' => 'favorited'));
178         $m->connect('favorited', array('action' => 'favorited'));
179
180         // groups
181
182         $m->connect('group/new', array('action' => 'newgroup'));
183
184         foreach (array('edit', 'join', 'leave') as $v) {
185             $m->connect('group/:nickname/'.$v,
186                         array('action' => $v.'group'),
187                         array('nickname' => '[a-zA-Z0-9]+'));
188         }
189
190         foreach (array('members', 'logo', 'rss') as $n) {
191             $m->connect('group/:nickname/'.$n,
192                         array('action' => 'group'.$n),
193                         array('nickname' => '[a-zA-Z0-9]+'));
194         }
195
196         $m->connect('group/:id/id',
197                     array('action' => 'groupbyid'),
198                     array('id' => '[0-9]+'));
199
200         $m->connect('group/:nickname',
201                     array('action' => 'showgroup'),
202                     array('nickname' => '[a-zA-Z0-9]+'));
203
204         $m->connect('group/', array('action' => 'groups'));
205         $m->connect('group', array('action' => 'groups'));
206         $m->connect('groups/', array('action' => 'groups'));
207         $m->connect('groups', array('action' => 'groups'));
208
209         // Twitter-compatible API
210
211         // statuses API
212
213         $m->connect('api/statuses/:method',
214                     array('action' => 'api',
215                           'apiaction' => 'statuses'),
216                     array('method' => '(public_timeline|friends_timeline|user_timeline|update|replies|friends|followers|featured)(\.(atom|rss|xml|json))?'));
217
218         $m->connect('api/statuses/:method/:argument',
219                     array('action' => 'api',
220                           'apiaction' => 'statuses'),
221                     array('method' => '(user_timeline|friends_timeline|show|destroy|friends|followers)'));
222
223         // users
224
225         $m->connect('api/users/show/:argument',
226                     array('action' => 'api',
227                           'apiaction' => 'users'));
228
229         $m->connect('api/users/:method',
230                     array('action' => 'api',
231                           'apiaction' => 'users'),
232                     array('method' => 'show(\.(xml|json|atom|rss))?'));
233
234         // direct messages
235
236         foreach (array('xml', 'json') as $e) {
237             $m->connect('api/direct_messages/new.'.$e,
238                         array('action' => 'api',
239                               'apiaction' => 'direct_messages',
240                               'method' => 'create.'.$e));
241         }
242
243         foreach (array('xml', 'json', 'rss', 'atom') as $e) {
244             $m->connect('api/direct_messages.'.$e,
245                         array('action' => 'api',
246                               'apiaction' => 'direct_messages',
247                               'method' => 'direct_messages.'.$e));
248         }
249
250         foreach (array('xml', 'json', 'rss', 'atom') as $e) {
251             $m->connect('api/direct_message/sent.'.$e,
252                         array('action' => 'api',
253                         'apiaction' => 'direct_messages',
254                         'method' => 'sent.'.$e));
255         }
256
257         $m->connect('api/direct_messages/destroy/:argument',
258                     array('action' => 'api',
259                           'apiaction' => 'direct_messages'));
260
261         // friendships
262
263         $m->connect('api/friendships/:method/:argument',
264                     array('action' => 'api',
265                           'apiaction' => 'friendships'),
266                     array('method' => '(create|destroy)'));
267
268         $m->connect('api/friendships/:method',
269                     array('action' => 'api',
270                           'apiaction' => 'friendships'),
271                     array('method' => 'exists(\.(xml|json|rss|atom))'));
272
273
274         // Social graph
275
276         $m->connect('api/friends/ids/:argument',
277                     array('action' => 'api',
278                           'apiaction' => 'statuses',
279                           'method' => 'friendsIDs'));
280                                                    
281         foreach (array('xml', 'json') as $e) {
282             $m->connect('api/friends/ids.'.$e,
283                         array('action' => 'api',
284                               'apiaction' => 'statuses',
285                               'method' => 'friendsIDs.'.$e));
286         }
287                                                     
288         $m->connect('api/followers/ids/:argument',
289                     array('action' => 'api',
290                           'apiaction' => 'statuses',
291                           'method' => 'followersIDs'));
292
293         foreach (array('xml', 'json') as $e) {
294             $m->connect('api/followers/ids.'.$e,
295                         array('action' => 'api',
296                               'apiaction' => 'statuses',
297                               'method' => 'followersIDs.'.$e));
298         }
299
300         // account
301
302         $m->connect('api/account/:method',
303                     array('action' => 'api',
304                           'apiaction' => 'account'));
305
306         // favorites
307
308         $m->connect('api/favorites/:method/:argument',
309                     array('action' => 'api',
310                           'apiaction' => 'favorites'));
311
312         $m->connect('api/favorites/:argument',
313                     array('action' => 'api',
314                           'apiaction' => 'favorites',
315                           'method' => 'favorites'));
316
317         foreach (array('xml', 'json', 'rss', 'atom') as $e) {
318             $m->connect('api/favorites.'.$e,
319                 array('action' => 'api',
320                       'apiaction' => 'favorites',
321                       'method' => 'favorites.'.$e));
322         }
323
324         // notifications
325
326         $m->connect('api/notifications/:method/:argument',
327                     array('action' => 'api',
328                           'apiaction' => 'favorites'));
329
330         // blocks
331
332         $m->connect('api/blocks/:method/:argument',
333                     array('action' => 'api',
334                           'apiaction' => 'blocks'));
335
336         // help
337
338         $m->connect('api/help/:method',
339                     array('action' => 'api',
340                           'apiaction' => 'help'));
341
342         // laconica
343
344         $m->connect('api/laconica/:method',
345                     array('action' => 'api',
346                           'apiaction' => 'laconica'));
347
348         // user stuff
349
350         foreach (array('subscriptions', 'subscribers',
351                        'nudge', 'xrds', 'all', 'foaf',
352                        'replies', 'inbox', 'outbox', 'microsummary') as $a) {
353             $m->connect(':nickname/'.$a,
354                         array('action' => $a),
355                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
356         }
357
358         foreach (array('subscriptions', 'subscribers') as $a) {
359             $m->connect(':nickname/'.$a.'/:tag',
360                         array('action' => $a),
361                         array('tag' => '[a-zA-Z0-9]+',
362                               'nickname' => '[a-zA-Z0-9]{1,64}'));
363         }
364
365         foreach (array('rss', 'groups') as $a) {
366             $m->connect(':nickname/'.$a,
367                         array('action' => 'user'.$a),
368                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
369         }
370
371         foreach (array('all', 'replies', 'favorites') as $a) {
372             $m->connect(':nickname/'.$a.'/rss',
373                         array('action' => $a.'rss'),
374                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
375         }
376
377         $m->connect(':nickname/favorites',
378                     array('action' => 'showfavorites'),
379                     array('nickname' => '[a-zA-Z0-9]{1,64}'));
380
381         $m->connect(':nickname/avatar/:size',
382                     array('action' => 'avatarbynickname'),
383                     array('size' => '(original|96|48|24)',
384                           'nickname' => '[a-zA-Z0-9]{1,64}'));
385
386         $m->connect(':nickname',
387                     array('action' => 'showstream'),
388                     array('nickname' => '[a-zA-Z0-9]{1,64}'));
389
390         return $m;
391     }
392
393     function map($path)
394     {
395         try {
396             $match = $this->m->match($path);
397         } catch (Net_URL_Mapper_InvalidException $e) {
398             common_log(LOG_ERR, "Problem getting route for $path - " .
399                 $e->getMessage());
400             $cac = new ClientErrorAction("Page not found.", 404);
401             $cac->showPage();
402         }
403
404         return $match;
405     }
406
407     function build($action, $args=null, $params=null, $fragment=null)
408     {
409         $action_arg = array('action' => $action);
410
411         if ($args) {
412             $args = array_merge($action_arg, $args);
413         } else {
414             $args = $action_arg;
415         }
416
417         return $this->m->generate($args, $params, $fragment);
418     }
419 }