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