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