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