Example #1
0
 /**
  * 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");
 }
Example #2
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
 }