1 | <?php |
||
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
2 | /** |
||
3 | * This file is part of the ZBateson\StreamDecorators project. |
||
4 | * |
||
5 | * @license http://opensource.org/licenses/bsd-license.php BSD |
||
0 ignored issues
–
show
|
|||
6 | */ |
||
0 ignored issues
–
show
|
|||
7 | namespace ZBateson\StreamDecorators; |
||
8 | |||
9 | use Psr\Http\Message\StreamInterface; |
||
10 | use GuzzleHttp\Psr7\StreamDecoratorTrait; |
||
11 | use RuntimeException; |
||
12 | |||
13 | /** |
||
14 | * GuzzleHttp\Psr7 stream decoder decorator for quoted printable streams. |
||
15 | * |
||
16 | * @author Zaahid Bateson |
||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||
17 | */ |
||
0 ignored issues
–
show
|
|||
18 | class QuotedPrintableStream implements StreamInterface |
||
19 | { |
||
0 ignored issues
–
show
|
|||
20 | use StreamDecoratorTrait; |
||
21 | |||
22 | /** |
||
0 ignored issues
–
show
|
|||
23 | * @var int current read/write position |
||
24 | */ |
||
25 | private $position = 0; |
||
0 ignored issues
–
show
|
|||
26 | |||
27 | /** |
||
0 ignored issues
–
show
|
|||
28 | * @var string Last line of written text (used to maintain good line-breaks) |
||
29 | */ |
||
30 | private $lastLine = ''; |
||
0 ignored issues
–
show
|
|||
31 | |||
32 | /** |
||
33 | * Overridden to return the position in the target encoding. |
||
34 | * |
||
35 | * @return int |
||
0 ignored issues
–
show
|
|||
36 | */ |
||
37 | 3 | public function tell() |
|
0 ignored issues
–
show
|
|||
38 | { |
||
0 ignored issues
–
show
|
|||
39 | 3 | return $this->position; |
|
40 | } |
||
0 ignored issues
–
show
|
|||
41 | |||
42 | /** |
||
43 | * Returns null, getSize isn't supported |
||
44 | * |
||
45 | * @return null |
||
46 | */ |
||
47 | 1 | public function getSize() |
|
48 | { |
||
0 ignored issues
–
show
|
|||
49 | 1 | return null; |
|
0 ignored issues
–
show
|
|||
50 | } |
||
0 ignored issues
–
show
|
|||
51 | |||
52 | /** |
||
53 | * Not supported. |
||
54 | * |
||
55 | * @param int $offset |
||
0 ignored issues
–
show
|
|||
56 | * @param int $whence |
||
0 ignored issues
–
show
|
|||
57 | * @throws RuntimeException |
||
0 ignored issues
–
show
|
|||
58 | */ |
||
0 ignored issues
–
show
|
|||
59 | 1 | public function seek($offset, $whence = SEEK_SET) |
|
0 ignored issues
–
show
|
|||
60 | { |
||
0 ignored issues
–
show
|
|||
61 | 1 | throw new RuntimeException('Cannot seek a QuotedPrintableStream'); |
|
62 | } |
||
0 ignored issues
–
show
|
|||
63 | |||
64 | /** |
||
65 | * Overridden to return false |
||
66 | * |
||
67 | * @return boolean |
||
68 | */ |
||
69 | 1 | public function isSeekable() |
|
70 | { |
||
0 ignored issues
–
show
|
|||
71 | 1 | return false; |
|
0 ignored issues
–
show
|
|||
72 | } |
||
0 ignored issues
–
show
|
|||
73 | |||
74 | /** |
||
75 | * Reads $length chars from the underlying stream, prepending the past $pre |
||
76 | * to it first. |
||
77 | * |
||
78 | * If the characters read (including the prepended $pre) contain invalid |
||
79 | * quoted-printable characters, the underlying stream is rewound by the |
||
80 | * total number of characters ($length + strlen($pre)). |
||
81 | * |
||
82 | * The quoted-printable encoded characters are returned. If the characters |
||
83 | * read are invalid, '3D' is returned indicating an '=' character. |
||
84 | * |
||
85 | * @param int $length |
||
0 ignored issues
–
show
|
|||
86 | * @param string $pre |
||
0 ignored issues
–
show
|
|||
87 | * @return string |
||
0 ignored issues
–
show
|
|||
88 | */ |
||
89 | 5 | private function readEncodedChars($length, $pre = '') |
|
0 ignored issues
–
show
|
|||
90 | { |
||
0 ignored issues
–
show
|
|||
91 | 5 | $str = $pre . $this->stream->read($length); |
|
0 ignored issues
–
show
|
|||
92 | 5 | $len = strlen($str); |
|
93 | 5 | if ($len > 0 && !preg_match('/^[0-9a-f]{2}$|^[\r\n].$/is', $str) && $this->stream->isSeekable()) { |
|
0 ignored issues
–
show
|
|||
94 | 1 | $this->stream->seek(-$len, SEEK_CUR); |
|
95 | 1 | return '3D'; // '=' character |
|
0 ignored issues
–
show
|
|||
96 | } |
||
97 | 5 | return $str; |
|
98 | } |
||
0 ignored issues
–
show
|
|||
99 | |||
100 | /** |
||
101 | * Decodes the passed $block of text. |
||
102 | * |
||
103 | * If the last or before last character is an '=' char, indicating the |
||
104 | * beginning of a quoted-printable encoded char, 1 or 2 additional bytes are |
||
105 | * read from the underlying stream respectively. |
||
106 | * |
||
107 | * The decoded string is returned. |
||
108 | * |
||
109 | * @param string $block |
||
0 ignored issues
–
show
|
|||
110 | * @return string |
||
0 ignored issues
–
show
|
|||
111 | */ |
||
112 | 8 | private function decodeBlock($block) |
|
0 ignored issues
–
show
|
|||
113 | { |
||
0 ignored issues
–
show
|
|||
114 | 8 | if (substr($block, -1) === '=') { |
|
115 | 5 | $block .= $this->readEncodedChars(2); |
|
116 | 8 | } elseif (substr($block, -2, 1) === '=') { |
|
0 ignored issues
–
show
|
|||
117 | 4 | $first = substr($block, -1); |
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
118 | 4 | $block = substr($block, 0, -1); |
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
119 | 4 | $block .= $this->readEncodedChars(1, $first); |
|
120 | } |
||
121 | 8 | return quoted_printable_decode($block); |
|
122 | } |
||
0 ignored issues
–
show
|
|||
123 | |||
124 | /** |
||
0 ignored issues
–
show
|
|||
125 | * Reads up to $length characters, appends them to the passed $str string, |
||
126 | * and returns the total number of characters read. |
||
127 | * |
||
128 | * -1 is returned if there are no more bytes to read. |
||
129 | * |
||
130 | * @param int $length |
||
0 ignored issues
–
show
|
|||
131 | * @param string $append |
||
0 ignored issues
–
show
|
|||
132 | * @return int |
||
0 ignored issues
–
show
|
|||
133 | */ |
||
134 | 8 | private function readRawDecodeAndAppend($length, &$str) |
|
0 ignored issues
–
show
|
|||
135 | { |
||
0 ignored issues
–
show
|
|||
136 | 8 | $block = $this->stream->read($length); |
|
137 | 8 | if ($block === false || $block === '') { |
|
0 ignored issues
–
show
|
|||
138 | 8 | return -1; |
|
139 | } |
||
140 | 8 | $decoded = $this->decodeBlock($block); |
|
141 | 8 | $count = strlen($decoded); |
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
142 | 8 | $str .= $decoded; |
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
143 | 8 | return $count; |
|
144 | } |
||
0 ignored issues
–
show
|
|||
145 | |||
146 | /** |
||
147 | * Reads up to $length decoded bytes from the underlying quoted-printable |
||
148 | * encoded stream and returns them. |
||
149 | * |
||
150 | * @param int $length |
||
0 ignored issues
–
show
|
|||
151 | * @return string |
||
0 ignored issues
–
show
|
|||
152 | */ |
||
153 | 8 | public function read($length) |
|
0 ignored issues
–
show
|
|||
154 | { |
||
0 ignored issues
–
show
|
|||
155 | // let Guzzle decide what to do. |
||
0 ignored issues
–
show
|
|||
156 | 8 | if ($length <= 0 || $this->eof()) { |
|
157 | return $this->stream->read($length); |
||
158 | } |
||
159 | 8 | $count = 0; |
|
160 | 8 | $bytes = ''; |
|
161 | 8 | while ($count < $length) { |
|
162 | 8 | $nRead = $this->readRawDecodeAndAppend($length - $count, $bytes); |
|
0 ignored issues
–
show
|
|||
163 | 8 | if ($nRead === -1) { |
|
164 | 8 | break; |
|
165 | } |
||
166 | 8 | $this->position += $nRead; |
|
167 | 8 | $count += $nRead; |
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
168 | } |
||
169 | 8 | return $bytes; |
|
170 | } |
||
0 ignored issues
–
show
|
|||
171 | |||
172 | /** |
||
173 | * Writes the passed string to the underlying stream after encoding it as |
||
174 | * quoted-printable. |
||
175 | * |
||
176 | * Note that reading and writing to the same stream without rewinding is not |
||
177 | * supported. |
||
178 | * |
||
179 | * @param string $string |
||
0 ignored issues
–
show
|
|||
180 | * @return int the number of bytes written |
||
0 ignored issues
–
show
|
|||
181 | */ |
||
182 | 1 | public function write($string) |
|
0 ignored issues
–
show
|
|||
183 | { |
||
0 ignored issues
–
show
|
|||
184 | 1 | $encodedLine = quoted_printable_encode($this->lastLine); |
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
185 | 1 | $lineAndString = rtrim(quoted_printable_encode($this->lastLine . $string), "\r\n"); |
|
0 ignored issues
–
show
|
|||
186 | 1 | $write = substr($lineAndString, strlen($encodedLine)); |
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
187 | 1 | $this->stream->write($write); |
|
188 | 1 | $written = strlen($string); |
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
189 | 1 | $this->position += $written; |
|
190 | |||
191 | 1 | $lpos = strrpos($lineAndString, "\n"); |
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
192 | 1 | $lastLine = $lineAndString; |
|
193 | 1 | if ($lpos !== false) { |
|
0 ignored issues
–
show
|
|||
194 | 1 | $lastLine = substr($lineAndString, $lpos + 1); |
|
0 ignored issues
–
show
|
|||
195 | } |
||
196 | 1 | $this->lastLine = quoted_printable_decode($lastLine); |
|
197 | 1 | return $written; |
|
198 | } |
||
0 ignored issues
–
show
|
|||
199 | |||
200 | /** |
||
201 | * Writes out a final CRLF if the current line isn't empty. |
||
202 | */ |
||
0 ignored issues
–
show
|
|||
203 | 1 | private function beforeClose() |
|
0 ignored issues
–
show
|
|||
204 | { |
||
0 ignored issues
–
show
|
|||
205 | 1 | if ($this->isWritable() && $this->lastLine !== '') { |
|
206 | 1 | $this->stream->write("\r\n"); |
|
207 | 1 | $this->lastLine = ''; |
|
208 | } |
||
209 | 1 | } |
|
0 ignored issues
–
show
|
|||
210 | |||
211 | /** |
||
212 | * Closes the underlying stream and writes a final CRLF if the current line |
||
213 | * isn't empty. |
||
214 | */ |
||
0 ignored issues
–
show
|
|||
215 | 1 | public function close() |
|
216 | { |
||
0 ignored issues
–
show
|
|||
217 | 1 | $this->beforeClose(); |
|
218 | 1 | $this->stream->close(); |
|
219 | 1 | } |
|
0 ignored issues
–
show
|
|||
220 | |||
221 | /** |
||
222 | * Closes the underlying stream and writes a final CRLF if the current line |
||
223 | * isn't empty. |
||
224 | */ |
||
0 ignored issues
–
show
|
|||
225 | public function detach() |
||
226 | { |
||
0 ignored issues
–
show
|
|||
227 | $this->beforeClose(); |
||
228 | $this->stream->detach(); |
||
229 | } |
||
0 ignored issues
–
show
|
|||
230 | } |
||
0 ignored issues
–
show
|
|||
231 |