forked from zeebinz/rarinfo
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPipeReader.php
More file actions
255 lines (225 loc) · 5.91 KB
/
Copy pathPipeReader.php
File metadata and controls
255 lines (225 loc) · 5.91 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
<?php
namespace dariusiii\rarinfo;
/**
* PipeReader class.
*
* A very crude utility class for handling the piped output of an external command,
* with basic methods for reading and seeking in the stream.
*
* Technical note: this class uses popen() instead of proc_open() because the latter
* chokes on Windows if the pipe buffers fill due to PHP bugs #51800 & #60120 (pipes
* can't be set to non-blocking). This means we don't have easy access to a STDERR pipe,
* so the calling application will need to handle that itself by writing errors to a
* file (add '2> errors.txt' to the command) or by combining with STDOUT ('2>&1').
* Then the application must either read the file or parse the output for errors.
*
* @author Hecks
* @copyright (c) 2010-2013 Hecks
* @license Modified BSD
* @version 1.3
*/
class PipeReader
{
/**
* The last error message.
* @var string
*/
public $error = '';
/**
* The last process exit code.
* @var integer
*/
public $exitCode = 0;
/**
* Default constructor for opening a pipe.
*
* @param string $command the command to execute
* @return void
*/
public function __construct(?string $command = null)
{
if ($command) {
$this->open($command);
}
}
/**
* Opens a pipe to stream the output of a given command.
*
* Note that it's the responsibility of the calling application to sanitize
* the command with e.g. escapeshellcmd(), escapeshellarg(), etc.
*
* @param string $command the command to execute
* @return boolean false if the pipe could not be openend
*/
public function open($command): bool
{
$this->reset();
if (!($handle = popen($command, 'rb'))) {
$this->error = "Could not execute command: ($command)";
return false;
}
$this->handle = $handle;
$this->command = $command;
return true;
}
/**
* Closes any open pipe handle and sets the exit code.
*
* @return void
*/
public function close(): void
{
if (is_resource($this->handle)) {
$this->exitCode = pclose($this->handle);
}
$this->handle = null;
}
/**
* Reads the given number of bytes from the piped command output and moves the
* offset pointer forward, with optional confirmation that the requested bytes
* are available.
*
* @param integer $num number of bytes to read
* @param boolean $confirm check available bytes?
* @return string the byte string
* @throws \InvalidArgumentException
*/
public function read($num, $confirm = true): string
{
if ($num < 1) {
throw new \InvalidArgumentException("Could not read {$num} bytes from offset {$this->offset}");
}
if ($num === 0) {
return '';
}
// Read the requested bytes
if ($this->command && is_resource($this->handle)) {
$read = ''; $rlen = $num;
while ($rlen > 0 && !feof($this->handle)) {
$data = fread($this->handle, min($this->maxReadBytes, $rlen));
$rlen -= strlen($data);
$read .= $data;
}
}
// Confirm the read length?
if ($confirm && (!isset($read) || strlen($read) < $num)) {
$this->offset = $this->tell();
throw new \InvalidArgumentException("Could not read {$num} bytes from offset {$this->offset}");
}
// Move the data pointer
$this->offset = $this->tell();
return $read ?? '';
}
/**
* Convenience method for reading the remaining bytes from the piped output.
*
* @return string the remaining output data
* @throws \InvalidArgumentException
*/
public function readAll(): string
{
$data = '';
while ($read = $this->read($this->maxReadBytes, false)) {
$data .= $read;
}
return $data;
}
/**
* Convenience method for reading a single line, with the line ending included
* in the output.
*
* @return string|boolean the next output line, or false if none available
*/
public function readLine()
{
if (!$this->command || !is_resource($this->handle) || feof($this->handle)) {
return false;
}
$line = fgets($this->handle, $this->maxReadBytes);
$this->offset = $this->tell();
return $line;
}
/**
* Moves the current offset pointer to a position in the piped command output.
*
* Since only seeking ahead in the pipe is possible - and only by reading the
* output stream - seeking to an earlier offset necessarily means invoking the
* command again, so care must be taken that any commands are idempotent.
*
* @param integer $pos new pointer position
* @return void
* @throws \InvalidArgumentException
*/
public function seek($pos): void
{
if ($pos < 0) {
throw new \InvalidArgumentException("Could not seek to offset: {$pos}");
}
if ($this->command) {
if ($pos < $this->offset) {
$this->open($this->command);
}
if ($pos > $this->offset && $pos > 0) {
$this->read($pos - $this->offset);
}
}
$this->offset = $this->tell();
}
/**
* Provides the absolute position within the current piped output.
*
* @return integer the absolute position
*/
public function tell(): int
{
if ($this->command && is_resource($this->handle)) {
return ftell($this->handle);
}
return $this->offset;
}
/**
* Sets the maximum number of bytes to read in one operation.
*
* @param integer $bytes the max bytes to read
* @return void
*/
public function setMaxReadBytes($bytes): void
{
if (is_int($bytes) && $bytes > 0) {
$this->maxReadBytes = $bytes;
}
}
/**
* The command string to be executed for piped output.
* @var string
*/
protected $command = '';
/**
* Stream handle for the current pipe.
* @var resource
*/
protected $handle;
/**
* The maximum number of bytes to read in one operation.
* @var integer
*/
protected $maxReadBytes = 1048576;
/**
* The current position in the piped output.
* @var integer
*/
protected $offset = 0;
/**
* Resets the instance variables.
*
* @return void
*/
protected function reset(): void
{
$this->close();
$this->error = '';
$this->exitCode = 0;
$this->command = '';
$this->offset = 0;
}
} // End PipeReader class