1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: App::uses('Hash', 'Utility');
17:
18: 19: 20: 21: 22: 23: 24: 25: 26:
27: class CakeRoute {
28:
29: 30: 31: 32: 33: 34:
35: public $keys = array();
36:
37: 38: 39: 40: 41:
42: public $options = array();
43:
44: 45: 46: 47: 48:
49: public $defaults = array();
50:
51: 52: 53: 54: 55:
56: public $template = null;
57:
58: 59: 60: 61: 62: 63:
64: protected $_greedy = false;
65:
66: 67: 68: 69: 70:
71: protected $_compiledRoute = null;
72:
73: 74: 75: 76: 77:
78: protected $_headerMap = array(
79: 'type' => 'content_type',
80: 'method' => 'request_method',
81: 'server' => 'server_name'
82: );
83:
84: 85: 86: 87: 88: 89: 90:
91: public function __construct($template, $defaults = array(), $options = array()) {
92: $this->template = $template;
93: $this->defaults = (array)$defaults;
94: $this->options = (array)$options;
95: }
96:
97: 98: 99: 100: 101:
102: public function compiled() {
103: return !empty($this->_compiledRoute);
104: }
105:
106: 107: 108: 109: 110: 111: 112: 113:
114: public function compile() {
115: if ($this->compiled()) {
116: return $this->_compiledRoute;
117: }
118: $this->_writeRoute();
119: return $this->_compiledRoute;
120: }
121:
122: 123: 124: 125: 126: 127: 128: 129:
130: protected function _writeRoute() {
131: if (empty($this->template) || ($this->template === '/')) {
132: $this->_compiledRoute = '#^/*$#';
133: $this->keys = array();
134: return;
135: }
136: $route = $this->template;
137: $names = $routeParams = array();
138: $parsed = preg_quote($this->template, '#');
139:
140: preg_match_all('#:([A-Za-z0-9_-]+[A-Z0-9a-z])#', $route, $namedElements);
141: foreach ($namedElements[1] as $i => $name) {
142: $search = '\\' . $namedElements[0][$i];
143: if (isset($this->options[$name])) {
144: $option = null;
145: if ($name !== 'plugin' && array_key_exists($name, $this->defaults)) {
146: $option = '?';
147: }
148: $slashParam = '/\\' . $namedElements[0][$i];
149: if (strpos($parsed, $slashParam) !== false) {
150: $routeParams[$slashParam] = '(?:/(?P<' . $name . '>' . $this->options[$name] . ')' . $option . ')' . $option;
151: } else {
152: $routeParams[$search] = '(?:(?P<' . $name . '>' . $this->options[$name] . ')' . $option . ')' . $option;
153: }
154: } else {
155: $routeParams[$search] = '(?:(?P<' . $name . '>[^/]+))';
156: }
157: $names[] = $name;
158: }
159: if (preg_match('#\/\*\*$#', $route)) {
160: $parsed = preg_replace('#/\\\\\*\\\\\*$#', '(?:/(?P<_trailing_>.*))?', $parsed);
161: $this->_greedy = true;
162: }
163: if (preg_match('#\/\*$#', $route)) {
164: $parsed = preg_replace('#/\\\\\*$#', '(?:/(?P<_args_>.*))?', $parsed);
165: $this->_greedy = true;
166: }
167: krsort($routeParams);
168: $parsed = str_replace(array_keys($routeParams), array_values($routeParams), $parsed);
169: $this->_compiledRoute = '#^' . $parsed . '[/]*$#';
170: $this->keys = $names;
171:
172:
173: foreach ($this->keys as $key) {
174: unset($this->defaults[$key]);
175: }
176: }
177:
178: 179: 180: 181: 182: 183: 184: 185: 186:
187: public function parse($url) {
188: if (!$this->compiled()) {
189: $this->compile();
190: }
191: if (!preg_match($this->_compiledRoute, urldecode($url), $route)) {
192: return false;
193: }
194: foreach ($this->defaults as $key => $val) {
195: $key = (string)$key;
196: if ($key[0] === '[' && preg_match('/^\[(\w+)\]$/', $key, $header)) {
197: if (isset($this->_headerMap[$header[1]])) {
198: $header = $this->_headerMap[$header[1]];
199: } else {
200: $header = 'http_' . $header[1];
201: }
202: $header = strtoupper($header);
203:
204: $val = (array)$val;
205: $h = false;
206:
207: foreach ($val as $v) {
208: if (env($header) === $v) {
209: $h = true;
210: }
211: }
212: if (!$h) {
213: return false;
214: }
215: }
216: }
217: array_shift($route);
218: $count = count($this->keys);
219: for ($i = 0; $i <= $count; $i++) {
220: unset($route[$i]);
221: }
222: $route['pass'] = $route['named'] = array();
223:
224:
225: foreach ($this->defaults as $key => $value) {
226: if (isset($route[$key])) {
227: continue;
228: }
229: if (is_int($key)) {
230: $route['pass'][] = $value;
231: continue;
232: }
233: $route[$key] = $value;
234: }
235:
236: foreach ($this->keys as $key) {
237: if (isset($route[$key])) {
238: $route[$key] = rawurldecode($route[$key]);
239: }
240: }
241:
242: if (isset($route['_args_'])) {
243: list($pass, $named) = $this->_parseArgs($route['_args_'], $route);
244: $route['pass'] = array_merge($route['pass'], $pass);
245: $route['named'] = $named;
246: unset($route['_args_']);
247: }
248:
249: if (isset($route['_trailing_'])) {
250: $route['pass'][] = rawurldecode($route['_trailing_']);
251: unset($route['_trailing_']);
252: }
253:
254:
255: if (isset($this->options['pass'])) {
256: $j = count($this->options['pass']);
257: while ($j--) {
258: if (isset($route[$this->options['pass'][$j]])) {
259: array_unshift($route['pass'], $route[$this->options['pass'][$j]]);
260: }
261: }
262: }
263: return $route;
264: }
265:
266: 267: 268: 269: 270: 271: 272: 273:
274: protected function _parseArgs($args, $context) {
275: $pass = $named = array();
276: $args = explode('/', $args);
277:
278: $namedConfig = Router::namedConfig();
279: $greedy = $namedConfig['greedyNamed'];
280: $rules = $namedConfig['rules'];
281: if (!empty($this->options['named'])) {
282: $greedy = isset($this->options['greedyNamed']) && $this->options['greedyNamed'] === true;
283: foreach ((array)$this->options['named'] as $key => $val) {
284: if (is_numeric($key)) {
285: $rules[$val] = true;
286: continue;
287: }
288: $rules[$key] = $val;
289: }
290: }
291:
292: foreach ($args as $param) {
293: if (empty($param) && $param !== '0' && $param !== 0) {
294: continue;
295: }
296:
297: $separatorIsPresent = strpos($param, $namedConfig['separator']) !== false;
298: if ((!isset($this->options['named']) || !empty($this->options['named'])) && $separatorIsPresent) {
299: list($key, $val) = explode($namedConfig['separator'], $param, 2);
300: $key = rawurldecode($key);
301: $val = rawurldecode($val);
302: $hasRule = isset($rules[$key]);
303: $passIt = (!$hasRule && !$greedy) || ($hasRule && !$this->_matchNamed($val, $rules[$key], $context));
304: if ($passIt) {
305: $pass[] = rawurldecode($param);
306: } else {
307: if (preg_match_all('/\[([A-Za-z0-9_-]+)?\]/', $key, $matches, PREG_SET_ORDER)) {
308: $matches = array_reverse($matches);
309: $parts = explode('[', $key);
310: $key = array_shift($parts);
311: $arr = $val;
312: foreach ($matches as $match) {
313: if (empty($match[1])) {
314: $arr = array($arr);
315: } else {
316: $arr = array(
317: $match[1] => $arr
318: );
319: }
320: }
321: $val = $arr;
322: }
323: $named = array_merge_recursive($named, array($key => $val));
324: }
325: } else {
326: $pass[] = rawurldecode($param);
327: }
328: }
329: return array($pass, $named);
330: }
331:
332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342:
343: protected function _matchNamed($val, $rule, $context) {
344: if ($rule === true || $rule === false) {
345: return $rule;
346: }
347: if (is_string($rule)) {
348: $rule = array('match' => $rule);
349: }
350: if (!is_array($rule)) {
351: return false;
352: }
353:
354: $controllerMatches = (
355: !isset($rule['controller'], $context['controller']) ||
356: in_array($context['controller'], (array)$rule['controller'])
357: );
358: if (!$controllerMatches) {
359: return false;
360: }
361: $actionMatches = (
362: !isset($rule['action'], $context['action']) ||
363: in_array($context['action'], (array)$rule['action'])
364: );
365: if (!$actionMatches) {
366: return false;
367: }
368: return (!isset($rule['match']) || preg_match('/' . $rule['match'] . '/', $val));
369: }
370:
371: 372: 373: 374: 375: 376: 377: 378: 379:
380: public function persistParams($url, $params) {
381: if (empty($this->options['persist']) || !is_array($this->options['persist'])) {
382: return $url;
383: }
384: foreach ($this->options['persist'] as $persistKey) {
385: if (array_key_exists($persistKey, $params) && !isset($url[$persistKey])) {
386: $url[$persistKey] = $params[$persistKey];
387: }
388: }
389: return $url;
390: }
391:
392: 393: 394: 395: 396: 397: 398: 399: 400: 401:
402: public function match($url) {
403: if (!$this->compiled()) {
404: $this->compile();
405: }
406: $defaults = $this->defaults;
407:
408: if (isset($defaults['prefix'])) {
409: $url['prefix'] = $defaults['prefix'];
410: }
411:
412:
413: $keyNames = array_flip($this->keys);
414: if (array_intersect_key($keyNames, $url) !== $keyNames) {
415: return false;
416: }
417:
418:
419: if (array_diff_key($defaults, $url) !== array()) {
420: return false;
421: }
422:
423: $namedConfig = Router::namedConfig();
424: $prefixes = Router::prefixes();
425: $greedyNamed = $namedConfig['greedyNamed'];
426: $allowedNamedParams = $namedConfig['rules'];
427:
428: $named = $pass = array();
429:
430: foreach ($url as $key => $value) {
431:
432:
433: $defaultExists = array_key_exists($key, $defaults);
434: if ($defaultExists && $defaults[$key] != $value) {
435: return false;
436: } elseif ($defaultExists) {
437: continue;
438: }
439:
440:
441: if (array_key_exists($key, $keyNames)) {
442: continue;
443: }
444:
445:
446: $numeric = is_numeric($key);
447: if ($numeric && isset($defaults[$key]) && $defaults[$key] == $value) {
448: continue;
449: } elseif ($numeric) {
450: $pass[] = $value;
451: unset($url[$key]);
452: continue;
453: }
454:
455:
456: if (
457: ($greedyNamed || isset($allowedNamedParams[$key])) &&
458: ($value !== false && $value !== null) &&
459: (!in_array($key, $prefixes))
460: ) {
461: $named[$key] = $value;
462: continue;
463: }
464:
465:
466: if (!$defaultExists && !empty($value)) {
467: return false;
468: }
469: }
470:
471:
472: if (!$this->_greedy && (!empty($pass) || !empty($named))) {
473: return false;
474: }
475:
476:
477: if (!empty($this->options)) {
478: foreach ($this->options as $key => $pattern) {
479: if (array_key_exists($key, $url) && !preg_match('#^' . $pattern . '$#', $url[$key])) {
480: return false;
481: }
482: }
483: }
484: return $this->_writeUrl(array_merge($url, compact('pass', 'named')));
485: }
486:
487: 488: 489: 490: 491: 492: 493: 494: 495:
496: protected function _writeUrl($params) {
497: if (isset($params['prefix'])) {
498: $prefixed = $params['prefix'] . '_';
499: }
500: if (isset($prefixed, $params['action']) && strpos($params['action'], $prefixed) === 0) {
501: $params['action'] = substr($params['action'], strlen($prefixed) * -1);
502: unset($params['prefix']);
503: }
504:
505: if (is_array($params['pass'])) {
506: $params['pass'] = implode('/', array_map('rawurlencode', $params['pass']));
507: }
508:
509: $namedConfig = Router::namedConfig();
510: $separator = $namedConfig['separator'];
511:
512: if (!empty($params['named']) && is_array($params['named'])) {
513: $named = array();
514: foreach ($params['named'] as $key => $value) {
515: if (is_array($value)) {
516: $flat = Hash::flatten($value, '%5D%5B');
517: foreach ($flat as $namedKey => $namedValue) {
518: $named[] = $key . "%5B{$namedKey}%5D" . $separator . rawurlencode($namedValue);
519: }
520: } else {
521: $named[] = $key . $separator . rawurlencode($value);
522: }
523: }
524: $params['pass'] = $params['pass'] . '/' . implode('/', $named);
525: }
526: $out = $this->template;
527:
528: $search = $replace = array();
529: foreach ($this->keys as $key) {
530: $string = null;
531: if (isset($params[$key])) {
532: $string = $params[$key];
533: } elseif (strpos($out, $key) != strlen($out) - strlen($key)) {
534: $key .= '/';
535: }
536: $search[] = ':' . $key;
537: $replace[] = $string;
538: }
539: $out = str_replace($search, $replace, $out);
540:
541: if (strpos($this->template, '*')) {
542: $out = str_replace('*', $params['pass'], $out);
543: }
544: $out = str_replace('//', '/', $out);
545: return $out;
546: }
547:
548: }
549: