1: <?php
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: if (!class_exists('Object')) {
31: App::import('Core', 'Object');
32: }
33: 34: 35: 36: 37: 38:
39: class Router extends Object {
40: 41: 42: 43: 44: 45:
46: var $routes = array();
47: 48: 49: 50: 51: 52:
53: var $__admin = null;
54: 55: 56: 57: 58: 59:
60: var $__prefixes = array();
61: 62: 63: 64: 65: 66:
67: var $__parseExtensions = false;
68: 69: 70: 71: 72: 73:
74: var $__validExtensions = null;
75: 76: 77: 78: 79: 80:
81: var $__named = array(
82: 'Action' => 'index|show|add|create|edit|update|remove|del|delete|view|item',
83: 'Year' => '[12][0-9]{3}',
84: 'Month' => '0[1-9]|1[012]',
85: 'Day' => '0[1-9]|[12][0-9]|3[01]',
86: 'ID' => '[0-9]+',
87: '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}'
88: );
89: 90: 91: 92: 93: 94:
95: var $named = array(
96: 'default' => array('page', 'fields', 'order', 'limit', 'recursive', 'sort', 'direction', 'step'),
97: 'greedy' => true,
98: 'separator' => ':',
99: 'rules' => false,
100: );
101: 102: 103: 104: 105: 106:
107: var $__currentRoute = array();
108: 109: 110: 111: 112: 113:
114: var $__headerMap = array(
115: 'type' => 'content_type',
116: 'method' => 'request_method',
117: 'server' => 'server_name'
118: );
119: 120: 121: 122: 123: 124:
125: var $__resourceMap = array(
126: array('action' => 'index', 'method' => 'GET', 'id' => false),
127: array('action' => 'view', 'method' => 'GET', 'id' => true),
128: array('action' => 'add', 'method' => 'POST', 'id' => false),
129: array('action' => 'edit', 'method' => 'PUT', 'id' => true),
130: array('action' => 'delete', 'method' => 'DELETE', 'id' => true),
131: array('action' => 'edit', 'method' => 'POST', 'id' => true)
132: );
133: 134: 135: 136: 137: 138:
139: var $__resourceMapped = array();
140: 141: 142: 143: 144: 145:
146: var $__params = array();
147: 148: 149: 150: 151: 152:
153: var $__paths = array();
154: 155: 156: 157: 158: 159:
160: var $__defaultsMapped = false;
161: 162: 163: 164: 165: 166: 167:
168: function &getInstance() {
169: static $instance = array();
170:
171: if (!$instance) {
172: $instance[0] =& new Router();
173: $instance[0]->__admin = Configure::read('Routing.admin');
174: }
175: return $instance[0];
176: }
177: 178: 179: 180: 181: 182: 183: 184:
185: function getNamedExpressions() {
186: $_this =& Router::getInstance();
187: return $_this->__named;
188: }
189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199:
200: function connect($route, $default = array(), $params = array()) {
201: $_this =& Router::getInstance();
202:
203: if (!isset($default['action'])) {
204: $default['action'] = 'index';
205: }
206: if (isset($default[$_this->__admin])) {
207: $default['prefix'] = $_this->__admin;
208: }
209: if (isset($default['prefix'])) {
210: $_this->__prefixes[] = $default['prefix'];
211: $_this->__prefixes = array_keys(array_flip($_this->__prefixes));
212: }
213: $_this->routes[] = array($route, $default, $params);
214: return $_this->routes;
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: function connectNamed($named, $options = array()) {
244: $_this =& Router::getInstance();
245:
246: if (isset($options['argSeparator'])) {
247: $_this->named['separator'] = $options['argSeparator'];
248: unset($options['argSeparator']);
249: }
250:
251: if ($named === true || $named === false) {
252: $options = array_merge(array('default' => $named, 'reset' => true, 'greedy' => $named), $options);
253: $named = array();
254: } else {
255: $options = array_merge(array('default' => false, 'reset' => false, 'greedy' => true), $options);
256: }
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: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290:
291: function mapResources($controller, $options = array()) {
292: $_this =& Router::getInstance();
293: $options = array_merge(array('prefix' => '/', 'id' => $_this->__named['ID'] . '|' . $_this->__named['UUID']), $options);
294: $prefix = $options['prefix'];
295:
296: foreach ((array)$controller as $ctlName) {
297: $urlName = Inflector::underscore($ctlName);
298:
299: foreach ($_this->__resourceMap as $params) {
300: extract($params);
301: $url = $prefix . $urlName . (($id) ? '/:id' : '');
302:
303: Router::connect($url,
304: array('controller' => $urlName, 'action' => $action, '[method]' => $params['method']),
305: array('id' => $options['id'], 'pass' => array('id'))
306: );
307: }
308: $_this->__resourceMapped[] = $urlName;
309: }
310: }
311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321:
322: function writeRoute($route, $default, $params) {
323: if (empty($route) || ($route === '/')) {
324: return array('/^[\/]*$/', array());
325: }
326: $names = array();
327: $elements = explode('/', $route);
328:
329: foreach ($elements as $element) {
330: if (empty($element)) {
331: continue;
332: }
333: $q = null;
334: $element = trim($element);
335: $namedParam = strpos($element, ':') !== false;
336:
337: if ($namedParam && preg_match('/^:([^:]+)$/', $element, $r)) {
338: if (isset($params[$r[1]])) {
339: if ($r[1] != 'plugin' && array_key_exists($r[1], $default)) {
340: $q = '?';
341: }
342: $parsed[] = '(?:/(' . $params[$r[1]] . ')' . $q . ')' . $q;
343: } else {
344: $parsed[] = '(?:/([^\/]+))?';
345: }
346: $names[] = $r[1];
347: } elseif ($element === '*') {
348: $parsed[] = '(?:/(.*))?';
349: } else if ($namedParam && preg_match_all('/(?!\\\\):([a-z_0-9]+)/i', $element, $matches)) {
350: $matchCount = count($matches[1]);
351:
352: foreach ($matches[1] as $i => $name) {
353: $pos = strpos($element, ':' . $name);
354: $before = substr($element, 0, $pos);
355: $element = substr($element, $pos + strlen($name) + 1);
356: $after = null;
357:
358: if ($i + 1 === $matchCount && $element) {
359: $after = preg_quote($element);
360: }
361:
362: if ($i === 0) {
363: $before = '/' . $before;
364: }
365: $before = preg_quote($before, '#');
366:
367: if (isset($params[$name])) {
368: if (isset($default[$name]) && $name != 'plugin') {
369: $q = '?';
370: }
371: $parsed[] = '(?:' . $before . '(' . $params[$name] . ')' . $q . $after . ')' . $q;
372: } else {
373: $parsed[] = '(?:' . $before . '([^\/]+)' . $after . ')?';
374: }
375: $names[] = $name;
376: }
377: } else {
378: $parsed[] = '/' . $element;
379: }
380: }
381: return array('#^' . implode('', $parsed) . '[\/]*$#', $names);
382: }
383: 384: 385: 386: 387: 388: 389:
390: function prefixes() {
391: $_this =& Router::getInstance();
392: return $_this->__prefixes;
393: }
394: 395: 396: 397: 398: 399: 400: 401: 402:
403: function parse($url) {
404: $_this =& Router::getInstance();
405: if (!$_this->__defaultsMapped) {
406: $_this->__connectDefaultRoutes();
407: }
408: $out = array('pass' => array(), 'named' => array());
409: $r = $ext = null;
410:
411: if (ini_get('magic_quotes_gpc') === '1') {
412: $url = stripslashes_deep($url);
413: }
414:
415: if ($url && strpos($url, '/') !== 0) {
416: $url = '/' . $url;
417: }
418: if (strpos($url, '?') !== false) {
419: $url = substr($url, 0, strpos($url, '?'));
420: }
421: extract($_this->__parseExtension($url));
422:
423: foreach ($_this->routes as $i => $route) {
424: if (count($route) === 3) {
425: $route = $_this->compile($i);
426: }
427:
428: if (($r = $_this->__matchRoute($route, $url)) !== false) {
429: $_this->__currentRoute[] = $route;
430: list($route, $regexp, $names, $defaults, $params) = $route;
431: $argOptions = array();
432:
433: if (array_key_exists('named', $params)) {
434: $argOptions['named'] = $params['named'];
435: unset($params['named']);
436: }
437: if (array_key_exists('greedy', $params)) {
438: $argOptions['greedy'] = $params['greedy'];
439: unset($params['greedy']);
440: }
441: array_shift($r);
442:
443: foreach ($names as $name) {
444: $out[$name] = null;
445: }
446: if (is_array($defaults)) {
447: foreach ($defaults as $name => $value) {
448: if (preg_match('#[a-zA-Z_\-]#i', $name)) {
449: $out[$name] = $value;
450: } else {
451: $out['pass'][] = $value;
452: }
453: }
454: }
455:
456: foreach ($r as $key => $found) {
457: if (empty($found) && $found != 0) {
458: continue;
459: }
460:
461: if (isset($names[$key])) {
462: $out[$names[$key]] = $_this->stripEscape($found);
463: } else {
464: $argOptions['context'] = array('action' => $out['action'], 'controller' => $out['controller']);
465: extract($_this->getArgs($found, $argOptions));
466: $out['pass'] = array_merge($out['pass'], $pass);
467: $out['named'] = $named;
468: }
469: }
470:
471: if (isset($params['pass'])) {
472: for ($j = count($params['pass']) - 1; $j > -1; $j--) {
473: if (isset($out[$params['pass'][$j]])) {
474: array_unshift($out['pass'], $out[$params['pass'][$j]]);
475: }
476: }
477: }
478: break;
479: }
480: }
481:
482: if (!empty($ext)) {
483: $out['url']['ext'] = $ext;
484: }
485: return $out;
486: }
487: 488: 489: 490: 491: 492: 493: 494:
495: function __matchRoute($route, $url) {
496: list($route, $regexp, $names, $defaults) = $route;
497:
498: if (!preg_match($regexp, $url, $r)) {
499: return false;
500: } else {
501: foreach ($defaults as $key => $val) {
502: if ($key{0} === '[' && preg_match('/^\[(\w+)\]$/', $key, $header)) {
503: if (isset($this->__headerMap[$header[1]])) {
504: $header = $this->__headerMap[$header[1]];
505: } else {
506: $header = 'http_' . $header[1];
507: }
508:
509: $val = (array)$val;
510: $h = false;
511:
512: foreach ($val as $v) {
513: if (env(strtoupper($header)) === $v) {
514: $h = true;
515: }
516: }
517: if (!$h) {
518: return false;
519: }
520: }
521: }
522: }
523: return $r;
524: }
525: 526: 527: 528: 529: 530: 531: 532:
533: function compile($i) {
534: $route = $this->routes[$i];
535:
536: list($pattern, $names) = $this->writeRoute($route[0], $route[1], $route[2]);
537: $this->routes[$i] = array(
538: $route[0], $pattern, $names,
539: array_merge(array('plugin' => null, 'controller' => null), (array)$route[1]),
540: $route[2]
541: );
542: return $this->routes[$i];
543: }
544: 545: 546: 547: 548: 549: 550:
551: function __parseExtension($url) {
552: $ext = null;
553:
554: if ($this->__parseExtensions) {
555: if (preg_match('/\.[0-9a-zA-Z]*$/', $url, $match) === 1) {
556: $match = substr($match[0], 1);
557: if (empty($this->__validExtensions)) {
558: $url = substr($url, 0, strpos($url, '.' . $match));
559: $ext = $match;
560: } else {
561: foreach ($this->__validExtensions as $name) {
562: if (strcasecmp($name, $match) === 0) {
563: $url = substr($url, 0, strpos($url, '.' . $name));
564: $ext = $match;
565: break;
566: }
567: }
568: }
569: }
570: if (empty($ext)) {
571: $ext = 'html';
572: }
573: }
574: return compact('ext', 'url');
575: }
576: 577: 578: 579: 580: 581: 582:
583: function __connectDefaultRoutes() {
584: if ($this->__admin) {
585: $params = array('prefix' => $this->__admin, $this->__admin => true);
586: }
587:
588: if ($plugins = Configure::listObjects('plugin')) {
589: foreach ($plugins as $key => $value) {
590: $plugins[$key] = Inflector::underscore($value);
591: }
592:
593: $match = array('plugin' => implode('|', $plugins));
594: $this->connect('/:plugin/:controller/:action/*', array(), $match);
595:
596: if ($this->__admin) {
597: $this->connect("/{$this->__admin}/:plugin/:controller", $params, $match);
598: $this->connect("/{$this->__admin}/:plugin/:controller/:action/*", $params, $match);
599: }
600: }
601:
602: if ($this->__admin) {
603: $this->connect("/{$this->__admin}/:controller", $params);
604: $this->connect("/{$this->__admin}/:controller/:action/*", $params);
605: }
606: $this->connect('/:controller', array('action' => 'index'));
607: $this->connect('/:controller/:action/*');
608:
609: if ($this->named['rules'] === false) {
610: $this->connectNamed(true);
611: }
612: $this->__defaultsMapped = true;
613: }
614: 615: 616: 617: 618: 619: 620: 621:
622: function setRequestInfo($params) {
623: $_this =& Router::getInstance();
624: $defaults = array('plugin' => null, 'controller' => null, 'action' => null);
625: $params[0] = array_merge($defaults, (array)$params[0]);
626: $params[1] = array_merge($defaults, (array)$params[1]);
627: list($_this->__params[], $_this->__paths[]) = $params;
628:
629: if (count($_this->__paths)) {
630: if (isset($_this->__paths[0]['namedArgs'])) {
631: foreach ($_this->__paths[0]['namedArgs'] as $arg => $value) {
632: $_this->named['rules'][$arg] = true;
633: }
634: }
635: }
636: }
637: 638: 639: 640: 641: 642: 643: 644:
645: function getParams($current = false) {
646: $_this =& Router::getInstance();
647: if ($current) {
648: return $_this->__params[count($_this->__params) - 1];
649: }
650: if (isset($_this->__params[0])) {
651: return $_this->__params[0];
652: }
653: return array();
654: }
655: 656: 657: 658: 659: 660: 661: 662: 663:
664: function getParam($name = 'controller', $current = false) {
665: $params = Router::getParams($current);
666: if (isset($params[$name])) {
667: return $params[$name];
668: }
669: return null;
670: }
671: 672: 673: 674: 675: 676: 677: 678:
679: function getPaths($current = false) {
680: $_this =& Router::getInstance();
681: if ($current) {
682: return $_this->__paths[count($_this->__paths) - 1];
683: }
684: if (!isset($_this->__paths[0])) {
685: return array('base' => null);
686: }
687: return $_this->__paths[0];
688: }
689: 690: 691: 692: 693: 694: 695:
696: function reload() {
697: $_this =& Router::getInstance();
698: foreach (get_class_vars('Router') as $key => $val) {
699: $_this->{$key} = $val;
700: }
701: $_this->__admin = Configure::read('Routing.admin');
702: }
703: 704: 705: 706: 707: 708: 709: 710: 711:
712: function promote($which = null) {
713: $_this =& Router::getInstance();
714: if ($which === null) {
715: $which = count($_this->routes) - 1;
716: }
717: if (!isset($_this->routes[$which])) {
718: return false;
719: }
720: $route = $_this->routes[$which];
721: unset($_this->routes[$which]);
722: array_unshift($_this->routes, $route);
723: return true;
724: }
725: 726: 727: 728: 729: 730: 731: 732: 733: 734: 735: 736: 737: 738: 739: 740: 741: 742: 743: 744: 745: 746:
747: function url($url = null, $full = false) {
748: $_this =& Router::getInstance();
749: $defaults = $params = array('plugin' => null, 'controller' => null, 'action' => 'index');
750:
751: if (is_bool($full)) {
752: $escape = false;
753: } else {
754: extract(array_merge(array('escape' => false, 'full' => false), $full));
755: }
756:
757: if (!empty($_this->__params)) {
758: if (isset($this) && !isset($this->params['requested'])) {
759: $params = $_this->__params[0];
760: } else {
761: $params = end($_this->__params);
762: }
763: if (isset($params['prefix']) && strpos($params['action'], $params['prefix']) === 0) {
764: $params['action'] = substr($params['action'], strlen($params['prefix']) + 1);
765: }
766: }
767: $path = array('base' => null);
768:
769: if (!empty($_this->__paths)) {
770: if (isset($this) && !isset($this->params['requested'])) {
771: $path = $_this->__paths[0];
772: } else {
773: $path = end($_this->__paths);
774: }
775: }
776: $base = $path['base'];
777: $extension = $output = $mapped = $q = $frag = null;
778:
779: if (is_array($url)) {
780: if (isset($url['base']) && $url['base'] === false) {
781: $base = null;
782: unset($url['base']);
783: }
784: if (isset($url['full_base']) && $url['full_base'] === true) {
785: $full = true;
786: unset($url['full_base']);
787: }
788: if (isset($url['?'])) {
789: $q = $url['?'];
790: unset($url['?']);
791: }
792: if (isset($url['#'])) {
793: $frag = '#' . urlencode($url['#']);
794: unset($url['#']);
795: }
796: if (empty($url['action'])) {
797: if (empty($url['controller']) || $params['controller'] === $url['controller']) {
798: $url['action'] = $params['action'];
799: } else {
800: $url['action'] = 'index';
801: }
802: }
803: if ($_this->__admin) {
804: if (!isset($url[$_this->__admin]) && !empty($params[$_this->__admin])) {
805: $url[$_this->__admin] = true;
806: } elseif ($_this->__admin && isset($url[$_this->__admin]) && !$url[$_this->__admin]) {
807: unset($url[$_this->__admin]);
808: }
809: }
810: $plugin = false;
811:
812: if (array_key_exists('plugin', $url)) {
813: $plugin = $url['plugin'];
814: }
815:
816: $url = array_merge(array('controller' => $params['controller'], 'plugin' => $params['plugin']), Set::filter($url, true));
817:
818: if ($plugin !== false) {
819: $url['plugin'] = $plugin;
820: }
821:
822: if (isset($url['ext'])) {
823: $extension = '.' . $url['ext'];
824: unset($url['ext']);
825: }
826: $match = false;
827:
828: foreach ($_this->routes as $i => $route) {
829: if (count($route) === 3) {
830: $route = $_this->compile($i);
831: }
832: $originalUrl = $url;
833:
834: if (isset($route[4]['persist'], $_this->__params[0])) {
835: $url = array_merge(array_intersect_key($params, Set::combine($route[4]['persist'], '/')), $url);
836: }
837: if ($match = $_this->mapRouteElements($route, $url)) {
838: $output = trim($match, '/');
839: $url = array();
840: break;
841: }
842: $url = $originalUrl;
843: }
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:
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 = implode('/', $urlOut) . '/';
884: }
885:
886: if (!empty($args)) {
887: $args = implode('/', $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: $output = str_replace('//', '/', $base . '/' . $output);
900: } else {
901: if (((strpos($url, '://')) || (strpos($url, 'javascript:') === 0) || (strpos($url, 'mailto:') === 0)) || (!strncmp($url, '#', 1))) {
902: return $url;
903: }
904: if (empty($url)) {
905: if (!isset($path['here'])) {
906: $path['here'] = '/';
907: }
908: $output = $path['here'];
909: } elseif (substr($url, 0, 1) === '/') {
910: $output = $base . $url;
911: } else {
912: $output = $base . '/';
913: if ($_this->__admin && isset($params[$_this->__admin])) {
914: $output .= $_this->__admin . '/';
915: }
916: if (!empty($params['plugin']) && $params['plugin'] !== $params['controller']) {
917: $output .= Inflector::underscore($params['plugin']) . '/';
918: }
919: $output .= Inflector::underscore($params['controller']) . '/' . $url;
920: }
921: $output = str_replace('//', '/', $output);
922: }
923: if ($full && defined('FULL_BASE_URL')) {
924: $output = FULL_BASE_URL . $output;
925: }
926: if (!empty($extension) && substr($output, -1) === '/') {
927: $output = substr($output, 0, -1);
928: }
929:
930: return $output . $extension . $_this->queryString($q, array(), $escape) . $frag;
931: }
932: 933: 934: 935: 936: 937: 938: 939: 940:
941: function mapRouteElements($route, $url) {
942: if (isset($route[3]['prefix'])) {
943: $prefix = $route[3]['prefix'];
944: unset($route[3]['prefix']);
945: }
946:
947: $pass = array();
948: $defaults = $route[3];
949: $routeParams = $route[2];
950: $params = Set::diff($url, $defaults);
951: $urlInv = array_combine(array_values($url), array_keys($url));
952:
953: $i = 0;
954: while (isset($defaults[$i])) {
955: if (isset($urlInv[$defaults[$i]])) {
956: if (!in_array($defaults[$i], $url) && is_int($urlInv[$defaults[$i]])) {
957: return false;
958: }
959: unset($urlInv[$defaults[$i]], $defaults[$i]);
960: } else {
961: return false;
962: }
963: $i++;
964: }
965:
966: foreach ($params as $key => $value) {
967: if (is_int($key)) {
968: $pass[] = $value;
969: unset($params[$key]);
970: }
971: }
972: list($named, $params) = Router::getNamedElements($params);
973:
974: if (!strpos($route[0], '*') && (!empty($pass) || !empty($named))) {
975: return false;
976: }
977:
978: $urlKeys = array_keys($url);
979: $paramsKeys = array_keys($params);
980: $defaultsKeys = array_keys($defaults);
981:
982: if (!empty($params)) {
983: if (array_diff($paramsKeys, $routeParams) != array()) {
984: return false;
985: }
986: $required = array_values(array_diff($routeParams, $urlKeys));
987: $reqCount = count($required);
988:
989: for ($i = 0; $i < $reqCount; $i++) {
990: if (array_key_exists($required[$i], $defaults) && $defaults[$required[$i]] === null) {
991: unset($required[$i]);
992: }
993: }
994: }
995: $isFilled = true;
996:
997: if (!empty($routeParams)) {
998: $filled = array_intersect_key($url, array_combine($routeParams, array_keys($routeParams)));
999: $isFilled = (array_diff($routeParams, array_keys($filled)) === array());
1000: if (!$isFilled && empty($params)) {
1001: return false;
1002: }
1003: }
1004:
1005: if (empty($params)) {
1006: return Router::__mapRoute($route, array_merge($url, compact('pass', 'named', 'prefix')));
1007: } elseif (!empty($routeParams) && !empty($route[3])) {
1008:
1009: if (!empty($required)) {
1010: return false;
1011: }
1012: foreach ($params as $key => $val) {
1013: if ((!isset($url[$key]) || $url[$key] != $val) || (!isset($defaults[$key]) || $defaults[$key] != $val) && !in_array($key, $routeParams)) {
1014: if (!isset($defaults[$key])) {
1015: continue;
1016: }
1017: return false;
1018: }
1019: }
1020: } else {
1021: if (empty($required) && $defaults['plugin'] === $url['plugin'] && $defaults['controller'] === $url['controller'] && $defaults['action'] === $url['action']) {
1022: return Router::__mapRoute($route, array_merge($url, compact('pass', 'named', 'prefix')));
1023: }
1024: return false;
1025: }
1026:
1027: if (!empty($route[4])) {
1028: foreach ($route[4] as $key => $reg) {
1029: if (array_key_exists($key, $url) && !preg_match('#' . $reg . '#', $url[$key])) {
1030: return false;
1031: }
1032: }
1033: }
1034: return Router::__mapRoute($route, array_merge($filled, compact('pass', 'named', 'prefix')));
1035: }
1036: 1037: 1038: 1039: 1040: 1041: 1042: 1043:
1044: function __mapRoute($route, $params = array()) {
1045: if (isset($params['plugin']) && isset($params['controller']) && $params['plugin'] === $params['controller']) {
1046: unset($params['controller']);
1047: }
1048:
1049: if (isset($params['prefix']) && isset($params['action'])) {
1050: $params['action'] = str_replace($params['prefix'] . '_', '', $params['action']);
1051: unset($params['prefix']);
1052: }
1053:
1054: if (isset($params['pass']) && is_array($params['pass'])) {
1055: $params['pass'] = implode('/', Set::filter($params['pass'], true));
1056: } elseif (!isset($params['pass'])) {
1057: $params['pass'] = '';
1058: }
1059:
1060: if (isset($params['named'])) {
1061: if (is_array($params['named'])) {
1062: $count = count($params['named']);
1063: $keys = array_keys($params['named']);
1064: $named = array();
1065:
1066: for ($i = 0; $i < $count; $i++) {
1067: $named[] = $keys[$i] . $this->named['separator'] . $params['named'][$keys[$i]];
1068: }
1069: $params['named'] = implode('/', $named);
1070: }
1071: $params['pass'] = str_replace('//', '/', $params['pass'] . '/' . $params['named']);
1072: }
1073: $out = $route[0];
1074:
1075: foreach ($route[2] as $key) {
1076: $string = null;
1077: if (isset($params[$key])) {
1078: $string = $params[$key];
1079: unset($params[$key]);
1080: } elseif (strpos($out, $key) != strlen($out) - strlen($key)) {
1081: $key = $key . '/';
1082: }
1083: $out = str_replace(':' . $key, $string, $out);
1084: }
1085:
1086: if (strpos($route[0], '*')) {
1087: $out = str_replace('*', $params['pass'], $out);
1088: }
1089:
1090: return $out;
1091: }
1092: 1093: 1094: 1095: 1096: 1097: 1098: 1099: 1100: 1101:
1102: function getNamedElements($params, $controller = null, $action = null) {
1103: $_this =& Router::getInstance();
1104: $named = array();
1105:
1106: foreach ($params as $param => $val) {
1107: if (isset($_this->named['rules'][$param])) {
1108: $rule = $_this->named['rules'][$param];
1109: if (Router::matchNamed($param, $val, $rule, compact('controller', 'action'))) {
1110: $named[$param] = $val;
1111: unset($params[$param]);
1112: }
1113: }
1114: }
1115: return array($named, $params);
1116: }
1117: 1118: 1119: 1120: 1121: 1122: 1123: 1124: 1125: 1126: 1127:
1128: function matchNamed($param, $val, $rule, $context = array()) {
1129: if ($rule === true || $rule === false) {
1130: return $rule;
1131: }
1132: if (is_string($rule)) {
1133: $rule = array('match' => $rule);
1134: }
1135: if (!is_array($rule)) {
1136: return false;
1137: }
1138:
1139: $controllerMatches = !isset($rule['controller'], $context['controller']) || in_array($context['controller'], (array)$rule['controller']);
1140: if (!$controllerMatches) {
1141: return false;
1142: }
1143: $actionMatches = !isset($rule['action'], $context['action']) || in_array($context['action'], (array)$rule['action']);
1144: if (!$actionMatches) {
1145: return false;
1146: }
1147: $valueMatches = !isset($rule['match']) || preg_match(sprintf('/%s/', $rule['match']), $val);
1148: return $valueMatches;
1149: }
1150: 1151: 1152: 1153: 1154: 1155: 1156: 1157: 1158: 1159:
1160: function queryString($q, $extra = array(), $escape = false) {
1161: if (empty($q) && empty($extra)) {
1162: return null;
1163: }
1164: $join = '&';
1165: if ($escape === true) {
1166: $join = '&';
1167: }
1168: $out = '';
1169:
1170: if (is_array($q)) {
1171: $q = array_merge($extra, $q);
1172: } else {
1173: $out = $q;
1174: $q = $extra;
1175: }
1176: $out .= http_build_query($q, null, $join);
1177: if (isset($out[0]) && $out[0] != '?') {
1178: $out = '?' . $out;
1179: }
1180: return $out;
1181: }
1182: 1183: 1184: 1185: 1186: 1187: 1188: 1189: 1190:
1191: function normalize($url = '/') {
1192: if (is_array($url)) {
1193: $url = Router::url($url);
1194: } elseif (preg_match('/^[a-z\-]+:\/\//', $url)) {
1195: return $url;
1196: }
1197: $paths = Router::getPaths();
1198:
1199: if (!empty($paths['base']) && stristr($url, $paths['base'])) {
1200: $url = preg_replace('/^' . preg_quote($paths['base'], '/') . '/', '', $url, 1);
1201: }
1202: $url = '/' . $url;
1203:
1204: while (strpos($url, '//') !== false) {
1205: $url = str_replace('//', '/', $url);
1206: }
1207: $url = preg_replace('/(?:(\/$))/', '', $url);
1208:
1209: if (empty($url)) {
1210: return '/';
1211: }
1212: return $url;
1213: }
1214: 1215: 1216: 1217: 1218: 1219: 1220:
1221: function requestRoute() {
1222: $_this =& Router::getInstance();
1223: return $_this->__currentRoute[0];
1224: }
1225: 1226: 1227: 1228: 1229: 1230: 1231:
1232: function currentRoute() {
1233: $_this =& Router::getInstance();
1234: return $_this->__currentRoute[count($_this->__currentRoute) - 1];
1235: }
1236: 1237: 1238: 1239: 1240: 1241: 1242: 1243: 1244:
1245: function stripPlugin($base, $plugin = null) {
1246: if ($plugin != null) {
1247: $base = preg_replace('/(?:' . $plugin . ')/', '', $base);
1248: $base = str_replace('//', '', $base);
1249: $pos1 = strrpos($base, '/');
1250: $char = strlen($base) - 1;
1251:
1252: if ($pos1 === $char) {
1253: $base = substr($base, 0, $char);
1254: }
1255: }
1256: return $base;
1257: }
1258: 1259: 1260: 1261: 1262: 1263: 1264: 1265:
1266: function stripEscape($param) {
1267: $_this =& Router::getInstance();
1268: if (!is_array($param) || empty($param)) {
1269: if (is_bool($param)) {
1270: return $param;
1271: }
1272:
1273: return preg_replace('/^(?:[\\t ]*(?:-!)+)/', '', $param);
1274: }
1275:
1276: foreach ($param as $key => $value) {
1277: if (is_string($value)) {
1278: $return[$key] = preg_replace('/^(?:[\\t ]*(?:-!)+)/', '', $value);
1279: } else {
1280: foreach ($value as $array => $string) {
1281: $return[$key][$array] = $_this->stripEscape($string);
1282: }
1283: }
1284: }
1285: return $return;
1286: }
1287: 1288: 1289: 1290: 1291: 1292: 1293: 1294: 1295: 1296: 1297: 1298: 1299: 1300: 1301: 1302:
1303: function parseExtensions() {
1304: $_this =& Router::getInstance();
1305: $_this->__parseExtensions = true;
1306: if (func_num_args() > 0) {
1307: $_this->__validExtensions = func_get_args();
1308: }
1309: }
1310: 1311: 1312: 1313: 1314: 1315: 1316: 1317:
1318: function getArgs($args, $options = array()) {
1319: $_this =& Router::getInstance();
1320: $pass = $named = array();
1321: $args = explode('/', $args);
1322:
1323: $greedy = $_this->named['greedy'];
1324: if (isset($options['greedy'])) {
1325: $greedy = $options['greedy'];
1326: }
1327: $context = array();
1328: if (isset($options['context'])) {
1329: $context = $options['context'];
1330: }
1331: $rules = $_this->named['rules'];
1332: if (isset($options['named'])) {
1333: $greedy = isset($options['greedy']) && $options['greedy'] === true;
1334: foreach ((array)$options['named'] as $key => $val) {
1335: if (is_numeric($key)) {
1336: $rules[$val] = true;
1337: continue;
1338: }
1339: $rules[$key] = $val;
1340: }
1341: }
1342:
1343: foreach ($args as $param) {
1344: if (empty($param) && $param !== '0' && $param !== 0) {
1345: continue;
1346: }
1347: $param = $_this->stripEscape($param);
1348:
1349: $separatorIsPresent = strpos($param, $_this->named['separator']) !== false;
1350: if ((!isset($options['named']) || !empty($options['named'])) && $separatorIsPresent) {
1351: list($key, $val) = explode($_this->named['separator'], $param, 2);
1352: $hasRule = isset($rules[$key]);
1353: $passIt = (!$hasRule && !$greedy) || ($hasRule && !Router::matchNamed($key, $val, $rules[$key], $context));
1354: if ($passIt) {
1355: $pass[] = $param;
1356: } else {
1357: $named[$key] = $val;
1358: }
1359: } else {
1360: $pass[] = $param;
1361: }
1362: }
1363: return compact('pass', 'named');
1364: }
1365: }
1366: ?>