function fixUp(\HTRouter\Request $request) { if ($this->getConfig()->get("RewriteEngine") == false) { return \HTRouter::STATUS_DECLINED; } // Temp save $oldFilename = $request->getFilename(); if (!$request->getFilename()) { $request->setFilename($request->getUri()); } $ruleStatus = $this->_applyRewrites(); if ($ruleStatus) { if ($ruleStatus == self::ACTION_STATUS) { $n = $request->getStatus(); $request->setStatus(\HTROUTER::STATUS_HTTP_OK); return $n; } if (($skip = $this->_is_absolute_url($request->getFilename())) > 0) { if ($ruleStatus == self::ACTION_NOESCAPE) { $request->setFilename(urlencode($request->getFilename(), $skip)); } // Add query string if needed if ($request->getArgs()) { if ($ruleStatus == self::ACTION_NOESCAPE) { $request->setFilename($request->getFilename() . "?" . $request->getQueryString()); } else { $request->setFilename($request->getFilename() . "?" . urlencode($request->getQueryString())); } } // Is this a redirect? if ($request->getStatus() >= 300 && $request->getStatus() < 400) { $n = $request->getStatus(); $request->setStatus(\HTRouter::STATUS_HTTP_OK); } else { // No redirect, but we need to redir anyway.. $n = \HTRouter::STATUS_HTTP_MOVED_TEMPORARILY; } // The filename is the URI to redirect.. strange, I know... $request->appendOutHeaders("Location", $request->getFilename()); return $n; } elseif (substr($request->getFilename(), 0, 12) == "passthrough:") { // Starts with passthrough? Let's pass $request->setUri(substr($request->getFilename(), 13)); return \HTRouter::STATUS_DECLINED; } else { // Local path if ($oldFilename == $request->getFilename()) { // Rewrite to the same name. Prevent deadlocks return \HTRouter::STATUS_HTTP_OK; } } } else { $request->getFilename($oldFilename); return \HTRouter::STATUS_DECLINED; } return \HTRouter::STATUS_DECLINED; }
/** * Initialize the request with standard values taken from the $_SERVER. * * @param HTRouter\Request $request The request to be filled */ protected function _populateInitialRequest(\HTRouter\Request $request) { /** * A lot of stuff is already filtered by either apache or the built-in webserver. We just have to * populate our request so we have a generic state which we can work with. From this point on, it * should never matter on what kind of webserver we are actually working on (in fact: this can be * the base of writing your own webserver like nanoweb) */ $routerConfig = $this->_getRouterConfig(); // By default, we don't have any authentication //$request->setAuthType(null); $request->setUser(""); // Query arguments if (isset($_SERVER['QUERY_STRING'])) { parse_str($_SERVER['QUERY_STRING'], $args); } else { $args = array(); } $request->setArgs($args); $request->setContentEncoding(""); $request->setContentLanguage(""); $request->setContentType("text/plain"); // @TODO: Find requesting file? // NOTE: Must be set before checking findUriOnDisk! if (isset($routerConfig['global']['documentroot'])) { $request->setDocumentRoot($routerConfig['global']['documentroot']); } else { $request->setDocumentRoot($_SERVER['DOCUMENT_ROOT']); } // Set INPUT headers foreach ($_SERVER as $key => $item) { if (!is_string($key)) { continue; } if (substr($key, 0, 5) != "HTTP_") { continue; } $key = substr($key, 5); $key = strtolower($key); $key = str_replace("_", "-", $key); $key = preg_replace_callback("/^(.)|-(.)/", function ($matches) { return strtoupper($matches[0]); }, $key); $request->appendInHeaders($key, $item); } /* * Apache does not send us the Authorization variable. So this piece of code checks if apache_request_headers * function is present (we are running the router from apache(compatible) browser), and add the authorization * header. */ if (function_exists("apache_request_headers")) { $tmp = apache_request_headers(); if (isset($tmp['Authorization'])) { $request->appendInHeaders('Authorization', $tmp['Authorization']); } } // We don't have the actual host-header, but we can use the http_host variable for this $tmp = parse_url($_SERVER['HTTP_HOST']); $request->setHostname(isset($tmp['host']) ? $tmp['host'] : $tmp['path']); $request->setMethod($_SERVER['REQUEST_METHOD']); $request->setProtocol($_SERVER['SERVER_PROTOCOL']); $request->setStatus(\HTRouter::STATUS_HTTP_OK); if (!isset($_SERVER['PATH_INFO'])) { $_SERVER['PATH_INFO'] = ""; } // These are again, depending on the type of server. Strip the router.php if needed $request->setUnparsedUri($_SERVER['REQUEST_URI']); $request->setUri($_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO']); // Check if we need to remove our router info if (isset($routerConfig['global']['apacherouterprefix'])) { $routerName = $routerConfig['global']['apacherouterprefix']; if (strpos($_SERVER['REQUEST_URI'], $routerName) === 0) { $uri = substr($_SERVER['REQUEST_URI'], strlen($routerName)); if ($uri === false) { $uri = "/"; } $request->setUnparsedUri($uri); } if (strpos($_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'], $routerName) === 0) { $uri = substr($_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'], strlen($routerName)); if ($uri === false) { $uri = "/"; } $request->setUri($uri); } } // Let SetEnvIf etc do their thing $this->runHook(self::HOOK_POST_READ_REQUEST, self::RUNHOOK_ALL, $this->_container); $this->_getLogger()->log(\HTRouter\Logger::ERRORLEVEL_DEBUG, "Populating new request done"); }
function coreHandler(\HTRouter\Request $request) { // Since we don't actually use handlers, this just checks to see if filename is an // actual file. If not, we create a 404. // Check if the status has been set correctly. If so, don't modify our status $status = $request->getStatus(); if ($status != \HTrouter::STATUS_HTTP_OK) { return \HTRouter::STATUS_DECLINED; } // From this point, we know that the filename is the one we need. So we can check // for existence. $path = $request->getDocumentRoot() . $request->getFilename(); if (is_dir($path)) { // Is it a directory. We are not allowed to view it! $request->setStatus(\HTROuter::STATUS_HTTP_FORBIDDEN); return \HTROuter::STATUS_OK; } if (!is_readable($path)) { // Is the file not viewable or existing? Not found! $request->setStatus(\HTROuter::STATUS_HTTP_NOT_FOUND); return \HTROuter::STATUS_OK; } // Next handler return \HTROuter::STATUS_DECLINED; }