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