]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/router.php
start conversation action
[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         // conversation
147
148         $m->connect('conversation/:id',
149                     array('action' => 'conversation'),
150                     array('id' => '[0-9]+'));
151
152         $m->connect('message/new', array('action' => 'newmessage'));
153         $m->connect('message/:message',
154                     array('action' => 'showmessage'),
155                     array('message' => '[0-9]+'));
156
157         $m->connect('user/:id',
158                     array('action' => 'userbyid'),
159                     array('id' => '[0-9]+'));
160
161         $m->connect('tags/', array('action' => 'publictagcloud'));
162         $m->connect('tag/', array('action' => 'publictagcloud'));
163         $m->connect('tags', array('action' => 'publictagcloud'));
164         $m->connect('tag', array('action' => 'publictagcloud'));
165         $m->connect('tag/:tag/rss',
166                     array('action' => 'tagrss'),
167                     array('tag' => '[a-zA-Z0-9]+'));
168         $m->connect('tag/:tag',
169                     array('action' => 'tag'),
170                     array('tag' => '[a-zA-Z0-9]+'));
171
172         $m->connect('peopletag/:tag',
173                     array('action' => 'peopletag'),
174                     array('tag' => '[a-zA-Z0-9]+'));
175
176         $m->connect('featured/', array('action' => 'featured'));
177         $m->connect('featured', array('action' => 'featured'));
178         $m->connect('favorited/', array('action' => 'favorited'));
179         $m->connect('favorited', array('action' => 'favorited'));
180
181         // groups
182
183         $m->connect('group/new', array('action' => 'newgroup'));
184
185         foreach (array('edit', 'join', 'leave') as $v) {
186             $m->connect('group/:nickname/'.$v,
187                         array('action' => $v.'group'),
188                         array('nickname' => '[a-zA-Z0-9]+'));
189         }
190
191         foreach (array('members', 'logo', 'rss') as $n) {
192             $m->connect('group/:nickname/'.$n,
193                         array('action' => 'group'.$n),
194                         array('nickname' => '[a-zA-Z0-9]+'));
195         }
196
197         $m->connect('group/:id/id',
198                     array('action' => 'groupbyid'),
199                     array('id' => '[0-9]+'));
200
201         $m->connect('group/:nickname',
202                     array('action' => 'showgroup'),
203                     array('nickname' => '[a-zA-Z0-9]+'));
204
205         $m->connect('group/', array('action' => 'groups'));
206         $m->connect('group', array('action' => 'groups'));
207         $m->connect('groups/', array('action' => 'groups'));
208         $m->connect('groups', array('action' => 'groups'));
209
210         // Twitter-compatible API
211
212         // statuses API
213
214         $m->connect('api/statuses/:method',
215                     array('action' => 'api',
216                           'apiaction' => 'statuses'),
217                     array('method' => '(public_timeline|friends_timeline|user_timeline|update|replies|friends|followers|featured)(\.(atom|rss|xml|json))?'));
218
219         $m->connect('api/statuses/:method/:argument',
220                     array('action' => 'api',
221                           'apiaction' => 'statuses'),
222                     array('method' => '(user_timeline|friends_timeline|show|destroy|friends|followers)'));
223
224         // users
225
226         $m->connect('api/users/show/:argument',
227                     array('action' => 'api',
228                           'apiaction' => 'users'));
229
230         $m->connect('api/users/:method',
231                     array('action' => 'api',
232                           'apiaction' => 'users'),
233                     array('method' => 'show(\.(xml|json|atom|rss))?'));
234
235         // direct messages
236
237         foreach (array('xml', 'json') as $e) {
238             $m->connect('api/direct_messages/new.'.$e,
239                         array('action' => 'api',
240                               'apiaction' => 'direct_messages',
241                               'method' => 'create.'.$e));
242         }
243
244         foreach (array('xml', 'json', 'rss', 'atom') as $e) {
245             $m->connect('api/direct_messages.'.$e,
246                         array('action' => 'api',
247                               'apiaction' => 'direct_messages',
248                               'method' => 'direct_messages.'.$e));
249         }
250
251         foreach (array('xml', 'json', 'rss', 'atom') as $e) {
252             $m->connect('api/direct_message/sent.'.$e,
253                         array('action' => 'api',
254                         'apiaction' => 'direct_messages',
255                         'method' => 'sent.'.$e));
256         }
257
258         $m->connect('api/direct_messages/destroy/:argument',
259                     array('action' => 'api',
260                           'apiaction' => 'direct_messages'));
261
262         // friendships
263
264         $m->connect('api/friendships/:method/:argument',
265                     array('action' => 'api',
266                           'apiaction' => 'friendships'),
267                     array('method' => '(create|destroy)'));
268
269         $m->connect('api/friendships/:method',
270                     array('action' => 'api',
271                           'apiaction' => 'friendships'),
272                     array('method' => 'exists(\.(xml|json|rss|atom))'));
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 }