| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* SVN FILE: $Id: router.php 8004 2009-01-16 20:15:21Z gwoo $ */
|
|
|
3 |
/**
|
|
|
4 |
* Parses the request URL into controller, action, and parameters.
|
|
|
5 |
*
|
|
|
6 |
* Long description for file
|
|
|
7 |
*
|
|
|
8 |
* PHP versions 4 and 5
|
|
|
9 |
*
|
|
|
10 |
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
|
|
11 |
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
12 |
*
|
|
|
13 |
* Licensed under The MIT License
|
|
|
14 |
* Redistributions of files must retain the above copyright notice.
|
|
|
15 |
*
|
|
|
16 |
* @filesource
|
|
|
17 |
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
18 |
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
19 |
* @package cake
|
|
|
20 |
* @subpackage cake.cake.libs
|
|
|
21 |
* @since CakePHP(tm) v 0.2.9
|
|
|
22 |
* @version $Revision: 8004 $
|
|
|
23 |
* @modifiedby $LastChangedBy: gwoo $
|
|
|
24 |
* @lastmodified $Date: 2009-01-16 12:15:21 -0800 (Fri, 16 Jan 2009) $
|
|
|
25 |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
26 |
*/
|
|
|
27 |
/**
|
|
|
28 |
* Included libraries.
|
|
|
29 |
*
|
|
|
30 |
*/
|
|
|
31 |
if (!class_exists('Object')) {
|
|
|
32 |
App::import('Core', 'Object');
|
|
|
33 |
}
|
|
|
34 |
/**
|
|
|
35 |
* Parses the request URL into controller, action, and parameters.
|
|
|
36 |
*
|
|
|
37 |
* @package cake
|
|
|
38 |
* @subpackage cake.cake.libs
|
|
|
39 |
*/
|
|
|
40 |
class Router extends Object {
|
|
|
41 |
/**
|
|
|
42 |
* Array of routes
|
|
|
43 |
*
|
|
|
44 |
* @var array
|
|
|
45 |
* @access public
|
|
|
46 |
*/
|
|
|
47 |
var $routes = array();
|
|
|
48 |
/**
|
|
|
49 |
* Caches admin setting from Configure class
|
|
|
50 |
*
|
|
|
51 |
* @var array
|
|
|
52 |
* @access private
|
|
|
53 |
*/
|
|
|
54 |
var $__admin = null;
|
|
|
55 |
/**
|
|
|
56 |
* List of action prefixes used in connected routes
|
|
|
57 |
*
|
|
|
58 |
* @var array
|
|
|
59 |
* @access private
|
|
|
60 |
*/
|
|
|
61 |
var $__prefixes = array();
|
|
|
62 |
/**
|
|
|
63 |
* Directive for Router to parse out file extensions for mapping to Content-types.
|
|
|
64 |
*
|
|
|
65 |
* @var boolean
|
|
|
66 |
* @access private
|
|
|
67 |
*/
|
|
|
68 |
var $__parseExtensions = false;
|
|
|
69 |
/**
|
|
|
70 |
* List of valid extensions to parse from a URL. If null, any extension is allowed.
|
|
|
71 |
*
|
|
|
72 |
* @var array
|
|
|
73 |
* @access private
|
|
|
74 |
*/
|
|
|
75 |
var $__validExtensions = null;
|
|
|
76 |
/**
|
|
|
77 |
* 'Constant' regular expression definitions for named route elements
|
|
|
78 |
*
|
|
|
79 |
* @var array
|
|
|
80 |
* @access private
|
|
|
81 |
*/
|
|
|
82 |
var $__named = array(
|
|
|
83 |
'Action' => 'index|show|add|create|edit|update|remove|del|delete|view|item',
|
|
|
84 |
'Year' => '[12][0-9]{3}',
|
|
|
85 |
'Month' => '0[1-9]|1[012]',
|
|
|
86 |
'Day' => '0[1-9]|[12][0-9]|3[01]',
|
|
|
87 |
'ID' => '[0-9]+',
|
|
|
88 |
'UUID' => '[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}'
|
|
|
89 |
);
|
|
|
90 |
/**
|
|
|
91 |
* Stores all information necessary to decide what named arguments are parsed under what conditions.
|
|
|
92 |
*
|
|
|
93 |
* @var string
|
|
|
94 |
* @access public
|
|
|
95 |
*/
|
|
|
96 |
var $named = array(
|
|
|
97 |
'default' => array('page', 'fields', 'order', 'limit', 'recursive', 'sort', 'direction', 'step'),
|
|
|
98 |
'greedy' => true,
|
|
|
99 |
'separator' => ':',
|
|
|
100 |
'rules' => false,
|
|
|
101 |
);
|
|
|
102 |
/**
|
|
|
103 |
* The route matching the URL of the current request
|
|
|
104 |
*
|
|
|
105 |
* @var array
|
|
|
106 |
* @access private
|
|
|
107 |
*/
|
|
|
108 |
var $__currentRoute = array();
|
|
|
109 |
/**
|
|
|
110 |
* HTTP header shortcut map. Used for evaluating header-based route expressions.
|
|
|
111 |
*
|
|
|
112 |
* @var array
|
|
|
113 |
* @access private
|
|
|
114 |
*/
|
|
|
115 |
var $__headerMap = array(
|
|
|
116 |
'type' => 'content_type',
|
|
|
117 |
'method' => 'request_method',
|
|
|
118 |
'server' => 'server_name'
|
|
|
119 |
);
|
|
|
120 |
/**
|
|
|
121 |
* Default HTTP request method => controller action map.
|
|
|
122 |
*
|
|
|
123 |
* @var array
|
|
|
124 |
* @access private
|
|
|
125 |
*/
|
|
|
126 |
var $__resourceMap = array(
|
|
|
127 |
array('action' => 'index', 'method' => 'GET', 'id' => false),
|
|
|
128 |
array('action' => 'view', 'method' => 'GET', 'id' => true),
|
|
|
129 |
array('action' => 'add', 'method' => 'POST', 'id' => false),
|
|
|
130 |
array('action' => 'edit', 'method' => 'PUT', 'id' => true),
|
|
|
131 |
array('action' => 'delete', 'method' => 'DELETE', 'id' => true),
|
|
|
132 |
array('action' => 'edit', 'method' => 'POST', 'id' => true)
|
|
|
133 |
);
|
|
|
134 |
/**
|
|
|
135 |
* List of resource-mapped controllers
|
|
|
136 |
*
|
|
|
137 |
* @var array
|
|
|
138 |
* @access private
|
|
|
139 |
*/
|
|
|
140 |
var $__resourceMapped = array();
|
|
|
141 |
/**
|
|
|
142 |
* Maintains the parameter stack for the current request
|
|
|
143 |
*
|
|
|
144 |
* @var array
|
|
|
145 |
* @access private
|
|
|
146 |
*/
|
|
|
147 |
var $__params = array();
|
|
|
148 |
/**
|
|
|
149 |
* Maintains the path stack for the current request
|
|
|
150 |
*
|
|
|
151 |
* @var array
|
|
|
152 |
* @access private
|
|
|
153 |
*/
|
|
|
154 |
var $__paths = array();
|
|
|
155 |
/**
|
|
|
156 |
* Keeps Router state to determine if default routes have already been connected
|
|
|
157 |
*
|
|
|
158 |
* @var boolean
|
|
|
159 |
* @access private
|
|
|
160 |
*/
|
|
|
161 |
var $__defaultsMapped = false;
|
|
|
162 |
/**
|
|
|
163 |
* Gets a reference to the Router object instance
|
|
|
164 |
*
|
|
|
165 |
* @return object Object instance
|
|
|
166 |
* @access public
|
|
|
167 |
* @static
|
|
|
168 |
*/
|
|
|
169 |
function &getInstance() {
|
|
|
170 |
static $instance = array();
|
|
|
171 |
|
|
|
172 |
if (!$instance) {
|
|
|
173 |
$instance[0] =& new Router();
|
|
|
174 |
$instance[0]->__admin = Configure::read('Routing.admin');
|
|
|
175 |
}
|
|
|
176 |
return $instance[0];
|
|
|
177 |
}
|
|
|
178 |
/**
|
|
|
179 |
* Gets the named route elements for use in app/config/routes.php
|
|
|
180 |
*
|
|
|
181 |
* @return array Named route elements
|
|
|
182 |
* @access public
|
|
|
183 |
* @see Router::$__named
|
|
|
184 |
* @static
|
|
|
185 |
*/
|
|
|
186 |
function getNamedExpressions() {
|
|
|
187 |
$_this =& Router::getInstance();
|
|
|
188 |
return $_this->__named;
|
|
|
189 |
}
|
|
|
190 |
/**
|
|
|
191 |
* Returns this object's routes array. Returns false if there are no routes available.
|
|
|
192 |
*
|
|
|
193 |
* @param string $route An empty string, or a route string "/"
|
|
|
194 |
* @param array $default NULL or an array describing the default route
|
|
|
195 |
* @param array $params An array matching the named elements in the route to regular expressions which that element should match.
|
|
|
196 |
* @see routes
|
|
|
197 |
* @return array Array of routes
|
|
|
198 |
* @access public
|
|
|
199 |
* @static
|
|
|
200 |
*/
|
|
|
201 |
function connect($route, $default = array(), $params = array()) {
|
|
|
202 |
$_this =& Router::getInstance();
|
|
|
203 |
|
|
|
204 |
if (!isset($default['action'])) {
|
|
|
205 |
$default['action'] = 'index';
|
|
|
206 |
}
|
|
|
207 |
if (isset($default[$_this->__admin])) {
|
|
|
208 |
$default['prefix'] = $_this->__admin;
|
|
|
209 |
}
|
|
|
210 |
if (isset($default['prefix'])) {
|
|
|
211 |
$_this->__prefixes[] = $default['prefix'];
|
|
|
212 |
$_this->__prefixes = array_keys(array_flip($_this->__prefixes));
|
|
|
213 |
}
|
|
|
214 |
$_this->routes[] = array($route, $default, $params);
|
|
|
215 |
return $_this->routes;
|
|
|
216 |
}
|
|
|
217 |
/**
|
|
|
218 |
*Specifies what named parameters CakePHP should be parsing. The most common setups are:
|
|
|
219 |
*
|
|
|
220 |
* Do not parse any named parameters:
|
|
|
221 |
* Router::connectNamed(false);
|
|
|
222 |
*
|
|
|
223 |
* Parse only default parameters used for CakePHP's pagination:
|
|
|
224 |
* Router::connectNamed(false, array('default' => true));
|
|
|
225 |
*
|
|
|
226 |
* Parse only the page parameter if its value is a number:
|
|
|
227 |
* Router::connectNamed(array('page' => '[\d]+'), array('default' => false, 'greedy' => false));
|
|
|
228 |
*
|
|
|
229 |
* Parse only the page parameter no mater what.
|
|
|
230 |
* Router::connectNamed(array('page'), array('default' => false, 'greedy' => false));
|
|
|
231 |
*
|
|
|
232 |
* Parse only the page parameter if the current action is 'index'.
|
|
|
233 |
* Router::connectNamed(array('page' => array('action' => 'index')), array('default' => false, 'greedy' => false));
|
|
|
234 |
*
|
|
|
235 |
* Parse only the page parameter if the current action is 'index' and the controller is 'pages'.
|
|
|
236 |
* Router::connectNamed(array('page' => array('action' => 'index', 'controller' => 'pages')), array('default' => false, 'greedy' => false));
|
|
|
237 |
*
|
|
|
238 |
* @param array $named A list of named parameters. Key value pairs are accepted where values are either regex strings to match, or arrays as seen above.
|
|
|
239 |
* @param array $options Allows to control all settings: separator, greedy, reset, default
|
|
|
240 |
* @return array
|
|
|
241 |
* @access public
|
|
|
242 |
* @static
|
|
|
243 |
*/
|
|
|
244 |
function connectNamed($named, $options = array()) {
|
|
|
245 |
$_this =& Router::getInstance();
|
|
|
246 |
|
|
|
247 |
if (isset($options['argSeparator'])) {
|
|
|
248 |
$_this->named['separator'] = $options['argSeparator'];
|
|
|
249 |
unset($options['argSeparator']);
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
if ($named === true || $named === false) {
|
|
|
253 |
$options = array_merge(array('default' => $named, 'reset' => true, 'greedy' => $named), $options);
|
|
|
254 |
$named = array();
|
|
|
255 |
}
|
|
|
256 |
$options = array_merge(array('default' => false, 'reset' => false, 'greedy' => true), $options);
|
|
|
257 |
|
|
|
258 |
if ($options['reset'] == true || $_this->named['rules'] === false) {
|
|
|
259 |
$_this->named['rules'] = array();
|
|
|
260 |
}
|
|
|
261 |
|
|
|
262 |
if ($options['default']) {
|
|
|
263 |
$named = array_merge($named, $_this->named['default']);
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
foreach ($named as $key => $val) {
|
|
|
267 |
if (is_numeric($key)) {
|
|
|
268 |
$_this->named['rules'][$val] = true;
|
|
|
269 |
} else {
|
|
|
270 |
$_this->named['rules'][$key] = $val;
|
|
|
271 |
}
|
|
|
272 |
}
|
|
|
273 |
$_this->named['greedy'] = $options['greedy'];
|
|
|
274 |
return $_this->named;
|
|
|
275 |
}
|
|
|
276 |
/**
|
|
|
277 |
* Creates REST resource routes for the given controller(s)
|
|
|
278 |
*
|
|
|
279 |
* @param mixed $controller A controller name or array of controller names (i.e. "Posts" or "ListItems")
|
|
|
280 |
* @param array $options Options to use when generating REST routes
|
|
|
281 |
* 'id' - The regular expression fragment to use when matching IDs. By default, matches
|
|
|
282 |
* integer values and UUIDs.
|
|
|
283 |
* 'prefix' - URL prefix to use for the generated routes. Defaults to '/'.
|
|
|
284 |
* @return void
|
|
|
285 |
* @access public
|
|
|
286 |
* @static
|
|
|
287 |
*/
|
|
|
288 |
function mapResources($controller, $options = array()) {
|
|
|
289 |
$_this =& Router::getInstance();
|
|
|
290 |
$options = array_merge(array('prefix' => '/', 'id' => $_this->__named['ID'] . '|' . $_this->__named['UUID']), $options);
|
|
|
291 |
$prefix = $options['prefix'];
|
|
|
292 |
|
|
|
293 |
foreach ((array)$controller as $ctlName) {
|
|
|
294 |
$urlName = Inflector::underscore($ctlName);
|
|
|
295 |
|
|
|
296 |
foreach ($_this->__resourceMap as $params) {
|
|
|
297 |
extract($params);
|
|
|
298 |
$url = $prefix . $urlName . (($id) ? '/:id' : '');
|
|
|
299 |
|
|
|
300 |
Router::connect($url,
|
|
|
301 |
array('controller' => $urlName, 'action' => $action, '[method]' => $params['method']),
|
|
|
302 |
array('id' => $options['id'], 'pass' => array('id'))
|
|
|
303 |
);
|
|
|
304 |
}
|
|
|
305 |
$_this->__resourceMapped[] = $urlName;
|
|
|
306 |
}
|
|
|
307 |
}
|
|
|
308 |
/**
|
|
|
309 |
* Builds a route regular expression
|
|
|
310 |
*
|
|
|
311 |
* @param string $route An empty string, or a route string "/"
|
|
|
312 |
* @param array $default NULL or an array describing the default route
|
|
|
313 |
* @param array $params An array matching the named elements in the route to regular expressions which that element should match.
|
|
|
314 |
* @return array
|
|
|
315 |
* @see routes
|
|
|
316 |
* @access public
|
|
|
317 |
* @static
|
|
|
318 |
*/
|
|
|
319 |
function writeRoute($route, $default, $params) {
|
|
|
320 |
if (empty($route) || ($route === '/')) {
|
|
|
321 |
return array('/^[\/]*$/', array());
|
|
|
322 |
}
|
|
|
323 |
$names = array();
|
|
|
324 |
$elements = explode('/', $route);
|
|
|
325 |
|
|
|
326 |
foreach ($elements as $element) {
|
|
|
327 |
if (empty($element)) {
|
|
|
328 |
continue;
|
|
|
329 |
}
|
|
|
330 |
$q = null;
|
|
|
331 |
$element = trim($element);
|
|
|
332 |
$namedParam = strpos($element, ':') !== false;
|
|
|
333 |
|
|
|
334 |
if ($namedParam && preg_match('/^:([^:]+)$/', $element, $r)) {
|
|
|
335 |
if (isset($params[$r[1]])) {
|
|
|
336 |
if ($r[1] != 'plugin' && array_key_exists($r[1], $default)) {
|
|
|
337 |
$q = '?';
|
|
|
338 |
}
|
|
|
339 |
$parsed[] = '(?:/(' . $params[$r[1]] . ')' . $q . ')' . $q;
|
|
|
340 |
} else {
|
|
|
341 |
$parsed[] = '(?:/([^\/]+))?';
|
|
|
342 |
}
|
|
|
343 |
$names[] = $r[1];
|
|
|
344 |
} elseif ($element === '*') {
|
|
|
345 |
$parsed[] = '(?:/(.*))?';
|
|
|
346 |
} else if ($namedParam && preg_match_all('/(?!\\\\):([a-z_0-9]+)/i', $element, $matches)) {
|
|
|
347 |
$matchCount = count($matches[1]);
|
|
|
348 |
|
|
|
349 |
foreach ($matches[1] as $i => $name) {
|
|
|
350 |
$pos = strpos($element, ':' . $name);
|
|
|
351 |
$before = substr($element, 0, $pos);
|
|
|
352 |
$element = substr($element, $pos + strlen($name) + 1);
|
|
|
353 |
$after = null;
|
|
|
354 |
|
|
|
355 |
if ($i + 1 === $matchCount && $element) {
|
|
|
356 |
$after = preg_quote($element);
|
|
|
357 |
}
|
|
|
358 |
|
|
|
359 |
if ($i === 0) {
|
|
|
360 |
$before = '/' . $before;
|
|
|
361 |
}
|
|
|
362 |
$before = preg_quote($before, '#');
|
|
|
363 |
|
|
|
364 |
if (isset($params[$name])) {
|
|
|
365 |
if (isset($default[$name]) && $name != 'plugin') {
|
|
|
366 |
$q = '?';
|
|
|
367 |
}
|
|
|
368 |
$parsed[] = '(?:' . $before . '(' . $params[$name] . ')' . $q . $after . ')' . $q;
|
|
|
369 |
} else {
|
|
|
370 |
$parsed[] = '(?:' . $before . '([^\/]+)' . $after . ')?';
|
|
|
371 |
}
|
|
|
372 |
$names[] = $name;
|
|
|
373 |
}
|
|
|
374 |
} else {
|
|
|
375 |
$parsed[] = '/' . $element;
|
|
|
376 |
}
|
|
|
377 |
}
|
|
|
378 |
return array('#^' . join('', $parsed) . '[\/]*$#', $names);
|
|
|
379 |
}
|
|
|
380 |
/**
|
|
|
381 |
* Returns the list of prefixes used in connected routes
|
|
|
382 |
*
|
|
|
383 |
* @return array A list of prefixes used in connected routes
|
|
|
384 |
* @access public
|
|
|
385 |
* @static
|
|
|
386 |
*/
|
|
|
387 |
function prefixes() {
|
|
|
388 |
$_this =& Router::getInstance();
|
|
|
389 |
return $_this->__prefixes;
|
|
|
390 |
}
|
|
|
391 |
/**
|
|
|
392 |
* Parses given URL and returns an array of controllers, action and parameters
|
|
|
393 |
* taken from that URL.
|
|
|
394 |
*
|
|
|
395 |
* @param string $url URL to be parsed
|
|
|
396 |
* @return array Parsed elements from URL
|
|
|
397 |
* @access public
|
|
|
398 |
* @static
|
|
|
399 |
*/
|
|
|
400 |
function parse($url) {
|
|
|
401 |
$_this =& Router::getInstance();
|
|
|
402 |
if (!$_this->__defaultsMapped) {
|
|
|
403 |
$_this->__connectDefaultRoutes();
|
|
|
404 |
}
|
|
|
405 |
$out = array('pass' => array(), 'named' => array());
|
|
|
406 |
$r = $ext = null;
|
|
|
407 |
|
|
|
408 |
if (ini_get('magic_quotes_gpc') === '1') {
|
|
|
409 |
$url = stripslashes_deep($url);
|
|
|
410 |
}
|
|
|
411 |
|
|
|
412 |
if ($url && strpos($url, '/') !== 0) {
|
|
|
413 |
$url = '/' . $url;
|
|
|
414 |
}
|
|
|
415 |
if (strpos($url, '?') !== false) {
|
|
|
416 |
$url = substr($url, 0, strpos($url, '?'));
|
|
|
417 |
}
|
|
|
418 |
extract($_this->__parseExtension($url));
|
|
|
419 |
|
|
|
420 |
foreach ($_this->routes as $i => $route) {
|
|
|
421 |
if (count($route) === 3) {
|
|
|
422 |
$route = $_this->compile($i);
|
|
|
423 |
}
|
|
|
424 |
|
|
|
425 |
if (($r = $_this->__matchRoute($route, $url)) !== false) {
|
|
|
426 |
$_this->__currentRoute[] = $route;
|
|
|
427 |
list($route, $regexp, $names, $defaults, $params) = $route;
|
|
|
428 |
$argOptions = array();
|
|
|
429 |
|
|
|
430 |
if (array_key_exists('named', $params)) {
|
|
|
431 |
$argOptions['named'] = $params['named'];
|
|
|
432 |
unset($params['named']);
|
|
|
433 |
}
|
|
|
434 |
if (array_key_exists('greedy', $params)) {
|
|
|
435 |
$argOptions['greedy'] = $params['greedy'];
|
|
|
436 |
unset($params['greedy']);
|
|
|
437 |
}
|
|
|
438 |
array_shift($r);
|
|
|
439 |
|
|
|
440 |
foreach ($names as $name) {
|
|
|
441 |
$out[$name] = null;
|
|
|
442 |
}
|
|
|
443 |
if (is_array($defaults)) {
|
|
|
444 |
foreach ($defaults as $name => $value) {
|
|
|
445 |
if (preg_match('#[a-zA-Z_\-]#i', $name)) {
|
|
|
446 |
$out[$name] = $value;
|
|
|
447 |
} else {
|
|
|
448 |
$out['pass'][] = $value;
|
|
|
449 |
}
|
|
|
450 |
}
|
|
|
451 |
}
|
|
|
452 |
|
|
|
453 |
foreach ($r as $key => $found) {
|
|
|
454 |
if (empty($found) && $found != 0) {
|
|
|
455 |
continue;
|
|
|
456 |
}
|
|
|
457 |
|
|
|
458 |
if (isset($names[$key])) {
|
|
|
459 |
$out[$names[$key]] = $_this->stripEscape($found);
|
|
|
460 |
} elseif (isset($names[$key]) && empty($names[$key]) && empty($out[$names[$key]])) {
|
|
|
461 |
break;
|
|
|
462 |
} else {
|
|
|
463 |
$argOptions['context'] = array('action' => $out['action'], 'controller' => $out['controller']);
|
|
|
464 |
extract($_this->getArgs($found, $argOptions));
|
|
|
465 |
$out['pass'] = array_merge($out['pass'], $pass);
|
|
|
466 |
$out['named'] = $named;
|
|
|
467 |
}
|
|
|
468 |
}
|
|
|
469 |
|
|
|
470 |
if (isset($params['pass'])) {
|
|
|
471 |
for ($j = count($params['pass']) - 1; $j > -1; $j--) {
|
|
|
472 |
if (isset($out[$params['pass'][$j]])) {
|
|
|
473 |
array_unshift($out['pass'], $out[$params['pass'][$j]]);
|
|
|
474 |
}
|
|
|
475 |
}
|
|
|
476 |
}
|
|
|
477 |
break;
|
|
|
478 |
}
|
|
|
479 |
}
|
|
|
480 |
|
|
|
481 |
if (!empty($ext)) {
|
|
|
482 |
$out['url']['ext'] = $ext;
|
|
|
483 |
}
|
|
|
484 |
return $out;
|
|
|
485 |
}
|
|
|
486 |
/**
|
|
|
487 |
* Checks to see if the given URL matches the given route
|
|
|
488 |
*
|
|
|
489 |
* @param array $route
|
|
|
490 |
* @param string $url
|
|
|
491 |
* @return mixed Boolean false on failure, otherwise array
|
|
|
492 |
* @access private
|
|
|
493 |
*/
|
|
|
494 |
function __matchRoute($route, $url) {
|
|
|
495 |
list($route, $regexp, $names, $defaults) = $route;
|
|
|
496 |
|
|
|
497 |
if (!preg_match($regexp, $url, $r)) {
|
|
|
498 |
return false;
|
|
|
499 |
} else {
|
|
|
500 |
foreach ($defaults as $key => $val) {
|
|
|
501 |
if ($key{0} === '[' && preg_match('/^\[(\w+)\]$/', $key, $header)) {
|
|
|
502 |
if (isset($this->__headerMap[$header[1]])) {
|
|
|
503 |
$header = $this->__headerMap[$header[1]];
|
|
|
504 |
} else {
|
|
|
505 |
$header = 'http_' . $header[1];
|
|
|
506 |
}
|
|
|
507 |
|
|
|
508 |
$val = (array)$val;
|
|
|
509 |
$h = false;
|
|
|
510 |
|
|
|
511 |
foreach ($val as $v) {
|
|
|
512 |
if (env(strtoupper($header)) === $v) {
|
|
|
513 |
$h = true;
|
|
|
514 |
}
|
|
|
515 |
}
|
|
|
516 |
if (!$h) {
|
|
|
517 |
return false;
|
|
|
518 |
}
|
|
|
519 |
}
|
|
|
520 |
}
|
|
|
521 |
}
|
|
|
522 |
return $r;
|
|
|
523 |
}
|
|
|
524 |
/**
|
|
|
525 |
* Compiles a route by numeric key and returns the compiled expression, replacing
|
|
|
526 |
* the existing uncompiled route. Do not call statically.
|
|
|
527 |
*
|
|
|
528 |
* @param integer $i
|
|
|
529 |
* @return array Returns an array containing the compiled route
|
|
|
530 |
* @access public
|
|
|
531 |
*/
|
|
|
532 |
function compile($i) {
|
|
|
533 |
$route = $this->routes[$i];
|
|
|
534 |
|
|
|
535 |
if (!list($pattern, $names) = $this->writeRoute($route[0], $route[1], $route[2])) {
|
|
|
536 |
unset($this->routes[$i]);
|
|
|
537 |
return array();
|
|
|
538 |
}
|
|
|
539 |
$this->routes[$i] = array(
|
|
|
540 |
$route[0], $pattern, $names,
|
|
|
541 |
array_merge(array('plugin' => null, 'controller' => null), (array)$route[1]),
|
|
|
542 |
$route[2]
|
|
|
543 |
);
|
|
|
544 |
return $this->routes[$i];
|
|
|
545 |
}
|
|
|
546 |
/**
|
|
|
547 |
* Parses a file extension out of a URL, if Router::parseExtensions() is enabled.
|
|
|
548 |
*
|
|
|
549 |
* @param string $url
|
|
|
550 |
* @return array Returns an array containing the altered URL and the parsed extension.
|
|
|
551 |
* @access private
|
|
|
552 |
*/
|
|
|
553 |
function __parseExtension($url) {
|
|
|
554 |
$ext = null;
|
|
|
555 |
|
|
|
556 |
if ($this->__parseExtensions) {
|
|
|
557 |
if (preg_match('/\.[0-9a-zA-Z]*$/', $url, $match) === 1) {
|
|
|
558 |
$match = substr($match[0], 1);
|
|
|
559 |
if (empty($this->__validExtensions)) {
|
|
|
560 |
$url = substr($url, 0, strpos($url, '.' . $match));
|
|
|
561 |
$ext = $match;
|
|
|
562 |
} else {
|
|
|
563 |
foreach ($this->__validExtensions as $name) {
|
|
|
564 |
if (strcasecmp($name, $match) === 0) {
|
|
|
565 |
$url = substr($url, 0, strpos($url, '.' . $name));
|
|
|
566 |
$ext = $match;
|
|
|
567 |
}
|
|
|
568 |
}
|
|
|
569 |
}
|
|
|
570 |
}
|
|
|
571 |
if (empty($ext)) {
|
|
|
572 |
$ext = 'html';
|
|
|
573 |
}
|
|
|
574 |
}
|
|
|
575 |
return compact('ext', 'url');
|
|
|
576 |
}
|
|
|
577 |
/**
|
|
|
578 |
* Connects the default, built-in routes, including admin routes, and (deprecated) web services
|
|
|
579 |
* routes.
|
|
|
580 |
*
|
|
|
581 |
* @return void
|
|
|
582 |
* @access private
|
|
|
583 |
*/
|
|
|
584 |
function __connectDefaultRoutes() {
|
|
|
585 |
if ($this->__defaultsMapped) {
|
|
|
586 |
return;
|
|
|
587 |
}
|
|
|
588 |
|
|
|
589 |
if ($this->__admin) {
|
|
|
590 |
$params = array('prefix' => $this->__admin, $this->__admin => true);
|
|
|
591 |
}
|
|
|
592 |
|
|
|
593 |
if ($plugins = Configure::listObjects('plugin')) {
|
|
|
594 |
foreach ($plugins as $key => $value) {
|
|
|
595 |
$plugins[$key] = Inflector::underscore($value);
|
|
|
596 |
}
|
|
|
597 |
|
|
|
598 |
$match = array('plugin' => implode('|', $plugins));
|
|
|
599 |
$this->connect('/:plugin/:controller/:action/*', array(), $match);
|
|
|
600 |
|
|
|
601 |
if ($this->__admin) {
|
|
|
602 |
$this->connect("/{$this->__admin}/:plugin/:controller", $params, $match);
|
|
|
603 |
$this->connect("/{$this->__admin}/:plugin/:controller/:action/*", $params, $match);
|
|
|
604 |
}
|
|
|
605 |
}
|
|
|
606 |
|
|
|
607 |
if ($this->__admin) {
|
|
|
608 |
$this->connect("/{$this->__admin}/:controller", $params);
|
|
|
609 |
$this->connect("/{$this->__admin}/:controller/:action/*", $params);
|
|
|
610 |
}
|
|
|
611 |
$this->connect('/:controller', array('action' => 'index'));
|
|
|
612 |
$this->connect('/:controller/:action/*');
|
|
|
613 |
|
|
|
614 |
if ($this->named['rules'] === false) {
|
|
|
615 |
$this->connectNamed(true);
|
|
|
616 |
}
|
|
|
617 |
$this->__defaultsMapped = true;
|
|
|
618 |
}
|
|
|
619 |
/**
|
|
|
620 |
* Takes parameter and path information back from the Dispatcher
|
|
|
621 |
*
|
|
|
622 |
* @param array $params Parameters and path information
|
|
|
623 |
* @return void
|
|
|
624 |
* @access public
|
|
|
625 |
* @static
|
|
|
626 |
*/
|
|
|
627 |
function setRequestInfo($params) {
|
|
|
628 |
$_this =& Router::getInstance();
|
|
|
629 |
$defaults = array('plugin' => null, 'controller' => null, 'action' => null);
|
|
|
630 |
$params[0] = array_merge($defaults, (array)$params[0]);
|
|
|
631 |
$params[1] = array_merge($defaults, (array)$params[1]);
|
|
|
632 |
list($_this->__params[], $_this->__paths[]) = $params;
|
|
|
633 |
|
|
|
634 |
if (count($_this->__paths)) {
|
|
|
635 |
if (isset($_this->__paths[0]['namedArgs'])) {
|
|
|
636 |
foreach ($_this->__paths[0]['namedArgs'] as $arg => $value) {
|
|
|
637 |
$_this->named['rules'][$arg] = true;
|
|
|
638 |
}
|
|
|
639 |
}
|
|
|
640 |
}
|
|
|
641 |
}
|
|
|
642 |
/**
|
|
|
643 |
* Gets parameter information
|
|
|
644 |
*
|
|
|
645 |
* @param boolean $current Get current parameter (true)
|
|
|
646 |
* @return array Parameter information
|
|
|
647 |
* @access public
|
|
|
648 |
* @static
|
|
|
649 |
*/
|
|
|
650 |
function getParams($current = false) {
|
|
|
651 |
$_this =& Router::getInstance();
|
|
|
652 |
if ($current) {
|
|
|
653 |
return $_this->__params[count($_this->__params) - 1];
|
|
|
654 |
}
|
|
|
655 |
if (isset($_this->__params[0])) {
|
|
|
656 |
return $_this->__params[0];
|
|
|
657 |
}
|
|
|
658 |
return array();
|
|
|
659 |
}
|
|
|
660 |
/**
|
|
|
661 |
* Gets URL parameter by name
|
|
|
662 |
*
|
|
|
663 |
* @param string $name Parameter name
|
|
|
664 |
* @param boolean $current Current parameter
|
|
|
665 |
* @return string Parameter value
|
|
|
666 |
* @access public
|
|
|
667 |
* @static
|
|
|
668 |
*/
|
|
|
669 |
function getParam($name = 'controller', $current = false) {
|
|
|
670 |
$params = Router::getParams($current);
|
|
|
671 |
if (isset($params[$name])) {
|
|
|
672 |
return $params[$name];
|
|
|
673 |
}
|
|
|
674 |
return null;
|
|
|
675 |
}
|
|
|
676 |
/**
|
|
|
677 |
* Gets path information
|
|
|
678 |
*
|
|
|
679 |
* @param boolean $current Current parameter
|
|
|
680 |
* @return array
|
|
|
681 |
* @access public
|
|
|
682 |
* @static
|
|
|
683 |
*/
|
|
|
684 |
function getPaths($current = false) {
|
|
|
685 |
$_this =& Router::getInstance();
|
|
|
686 |
if ($current) {
|
|
|
687 |
return $_this->__paths[count($_this->__paths) - 1];
|
|
|
688 |
}
|
|
|
689 |
if (!isset($_this->__paths[0])) {
|
|
|
690 |
return array('base' => null);
|
|
|
691 |
}
|
|
|
692 |
return $_this->__paths[0];
|
|
|
693 |
}
|
|
|
694 |
/**
|
|
|
695 |
* Reloads default Router settings
|
|
|
696 |
*
|
|
|
697 |
* @access public
|
|
|
698 |
* @return void
|
|
|
699 |
* @static
|
|
|
700 |
*/
|
|
|
701 |
function reload() {
|
|
|
702 |
$_this =& Router::getInstance();
|
|
|
703 |
foreach (get_class_vars('Router') as $key => $val) {
|
|
|
704 |
$_this->{$key} = $val;
|
|
|
705 |
}
|
|
|
706 |
$_this->__admin = Configure::read('Routing.admin');
|
|
|
707 |
}
|
|
|
708 |
/**
|
|
|
709 |
* Promote a route (by default, the last one added) to the beginning of the list
|
|
|
710 |
*
|
|
|
711 |
* @param $which A zero-based array index representing the route to move. For example,
|
|
|
712 |
* if 3 routes have been added, the last route would be 2.
|
|
|
713 |
* @return boolean Retuns false if no route exists at the position specified by $which.
|
|
|
714 |
* @access public
|
|
|
715 |
* @static
|
|
|
716 |
*/
|
|
|
717 |
function promote($which = null) {
|
|
|
718 |
$_this =& Router::getInstance();
|
|
|
719 |
if ($which === null) {
|
|
|
720 |
$which = count($_this->routes) - 1;
|
|
|
721 |
}
|
|
|
722 |
if (!isset($_this->routes[$which])) {
|
|
|
723 |
return false;
|
|
|
724 |
}
|
|
|
725 |
$route = $_this->routes[$which];
|
|
|
726 |
unset($_this->routes[$which]);
|
|
|
727 |
array_unshift($_this->routes, $route);
|
|
|
728 |
return true;
|
|
|
729 |
}
|
|
|
730 |
/**
|
|
|
731 |
* Finds URL for specified action.
|
|
|
732 |
*
|
|
|
733 |
* Returns an URL pointing to a combination of controller and action. Param
|
|
|
734 |
* $url can be:
|
|
|
735 |
* + Empty - the method will find adress to actuall controller/action.
|
|
|
736 |
* + '/' - the method will find base URL of application.
|
|
|
737 |
* + A combination of controller/action - the method will find url for it.
|
|
|
738 |
*
|
|
|
739 |
* @param mixed $url Cake-relative URL, like "/products/edit/92" or "/presidents/elect/4"
|
|
|
740 |
* or an array specifying any of the following: 'controller', 'action',
|
|
|
741 |
* and/or 'plugin', in addition to named arguments (keyed array elements),
|
|
|
742 |
* and standard URL arguments (indexed array elements)
|
|
|
743 |
* @param mixed $options If (bool)true, the full base URL will be prepended to the result.
|
|
|
744 |
* If an array accepts the following keys
|
|
|
745 |
* escape - used when making urls embedded in html escapes query string '&'
|
|
|
746 |
* full - if true the full base URL will be prepended.
|
|
|
747 |
* @return string Full translated URL with base path.
|
|
|
748 |
* @access public
|
|
|
749 |
* @static
|
|
|
750 |
*/
|
|
|
751 |
function url($url = null, $full = false) {
|
|
|
752 |
$_this =& Router::getInstance();
|
|
|
753 |
$defaults = $params = array('plugin' => null, 'controller' => null, 'action' => 'index');
|
|
|
754 |
|
|
|
755 |
if (is_bool($full)) {
|
|
|
756 |
$escape = false;
|
|
|
757 |
} else {
|
|
|
758 |
extract(array_merge(array('escape' => false, 'full' => false), $full));
|
|
|
759 |
}
|
|
|
760 |
|
|
|
761 |
if (!empty($_this->__params)) {
|
|
|
762 |
if (isset($this) && !isset($this->params['requested'])) {
|
|
|
763 |
$params = $_this->__params[0];
|
|
|
764 |
} else {
|
|
|
765 |
$params = end($_this->__params);
|
|
|
766 |
}
|
|
|
767 |
}
|
|
|
768 |
$path = array('base' => null);
|
|
|
769 |
|
|
|
770 |
if (!empty($_this->__paths)) {
|
|
|
771 |
if (isset($this) && !isset($this->params['requested'])) {
|
|
|
772 |
$path = $_this->__paths[0];
|
|
|
773 |
} else {
|
|
|
774 |
$path = end($_this->__paths);
|
|
|
775 |
}
|
|
|
776 |
}
|
|
|
777 |
$base = $path['base'];
|
|
|
778 |
$extension = $output = $mapped = $q = $frag = null;
|
|
|
779 |
|
|
|
780 |
if (is_array($url)) {
|
|
|
781 |
if (isset($url['base']) && $url['base'] === false) {
|
|
|
782 |
$base = null;
|
|
|
783 |
unset($url['base']);
|
|
|
784 |
}
|
|
|
785 |
if (isset($url['full_base']) && $url['full_base'] === true) {
|
|
|
786 |
$full = true;
|
|
|
787 |
unset($url['full_base']);
|
|
|
788 |
}
|
|
|
789 |
if (isset($url['?'])) {
|
|
|
790 |
$q = $url['?'];
|
|
|
791 |
unset($url['?']);
|
|
|
792 |
}
|
|
|
793 |
if (isset($url['#'])) {
|
|
|
794 |
$frag = '#' . urlencode($url['#']);
|
|
|
795 |
unset($url['#']);
|
|
|
796 |
}
|
|
|
797 |
if (empty($url['action'])) {
|
|
|
798 |
if (empty($url['controller']) || $params['controller'] === $url['controller']) {
|
|
|
799 |
$url['action'] = $params['action'];
|
|
|
800 |
} else {
|
|
|
801 |
$url['action'] = 'index';
|
|
|
802 |
}
|
|
|
803 |
}
|
|
|
804 |
if ($_this->__admin) {
|
|
|
805 |
if (!isset($url[$_this->__admin]) && !empty($params[$_this->__admin])) {
|
|
|
806 |
$url[$_this->__admin] = true;
|
|
|
807 |
} elseif ($_this->__admin && isset($url[$_this->__admin]) && !$url[$_this->__admin]) {
|
|
|
808 |
unset($url[$_this->__admin]);
|
|
|
809 |
}
|
|
|
810 |
}
|
|
|
811 |
$plugin = false;
|
|
|
812 |
|
|
|
813 |
if (array_key_exists('plugin', $url)) {
|
|
|
814 |
$plugin = $url['plugin'];
|
|
|
815 |
}
|
|
|
816 |
|
|
|
817 |
$url = array_merge(array('controller' => $params['controller'], 'plugin' => $params['plugin']), Set::filter($url, true));
|
|
|
818 |
|
|
|
819 |
if ($plugin !== false) {
|
|
|
820 |
$url['plugin'] = $plugin;
|
|
|
821 |
}
|
|
|
822 |
|
|
|
823 |
if (isset($url['ext'])) {
|
|
|
824 |
$extension = '.' . $url['ext'];
|
|
|
825 |
unset($url['ext']);
|
|
|
826 |
}
|
|
|
827 |
$match = false;
|
|
|
828 |
|
|
|
829 |
foreach ($_this->routes as $i => $route) {
|
|
|
830 |
if (count($route) === 3) {
|
|
|
831 |
$route = $_this->compile($i);
|
|
|
832 |
}
|
|
|
833 |
$originalUrl = $url;
|
|
|
834 |
|
|
|
835 |
if (isset($route[4]['persist'], $_this->__params[0])) {
|
|
|
836 |
$url = array_merge(array_intersect_key($params, Set::combine($route[4]['persist'], '/')), $url);
|
|
|
837 |
}
|
|
|
838 |
if ($match = $_this->mapRouteElements($route, $url)) {
|
|
|
839 |
$output = trim($match, '/');
|
|
|
840 |
$url = array();
|
|
|
841 |
break;
|
|
|
842 |
}
|
|
|
843 |
$url = $originalUrl;
|
|
|
844 |
}
|
|
|
845 |
$named = $args = array();
|
|
|
846 |
$skip = array(
|
|
|
847 |
'bare', 'action', 'controller', 'plugin', 'ext', '?', '#', 'prefix', $_this->__admin
|
|
|
848 |
);
|
|
|
849 |
|
|
|
850 |
$keys = array_values(array_diff(array_keys($url), $skip));
|
|
|
851 |
$count = count($keys);
|
|
|
852 |
|
|
|
853 |
// Remove this once parsed URL parameters can be inserted into 'pass'
|
|
|
854 |
for ($i = 0; $i < $count; $i++) {
|
|
|
855 |
if ($i === 0 && is_numeric($keys[$i]) && in_array('id', $keys)) {
|
|
|
856 |
$args[0] = $url[$keys[$i]];
|
|
|
857 |
} elseif (is_numeric($keys[$i]) || $keys[$i] === 'id') {
|
|
|
858 |
$args[] = $url[$keys[$i]];
|
|
|
859 |
} else {
|
|
|
860 |
$named[$keys[$i]] = $url[$keys[$i]];
|
|
|
861 |
}
|
|
|
862 |
}
|
|
|
863 |
|
|
|
864 |
if ($match === false) {
|
|
|
865 |
list($args, $named) = array(Set::filter($args, true), Set::filter($named));
|
|
|
866 |
if (!empty($url[$_this->__admin])) {
|
|
|
867 |
$url['action'] = str_replace($_this->__admin . '_', '', $url['action']);
|
|
|
868 |
}
|
|
|
869 |
|
|
|
870 |
if (empty($named) && empty($args) && (!isset($url['action']) || $url['action'] === 'index')) {
|
|
|
871 |
$url['action'] = null;
|
|
|
872 |
}
|
|
|
873 |
|
|
|
874 |
$urlOut = Set::filter(array($url['controller'], $url['action']));
|
|
|
875 |
|
|
|
876 |
if (isset($url['plugin']) && $url['plugin'] != $url['controller']) {
|
|
|
877 |
array_unshift($urlOut, $url['plugin']);
|
|
|
878 |
}
|
|
|
879 |
|
|
|
880 |
if ($_this->__admin && isset($url[$_this->__admin])) {
|
|
|
881 |
array_unshift($urlOut, $_this->__admin);
|
|
|
882 |
}
|
|
|
883 |
$output = join('/', $urlOut) . '/';
|
|
|
884 |
}
|
|
|
885 |
|
|
|
886 |
if (!empty($args)) {
|
|
|
887 |
$args = join('/', $args);
|
|
|
888 |
if ($output{strlen($output) - 1} != '/') {
|
|
|
889 |
$args = '/'. $args;
|
|
|
890 |
}
|
|
|
891 |
$output .= $args;
|
|
|
892 |
}
|
|
|
893 |
|
|
|
894 |
if (!empty($named)) {
|
|
|
895 |
foreach ($named as $name => $value) {
|
|
|
896 |
$output .= '/' . $name . $_this->named['separator'] . $value;
|
|
|
897 |
}
|
|
|
898 |
}
|
|
|
899 |
|
|
|
900 |
$output = str_replace('//', '/', $base . '/' . $output);
|
|
|
901 |
} else {
|
|
|
902 |
if (((strpos($url, '://')) || (strpos($url, 'javascript:') === 0) || (strpos($url, 'mailto:') === 0)) || (!strncmp($url, '#', 1))) {
|
|
|
903 |
return $url;
|
|
|
904 |
}
|
|
|
905 |
if (empty($url)) {
|
|
|
906 |
if (!isset($path['here'])) {
|
|
|
907 |
$path['here'] = '/';
|
|
|
908 |
}
|
|
|
909 |
$output = $path['here'];
|
|
|
910 |
} elseif (substr($url, 0, 1) === '/') {
|
|
|
911 |
$output = $base . $url;
|
|
|
912 |
} else {
|
|
|
913 |
$output = $base . '/';
|
|
|
914 |
if ($_this->__admin && isset($params[$_this->__admin])) {
|
|
|
915 |
$output .= $_this->__admin . '/';
|
|
|
916 |
}
|
|
|
917 |
if (!empty($params['plugin']) && $params['plugin'] !== $params['controller']) {
|
|
|
918 |
$output .= Inflector::underscore($params['plugin']) . '/';
|
|
|
919 |
}
|
|
|
920 |
$output .= Inflector::underscore($params['controller']) . '/' . $url;
|
|
|
921 |
}
|
|
|
922 |
$output = str_replace('//', '/', $output);
|
|
|
923 |
}
|
|
|
924 |
if ($full && defined('FULL_BASE_URL')) {
|
|
|
925 |
$output = FULL_BASE_URL . $output;
|
|
|
926 |
}
|
|
|
927 |
if (!empty($extension) && substr($output, -1) === '/') {
|
|
|
928 |
$output = substr($output, 0, -1);
|
|
|
929 |
}
|
|
|
930 |
|
|
|
931 |
return $output . $extension . $_this->queryString($q, array(), $escape) . $frag;
|
|
|
932 |
}
|
|
|
933 |
/**
|
|
|
934 |
* Maps a URL array onto a route and returns the string result, or false if no match
|
|
|
935 |
*
|
|
|
936 |
* @param array $route Route Route
|
|
|
937 |
* @param array $url URL URL to map
|
|
|
938 |
* @return mixed Result (as string) or false if no match
|
|
|
939 |
* @access public
|
|
|
940 |
* @static
|
|
|
941 |
*/
|
|
|
942 |
function mapRouteElements($route, $url) {
|
|
|
943 |
if (isset($route[3]['prefix'])) {
|
|
|
944 |
$prefix = $route[3]['prefix'];
|
|
|
945 |
unset($route[3]['prefix']);
|
|
|
946 |
}
|
|
|
947 |
|
|
|
948 |
$pass = array();
|
|
|
949 |
$defaults = $route[3];
|
|
|
950 |
$routeParams = $route[2];
|
|
|
951 |
$params = Set::diff($url, $defaults);
|
|
|
952 |
$urlInv = array_combine(array_values($url), array_keys($url));
|
|
|
953 |
|
|
|
954 |
$i = 0;
|
|
|
955 |
while (isset($defaults[$i])) {
|
|
|
956 |
if (isset($urlInv[$defaults[$i]])) {
|
|
|
957 |
if (!in_array($defaults[$i], $url) && is_int($urlInv[$defaults[$i]])) {
|
|
|
958 |
return false;
|
|
|
959 |
}
|
|
|
960 |
unset($urlInv[$defaults[$i]], $defaults[$i]);
|
|
|
961 |
} else {
|
|
|
962 |
return false;
|
|
|
963 |
}
|
|
|
964 |
$i++;
|
|
|
965 |
}
|
|
|
966 |
|
|
|
967 |
foreach ($params as $key => $value) {
|
|
|
968 |
if (is_int($key)) {
|
|
|
969 |
$pass[] = $value;
|
|
|
970 |
unset($params[$key]);
|
|
|
971 |
}
|
|
|
972 |
}
|
|
|
973 |
list($named, $params) = Router::getNamedElements($params);
|
|
|
974 |
|
|
|
975 |
if (!strpos($route[0], '*') && (!empty($pass) || !empty($named))) {
|
|
|
976 |
return false;
|
|
|
977 |
}
|
|
|
978 |
|
|
|
979 |
$urlKeys = array_keys($url);
|
|
|
980 |
$paramsKeys = array_keys($params);
|
|
|
981 |
$defaultsKeys = array_keys($defaults);
|
|
|
982 |
|
|
|
983 |
if (!empty($params)) {
|
|
|
984 |
if (array_diff($paramsKeys, $routeParams) != array()) {
|
|
|
985 |
return false;
|
|
|
986 |
}
|
|
|
987 |
$required = array_values(array_diff($routeParams, $urlKeys));
|
|
|
988 |
$reqCount = count($required);
|
|
|
989 |
|
|
|
990 |
for ($i = 0; $i < $reqCount; $i++) {
|
|
|
991 |
if (array_key_exists($required[$i], $defaults) && $defaults[$required[$i]] === null) {
|
|
|
992 |
unset($required[$i]);
|
|
|
993 |
}
|
|
|
994 |
}
|
|
|
995 |
}
|
|
|
996 |
$isFilled = true;
|
|
|
997 |
|
|
|
998 |
if (!empty($routeParams)) {
|
|
|
999 |
$filled = array_intersect_key($url, array_combine($routeParams, array_keys($routeParams)));
|
|
|
1000 |
$isFilled = (array_diff($routeParams, array_keys($filled)) === array());
|
|
|
1001 |
if (!$isFilled && empty($params)) {
|
|
|
1002 |
return false;
|
|
|
1003 |
}
|
|
|
1004 |
}
|
|
|
1005 |
|
|
|
1006 |
if (empty($params)) {
|
|
|
1007 |
return Router::__mapRoute($route, array_merge($url, compact('pass', 'named', 'prefix')));
|
|
|
1008 |
} elseif (!empty($routeParams) && !empty($route[3])) {
|
|
|
1009 |
|
|
|
1010 |
if (!empty($required)) {
|
|
|
1011 |
return false;
|
|
|
1012 |
}
|
|
|
1013 |
foreach ($params as $key => $val) {
|
|
|
1014 |
if ((!isset($url[$key]) || $url[$key] != $val) || (!isset($defaults[$key]) || $defaults[$key] != $val) && !in_array($key, $routeParams)) {
|
|
|
1015 |
if (!isset($defaults[$key])) {
|
|
|
1016 |
continue;
|
|
|
1017 |
}
|
|
|
1018 |
return false;
|
|
|
1019 |
}
|
|
|
1020 |
}
|
|
|
1021 |
} else {
|
|
|
1022 |
if (empty($required) && $defaults['plugin'] === $url['plugin'] && $defaults['controller'] === $url['controller'] && $defaults['action'] === $url['action']) {
|
|
|
1023 |
return Router::__mapRoute($route, array_merge($url, compact('pass', 'named', 'prefix')));
|
|
|
1024 |
}
|
|
|
1025 |
return false;
|
|
|
1026 |
}
|
|
|
1027 |
|
|
|
1028 |
if (!empty($route[4])) {
|
|
|
1029 |
foreach ($route[4] as $key => $reg) {
|
|
|
1030 |
if (array_key_exists($key, $url) && !preg_match('#' . $reg . '#', $url[$key])) {
|
|
|
1031 |
return false;
|
|
|
1032 |
}
|
|
|
1033 |
}
|
|
|
1034 |
}
|
|
|
1035 |
return Router::__mapRoute($route, array_merge($filled, compact('pass', 'named', 'prefix')));
|
|
|
1036 |
}
|
|
|
1037 |
/**
|
|
|
1038 |
* Merges URL parameters into a route string
|
|
|
1039 |
*
|
|
|
1040 |
* @param array $route Route
|
|
|
1041 |
* @param array $params Parameters
|
|
|
1042 |
* @return string Merged URL with parameters
|
|
|
1043 |
* @access private
|
|
|
1044 |
*/
|
|
|
1045 |
function __mapRoute($route, $params = array()) {
|
|
|
1046 |
if (isset($params['plugin']) && isset($params['controller']) && $params['plugin'] === $params['controller']) {
|
|
|
1047 |
unset($params['controller']);
|
|
|
1048 |
}
|
|
|
1049 |
|
|
|
1050 |
if (isset($params['prefix']) && isset($params['action'])) {
|
|
|
1051 |
$params['action'] = str_replace($params['prefix'] . '_', '', $params['action']);
|
|
|
1052 |
unset($params['prefix']);
|
|
|
1053 |
}
|
|
|
1054 |
|
|
|
1055 |
if (isset($params['pass']) && is_array($params['pass'])) {
|
|
|
1056 |
$params['pass'] = implode('/', Set::filter($params['pass'], true));
|
|
|
1057 |
} elseif (!isset($params['pass'])) {
|
|
|
1058 |
$params['pass'] = '';
|
|
|
1059 |
}
|
|
|
1060 |
|
|
|
1061 |
if (isset($params['named'])) {
|
|
|
1062 |
if (is_array($params['named'])) {
|
|
|
1063 |
$count = count($params['named']);
|
|
|
1064 |
$keys = array_keys($params['named']);
|
|
|
1065 |
$named = array();
|
|
|
1066 |
|
|
|
1067 |
for ($i = 0; $i < $count; $i++) {
|
|
|
1068 |
$named[] = $keys[$i] . $this->named['separator'] . $params['named'][$keys[$i]];
|
|
|
1069 |
}
|
|
|
1070 |
$params['named'] = join('/', $named);
|
|
|
1071 |
}
|
|
|
1072 |
$params['pass'] = str_replace('//', '/', $params['pass'] . '/' . $params['named']);
|
|
|
1073 |
}
|
|
|
1074 |
$out = $route[0];
|
|
|
1075 |
|
|
|
1076 |
foreach ($route[2] as $key) {
|
|
|
1077 |
$string = null;
|
|
|
1078 |
if (isset($params[$key])) {
|
|
|
1079 |
$string = $params[$key];
|
|
|
1080 |
unset($params[$key]);
|
|
|
1081 |
}
|
|
|
1082 |
$out = str_replace(':' . $key, $string, $out);
|
|
|
1083 |
}
|
|
|
1084 |
|
|
|
1085 |
if (strpos($route[0], '*')) {
|
|
|
1086 |
$out = str_replace('*', $params['pass'], $out);
|
|
|
1087 |
}
|
|
|
1088 |
return $out;
|
|
|
1089 |
}
|
|
|
1090 |
/**
|
|
|
1091 |
* Takes an array of URL parameters and separates the ones that can be used as named arguments
|
|
|
1092 |
*
|
|
|
1093 |
* @param array $params Associative array of URL parameters.
|
|
|
1094 |
* @param string $controller Name of controller being routed. Used in scoping.
|
|
|
1095 |
* @param string $action Name of action being routed. Used in scoping.
|
|
|
1096 |
* @return array
|
|
|
1097 |
* @access public
|
|
|
1098 |
* @static
|
|
|
1099 |
*/
|
|
|
1100 |
function getNamedElements($params, $controller = null, $action = null) {
|
|
|
1101 |
$_this =& Router::getInstance();
|
|
|
1102 |
$named = array();
|
|
|
1103 |
|
|
|
1104 |
foreach ($params as $param => $val) {
|
|
|
1105 |
if (isset($_this->named['rules'][$param])) {
|
|
|
1106 |
$rule = $_this->named['rules'][$param];
|
|
|
1107 |
if (Router::matchNamed($param, $val, $rule, compact('controller', 'action'))) {
|
|
|
1108 |
$named[$param] = $val;
|
|
|
1109 |
unset($params[$param]);
|
|
|
1110 |
}
|
|
|
1111 |
}
|
|
|
1112 |
}
|
|
|
1113 |
return array($named, $params);
|
|
|
1114 |
}
|
|
|
1115 |
/**
|
|
|
1116 |
* Return true if a given named $param's $val matches a given $rule depending on $context. Currently implemented
|
|
|
1117 |
* rule types are controller, action and match that can be combined with each other.
|
|
|
1118 |
*
|
|
|
1119 |
* @param string $param The name of the named parameter
|
|
|
1120 |
* @param string $val The value of the named parameter
|
|
|
1121 |
* @param array $rule The rule(s) to apply, can also be a match string
|
|
|
1122 |
* @param string $context An array with additional context information (controller / action)
|
|
|
1123 |
* @return boolean
|
|
|
1124 |
* @access public
|
|
|
1125 |
*/
|
|
|
1126 |
function matchNamed($param, $val, $rule, $context = array()) {
|
|
|
1127 |
if ($rule === true || $rule === false) {
|
|
|
1128 |
return $rule;
|
|
|
1129 |
}
|
|
|
1130 |
if (is_string($rule)) {
|
|
|
1131 |
$rule = array('match' => $rule);
|
|
|
1132 |
}
|
|
|
1133 |
if (!is_array($rule)) {
|
|
|
1134 |
return false;
|
|
|
1135 |
}
|
|
|
1136 |
|
|
|
1137 |
$controllerMatches = !isset($rule['controller'], $context['controller']) || in_array($context['controller'], (array)$rule['controller']);
|
|
|
1138 |
if (!$controllerMatches) {
|
|
|
1139 |
return false;
|
|
|
1140 |
}
|
|
|
1141 |
$actionMatches = !isset($rule['action'], $context['action']) || in_array($context['action'], (array)$rule['action']);
|
|
|
1142 |
if (!$actionMatches) {
|
|
|
1143 |
return false;
|
|
|
1144 |
}
|
|
|
1145 |
$valueMatches = !isset($rule['match']) || preg_match(sprintf('/%s/', $rule['match']), $val);
|
|
|
1146 |
return $valueMatches;
|
|
|
1147 |
}
|
|
|
1148 |
/**
|
|
|
1149 |
* Generates a well-formed querystring from $q
|
|
|
1150 |
*
|
|
|
1151 |
* @param mixed $q Query string
|
|
|
1152 |
* @param array $extra Extra querystring parameters.
|
|
|
1153 |
* @param bool $escape Whether or not to use escaped &
|
|
|
1154 |
* @return array
|
|
|
1155 |
* @access public
|
|
|
1156 |
* @static
|
|
|
1157 |
*/
|
|
|
1158 |
function queryString($q, $extra = array(), $escape = false) {
|
|
|
1159 |
if (empty($q) && empty($extra)) {
|
|
|
1160 |
return null;
|
|
|
1161 |
}
|
|
|
1162 |
$join = '&';
|
|
|
1163 |
if ($escape === true) {
|
|
|
1164 |
$join = '&';
|
|
|
1165 |
}
|
|
|
1166 |
$out = '';
|
|
|
1167 |
|
|
|
1168 |
if (is_array($q)) {
|
|
|
1169 |
$q = array_merge($extra, $q);
|
|
|
1170 |
} else {
|
|
|
1171 |
$out = $q;
|
|
|
1172 |
$q = $extra;
|
|
|
1173 |
}
|
|
|
1174 |
$out .= http_build_query($q, null, $join);
|
|
|
1175 |
if (isset($out[0]) && $out[0] != '?') {
|
|
|
1176 |
$out = '?' . $out;
|
|
|
1177 |
}
|
|
|
1178 |
return $out;
|
|
|
1179 |
}
|
|
|
1180 |
/**
|
|
|
1181 |
* Normalizes a URL for purposes of comparison
|
|
|
1182 |
*
|
|
|
1183 |
* @param mixed $url URL to normalize
|
|
|
1184 |
* @return string Normalized URL
|
|
|
1185 |
* @access public
|
|
|
1186 |
*/
|
|
|
1187 |
function normalize($url = '/') {
|
|
|
1188 |
if (is_array($url)) {
|
|
|
1189 |
$url = Router::url($url);
|
|
|
1190 |
} elseif (preg_match('/^[a-z\-]+:\/\//', $url)) {
|
|
|
1191 |
return $url;
|
|
|
1192 |
}
|
|
|
1193 |
$paths = Router::getPaths();
|
|
|
1194 |
|
|
|
1195 |
if (!empty($paths['base']) && stristr($url, $paths['base'])) {
|
|
|
1196 |
$url = preg_replace('/' . preg_quote($paths['base'], '/') . '/', '', $url, 1);
|
|
|
1197 |
}
|
|
|
1198 |
$url = '/' . $url;
|
|
|
1199 |
|
|
|
1200 |
while (strpos($url, '//') !== false) {
|
|
|
1201 |
$url = str_replace('//', '/', $url);
|
|
|
1202 |
}
|
|
|
1203 |
$url = preg_replace('/(?:(\/$))/', '', $url);
|
|
|
1204 |
|
|
|
1205 |
if (empty($url)) {
|
|
|
1206 |
return '/';
|
|
|
1207 |
}
|
|
|
1208 |
return $url;
|
|
|
1209 |
}
|
|
|
1210 |
/**
|
|
|
1211 |
* Returns the route matching the current request URL.
|
|
|
1212 |
*
|
|
|
1213 |
* @return array Matching route
|
|
|
1214 |
* @access public
|
|
|
1215 |
* @static
|
|
|
1216 |
*/
|
|
|
1217 |
function requestRoute() {
|
|
|
1218 |
$_this =& Router::getInstance();
|
|
|
1219 |
return $_this->__currentRoute[0];
|
|
|
1220 |
}
|
|
|
1221 |
/**
|
|
|
1222 |
* Returns the route matching the current request (useful for requestAction traces)
|
|
|
1223 |
*
|
|
|
1224 |
* @return array Matching route
|
|
|
1225 |
* @access public
|
|
|
1226 |
* @static
|
|
|
1227 |
*/
|
|
|
1228 |
function currentRoute() {
|
|
|
1229 |
$_this =& Router::getInstance();
|
|
|
1230 |
return $_this->__currentRoute[count($_this->__currentRoute) - 1];
|
|
|
1231 |
}
|
|
|
1232 |
/**
|
|
|
1233 |
* Removes the plugin name from the base URL.
|
|
|
1234 |
*
|
|
|
1235 |
* @param string $base Base URL
|
|
|
1236 |
* @param string $plugin Plugin name
|
|
|
1237 |
* @return base url with plugin name removed if present
|
|
|
1238 |
* @access public
|
|
|
1239 |
* @static
|
|
|
1240 |
*/
|
|
|
1241 |
function stripPlugin($base, $plugin) {
|
|
|
1242 |
if ($plugin != null) {
|
|
|
1243 |
$base = preg_replace('/(?:' . $plugin . ')/', '', $base);
|
|
|
1244 |
$base = str_replace('//', '', $base);
|
|
|
1245 |
$pos1 = strrpos($base, '/');
|
|
|
1246 |
$char = strlen($base) - 1;
|
|
|
1247 |
|
|
|
1248 |
if ($pos1 === $char) {
|
|
|
1249 |
$base = substr($base, 0, $char);
|
|
|
1250 |
}
|
|
|
1251 |
}
|
|
|
1252 |
return $base;
|
|
|
1253 |
}
|
|
|
1254 |
|
|
|
1255 |
/**
|
|
|
1256 |
* Strip escape characters from parameter values.
|
|
|
1257 |
*
|
|
|
1258 |
* @param mixed $param Either an array, or a string
|
|
|
1259 |
* @return mixed Array or string escaped
|
|
|
1260 |
* @access public
|
|
|
1261 |
* @static
|
|
|
1262 |
*/
|
|
|
1263 |
function stripEscape($param) {
|
|
|
1264 |
$_this =& Router::getInstance();
|
|
|
1265 |
if (!is_array($param) || empty($param)) {
|
|
|
1266 |
if (is_bool($param)) {
|
|
|
1267 |
return $param;
|
|
|
1268 |
}
|
|
|
1269 |
|
|
|
1270 |
$return = preg_replace('/^(?:[\\t ]*(?:-!)+)/', '', $param);
|
|
|
1271 |
return $return;
|
|
|
1272 |
}
|
|
|
1273 |
foreach ($param as $key => $value) {
|
|
|
1274 |
if (is_string($value)) {
|
|
|
1275 |
$return[$key] = preg_replace('/^(?:[\\t ]*(?:-!)+)/', '', $value);
|
|
|
1276 |
} else {
|
|
|
1277 |
foreach ($value as $array => $string) {
|
|
|
1278 |
$return[$key][$array] = $_this->stripEscape($string);
|
|
|
1279 |
}
|
|
|
1280 |
}
|
|
|
1281 |
}
|
|
|
1282 |
return $return;
|
|
|
1283 |
}
|
|
|
1284 |
/**
|
|
|
1285 |
* Instructs the router to parse out file extensions from the URL. For example,
|
|
|
1286 |
* http://example.com/posts.rss would yield an file extension of "rss".
|
|
|
1287 |
* The file extension itself is made available in the controller as
|
|
|
1288 |
* $this->params['url']['ext'], and is used by the RequestHandler component to
|
|
|
1289 |
* automatically switch to alternate layouts and templates, and load helpers
|
|
|
1290 |
* corresponding to the given content, i.e. RssHelper.
|
|
|
1291 |
*
|
|
|
1292 |
* A list of valid extension can be passed to this method, i.e. Router::parseExtensions('rss', 'xml');
|
|
|
1293 |
* If no parameters are given, anything after the first . (dot) after the last / in the URL will be
|
|
|
1294 |
* parsed, excluding querystring parameters (i.e. ?q=...).
|
|
|
1295 |
*
|
|
|
1296 |
* @access public
|
|
|
1297 |
* @return void
|
|
|
1298 |
* @static
|
|
|
1299 |
*/
|
|
|
1300 |
function parseExtensions() {
|
|
|
1301 |
$_this =& Router::getInstance();
|
|
|
1302 |
$_this->__parseExtensions = true;
|
|
|
1303 |
if (func_num_args() > 0) {
|
|
|
1304 |
$_this->__validExtensions = func_get_args();
|
|
|
1305 |
}
|
|
|
1306 |
}
|
|
|
1307 |
/**
|
|
|
1308 |
* Takes an passed params and converts it to args
|
|
|
1309 |
*
|
|
|
1310 |
* @param array $params
|
|
|
1311 |
* @return array Array containing passed and named parameters
|
|
|
1312 |
* @access public
|
|
|
1313 |
* @static
|
|
|
1314 |
*/
|
|
|
1315 |
function getArgs($args, $options = array()) {
|
|
|
1316 |
$_this =& Router::getInstance();
|
|
|
1317 |
$pass = $named = array();
|
|
|
1318 |
$args = explode('/', $args);
|
|
|
1319 |
|
|
|
1320 |
$greedy = $_this->named['greedy'];
|
|
|
1321 |
if (isset($options['greedy'])) {
|
|
|
1322 |
$greedy = $options['greedy'];
|
|
|
1323 |
}
|
|
|
1324 |
$context = array();
|
|
|
1325 |
if (isset($options['context'])) {
|
|
|
1326 |
$context = $options['context'];
|
|
|
1327 |
}
|
|
|
1328 |
$rules = $_this->named['rules'];
|
|
|
1329 |
if (isset($options['named'])) {
|
|
|
1330 |
$greedy = isset($options['greedy']) && $options['greedy'] === true;
|
|
|
1331 |
foreach ((array)$options['named'] as $key => $val) {
|
|
|
1332 |
if (is_numeric($key)) {
|
|
|
1333 |
$rules[$val] = true;
|
|
|
1334 |
continue;
|
|
|
1335 |
}
|
|
|
1336 |
$rules[$key] = $val;
|
|
|
1337 |
}
|
|
|
1338 |
}
|
|
|
1339 |
|
|
|
1340 |
foreach ($args as $param) {
|
|
|
1341 |
if (empty($param) && $param !== '0' && $param !== 0) {
|
|
|
1342 |
continue;
|
|
|
1343 |
}
|
|
|
1344 |
$param = $_this->stripEscape($param);
|
|
|
1345 |
if ((!isset($options['named']) || !empty($options['named'])) && strpos($param, $_this->named['separator']) !== false) {
|
|
|
1346 |
list($key, $val) = explode($_this->named['separator'], $param, 2);
|
|
|
1347 |
$hasRule = isset($rules[$key]);
|
|
|
1348 |
$passIt = (!$hasRule && !$greedy) || ($hasRule && !Router::matchNamed($key, $val, $rules[$key], $context));
|
|
|
1349 |
if ($passIt) {
|
|
|
1350 |
$pass[] = $param;
|
|
|
1351 |
} else {
|
|
|
1352 |
$named[$key] = $val;
|
|
|
1353 |
}
|
|
|
1354 |
} else {
|
|
|
1355 |
$pass[] = $param;
|
|
|
1356 |
}
|
|
|
1357 |
}
|
|
|
1358 |
return compact('pass', 'named');
|
|
|
1359 |
}
|
|
|
1360 |
}
|
|
|
1361 |
?>
|