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