]> 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     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|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         // account
269
270         $m->connect('api/account/:method',
271                     array('action' => 'api',
272                           'apiaction' => 'account'));
273
274         // favorites
275
276         $m->connect('api/favorites/:method/:argument',
277                     array('action' => 'api',
278                           'apiaction' => 'favorites'));
279
280         $m->connect('api/favorites/:argument',
281                     array('action' => 'api',
282                           'apiaction' => 'favorites',
283                           'method' => 'favorites'));
284
285         foreach (array('xml', 'json', 'rss', 'atom') as $e) {
286             $m->connect('api/favorites.'.$e,
287                 array('action' => 'api',
288                       'apiaction' => 'favorites',
289                       'method' => 'favorites.'.$e));
290         }
291
292         // notifications
293
294         $m->connect('api/notifications/:method/:argument',
295                     array('action' => 'api',
296                           'apiaction' => 'favorites'));
297
298         // blocks
299
300         $m->connect('api/blocks/:method/:argument',
301                     array('action' => 'api',
302                           'apiaction' => 'blocks'));
303
304         // help
305
306         $m->connect('api/help/:method',
307                     array('action' => 'api',
308                           'apiaction' => 'help'));
309
310         // laconica
311
312         $m->connect('api/laconica/:method',
313                     array('action' => 'api',
314                           'apiaction' => 'laconica'));
315
316         // user stuff
317
318         foreach (array('subscriptions', 'subscribers',
319                        'nudge', 'xrds', 'all', 'foaf',
320                        'replies', 'inbox', 'outbox', 'microsummary') as $a) {
321             $m->connect(':nickname/'.$a,
322                         array('action' => $a),
323                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
324         }
325
326         foreach (array('subscriptions', 'subscribers') as $a) {
327             $m->connect(':nickname/'.$a.'/:tag',
328                         array('action' => $a),
329                         array('tag' => '[a-zA-Z0-9]+',
330                               'nickname' => '[a-zA-Z0-9]{1,64}'));
331         }
332
333         foreach (array('rss', 'groups') as $a) {
334             $m->connect(':nickname/'.$a,
335                         array('action' => 'user'.$a),
336                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
337         }
338
339         foreach (array('all', 'replies', 'favorites') as $a) {
340             $m->connect(':nickname/'.$a.'/rss',
341                         array('action' => $a.'rss'),
342                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
343         }
344
345         $m->connect(':nickname/favorites',
346                     array('action' => 'showfavorites'),
347                     array('nickname' => '[a-zA-Z0-9]{1,64}'));
348
349         $m->connect(':nickname/avatar/:size',
350                     array('action' => 'avatarbynickname'),
351                     array('size' => '(original|96|48|24)',
352                           'nickname' => '[a-zA-Z0-9]{1,64}'));
353
354         $m->connect(':nickname',
355                     array('action' => 'showstream'),
356                     array('nickname' => '[a-zA-Z0-9]{1,64}'));
357
358         return $m;
359     }
360
361     function map($path)
362     {
363         try {
364             $match = $this->m->match($path);
365         } catch (Net_URL_Mapper_InvalidException $e) {
366             common_log(LOG_ERR, "Problem getting route for $path - " .
367                 $e->getMessage());
368             $cac = new ClientErrorAction("Page not found.", 404);
369             $cac->showPage();
370         }
371
372         return $match;
373     }
374
375     function build($action, $args=null, $params=null, $fragment=null)
376     {
377         $action_arg = array('action' => $action);
378
379         if ($args) {
380             $args = array_merge($action_arg, $args);
381         } else {
382             $args = $action_arg;
383         }
384
385         return $this->m->generate($args, $params, $fragment);
386     }
387 }