CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 1.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 1.2
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Classes

  • AclBase
  • AclBehavior
  • AclComponent
  • AclNode
  • AclShell
  • Aco
  • AcoAction
  • AjaxHelper
  • ApcEngine
  • ApiShell
  • App
  • AppController
  • AppHelper
  • AppModel
  • Aro
  • AuthComponent
  • BakeShell
  • BehaviorCollection
  • Cache
  • CacheEngine
  • CacheHelper
  • CakeErrorController
  • CakeLog
  • CakeSchema
  • CakeSession
  • CakeSocket
  • ClassRegistry
  • Component
  • Configure
  • ConnectionManager
  • ConsoleShell
  • ContainableBehavior
  • Controller
  • ControllerTask
  • CookieComponent
  • DataSource
  • DbAcl
  • DbAclSchema
  • DbConfigTask
  • DboAdodb
  • DboDb2
  • DboFirebird
  • DboMssql
  • DboMysql
  • DboMysqlBase
  • DboMysqli
  • DboOdbc
  • DboOracle
  • DboPostgres
  • DboSource
  • DboSqlite
  • DboSybase
  • Debugger
  • EmailComponent
  • ErrorHandler
  • ExtractTask
  • File
  • FileEngine
  • Flay
  • Folder
  • FormHelper
  • Helper
  • HtmlHelper
  • HttpSocket
  • I18n
  • I18nModel
  • i18nSchema
  • I18nShell
  • Inflector
  • IniAcl
  • JavascriptHelper
  • JsHelper
  • JsHelperObject
  • L10n
  • MagicDb
  • MagicFileResource
  • MediaView
  • MemcacheEngine
  • Model
  • ModelBehavior
  • ModelTask
  • Multibyte
  • NumberHelper
  • Object
  • Overloadable
  • Overloadable2
  • PagesController
  • PaginatorHelper
  • Permission
  • PluginTask
  • ProjectTask
  • RequestHandlerComponent
  • Router
  • RssHelper
  • Sanitize
  • Scaffold
  • ScaffoldView
  • SchemaShell
  • Security
  • SecurityComponent
  • SessionComponent
  • SessionHelper
  • SessionsSchema
  • Set
  • Shell
  • String
  • TestSuiteShell
  • TestTask
  • TextHelper
  • ThemeView
  • TimeHelper
  • TranslateBehavior
  • TreeBehavior
  • Validation
  • View
  • ViewTask
  • XcacheEngine
  • Xml
  • XmlElement
  • XmlHelper
  • XmlManager
  • XmlNode
  • XmlTextNode

