Ejemplo n.º 1
0
 /**
  * Route current request
  * @param eRequest $request
  * @return boolean
  */
 public function route(eRequest $request, $checkOnly = false)
 {
     $request->routed = false;
     if (isset($_GET[$this->routeVar])) {
         $rawPathInfo = $_GET[$this->routeVar];
         unset($_GET[$this->routeVar]);
         $this->_urlFormat = self::FORMAT_GET;
     } else {
         $rawPathInfo = rawurldecode($request->getPathInfo());
         //$this->_urlFormat = self::FORMAT_PATH;
     }
     // Route to front page - index/index/index route
     if (!$rawPathInfo && (!$this->getMainModule() || empty($_GET))) {
         // front page settings will be detected and front page will be rendered
         $request->setRoute('index/index/index');
         $request->addRouteHistory($rawPathInfo);
         $request->routed = true;
         return true;
     }
     // max number of parts is actually 4 - module/controller/action/[additional/pathinfo/vars], here for reference only
     $parts = $rawPathInfo ? explode('/', $rawPathInfo, 4) : array();
     // find module - check aliases
     $module = $this->retrieveModule($parts[0]);
     $mainSwitch = false;
     // no module found, switch to Main module (pref) if available
     if (null === $module && $this->getMainModule() && $this->isModule($this->getMainModule())) {
         $module = $this->getMainModule();
         $rawPathInfo = $module . '/' . $rawPathInfo;
         array_unshift($parts, $module);
         $mainSwitch = true;
     }
     $request->routePathInfo = $rawPathInfo;
     // valid module
     if (null !== $module) {
         // we have valid module
         $config = $this->getConfig($module);
         // set legacy state
         eFront::isLegacy(varset($config['legacy']));
         // Don't allow single entry if required by module config
         if (vartrue($config['noSingleEntry'])) {
             $request->routed = true;
             if (!eFront::isLegacy()) {
                 $request->setRoute($this->notFoundRoute);
                 return false;
             }
             // legacy entry point - include it later in the bootstrap, legacy query string will be set to current
             $request->addRouteHistory($rawPathInfo);
             return true;
         }
         // URL format - the one set by current config overrides the auto-detection
         $format = isset($config['format']) && $config['format'] ? $config['format'] : $this->getUrlFormat();
         //remove leading module, unnecessary overhead while matching
         array_shift($parts);
         $rawPathInfo = $parts ? implode('/', $parts) : '';
         $pathInfo = $this->removeUrlSuffix($rawPathInfo, $this->urlSuffix);
         // retrieve rules if any and if needed
         $rules = $format == self::FORMAT_PATH ? $this->getRules($module) : array();
         // Further parsing may still be needed
         if (empty($rawPathInfo)) {
             $rawPathInfo = $pathInfo;
         }
         // parse callback
         if (vartrue($config['selfParse'])) {
             // controller/action[/additional/parms]
             if (vartrue($config['urlSuffix'])) {
                 $rawPathInfo = $this->removeUrlSuffix($rawPathInfo, $config['urlSuffix']);
             }
             $route = $this->configCallback($module, 'parse', array($rawPathInfo, $_GET, $request, $this, $config), $config['location']);
         } elseif ($format == self::FORMAT_GET || !$rules) {
             $route = $pathInfo;
         } elseif ($rules) {
             foreach ($rules as $rule) {
                 $route = $rule->parseUrl($this, $request, $pathInfo, $rawPathInfo);
                 if ($route !== false) {
                     eFront::isLegacy($rule->legacy);
                     // legacy include override
                     if ($rule->parseCallback) {
                         $this->configCallback($module, $rule->parseCallback, array($request), $config['location']);
                     }
                     // parse legacy query string if any
                     if (null !== $rule->legacyQuery) {
                         $obj = eDispatcher::getConfigObject($module, $config['location']);
                         // eUrlConfig::legacyQueryString set as legacy string by default in eUrlConfig::legacy() method
                         $vars = new e_vars($request->getRequestParams());
                         $vars->module = $module;
                         $vars->controller = $request->getController();
                         $vars->action = $request->getAction();
                         if ($rule->allowVars) {
                             foreach ($rule->allowVars as $key) {
                                 if (isset($_GET[$key]) && !$request->isRequestParam($key)) {
                                     // sanitize
                                     $vars->{$key} = preg_replace('/[^\\d\\w\\-]/', '', $_GET[$key]);
                                 }
                             }
                         }
                         $obj->legacyQueryString = e107::getParser()->simpleParse($rule->legacyQuery, $vars, '0');
                         unset($vars, $obj);
                     }
                     break;
                 }
             }
         }
         // append module to be registered in the request object
         if (false !== $route) {
             // don't modify if true - request directly modified by config callback
             if (!$request->routed) {
                 if (eFront::isLegacy()) {
                     $this->configCallback($module, 'legacy', array($route, $request), $config['location']);
                 }
                 $route = $module . '/' . $route;
             }
         } elseif (!$mainSwitch && vartrue($config['errorRoute'])) {
             $route = !$checkOnly ? $module . '/' . $config['errorRoute'] : false;
         }
     }
     // final fallback
     if (!vartrue($route)) {
         if ($request->routed) {
             $route = $request->getRoute();
         }
         if (!$route) {
             $route = $this->notFoundRoute;
             eFront::isLegacy('');
             // reset legacy - not found route isn't legacy call
             $request->routed = true;
             if ($checkOnly) {
                 return false;
             }
             ## Global redirect on error option
             if (e107::getPref('url_error_redirect', false) && $this->notFoundUrl) {
                 $redirect = $this->assemble($this->notFoundUrl, '', 'encode=0&full=1');
                 //echo $redirect; exit;
                 e107::getRedirect()->redirect($redirect, true, 404);
             }
         }
     }
     $request->setRoute($route);
     $request->addRouteHistory($route);
     $request->routed = true;
     return true;
 }