/** * Get the Typeframe_Response object for the specified URL. * @param string $url * @return \Typeframe_Response */ public static function At($url) { $response = new Typeframe_Response(); if ($url == '') { $url = '/'; } if (substr($url, 0, 1) == '/') { if (isset($_SERVER['HTTP_HOST']) && defined('TYPEF_HOST')) { $url = $_SERVER['HTTP_HOST'] . $url; } else { $url = (defined('TYPEF_HOST') ? TYPEF_HOST : '') . $url; } } else { if (substr($url, 0, 7) == 'http://' || substr($url, 0, 8) == 'https://') { $parts = parse_url($url); $url = $parts['domain'] . $parts['path'] . (!empty($parts['query']) ? '?' . $parts['query'] : ''); } } if ($url != '/' && substr($url, -1, 1) == '/') { $url = substr($url, 0, -1); } $response->_url = $url; if (substr($response->_url, -1) == '/') { $response->_url = substr($response->_url, 0, -1); } $pages = Typeframe::Registry()->pages(); /*if (substr($url, 0, 1) != '/') { $host = substr($url, 0, strpos($url, '/')); } else { $host = ''; }*/ $host = defined('TYPEF_HOST') ? @$_SERVER['HTTP_HOST'] : ''; $uri = strpos($url, '?') !== false ? substr($url, 0, strlen(strpos($url, '?'))) : $url; $uri = preg_replace('/\\/+/', '/', $uri); if (substr($uri, 0, -1) == '/') { $uri = substr($uri, 0, -1); } $relativeUri = substr($uri, strlen(TYPEF_WEB_DIR) + (defined('TYPEF_HOST') ? strlen($host ? $host : TYPEF_HOST) : 0)); $dirs = explode('/', $relativeUri); while (count($dirs)) { $uri = strpos($url, '?') !== false ? substr($url, 0, strlen(strpos($url, '?'))) : $url; $uri = preg_replace('/\\/+/', '/', $uri); if (substr($uri, 0, -1) == '/') { $uri = substr($uri, 0, -1); } $relativeUri = substr($uri, strpos($uri, '/') + strlen(TYPEF_WEB_DIR)); $currentPath = $host . TYPEF_WEB_DIR . '/' . implode('/', $dirs); $currentPath = preg_replace('/\\/+/', '/', $currentPath); if (substr($currentPath, -1) == '/') { $currentPath = substr($currentPath, 0, -1); } if (!isset($pages[$currentPath]) && substr($currentPath, 0, strlen($host)) == $host) { $currentPath = substr($currentPath, strlen($host)); $uri = substr($uri, strlen($host)); } if (isset($pages[$currentPath])) { $currentPage = $pages[$currentPath]; $uriWithoutDomain = substr($uri, 0, 1) == '/' ? $uri : substr($uri, strlen($host)); $controllerPath = null; if ($currentPath && strlen($uri) > strlen($currentPath) && strpos($uri, $currentPath) === 0) { $controllerPath = TYPEF_SOURCE_DIR . '/controllers' . $currentPage->application()->base() . substr($uriWithoutDomain, strlen($currentPage->uri())); if (file_exists($controllerPath . '.php') && is_file($controllerPath . '.php')) { $response->_controller = $controllerPath . '.php'; $response->_page = $currentPage; break; } else { if (file_exists($controllerPath . '/index.php') && is_file($controllerPath . '/index.php')) { $response->_controller = $controllerPath . '/index.php'; $response->_page = $currentPage; break; } else { $controllerPath = null; foreach ($currentPage->extenders() as $extender) { if (defined('TYPEF_HOST') && substr($uri, 0, strlen($host)) == $host) { $pathinfo = substr($uri, strlen($host) + strlen($extender->path())); } else { $pathinfo = substr($uri, strlen($extender->path())); } //if (substr($uri, strlen($host), strlen($extender->path())) == $extender->path()) { //$extended = substr($uri, strlen($host) + strlen($extender->path())); //if (preg_match($extender->preg(), $extended)) { if (preg_match($extender->preg(), $pathinfo)) { $response->_pathInfo = substr($pathinfo, 1); if ($extender->redirect()) { $controllerPath = TYPEF_SOURCE_DIR . '/controllers' . $extender->redirect(); } else { $controllerPath = TYPEF_SOURCE_DIR . '/controllers' . $currentPage->application()->base(); } break; } //} } } } } else { if ($currentPath == $uri) { $controllerPath = TYPEF_SOURCE_DIR . '/controllers' . $currentPage->application()->base(); if (file_exists($controllerPath . '.php') && is_file($controllerPath . '.php')) { $response->_controller = $controllerPath . '.php'; $response->_page = $currentPage; break; } else { if (file_exists($controllerPath . '/index.php') && is_file($controllerPath . '/index.php')) { $response->_controller = $controllerPath . '/index.php'; $response->_page = $currentPage; break; } } } else { $controllerPath = TYPEF_SOURCE_DIR . '/controllers' . $currentPage->application()->base(); $response->_pathInfo = substr($uri, strlen($currentPath) + 1); } } if (!is_null($controllerPath)) { if (file_exists($controllerPath . '.php') && is_file($controllerPath . '.php')) { $response->_controller = $controllerPath . '.php'; } else { if (file_exists($controllerPath . '/index.php') && is_file($controllerPath . '/index.php')) { $response->_controller = $controllerPath . '/index.php'; } } } if ($response->_controller) { $response->_page = $currentPage; break; } } array_pop($dirs); } if ($response->_controller && $response->_page) { $response->_controller = preg_replace('/\\/+/', '/', $response->_controller); if (!$response->_page->allow()) { $response->_return403(); } } else { $response->_return404(); } return $response; }