Example #1
0
 /**
  * Create an instance from a stringified request.
  * @param  string     $str the stringified request
  * @return \vakata\http\Request          the request instance
  * @codeCoverageIgnore
  */
 public static function fromString($str)
 {
     $req = new self();
     $break = strpos($str, "\r\n\r\n") === false ? "\n" : "\r\n";
     // just in case someone breaks RFC 2616
     list($headers, $message) = explode($break . $break, $str, 2);
     $headers = explode($break, preg_replace("(" . $break . "\\s+)", " ", $headers));
     if (isset($headers[0]) && strlen($headers[0])) {
         $temp = explode(' ', $headers[0]);
         if (in_array($temp[0], ['GET', 'POST', 'HEAD', 'PATCH', 'PUT', 'OPTIONS', 'TRACE', 'DELETE'])) {
             $req->setMethod($temp[0]);
             $req->setUrl($temp[1]);
             if (isset($temp[2])) {
                 $req->setProtocolVersion(substr($temp[2], 5));
             }
             unset($headers[0]);
             $headers = array_values($headers);
         }
     }
     foreach (array_filter($headers) as $v) {
         $v = explode(':', $v, 2);
         $req->setHeader(trim($v[0]), trim($v[1]));
     }
     if ($req->hasHeader('Host')) {
         $host = explode(':', $req->getHeader('Host'), 2);
         $req->getUrl()->setHost($host[0]);
         if (isset($host[1]) && (int) $host[1]) {
             $req->getUrl()->setPort($host[1]);
         }
     }
     if (strpos($req->getHeader('Content-Type'), 'multipart') !== false) {
         $bndr = trim(explode(' boundary=', $req->getHeader('Content-Type'))[1], '"');
         $parts = explode($break . '--' . $bndr, $break . $message);
         array_pop($parts);
         array_shift($parts);
         $post = [];
         foreach ($parts as $item) {
             list($head, $body) = explode($break . $break, $item, 2);
             $head = explode($break, preg_replace("(" . $break . "\\s+)", " ", $head));
             foreach ($head as $h) {
                 if (strpos(strtolower($h), 'content-disposition') === 0) {
                     $cd = explode(';', $h);
                     $name = '';
                     $file = '';
                     foreach ($cd as $p) {
                         if (strpos(trim($p), 'name=') === 0) {
                             $name = trim(explode('name=', $p)[1], ' "');
                         }
                         if (strpos(trim($p), 'filename=') === 0) {
                             $file = trim(explode('filename=', $p)[1], ' "');
                         }
                     }
                     if ($file) {
                         $req->addUpload($name, $body, $file);
                     } else {
                         $post[$name] = $body;
                     }
                 }
             }
         }
         $req->setBody(http_build_query($post));
     } elseif (strlen($message)) {
         $req->setBody($message);
     }
     $req->removeHeader('Content-Length');
     $req->removeHeader('Transfer-Encoding');
     return $req;
 }
Example #2
0
 public function translate()
 {
     // Initialize the response with the current state
     $request = new self(array(), $this->url, $this->status);
     // Attempt to translate the url until no more translations are found
     do {
         // If this flag is set to true, then translation will continue another round
         $translated = false;
         // Routes is formatted as `array(url-pattern-to-match => rewrite-instructions, ...)`.
         // Step through each route and try to match the base URL pattern with the current URL
         foreach ($this->routes as $route) {
             // A match with the current URL was found
             if (preg_match($route->pattern, $request->url)) {
                 // Rewrite the URL
                 $request->setUrl(preg_replace($route->pattern, $route->rewrite, $request->url));
                 $translated = true;
                 // Optional: If a new status was provided, set it
                 if ($route->status !== false) {
                     $request->setStatus($route->status);
                 }
                 // If the `last` flag is set or a redirect is required, then stop all translations
                 if (!empty($route->is_last) || $request->isStatusRedirect()) {
                     $translated = false;
                     break;
                 }
             }
         }
     } while ($translated);
     // Make sure that the URL is set strictly as a path
     $url_parts = parse_url($request->getUrl(false));
     $request->setUrl($url_parts['path']);
     // Inject rewritten URL params into _GET
     if (!empty($url_parts['query'])) {
         $params = array();
         parse_str($url_parts['query'], $params);
         $_GET = array_merge($_GET, $params);
     }
     // If the new status is a redirect
     if ((int) ($request->status / 100) == 3) {
         // Clean up the query string before creeating the new request
         unset($_GET[GOBE_QUERY_PATH]);
         unset($_GET[GOBE_QUERY_STATUS]);
         header("Location: " . $url_parts['path'] . '?' . http_build_query($_GET), true, $request->status);
         die;
     }
     // Set the new status in the HTTP header
     //		header("gobe-status", true, $request->status);
     return $request;
 }
Example #3
0
 public static function getColorizedUrl($image_id, $color)
 {
     $color = str_replace('#', '', $color);
     $id = md5(implode('+', array($image_id, $color)));
     $url = '';
     $image = new Media_Model_Library_Image();
     if (is_numeric($image_id)) {
         $image->find($image_id);
         if (!$image->getId()) {
             return $url;
         }
         if (!$image->getCanBeColorized()) {
             $color = null;
         }
         $path = $image->getLink();
         $path = Media_Model_Library_Image::getBaseImagePathTo($path, $image->getAppId());
     } else {
         if (!Zend_Uri::check($image_id) and stripos($image_id, Core_Model_Directory::getBasePathTo()) === false) {
             $path = Core_Model_Directory::getBasePathTo($image_id);
         } else {
             $path = $image_id;
         }
     }
     try {
         $image = new self();
         $image->setId($id)->setPath($path)->setColor($color)->colorize();
         $url = $image->getUrl();
     } catch (Exception $e) {
         $url = '';
     }
     return $url;
 }