Example #1
0
 /**
  * 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
 }
Example #2
0
 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;
 }
Example #3
0
 function coreTranslateName(\HTRouter\Request $request)
 {
     $uri = $request->getUri();
     if (empty($uri) || $uri[0] != '/' || $uri == "*") {
         $this->getLogger()->log(\HTRouter\Logger::ERRORLEVEL_ERROR, "Invalid uri in request: " . $uri);
         return \HTRouter::STATUS_HTTP_BAD_REQUEST;
     }
     $filename = $request->getUri();
     $request->setFilename($filename);
     // Remember, filename must be relative from documentroot!
     return \HTRouter::STATUS_OK;
 }