Functions

  • __enclose
  • make_clean_css
  • mb_encode_mimeheader
  • mb_stripos
  • mb_stristr
  • mb_strlen
  • mb_strpos
  • mb_strrchr
  • mb_strrichr
  • mb_strripos
  • mb_strrpos
  • mb_strstr
  • mb_strtolower
  • mb_strtoupper
  • mb_substr
  • mb_substr_count
  • write_css_cache
   1: <?php
   2: /* SVN FILE: $Id$ */
   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://cakephp.org)
  11:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  12:  *
  13:  * Licensed under The MIT License
  14:  * Redistributions of files must retain the above copyright notice.
  15:  *
  16:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  17:  * @link          http://cakephp.org CakePHP(tm) Project
  18:  * @package       cake
  19:  * @subpackage    cake.cake.libs
  20:  * @since         CakePHP(tm) v 0.2.9
  21:  * @version       $Revision$
  22:  * @modifiedby    $LastChangedBy$
  23:  * @lastmodified  $Date$
  24:  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
  25:  */
  26: /**
  27:  * Included libraries.
  28:  *
  29:  */
  30: if (!class_exists('Object')) {
  31:     App::import('Core', 'Object');
  32: }
  33: /**
  34:  * Parses the request URL into controller, action, and parameters.
  35:  *
  36:  * @package       cake
  37:  * @subpackage    cake.cake.libs
  38:  */
  39: class Router extends Object {
  40: /**
  41:  * Array of routes
  42:  *
  43:  * @var array
  44:  * @access public
  45:  */
  46:     var $routes = array();
  47: /**
  48:  * Caches admin setting from Configure class
  49:  *
  50:  * @var array
  51:  * @access private
  52:  */
  53:     var $__admin = null;
  54: /**
  55:  * List of action prefixes used in connected routes
  56:  *
  57:  * @var array
  58:  * @access private
  59:  */
  60:     var $__prefixes = array();
  61: /**
  62:  * Directive for Router to parse out file extensions for mapping to Content-types.
  63:  *
  64:  * @var boolean
  65:  * @access private
  66:  */
  67:     var $__parseExtensions = false;
  68: /**
  69:  * List of valid extensions to parse from a URL.  If null, any extension is allowed.
  70:  *
  71:  * @var array
  72:  * @access private
  73:  */
  74:     var $__validExtensions = null;
  75: /**
  76:  * 'Constant' regular expression definitions for named route elements
  77:  *
  78:  * @var array
  79:  * @access private
  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:  * Stores all information necessary to decide what named arguments are parsed under what conditions.
  91:  *
  92:  * @var string
  93:  * @access public
  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:  * The route matching the URL of the current request
 103:  *
 104:  * @var array
 105:  * @access private
 106:  */
 107:     var $__currentRoute = array();
 108: /**
 109:  * HTTP header shortcut map.  Used for evaluating header-based route expressions.
 110:  *
 111:  * @var array
 112:  * @access private
 113:  */
 114:     var $__headerMap = array(
 115:         'type'      => 'content_type',
 116:         'method'    => 'request_method',
 117:         'server'    => 'server_name'
 118:     );
 119: /**
 120:  * Default HTTP request method => controller action map.
 121:  *
 122:  * @var array
 123:  * @access private
 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:  * List of resource-mapped controllers
 135:  *
 136:  * @var array
 137:  * @access private
 138:  */
 139:     var $__resourceMapped = array();
 140: /**
 141:  * Maintains the parameter stack for the current request
 142:  *
 143:  * @var array
 144:  * @access private
 145:  */
 146:     var $__params = array();
 147: /**
 148:  * Maintains the path stack for the current request
 149:  *
 150:  * @var array
 151:  * @access private
 152:  */
 153:     var $__paths = array();
 154: /**
 155:  * Keeps Router state to determine if default routes have already been connected
 156:  *
 157:  * @var boolean
 158:  * @access private
 159:  */
 160:     var $__defaultsMapped = false;
 161: /**
 162:  * Gets a reference to the Router object instance
 163:  *
 164:  * @return object Object instance
 165:  * @access public
 166:  * @static
 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:  * Gets the named route elements for use in app/config/routes.php
 179:  *
 180:  * @return array Named route elements
 181:  * @access public
 182:  * @see Router::$__named
 183:  * @static
 184:  */
 185:     function getNamedExpressions() {
 186:         $_this =& Router::getInstance();
 187:         return $_this->__named;
 188:     }
 189: /**
 190:  * Returns this object's routes array. Returns false if there are no routes available.
 191:  *
 192:  * @param string $route         An empty string, or a route string "/"
 193:  * @param array $default        NULL or an array describing the default route
 194:  * @param array $params         An array matching the named elements in the route to regular expressions which that element should match.
 195:  * @see routes
 196:  * @return array            Array of routes
 197:  * @access public
 198:  * @static
 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:  * Specifies what named parameters CakePHP should be parsing. The most common setups are:
 218:  *
 219:  * Do not parse any named parameters:
 220:  * {{{ Router::connectNamed(false); }}}
 221:  *
 222:  * Parse only default parameters used for CakePHP's pagination:
 223:  * {{{ Router::connectNamed(false, array('default' => true)); }}}
 224:  *
 225:  * Parse only the page parameter if its value is a number:
 226:  * {{{ Router::connectNamed(array('page' => '[\d]+'), array('default' => false, 'greedy' => false)); }}}
 227:  *
 228:  * Parse only the page parameter no mater what.
 229:  * {{{ Router::connectNamed(array('page'), array('default' => false, 'greedy' => false)); }}}
 230:  *
 231:  * Parse only the page parameter if the current action is 'index'.
 232:  * {{{ Router::connectNamed(array('page' => array('action' => 'index')), array('default' => false, 'greedy' => false)); }}}
 233:  *
 234:  * Parse only the page parameter if the current action is 'index' and the controller is 'pages'.
 235:  * {{{ Router::connectNamed(array('page' => array('action' => 'index', 'controller' => 'pages')), array('default' => false, 'greedy' => false)); }}}
 236:  *
 237:  * @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.
 238:  * @param array $options Allows to control all settings: separator, greedy, reset, default
 239:  * @return array
 240:  * @access public
 241:  * @static
 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:  * Creates REST resource routes for the given controller(s)
 278:  *
 279:  * Options:
 280:  *
 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:  *
 285:  * @param mixed $controller A controller name or array of controller names (i.e. "Posts" or "ListItems")
 286:  * @param array $options Options to use when generating REST routes
 287:  * @return void
 288:  * @access public
 289:  * @static
 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:  * Builds a route regular expression
 313:  *
 314:  * @param string $route An empty string, or a route string "/"
 315:  * @param array $default NULL or an array describing the default route
 316:  * @param array $params An array matching the named elements in the route to regular expressions which that element should match.
 317:  * @return array
 318:  * @see routes
 319:  * @access public
 320:  * @static
 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:  * Returns the list of prefixes used in connected routes
 385:  *
 386:  * @return array A list of prefixes used in connected routes
 387:  * @access public
 388:  * @static
 389:  */
 390:     function prefixes() {
 391:         $_this =& Router::getInstance();
 392:         return $_this->__prefixes;
 393:     }
 394: /**
 395:  * Parses given URL and returns an array of controllers, action and parameters
 396:  * taken from that URL.
 397:  *
 398:  * @param string $url URL to be parsed
 399:  * @return array Parsed elements from URL
 400:  * @access public
 401:  * @static
 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:  * Checks to see if the given URL matches the given route
 489:  *
 490:  * @param array $route
 491:  * @param string $url
 492:  * @return mixed Boolean false on failure, otherwise array
 493:  * @access private
 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:  * Compiles a route by numeric key and returns the compiled expression, replacing
 527:  * the existing uncompiled route.  Do not call statically.
 528:  *
 529:  * @param integer $i
 530:  * @return array Returns an array containing the compiled route
 531:  * @access public
 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:  * Parses a file extension out of a URL, if Router::parseExtensions() is enabled.
 546:  *
 547:  * @param string $url
 548:  * @return array Returns an array containing the altered URL and the parsed extension.
 549:  * @access private
 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:  * Connects the default, built-in routes, including admin routes, and (deprecated) web services
 578:  * routes.
 579:  *
 580:  * @return void
 581:  * @access private
 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:  * Takes parameter and path information back from the Dispatcher
 616:  *
 617:  * @param array $params Parameters and path information
 618:  * @return void
 619:  * @access public
 620:  * @static
 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:  * Gets parameter information
 639:  *
 640:  * @param boolean $current Get current parameter (true)
 641:  * @return array Parameter information
 642:  * @access public
 643:  * @static
 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:  * Gets URL parameter by name
 657:  *
 658:  * @param string $name Parameter name
 659:  * @param boolean $current Current parameter
 660:  * @return string Parameter value
 661:  * @access public
 662:  * @static
 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:  * Gets path information
 673:  *
 674:  * @param boolean $current Current parameter
 675:  * @return array
 676:  * @access public
 677:  * @static
 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:  * Reloads default Router settings
 691:  *
 692:  * @access public
 693:  * @return void
 694:  * @static
 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:  * Promote a route (by default, the last one added) to the beginning of the list
 705:  *
 706:  * @param $which A zero-based array index representing the route to move. For example,
 707:  *               if 3 routes have been added, the last route would be 2.
 708:  * @return boolean Retuns false if no route exists at the position specified by $which.
 709:  * @access public
 710:  * @static
 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:  * Finds URL for specified action.
 727:  *
 728:  * Returns an URL pointing to a combination of controller and action. Param
 729:  * $url can be:
 730:  *
 731:  * - Empty - the method will find adress to actuall controller/action.
 732:  * - '/' - the method will find base URL of application.
 733:  * - A combination of controller/action - the method will find url for it.
 734:  *
 735:  * @param mixed $url Cake-relative URL, like "/products/edit/92" or "/presidents/elect/4"
 736:  *   or an array specifying any of the following: 'controller', 'action',
 737:  *   and/or 'plugin', in addition to named arguments (keyed array elements),
 738:  *   and standard URL arguments (indexed array elements)
 739:  * @param mixed $full If (bool) true, the full base URL will be prepended to the result.
 740:  *   If an array accepts the following keys
 741:  *    - escape - used when making urls embedded in html escapes query string '&'
 742:  *    - full - if true the full base URL will be prepended.
 743:  * @return string Full translated URL with base path.
 744:  * @access public
 745:  * @static
 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:             // 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 = 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:  * Maps a URL array onto a route and returns the string result, or false if no match
 934:  *
 935:  * @param array $route Route Route
 936:  * @param array $url URL URL to map
 937:  * @return mixed Result (as string) or false if no match
 938:  * @access public
 939:  * @static
 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:  * Merges URL parameters into a route string
1038:  *
1039:  * @param array $route Route
1040:  * @param array $params Parameters
1041:  * @return string Merged URL with parameters
1042:  * @access private
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:  * Takes an array of URL parameters and separates the ones that can be used as named arguments
1094:  *
1095:  * @param array $params         Associative array of URL parameters.
1096:  * @param string $controller    Name of controller being routed.  Used in scoping.
1097:  * @param string $action        Name of action being routed.  Used in scoping.
1098:  * @return array
1099:  * @access public
1100:  * @static
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:  * Return true if a given named $param's $val matches a given $rule depending on $context. Currently implemented
1119:  * rule types are controller, action and match that can be combined with each other.
1120:  *
1121:  * @param string $param The name of the named parameter
1122:  * @param string $val The value of the named parameter
1123:  * @param array $rule The rule(s) to apply, can also be a match string
1124:  * @param string $context An array with additional context information (controller / action)
1125:  * @return boolean
1126:  * @access public
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:  * Generates a well-formed querystring from $q
1152:  *
1153:  * @param mixed $q Query string
1154:  * @param array $extra Extra querystring parameters.
1155:  * @param bool $escape Whether or not to use escaped &
1156:  * @return array
1157:  * @access public
1158:  * @static
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 = '&amp;';
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:  * Normalizes a URL for purposes of comparison.  Will strip the base path off
1184:  * and replace any double /'s.  It will not unify the casing and underscoring
1185:  * of the input value.
1186:  *
1187:  * @param mixed $url URL to normalize Either an array or a string url.
1188:  * @return string Normalized URL
1189:  * @access public
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:  * Returns the route matching the current request URL.
1216:  *
1217:  * @return array Matching route
1218:  * @access public
1219:  * @static
1220:  */
1221:     function requestRoute() {
1222:         $_this =& Router::getInstance();
1223:         return $_this->__currentRoute[0];
1224:     }
1225: /**
1226:  * Returns the route matching the current request (useful for requestAction traces)
1227:  *
1228:  * @return array Matching route
1229:  * @access public
1230:  * @static
1231:  */
1232:     function currentRoute() {
1233:         $_this =& Router::getInstance();
1234:         return $_this->__currentRoute[count($_this->__currentRoute) - 1];
1235:     }
1236: /**
1237:  * Removes the plugin name from the base URL.
1238:  *
1239:  * @param string $base Base URL
1240:  * @param string $plugin Plugin name
1241:  * @return base url with plugin name removed if present
1242:  * @access public
1243:  * @static
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:  * Strip escape characters from parameter values.
1260:  *
1261:  * @param mixed $param Either an array, or a string
1262:  * @return mixed Array or string escaped
1263:  * @access public
1264:  * @static
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:  * Instructs the router to parse out file extensions from the URL. For example,
1289:  * http://example.com/posts.rss would yield an file extension of "rss".
1290:  * The file extension itself is made available in the controller as
1291:  * $this->params['url']['ext'], and is used by the RequestHandler component to
1292:  * automatically switch to alternate layouts and templates, and load helpers
1293:  * corresponding to the given content, i.e. RssHelper.
1294:  *
1295:  * A list of valid extension can be passed to this method, i.e. Router::parseExtensions('rss', 'xml');
1296:  * If no parameters are given, anything after the first . (dot) after the last / in the URL will be
1297:  * parsed, excluding querystring parameters (i.e. ?q=...).
1298:  *
1299:  * @access public
1300:  * @return void
1301:  * @static
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:  * Takes an passed params and converts it to args
1312:  *
1313:  * @param array $params
1314:  * @return array Array containing passed and named parameters
1315:  * @access public
1316:  * @static
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: ?>
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs