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