| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <contact@managewp.com> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
class MWP_IncrementalBackup_FileReader |
| 12 |
{ |
| 13 |
|
| 14 |
/** |
| 15 |
* @var int |
| 16 |
*/ |
| 17 |
private $chunkByteSize = 4096; |
| 18 |
|
| 19 |
/** |
| 20 |
* @return int |
| 21 |
*/ |
| 22 |
public function getChunkByteSize() |
| 23 |
{ |
| 24 |
return $this->chunkByteSize; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* @param int $chunkByteSize |
| 29 |
*/ |
| 30 |
public function setChunkByteSize($chunkByteSize) |
| 31 |
{ |
| 32 |
$this->chunkByteSize = $chunkByteSize; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* |
| 37 |
* |
| 38 |
* @param string $realPath |
| 39 |
* @param int $offset |
| 40 |
* @param int $limit |
| 41 |
* |
| 42 |
* @return mixed |
| 43 |
*/ |
| 44 |
public function readFileContents($realPath, $offset = 0, $limit = 0) |
| 45 |
{ |
| 46 |
if (!file_exists($realPath)) { |
| 47 |
return null; |
| 48 |
} |
| 49 |
|
| 50 |
$handle = fopen($realPath, "rb"); |
| 51 |
if (!$handle) { |
| 52 |
return null; |
| 53 |
} |
| 54 |
|
| 55 |
$contentLength = 0; |
| 56 |
$buffer = ''; |
| 57 |
|
| 58 |
if ($limit === 0) { |
| 59 |
$limit = filesize($realPath) - $offset; |
| 60 |
} |
| 61 |
|
| 62 |
if ($offset !== 0) { |
| 63 |
fseek($handle, $offset); |
| 64 |
} |
| 65 |
|
| 66 |
while ($limit > 0) { |
| 67 |
$chunkSize = $limit > $this->chunkByteSize ? $this->chunkByteSize : $limit; |
| 68 |
$limit = $limit - $chunkSize; |
| 69 |
$contentLength = $contentLength + $chunkSize; |
| 70 |
|
| 71 |
$contents = fread($handle, $chunkSize); |
| 72 |
$buffer = $buffer.$contents; |
| 73 |
} |
| 74 |
|
| 75 |
fclose($handle); |
| 76 |
|
| 77 |
return array($buffer, $contentLength); |
| 78 |
} |
| 79 |
} |
| 80 |
|