Example #1
0
 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);
 }