]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/router.php
Change common_local_url() to take 4 arguments
[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         $m->connect('api/direct_messages/:method',
232                     array('action' => 'api',
233                           'apiaction' => 'direct_messages'),
234                     array('method' => '(sent|new)(\.(xml|json|atom|rss))?'));
235
236         $m->connect('api/direct_messages/destroy/:argument',
237                     array('action' => 'api',
238                           'apiaction' => 'direct_messages'));
239
240         $m->connect('api/:method',
241                     array('action' => 'api',
242                           'apiaction' => 'direct_messages'),
243                     array('method' => 'direct_messages(\.(xml|json|atom|rss))?'));
244
245         // friendships
246
247         $m->connect('api/friendships/:method/:argument',
248                     array('action' => 'api',
249                           'apiaction' => 'friendships'),
250                     array('method' => '(create|destroy)'));
251
252         $m->connect('api/friendships/:method',
253                     array('action' => 'api',
254                           'apiaction' => 'friendships'),
255                     array('method' => 'exists(\.(xml|json|rss|atom))'));
256
257         // account
258
259         $m->connect('api/account/:method',
260                     array('action' => 'api',
261                           'apiaction' => 'account'));
262
263         // favorites
264
265         $m->connect('api/favorites/:method/:argument',
266                     array('action' => 'api',
267                           'apiaction' => 'favorites'));
268
269         $m->connect('api/favorites/:argument',
270                     array('action' => 'api',
271                           'apiaction' => 'favorites',
272                           'method' => 'favorites'));
273
274         $m->connect('api/:method',
275                     array('action' => 'api',
276                           'apiaction' => 'favorites'),
277                     array('method' => 'favorites(\.(xml|json|rss|atom))?'));
278
279         // notifications
280
281         $m->connect('api/notifications/:method/:argument',
282                     array('action' => 'api',
283                           'apiaction' => 'favorites'));
284
285         // blocks
286
287         $m->connect('api/blocks/:method/:argument',
288                     array('action' => 'api',
289                           'apiaction' => 'blocks'));
290
291         // help
292
293         $m->connect('api/help/:method',
294                     array('action' => 'api',
295                           'apiaction' => 'help'));
296
297         // laconica
298
299         $m->connect('api/laconica/:method',
300                     array('action' => 'api',
301                           'apiaction' => 'laconica'));
302
303         // user stuff
304
305         foreach (array('subscriptions', 'subscribers',
306                        'nudge', 'xrds', 'all', 'foaf',
307                        'replies', 'inbox', 'outbox', 'microsummary') as $a) {
308             $m->connect(':nickname/'.$a,
309                         array('action' => $a),
310                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
311         }
312
313         foreach (array('subscriptions', 'subscribers') as $a) {
314             $m->connect(':nickname/'.$a.'/:tag',
315                         array('action' => $a),
316                         array('tag' => '[a-zA-Z0-9]+',
317                               'nickname' => '[a-zA-Z0-9]{1,64}'));
318         }
319
320         foreach (array('rss', 'groups') as $a) {
321             $m->connect(':nickname/'.$a,
322                         array('action' => 'user'.$a),
323                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
324         }
325
326         foreach (array('all', 'replies', 'favorites') as $a) {
327             $m->connect(':nickname/'.$a.'/rss',
328                         array('action' => $a.'rss'),
329                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
330         }
331
332         $m->connect(':nickname/favorites',
333                     array('action' => 'showfavorites'),
334                     array('nickname' => '[a-zA-Z0-9]{1,64}'));
335
336         $m->connect(':nickname/avatar/:size',
337                     array('action' => 'avatarbynickname'),
338                     array('size' => '(original|96|48|24)',
339                           'nickname' => '[a-zA-Z0-9]{1,64}'));
340
341         $m->connect(':nickname',
342                     array('action' => 'showstream'),
343                     array('nickname' => '[a-zA-Z0-9]{1,64}'));
344
345         return $m;
346     }
347
348     function map($path)
349     {
350         return $this->m->match($path);
351     }
352
353     function build($action, $args=null, $params=null, $fragment=null)
354     {
355         $action_arg = array('action' => $action);
356
357         if ($args) {
358             $args = array_merge($action_arg, $args);
359         } else {
360             $args = $action_arg;
361         }
362
363         return $this->m->generate($args, $params, $fragment);
364     }
365 }