This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
FileReaderResponse.php
474 lines (400 loc) · 16.5 KB
/
FileReaderResponse.php
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
<?php
/*
* Copyright 2007-2016 Abstrium <contact (at) pydio.com>
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com/>.
*/
namespace Pydio\Core\Http\Response;
use Pydio\Access\Core\MetaStreamWrapper;
use Pydio\Access\Core\Model\AJXP_Node;
use Pydio\Core\Controller\HTMLWriter;
use Pydio\Core\Services\ApiKeysService;
use Pydio\Core\Services\AuthService;
use Pydio\Core\Services\ConfService;
use Pydio\Core\Utils\ApplicationState;
use Pydio\Core\Utils\FileHelper;
use Pydio\Core\Utils\Vars\PathUtils;
use Pydio\Core\Utils\Vars\StatHelper;
use Pydio\Core\Utils\TextEncoder;
use Pydio\Log\Core\Logger;
use Zend\Diactoros\ServerRequestFactory;
defined('AJXP_EXEC') or die('Access not allowed');
/**
* Class FileReaderResponse
* Async class that can be used as a Body for a ResponseInterface. Will read the file or data
* when the reponse is emitted.
*
* @package Pydio\Core\Http\Response
*/
class FileReaderResponse extends AsyncResponseStream
{
/** @var AJXP_Node */
private $node;
/** @var string */
private $file;
/** @var string */
private $data;
/** @var string */
private $localName = "";
/** @var string */
private $headerType = "force-download";
/** @var integer */
private $offset = -1;
/** @var integer */
private $length = -1;
/** @var callable */
private $preRead;
/** @var callable */
private $postRead;
/** @var boolean */
private $unlinkAfterRead;
/**
* FileReaderResponse constructor.
* @param AJXP_Node|string $nodeOrFile
* @param string $data
*/
public function __construct($nodeOrFile = null, $data = null)
{
$callable = array($this, "readData");
if($nodeOrFile instanceof AJXP_Node){
$this->node = $nodeOrFile;
}else{
$this->file = $nodeOrFile;
}
if($data !== null){
$this->data = $data;
}
parent::__construct($callable);
}
/**
* Simple debug instruction
* @param $message
* @param array $params
*/
protected function logDebug($message, $params = []){
Logger::getInstance()->logDebug("FileReader", $message, $params);
}
/**
* Set a possible header type "plain", "image" if it's force attachement.
* @param $headerType
*/
public function setHeaderType($headerType){
$this->headerType = $headerType;
}
/**
* Set the name as it should appear in the download window,
* if it's not the file basename
* @param $localName
*/
public function setLocalName($localName){
$this->localName = $localName;
}
/**
* @param integer $offset
* @param integer $length
*/
public function setPartial($offset, $length){
$this->offset = $offset;
$this->length = $length;
}
/**
* Callback to be triggered just before reading the file
* @param callable $pre
*/
public function setPreReadCallback(callable $pre){
$this->preRead = $pre;
}
/**
* Callback to be triggered at the end of the file read operation
* @param callable $post
*/
public function setPostReadCallback(callable $post){
$this->postRead = $post;
}
/**
* Set a flag to trigger an unlink after reading the file.
*/
public function setUnlinkAfterRead(){
$this->unlinkAfterRead = true;
}
/**
* Actually read the data to the output
* @throws \Exception
*/
protected function readData(){
if($this->preRead){
call_user_func($this->preRead);
}
$this->readFile($this->node, $this->file, $this->data, $this->headerType, $this->localName, $this->offset, $this->length);
if($this->postRead){
call_user_func($this->postRead);
}
}
/**
* @param AJXP_Node|null $node
* @param string $filePath
* @param string|bool $data
* @param string $headerType
* @param string $localName
* @param int $byteOffset
* @param int $byteLength
* @throws \Exception
*/
public function readFile($node = null, $filePath = null, $data = null, $headerType="plain", $localName="", $byteOffset=-1, $byteLength=-1)
{
if($node !== null){
$filePathOrData = $node->getUrl();
}else{
$filePathOrData = $filePath;
}
if(!$data === null && !file_exists($filePathOrData)){
throw new \Exception("File $filePathOrData not found!");
}
$confGzip = ConfService::getGlobalConf("GZIP_COMPRESSION");
$confGzipLimit = ConfService::getGlobalConf("GZIP_LIMIT");
$confUseAccelerator = ConfService::getGlobalConf("USE_DOWNLOAD_ACCELERATOR");
if($this->unlinkAfterRead && $filePathOrData !== null && empty($confUseAccelerator)){
register_shutdown_function(function () use ($filePathOrData){
FileHelper::silentUnlink($filePathOrData);
});
}
$fakeReq = ServerRequestFactory::fromGlobals();
$serverParams = $fakeReq->getServerParams();
if ($node !== null && !$node->wrapperIsRemote()) {
$originalFilePath = $filePathOrData;
$filePathOrData = PathUtils::patchPathForBaseDir($filePathOrData);
}
session_write_close();
restore_error_handler();
restore_exception_handler();
set_exception_handler('Pydio\Access\Driver\StreamProvider\FS\download_exception_handler');
set_error_handler('Pydio\Access\Driver\StreamProvider\FS\download_exception_handler');
// required for IE, otherwise Content-disposition is ignored
if (ini_get('zlib.output_compression')) {
ApplicationState::safeIniSet('zlib.output_compression', 'Off');
}
$isFile = ($data !== null) && !$confGzip;
if ($byteLength == -1) {
if ($data !== null) {
$size = strlen($data);
} else if ($node === null) {
$size = sprintf("%u", filesize($filePathOrData));
} else {
$size = filesize($filePathOrData);
}
} else {
$size = $byteLength;
}
if ($confGzip && ($size > $confGzipLimit || !function_exists("gzencode") || (isSet($serverParams['HTTP_ACCEPT_ENCODING']) && strpos($serverParams['HTTP_ACCEPT_ENCODING'], 'gzip') === FALSE))) {
$confGzip = false;
}
$localName = ($localName =="" ? basename((isSet($originalFilePath)?$originalFilePath:$filePathOrData)) : $localName);
if ($headerType == "plain") {
header("Content-type:text/plain");
header("Content-Length: ".$size);
} else if ($headerType == "image") {
header("Content-Type: ". StatHelper::getImageMimeType(basename($filePathOrData)) ."; name=\"".$localName."\"");
header("Content-Length: ".$size);
header('Cache-Control: public');
} else {
if ($isFile) {
header("Accept-Ranges: 0-$size");
$this->logDebug("Sending accept range 0-$size");
}
// Check if we have a range header (we are resuming a transfer)
if ( isset($serverParams['HTTP_RANGE']) && $isFile && $size != 0 ) {
if ($headerType == "stream_content") {
if (extension_loaded('fileinfo') && (( $node !== null && !$node->wrapperIsRemote()) || $filePath !== null)) {
$fInfo = new \fInfo( FILEINFO_MIME );
if($node !== null){
$realfile = $node->getRealFile();
}else{
$realfile = $filePathOrData;
}
$mimeType = $fInfo->file($realfile);
$splitChar = explode(";", $mimeType);
$mimeType = trim($splitChar[0]);
$this->logDebug("Detected mime $mimeType for $realfile");
} else {
$mimeType = StatHelper::getStreamingMimeType(basename($filePathOrData));
}
header('Content-type: '.$mimeType);
}
// multiple ranges, which can become pretty complex, so ignore it for now
$ranges = explode('=', $_SERVER['HTTP_RANGE']);
$offsets = explode('-', $ranges[1]);
$offset = floatval($offsets[0]);
$length = floatval($offsets[1]) - $offset;
if (!$length) $length = $size - $offset;
if ($length + $offset > $size || $length < 0) $length = $size - $offset;
$this->logDebug('Content-Range: bytes ' . $offset . '-' . $length . '/' . $size);
header('HTTP/1.1 206 Partial Content');
header('Content-Range: bytes ' . $offset . '-' . ($offset + $length) . '/' . $size);
header("Content-Length: ". $length);
$file = fopen($filePathOrData, 'rb');
if(!is_resource($file)){
throw new \Exception("Failed opening file ".$filePathOrData);
}
fseek($file, 0);
$relOffset = $offset;
while ($relOffset > 2.0E9) {
// seek to the requested offset, this is 0 if it's not a partial content request
fseek($file, 2000000000, SEEK_CUR);
$relOffset -= 2000000000;
// This works because we never overcome the PHP 32 bit limit
}
fseek($file, $relOffset, SEEK_CUR);
while(ob_get_level()) ob_end_flush();
$readSize = 0.0;
$bufferSize = 1024 * 8;
while (!feof($file) && $readSize < $length && connection_status() == 0) {
$this->logDebug("dl reading $readSize to $length", ["httpRange" => $serverParams["HTTP_RANGE"]]);
echo fread($file, $bufferSize);
$readSize += $bufferSize;
flush();
}
fclose($file);
return;
} else {
if ($confGzip) {
$gzippedData = ($data?gzencode($filePathOrData,9):gzencode(file_get_contents($filePathOrData), 9));
$size = strlen($gzippedData);
}
HTMLWriter::emitAttachmentsHeaders($localName, $size, $isFile, $confGzip);
if ($confGzip && isSet($gzippedData)) {
print $gzippedData;
return;
}
}
}
if ($data !== null) {
print($data);
} else {
if ( !empty($confUseAccelerator)){
$requestSent = $this->sendToAccelerator($confUseAccelerator, ($node !== null ? $node : $filePath), $serverParams);
if($requestSent){
return;
}
}
$stream = fopen("php://output", "a");
if ($node == null) {
$this->logDebug("realFS!", array("file"=>$filePathOrData));
$fp = fopen($filePathOrData, "rb");
if(!is_resource($fp)){
throw new \Exception("Failed opening file ".$filePathOrData);
}
if ($byteOffset != -1) {
fseek($fp, $byteOffset);
}
$sentSize = 0;
$readChunk = 4096;
while (!feof($fp)) {
if ( $byteLength != -1 && ($sentSize + $readChunk) >= $byteLength) {
// compute last chunk and break after
$readChunk = $byteLength - $sentSize;
$break = true;
}
$data = fread($fp, $readChunk);
$dataSize = strlen($data);
fwrite($stream, $data, $dataSize);
$sentSize += $dataSize;
if (isSet($break)) {
break;
}
}
fclose($fp);
} else {
MetaStreamWrapper::copyFileInStream($filePathOrData, $stream);
}
fflush($stream);
fclose($stream);
}
}
/**
* @param string $accelConfiguration
* @param string|AJXP_Node $localPathOrNode
* @param array $serverParams
* @return bool Wether headers were sent and we should interrupt DL now or not.
*/
protected function sendToAccelerator($accelConfiguration, $localPathOrNode, $serverParams){
$remoteNode = false;
if($localPathOrNode instanceof AJXP_Node) {
$filePathOrData = $localPathOrNode->getRealFile();
$remoteNode = $localPathOrNode->wrapperIsRemote();
}else{
$filePathOrData = $localPathOrNode;
}
// TRY XSendFile for local FS nodes or local file
if (!$remoteNode && $accelConfiguration === "xsendfile") {
$filePathOrData = str_replace("\\", "/", $filePathOrData);
$server_name = $serverParams["SERVER_SOFTWARE"];
$regex = '/^(lighttpd\/1.4).([0-9]{2}$|[0-9]{3}$|[0-9]{4}$)+/';
if(preg_match($regex, $server_name))
$header_sendfile = "X-LIGHTTPD-send-file";
else
$header_sendfile = "X-Sendfile";
header($header_sendfile.": ".TextEncoder::toUTF8($filePathOrData));
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . basename($filePathOrData) . '"');
return true;
}
// TRY XAccelRedirect for local FS nodes or local file
if (!$remoteNode && $accelConfiguration === "xaccelredirect" && array_key_exists("HTTP_X_ACCEL_MAPPING", $serverParams)) {
$filePathOrData = str_replace("\\", "/", $filePathOrData);
$filePathOrData = TextEncoder::toUTF8($filePathOrData);
$mapping = explode('=',$serverParams['X-Accel-Mapping']);
$replacecount = 0;
$accelfile = str_replace($mapping[0],$mapping[1],$filePathOrData,$replacecount);
if ($replacecount == 1) {
header("X-Accel-Redirect: $accelfile");
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . basename($accelfile) . '"');
return true;
} else {
$this->logDebug("X-Accel-Redirect: Problem with X-Accel-Mapping for file $filePathOrData");
return false;
}
}
// Pydio Agent acceleration - We make sure that request was really proxied by Agent, by checking a specific header.
if($accelConfiguration === "pydio" && array_key_exists("HTTP_X_PYDIO_DOWNLOAD_SUPPORTED", $serverParams)
&& ApiKeysService::requestHasValidHeadersForAdminTask($serverParams, "go-upload")) {
if ($localPathOrNode instanceof AJXP_Node) {
$options = MetaStreamWrapper::getResolvedOptionsForNode($localPathOrNode);
if($options["TYPE"] === "php"){
// Not implemented
return false;
}
$path = $localPathOrNode->getPath();
}else{
$options = ["TYPE" => "local"];
$path = $localPathOrNode;
}
$data = [
"OPTIONS" => $options,
"PATH" => $path
];
if($this->unlinkAfterRead){
$data["UNLINK_AFTER_READ"] = true;
}
header("X-Pydio-Download-Redirect: ".json_encode($data));
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . basename($path) . '"');
return true;
}
return false;
}
}