FileHandler.php
14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<?php
/**
* FileHandler.php
*
* The FileHandler class file.
*
* PHP versions 5
*
* @author Alexander Schneider <alexanderschneider85@gmail.com>
* @copyright 2008-2017 Alexander Schneider
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
* @version SVN: $id$
* @link http://wordpress.org/extend/plugins/user-access-manager/
*/
declare(strict_types=1);
namespace UserAccessManager\File;
use UserAccessManager\Config\MainConfig;
use UserAccessManager\Config\WordpressConfig;
use UserAccessManager\Wrapper\Php;
use UserAccessManager\Wrapper\Wordpress;
/**
* Class FileHandler
*
* @package UserAccessManager\FileHandler
*/
class FileHandler
{
const X_SEND_FILE_TEST_FILE = 'xSendFileTestFile';
/**
* @var Php
*/
private $php;
/**
* @var Wordpress
*/
private $wordpress;
/**
* @var WordpressConfig
*/
private $wordpressConfig;
/**
* @var MainConfig
*/
private $mainConfig;
/**
* @var FileProtectionFactory
*/
private $fileProtectionFactory;
/**
* FileHandler constructor.
* @param Php $php
* @param Wordpress $wordpress
* @param WordpressConfig $wordpressConfig
* @param MainConfig $mainConfig
* @param FileProtectionFactory $fileProtectionFactory
*/
public function __construct(
Php $php,
Wordpress $wordpress,
WordpressConfig $wordpressConfig,
MainConfig $mainConfig,
FileProtectionFactory $fileProtectionFactory
) {
$this->php = $php;
$this->wordpress = $wordpress;
$this->wordpressConfig = $wordpressConfig;
$this->mainConfig = $mainConfig;
$this->fileProtectionFactory = $fileProtectionFactory;
}
/**
* Clears the buffer.
*/
private function clearBuffer()
{
//prevent '\n' / '0A'
if ((int) $this->php->iniGet('output_buffering') === 0
&& is_numeric(ob_get_length()) === true
) {
ob_clean();
}
flush();
}
/**
* Returns the file mine type.
* @param string $file
* @return string
*/
private function getFileMineType(string $file): string
{
$fileName = basename($file);
/*
* This only for compatibility
* mime_content_type has been deprecated as the PECL extension file info
* provides the same functionality (and more) in a much cleaner way.
*/
$explodedFileName = explode('.', $fileName);
$lastElement = array_pop($explodedFileName);
$fileExt = strtolower($lastElement);
$mimeTypes = $this->wordpressConfig->getMimeTypes();
if ($this->php->functionExists('finfo_open') === true) {
$fileInfo = finfo_open(FILEINFO_MIME);
$fileMimeType = finfo_file($fileInfo, $file);
finfo_close($fileInfo);
} elseif ($this->php->functionExists('mime_content_type')) {
$fileMimeType = mime_content_type($file);
} elseif (isset($mimeTypes[$fileExt]) === true) {
$fileMimeType = $mimeTypes[$fileExt];
} else {
$fileMimeType = 'application/octet-stream';
}
return (string) $fileMimeType;
}
/**
* Adds the default header.
* @param string $file
* @param bool $isInline
*/
private function addDefaultHeader(string $file, bool $isInline)
{
$fileMimeType = $this->getFileMineType($file);
$contentDisposition = ($isInline === true) ? 'inline' : 'attachment';
$baseName = str_replace(' ', '_', basename($file));
header('Content-Description: File Transfer');
header('Content-Type: ' . $fileMimeType);
header("Content-Disposition: {$contentDisposition}; filename=\"{$baseName}\"");
}
/**
* Delivers the file via fopen.
* @param string $file
*/
private function deliverFileViaFopen(string $file)
{
$handler = fopen($file, 'r');
while (feof($handler) === false) {
if ($this->php->iniGet('safe_mode') !== '') {
$this->php->setTimeLimit(30);
}
echo $this->php->fread($handler, 1024);
}
}
/**
* Delivers the file.
* @param string $file
* @param bool $isInline
*/
private function deliverFile(string $file, bool $isInline)
{
header("HTTP/1.1 200 OK");
$downloadType = $this->mainConfig->getDownloadType();
if ($downloadType === 'xsendfile') {
header("X-Sendfile: {$file}");
}
$this->addDefaultHeader($file, $isInline);
if ($downloadType !== 'xsendfile') {
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
$this->clearBuffer();
if ($downloadType === 'fopen') {
$this->deliverFileViaFopen($file);
} else {
readfile($file);
}
}
}
/**
* Sets the seek start and end.
* @param string $range
* @param int $fileSize
* @param int|null $seekStart
* @param int|null $seekEnd
* @return bool
*/
private function getSeekStartEnd(string $range, int $fileSize, ?int &$seekStart, ?int &$seekEnd): bool
{
//Figure out download piece from range (if set)
$seek = explode('-', $range);
$seekStart = ($seek[0] !== '') ? abs((int) $seek[0]) : null;
$seekEnd = (isset($seek[1]) === true && $seek[1] !== '') ? abs((int) $seek[1]) : null;
$maxSize = $fileSize - 1;
if ($seekStart === null) {
$seekStart = $fileSize - $seekEnd;
$seekEnd = $maxSize;
} elseif ($seekEnd === null) {
$seekEnd = $maxSize;
}
//Start and end based on range (if set), else set defaults also check for invalid ranges.
$seekEnd = min($seekEnd, $maxSize);
return $seekStart < $seekEnd;
}
/**
* Reads the file partly.
* @param resource $fileHandler
* @param int $bytes
*/
private function readFilePartly($fileHandler, int $bytes)
{
$bytesLeft = $bytes;
$bufferSize = 1024;
while ($bytesLeft > 0 && feof($fileHandler) === false) {
$bytesToRead = min($bytesLeft, $bufferSize);
$bytesLeft -= $bytesToRead;
echo $this->php->fread($fileHandler, $bytesToRead);
$this->clearBuffer();
if ($this->php->connectionStatus() !== 0) {
$this->php->fClose($fileHandler);
break;
}
}
}
/**
* Returns the http ranges.
* @param int $fileSize
* @return array
*/
private function getRanges(int $fileSize): array
{
$httpRange = explode('=', $_SERVER['HTTP_RANGE']);
$originRanges = isset($httpRange[1]) === true ? $httpRange[1] : '';
$originRanges = explode(',', $originRanges);
$sizeUnit = $httpRange[0];
$ranges = [];
if ($sizeUnit === 'bytes') {
foreach ($originRanges as $originRange) {
if ($this->getSeekStartEnd($originRange, $fileSize, $seekStart, $seekEnd) === false) {
$ranges = [];
break;
}
$ranges[] = [$seekStart, $seekEnd];
}
}
return $ranges;
}
/**
* Returns the extra contents.
* @param string $file
* @param array $ranges
* @param int|null $contentLength
* @param string|null $boundary
* @return array
*/
private function getExtraContents(string $file, array $ranges, ?int &$contentLength, ?string &$boundary): array
{
$contentLength = 0;
$extraContents = [];
//More than one range is requested?
if (count($ranges) > 1) {
$boundary = 'g45d64df96bmdf4sdgh45hf5';
$fullBoundary = "\r\n--{$boundary}--\r\n";
$fileSize = filesize($file);
$mineType = $this->getFileMineType($file);
//compute content length
foreach ($ranges as $index => $range) {
list($seekStart, $seekEnd) = $range;
$extraContent = $fullBoundary;
$extraContent .= "Content-Type: {$mineType}\r\n";
$extraContent .= "Content-Range: bytes $seekStart-$seekEnd/$fileSize\r\n\r\n";
$extraContents[$index] = $extraContent;
$contentLength += strlen($extraContent) + ($seekEnd - $seekStart + 1);
}
$contentLength += strlen($fullBoundary);
$extraContents[] = $fullBoundary;
}
return $extraContents;
}
/**
* Delivers the file partial.
* @param string $file
* @param bool $isInline
*/
private function deliverFilePartial(string $file, bool $isInline)
{
$fileSize = filesize($file);
$ranges = $this->getRanges($fileSize);
if ($ranges !== []) {
$extraContents = $this->getExtraContents($file, $ranges, $contentLength, $boundary);
header('HTTP/1.1 206 Partial Content');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
if ($extraContents === []) {
$this->addDefaultHeader($file, $isInline);
list($seekStart, $seekEnd) = $ranges[0];
$contentLength = ($seekEnd - $seekStart + 1);
header("Content-Range: bytes {$seekStart}-{$seekEnd}/{$fileSize}");
} else {
header("Content-Type: multipart/x-byteranges; boundary={$boundary}");
}
header("Content-Length: {$contentLength}");
$fileHandler = fopen($file, 'r');
foreach ($ranges as $index => $range) {
if (isset($extraContents[$index]) === true) {
echo $extraContents[$index];
}
list($seekStart, $seekEnd) = $ranges[0];
fseek($fileHandler, $seekStart);
$this->readFilePartly($fileHandler, $seekEnd - $seekStart + 1);
}
if ($extraContents !== []) {
echo end($extraContents);
$this->clearBuffer();
}
} else {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: */$fileSize");
}
}
/**
* Checks if the file is an inline file
* @param string $file
* @return bool
*/
private function isInlineFile(string $file): bool
{
$inlineFiles = array_map('trim', explode(',', (string) $this->mainConfig->getInlineFiles()));
$map = array_flip($inlineFiles);
$extension = pathinfo($file, PATHINFO_EXTENSION);
return isset($map[$extension]);
}
/**
* Delivers the content of the requested file.
* @param string $file
* @param bool $isImage
*/
public function getFile(string $file, bool $isImage)
{
//Deliver content
if (file_exists($file) === true) {
$isInline = $isImage === true || $this->isInlineFile($file) === true;
if (isset($_SERVER['HTTP_RANGE']) === true
&& isset($_SERVER['REQUEST_METHOD']) === true
&& $_SERVER['REQUEST_METHOD'] === 'GET'
) {
$this->deliverFilePartial($file, $isInline);
} else {
$this->deliverFile($file, $isInline);
}
$this->php->callExit();
} else {
$this->wordpress->wpDie(
TXT_UAM_FILE_NOT_FOUND_ERROR_MESSAGE,
TXT_UAM_FILE_NOT_FOUND_ERROR_TITLE,
['response' => 404]
);
}
}
/**
* Returns the current file protection handler.
* @return FileProtectionInterface
*/
private function getCurrentFileProtectionHandler(): FileProtectionInterface
{
if ($this->wordpress->isNginx() === true) {
return $this->fileProtectionFactory->createNginxFileProtection();
}
return $this->fileProtectionFactory->createApacheFileProtection();
}
/**
* Returns the file protection file.
* @return string
*/
public function getFileProtectionFileName(): string
{
return $this->getCurrentFileProtectionHandler()->getFileNameWithPath(
$this->wordpressConfig->getUploadDirectory()
);
}
/**
* Creates a protection file.
* @param string $dir The destination directory.
* @param string $objectType The object type.
* @return false
*/
public function createFileProtection($dir = null, $objectType = null): bool
{
$dir = ($dir === null) ? $this->wordpressConfig->getUploadDirectory() : $dir;
if ($dir !== null) {
return $this->getCurrentFileProtectionHandler()->create($dir, $objectType);
}
return false;
}
/**
* Deletes the protection files.
* @param string $dir The destination directory.
* @return false
*/
public function deleteFileProtection($dir = null): bool
{
$dir = ($dir === null) ? $this->wordpressConfig->getUploadDirectory() : $dir;
if ($dir !== null) {
return $this->getCurrentFileProtectionHandler()->delete($dir);
}
return false;
}
/**
* Delivers a xsendfile test file.
*/
public function deliverXSendFileTestFile()
{
$file = $this->wordpressConfig->getUploadDirectory() . DIRECTORY_SEPARATOR . self::X_SEND_FILE_TEST_FILE;
file_put_contents($file, 'success');
header("X-Sendfile: {$file}");
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
$this->php->callExit();
}
/**
* Removes the xsendfile test file if exists.
*/
public function removeXSendFileTestFile()
{
$file = $this->wordpressConfig->getUploadDirectory() . DIRECTORY_SEPARATOR . self::X_SEND_FILE_TEST_FILE;
if (file_exists($file) === true) {
unlink($file);
}
}
}