public function redirectDirective(\HTRouter\Request $request, $line) { $redirect = new \StdClass(); // We should check if "status" is given. If not, it defaults to 302 $redirect->http_status = 302; // temporary status by default // parse argument list // @TODO better argument splitting! $args = preg_split("/\\s+/", $line); if (count($args) == 3) { // We have 3 arguments, which means the first argument is the 'status' $redirect->http_status = 0; if (strtolower($args[0]) == "permanent") { $redirect->http_status = 301; } elseif (strtolower($args[0]) == "temp") { $redirect->http_status = 302; } elseif (strtolower($args[0]) == "seeother") { $redirect->http_status = 303; } elseif (strtolower($args[0]) == "gone" && count($args) == 2) { // Gone does not have 3 arguments, but 2! $redirect->http_status = 410; } elseif (is_numeric($args[0]) && $args[0] >= 300 && $args[0] <= 399) { $redirect->http_status = $args[0]; } if ($redirect->http_status == 0) { throw new \InvalidArgumentException("redirect does not have correct first argument (of three)"); } // Remove "status" from the list. Now we only have 2 arguments! array_shift($args); } // Check the url path $redirect->urlpath = $args[0]; if ($redirect->urlpath[0] != '/') { throw new \InvalidArgumentException("URL path needs to be an absolute path"); } // Check the url (if available) if (isset($args[1])) { $redirect->url = $args[1]; $utils = new \HTRouter\Utils(); if (!$utils->isUrl($redirect->url)) { throw new \InvalidArgumentException("URL needs to be an actual URL (http://...)"); } } // Add to the list $this->getConfig()->append("Redirects", $redirect); }
/** * The actual processing of a request, this should look somewhat similar to request.c:ap_process_request_internal() * * @return int status */ function processRequest() { $r = $this->_container->getRequest(); $this->getLogger()->log(\HTRouter\Logger::ERRORLEVEL_DEBUG, "processRequest(" . $r->getUri() . ")"); $utils = new \HTRouter\Utils(); // If you are looking for proxy stuff. It's not here to simplify things // Remove /. /.. and // from the URI $realUri = $utils->getParents($r->getUri()); $r->setUri($realUri); // We don't have a filename yet, try to find the file that corresponds to the URI we need $status = $this->_locationWalk($r); if ($status != \HTRouter::STATUS_OK) { return $status; } $status = $this->getRouter()->runHook(\HTRouter::HOOK_TRANSLATE_NAME, \HTRouter::RUNHOOK_FIRST, $this->_container); if ($status != \HTRouter::STATUS_OK) { return $this->_declDie($status, "translate", $r); } $status = $this->getRouter()->runHook(\HTRouter::HOOK_MAP_TO_STORAGE, \HTRouter::RUNHOOK_FIRST, $this->_container); if ($status != \HTRouter::STATUS_OK) { return $status; } if ($r->isMainRequest()) { $status = $this->getRouter()->runHook(\HTRouter::HOOK_HEADER_PARSER, \HTRouter::RUNHOOK_FIRST, $this->_container); if ($status != \HTRouter::STATUS_OK) { return $status; } } // We always re-authenticate. Something request.c doesn't do for optimizing. Easy enough to create though. $status = $this->_authenticate($r); if ($status != \HTRouter::STATUS_OK) { return $status; } $status = $this->getRouter()->runHook(\HTRouter::HOOK_CHECK_TYPE, \HTRouter::RUNHOOK_FIRST, $this->_container); if ($status != \HTRouter::STATUS_OK) { return $this->_declDie($status, "find types", $r); } $status = $this->getRouter()->runHook(\HTRouter::HOOK_FIXUPS, \HTRouter::RUNHOOK_ALL, $this->_container); if ($status != \HTRouter::STATUS_OK) { return $status; } // If everything is ok. Note that we return 200 OK instead of "OK" since we need to return a code for the // router to work with... return \HTRouter::STATUS_HTTP_OK; }
function checkPassword(\HTRouter\Request $request, $user, $pass) { $utils = new \HTRouter\Utils(); // Read htpasswd file line by line $htpasswdFile = $this->getConfig()->get("AuthUserFile"); foreach (file($htpasswdFile) as $line) { // Trim line and parse user/pass $line = trim($line); if ($line[0] == "#") { continue; } list($chk_user, $chk_pass) = explode(":", $line); // Note: case SENSITIVE: jay != JAY if ($chk_user == $user and $utils->validatePassword($pass, $chk_pass)) { return \HTRouter\AuthModule::AUTH_GRANTED; } } return \HTRouter\AuthModule::AUTH_DENIED; }
protected function _updateUrl($url, $path) { $utils = new \HTRouter\Utils(); $url = parse_url($url); // Is it an absolute url? if ($path[0] == "/") { $url['path'] = $path; // Replace } else { $url['path'] .= $path; // Append } $url = $utils->unparse_url($url); return $url; }
function getStatusLine() { $utils = new \HTRouter\Utils(); return $utils->getStatusLine($this->getStatus()); }
public function RewriteEngineDirective(\HTRouter\Request $request, $line) { $utils = new \HTRouter\Utils(); $value = $utils->fetchDirectiveFlags($line, array("on" => true, "off" => false)); $this->getConfig()->set("RewriteEngine", $value); }
protected function _findAllowDeny(array $items) { $utils = new \HTRouter\Utils(); // Iterate all "ALLOW" or "DENY" items. We just return if at least one of them matches foreach ($items as $entry) { switch ($entry->type) { case "env": $env = $this->getRouter()->getEnvironment(); if (isset($env[$entry->env])) { return true; } break; case "nenv": $env = $this->getRouter()->getEnvironment(); if (!isset($env[$entry->env])) { return true; } break; case "all": return true; break; case "ip": if ($utils->checkMatchingIP($entry->ip, $this->getRequest()->getIp())) { return true; } break; case "host": if ($utils->checkMatchingHost($entry->host, $this->getRequest()->getIp())) { return true; } break; default: throw new \LogicException("Unknown entry type: " . $entry->type); break; } } return false; }
/** * 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 }
public function AuthzUserAuthoritativeDirective(\HTRouter\Request $request, $line) { $utils = new \HTRouter\Utils(); $value = $utils->fetchDirectiveFlags($line, array("on" => "on", "off" => "off")); $this->getConfig()->set("AuthzUserAuthoritative", $value); }
public function satisfyDirective(\HTRouter\Request $request, $line) { $utils = new \HTRouter\Utils(); $value = $utils->fetchDirectiveFlags($line, array("all" => "all", "any" => "any")); $this->getConfig()->set("Satisfy", $value); }