forked from joomla/joomla-platform
/
router.php
477 lines (428 loc) · 8.49 KB
/
router.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
/**
* @package Joomla.Platform
* @subpackage Application
*
* @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
defined('JPATH_PLATFORM') or die();
/**
* Set the available masks for the routing mode
*/
define('JROUTER_MODE_RAW', 0);
define('JROUTER_MODE_SEF', 1);
/**
* Class to create and parse routes
*
* @package Joomla.Platform
* @subpackage Application
* @since 11.1
*/
class JRouter extends JObject
{
/**
* The rewrite mode
*
* @var integer
* @since 11.1
*/
protected $_mode = null;
/**
* An array of variables
*
* @var array
* @since 11.1
*/
protected $_vars = array();
/**
* An array of rules
*
* @var array
* @since 11.1
*/
protected $_rules = array('build' => array(), 'parse' => array());
/**
* Class constructor
*
* @param array $options Array of options
*
* @return void
*
* @since 11.1
*/
public function __construct($options = array())
{
if (array_key_exists('mode', $options))
{
$this->_mode = $options['mode'];
}
else
{
$this->_mode = JROUTER_MODE_RAW;
}
}
/**
* Returns the global JRouter object, only creating it if it
* doesn't already exist.
*
* @param string $client The name of the client
* @param array $options An associative array of options
*
* @return JRouter A JRouter object.
*
* @since 11.1
*/
public static function getInstance($client, $options = array())
{
static $instances;
if (!isset($instances))
{
$instances = array();
}
if (empty($instances[$client]))
{
// Load the router object
$info = JApplicationHelper::getClientInfo($client, true);
$path = $info->path . '/includes/router.php';
if (file_exists($path))
{
include_once $path;
// Create a JRouter object
$classname = 'JRouter' . ucfirst($client);
$instance = new $classname($options);
}
else
{
$error = JError::raiseError(500, JText::sprintf('JLIB_APPLICATION_ERROR_ROUTER_LOAD', $client));
return $error;
}
$instances[$client] = & $instance;
}
return $instances[$client];
}
/**
* Function to convert a route to an internal URI
*
* @param JURI &$uri The uri.
*
* @return array
*
* @since 11.1
*/
public function parse(&$uri)
{
$vars = array();
// Process the parsed variables based on custom defined rules
$vars = $this->_processParseRules($uri);
// Parse RAW URL
if ($this->_mode == JROUTER_MODE_RAW)
{
$vars += $this->_parseRawRoute($uri);
}
// Parse SEF URL
if ($this->_mode == JROUTER_MODE_SEF)
{
$vars += $this->_parseSefRoute($uri);
}
return array_merge($this->getVars(), $vars);
}
/**
* Function to convert an internal URI to a route
*
* @param string $url The internal URL
*
* @return string The absolute search engine friendly URL
*
* @since 11.1
*/
public function build($url)
{
// Create the URI object
$uri = $this->_createURI($url);
// Process the uri information based on custom defined rules
$this->_processBuildRules($uri);
// Build RAW URL
if ($this->_mode == JROUTER_MODE_RAW)
{
$this->_buildRawRoute($uri);
}
// Build SEF URL : mysite/route/index.php?var=x
if ($this->_mode == JROUTER_MODE_SEF)
{
$this->_buildSefRoute($uri);
}
return $uri;
}
/**
* Get the router mode
*
* @return integer
*
* @since 11.1
*/
public function getMode()
{
return $this->_mode;
}
/**
* Set the router mode
*
* @param integer $mode The routing mode.
*
* @return void
*
* @since 11.1
*/
public function setMode($mode)
{
$this->_mode = $mode;
}
/**
* Set a router variable, creating it if it doesn't exist
*
* @param string $key The name of the variable
* @param mixed $value The value of the variable
* @param boolean $create If True, the variable will be created if it doesn't exist yet
*
* @return void
*
* @since 11.1
*/
public function setVar($key, $value, $create = true)
{
if ($create || array_key_exists($key, $this->_vars))
{
$this->_vars[$key] = $value;
}
}
/**
* Set the router variable array
*
* @param array $vars An associative array with variables
* @param boolean $merge If True, the array will be merged instead of overwritten
*
* @return void
*
* @since 11.1
*/
public function setVars($vars = array(), $merge = true)
{
if ($merge)
{
$this->_vars = array_merge($this->_vars, $vars);
}
else
{
$this->_vars = $vars;
}
}
/**
* Get a router variable
*
* @param string $key The name of the variable
*
* @return mixed Value of the variable
*
* @since 11.1
*/
public function getVar($key)
{
$result = null;
if (isset($this->_vars[$key]))
{
$result = $this->_vars[$key];
}
return $result;
}
/**
* Get the router variable array
*
* @return array An associative array of router variables
*
* @since 11.1
*/
public function getVars()
{
return $this->_vars;
}
/**
* Attach a build rule
*
* @param callback $callback The function to be called
*
* @return void
*
* @since 11.1.
*/
public function attachBuildRule($callback)
{
$this->_rules['build'][] = $callback;
}
/**
* Attach a parse rule
*
* @param callback $callback The function to be called.
*
* @return void
*
* @since 11.1
*/
public function attachParseRule($callback)
{
$this->_rules['parse'][] = $callback;
}
/**
* Function to convert a raw route to an internal URI
*
* @param JURI &$uri The raw route
*
* @return boolean
*
* @since 11.1
*/
protected function _parseRawRoute(&$uri)
{
return false;
}
/**
* Function to convert a sef route to an internal URI
*
* @param JURI &$uri The sef URI
*
* @return string Internal URI
*
* @since 11.1
*/
protected function _parseSefRoute(&$uri)
{
return false;
}
/**
* Function to build a raw route
*
* @param JURI &$uri The internal URL
*
* @return string Raw Route
*
* @since 11.1
*/
protected function _buildRawRoute(&$uri)
{
}
/**
* Function to build a sef route
*
* @param JURI &$uri The uri
*
* @return string The SEF route
*
* @since 11.1
*/
protected function _buildSefRoute(&$uri)
{
}
/**
* Process the parsed router variables based on custom defined rules
*
* @param JURI &$uri The URI to parse
*
* @return array The array of processed URI variables
*
* @since 11.1
*/
protected function _processParseRules(&$uri)
{
$vars = array();
foreach ($this->_rules['parse'] as $rule)
{
$vars += call_user_func_array($rule, array(&$this, &$uri));
}
return $vars;
}
/**
* Process the build uri query data based on custom defined rules
*
* @param JURI &$uri The URI
*
* @return void
*
* @since 11.1
*/
protected function _processBuildRules(&$uri)
{
foreach ($this->_rules['build'] as $rule)
{
call_user_func_array($rule, array(&$this, &$uri));
}
}
/**
* Create a uri based on a full or partial url string
*
* @param string $url The URI
*
* @return JURI
*
* @since 11.1
*/
protected function _createURI($url)
{
// Create full URL if we are only appending variables to it
if (substr($url, 0, 1) == '&')
{
$vars = array();
if (strpos($url, '&') !== false)
{
$url = str_replace('&', '&', $url);
}
parse_str($url, $vars);
$vars = array_merge($this->getVars(), $vars);
foreach ($vars as $key => $var)
{
if ($var == "")
{
unset($vars[$key]);
}
}
$url = 'index.php?' . JURI::buildQuery($vars);
}
// Decompose link into url component parts
return new JURI($url);
}
/**
* Encode route segments
*
* @param array $segments An array of route segments
*
* @return array Array of encoded route segments
*
* @since 11.1
*/
protected function _encodeSegments($segments)
{
$total = count($segments);
for ($i = 0; $i < $total; $i++)
{
$segments[$i] = str_replace(':', '-', $segments[$i]);
}
return $segments;
}
/**
* Decode route segments
*
* @param array $segments An array of route segments
*
* @return array Array of decoded route segments
*
* @since 11,1
*/
protected function _decodeSegments($segments)
{
$total = count($segments);
for ($i = 0; $i < $total; $i++)
{
$segments[$i] = preg_replace('/-/', ':', $segments[$i], 1);
}
return $segments;
}
}