1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* balloon |
7
|
|
|
* |
8
|
|
|
* @copyright Copryright (c) 2012-2018 gyselroth GmbH (https://gyselroth.com) |
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Balloon\App\Api; |
13
|
|
|
|
14
|
|
|
use Balloon\Filesystem\Exception; |
|
|
|
|
15
|
|
|
use Balloon\Filesystem\Node\File; |
16
|
|
|
use Micro\Http\Response; |
17
|
|
|
|
18
|
|
|
class Helper |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Stream content. |
22
|
|
|
*/ |
23
|
|
|
public static function streamContent(Response $response, File $file, bool $download = false): ?Response |
24
|
|
|
{ |
25
|
|
|
if (true === $download) { |
26
|
|
|
$response->setHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\''.rawurlencode($file->getName())); |
|
|
|
|
27
|
|
|
$response->setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'); |
28
|
|
|
$response->setHeader('Content-Type', 'application/octet-stream'); |
29
|
|
|
$response->setHeader('Content-Length', (string) $file->getSize()); |
30
|
|
|
$response->setHeader('Content-Transfer-Encoding', 'binary'); |
31
|
|
|
} else { |
32
|
|
|
$response->setHeader('Content-Disposition', 'inline; filename*=UTF-8\'\''.rawurlencode($file->getName())); |
|
|
|
|
33
|
|
|
$response->setHeader('Content-Type', $file->getContentType()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $response |
37
|
|
|
->setOutputFormat(null) |
38
|
|
|
->setBody(function () use ($file) { |
39
|
|
|
$stream = $file->get(); |
40
|
|
|
$name = $file->getName(); |
|
|
|
|
41
|
|
|
|
42
|
|
|
if (null === $stream) { |
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$size = $file->getSize(); |
47
|
|
|
$length = $size; |
48
|
|
|
$start = 0; |
49
|
|
|
$end = $size - 1; |
50
|
|
|
|
51
|
|
|
if (isset($_SERVER['HTTP_RANGE'])) { |
52
|
|
|
header('Accept-Ranges: bytes'); |
53
|
|
|
$c_start = $start; |
|
|
|
|
54
|
|
|
$c_end = $end; |
55
|
|
|
list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2); |
56
|
|
|
|
57
|
|
|
if (strpos($range, ',') !== false) { |
58
|
|
|
header("Content-Range: bytes $start-$end/$size"); |
59
|
|
|
|
60
|
|
|
throw new Exception\InvalidRange('invalid offset/limit requested'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($range == '-') { |
64
|
|
|
$c_start = $size - substr($range, 1); |
65
|
|
|
} else { |
66
|
|
|
$range = explode('-', $range); |
67
|
|
|
$c_start = $range[0]; |
68
|
|
|
$c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$c_end = ($c_end > $end) ? $end : $c_end; |
72
|
|
|
if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) { |
73
|
|
|
header("Content-Range: bytes $start-$end/$size"); |
74
|
|
|
|
75
|
|
|
throw new Exception\InvalidRange('invalid offset/limit requested'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$start = (int) $c_start; |
79
|
|
|
$end = (int) $c_end; |
80
|
|
|
$length = (int) $end - $start + 1; |
81
|
|
|
fseek($stream, $start); |
82
|
|
|
header('HTTP/1.1 206 Partial Content'); |
83
|
|
|
header("Content-Range: bytes $start-$end/$size"); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
header('Content-Length: '.$length); |
87
|
|
|
$buffer = 1024 * 8; |
88
|
|
|
|
89
|
|
|
while (!feof($stream) && ($p = ftell($stream)) <= $end) { |
90
|
|
|
if ($p + $buffer > $end) { |
91
|
|
|
$buffer = $end - $p + 1; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
echo fread($stream, $buffer); |
95
|
|
|
flush(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
fclose($stream); |
99
|
|
|
}); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: