Open Journal Systems  3.3.0
vendor/symfony/http-foundation/BinaryFileResponse.php
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
13 
16 
26 class BinaryFileResponse extends Response
27 {
28  protected static $trustXSendfileTypeHeader = false;
29 
33  protected $file;
34  protected $offset = 0;
35  protected $maxlen = -1;
36  protected $deleteFileAfterSend = false;
37 
47  public function __construct($file, int $status = 200, array $headers = [], bool $public = true, string $contentDisposition = null, bool $autoEtag = false, bool $autoLastModified = true)
48  {
49  parent::__construct(null, $status, $headers);
50 
51  $this->setFile($file, $contentDisposition, $autoEtag, $autoLastModified);
52 
53  if ($public) {
54  $this->setPublic();
55  }
56  }
57 
69  public static function create($file = null, $status = 200, $headers = [], $public = true, $contentDisposition = null, $autoEtag = false, $autoLastModified = true)
70  {
71  return new static($file, $status, $headers, $public, $contentDisposition, $autoEtag, $autoLastModified);
72  }
73 
86  public function setFile($file, $contentDisposition = null, $autoEtag = false, $autoLastModified = true)
87  {
88  if (!$file instanceof File) {
89  if ($file instanceof \SplFileInfo) {
90  $file = new File($file->getPathname());
91  } else {
92  $file = new File((string) $file);
93  }
94  }
95 
96  if (!$file->isReadable()) {
97  throw new FileException('File must be readable.');
98  }
99 
100  $this->file = $file;
101 
102  if ($autoEtag) {
103  $this->setAutoEtag();
104  }
105 
106  if ($autoLastModified) {
107  $this->setAutoLastModified();
108  }
109 
110  if ($contentDisposition) {
111  $this->setContentDisposition($contentDisposition);
112  }
113 
114  return $this;
115  }
116 
122  public function getFile()
123  {
124  return $this->file;
125  }
126 
130  public function setAutoLastModified()
131  {
132  $this->setLastModified(\DateTime::createFromFormat('U', $this->file->getMTime()));
133 
134  return $this;
135  }
136 
140  public function setAutoEtag()
141  {
142  $this->setEtag(base64_encode(hash_file('sha256', $this->file->getPathname(), true)));
143 
144  return $this;
145  }
146 
156  public function setContentDisposition($disposition, $filename = '', $filenameFallback = '')
157  {
158  if ('' === $filename) {
159  $filename = $this->file->getFilename();
160  }
161 
162  if ('' === $filenameFallback && (!preg_match('/^[\x20-\x7e]*$/', $filename) || false !== strpos($filename, '%'))) {
163  $encoding = mb_detect_encoding($filename, null, true) ?: '8bit';
164 
165  for ($i = 0, $filenameLength = mb_strlen($filename, $encoding); $i < $filenameLength; ++$i) {
166  $char = mb_substr($filename, $i, 1, $encoding);
167 
168  if ('%' === $char || \ord($char) < 32 || \ord($char) > 126) {
169  $filenameFallback .= '_';
170  } else {
171  $filenameFallback .= $char;
172  }
173  }
174  }
175 
176  $dispositionHeader = $this->headers->makeDisposition($disposition, $filename, $filenameFallback);
177  $this->headers->set('Content-Disposition', $dispositionHeader);
178 
179  return $this;
180  }
181 
185  public function prepare(Request $request)
186  {
187  if (!$this->headers->has('Content-Type')) {
188  $this->headers->set('Content-Type', $this->file->getMimeType() ?: 'application/octet-stream');
189  }
190 
191  if ('HTTP/1.0' !== $request->server->get('SERVER_PROTOCOL')) {
192  $this->setProtocolVersion('1.1');
193  }
194 
195  $this->ensureIEOverSSLCompatibility($request);
196 
197  $this->offset = 0;
198  $this->maxlen = -1;
199 
200  if (false === $fileSize = $this->file->getSize()) {
201  return $this;
202  }
203  $this->headers->set('Content-Length', $fileSize);
204 
205  if (!$this->headers->has('Accept-Ranges')) {
206  // Only accept ranges on safe HTTP methods
207  $this->headers->set('Accept-Ranges', $request->isMethodSafe() ? 'bytes' : 'none');
208  }
209 
210  if (self::$trustXSendfileTypeHeader && $request->headers->has('X-Sendfile-Type')) {
211  // Use X-Sendfile, do not send any content.
212  $type = $request->headers->get('X-Sendfile-Type');
213  $path = $this->file->getRealPath();
214  // Fall back to scheme://path for stream wrapped locations.
215  if (false === $path) {
216  $path = $this->file->getPathname();
217  }
218  if ('x-accel-redirect' === strtolower($type)) {
219  // Do X-Accel-Mapping substitutions.
220  // @link https://www.nginx.com/resources/wiki/start/topics/examples/x-accel/#x-accel-redirect
221  $parts = HeaderUtils::split($request->headers->get('X-Accel-Mapping', ''), ',=');
222  foreach ($parts as $part) {
223  list($pathPrefix, $location) = $part;
224  if (substr($path, 0, \strlen($pathPrefix)) === $pathPrefix) {
225  $path = $location.substr($path, \strlen($pathPrefix));
226  // Only set X-Accel-Redirect header if a valid URI can be produced
227  // as nginx does not serve arbitrary file paths.
228  $this->headers->set($type, $path);
229  $this->maxlen = 0;
230  break;
231  }
232  }
233  } else {
234  $this->headers->set($type, $path);
235  $this->maxlen = 0;
236  }
237  } elseif ($request->headers->has('Range')) {
238  // Process the range headers.
239  if (!$request->headers->has('If-Range') || $this->hasValidIfRangeHeader($request->headers->get('If-Range'))) {
240  $range = $request->headers->get('Range');
241 
242  list($start, $end) = explode('-', substr($range, 6), 2) + [0];
243 
244  $end = ('' === $end) ? $fileSize - 1 : (int) $end;
245 
246  if ('' === $start) {
247  $start = $fileSize - $end;
248  $end = $fileSize - 1;
249  } else {
250  $start = (int) $start;
251  }
252 
253  if ($start <= $end) {
254  if ($start < 0 || $end > $fileSize - 1) {
255  $this->setStatusCode(416);
256  $this->headers->set('Content-Range', sprintf('bytes */%s', $fileSize));
257  } elseif (0 !== $start || $end !== $fileSize - 1) {
258  $this->maxlen = $end < $fileSize ? $end - $start + 1 : -1;
259  $this->offset = $start;
260 
261  $this->setStatusCode(206);
262  $this->headers->set('Content-Range', sprintf('bytes %s-%s/%s', $start, $end, $fileSize));
263  $this->headers->set('Content-Length', $end - $start + 1);
264  }
265  }
266  }
267  }
268 
269  return $this;
270  }
271 
272  private function hasValidIfRangeHeader(?string $header): bool
273  {
274  if ($this->getEtag() === $header) {
275  return true;
276  }
277 
278  if (null === $lastModified = $this->getLastModified()) {
279  return false;
280  }
281 
282  return $lastModified->format('D, d M Y H:i:s').' GMT' === $header;
283  }
284 
290  public function sendContent()
291  {
292  if (!$this->isSuccessful()) {
293  return parent::sendContent();
294  }
295 
296  if (0 === $this->maxlen) {
297  return $this;
298  }
299 
300  $out = fopen('php://output', 'wb');
301  $file = fopen($this->file->getPathname(), 'rb');
302 
303  stream_copy_to_stream($file, $out, $this->maxlen, $this->offset);
304 
305  fclose($out);
306  fclose($file);
307 
308  if ($this->deleteFileAfterSend && file_exists($this->file->getPathname())) {
309  unlink($this->file->getPathname());
310  }
311 
312  return $this;
313  }
314 
320  public function setContent($content)
321  {
322  if (null !== $content) {
323  throw new \LogicException('The content cannot be set on a BinaryFileResponse instance.');
324  }
325 
326  return $this;
327  }
328 
332  public function getContent()
333  {
334  return false;
335  }
336 
340  public static function trustXSendfileTypeHeader()
341  {
342  self::$trustXSendfileTypeHeader = true;
343  }
344 
353  public function deleteFileAfterSend($shouldDelete = true)
354  {
355  $this->deleteFileAfterSend = $shouldDelete;
356 
357  return $this;
358  }
359 }
Symfony\Component\HttpFoundation\BinaryFileResponse\setAutoLastModified
setAutoLastModified()
Definition: lib/vendor/symfony/http-foundation/BinaryFileResponse.php:135
Symfony\Component\HttpFoundation\File\File
Definition: lib/vendor/symfony/http-foundation/File/File.php:24
Symfony\Component\HttpFoundation\BinaryFileResponse\prepare
prepare(Request $request)
Definition: vendor/symfony/http-foundation/BinaryFileResponse.php:188
Symfony\Component\HttpFoundation\Response\setProtocolVersion
setProtocolVersion($version)
Definition: lib/vendor/symfony/http-foundation/Response.php:454
Symfony\Component\HttpFoundation\Request\isMethodSafe
isMethodSafe()
Definition: lib/vendor/symfony/http-foundation/Request.php:1612
Symfony\Component\HttpFoundation\Response\getLastModified
getLastModified()
Definition: lib/vendor/symfony/http-foundation/Response.php:896
Symfony\Component\HttpFoundation\Response\ensureIEOverSSLCompatibility
ensureIEOverSSLCompatibility(Request $request)
Definition: lib/vendor/symfony/http-foundation/Response.php:1298
Symfony\Component\HttpFoundation\Response\setLastModified
setLastModified(\DateTime $date=null)
Definition: lib/vendor/symfony/http-foundation/Response.php:912
Symfony\Component\HttpFoundation\BinaryFileResponse\setAutoEtag
setAutoEtag()
Definition: lib/vendor/symfony/http-foundation/BinaryFileResponse.php:145
Symfony\Component\HttpFoundation\BinaryFileResponse\deleteFileAfterSend
deleteFileAfterSend($shouldDelete)
Definition: lib/vendor/symfony/http-foundation/BinaryFileResponse.php:358
Symfony\Component\HttpFoundation\BinaryFileResponse\create
static create($file=null, $status=200, $headers=array(), $public=true, $contentDisposition=null, $autoEtag=false, $autoLastModified=true)
Definition: lib/vendor/symfony/http-foundation/BinaryFileResponse.php:74
Symfony\Component\HttpFoundation\BinaryFileResponse\__construct
__construct($file, $status=200, $headers=array(), $public=true, $contentDisposition=null, $autoEtag=false, $autoLastModified=true)
Definition: lib/vendor/symfony/http-foundation/BinaryFileResponse.php:52
Symfony\Component\HttpFoundation\BinaryFileResponse\setContent
setContent($content)
Definition: vendor/symfony/http-foundation/BinaryFileResponse.php:323
Symfony\Component\HttpFoundation\Request\get
get($key, $default=null)
Definition: lib/vendor/symfony/http-foundation/Request.php:869
Symfony\Component\HttpFoundation\Request
Definition: lib/vendor/symfony/http-foundation/Request.php:31
Symfony\Component\HttpFoundation\BinaryFileResponse\trustXSendfileTypeHeader
static trustXSendfileTypeHeader()
Definition: vendor/symfony/http-foundation/BinaryFileResponse.php:343
Symfony\Component\HttpFoundation\BinaryFileResponse\sendContent
sendContent()
Definition: vendor/symfony/http-foundation/BinaryFileResponse.php:293
Symfony\Component\HttpFoundation\BinaryFileResponse\setFile
setFile($file, $contentDisposition=null, $autoEtag=false, $autoLastModified=true)
Definition: lib/vendor/symfony/http-foundation/BinaryFileResponse.php:91
Symfony\Component\HttpFoundation\BinaryFileResponse\__construct
__construct($file, int $status=200, array $headers=[], bool $public=true, string $contentDisposition=null, bool $autoEtag=false, bool $autoLastModified=true)
Definition: vendor/symfony/http-foundation/BinaryFileResponse.php:50
Symfony\Component\HttpFoundation\Response\$content
$content
Definition: lib/vendor/symfony/http-foundation/Response.php:98
Symfony\Component\HttpFoundation\Response\$headers
$headers
Definition: lib/vendor/symfony/http-foundation/Response.php:90
Symfony\Component\HttpFoundation\BinaryFileResponse\setContentDisposition
setContentDisposition($disposition, $filename='', $filenameFallback='')
Definition: lib/vendor/symfony/http-foundation/BinaryFileResponse.php:161
Symfony\Component\HttpFoundation\BinaryFileResponse\$trustXSendfileTypeHeader
static $trustXSendfileTypeHeader
Definition: lib/vendor/symfony/http-foundation/BinaryFileResponse.php:28
Symfony\Component\HttpFoundation\BinaryFileResponse\$offset
$offset
Definition: lib/vendor/symfony/http-foundation/BinaryFileResponse.php:37
Symfony\Component\HttpFoundation\BinaryFileResponse\getFile
getFile()
Definition: vendor/symfony/http-foundation/BinaryFileResponse.php:125
Symfony\Component\HttpFoundation\BinaryFileResponse\getContent
getContent()
Definition: vendor/symfony/http-foundation/BinaryFileResponse.php:335
Symfony\Component\HttpFoundation\Response\isSuccessful
isSuccessful()
Definition: lib/vendor/symfony/http-foundation/Response.php:1162
Symfony\Component\HttpFoundation\BinaryFileResponse\$file
$file
Definition: lib/vendor/symfony/http-foundation/BinaryFileResponse.php:36
Symfony\Component\HttpFoundation\BinaryFileResponse\$deleteFileAfterSend
$deleteFileAfterSend
Definition: lib/vendor/symfony/http-foundation/BinaryFileResponse.php:39
Symfony\Component\HttpFoundation\BinaryFileResponse\create
static create($file=null, $status=200, $headers=[], $public=true, $contentDisposition=null, $autoEtag=false, $autoLastModified=true)
Definition: vendor/symfony/http-foundation/BinaryFileResponse.php:72
Request
Mock implementation of the Request class.
Definition: Request.inc.php:21
Symfony\Component\HttpFoundation
Definition: lib/vendor/symfony/http-foundation/AcceptHeader.php:12
Symfony\Component\HttpFoundation\Response\setPublic
setPublic()
Definition: lib/vendor/symfony/http-foundation/Response.php:633
Symfony\Component\HttpFoundation\Response\setEtag
setEtag($etag=null, $weak=false)
Definition: lib/vendor/symfony/http-foundation/Response.php:947
Symfony\Component\HttpFoundation\Response\getEtag
getEtag()
Definition: lib/vendor/symfony/http-foundation/Response.php:932
Symfony\Component\HttpFoundation\HeaderUtils\split
static split(string $header, string $separators)
Definition: HeaderUtils.php:45
Symfony\Component\HttpFoundation\File\Exception\FileException
Definition: lib/vendor/symfony/http-foundation/File/Exception/FileException.php:19
Symfony\Component\HttpFoundation\Response\setStatusCode
setStatusCode($code, $text=null)
Definition: lib/vendor/symfony/http-foundation/Response.php:488
Symfony\Component\HttpFoundation\BinaryFileResponse\deleteFileAfterSend
deleteFileAfterSend($shouldDelete=true)
Definition: vendor/symfony/http-foundation/BinaryFileResponse.php:356
Symfony\Component\HttpFoundation\BinaryFileResponse\$maxlen
$maxlen
Definition: lib/vendor/symfony/http-foundation/BinaryFileResponse.php:38