/** * Downloads the contents of the specified document * @param int $id The id of the document * @param int $width The width to which an image should be resized * @param int $height The height to which an image should be resized * @param string $resizeMode Specifies how an image should be resized * @param int $ifModifiedSince If specified, the file is only downloaded if it was modified since <code>$ifModifiedSince</code> (timestamp) * @throws Exception If an error occurs during download */ private function download($id, $width, $height, $resizeMode, $ifModifiedSince = null) { // Build uri $uri = new UriBuilder($this->getFileUrl); $uri->addParameter("host", $this->connection->host); $uri->addParameter("port", $this->connection->port); $uri->addParameter("username", $this->connection->username); $uri->addParameter("password", $this->connection->password); $uri->addParameter("id", $id); if (isset($width)) { $uri->addParameter("width", $width); } if (isset($height)) { $uri->addParameter("height", $height); } if (isset($resizeMode)) { $uri->addParameter("resizeMode", $resizeMode); } // Begin request if (isset($ifModifiedSince) && is_int($ifModifiedSince)) { // If $ifModifiedSince is specified, set the If-Modified-Since header $header = 'If-Modified-Since: ' . date('r', $ifModifiedSince); $this->webapi->debug("FileOperation::download - Adding header ", $header); $source = fopen($uri, 'rb', false, stream_context_create(array('http' => array('header' => $header)))); } else { $source = fopen($uri, 'rb'); } $this->webapi->debug("FileOperation::download - Requesting ", (string) $uri); if (!$source) { throw new \Exception('Could not open ' . $uri); } // Get content-type and file extension from stream meta-data (http-headers) $meta = stream_get_meta_data($source); if (isset($meta['wrapper_data']) && is_array($meta['wrapper_data'])) { $this->webapi->debug("FileOperation::download - Headers: ", $meta['wrapper_data']); foreach ($meta['wrapper_data'] as $header) { if (stripos($header, 'HTTP/1.1 304') === 0) { // Not modified: Return immediately $this->webapi->debug("FileOperation::download - Retrieved status: 304 Not Modified"); fclose($source); return; } if (stripos($header, 'content-type:') === 0) { $contentType = substr($header, strpos($header, ':') + 2); } if (stripos($header, 'last-modified:') === 0) { $lastModified = strtotime(substr($header, strpos($header, ':') + 2)); } else { $lastModified = time(); } if (stripos($header, 'content-disposition:') === 0) { $contentDisposition = substr($header, strpos($header, ':') + 1); $pos = stripos($contentDisposition, 'filename='); if ($pos !== false) { $filename = substr($contentDisposition, $pos + strlen('filename=')); if (strpos($filename, '"') === 0) { $filename = substr($filename, 1, strlen($filename) - 2); } $extension = strtolower(substr($filename, strrpos($filename, '.'))); } } } } if (!isset($contentType)) { throw new \Exception('Could not get content-type from ' . $uri); } if (!isset($extension)) { throw new \Exception('Could not get file extension from ' . $uri); } $this->webapi->debug("FileOperation::download - Got content-type: ", $contentType); $this->webapi->debug("FileOperation::download - Got file extension: ", $extension); $this->webapi->debug("FileOperation::download - Got last-modified: ", $lastModified); $this->processMetaData($id, $width, $height, $resizeMode, $contentType, $filename, $extension, $lastModified); // Process file data while (!feof($source)) { $data = fread($source, $this->bufferSize); $this->process($data); } // Close file handle fclose($source); }
foreach ($parts as $part) { $encodedPart = rawurlencode($part); $result .= "/{$encodedPart}"; } } return $result; } public static function buildQuery(array $params) { foreach ($params as $key => $value) { if ($value instanceof \DateTime) { $params[$key] = $value->format(\DateTime::ISO8601); } } if (defined("PHP_QUERY_RFC3986")) { // >= 5.4 return http_build_query($params, '', self::$url_separator, PHP_QUERY_RFC3986); } $result = ''; $glue = ''; foreach ($params as $key => $value) { $encodedKey = rawurlencode($key); $encodedValue = rawurlencode($value); $result .= "{$glue}{$encodedKey}={$encodedValue}"; $glue = self::$url_separator; } return $result; } } UriBuilder::init();
/** * Generate a minify URI. * <p>This function is simply a wrapper function to generating a Minify/UriBuilder and running it. * @param Minifier|\JsonSerializable|array $data Any value that can be used to construct a * Minify/UriBuilder. */ function minify($data) { $builder = new UriBuilder($data); return $builder->buildUri(); }
/** * @test */ function buildQueryWithDateTimeValueReturnsExpectedQueryString() { $params = array('date' => \DateTime::createFromFormat(\DateTime::ISO8601, '2014-03-02T01:02:03+0000')); $expected = "date=2014-03-02T01%3A02%3A03%2B0000"; $result = UriBuilder::buildQuery($params); $this->assertEquals($expected, $result); }