Open Journal Systems  3.3.0
EntityBody.php
1 <?php
2 
3 namespace Guzzle\Http;
4 
9 
13 class EntityBody extends Stream implements EntityBodyInterface
14 {
16  protected $contentEncoding = false;
17 
19  protected $rewindFunction;
20 
30  public static function factory($resource = '', $size = null)
31  {
32  if ($resource instanceof EntityBodyInterface) {
33  return $resource;
34  }
35 
36  switch (gettype($resource)) {
37  case 'string':
38  return self::fromString($resource);
39  case 'resource':
40  return new static($resource, $size);
41  case 'object':
42  if (method_exists($resource, '__toString')) {
43  return self::fromString((string) $resource);
44  }
45  break;
46  case 'array':
47  return self::fromString(http_build_query($resource));
48  }
49 
50  throw new InvalidArgumentException('Invalid resource type');
51  }
52 
53  public function setRewindFunction($callable)
54  {
55  if (!is_callable($callable)) {
56  throw new InvalidArgumentException('Must specify a callable');
57  }
58 
59  $this->rewindFunction = $callable;
60 
61  return $this;
62  }
63 
64  public function rewind()
65  {
66  return $this->rewindFunction ? call_user_func($this->rewindFunction, $this) : parent::rewind();
67  }
68 
76  public static function fromString($string)
77  {
78  $stream = fopen('php://temp', 'r+');
79  if ($string !== '') {
80  fwrite($stream, $string);
81  rewind($stream);
82  }
83 
84  return new static($stream);
85  }
86 
87  public function compress($filter = 'zlib.deflate')
88  {
89  $result = $this->handleCompression($filter);
90  $this->contentEncoding = $result ? $filter : false;
91 
92  return $result;
93  }
94 
95  public function uncompress($filter = 'zlib.inflate')
96  {
97  $offsetStart = 0;
98 
99  // When inflating gzipped data, the first 10 bytes must be stripped
100  // if a gzip header is present
101  if ($filter == 'zlib.inflate') {
102  // @codeCoverageIgnoreStart
103  if (!$this->isReadable() || ($this->isConsumed() && !$this->isSeekable())) {
104  return false;
105  }
106  // @codeCoverageIgnoreEnd
107  if (stream_get_contents($this->stream, 3, 0) === "\x1f\x8b\x08") {
108  $offsetStart = 10;
109  }
110  }
111 
112  $this->contentEncoding = false;
113 
114  return $this->handleCompression($filter, $offsetStart);
115  }
116 
117  public function getContentLength()
118  {
119  return $this->getSize();
120  }
121 
122  public function getContentType()
123  {
124  return $this->getUri() ? Mimetypes::getInstance()->fromFilename($this->getUri()) : null;
125  }
126 
127  public function getContentMd5($rawOutput = false, $base64Encode = false)
128  {
129  if ($hash = self::getHash($this, 'md5', $rawOutput)) {
130  return $hash && $base64Encode ? base64_encode($hash) : $hash;
131  } else {
132  return false;
133  }
134  }
135 
147  public static function calculateMd5(EntityBodyInterface $body, $rawOutput = false, $base64Encode = false)
148  {
149  Version::warn(__CLASS__ . ' is deprecated. Use getContentMd5()');
150  return $body->getContentMd5($rawOutput, $base64Encode);
151  }
152 
153  public function setStreamFilterContentEncoding($streamFilterContentEncoding)
154  {
155  $this->contentEncoding = $streamFilterContentEncoding;
156 
157  return $this;
158  }
159 
160  public function getContentEncoding()
161  {
162  return strtr($this->contentEncoding, array(
163  'zlib.deflate' => 'gzip',
164  'bzip2.compress' => 'compress'
165  )) ?: false;
166  }
167 
168  protected function handleCompression($filter, $offsetStart = 0)
169  {
170  // @codeCoverageIgnoreStart
171  if (!$this->isReadable() || ($this->isConsumed() && !$this->isSeekable())) {
172  return false;
173  }
174  // @codeCoverageIgnoreEnd
175 
176  $handle = fopen('php://temp', 'r+');
177  $filter = @stream_filter_append($handle, $filter, STREAM_FILTER_WRITE);
178  if (!$filter) {
179  return false;
180  }
181 
182  // Seek to the offset start if possible
183  $this->seek($offsetStart);
184  while ($data = fread($this->stream, 8096)) {
185  fwrite($handle, $data);
186  }
187 
188  fclose($this->stream);
189  $this->stream = $handle;
190  stream_filter_remove($filter);
191  $stat = fstat($this->stream);
192  $this->size = $stat['size'];
193  $this->rebuildCache();
194  $this->seek(0);
195 
196  // Remove any existing rewind function as the underlying stream has been replaced
197  $this->rewindFunction = null;
198 
199  return true;
200  }
201 }
Guzzle\Stream\Stream\$stream
$stream
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Stream/Stream.php:23
Guzzle\Http\EntityBody\$contentEncoding
$contentEncoding
Definition: EntityBody.php:19
Guzzle\Stream\Stream\seek
seek($offset, $whence=SEEK_SET)
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Stream/Stream.php:242
Guzzle\Http\EntityBody\handleCompression
handleCompression($filter, $offsetStart=0)
Definition: EntityBody.php:174
Guzzle\Http\EntityBody\fromString
static fromString($string)
Definition: EntityBody.php:82
Guzzle\Http\Mimetypes
Definition: Mimetypes.php:9
Guzzle\Http\EntityBodyInterface
Definition: EntityBodyInterface.php:10
Guzzle\Http\EntityBody\uncompress
uncompress($filter='zlib.inflate')
Definition: EntityBody.php:101
Guzzle\Stream\Stream
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Stream/Stream.php:10
Guzzle\Stream\Stream\$size
$size
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Stream/Stream.php:29
Guzzle\Http\EntityBody\getContentLength
getContentLength()
Definition: EntityBody.php:123
Guzzle\Http\EntityBody\factory
static factory($resource='', $size=null)
Definition: EntityBody.php:36
Guzzle\Http\EntityBody\getContentType
getContentType()
Definition: EntityBody.php:128
Guzzle\Common\Version\warn
static warn($message)
Definition: Version.php:23
Guzzle\Http\EntityBody
Definition: EntityBody.php:13
Guzzle\Stream\Stream\getSize
getSize()
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Stream/Stream.php:177
Guzzle\Http\EntityBody\setStreamFilterContentEncoding
setStreamFilterContentEncoding($streamFilterContentEncoding)
Definition: EntityBody.php:159
Guzzle\Http\EntityBody\getContentMd5
getContentMd5($rawOutput=false, $base64Encode=false)
Definition: EntityBody.php:133
Guzzle\Http\EntityBodyInterface\getContentMd5
getContentMd5($rawOutput=false, $base64Encode=false)
Guzzle\Common\Exception\InvalidArgumentException
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Exception/InvalidArgumentException.php:5
Guzzle\Common\Version
Definition: Version.php:8
Guzzle\Http\EntityBody\$rewindFunction
$rewindFunction
Definition: EntityBody.php:25
Guzzle\Stream\Stream\isConsumed
isConsumed()
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Stream/Stream.php:215
Guzzle\Http
Definition: AbstractEntityBodyDecorator.php:3
Guzzle\Stream\Stream\isReadable
isReadable()
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Stream/Stream.php:200
Guzzle\Http\EntityBody\calculateMd5
static calculateMd5(EntityBodyInterface $body, $rawOutput=false, $base64Encode=false)
Definition: EntityBody.php:153
Guzzle\Http\EntityBody\setRewindFunction
setRewindFunction($callable)
Definition: EntityBody.php:59
Guzzle\Http\EntityBody\rewind
rewind()
Definition: EntityBody.php:70
Guzzle\Http\Mimetypes\getInstance
static getInstance()
Definition: Mimetypes.php:930
Guzzle\Stream\Stream\rebuildCache
rebuildCache()
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Stream/Stream.php:294
Guzzle\Stream\Stream\getUri
getUri()
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Stream/Stream.php:172
Guzzle\Http\EntityBody\compress
compress($filter='zlib.deflate')
Definition: EntityBody.php:93
Guzzle\Stream\Stream\isSeekable
isSeekable()
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Stream/Stream.php:230
Guzzle\Http\EntityBody\getContentEncoding
getContentEncoding()
Definition: EntityBody.php:166