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