Edit File by line
/home/barbar84/www/wp-conte.../plugins/updraftp.../includes
File: class-partialfileservlet.php
<?php
[0] Fix | Delete
[1] Fix | Delete
// https://stackoverflow.com/questions/157318/resumable-downloads-when-using-php-to-send-the-file
[2] Fix | Delete
// User: DaveRandom
[3] Fix | Delete
[4] Fix | Delete
if (!class_exists('UpdraftPlus_NonExistentFileException')):
[5] Fix | Delete
class UpdraftPlus_NonExistentFileException extends RuntimeException {}
[6] Fix | Delete
endif;
[7] Fix | Delete
[8] Fix | Delete
if (!class_exists('UpdraftPlus_UnreadableFileException')):
[9] Fix | Delete
class UpdraftPlus_UnreadableFileException extends RuntimeException {}
[10] Fix | Delete
endif;
[11] Fix | Delete
[12] Fix | Delete
if (!class_exists('UpdraftPlus_UnsatisfiableRangeException')):
[13] Fix | Delete
class UpdraftPlus_UnsatisfiableRangeException extends RuntimeException {}
[14] Fix | Delete
endif;
[15] Fix | Delete
[16] Fix | Delete
if (!class_exists('UpdraftPlus_InvalidRangeHeaderException')):
[17] Fix | Delete
class UpdraftPlus_InvalidRangeHeaderException extends RuntimeException {}
[18] Fix | Delete
endif;
[19] Fix | Delete
[20] Fix | Delete
class UpdraftPlus_RangeHeader
[21] Fix | Delete
{
[22] Fix | Delete
/**
[23] Fix | Delete
* The first byte in the file to send (0-indexed), a null value indicates the last
[24] Fix | Delete
* $end bytes
[25] Fix | Delete
*
[26] Fix | Delete
* @var int|null
[27] Fix | Delete
*/
[28] Fix | Delete
private $firstByte;
[29] Fix | Delete
[30] Fix | Delete
/**
[31] Fix | Delete
* The last byte in the file to send (0-indexed), a null value indicates $start to
[32] Fix | Delete
* EOF
[33] Fix | Delete
*
[34] Fix | Delete
* @var int|null
[35] Fix | Delete
*/
[36] Fix | Delete
private $lastByte;
[37] Fix | Delete
[38] Fix | Delete
/**
[39] Fix | Delete
* Create a new instance from a Range header string
[40] Fix | Delete
*
[41] Fix | Delete
* @param string $header
[42] Fix | Delete
* @return UpdraftPlus_RangeHeader
[43] Fix | Delete
*/
[44] Fix | Delete
public static function createFromHeaderString($header)
[45] Fix | Delete
{
[46] Fix | Delete
if ($header === null) {
[47] Fix | Delete
return null;
[48] Fix | Delete
}
[49] Fix | Delete
if (!preg_match('/^\s*([A-Za-z]+)\s*=\s*(\d*)\s*-\s*(\d*)\s*(?:,|$)/', $header, $info)) {
[50] Fix | Delete
throw new UpdraftPlus_InvalidRangeHeaderException('Invalid header format');
[51] Fix | Delete
} else if (strtolower($info[1]) !== 'bytes') {
[52] Fix | Delete
throw new UpdraftPlus_InvalidRangeHeaderException('Unknown range unit: ' . $info[1]);
[53] Fix | Delete
}
[54] Fix | Delete
[55] Fix | Delete
return new self(
[56] Fix | Delete
$info[2] === '' ? null : $info[2],
[57] Fix | Delete
$info[3] === '' ? null : $info[3]
[58] Fix | Delete
);
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
/**
[62] Fix | Delete
* @param int|null $firstByte
[63] Fix | Delete
* @param int|null $lastByte
[64] Fix | Delete
* @throws UpdraftPlus_InvalidRangeHeaderException
[65] Fix | Delete
*/
[66] Fix | Delete
public function __construct($firstByte, $lastByte)
[67] Fix | Delete
{
[68] Fix | Delete
$this->firstByte = $firstByte === null ? $firstByte : (int)$firstByte;
[69] Fix | Delete
$this->lastByte = $lastByte === null ? $lastByte : (int)$lastByte;
[70] Fix | Delete
[71] Fix | Delete
if ($this->firstByte === null && $this->lastByte === null) {
[72] Fix | Delete
throw new UpdraftPlus_InvalidRangeHeaderException(
[73] Fix | Delete
'Both start and end position specifiers empty'
[74] Fix | Delete
);
[75] Fix | Delete
} else if ($this->firstByte < 0 || $this->lastByte < 0) {
[76] Fix | Delete
throw new UpdraftPlus_InvalidRangeHeaderException(
[77] Fix | Delete
'Position specifiers cannot be negative'
[78] Fix | Delete
);
[79] Fix | Delete
} else if ($this->lastByte !== null && $this->lastByte < $this->firstByte) {
[80] Fix | Delete
throw new UpdraftPlus_InvalidRangeHeaderException(
[81] Fix | Delete
'Last byte cannot be less than first byte'
[82] Fix | Delete
);
[83] Fix | Delete
}
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
/**
[87] Fix | Delete
* Get the start position when this range is applied to a file of the specified size
[88] Fix | Delete
*
[89] Fix | Delete
* @param int $fileSize
[90] Fix | Delete
* @return int
[91] Fix | Delete
* @throws UpdraftPlus_UnsatisfiableRangeException
[92] Fix | Delete
*/
[93] Fix | Delete
public function getStartPosition($fileSize)
[94] Fix | Delete
{
[95] Fix | Delete
$size = (int)$fileSize;
[96] Fix | Delete
[97] Fix | Delete
if ($this->firstByte === null) {
[98] Fix | Delete
return ($size - 1) - $this->lastByte;
[99] Fix | Delete
}
[100] Fix | Delete
[101] Fix | Delete
if ($size <= $this->firstByte) {
[102] Fix | Delete
throw new UpdraftPlus_UnsatisfiableRangeException(
[103] Fix | Delete
'Start position is after the end of the file'
[104] Fix | Delete
);
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
return $this->firstByte;
[108] Fix | Delete
}
[109] Fix | Delete
[110] Fix | Delete
/**
[111] Fix | Delete
* Get the end position when this range is applied to a file of the specified size
[112] Fix | Delete
*
[113] Fix | Delete
* @param int $fileSize
[114] Fix | Delete
* @return int
[115] Fix | Delete
* @throws UpdraftPlus_UnsatisfiableRangeException
[116] Fix | Delete
*/
[117] Fix | Delete
public function getEndPosition($fileSize)
[118] Fix | Delete
{
[119] Fix | Delete
$size = (int)$fileSize;
[120] Fix | Delete
[121] Fix | Delete
if ($this->lastByte === null) {
[122] Fix | Delete
return $size - 1;
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
if ($size <= $this->lastByte) {
[126] Fix | Delete
throw new UpdraftPlus_UnsatisfiableRangeException(
[127] Fix | Delete
'End position is after the end of the file'
[128] Fix | Delete
);
[129] Fix | Delete
}
[130] Fix | Delete
[131] Fix | Delete
return $this->lastByte;
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
/**
[135] Fix | Delete
* Get the length when this range is applied to a file of the specified size
[136] Fix | Delete
*
[137] Fix | Delete
* @param int $fileSize
[138] Fix | Delete
* @return int
[139] Fix | Delete
* @throws UpdraftPlus_UnsatisfiableRangeException
[140] Fix | Delete
*/
[141] Fix | Delete
public function getLength($fileSize)
[142] Fix | Delete
{
[143] Fix | Delete
$size = (int)$fileSize;
[144] Fix | Delete
[145] Fix | Delete
return $this->getEndPosition($size) - $this->getStartPosition($size) + 1;
[146] Fix | Delete
}
[147] Fix | Delete
[148] Fix | Delete
/**
[149] Fix | Delete
* Get a Content-Range header corresponding to this Range and the specified file
[150] Fix | Delete
* size
[151] Fix | Delete
*
[152] Fix | Delete
* @param int $fileSize
[153] Fix | Delete
* @return string
[154] Fix | Delete
*/
[155] Fix | Delete
public function getContentRangeHeader($fileSize)
[156] Fix | Delete
{
[157] Fix | Delete
return 'bytes ' . $this->getStartPosition($fileSize) . '-'
[158] Fix | Delete
. $this->getEndPosition($fileSize) . '/' . $fileSize;
[159] Fix | Delete
}
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
class UpdraftPlus_PartialFileServlet
[163] Fix | Delete
{
[164] Fix | Delete
/**
[165] Fix | Delete
* The range header on which the data transmission will be based
[166] Fix | Delete
*
[167] Fix | Delete
* @var UpdraftPlus_RangeHeader|null
[168] Fix | Delete
*/
[169] Fix | Delete
private $range;
[170] Fix | Delete
[171] Fix | Delete
/**
[172] Fix | Delete
* @param UpdraftPlus_RangeHeader $range Range header on which the transmission will be based
[173] Fix | Delete
*/
[174] Fix | Delete
public function __construct($range = null)
[175] Fix | Delete
{
[176] Fix | Delete
$this->range = $range;
[177] Fix | Delete
}
[178] Fix | Delete
[179] Fix | Delete
/**
[180] Fix | Delete
* Send part of the data in a seekable stream resource to the output buffer
[181] Fix | Delete
*
[182] Fix | Delete
* @param resource $fp Stream resource to read data from
[183] Fix | Delete
* @param int $start Position in the stream to start reading
[184] Fix | Delete
* @param int $length Number of bytes to read
[185] Fix | Delete
* @param int $chunkSize Maximum bytes to read from the file in a single operation
[186] Fix | Delete
*/
[187] Fix | Delete
private function sendDataRange($fp, $start, $length, $chunkSize = 2097152)
[188] Fix | Delete
{
[189] Fix | Delete
if ($start > 0) {
[190] Fix | Delete
fseek($fp, $start, SEEK_SET);
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
while ($length) {
[194] Fix | Delete
$read = ($length > $chunkSize) ? $chunkSize : $length;
[195] Fix | Delete
$length -= $read;
[196] Fix | Delete
echo fread($fp, $read);
[197] Fix | Delete
}
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
/**
[201] Fix | Delete
* Send the headers that are included regardless of whether a range was requested
[202] Fix | Delete
*
[203] Fix | Delete
* @param string $fileName
[204] Fix | Delete
* @param int $contentLength
[205] Fix | Delete
* @param string $contentType
[206] Fix | Delete
*/
[207] Fix | Delete
private function sendDownloadHeaders($fileName, $contentLength, $contentType)
[208] Fix | Delete
{
[209] Fix | Delete
header('Content-Type: ' . $contentType);
[210] Fix | Delete
header('Content-Length: ' . $contentLength);
[211] Fix | Delete
header('Content-Disposition: attachment; filename="' . $fileName . '"');
[212] Fix | Delete
header('Accept-Ranges: bytes');
[213] Fix | Delete
}
[214] Fix | Delete
[215] Fix | Delete
/**
[216] Fix | Delete
* Send data from a file based on the current Range header
[217] Fix | Delete
*
[218] Fix | Delete
* @param string $path Local file system path to serve
[219] Fix | Delete
* @param string $contentType MIME type of the data stream
[220] Fix | Delete
*/
[221] Fix | Delete
public function sendFile($path, $contentType = 'application/octet-stream')
[222] Fix | Delete
{
[223] Fix | Delete
// Make sure the file exists and is a file, otherwise we are wasting our time
[224] Fix | Delete
$localPath = realpath($path);
[225] Fix | Delete
if ($localPath === false || !is_file($localPath)) {
[226] Fix | Delete
throw new UpdraftPlus_NonExistentFileException(
[227] Fix | Delete
$path . ' does not exist or is not a file'
[228] Fix | Delete
);
[229] Fix | Delete
}
[230] Fix | Delete
[231] Fix | Delete
// Make sure we can open the file for reading
[232] Fix | Delete
if (!$fp = fopen($localPath, 'r')) {
[233] Fix | Delete
throw new UpdraftPlus_UnreadableFileException(
[234] Fix | Delete
'Failed to open ' . $localPath . ' for reading'
[235] Fix | Delete
);
[236] Fix | Delete
}
[237] Fix | Delete
[238] Fix | Delete
$fileSize = filesize($localPath);
[239] Fix | Delete
[240] Fix | Delete
if ($this->range == null) {
[241] Fix | Delete
// No range requested, just send the whole file
[242] Fix | Delete
header('HTTP/1.1 200 OK');
[243] Fix | Delete
$this->sendDownloadHeaders(basename($localPath), $fileSize, $contentType);
[244] Fix | Delete
[245] Fix | Delete
fpassthru($fp);
[246] Fix | Delete
} else {
[247] Fix | Delete
// Send the request range
[248] Fix | Delete
header('HTTP/1.1 206 Partial Content');
[249] Fix | Delete
header('Content-Range: ' . $this->range->getContentRangeHeader($fileSize));
[250] Fix | Delete
$this->sendDownloadHeaders(
[251] Fix | Delete
basename($localPath),
[252] Fix | Delete
$this->range->getLength($fileSize),
[253] Fix | Delete
$contentType
[254] Fix | Delete
);
[255] Fix | Delete
[256] Fix | Delete
$this->sendDataRange(
[257] Fix | Delete
$fp,
[258] Fix | Delete
$this->range->getStartPosition($fileSize),
[259] Fix | Delete
$this->range->getLength($fileSize)
[260] Fix | Delete
);
[261] Fix | Delete
}
[262] Fix | Delete
[263] Fix | Delete
fclose($fp);
[264] Fix | Delete
}
[265] Fix | Delete
}
[266] Fix | Delete
[267] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function