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