]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/router.php
Build urls using Net_URL_Mapper, too
[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
52     function __construct()
53     {
54         if (!$this->m) {
55             $this->m = $this->initialize();
56         }
57     }
58
59     function initialize() {
60
61         $m = Net_URL_Mapper::getInstance();
62
63         // In the "root"
64
65         $m->connect('', array('action' => 'public'));
66         $m->connect('rss', array('action' => 'publicrss'));
67         $m->connect('xrds', array('action' => 'publicxrds'));
68         $m->connect('featuredrss', array('action' => 'featuredrss'));
69         $m->connect('favoritedrss', array('action' => 'favoritedrss'));
70         $m->connect('opensearch/people', array('action' => 'opensearch',
71                                                'type' => 'people'));
72         $m->connect('opensearch/notice', array('action' => 'opensearch',
73                                                'type' => 'notice'));
74
75         // docs
76
77         $m->connect('doc/:title', array('action' => 'doc'));
78
79         // facebook
80
81         $m->connect('facebook', array('action' => 'facebookhome'));
82         $m->connect('facebook/index.php', array('action' => 'facebookhome'));
83         $m->connect('facebook/settings.php', array('action' => 'facebooksettings'));
84         $m->connect('facebook/invite.php', array('action' => 'facebookinvite'));
85         $m->connect('facebook/remove', array('action' => 'facebookremove'));
86
87         // main stuff is repetitive
88
89         $main = array('login', 'logout', 'register', 'subscribe',
90                       'unsubscribe', 'confirmaddress', 'recoverpassword',
91                       'invite', 'favor', 'disfavor', 'sup',
92                       'tagother', 'block');
93
94         foreach ($main as $a) {
95             $m->connect('main/'.$a, array('action' => $a));
96         }
97
98         // these take a code
99
100         foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
101             $m->connect('main/'.$c.'/:code', array('action' => $c));
102         }
103
104         // exceptional
105
106         $m->connect('main/openid', array('action' => 'openidlogin'));
107         $m->connect('main/remote', array('action' => 'remotesubscribe'));
108
109         // settings
110
111         foreach (array('profile', 'avatar', 'password', 'openid', 'im',
112                        'email', 'sms', 'twitter', 'other') as $s) {
113             $m->connect('settings/'.$s, array('action' => $s.'settings'));
114         }
115
116         // search
117
118         foreach (array('group', 'people', 'notice') as $s) {
119             $m->connect('search/'.$s, array('action' => $s.'search'));
120         }
121
122         $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
123
124         // notice
125
126         $m->connect('notice/new', array('action' => 'newnotice'));
127         $m->connect('notice/:notice',
128                     array('action' => 'shownotice'),
129                     array('notice' => '[0-9]+'));
130         $m->connect('notice/delete', array('action' => 'deletenotice'));
131         $m->connect('notice/delete/:notice',
132                     array('action' => 'deletenotice'),
133                     array('notice' => '[0-9]+'));
134
135         $m->connect('message/new', array('action' => 'newmessage'));
136         $m->connect('message/:message',
137                     array('action' => 'showmessage'),
138                     array('message' => '[0-9]+'));
139
140         $m->connect('user/:id',
141                     array('action' => 'userbyid'),
142                     array('id' => '[0-9]+'));
143
144         $m->connect('tags/', array('action' => 'publictagcloud'));
145         $m->connect('tag/', array('action' => 'publictagcloud'));
146         $m->connect('tags', array('action' => 'publictagcloud'));
147         $m->connect('tag', array('action' => 'publictagcloud'));
148         $m->connect('tag/:tag/rss',
149                     array('action' => 'tagrss'),
150                     array('tag' => '[a-zA-Z0-9]+'));
151         $m->connect('tag/:tag',
152                     array('action' => 'tag'),
153                     array('tag' => '[a-zA-Z0-9]+'));
154
155         $m->connect('peopletag/:tag',
156                     array('action' => 'peopletag'),
157                     array('tag' => '[a-zA-Z0-9]+'));
158
159         $m->connect('featured/', array('action' => 'featured'));
160         $m->connect('featured', array('action' => 'featured'));
161         $m->connect('favorited/', array('action' => 'favorited'));
162         $m->connect('favorited', array('action' => 'favorited'));
163
164         // groups
165
166         $m->connect('group/new', array('action' => 'newgroup'));
167
168         foreach (array('edit', 'join', 'leave') as $v) {
169             $m->connect('group/:nickname/'.$v,
170                         array('action' => $v.'group'),
171                         array('nickname' => '[a-zA-Z0-9]+'));
172         }
173
174         foreach (array('members', 'logo', 'rss') as $n) {
175             $m->connect('group/:nickname/'.$n,
176                         array('action' => 'group'.$n),
177                         array('nickname' => '[a-zA-Z0-9]+'));
178         }
179
180         $m->connect('group/:id/id',
181                     array('action' => 'groupbyid'),
182                     array('id' => '[0-9]+'));
183
184         $m->connect('group/:nickname',
185                     array('action' => 'showgroup'),
186                     array('nickname' => '[a-zA-Z0-9]+'));
187
188         $m->connect('group/', array('action' => 'groups'));
189         $m->connect('group', array('action' => 'groups'));
190         $m->connect('groups/', array('action' => 'groups'));
191         $m->connect('groups', array('action' => 'groups'));
192
193         // Twitter-compatible API
194
195         // statuses API
196
197         $m->connect('api/statuses/:method',
198                     array('action' => 'api',
199                           'apiaction' => 'statuses'),
200                     array('method' => '(public_timeline|friends_timeline|user_timeline|update|replies|friends|followers|featured)(\.(atom|rss|xml|json))?'));
201
202         $m->connect('api/statuses/:method/:argument',
203                     array('action' => 'api',
204                           'apiaction' => 'statuses'),
205                     array('method' => '(user_timeline|show|destroy|friends|followers)'));
206
207         // users
208
209         $m->connect('api/users/show/:argument',
210                     array('action' => 'api',
211                           'apiaction' => 'users'));
212
213         $m->connect('api/users/:method',
214                     array('action' => 'api',
215                           'apiaction' => 'users'),
216                     array('method' => 'show(\.(xml|json|atom|rss))?'));
217
218         // direct messages
219
220         $m->connect('api/direct_messages/:method',
221                     array('action' => 'api',
222                           'apiaction' => 'direct_messages'),
223                     array('method' => '(sent|new)(\.(xml|json|atom|rss))?'));
224
225         $m->connect('api/direct_messages/destroy/:argument',
226                     array('action' => 'api',
227                           'apiaction' => 'direct_messages'));
228
229         $m->connect('api/:method',
230                     array('action' => 'api',
231                           'apiaction' => 'direct_messages'),
232                     array('method' => 'direct_messages(\.(xml|json|atom|rss))?'));
233
234         // friendships
235
236         $m->connect('api/friendships/:method/:argument',
237                     array('action' => 'api',
238                           'apiaction' => 'friendships'),
239                     array('method' => '(create|destroy)'));
240
241         $m->connect('api/friendships/:method',
242                     array('action' => 'api',
243                           'apiaction' => 'friendships'),
244                     array('method' => 'exists(\.(xml|json|rss|atom))'));
245
246         // account
247
248         $m->connect('api/account/:method',
249                     array('action' => 'api',
250                           'apiaction' => 'account'));
251
252         // favorites
253
254         $m->connect('api/favorites/:method/:argument',
255                     array('action' => 'api',
256                           'apiaction' => 'favorites'));
257
258         $m->connect('api/favorites/:argument',
259                     array('action' => 'api',
260                           'apiaction' => 'favorites',
261                           'method' => 'favorites'));
262
263         $m->connect('api/:method',
264                     array('action' => 'api',
265                           'apiaction' => 'favorites'),
266                     array('method' => 'favorites(\.(xml|json|rss|atom))?'));
267
268         // notifications
269
270         $m->connect('api/notifications/:method/:argument',
271                     array('action' => 'api',
272                           'apiaction' => 'favorites'));
273
274         // blocks
275
276         $m->connect('api/blocks/:method/:argument',
277                     array('action' => 'api',
278                           'apiaction' => 'blocks'));
279
280         // help
281
282         $m->connect('api/help/:method',
283                     array('action' => 'api',
284                           'apiaction' => 'help'));
285
286         // laconica
287
288         $m->connect('api/laconica/:method',
289                     array('action' => 'api',
290                           'apiaction' => 'laconica'));
291
292         // user stuff
293
294         foreach (array('subscriptions', 'subscribers',
295                        'nudge', 'xrds', 'all', 'foaf',
296                        'replies', 'inbox', 'outbox', 'microsummary') as $a) {
297             $m->connect(':nickname/'.$a,
298                         array('action' => $a),
299                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
300         }
301
302         foreach (array('subscriptions', 'subscribers') as $a) {
303             $m->connect(':nickname/'.$a.'/:tag',
304                         array('action' => $a),
305                         array('tag' => '[a-zA-Z0-9]+',
306                               'nickname' => '[a-zA-Z0-9]{1,64}'));
307         }
308
309         foreach (array('rss', 'groups') as $a) {
310             $m->connect(':nickname/'.$a,
311                         array('action' => 'user'.$a),
312                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
313         }
314
315         foreach (array('all', 'replies', 'favorites') as $a) {
316             $m->connect(':nickname/'.$a.'/rss',
317                         array('action' => $a.'rss'),
318                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
319         }
320
321         $m->connect(':nickname/favorites',
322                     array('action' => 'showfavorites'),
323                     array('nickname' => '[a-zA-Z0-9]{1,64}'));
324
325         $m->connect(':nickname/avatar/:size',
326                     array('action' => 'avatarbynickname'),
327                     array('size' => '(original|96|48|24)',
328                           'nickname' => '[a-zA-Z0-9]{1,64}'));
329
330         $m->connect(':nickname',
331                     array('action' => 'showstream'),
332                     array('nickname' => '[a-zA-Z0-9]{1,64}'));
333
334         return $m;
335     }
336
337     function map($path)
338     {
339         return $this->m->match($path);
340     }
341
342     function build($action, $args=null, $fragment=null)
343     {
344         $action_arg = array('action' => $action);
345
346         if ($args) {
347             $args = array_merge($args, $action_arg);
348         } else {
349             $args = $action_arg;
350         }
351
352         return $this->m->generate($args, null, $fragment);
353     }
354 }