| 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_Stream_Limit implements MWP_Stream_Interface |
| 12 |
{ |
| 13 |
|
| 14 |
/** |
| 15 |
* @var MWP_Stream_Interface |
| 16 |
*/ |
| 17 |
private $stream; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var int |
| 21 |
*/ |
| 22 |
private $offset = 0; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var int |
| 26 |
*/ |
| 27 |
private $limit = -1; |
| 28 |
|
| 29 |
public function __construct(MWP_Stream_Interface $stream, $offset = 0, $limit = -1) |
| 30 |
{ |
| 31 |
$this->stream = $stream; |
| 32 |
$this->setOffset($offset); |
| 33 |
$this->limit = $limit; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* {@inheritdoc} |
| 38 |
*/ |
| 39 |
public function eof() |
| 40 |
{ |
| 41 |
// Always return true if the underlying stream is EOF |
| 42 |
if ($this->stream->eof()) { |
| 43 |
return true; |
| 44 |
} |
| 45 |
|
| 46 |
// No limit and the underlying stream is not at EOF |
| 47 |
if ($this->limit == -1) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
$tell = $this->stream->tell(); |
| 52 |
if ($tell === false) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
return $tell >= $this->offset + $this->limit; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* {@inheritdoc} |
| 61 |
*/ |
| 62 |
public function read($length) |
| 63 |
{ |
| 64 |
if ($this->limit == -1) { |
| 65 |
return $this->stream->read($length); |
| 66 |
} |
| 67 |
|
| 68 |
// Check if the current position is less than the total allowed |
| 69 |
// bytes + original offset |
| 70 |
$remaining = ($this->offset + $this->limit) - $this->stream->tell(); |
| 71 |
if ($remaining > 0) { |
| 72 |
// Only return the amount of requested data, ensuring that the byte |
| 73 |
// limit is not exceeded |
| 74 |
return $this->stream->read(min($remaining, $length)); |
| 75 |
} else { |
| 76 |
return false; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* {@inheritdoc} |
| 82 |
*/ |
| 83 |
public function setOffset($offset) |
| 84 |
{ |
| 85 |
$current = $this->stream->tell(); |
| 86 |
|
| 87 |
if ($current !== $offset) { |
| 88 |
// If the stream cannot seek to the offset position, then read to it |
| 89 |
if (!$this->stream->seek($offset)) { |
| 90 |
$this->stream->read($offset - $current); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
$this->offset = $offset; |
| 95 |
|
| 96 |
return $this; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* {@inheritdoc} |
| 101 |
*/ |
| 102 |
public function tell() |
| 103 |
{ |
| 104 |
return $this->stream->tell() - $this->offset; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* {@inheritdoc} |
| 109 |
*/ |
| 110 |
public function isSeekable() |
| 111 |
{ |
| 112 |
return $this->stream->isSeekable(); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* {@inheritdoc} |
| 117 |
*/ |
| 118 |
public function seek($offset, $whence = SEEK_SET) |
| 119 |
{ |
| 120 |
if ($whence !== SEEK_SET || $offset < 0) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
$offset += $this->offset; |
| 125 |
|
| 126 |
if ($this->limit !== -1) { |
| 127 |
if ($offset > $this->offset + $this->limit) { |
| 128 |
$offset = $this->offset + $this->limit; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return $this->stream->seek($offset); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* {@inheritdoc} |
| 137 |
*/ |
| 138 |
public function close() |
| 139 |
{ |
| 140 |
$this->stream->close(); |
| 141 |
} |
| 142 |
|
| 143 |
public function __toString() |
| 144 |
{ |
| 145 |
$buffer = ''; |
| 146 |
|
| 147 |
while (!$this->eof()) { |
| 148 |
$buffer .= $this->read(1048576); |
| 149 |
} |
| 150 |
|
| 151 |
return $buffer; |
| 152 |
} |
| 153 |
} |
| 154 |
|