Пример #1
0
 /**
  * Tests if this Route matches the given path
  *
  * @param string $path
  *
  * @return boolean
  */
 private function matchPath($path)
 {
     list($basePath, $querystring) = array_pad(explode('?', $path, 2), 2, null);
     if ($this->path == '*') {
         $subdomainPath = UrlHelper::convertUrlToPath($path);
         $testPath = $this->controller . '/' . $subdomainPath;
         if (is_dir(CONTROLLER_PATH . $testPath) && is_readable(CONTROLLER_PATH . $testPath . '/controller.php')) {
             $this->controller = $testPath;
             return true;
         }
     }
     preg_match_all("|<([-_a-zA-Z0-9]+)>|", $this->path, $namedParameterMatches);
     if ($namedParameterMatches[0]) {
         $pathRegex = $this->path;
         $pathVariables = array();
         foreach ($namedParameterMatches[1] as $capture) {
             $pathVariables[] = $capture;
             if (array_key_exists($capture, $this->regexArray)) {
                 $pathRegex = str_replace('<' . $capture . '>', $this->regexArray[$capture], $pathRegex);
             } else {
                 $pathRegex = str_replace('<' . $capture . '>', "([^\\.\\?\\/]+)", $pathRegex);
             }
         }
         //try matching url ignoring querystring
         if (preg_match('|^' . $pathRegex . '$|', $basePath, $pathMatches)) {
             array_shift($pathMatches);
             foreach ($pathMatches as $match) {
                 $this->pathVariables[array_shift($pathVariables)] = $match;
             }
             return true;
         }
         //try again using querystring
         if (preg_match('|^' . $pathRegex . '$|', $path, $pathMatches)) {
             array_shift($pathMatches);
             foreach ($pathMatches as $match) {
                 $this->pathVariables[array_shift($pathVariables)] = $match;
             }
             return true;
         }
     } elseif ($this->path == $path || $this->path == $basePath) {
         return true;
     } elseif (empty($this->path) && empty($basePath)) {
         return true;
     }
     return false;
 }
Пример #2
0
 /**
  * Finds and loads a Controller for the given http request
  * @return boolean
  */
 public function dispatch()
 {
     include_once BOOTSTRAP_FILE;
     $this->_domain = $_SERVER['HTTP_HOST'];
     $this->_url = $_SERVER['REQUEST_URI'];
     $this->_args = $_GET;
     $route = $this->findCustomRoute();
     if ($route) {
         //handle custom route
         if ($route->isRedirect()) {
             RequestHelper::redirect($route->httpStatus, $route->redirectLocation);
         } else {
             include CONTROLLER_PATH . $route->controller . '/controller.php';
             $template = $route->controller . '/view';
             $controller = new page();
             $controller->set_view($template);
             $controller->set_args($route->pathVariables);
             $controller->get();
         }
     } else {
         //custom route not found, map url to filesystem
         //auto map subdomains to _subdomain_ folders
         $subdomain = str_replace(Config::get('NAKED_DOMAIN'), '', $this->_domain);
         $subdomainFolder = "";
         if (substr($subdomain, -1) == '.') {
             $subdomain = substr($subdomain, 0, -1);
         }
         if ($subdomain != Config::get('DEFAULT_SUBDOMAIN')) {
             //redirect naked domain to default subdomain, if necessary;
             if (empty($subdomain)) {
                 RequestHelper::redirect(UrlHelper::uri($this->_url));
                 die;
             } else {
                 $subdomainFolder = '_' . $subdomain . '_/';
                 if (!is_dir(CONTROLLER_PATH . $subdomainFolder)) {
                     if (is_dir(CONTROLLER_PATH . '_*_/')) {
                         include CONTROLLER_PATH . '_*_/controller.php';
                         $template = CONTROLLER_PATH . '_*_/view';
                         $controller = new page();
                         $controller->set_view($template);
                         $controller->set_args(array('subdomain' => $subdomain));
                         $controller->get();
                         die;
                     } else {
                         $this->throwError('404');
                     }
                 }
             }
         }
         //on default subdomain, so continue
         $path = UrlHelper::convertUrlToPath($this->_url);
         if (empty($path)) {
             if (is_dir(CONTROLLER_PATH . $subdomainFolder) && is_readable(CONTROLLER_PATH . $subdomainFolder . 'controller.php')) {
                 include CONTROLLER_PATH . $subdomainFolder . 'controller.php';
                 $template = $subdomainFolder . 'view';
             } else {
                 $this->throwError('404');
             }
         } elseif (is_dir(CONTROLLER_PATH . $subdomainFolder . $path) && is_readable(CONTROLLER_PATH . $subdomainFolder . $path . '/controller.php')) {
             include CONTROLLER_PATH . $subdomainFolder . $path . '/controller.php';
             $template = $subdomainFolder . $path . DS . 'view';
         } elseif (is_readable(CONTROLLER_PATH . $subdomainFolder . $path)) {
             $file = CONTROLLER_PATH . $subdomainFolder . $path;
             if (function_exists('finfo_open')) {
                 $finfo = finfo_open(FILEINFO_MIME_TYPE);
                 // return mime type ala mimetype extension
                 header("Content-type: " . finfo_file($finfo, $file));
                 finfo_close($finfo);
             } elseif (function_exists('mime_content_type')) {
                 header("Content-type: " . mime_content_type($file));
             } else {
                 header("Content-type: text/plain");
             }
             readfile($file);
             die;
         } else {
             $this->throwError('404');
             return false;
         }
         $controller = new page();
         $controller->set_view($template);
         $controller->get();
     }
     return true;
 }