|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* balloon |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copryright (c) 2012-2019 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
|
|
|
$response->setHeader('ETag', $file->getETag()); |
|
37
|
|
|
|
|
38
|
|
|
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && ($file->getETag() === $_SERVER['HTTP_IF_NONE_MATCH'] || $_SERVER['HTTP_IF_NONE_MATCH'] === '"*"')) { |
|
39
|
|
|
return $response->setCode(304); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $response |
|
43
|
|
|
->setOutputFormat(null) |
|
44
|
|
|
->setBody(function () use ($file) { |
|
45
|
|
|
$stream = $file->get(); |
|
46
|
|
|
$name = $file->getName(); |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
if (null === $stream) { |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$size = $file->getSize(); |
|
53
|
|
|
$length = $size; |
|
54
|
|
|
$start = 0; |
|
55
|
|
|
$end = $size - 1; |
|
56
|
|
|
|
|
57
|
|
|
if (isset($_SERVER['HTTP_RANGE'])) { |
|
58
|
|
|
header('Accept-Ranges: bytes'); |
|
59
|
|
|
$c_start = $start; |
|
|
|
|
|
|
60
|
|
|
$c_end = $end; |
|
61
|
|
|
list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2); |
|
62
|
|
|
|
|
63
|
|
|
if (strpos($range, ',') !== false) { |
|
64
|
|
|
header("Content-Range: bytes $start-$end/$size"); |
|
65
|
|
|
|
|
66
|
|
|
throw new Exception\InvalidRange('invalid offset/limit requested'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if ($range == '-') { |
|
70
|
|
|
$c_start = $size - substr($range, 1); |
|
71
|
|
|
} else { |
|
72
|
|
|
$range = explode('-', $range); |
|
73
|
|
|
$c_start = $range[0]; |
|
74
|
|
|
$c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$c_end = ($c_end > $end) ? $end : $c_end; |
|
78
|
|
|
if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) { |
|
79
|
|
|
header("Content-Range: bytes $start-$end/$size"); |
|
80
|
|
|
|
|
81
|
|
|
throw new Exception\InvalidRange('invalid offset/limit requested'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$start = (int) $c_start; |
|
85
|
|
|
$end = (int) $c_end; |
|
86
|
|
|
$length = (int) $end - $start + 1; |
|
87
|
|
|
fseek($stream, $start); |
|
88
|
|
|
header('HTTP/1.1 206 Partial Content'); |
|
89
|
|
|
header("Content-Range: bytes $start-$end/$size"); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
header('Content-Length: '.$length); |
|
93
|
|
|
$buffer = 1024 * 8; |
|
94
|
|
|
|
|
95
|
|
|
while (!feof($stream) && ($p = ftell($stream)) <= $end) { |
|
96
|
|
|
if ($p + $buffer > $end) { |
|
97
|
|
|
$buffer = $end - $p + 1; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
echo fread($stream, $buffer); |
|
101
|
|
|
flush(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
fclose($stream); |
|
105
|
|
|
}); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: