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; }
/** * Either returns OK, DECLINED, or a HTTP status code * * @param \HTRouter\Request $request * @return \HTRouter\Module\Rewrite\Result * @throws \LogicException */ function rewrite(\HTRouter\Request $request) { $this->_request = $request; // Create default return object $result = new Result(); $result->vary = array(); $result->rc = \HTRouter::STATUS_OK; $utils = new \HTRouter\Utils(); $request->setUri($request->getFilename()); // Strip per directory stuff... :| // Check if pattern matches $regex = "|" . $this->_pattern . "|"; // Don't separate with / since it will be used a path delimiter if ($this->hasFlag(Flag::TYPE_NOCASE)) { $regex .= "i"; } $match = preg_match($regex, $request->getUri(), $matches) >= 1; $this->_ruleMatches = $matches; if ($this->_patternNegate) { $match = !$match; } // We didn't match the pattern (or negative pattern). Return unmodified url_path if (!$match) { $result->rc = \HTRouter::STATUS_OK; return $result; } // @TODO; Skip the conditions for now... // $ret = $this->matchConditions(); // if (! $ret) { // $result->rc = \HTRouter::STATUS_OK; // return $result; // } if ($this->_substitutionType == self::TYPE_SUB_NONE) { // This is a dash, so no need to rewrite $result->rc = \HTRouter::STATUS_OK; return $result; } if ($this->_substitutionType == self::TYPE_SUB) { $uri = $this->expandSubstitutions($this->_substitution, $this->getRequest(), $this->_ruleMatches, $this->_condMatches); $src_url = parse_url($request->getUri()); $dst_url = parse_url($uri); if (!isset($src_url['host'])) { $src_url['host'] = ""; } if (!isset($dst_url['host'])) { $dst_url['host'] = ""; } // If it's the same host or redirect flag is on, we do a redirect if ($dst_url['host'] != $src_url['host'] || $this->hasFlag(Flag::TYPE_REDIRECT)) { $url = $utils->unparse_url($dst_url); $request->appendOutHeaders("Location", $url); $result->rc = \HTRouter::STATUS_HTTP_MOVED_PERMANENTLY; return $result; } // Change url_path $request->setFilename("/" . $dst_url['path']); // Check if we need to append our original arguments if (isset($dst_url['query'])) { parse_str($dst_url['query'], $newArgs); } else { $newArgs = array(); } if ($this->hasFlag(Flag::TYPE_QSA)) { // We need to set new flags $request->setArgs(array_merge($request->getArgs(), $newArgs)); } else { $request->setArgs($newArgs); } $result->rc = \HTRouter::STATUS_OK; return $result; } // @TODO: It should be a sub_none or sub type. Must be changed later // @codeCoverageIgnoreStart throw new \LogicException("We should not be here!"); // @codeCoverageIgnoreEnd }
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; }
public function translateName(\HTRouter\Request $request) { // Need an (absolute) url $uri = $request->getUri(); if (empty($uri) || $uri[0] != '/') { return \HTRouter::STATUS_DECLINED; } // check if name matches one of the redirects foreach ($this->getConfig()->get("Redirects", array()) as $redirect) { // @TODO: Check if this is OK? $pos = strpos($request->getUri(), $redirect->urlpath); if ($pos === 0) { $url = $redirect->url . substr($request->getUri(), strlen($redirect->urlpath)); $qs = $request->getQueryString(); if (!empty($qs)) { $url .= '?' . $qs; } $request->appendOutHeaders("Location", $url); return $redirect->http_status; } } return \HTRouter::STATUS_DECLINED; }