protected function _fixup_dir(\HTRouter\Request $request) { $utils = new \HTRouter\Utils(); // Check if it doesn't end on a slash? $url = $request->getUri(); if (!empty($url) and $url[strlen($url) - 1] != '/') { // We are fixing a directory and we aren't allowed to add a slash. No good. if ($this->getConfig()->get("DirectorySlash") == false) { return \HTRouter::STATUS_DECLINED; } // Add the extra slash to the URL $url = parse_url($url); $url['path'] .= "/"; $url = $utils->unparse_url($url); // Redirect permanently new slashed url ( http://example.org/dir => http://example.org/dir/ ) $request->appendOutHeaders("Location", $url); return \HTRouter::STATUS_HTTP_MOVED_PERMANENTLY; } // In case a subrequest throws an error $error_notfound = false; // We can safely check and match against our directory index now $names = $this->getConfig()->get("DirectoryIndex"); $names[] = self::DEFAULT_DIRECTORY_INDEX_FILE; // @TODO: Seriously wrong. This needs to be placed in config? foreach ($names as $name) { $url = $this->_updateUrl($request->getUri(), $name); $subContainer = $this->_prepareContainerForSubRequest($url); $processor = new \HTRouter\Processor($subContainer); $status = $processor->processRequest(); $subrequest = $subContainer->getRequest(); $subrequest->setStatus($status); if (is_file($subrequest->getDocumentRoot() . $subrequest->getFilename())) { $this->_container->setRequest($subrequest); return \HTRouter::STATUS_OK; } if ($subrequest->getStatus() >= 300 && $subrequest->getStatus() < 400) { $this->_container->setRequest($subrequest); return $subrequest->getStatus(); } if ($subrequest->getStatus() != \HTRouter::STATUS_HTTP_NOT_FOUND && $subrequest->getStatus() != \HTRouter::STATUS_HTTP_OK) { $error_notfound = $subrequest->getStatus(); } } // "error_notfound" is set? return error_notfound if ($error_notfound) { return $error_notfound; } // Nothing to be done. Proceed to next module return \HTRouter::STATUS_DECLINED; }
/** * This is the main entry point that routes everything. The module hooks will take care of finding * and parsing .htaccess files correctly (i hope). */ public function route() { $request = $this->_getRequest(); // Do actual running $processor = new \HTRouter\Processor($this->_container); $status = $processor->processRequest(); $request->setStatus($status); // All done. But we need to do a final check to see which (output)handler we need to fetch (not really used) $this->runHook(self::HOOK_HANDLER, self::RUNHOOK_ALL, $this->_container); // Set our $_SERVER variables correctly according to our new request information $this->_modifySuperGlobalServerVars(); // Output header($request->getProtocol() . " " . $request->getStatus() . " " . $request->getStatusLine()); foreach ($request->getOutHeaders() as $k => $v) { header("{$k}: {$v}"); } // Include file if ($request->getStatus() == self::STATUS_HTTP_OK) { $path = $this->_getRequest()->getDocumentRoot() . $this->_getRequest()->getFilename(); $closure = function ($path) { require_once $path; }; $closure($path); } if ($request->getStatus() >= 400) { $this->_print_error($request); } exit; }