@@ -53,579 +53,579 @@ |
||
53 | 53 | */ |
54 | 54 | class HTTP_Request2_Response |
55 | 55 | { |
56 | - /** |
|
57 | - * HTTP protocol version (e.g. 1.0, 1.1) |
|
58 | - * @var string |
|
59 | - */ |
|
60 | - protected $version; |
|
61 | - |
|
62 | - /** |
|
63 | - * Status code |
|
64 | - * @var integer |
|
65 | - * @link http://tools.ietf.org/html/rfc2616#section-6.1.1 |
|
66 | - */ |
|
67 | - protected $code; |
|
68 | - |
|
69 | - /** |
|
70 | - * Reason phrase |
|
71 | - * @var string |
|
72 | - * @link http://tools.ietf.org/html/rfc2616#section-6.1.1 |
|
73 | - */ |
|
74 | - protected $reasonPhrase; |
|
75 | - |
|
76 | - /** |
|
77 | - * Effective URL (may be different from original request URL in case of redirects) |
|
78 | - * @var string |
|
79 | - */ |
|
80 | - protected $effectiveUrl; |
|
81 | - |
|
82 | - /** |
|
83 | - * Associative array of response headers |
|
84 | - * @var array |
|
85 | - */ |
|
86 | - protected $headers = array(); |
|
87 | - |
|
88 | - /** |
|
89 | - * Cookies set in the response |
|
90 | - * @var array |
|
91 | - */ |
|
92 | - protected $cookies = array(); |
|
93 | - |
|
94 | - /** |
|
95 | - * Name of last header processed by parseHederLine() |
|
96 | - * |
|
97 | - * Used to handle the headers that span multiple lines |
|
98 | - * |
|
99 | - * @var string |
|
100 | - */ |
|
101 | - protected $lastHeader = null; |
|
102 | - |
|
103 | - /** |
|
104 | - * Response body |
|
105 | - * @var string |
|
106 | - */ |
|
107 | - protected $body = ''; |
|
108 | - |
|
109 | - /** |
|
110 | - * Whether the body is still encoded by Content-Encoding |
|
111 | - * |
|
112 | - * cURL provides the decoded body to the callback; if we are reading from |
|
113 | - * socket the body is still gzipped / deflated |
|
114 | - * |
|
115 | - * @var bool |
|
116 | - */ |
|
117 | - protected $bodyEncoded; |
|
118 | - |
|
119 | - /** |
|
120 | - * Associative array of HTTP status code / reason phrase. |
|
121 | - * |
|
122 | - * @var array |
|
123 | - * @link http://tools.ietf.org/html/rfc2616#section-10 |
|
124 | - */ |
|
125 | - protected static $phrases = array( |
|
126 | - |
|
127 | - // 1xx: Informational - Request received, continuing process |
|
128 | - 100 => 'Continue', |
|
129 | - 101 => 'Switching Protocols', |
|
130 | - |
|
131 | - // 2xx: Success - The action was successfully received, understood and |
|
132 | - // accepted |
|
133 | - 200 => 'OK', |
|
134 | - 201 => 'Created', |
|
135 | - 202 => 'Accepted', |
|
136 | - 203 => 'Non-Authoritative Information', |
|
137 | - 204 => 'No Content', |
|
138 | - 205 => 'Reset Content', |
|
139 | - 206 => 'Partial Content', |
|
140 | - |
|
141 | - // 3xx: Redirection - Further action must be taken in order to complete |
|
142 | - // the request |
|
143 | - 300 => 'Multiple Choices', |
|
144 | - 301 => 'Moved Permanently', |
|
145 | - 302 => 'Found', // 1.1 |
|
146 | - 303 => 'See Other', |
|
147 | - 304 => 'Not Modified', |
|
148 | - 305 => 'Use Proxy', |
|
149 | - 307 => 'Temporary Redirect', |
|
150 | - |
|
151 | - // 4xx: Client Error - The request contains bad syntax or cannot be |
|
152 | - // fulfilled |
|
153 | - 400 => 'Bad Request', |
|
154 | - 401 => 'Unauthorized', |
|
155 | - 402 => 'Payment Required', |
|
156 | - 403 => 'Forbidden', |
|
157 | - 404 => 'Not Found', |
|
158 | - 405 => 'Method Not Allowed', |
|
159 | - 406 => 'Not Acceptable', |
|
160 | - 407 => 'Proxy Authentication Required', |
|
161 | - 408 => 'Request Timeout', |
|
162 | - 409 => 'Conflict', |
|
163 | - 410 => 'Gone', |
|
164 | - 411 => 'Length Required', |
|
165 | - 412 => 'Precondition Failed', |
|
166 | - 413 => 'Request Entity Too Large', |
|
167 | - 414 => 'Request-URI Too Long', |
|
168 | - 415 => 'Unsupported Media Type', |
|
169 | - 416 => 'Requested Range Not Satisfiable', |
|
170 | - 417 => 'Expectation Failed', |
|
171 | - |
|
172 | - // 5xx: Server Error - The server failed to fulfill an apparently |
|
173 | - // valid request |
|
174 | - 500 => 'Internal Server Error', |
|
175 | - 501 => 'Not Implemented', |
|
176 | - 502 => 'Bad Gateway', |
|
177 | - 503 => 'Service Unavailable', |
|
178 | - 504 => 'Gateway Timeout', |
|
179 | - 505 => 'HTTP Version Not Supported', |
|
180 | - 509 => 'Bandwidth Limit Exceeded', |
|
181 | - |
|
182 | - ); |
|
183 | - |
|
184 | - /** |
|
185 | - * Returns the default reason phrase for the given code or all reason phrases |
|
186 | - * |
|
187 | - * @param int $code Response code |
|
188 | - * |
|
189 | - * @return string|array|null Default reason phrase for $code if $code is given |
|
190 | - * (null if no phrase is available), array of all |
|
191 | - * reason phrases if $code is null |
|
192 | - * @link http://pear.php.net/bugs/18716 |
|
193 | - */ |
|
194 | - public static function getDefaultReasonPhrase($code = null) |
|
195 | - { |
|
196 | - if (null === $code) { |
|
197 | - return self::$phrases; |
|
198 | - } else { |
|
199 | - return isset(self::$phrases[$code]) ? self::$phrases[$code] : null; |
|
200 | - } |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * Constructor, parses the response status line |
|
205 | - * |
|
206 | - * @param string $statusLine Response status line (e.g. "HTTP/1.1 200 OK") |
|
207 | - * @param bool $bodyEncoded Whether body is still encoded by Content-Encoding |
|
208 | - * @param string $effectiveUrl Effective URL of the response |
|
209 | - * |
|
210 | - * @throws HTTP_Request2_MessageException if status line is invalid according to spec |
|
211 | - */ |
|
212 | - public function __construct($statusLine, $bodyEncoded = true, $effectiveUrl = null) |
|
213 | - { |
|
214 | - if (!preg_match('!^HTTP/(\d\.\d) (\d{3})(?: (.+))?!', $statusLine, $m)) { |
|
215 | - throw new HTTP_Request2_MessageException( |
|
216 | - "Malformed response: {$statusLine}", |
|
217 | - HTTP_Request2_Exception::MALFORMED_RESPONSE |
|
218 | - ); |
|
219 | - } |
|
220 | - $this->version = $m[1]; |
|
221 | - $this->code = intval($m[2]); |
|
222 | - $this->reasonPhrase = !empty($m[3]) ? trim($m[3]) : self::getDefaultReasonPhrase($this->code); |
|
223 | - $this->bodyEncoded = (bool)$bodyEncoded; |
|
224 | - $this->effectiveUrl = (string)$effectiveUrl; |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * Parses the line from HTTP response filling $headers array |
|
229 | - * |
|
230 | - * The method should be called after reading the line from socket or receiving |
|
231 | - * it into cURL callback. Passing an empty string here indicates the end of |
|
232 | - * response headers and triggers additional processing, so be sure to pass an |
|
233 | - * empty string in the end. |
|
234 | - * |
|
235 | - * @param string $headerLine Line from HTTP response |
|
236 | - */ |
|
237 | - public function parseHeaderLine($headerLine) |
|
238 | - { |
|
239 | - $headerLine = trim($headerLine, "\r\n"); |
|
240 | - |
|
241 | - if ('' == $headerLine) { |
|
242 | - // empty string signals the end of headers, process the received ones |
|
243 | - if (!empty($this->headers['set-cookie'])) { |
|
244 | - $cookies = is_array($this->headers['set-cookie'])? |
|
245 | - $this->headers['set-cookie']: |
|
246 | - array($this->headers['set-cookie']); |
|
247 | - foreach ($cookies as $cookieString) { |
|
248 | - $this->parseCookie($cookieString); |
|
249 | - } |
|
250 | - unset($this->headers['set-cookie']); |
|
251 | - } |
|
252 | - foreach (array_keys($this->headers) as $k) { |
|
253 | - if (is_array($this->headers[$k])) { |
|
254 | - $this->headers[$k] = implode(', ', $this->headers[$k]); |
|
255 | - } |
|
256 | - } |
|
257 | - |
|
258 | - } elseif (preg_match('!^([^\x00-\x1f\x7f-\xff()<>@,;:\\\\"/\[\]?={}\s]+):(.+)$!', $headerLine, $m)) { |
|
259 | - // string of the form header-name: header value |
|
260 | - $name = strtolower($m[1]); |
|
261 | - $value = trim($m[2]); |
|
262 | - if (empty($this->headers[$name])) { |
|
263 | - $this->headers[$name] = $value; |
|
264 | - } else { |
|
265 | - if (!is_array($this->headers[$name])) { |
|
266 | - $this->headers[$name] = array($this->headers[$name]); |
|
267 | - } |
|
268 | - $this->headers[$name][] = $value; |
|
269 | - } |
|
270 | - $this->lastHeader = $name; |
|
271 | - |
|
272 | - } elseif (preg_match('!^\s+(.+)$!', $headerLine, $m) && $this->lastHeader) { |
|
273 | - // continuation of a previous header |
|
274 | - if (!is_array($this->headers[$this->lastHeader])) { |
|
275 | - $this->headers[$this->lastHeader] .= ' ' . trim($m[1]); |
|
276 | - } else { |
|
277 | - $key = count($this->headers[$this->lastHeader]) - 1; |
|
278 | - $this->headers[$this->lastHeader][$key] .= ' ' . trim($m[1]); |
|
279 | - } |
|
280 | - } |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * Parses a Set-Cookie header to fill $cookies array |
|
285 | - * |
|
286 | - * @param string $cookieString value of Set-Cookie header |
|
287 | - * |
|
288 | - * @link http://web.archive.org/web/20080331104521/http://cgi.netscape.com/newsref/std/cookie_spec.html |
|
289 | - */ |
|
290 | - protected function parseCookie($cookieString) |
|
291 | - { |
|
292 | - $cookie = array( |
|
293 | - 'expires' => null, |
|
294 | - 'domain' => null, |
|
295 | - 'path' => null, |
|
296 | - 'secure' => false |
|
297 | - ); |
|
298 | - |
|
299 | - if (!strpos($cookieString, ';')) { |
|
300 | - // Only a name=value pair |
|
301 | - $pos = strpos($cookieString, '='); |
|
302 | - $cookie['name'] = trim(substr($cookieString, 0, $pos)); |
|
303 | - $cookie['value'] = trim(substr($cookieString, $pos + 1)); |
|
304 | - |
|
305 | - } else { |
|
306 | - // Some optional parameters are supplied |
|
307 | - $elements = explode(';', $cookieString); |
|
308 | - $pos = strpos($elements[0], '='); |
|
309 | - $cookie['name'] = trim(substr($elements[0], 0, $pos)); |
|
310 | - $cookie['value'] = trim(substr($elements[0], $pos + 1)); |
|
311 | - |
|
312 | - for ($i = 1; $i < count($elements); $i++) { |
|
313 | - if (false === strpos($elements[$i], '=')) { |
|
314 | - $elName = trim($elements[$i]); |
|
315 | - $elValue = null; |
|
316 | - } else { |
|
317 | - list ($elName, $elValue) = array_map('trim', explode('=', $elements[$i])); |
|
318 | - } |
|
319 | - $elName = strtolower($elName); |
|
320 | - if ('secure' == $elName) { |
|
321 | - $cookie['secure'] = true; |
|
322 | - } elseif ('expires' == $elName) { |
|
323 | - $cookie['expires'] = str_replace('"', '', $elValue); |
|
324 | - } elseif ('path' == $elName || 'domain' == $elName) { |
|
325 | - $cookie[$elName] = urldecode($elValue); |
|
326 | - } else { |
|
327 | - $cookie[$elName] = $elValue; |
|
328 | - } |
|
329 | - } |
|
330 | - } |
|
331 | - $this->cookies[] = $cookie; |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * Appends a string to the response body |
|
336 | - * |
|
337 | - * @param string $bodyChunk part of response body |
|
338 | - */ |
|
339 | - public function appendBody($bodyChunk) |
|
340 | - { |
|
341 | - $this->body .= $bodyChunk; |
|
342 | - } |
|
343 | - |
|
344 | - /** |
|
345 | - * Returns the effective URL of the response |
|
346 | - * |
|
347 | - * This may be different from the request URL if redirects were followed. |
|
348 | - * |
|
349 | - * @return string |
|
350 | - * @link http://pear.php.net/bugs/bug.php?id=18412 |
|
351 | - */ |
|
352 | - public function getEffectiveUrl() |
|
353 | - { |
|
354 | - return $this->effectiveUrl; |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * Returns the status code |
|
359 | - * |
|
360 | - * @return integer |
|
361 | - */ |
|
362 | - public function getStatus() |
|
363 | - { |
|
364 | - return $this->code; |
|
365 | - } |
|
366 | - |
|
367 | - /** |
|
368 | - * Returns the reason phrase |
|
369 | - * |
|
370 | - * @return string |
|
371 | - */ |
|
372 | - public function getReasonPhrase() |
|
373 | - { |
|
374 | - return $this->reasonPhrase; |
|
375 | - } |
|
376 | - |
|
377 | - /** |
|
378 | - * Whether response is a redirect that can be automatically handled by HTTP_Request2 |
|
379 | - * |
|
380 | - * @return bool |
|
381 | - */ |
|
382 | - public function isRedirect() |
|
383 | - { |
|
384 | - return in_array($this->code, array(300, 301, 302, 303, 307)) |
|
385 | - && isset($this->headers['location']); |
|
386 | - } |
|
387 | - |
|
388 | - /** |
|
389 | - * Returns either the named header or all response headers |
|
390 | - * |
|
391 | - * @param string $headerName Name of header to return |
|
392 | - * |
|
393 | - * @return string|array Value of $headerName header (null if header is |
|
394 | - * not present), array of all response headers if |
|
395 | - * $headerName is null |
|
396 | - */ |
|
397 | - public function getHeader($headerName = null) |
|
398 | - { |
|
399 | - if (null === $headerName) { |
|
400 | - return $this->headers; |
|
401 | - } else { |
|
402 | - $headerName = strtolower($headerName); |
|
403 | - return isset($this->headers[$headerName])? $this->headers[$headerName]: null; |
|
404 | - } |
|
405 | - } |
|
406 | - |
|
407 | - /** |
|
408 | - * Returns cookies set in response |
|
409 | - * |
|
410 | - * @return array |
|
411 | - */ |
|
412 | - public function getCookies() |
|
413 | - { |
|
414 | - return $this->cookies; |
|
415 | - } |
|
416 | - |
|
417 | - /** |
|
418 | - * Returns the body of the response |
|
419 | - * |
|
420 | - * @return string |
|
421 | - * @throws HTTP_Request2_Exception if body cannot be decoded |
|
422 | - */ |
|
423 | - public function getBody() |
|
424 | - { |
|
425 | - if (0 == strlen($this->body) || !$this->bodyEncoded |
|
426 | - || !in_array(strtolower($this->getHeader('content-encoding')), array('gzip', 'deflate')) |
|
427 | - ) { |
|
428 | - return $this->body; |
|
429 | - |
|
430 | - } else { |
|
431 | - if (extension_loaded('mbstring') && (2 & ini_get('mbstring.func_overload'))) { |
|
432 | - $oldEncoding = mb_internal_encoding(); |
|
433 | - mb_internal_encoding('8bit'); |
|
434 | - } |
|
435 | - |
|
436 | - try { |
|
437 | - switch (strtolower($this->getHeader('content-encoding'))) { |
|
438 | - case 'gzip': |
|
439 | - $decoded = self::decodeGzip($this->body); |
|
440 | - break; |
|
441 | - case 'deflate': |
|
442 | - $decoded = self::decodeDeflate($this->body); |
|
443 | - } |
|
444 | - } catch (Exception $e) { |
|
445 | - } |
|
446 | - |
|
447 | - if (!empty($oldEncoding)) { |
|
448 | - mb_internal_encoding($oldEncoding); |
|
449 | - } |
|
450 | - if (!empty($e)) { |
|
451 | - throw $e; |
|
452 | - } |
|
453 | - return $decoded; |
|
454 | - } |
|
455 | - } |
|
456 | - |
|
457 | - /** |
|
458 | - * Get the HTTP version of the response |
|
459 | - * |
|
460 | - * @return string |
|
461 | - */ |
|
462 | - public function getVersion() |
|
463 | - { |
|
464 | - return $this->version; |
|
465 | - } |
|
466 | - |
|
467 | - /** |
|
468 | - * Decodes the message-body encoded by gzip |
|
469 | - * |
|
470 | - * The real decoding work is done by gzinflate() built-in function, this |
|
471 | - * method only parses the header and checks data for compliance with |
|
472 | - * RFC 1952 |
|
473 | - * |
|
474 | - * @param string $data gzip-encoded data |
|
475 | - * |
|
476 | - * @return string decoded data |
|
477 | - * @throws HTTP_Request2_LogicException |
|
478 | - * @throws HTTP_Request2_MessageException |
|
479 | - * @link http://tools.ietf.org/html/rfc1952 |
|
480 | - */ |
|
481 | - public static function decodeGzip($data) |
|
482 | - { |
|
483 | - $length = strlen($data); |
|
484 | - // If it doesn't look like gzip-encoded data, don't bother |
|
485 | - if (18 > $length || strcmp(substr($data, 0, 2), "\x1f\x8b")) { |
|
486 | - return $data; |
|
487 | - } |
|
488 | - if (!function_exists('gzinflate')) { |
|
489 | - throw new HTTP_Request2_LogicException( |
|
490 | - 'Unable to decode body: gzip extension not available', |
|
491 | - HTTP_Request2_Exception::MISCONFIGURATION |
|
492 | - ); |
|
493 | - } |
|
494 | - $method = ord(substr($data, 2, 1)); |
|
495 | - if (8 != $method) { |
|
496 | - throw new HTTP_Request2_MessageException( |
|
497 | - 'Error parsing gzip header: unknown compression method', |
|
498 | - HTTP_Request2_Exception::DECODE_ERROR |
|
499 | - ); |
|
500 | - } |
|
501 | - $flags = ord(substr($data, 3, 1)); |
|
502 | - if ($flags & 224) { |
|
503 | - throw new HTTP_Request2_MessageException( |
|
504 | - 'Error parsing gzip header: reserved bits are set', |
|
505 | - HTTP_Request2_Exception::DECODE_ERROR |
|
506 | - ); |
|
507 | - } |
|
508 | - |
|
509 | - // header is 10 bytes minimum. may be longer, though. |
|
510 | - $headerLength = 10; |
|
511 | - // extra fields, need to skip 'em |
|
512 | - if ($flags & 4) { |
|
513 | - if ($length - $headerLength - 2 < 8) { |
|
514 | - throw new HTTP_Request2_MessageException( |
|
515 | - 'Error parsing gzip header: data too short', |
|
516 | - HTTP_Request2_Exception::DECODE_ERROR |
|
517 | - ); |
|
518 | - } |
|
519 | - $extraLength = unpack('v', substr($data, 10, 2)); |
|
520 | - if ($length - $headerLength - 2 - $extraLength[1] < 8) { |
|
521 | - throw new HTTP_Request2_MessageException( |
|
522 | - 'Error parsing gzip header: data too short', |
|
523 | - HTTP_Request2_Exception::DECODE_ERROR |
|
524 | - ); |
|
525 | - } |
|
526 | - $headerLength += $extraLength[1] + 2; |
|
527 | - } |
|
528 | - // file name, need to skip that |
|
529 | - if ($flags & 8) { |
|
530 | - if ($length - $headerLength - 1 < 8) { |
|
531 | - throw new HTTP_Request2_MessageException( |
|
532 | - 'Error parsing gzip header: data too short', |
|
533 | - HTTP_Request2_Exception::DECODE_ERROR |
|
534 | - ); |
|
535 | - } |
|
536 | - $filenameLength = strpos(substr($data, $headerLength), chr(0)); |
|
537 | - if (false === $filenameLength || $length - $headerLength - $filenameLength - 1 < 8) { |
|
538 | - throw new HTTP_Request2_MessageException( |
|
539 | - 'Error parsing gzip header: data too short', |
|
540 | - HTTP_Request2_Exception::DECODE_ERROR |
|
541 | - ); |
|
542 | - } |
|
543 | - $headerLength += $filenameLength + 1; |
|
544 | - } |
|
545 | - // comment, need to skip that also |
|
546 | - if ($flags & 16) { |
|
547 | - if ($length - $headerLength - 1 < 8) { |
|
548 | - throw new HTTP_Request2_MessageException( |
|
549 | - 'Error parsing gzip header: data too short', |
|
550 | - HTTP_Request2_Exception::DECODE_ERROR |
|
551 | - ); |
|
552 | - } |
|
553 | - $commentLength = strpos(substr($data, $headerLength), chr(0)); |
|
554 | - if (false === $commentLength || $length - $headerLength - $commentLength - 1 < 8) { |
|
555 | - throw new HTTP_Request2_MessageException( |
|
556 | - 'Error parsing gzip header: data too short', |
|
557 | - HTTP_Request2_Exception::DECODE_ERROR |
|
558 | - ); |
|
559 | - } |
|
560 | - $headerLength += $commentLength + 1; |
|
561 | - } |
|
562 | - // have a CRC for header. let's check |
|
563 | - if ($flags & 2) { |
|
564 | - if ($length - $headerLength - 2 < 8) { |
|
565 | - throw new HTTP_Request2_MessageException( |
|
566 | - 'Error parsing gzip header: data too short', |
|
567 | - HTTP_Request2_Exception::DECODE_ERROR |
|
568 | - ); |
|
569 | - } |
|
570 | - $crcReal = 0xffff & crc32(substr($data, 0, $headerLength)); |
|
571 | - $crcStored = unpack('v', substr($data, $headerLength, 2)); |
|
572 | - if ($crcReal != $crcStored[1]) { |
|
573 | - throw new HTTP_Request2_MessageException( |
|
574 | - 'Header CRC check failed', |
|
575 | - HTTP_Request2_Exception::DECODE_ERROR |
|
576 | - ); |
|
577 | - } |
|
578 | - $headerLength += 2; |
|
579 | - } |
|
580 | - // unpacked data CRC and size at the end of encoded data |
|
581 | - $tmp = unpack('V2', substr($data, -8)); |
|
582 | - $dataCrc = $tmp[1]; |
|
583 | - $dataSize = $tmp[2]; |
|
584 | - |
|
585 | - // finally, call the gzinflate() function |
|
586 | - // don't pass $dataSize to gzinflate, see bugs #13135, #14370 |
|
587 | - $unpacked = gzinflate(substr($data, $headerLength, -8)); |
|
588 | - if (false === $unpacked) { |
|
589 | - throw new HTTP_Request2_MessageException( |
|
590 | - 'gzinflate() call failed', |
|
591 | - HTTP_Request2_Exception::DECODE_ERROR |
|
592 | - ); |
|
593 | - } elseif ($dataSize != strlen($unpacked)) { |
|
594 | - throw new HTTP_Request2_MessageException( |
|
595 | - 'Data size check failed', |
|
596 | - HTTP_Request2_Exception::DECODE_ERROR |
|
597 | - ); |
|
598 | - } elseif ((0xffffffff & $dataCrc) != (0xffffffff & crc32($unpacked))) { |
|
599 | - throw new HTTP_Request2_Exception( |
|
600 | - 'Data CRC check failed', |
|
601 | - HTTP_Request2_Exception::DECODE_ERROR |
|
602 | - ); |
|
603 | - } |
|
604 | - return $unpacked; |
|
605 | - } |
|
606 | - |
|
607 | - /** |
|
608 | - * Decodes the message-body encoded by deflate |
|
609 | - * |
|
610 | - * @param string $data deflate-encoded data |
|
611 | - * |
|
612 | - * @return string decoded data |
|
613 | - * @throws HTTP_Request2_LogicException |
|
614 | - */ |
|
615 | - public static function decodeDeflate($data) |
|
616 | - { |
|
617 | - if (!function_exists('gzuncompress')) { |
|
618 | - throw new HTTP_Request2_LogicException( |
|
619 | - 'Unable to decode body: gzip extension not available', |
|
620 | - HTTP_Request2_Exception::MISCONFIGURATION |
|
621 | - ); |
|
622 | - } |
|
623 | - // RFC 2616 defines 'deflate' encoding as zlib format from RFC 1950, |
|
624 | - // while many applications send raw deflate stream from RFC 1951. |
|
625 | - // We should check for presence of zlib header and use gzuncompress() or |
|
626 | - // gzinflate() as needed. See bug #15305 |
|
627 | - $header = unpack('n', substr($data, 0, 2)); |
|
628 | - return (0 == $header[1] % 31)? gzuncompress($data): gzinflate($data); |
|
629 | - } |
|
56 | + /** |
|
57 | + * HTTP protocol version (e.g. 1.0, 1.1) |
|
58 | + * @var string |
|
59 | + */ |
|
60 | + protected $version; |
|
61 | + |
|
62 | + /** |
|
63 | + * Status code |
|
64 | + * @var integer |
|
65 | + * @link http://tools.ietf.org/html/rfc2616#section-6.1.1 |
|
66 | + */ |
|
67 | + protected $code; |
|
68 | + |
|
69 | + /** |
|
70 | + * Reason phrase |
|
71 | + * @var string |
|
72 | + * @link http://tools.ietf.org/html/rfc2616#section-6.1.1 |
|
73 | + */ |
|
74 | + protected $reasonPhrase; |
|
75 | + |
|
76 | + /** |
|
77 | + * Effective URL (may be different from original request URL in case of redirects) |
|
78 | + * @var string |
|
79 | + */ |
|
80 | + protected $effectiveUrl; |
|
81 | + |
|
82 | + /** |
|
83 | + * Associative array of response headers |
|
84 | + * @var array |
|
85 | + */ |
|
86 | + protected $headers = array(); |
|
87 | + |
|
88 | + /** |
|
89 | + * Cookies set in the response |
|
90 | + * @var array |
|
91 | + */ |
|
92 | + protected $cookies = array(); |
|
93 | + |
|
94 | + /** |
|
95 | + * Name of last header processed by parseHederLine() |
|
96 | + * |
|
97 | + * Used to handle the headers that span multiple lines |
|
98 | + * |
|
99 | + * @var string |
|
100 | + */ |
|
101 | + protected $lastHeader = null; |
|
102 | + |
|
103 | + /** |
|
104 | + * Response body |
|
105 | + * @var string |
|
106 | + */ |
|
107 | + protected $body = ''; |
|
108 | + |
|
109 | + /** |
|
110 | + * Whether the body is still encoded by Content-Encoding |
|
111 | + * |
|
112 | + * cURL provides the decoded body to the callback; if we are reading from |
|
113 | + * socket the body is still gzipped / deflated |
|
114 | + * |
|
115 | + * @var bool |
|
116 | + */ |
|
117 | + protected $bodyEncoded; |
|
118 | + |
|
119 | + /** |
|
120 | + * Associative array of HTTP status code / reason phrase. |
|
121 | + * |
|
122 | + * @var array |
|
123 | + * @link http://tools.ietf.org/html/rfc2616#section-10 |
|
124 | + */ |
|
125 | + protected static $phrases = array( |
|
126 | + |
|
127 | + // 1xx: Informational - Request received, continuing process |
|
128 | + 100 => 'Continue', |
|
129 | + 101 => 'Switching Protocols', |
|
130 | + |
|
131 | + // 2xx: Success - The action was successfully received, understood and |
|
132 | + // accepted |
|
133 | + 200 => 'OK', |
|
134 | + 201 => 'Created', |
|
135 | + 202 => 'Accepted', |
|
136 | + 203 => 'Non-Authoritative Information', |
|
137 | + 204 => 'No Content', |
|
138 | + 205 => 'Reset Content', |
|
139 | + 206 => 'Partial Content', |
|
140 | + |
|
141 | + // 3xx: Redirection - Further action must be taken in order to complete |
|
142 | + // the request |
|
143 | + 300 => 'Multiple Choices', |
|
144 | + 301 => 'Moved Permanently', |
|
145 | + 302 => 'Found', // 1.1 |
|
146 | + 303 => 'See Other', |
|
147 | + 304 => 'Not Modified', |
|
148 | + 305 => 'Use Proxy', |
|
149 | + 307 => 'Temporary Redirect', |
|
150 | + |
|
151 | + // 4xx: Client Error - The request contains bad syntax or cannot be |
|
152 | + // fulfilled |
|
153 | + 400 => 'Bad Request', |
|
154 | + 401 => 'Unauthorized', |
|
155 | + 402 => 'Payment Required', |
|
156 | + 403 => 'Forbidden', |
|
157 | + 404 => 'Not Found', |
|
158 | + 405 => 'Method Not Allowed', |
|
159 | + 406 => 'Not Acceptable', |
|
160 | + 407 => 'Proxy Authentication Required', |
|
161 | + 408 => 'Request Timeout', |
|
162 | + 409 => 'Conflict', |
|
163 | + 410 => 'Gone', |
|
164 | + 411 => 'Length Required', |
|
165 | + 412 => 'Precondition Failed', |
|
166 | + 413 => 'Request Entity Too Large', |
|
167 | + 414 => 'Request-URI Too Long', |
|
168 | + 415 => 'Unsupported Media Type', |
|
169 | + 416 => 'Requested Range Not Satisfiable', |
|
170 | + 417 => 'Expectation Failed', |
|
171 | + |
|
172 | + // 5xx: Server Error - The server failed to fulfill an apparently |
|
173 | + // valid request |
|
174 | + 500 => 'Internal Server Error', |
|
175 | + 501 => 'Not Implemented', |
|
176 | + 502 => 'Bad Gateway', |
|
177 | + 503 => 'Service Unavailable', |
|
178 | + 504 => 'Gateway Timeout', |
|
179 | + 505 => 'HTTP Version Not Supported', |
|
180 | + 509 => 'Bandwidth Limit Exceeded', |
|
181 | + |
|
182 | + ); |
|
183 | + |
|
184 | + /** |
|
185 | + * Returns the default reason phrase for the given code or all reason phrases |
|
186 | + * |
|
187 | + * @param int $code Response code |
|
188 | + * |
|
189 | + * @return string|array|null Default reason phrase for $code if $code is given |
|
190 | + * (null if no phrase is available), array of all |
|
191 | + * reason phrases if $code is null |
|
192 | + * @link http://pear.php.net/bugs/18716 |
|
193 | + */ |
|
194 | + public static function getDefaultReasonPhrase($code = null) |
|
195 | + { |
|
196 | + if (null === $code) { |
|
197 | + return self::$phrases; |
|
198 | + } else { |
|
199 | + return isset(self::$phrases[$code]) ? self::$phrases[$code] : null; |
|
200 | + } |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * Constructor, parses the response status line |
|
205 | + * |
|
206 | + * @param string $statusLine Response status line (e.g. "HTTP/1.1 200 OK") |
|
207 | + * @param bool $bodyEncoded Whether body is still encoded by Content-Encoding |
|
208 | + * @param string $effectiveUrl Effective URL of the response |
|
209 | + * |
|
210 | + * @throws HTTP_Request2_MessageException if status line is invalid according to spec |
|
211 | + */ |
|
212 | + public function __construct($statusLine, $bodyEncoded = true, $effectiveUrl = null) |
|
213 | + { |
|
214 | + if (!preg_match('!^HTTP/(\d\.\d) (\d{3})(?: (.+))?!', $statusLine, $m)) { |
|
215 | + throw new HTTP_Request2_MessageException( |
|
216 | + "Malformed response: {$statusLine}", |
|
217 | + HTTP_Request2_Exception::MALFORMED_RESPONSE |
|
218 | + ); |
|
219 | + } |
|
220 | + $this->version = $m[1]; |
|
221 | + $this->code = intval($m[2]); |
|
222 | + $this->reasonPhrase = !empty($m[3]) ? trim($m[3]) : self::getDefaultReasonPhrase($this->code); |
|
223 | + $this->bodyEncoded = (bool)$bodyEncoded; |
|
224 | + $this->effectiveUrl = (string)$effectiveUrl; |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * Parses the line from HTTP response filling $headers array |
|
229 | + * |
|
230 | + * The method should be called after reading the line from socket or receiving |
|
231 | + * it into cURL callback. Passing an empty string here indicates the end of |
|
232 | + * response headers and triggers additional processing, so be sure to pass an |
|
233 | + * empty string in the end. |
|
234 | + * |
|
235 | + * @param string $headerLine Line from HTTP response |
|
236 | + */ |
|
237 | + public function parseHeaderLine($headerLine) |
|
238 | + { |
|
239 | + $headerLine = trim($headerLine, "\r\n"); |
|
240 | + |
|
241 | + if ('' == $headerLine) { |
|
242 | + // empty string signals the end of headers, process the received ones |
|
243 | + if (!empty($this->headers['set-cookie'])) { |
|
244 | + $cookies = is_array($this->headers['set-cookie'])? |
|
245 | + $this->headers['set-cookie']: |
|
246 | + array($this->headers['set-cookie']); |
|
247 | + foreach ($cookies as $cookieString) { |
|
248 | + $this->parseCookie($cookieString); |
|
249 | + } |
|
250 | + unset($this->headers['set-cookie']); |
|
251 | + } |
|
252 | + foreach (array_keys($this->headers) as $k) { |
|
253 | + if (is_array($this->headers[$k])) { |
|
254 | + $this->headers[$k] = implode(', ', $this->headers[$k]); |
|
255 | + } |
|
256 | + } |
|
257 | + |
|
258 | + } elseif (preg_match('!^([^\x00-\x1f\x7f-\xff()<>@,;:\\\\"/\[\]?={}\s]+):(.+)$!', $headerLine, $m)) { |
|
259 | + // string of the form header-name: header value |
|
260 | + $name = strtolower($m[1]); |
|
261 | + $value = trim($m[2]); |
|
262 | + if (empty($this->headers[$name])) { |
|
263 | + $this->headers[$name] = $value; |
|
264 | + } else { |
|
265 | + if (!is_array($this->headers[$name])) { |
|
266 | + $this->headers[$name] = array($this->headers[$name]); |
|
267 | + } |
|
268 | + $this->headers[$name][] = $value; |
|
269 | + } |
|
270 | + $this->lastHeader = $name; |
|
271 | + |
|
272 | + } elseif (preg_match('!^\s+(.+)$!', $headerLine, $m) && $this->lastHeader) { |
|
273 | + // continuation of a previous header |
|
274 | + if (!is_array($this->headers[$this->lastHeader])) { |
|
275 | + $this->headers[$this->lastHeader] .= ' ' . trim($m[1]); |
|
276 | + } else { |
|
277 | + $key = count($this->headers[$this->lastHeader]) - 1; |
|
278 | + $this->headers[$this->lastHeader][$key] .= ' ' . trim($m[1]); |
|
279 | + } |
|
280 | + } |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * Parses a Set-Cookie header to fill $cookies array |
|
285 | + * |
|
286 | + * @param string $cookieString value of Set-Cookie header |
|
287 | + * |
|
288 | + * @link http://web.archive.org/web/20080331104521/http://cgi.netscape.com/newsref/std/cookie_spec.html |
|
289 | + */ |
|
290 | + protected function parseCookie($cookieString) |
|
291 | + { |
|
292 | + $cookie = array( |
|
293 | + 'expires' => null, |
|
294 | + 'domain' => null, |
|
295 | + 'path' => null, |
|
296 | + 'secure' => false |
|
297 | + ); |
|
298 | + |
|
299 | + if (!strpos($cookieString, ';')) { |
|
300 | + // Only a name=value pair |
|
301 | + $pos = strpos($cookieString, '='); |
|
302 | + $cookie['name'] = trim(substr($cookieString, 0, $pos)); |
|
303 | + $cookie['value'] = trim(substr($cookieString, $pos + 1)); |
|
304 | + |
|
305 | + } else { |
|
306 | + // Some optional parameters are supplied |
|
307 | + $elements = explode(';', $cookieString); |
|
308 | + $pos = strpos($elements[0], '='); |
|
309 | + $cookie['name'] = trim(substr($elements[0], 0, $pos)); |
|
310 | + $cookie['value'] = trim(substr($elements[0], $pos + 1)); |
|
311 | + |
|
312 | + for ($i = 1; $i < count($elements); $i++) { |
|
313 | + if (false === strpos($elements[$i], '=')) { |
|
314 | + $elName = trim($elements[$i]); |
|
315 | + $elValue = null; |
|
316 | + } else { |
|
317 | + list ($elName, $elValue) = array_map('trim', explode('=', $elements[$i])); |
|
318 | + } |
|
319 | + $elName = strtolower($elName); |
|
320 | + if ('secure' == $elName) { |
|
321 | + $cookie['secure'] = true; |
|
322 | + } elseif ('expires' == $elName) { |
|
323 | + $cookie['expires'] = str_replace('"', '', $elValue); |
|
324 | + } elseif ('path' == $elName || 'domain' == $elName) { |
|
325 | + $cookie[$elName] = urldecode($elValue); |
|
326 | + } else { |
|
327 | + $cookie[$elName] = $elValue; |
|
328 | + } |
|
329 | + } |
|
330 | + } |
|
331 | + $this->cookies[] = $cookie; |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * Appends a string to the response body |
|
336 | + * |
|
337 | + * @param string $bodyChunk part of response body |
|
338 | + */ |
|
339 | + public function appendBody($bodyChunk) |
|
340 | + { |
|
341 | + $this->body .= $bodyChunk; |
|
342 | + } |
|
343 | + |
|
344 | + /** |
|
345 | + * Returns the effective URL of the response |
|
346 | + * |
|
347 | + * This may be different from the request URL if redirects were followed. |
|
348 | + * |
|
349 | + * @return string |
|
350 | + * @link http://pear.php.net/bugs/bug.php?id=18412 |
|
351 | + */ |
|
352 | + public function getEffectiveUrl() |
|
353 | + { |
|
354 | + return $this->effectiveUrl; |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * Returns the status code |
|
359 | + * |
|
360 | + * @return integer |
|
361 | + */ |
|
362 | + public function getStatus() |
|
363 | + { |
|
364 | + return $this->code; |
|
365 | + } |
|
366 | + |
|
367 | + /** |
|
368 | + * Returns the reason phrase |
|
369 | + * |
|
370 | + * @return string |
|
371 | + */ |
|
372 | + public function getReasonPhrase() |
|
373 | + { |
|
374 | + return $this->reasonPhrase; |
|
375 | + } |
|
376 | + |
|
377 | + /** |
|
378 | + * Whether response is a redirect that can be automatically handled by HTTP_Request2 |
|
379 | + * |
|
380 | + * @return bool |
|
381 | + */ |
|
382 | + public function isRedirect() |
|
383 | + { |
|
384 | + return in_array($this->code, array(300, 301, 302, 303, 307)) |
|
385 | + && isset($this->headers['location']); |
|
386 | + } |
|
387 | + |
|
388 | + /** |
|
389 | + * Returns either the named header or all response headers |
|
390 | + * |
|
391 | + * @param string $headerName Name of header to return |
|
392 | + * |
|
393 | + * @return string|array Value of $headerName header (null if header is |
|
394 | + * not present), array of all response headers if |
|
395 | + * $headerName is null |
|
396 | + */ |
|
397 | + public function getHeader($headerName = null) |
|
398 | + { |
|
399 | + if (null === $headerName) { |
|
400 | + return $this->headers; |
|
401 | + } else { |
|
402 | + $headerName = strtolower($headerName); |
|
403 | + return isset($this->headers[$headerName])? $this->headers[$headerName]: null; |
|
404 | + } |
|
405 | + } |
|
406 | + |
|
407 | + /** |
|
408 | + * Returns cookies set in response |
|
409 | + * |
|
410 | + * @return array |
|
411 | + */ |
|
412 | + public function getCookies() |
|
413 | + { |
|
414 | + return $this->cookies; |
|
415 | + } |
|
416 | + |
|
417 | + /** |
|
418 | + * Returns the body of the response |
|
419 | + * |
|
420 | + * @return string |
|
421 | + * @throws HTTP_Request2_Exception if body cannot be decoded |
|
422 | + */ |
|
423 | + public function getBody() |
|
424 | + { |
|
425 | + if (0 == strlen($this->body) || !$this->bodyEncoded |
|
426 | + || !in_array(strtolower($this->getHeader('content-encoding')), array('gzip', 'deflate')) |
|
427 | + ) { |
|
428 | + return $this->body; |
|
429 | + |
|
430 | + } else { |
|
431 | + if (extension_loaded('mbstring') && (2 & ini_get('mbstring.func_overload'))) { |
|
432 | + $oldEncoding = mb_internal_encoding(); |
|
433 | + mb_internal_encoding('8bit'); |
|
434 | + } |
|
435 | + |
|
436 | + try { |
|
437 | + switch (strtolower($this->getHeader('content-encoding'))) { |
|
438 | + case 'gzip': |
|
439 | + $decoded = self::decodeGzip($this->body); |
|
440 | + break; |
|
441 | + case 'deflate': |
|
442 | + $decoded = self::decodeDeflate($this->body); |
|
443 | + } |
|
444 | + } catch (Exception $e) { |
|
445 | + } |
|
446 | + |
|
447 | + if (!empty($oldEncoding)) { |
|
448 | + mb_internal_encoding($oldEncoding); |
|
449 | + } |
|
450 | + if (!empty($e)) { |
|
451 | + throw $e; |
|
452 | + } |
|
453 | + return $decoded; |
|
454 | + } |
|
455 | + } |
|
456 | + |
|
457 | + /** |
|
458 | + * Get the HTTP version of the response |
|
459 | + * |
|
460 | + * @return string |
|
461 | + */ |
|
462 | + public function getVersion() |
|
463 | + { |
|
464 | + return $this->version; |
|
465 | + } |
|
466 | + |
|
467 | + /** |
|
468 | + * Decodes the message-body encoded by gzip |
|
469 | + * |
|
470 | + * The real decoding work is done by gzinflate() built-in function, this |
|
471 | + * method only parses the header and checks data for compliance with |
|
472 | + * RFC 1952 |
|
473 | + * |
|
474 | + * @param string $data gzip-encoded data |
|
475 | + * |
|
476 | + * @return string decoded data |
|
477 | + * @throws HTTP_Request2_LogicException |
|
478 | + * @throws HTTP_Request2_MessageException |
|
479 | + * @link http://tools.ietf.org/html/rfc1952 |
|
480 | + */ |
|
481 | + public static function decodeGzip($data) |
|
482 | + { |
|
483 | + $length = strlen($data); |
|
484 | + // If it doesn't look like gzip-encoded data, don't bother |
|
485 | + if (18 > $length || strcmp(substr($data, 0, 2), "\x1f\x8b")) { |
|
486 | + return $data; |
|
487 | + } |
|
488 | + if (!function_exists('gzinflate')) { |
|
489 | + throw new HTTP_Request2_LogicException( |
|
490 | + 'Unable to decode body: gzip extension not available', |
|
491 | + HTTP_Request2_Exception::MISCONFIGURATION |
|
492 | + ); |
|
493 | + } |
|
494 | + $method = ord(substr($data, 2, 1)); |
|
495 | + if (8 != $method) { |
|
496 | + throw new HTTP_Request2_MessageException( |
|
497 | + 'Error parsing gzip header: unknown compression method', |
|
498 | + HTTP_Request2_Exception::DECODE_ERROR |
|
499 | + ); |
|
500 | + } |
|
501 | + $flags = ord(substr($data, 3, 1)); |
|
502 | + if ($flags & 224) { |
|
503 | + throw new HTTP_Request2_MessageException( |
|
504 | + 'Error parsing gzip header: reserved bits are set', |
|
505 | + HTTP_Request2_Exception::DECODE_ERROR |
|
506 | + ); |
|
507 | + } |
|
508 | + |
|
509 | + // header is 10 bytes minimum. may be longer, though. |
|
510 | + $headerLength = 10; |
|
511 | + // extra fields, need to skip 'em |
|
512 | + if ($flags & 4) { |
|
513 | + if ($length - $headerLength - 2 < 8) { |
|
514 | + throw new HTTP_Request2_MessageException( |
|
515 | + 'Error parsing gzip header: data too short', |
|
516 | + HTTP_Request2_Exception::DECODE_ERROR |
|
517 | + ); |
|
518 | + } |
|
519 | + $extraLength = unpack('v', substr($data, 10, 2)); |
|
520 | + if ($length - $headerLength - 2 - $extraLength[1] < 8) { |
|
521 | + throw new HTTP_Request2_MessageException( |
|
522 | + 'Error parsing gzip header: data too short', |
|
523 | + HTTP_Request2_Exception::DECODE_ERROR |
|
524 | + ); |
|
525 | + } |
|
526 | + $headerLength += $extraLength[1] + 2; |
|
527 | + } |
|
528 | + // file name, need to skip that |
|
529 | + if ($flags & 8) { |
|
530 | + if ($length - $headerLength - 1 < 8) { |
|
531 | + throw new HTTP_Request2_MessageException( |
|
532 | + 'Error parsing gzip header: data too short', |
|
533 | + HTTP_Request2_Exception::DECODE_ERROR |
|
534 | + ); |
|
535 | + } |
|
536 | + $filenameLength = strpos(substr($data, $headerLength), chr(0)); |
|
537 | + if (false === $filenameLength || $length - $headerLength - $filenameLength - 1 < 8) { |
|
538 | + throw new HTTP_Request2_MessageException( |
|
539 | + 'Error parsing gzip header: data too short', |
|
540 | + HTTP_Request2_Exception::DECODE_ERROR |
|
541 | + ); |
|
542 | + } |
|
543 | + $headerLength += $filenameLength + 1; |
|
544 | + } |
|
545 | + // comment, need to skip that also |
|
546 | + if ($flags & 16) { |
|
547 | + if ($length - $headerLength - 1 < 8) { |
|
548 | + throw new HTTP_Request2_MessageException( |
|
549 | + 'Error parsing gzip header: data too short', |
|
550 | + HTTP_Request2_Exception::DECODE_ERROR |
|
551 | + ); |
|
552 | + } |
|
553 | + $commentLength = strpos(substr($data, $headerLength), chr(0)); |
|
554 | + if (false === $commentLength || $length - $headerLength - $commentLength - 1 < 8) { |
|
555 | + throw new HTTP_Request2_MessageException( |
|
556 | + 'Error parsing gzip header: data too short', |
|
557 | + HTTP_Request2_Exception::DECODE_ERROR |
|
558 | + ); |
|
559 | + } |
|
560 | + $headerLength += $commentLength + 1; |
|
561 | + } |
|
562 | + // have a CRC for header. let's check |
|
563 | + if ($flags & 2) { |
|
564 | + if ($length - $headerLength - 2 < 8) { |
|
565 | + throw new HTTP_Request2_MessageException( |
|
566 | + 'Error parsing gzip header: data too short', |
|
567 | + HTTP_Request2_Exception::DECODE_ERROR |
|
568 | + ); |
|
569 | + } |
|
570 | + $crcReal = 0xffff & crc32(substr($data, 0, $headerLength)); |
|
571 | + $crcStored = unpack('v', substr($data, $headerLength, 2)); |
|
572 | + if ($crcReal != $crcStored[1]) { |
|
573 | + throw new HTTP_Request2_MessageException( |
|
574 | + 'Header CRC check failed', |
|
575 | + HTTP_Request2_Exception::DECODE_ERROR |
|
576 | + ); |
|
577 | + } |
|
578 | + $headerLength += 2; |
|
579 | + } |
|
580 | + // unpacked data CRC and size at the end of encoded data |
|
581 | + $tmp = unpack('V2', substr($data, -8)); |
|
582 | + $dataCrc = $tmp[1]; |
|
583 | + $dataSize = $tmp[2]; |
|
584 | + |
|
585 | + // finally, call the gzinflate() function |
|
586 | + // don't pass $dataSize to gzinflate, see bugs #13135, #14370 |
|
587 | + $unpacked = gzinflate(substr($data, $headerLength, -8)); |
|
588 | + if (false === $unpacked) { |
|
589 | + throw new HTTP_Request2_MessageException( |
|
590 | + 'gzinflate() call failed', |
|
591 | + HTTP_Request2_Exception::DECODE_ERROR |
|
592 | + ); |
|
593 | + } elseif ($dataSize != strlen($unpacked)) { |
|
594 | + throw new HTTP_Request2_MessageException( |
|
595 | + 'Data size check failed', |
|
596 | + HTTP_Request2_Exception::DECODE_ERROR |
|
597 | + ); |
|
598 | + } elseif ((0xffffffff & $dataCrc) != (0xffffffff & crc32($unpacked))) { |
|
599 | + throw new HTTP_Request2_Exception( |
|
600 | + 'Data CRC check failed', |
|
601 | + HTTP_Request2_Exception::DECODE_ERROR |
|
602 | + ); |
|
603 | + } |
|
604 | + return $unpacked; |
|
605 | + } |
|
606 | + |
|
607 | + /** |
|
608 | + * Decodes the message-body encoded by deflate |
|
609 | + * |
|
610 | + * @param string $data deflate-encoded data |
|
611 | + * |
|
612 | + * @return string decoded data |
|
613 | + * @throws HTTP_Request2_LogicException |
|
614 | + */ |
|
615 | + public static function decodeDeflate($data) |
|
616 | + { |
|
617 | + if (!function_exists('gzuncompress')) { |
|
618 | + throw new HTTP_Request2_LogicException( |
|
619 | + 'Unable to decode body: gzip extension not available', |
|
620 | + HTTP_Request2_Exception::MISCONFIGURATION |
|
621 | + ); |
|
622 | + } |
|
623 | + // RFC 2616 defines 'deflate' encoding as zlib format from RFC 1950, |
|
624 | + // while many applications send raw deflate stream from RFC 1951. |
|
625 | + // We should check for presence of zlib header and use gzuncompress() or |
|
626 | + // gzinflate() as needed. See bug #15305 |
|
627 | + $header = unpack('n', substr($data, 0, 2)); |
|
628 | + return (0 == $header[1] % 31)? gzuncompress($data): gzinflate($data); |
|
629 | + } |
|
630 | 630 | } |
631 | 631 | ?> |
632 | 632 | \ No newline at end of file |
@@ -435,11 +435,11 @@ |
||
435 | 435 | |
436 | 436 | try { |
437 | 437 | switch (strtolower($this->getHeader('content-encoding'))) { |
438 | - case 'gzip': |
|
439 | - $decoded = self::decodeGzip($this->body); |
|
440 | - break; |
|
441 | - case 'deflate': |
|
442 | - $decoded = self::decodeDeflate($this->body); |
|
438 | + case 'gzip': |
|
439 | + $decoded = self::decodeGzip($this->body); |
|
440 | + break; |
|
441 | + case 'deflate': |
|
442 | + $decoded = self::decodeDeflate($this->body); |
|
443 | 443 | } |
444 | 444 | } catch (Exception $e) { |
445 | 445 | } |
@@ -142,7 +142,7 @@ discard block |
||
142 | 142 | // the request |
143 | 143 | 300 => 'Multiple Choices', |
144 | 144 | 301 => 'Moved Permanently', |
145 | - 302 => 'Found', // 1.1 |
|
145 | + 302 => 'Found', // 1.1 |
|
146 | 146 | 303 => 'See Other', |
147 | 147 | 304 => 'Not Modified', |
148 | 148 | 305 => 'Use Proxy', |
@@ -220,8 +220,8 @@ discard block |
||
220 | 220 | $this->version = $m[1]; |
221 | 221 | $this->code = intval($m[2]); |
222 | 222 | $this->reasonPhrase = !empty($m[3]) ? trim($m[3]) : self::getDefaultReasonPhrase($this->code); |
223 | - $this->bodyEncoded = (bool)$bodyEncoded; |
|
224 | - $this->effectiveUrl = (string)$effectiveUrl; |
|
223 | + $this->bodyEncoded = (bool) $bodyEncoded; |
|
224 | + $this->effectiveUrl = (string) $effectiveUrl; |
|
225 | 225 | } |
226 | 226 | |
227 | 227 | /** |
@@ -241,9 +241,8 @@ discard block |
||
241 | 241 | if ('' == $headerLine) { |
242 | 242 | // empty string signals the end of headers, process the received ones |
243 | 243 | if (!empty($this->headers['set-cookie'])) { |
244 | - $cookies = is_array($this->headers['set-cookie'])? |
|
245 | - $this->headers['set-cookie']: |
|
246 | - array($this->headers['set-cookie']); |
|
244 | + $cookies = is_array($this->headers['set-cookie']) ? |
|
245 | + $this->headers['set-cookie'] : array($this->headers['set-cookie']); |
|
247 | 246 | foreach ($cookies as $cookieString) { |
248 | 247 | $this->parseCookie($cookieString); |
249 | 248 | } |
@@ -272,10 +271,10 @@ discard block |
||
272 | 271 | } elseif (preg_match('!^\s+(.+)$!', $headerLine, $m) && $this->lastHeader) { |
273 | 272 | // continuation of a previous header |
274 | 273 | if (!is_array($this->headers[$this->lastHeader])) { |
275 | - $this->headers[$this->lastHeader] .= ' ' . trim($m[1]); |
|
274 | + $this->headers[$this->lastHeader] .= ' '.trim($m[1]); |
|
276 | 275 | } else { |
277 | 276 | $key = count($this->headers[$this->lastHeader]) - 1; |
278 | - $this->headers[$this->lastHeader][$key] .= ' ' . trim($m[1]); |
|
277 | + $this->headers[$this->lastHeader][$key] .= ' '.trim($m[1]); |
|
279 | 278 | } |
280 | 279 | } |
281 | 280 | } |
@@ -400,7 +399,7 @@ discard block |
||
400 | 399 | return $this->headers; |
401 | 400 | } else { |
402 | 401 | $headerName = strtolower($headerName); |
403 | - return isset($this->headers[$headerName])? $this->headers[$headerName]: null; |
|
402 | + return isset($this->headers[$headerName]) ? $this->headers[$headerName] : null; |
|
404 | 403 | } |
405 | 404 | } |
406 | 405 | |
@@ -625,7 +624,7 @@ discard block |
||
625 | 624 | // We should check for presence of zlib header and use gzuncompress() or |
626 | 625 | // gzinflate() as needed. See bug #15305 |
627 | 626 | $header = unpack('n', substr($data, 0, 2)); |
628 | - return (0 == $header[1] % 31)? gzuncompress($data): gzinflate($data); |
|
627 | + return (0 == $header[1] % 31) ? gzuncompress($data) : gzinflate($data); |
|
629 | 628 | } |
630 | 629 | } |
631 | 630 | ?> |
632 | 631 | \ No newline at end of file |
@@ -68,14 +68,14 @@ |
||
68 | 68 | ); |
69 | 69 | } |
70 | 70 | switch ($response['method']) { |
71 | - case 2: |
|
72 | - $this->performAuthentication($username, $password); |
|
73 | - case 0: |
|
74 | - break; |
|
75 | - default: |
|
76 | - throw new HTTP_Request2_ConnectionException( |
|
77 | - "Connection rejected by proxy due to unsupported auth method" |
|
78 | - ); |
|
71 | + case 2: |
|
72 | + $this->performAuthentication($username, $password); |
|
73 | + case 0: |
|
74 | + break; |
|
75 | + default: |
|
76 | + throw new HTTP_Request2_ConnectionException( |
|
77 | + "Connection rejected by proxy due to unsupported auth method" |
|
78 | + ); |
|
79 | 79 | } |
80 | 80 | } |
81 | 81 |
@@ -35,101 +35,101 @@ |
||
35 | 35 | */ |
36 | 36 | class HTTP_Request2_SOCKS5 extends HTTP_Request2_SocketWrapper |
37 | 37 | { |
38 | - /** |
|
39 | - * Constructor, tries to connect and authenticate to a SOCKS5 proxy |
|
40 | - * |
|
41 | - * @param string $address Proxy address, e.g. 'tcp://localhost:1080' |
|
42 | - * @param int $timeout Connection timeout (seconds) |
|
43 | - * @param array $contextOptions Stream context options |
|
44 | - * @param string $username Proxy user name |
|
45 | - * @param string $password Proxy password |
|
46 | - * |
|
47 | - * @throws HTTP_Request2_LogicException |
|
48 | - * @throws HTTP_Request2_ConnectionException |
|
49 | - * @throws HTTP_Request2_MessageException |
|
50 | - */ |
|
51 | - public function __construct( |
|
52 | - $address, $timeout = 10, array $contextOptions = array(), |
|
53 | - $username = null, $password = null |
|
54 | - ) { |
|
55 | - parent::__construct($address, $timeout, $contextOptions); |
|
38 | + /** |
|
39 | + * Constructor, tries to connect and authenticate to a SOCKS5 proxy |
|
40 | + * |
|
41 | + * @param string $address Proxy address, e.g. 'tcp://localhost:1080' |
|
42 | + * @param int $timeout Connection timeout (seconds) |
|
43 | + * @param array $contextOptions Stream context options |
|
44 | + * @param string $username Proxy user name |
|
45 | + * @param string $password Proxy password |
|
46 | + * |
|
47 | + * @throws HTTP_Request2_LogicException |
|
48 | + * @throws HTTP_Request2_ConnectionException |
|
49 | + * @throws HTTP_Request2_MessageException |
|
50 | + */ |
|
51 | + public function __construct( |
|
52 | + $address, $timeout = 10, array $contextOptions = array(), |
|
53 | + $username = null, $password = null |
|
54 | + ) { |
|
55 | + parent::__construct($address, $timeout, $contextOptions); |
|
56 | 56 | |
57 | - if (strlen($username)) { |
|
58 | - $request = pack('C4', 5, 2, 0, 2); |
|
59 | - } else { |
|
60 | - $request = pack('C3', 5, 1, 0); |
|
61 | - } |
|
62 | - $this->write($request); |
|
63 | - $response = unpack('Cversion/Cmethod', $this->read(3)); |
|
64 | - if (5 != $response['version']) { |
|
65 | - throw new HTTP_Request2_MessageException( |
|
66 | - 'Invalid version received from SOCKS5 proxy: ' . $response['version'], |
|
67 | - HTTP_Request2_Exception::MALFORMED_RESPONSE |
|
68 | - ); |
|
69 | - } |
|
70 | - switch ($response['method']) { |
|
71 | - case 2: |
|
72 | - $this->performAuthentication($username, $password); |
|
73 | - case 0: |
|
74 | - break; |
|
75 | - default: |
|
76 | - throw new HTTP_Request2_ConnectionException( |
|
77 | - "Connection rejected by proxy due to unsupported auth method" |
|
78 | - ); |
|
79 | - } |
|
80 | - } |
|
57 | + if (strlen($username)) { |
|
58 | + $request = pack('C4', 5, 2, 0, 2); |
|
59 | + } else { |
|
60 | + $request = pack('C3', 5, 1, 0); |
|
61 | + } |
|
62 | + $this->write($request); |
|
63 | + $response = unpack('Cversion/Cmethod', $this->read(3)); |
|
64 | + if (5 != $response['version']) { |
|
65 | + throw new HTTP_Request2_MessageException( |
|
66 | + 'Invalid version received from SOCKS5 proxy: ' . $response['version'], |
|
67 | + HTTP_Request2_Exception::MALFORMED_RESPONSE |
|
68 | + ); |
|
69 | + } |
|
70 | + switch ($response['method']) { |
|
71 | + case 2: |
|
72 | + $this->performAuthentication($username, $password); |
|
73 | + case 0: |
|
74 | + break; |
|
75 | + default: |
|
76 | + throw new HTTP_Request2_ConnectionException( |
|
77 | + "Connection rejected by proxy due to unsupported auth method" |
|
78 | + ); |
|
79 | + } |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Performs username/password authentication for SOCKS5 |
|
84 | - * |
|
85 | - * @param string $username Proxy user name |
|
86 | - * @param string $password Proxy password |
|
87 | - * |
|
88 | - * @throws HTTP_Request2_ConnectionException |
|
89 | - * @throws HTTP_Request2_MessageException |
|
90 | - * @link http://tools.ietf.org/html/rfc1929 |
|
91 | - */ |
|
92 | - protected function performAuthentication($username, $password) |
|
93 | - { |
|
94 | - $request = pack('C2', 1, strlen($username)) . $username |
|
95 | - . pack('C', strlen($password)) . $password; |
|
82 | + /** |
|
83 | + * Performs username/password authentication for SOCKS5 |
|
84 | + * |
|
85 | + * @param string $username Proxy user name |
|
86 | + * @param string $password Proxy password |
|
87 | + * |
|
88 | + * @throws HTTP_Request2_ConnectionException |
|
89 | + * @throws HTTP_Request2_MessageException |
|
90 | + * @link http://tools.ietf.org/html/rfc1929 |
|
91 | + */ |
|
92 | + protected function performAuthentication($username, $password) |
|
93 | + { |
|
94 | + $request = pack('C2', 1, strlen($username)) . $username |
|
95 | + . pack('C', strlen($password)) . $password; |
|
96 | 96 | |
97 | - $this->write($request); |
|
98 | - $response = unpack('Cvn/Cstatus', $this->read(3)); |
|
99 | - if (1 != $response['vn'] || 0 != $response['status']) { |
|
100 | - throw new HTTP_Request2_ConnectionException( |
|
101 | - 'Connection rejected by proxy due to invalid username and/or password' |
|
102 | - ); |
|
103 | - } |
|
104 | - } |
|
97 | + $this->write($request); |
|
98 | + $response = unpack('Cvn/Cstatus', $this->read(3)); |
|
99 | + if (1 != $response['vn'] || 0 != $response['status']) { |
|
100 | + throw new HTTP_Request2_ConnectionException( |
|
101 | + 'Connection rejected by proxy due to invalid username and/or password' |
|
102 | + ); |
|
103 | + } |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * Connects to a remote host via proxy |
|
108 | - * |
|
109 | - * @param string $remoteHost Remote host |
|
110 | - * @param int $remotePort Remote port |
|
111 | - * |
|
112 | - * @throws HTTP_Request2_ConnectionException |
|
113 | - * @throws HTTP_Request2_MessageException |
|
114 | - */ |
|
115 | - public function connect($remoteHost, $remotePort) |
|
116 | - { |
|
117 | - $request = pack('C5', 0x05, 0x01, 0x00, 0x03, strlen($remoteHost)) |
|
118 | - . $remoteHost . pack('n', $remotePort); |
|
106 | + /** |
|
107 | + * Connects to a remote host via proxy |
|
108 | + * |
|
109 | + * @param string $remoteHost Remote host |
|
110 | + * @param int $remotePort Remote port |
|
111 | + * |
|
112 | + * @throws HTTP_Request2_ConnectionException |
|
113 | + * @throws HTTP_Request2_MessageException |
|
114 | + */ |
|
115 | + public function connect($remoteHost, $remotePort) |
|
116 | + { |
|
117 | + $request = pack('C5', 0x05, 0x01, 0x00, 0x03, strlen($remoteHost)) |
|
118 | + . $remoteHost . pack('n', $remotePort); |
|
119 | 119 | |
120 | - $this->write($request); |
|
121 | - $response = unpack('Cversion/Creply/Creserved', $this->read(1024)); |
|
122 | - if (5 != $response['version'] || 0 != $response['reserved']) { |
|
123 | - throw new HTTP_Request2_MessageException( |
|
124 | - 'Invalid response received from SOCKS5 proxy', |
|
125 | - HTTP_Request2_Exception::MALFORMED_RESPONSE |
|
126 | - ); |
|
127 | - } elseif (0 != $response['reply']) { |
|
128 | - throw new HTTP_Request2_ConnectionException( |
|
129 | - "Unable to connect to {$remoteHost}:{$remotePort} through SOCKS5 proxy", |
|
130 | - 0, $response['reply'] |
|
131 | - ); |
|
132 | - } |
|
133 | - } |
|
120 | + $this->write($request); |
|
121 | + $response = unpack('Cversion/Creply/Creserved', $this->read(1024)); |
|
122 | + if (5 != $response['version'] || 0 != $response['reserved']) { |
|
123 | + throw new HTTP_Request2_MessageException( |
|
124 | + 'Invalid response received from SOCKS5 proxy', |
|
125 | + HTTP_Request2_Exception::MALFORMED_RESPONSE |
|
126 | + ); |
|
127 | + } elseif (0 != $response['reply']) { |
|
128 | + throw new HTTP_Request2_ConnectionException( |
|
129 | + "Unable to connect to {$remoteHost}:{$remotePort} through SOCKS5 proxy", |
|
130 | + 0, $response['reply'] |
|
131 | + ); |
|
132 | + } |
|
133 | + } |
|
134 | 134 | } |
135 | 135 | ?> |
136 | 136 | \ No newline at end of file |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | $response = unpack('Cversion/Cmethod', $this->read(3)); |
64 | 64 | if (5 != $response['version']) { |
65 | 65 | throw new HTTP_Request2_MessageException( |
66 | - 'Invalid version received from SOCKS5 proxy: ' . $response['version'], |
|
66 | + 'Invalid version received from SOCKS5 proxy: '.$response['version'], |
|
67 | 67 | HTTP_Request2_Exception::MALFORMED_RESPONSE |
68 | 68 | ); |
69 | 69 | } |
@@ -91,8 +91,8 @@ discard block |
||
91 | 91 | */ |
92 | 92 | protected function performAuthentication($username, $password) |
93 | 93 | { |
94 | - $request = pack('C2', 1, strlen($username)) . $username |
|
95 | - . pack('C', strlen($password)) . $password; |
|
94 | + $request = pack('C2', 1, strlen($username)).$username |
|
95 | + . pack('C', strlen($password)).$password; |
|
96 | 96 | |
97 | 97 | $this->write($request); |
98 | 98 | $response = unpack('Cvn/Cstatus', $this->read(3)); |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | public function connect($remoteHost, $remotePort) |
116 | 116 | { |
117 | 117 | $request = pack('C5', 0x05, 0x01, 0x00, 0x03, strlen($remoteHost)) |
118 | - . $remoteHost . pack('n', $remotePort); |
|
118 | + . $remoteHost.pack('n', $remotePort); |
|
119 | 119 | |
120 | 120 | $this->write($request); |
121 | 121 | $response = unpack('Cversion/Creply/Creserved', $this->read(1024)); |
@@ -170,7 +170,7 @@ |
||
170 | 170 | // reset socket timeout if we don't have request timeout specified, |
171 | 171 | // prevents further calls failing with a bogus Exception |
172 | 172 | if (!$this->deadline) { |
173 | - $default = (int)@ini_get('default_socket_timeout'); |
|
173 | + $default = (int) @ini_get('default_socket_timeout'); |
|
174 | 174 | stream_set_timeout($this->socket, $default > 0 ? $default : PHP_INT_MAX); |
175 | 175 | } |
176 | 176 | if ($info['timed_out']) { |
@@ -38,260 +38,260 @@ |
||
38 | 38 | */ |
39 | 39 | class HTTP_Request2_SocketWrapper |
40 | 40 | { |
41 | - /** |
|
42 | - * PHP warning messages raised during stream_socket_client() call |
|
43 | - * @var array |
|
44 | - */ |
|
45 | - protected $connectionWarnings = array(); |
|
41 | + /** |
|
42 | + * PHP warning messages raised during stream_socket_client() call |
|
43 | + * @var array |
|
44 | + */ |
|
45 | + protected $connectionWarnings = array(); |
|
46 | 46 | |
47 | - /** |
|
48 | - * Connected socket |
|
49 | - * @var resource |
|
50 | - */ |
|
51 | - protected $socket; |
|
47 | + /** |
|
48 | + * Connected socket |
|
49 | + * @var resource |
|
50 | + */ |
|
51 | + protected $socket; |
|
52 | 52 | |
53 | - /** |
|
54 | - * Sum of start time and global timeout, exception will be thrown if request continues past this time |
|
55 | - * @var integer |
|
56 | - */ |
|
57 | - protected $deadline; |
|
53 | + /** |
|
54 | + * Sum of start time and global timeout, exception will be thrown if request continues past this time |
|
55 | + * @var integer |
|
56 | + */ |
|
57 | + protected $deadline; |
|
58 | 58 | |
59 | - /** |
|
60 | - * Global timeout value, mostly for exception messages |
|
61 | - * @var integer |
|
62 | - */ |
|
63 | - protected $timeout; |
|
59 | + /** |
|
60 | + * Global timeout value, mostly for exception messages |
|
61 | + * @var integer |
|
62 | + */ |
|
63 | + protected $timeout; |
|
64 | 64 | |
65 | - /** |
|
66 | - * Class constructor, tries to establish connection |
|
67 | - * |
|
68 | - * @param string $address Address for stream_socket_client() call, |
|
69 | - * e.g. 'tcp://localhost:80' |
|
70 | - * @param int $timeout Connection timeout (seconds) |
|
71 | - * @param array $contextOptions Context options |
|
72 | - * |
|
73 | - * @throws HTTP_Request2_LogicException |
|
74 | - * @throws HTTP_Request2_ConnectionException |
|
75 | - */ |
|
76 | - public function __construct($address, $timeout, array $contextOptions = array()) |
|
77 | - { |
|
78 | - if (!empty($contextOptions) |
|
79 | - && !isset($contextOptions['socket']) && !isset($contextOptions['ssl']) |
|
80 | - ) { |
|
81 | - // Backwards compatibility with 2.1.0 and 2.1.1 releases |
|
82 | - $contextOptions = array('ssl' => $contextOptions); |
|
83 | - } |
|
84 | - $context = stream_context_create(); |
|
85 | - foreach ($contextOptions as $wrapper => $options) { |
|
86 | - foreach ($options as $name => $value) { |
|
87 | - if (!stream_context_set_option($context, $wrapper, $name, $value)) { |
|
88 | - throw new HTTP_Request2_LogicException( |
|
89 | - "Error setting '{$wrapper}' wrapper context option '{$name}'" |
|
90 | - ); |
|
91 | - } |
|
92 | - } |
|
93 | - } |
|
94 | - set_error_handler(array($this, 'connectionWarningsHandler')); |
|
95 | - $this->socket = stream_socket_client( |
|
96 | - $address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $context |
|
97 | - ); |
|
98 | - restore_error_handler(); |
|
99 | - // if we fail to bind to a specified local address (see request #19515), |
|
100 | - // connection still succeeds, albeit with a warning. Throw an Exception |
|
101 | - // with the warning text in this case as that connection is unlikely |
|
102 | - // to be what user wants and as Curl throws an error in similar case. |
|
103 | - if ($this->connectionWarnings) { |
|
104 | - if ($this->socket) { |
|
105 | - fclose($this->socket); |
|
106 | - } |
|
107 | - $error = $errstr ? $errstr : implode("\n", $this->connectionWarnings); |
|
108 | - throw new HTTP_Request2_ConnectionException( |
|
109 | - "Unable to connect to {$address}. Error: {$error}", 0, $errno |
|
110 | - ); |
|
111 | - } |
|
112 | - } |
|
65 | + /** |
|
66 | + * Class constructor, tries to establish connection |
|
67 | + * |
|
68 | + * @param string $address Address for stream_socket_client() call, |
|
69 | + * e.g. 'tcp://localhost:80' |
|
70 | + * @param int $timeout Connection timeout (seconds) |
|
71 | + * @param array $contextOptions Context options |
|
72 | + * |
|
73 | + * @throws HTTP_Request2_LogicException |
|
74 | + * @throws HTTP_Request2_ConnectionException |
|
75 | + */ |
|
76 | + public function __construct($address, $timeout, array $contextOptions = array()) |
|
77 | + { |
|
78 | + if (!empty($contextOptions) |
|
79 | + && !isset($contextOptions['socket']) && !isset($contextOptions['ssl']) |
|
80 | + ) { |
|
81 | + // Backwards compatibility with 2.1.0 and 2.1.1 releases |
|
82 | + $contextOptions = array('ssl' => $contextOptions); |
|
83 | + } |
|
84 | + $context = stream_context_create(); |
|
85 | + foreach ($contextOptions as $wrapper => $options) { |
|
86 | + foreach ($options as $name => $value) { |
|
87 | + if (!stream_context_set_option($context, $wrapper, $name, $value)) { |
|
88 | + throw new HTTP_Request2_LogicException( |
|
89 | + "Error setting '{$wrapper}' wrapper context option '{$name}'" |
|
90 | + ); |
|
91 | + } |
|
92 | + } |
|
93 | + } |
|
94 | + set_error_handler(array($this, 'connectionWarningsHandler')); |
|
95 | + $this->socket = stream_socket_client( |
|
96 | + $address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $context |
|
97 | + ); |
|
98 | + restore_error_handler(); |
|
99 | + // if we fail to bind to a specified local address (see request #19515), |
|
100 | + // connection still succeeds, albeit with a warning. Throw an Exception |
|
101 | + // with the warning text in this case as that connection is unlikely |
|
102 | + // to be what user wants and as Curl throws an error in similar case. |
|
103 | + if ($this->connectionWarnings) { |
|
104 | + if ($this->socket) { |
|
105 | + fclose($this->socket); |
|
106 | + } |
|
107 | + $error = $errstr ? $errstr : implode("\n", $this->connectionWarnings); |
|
108 | + throw new HTTP_Request2_ConnectionException( |
|
109 | + "Unable to connect to {$address}. Error: {$error}", 0, $errno |
|
110 | + ); |
|
111 | + } |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * Destructor, disconnects socket |
|
116 | - */ |
|
117 | - public function __destruct() |
|
118 | - { |
|
119 | - fclose($this->socket); |
|
120 | - } |
|
114 | + /** |
|
115 | + * Destructor, disconnects socket |
|
116 | + */ |
|
117 | + public function __destruct() |
|
118 | + { |
|
119 | + fclose($this->socket); |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * Wrapper around fread(), handles global request timeout |
|
124 | - * |
|
125 | - * @param int $length Reads up to this number of bytes |
|
126 | - * |
|
127 | - * @return string Data read from socket |
|
128 | - * @throws HTTP_Request2_MessageException In case of timeout |
|
129 | - */ |
|
130 | - public function read($length) |
|
131 | - { |
|
132 | - if ($this->deadline) { |
|
133 | - stream_set_timeout($this->socket, max($this->deadline - time(), 1)); |
|
134 | - } |
|
135 | - $data = fread($this->socket, $length); |
|
136 | - $this->checkTimeout(); |
|
137 | - return $data; |
|
138 | - } |
|
122 | + /** |
|
123 | + * Wrapper around fread(), handles global request timeout |
|
124 | + * |
|
125 | + * @param int $length Reads up to this number of bytes |
|
126 | + * |
|
127 | + * @return string Data read from socket |
|
128 | + * @throws HTTP_Request2_MessageException In case of timeout |
|
129 | + */ |
|
130 | + public function read($length) |
|
131 | + { |
|
132 | + if ($this->deadline) { |
|
133 | + stream_set_timeout($this->socket, max($this->deadline - time(), 1)); |
|
134 | + } |
|
135 | + $data = fread($this->socket, $length); |
|
136 | + $this->checkTimeout(); |
|
137 | + return $data; |
|
138 | + } |
|
139 | 139 | |
140 | - /** |
|
141 | - * Reads until either the end of the socket or a newline, whichever comes first |
|
142 | - * |
|
143 | - * Strips the trailing newline from the returned data, handles global |
|
144 | - * request timeout. Method idea borrowed from Net_Socket PEAR package. |
|
145 | - * |
|
146 | - * @param int $bufferSize buffer size to use for reading |
|
147 | - * @param int $localTimeout timeout value to use just for this call |
|
148 | - * (used when waiting for "100 Continue" response) |
|
149 | - * |
|
150 | - * @return string Available data up to the newline (not including newline) |
|
151 | - * @throws HTTP_Request2_MessageException In case of timeout |
|
152 | - */ |
|
153 | - public function readLine($bufferSize, $localTimeout = null) |
|
154 | - { |
|
155 | - $line = ''; |
|
156 | - while (!feof($this->socket)) { |
|
157 | - if (null !== $localTimeout) { |
|
158 | - stream_set_timeout($this->socket, $localTimeout); |
|
159 | - } elseif ($this->deadline) { |
|
160 | - stream_set_timeout($this->socket, max($this->deadline - time(), 1)); |
|
161 | - } |
|
140 | + /** |
|
141 | + * Reads until either the end of the socket or a newline, whichever comes first |
|
142 | + * |
|
143 | + * Strips the trailing newline from the returned data, handles global |
|
144 | + * request timeout. Method idea borrowed from Net_Socket PEAR package. |
|
145 | + * |
|
146 | + * @param int $bufferSize buffer size to use for reading |
|
147 | + * @param int $localTimeout timeout value to use just for this call |
|
148 | + * (used when waiting for "100 Continue" response) |
|
149 | + * |
|
150 | + * @return string Available data up to the newline (not including newline) |
|
151 | + * @throws HTTP_Request2_MessageException In case of timeout |
|
152 | + */ |
|
153 | + public function readLine($bufferSize, $localTimeout = null) |
|
154 | + { |
|
155 | + $line = ''; |
|
156 | + while (!feof($this->socket)) { |
|
157 | + if (null !== $localTimeout) { |
|
158 | + stream_set_timeout($this->socket, $localTimeout); |
|
159 | + } elseif ($this->deadline) { |
|
160 | + stream_set_timeout($this->socket, max($this->deadline - time(), 1)); |
|
161 | + } |
|
162 | 162 | |
163 | - $line .= @fgets($this->socket, $bufferSize); |
|
163 | + $line .= @fgets($this->socket, $bufferSize); |
|
164 | 164 | |
165 | - if (null === $localTimeout) { |
|
166 | - $this->checkTimeout(); |
|
165 | + if (null === $localTimeout) { |
|
166 | + $this->checkTimeout(); |
|
167 | 167 | |
168 | - } else { |
|
169 | - $info = stream_get_meta_data($this->socket); |
|
170 | - // reset socket timeout if we don't have request timeout specified, |
|
171 | - // prevents further calls failing with a bogus Exception |
|
172 | - if (!$this->deadline) { |
|
173 | - $default = (int)@ini_get('default_socket_timeout'); |
|
174 | - stream_set_timeout($this->socket, $default > 0 ? $default : PHP_INT_MAX); |
|
175 | - } |
|
176 | - if ($info['timed_out']) { |
|
177 | - throw new HTTP_Request2_MessageException( |
|
178 | - "readLine() call timed out", HTTP_Request2_Exception::TIMEOUT |
|
179 | - ); |
|
180 | - } |
|
181 | - } |
|
182 | - if (substr($line, -1) == "\n") { |
|
183 | - return rtrim($line, "\r\n"); |
|
184 | - } |
|
185 | - } |
|
186 | - return $line; |
|
187 | - } |
|
168 | + } else { |
|
169 | + $info = stream_get_meta_data($this->socket); |
|
170 | + // reset socket timeout if we don't have request timeout specified, |
|
171 | + // prevents further calls failing with a bogus Exception |
|
172 | + if (!$this->deadline) { |
|
173 | + $default = (int)@ini_get('default_socket_timeout'); |
|
174 | + stream_set_timeout($this->socket, $default > 0 ? $default : PHP_INT_MAX); |
|
175 | + } |
|
176 | + if ($info['timed_out']) { |
|
177 | + throw new HTTP_Request2_MessageException( |
|
178 | + "readLine() call timed out", HTTP_Request2_Exception::TIMEOUT |
|
179 | + ); |
|
180 | + } |
|
181 | + } |
|
182 | + if (substr($line, -1) == "\n") { |
|
183 | + return rtrim($line, "\r\n"); |
|
184 | + } |
|
185 | + } |
|
186 | + return $line; |
|
187 | + } |
|
188 | 188 | |
189 | - /** |
|
190 | - * Wrapper around fwrite(), handles global request timeout |
|
191 | - * |
|
192 | - * @param string $data String to be written |
|
193 | - * |
|
194 | - * @return int |
|
195 | - * @throws HTTP_Request2_MessageException |
|
196 | - */ |
|
197 | - public function write($data) |
|
198 | - { |
|
199 | - if ($this->deadline) { |
|
200 | - stream_set_timeout($this->socket, max($this->deadline - time(), 1)); |
|
201 | - } |
|
202 | - $written = fwrite($this->socket, $data); |
|
203 | - $this->checkTimeout(); |
|
204 | - // http://www.php.net/manual/en/function.fwrite.php#96951 |
|
205 | - if ($written < strlen($data)) { |
|
206 | - throw new HTTP_Request2_MessageException('Error writing request'); |
|
207 | - } |
|
208 | - return $written; |
|
209 | - } |
|
189 | + /** |
|
190 | + * Wrapper around fwrite(), handles global request timeout |
|
191 | + * |
|
192 | + * @param string $data String to be written |
|
193 | + * |
|
194 | + * @return int |
|
195 | + * @throws HTTP_Request2_MessageException |
|
196 | + */ |
|
197 | + public function write($data) |
|
198 | + { |
|
199 | + if ($this->deadline) { |
|
200 | + stream_set_timeout($this->socket, max($this->deadline - time(), 1)); |
|
201 | + } |
|
202 | + $written = fwrite($this->socket, $data); |
|
203 | + $this->checkTimeout(); |
|
204 | + // http://www.php.net/manual/en/function.fwrite.php#96951 |
|
205 | + if ($written < strlen($data)) { |
|
206 | + throw new HTTP_Request2_MessageException('Error writing request'); |
|
207 | + } |
|
208 | + return $written; |
|
209 | + } |
|
210 | 210 | |
211 | - /** |
|
212 | - * Tests for end-of-file on a socket |
|
213 | - * |
|
214 | - * @return bool |
|
215 | - */ |
|
216 | - public function eof() |
|
217 | - { |
|
218 | - return feof($this->socket); |
|
219 | - } |
|
211 | + /** |
|
212 | + * Tests for end-of-file on a socket |
|
213 | + * |
|
214 | + * @return bool |
|
215 | + */ |
|
216 | + public function eof() |
|
217 | + { |
|
218 | + return feof($this->socket); |
|
219 | + } |
|
220 | 220 | |
221 | - /** |
|
222 | - * Sets request deadline |
|
223 | - * |
|
224 | - * @param int $deadline Exception will be thrown if request continues |
|
225 | - * past this time |
|
226 | - * @param int $timeout Original request timeout value, to use in |
|
227 | - * Exception message |
|
228 | - */ |
|
229 | - public function setDeadline($deadline, $timeout) |
|
230 | - { |
|
231 | - $this->deadline = $deadline; |
|
232 | - $this->timeout = $timeout; |
|
233 | - } |
|
221 | + /** |
|
222 | + * Sets request deadline |
|
223 | + * |
|
224 | + * @param int $deadline Exception will be thrown if request continues |
|
225 | + * past this time |
|
226 | + * @param int $timeout Original request timeout value, to use in |
|
227 | + * Exception message |
|
228 | + */ |
|
229 | + public function setDeadline($deadline, $timeout) |
|
230 | + { |
|
231 | + $this->deadline = $deadline; |
|
232 | + $this->timeout = $timeout; |
|
233 | + } |
|
234 | 234 | |
235 | - /** |
|
236 | - * Turns on encryption on a socket |
|
237 | - * |
|
238 | - * @throws HTTP_Request2_ConnectionException |
|
239 | - */ |
|
240 | - public function enableCrypto() |
|
241 | - { |
|
242 | - $modes = array( |
|
243 | - STREAM_CRYPTO_METHOD_TLS_CLIENT, |
|
244 | - STREAM_CRYPTO_METHOD_SSLv3_CLIENT, |
|
245 | - STREAM_CRYPTO_METHOD_SSLv23_CLIENT, |
|
246 | - STREAM_CRYPTO_METHOD_SSLv2_CLIENT |
|
247 | - ); |
|
235 | + /** |
|
236 | + * Turns on encryption on a socket |
|
237 | + * |
|
238 | + * @throws HTTP_Request2_ConnectionException |
|
239 | + */ |
|
240 | + public function enableCrypto() |
|
241 | + { |
|
242 | + $modes = array( |
|
243 | + STREAM_CRYPTO_METHOD_TLS_CLIENT, |
|
244 | + STREAM_CRYPTO_METHOD_SSLv3_CLIENT, |
|
245 | + STREAM_CRYPTO_METHOD_SSLv23_CLIENT, |
|
246 | + STREAM_CRYPTO_METHOD_SSLv2_CLIENT |
|
247 | + ); |
|
248 | 248 | |
249 | - foreach ($modes as $mode) { |
|
250 | - if (stream_socket_enable_crypto($this->socket, true, $mode)) { |
|
251 | - return; |
|
252 | - } |
|
253 | - } |
|
254 | - throw new HTTP_Request2_ConnectionException( |
|
255 | - 'Failed to enable secure connection when connecting through proxy' |
|
256 | - ); |
|
257 | - } |
|
249 | + foreach ($modes as $mode) { |
|
250 | + if (stream_socket_enable_crypto($this->socket, true, $mode)) { |
|
251 | + return; |
|
252 | + } |
|
253 | + } |
|
254 | + throw new HTTP_Request2_ConnectionException( |
|
255 | + 'Failed to enable secure connection when connecting through proxy' |
|
256 | + ); |
|
257 | + } |
|
258 | 258 | |
259 | - /** |
|
260 | - * Throws an Exception if stream timed out |
|
261 | - * |
|
262 | - * @throws HTTP_Request2_MessageException |
|
263 | - */ |
|
264 | - protected function checkTimeout() |
|
265 | - { |
|
266 | - $info = stream_get_meta_data($this->socket); |
|
267 | - if ($info['timed_out'] || $this->deadline && time() > $this->deadline) { |
|
268 | - $reason = $this->deadline |
|
269 | - ? "after {$this->timeout} second(s)" |
|
270 | - : 'due to default_socket_timeout php.ini setting'; |
|
271 | - throw new HTTP_Request2_MessageException( |
|
272 | - "Request timed out {$reason}", HTTP_Request2_Exception::TIMEOUT |
|
273 | - ); |
|
274 | - } |
|
275 | - } |
|
259 | + /** |
|
260 | + * Throws an Exception if stream timed out |
|
261 | + * |
|
262 | + * @throws HTTP_Request2_MessageException |
|
263 | + */ |
|
264 | + protected function checkTimeout() |
|
265 | + { |
|
266 | + $info = stream_get_meta_data($this->socket); |
|
267 | + if ($info['timed_out'] || $this->deadline && time() > $this->deadline) { |
|
268 | + $reason = $this->deadline |
|
269 | + ? "after {$this->timeout} second(s)" |
|
270 | + : 'due to default_socket_timeout php.ini setting'; |
|
271 | + throw new HTTP_Request2_MessageException( |
|
272 | + "Request timed out {$reason}", HTTP_Request2_Exception::TIMEOUT |
|
273 | + ); |
|
274 | + } |
|
275 | + } |
|
276 | 276 | |
277 | - /** |
|
278 | - * Error handler to use during stream_socket_client() call |
|
279 | - * |
|
280 | - * One stream_socket_client() call may produce *multiple* PHP warnings |
|
281 | - * (especially OpenSSL-related), we keep them in an array to later use for |
|
282 | - * the message of HTTP_Request2_ConnectionException |
|
283 | - * |
|
284 | - * @param int $errno error level |
|
285 | - * @param string $errstr error message |
|
286 | - * |
|
287 | - * @return bool |
|
288 | - */ |
|
289 | - protected function connectionWarningsHandler($errno, $errstr) |
|
290 | - { |
|
291 | - if ($errno & E_WARNING) { |
|
292 | - array_unshift($this->connectionWarnings, $errstr); |
|
293 | - } |
|
294 | - return true; |
|
295 | - } |
|
277 | + /** |
|
278 | + * Error handler to use during stream_socket_client() call |
|
279 | + * |
|
280 | + * One stream_socket_client() call may produce *multiple* PHP warnings |
|
281 | + * (especially OpenSSL-related), we keep them in an array to later use for |
|
282 | + * the message of HTTP_Request2_ConnectionException |
|
283 | + * |
|
284 | + * @param int $errno error level |
|
285 | + * @param string $errstr error message |
|
286 | + * |
|
287 | + * @return bool |
|
288 | + */ |
|
289 | + protected function connectionWarningsHandler($errno, $errstr) |
|
290 | + { |
|
291 | + if ($errno & E_WARNING) { |
|
292 | + array_unshift($this->connectionWarnings, $errstr); |
|
293 | + } |
|
294 | + return true; |
|
295 | + } |
|
296 | 296 | } |
297 | 297 | ?> |
@@ -59,31 +59,31 @@ discard block |
||
59 | 59 | * MIME boundary |
60 | 60 | * @var string |
61 | 61 | */ |
62 | - private $_boundary; |
|
62 | + private $_boundary; |
|
63 | 63 | |
64 | 64 | /** |
65 | 65 | * Form parameters added via {@link HTTP_Request2::addPostParameter()} |
66 | 66 | * @var array |
67 | 67 | */ |
68 | - private $_params = array(); |
|
68 | + private $_params = array(); |
|
69 | 69 | |
70 | 70 | /** |
71 | 71 | * File uploads added via {@link HTTP_Request2::addUpload()} |
72 | 72 | * @var array |
73 | 73 | */ |
74 | - private $_uploads = array(); |
|
74 | + private $_uploads = array(); |
|
75 | 75 | |
76 | 76 | /** |
77 | 77 | * Header for parts with parameters |
78 | 78 | * @var string |
79 | 79 | */ |
80 | - private $_headerParam = "--%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n"; |
|
80 | + private $_headerParam = "--%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n"; |
|
81 | 81 | |
82 | 82 | /** |
83 | 83 | * Header for parts with uploads |
84 | 84 | * @var string |
85 | 85 | */ |
86 | - private $_headerUpload = "--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\nContent-Type: %s\r\n\r\n"; |
|
86 | + private $_headerUpload = "--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\nContent-Type: %s\r\n\r\n"; |
|
87 | 87 | |
88 | 88 | /** |
89 | 89 | * Current position in parameter and upload arrays |
@@ -93,7 +93,7 @@ discard block |
||
93 | 93 | * |
94 | 94 | * @var array |
95 | 95 | */ |
96 | - private $_pos = array(0, 0); |
|
96 | + private $_pos = array(0, 0); |
|
97 | 97 | |
98 | 98 | |
99 | 99 | /** |
@@ -103,59 +103,59 @@ discard block |
||
103 | 103 | * @param array file uploads set via {@link HTTP_Request2::addUpload()} |
104 | 104 | * @param bool whether to append brackets to array variable names |
105 | 105 | */ |
106 | - public function __construct(array $params, array $uploads, $useBrackets = true) |
|
107 | - { |
|
108 | - $this->_params = self::_flattenArray('', $params, $useBrackets); |
|
109 | - foreach ($uploads as $fieldName => $f) { |
|
110 | - if (!is_array($f['fp'])) { |
|
111 | - $this->_uploads[] = $f + array('name' => $fieldName); |
|
112 | - } else { |
|
113 | - for ($i = 0; $i < count($f['fp']); $i++) { |
|
114 | - $upload = array( |
|
115 | - 'name' => ($useBrackets? $fieldName . '[' . $i . ']': $fieldName) |
|
116 | - ); |
|
117 | - foreach (array('fp', 'filename', 'size', 'type') as $key) { |
|
118 | - $upload[$key] = $f[$key][$i]; |
|
119 | - } |
|
120 | - $this->_uploads[] = $upload; |
|
121 | - } |
|
122 | - } |
|
123 | - } |
|
124 | - } |
|
106 | + public function __construct(array $params, array $uploads, $useBrackets = true) |
|
107 | + { |
|
108 | + $this->_params = self::_flattenArray('', $params, $useBrackets); |
|
109 | + foreach ($uploads as $fieldName => $f) { |
|
110 | + if (!is_array($f['fp'])) { |
|
111 | + $this->_uploads[] = $f + array('name' => $fieldName); |
|
112 | + } else { |
|
113 | + for ($i = 0; $i < count($f['fp']); $i++) { |
|
114 | + $upload = array( |
|
115 | + 'name' => ($useBrackets? $fieldName . '[' . $i . ']': $fieldName) |
|
116 | + ); |
|
117 | + foreach (array('fp', 'filename', 'size', 'type') as $key) { |
|
118 | + $upload[$key] = $f[$key][$i]; |
|
119 | + } |
|
120 | + $this->_uploads[] = $upload; |
|
121 | + } |
|
122 | + } |
|
123 | + } |
|
124 | + } |
|
125 | 125 | |
126 | 126 | /** |
127 | 127 | * Returns the length of the body to use in Content-Length header |
128 | 128 | * |
129 | 129 | * @return integer |
130 | 130 | */ |
131 | - public function getLength() |
|
132 | - { |
|
133 | - $boundaryLength = strlen($this->getBoundary()); |
|
134 | - $headerParamLength = strlen($this->_headerParam) - 4 + $boundaryLength; |
|
135 | - $headerUploadLength = strlen($this->_headerUpload) - 8 + $boundaryLength; |
|
136 | - $length = $boundaryLength + 6; |
|
137 | - foreach ($this->_params as $p) { |
|
138 | - $length += $headerParamLength + strlen($p[0]) + strlen($p[1]) + 2; |
|
139 | - } |
|
140 | - foreach ($this->_uploads as $u) { |
|
141 | - $length += $headerUploadLength + strlen($u['name']) + strlen($u['type']) + |
|
142 | - strlen($u['filename']) + $u['size'] + 2; |
|
143 | - } |
|
144 | - return $length; |
|
145 | - } |
|
131 | + public function getLength() |
|
132 | + { |
|
133 | + $boundaryLength = strlen($this->getBoundary()); |
|
134 | + $headerParamLength = strlen($this->_headerParam) - 4 + $boundaryLength; |
|
135 | + $headerUploadLength = strlen($this->_headerUpload) - 8 + $boundaryLength; |
|
136 | + $length = $boundaryLength + 6; |
|
137 | + foreach ($this->_params as $p) { |
|
138 | + $length += $headerParamLength + strlen($p[0]) + strlen($p[1]) + 2; |
|
139 | + } |
|
140 | + foreach ($this->_uploads as $u) { |
|
141 | + $length += $headerUploadLength + strlen($u['name']) + strlen($u['type']) + |
|
142 | + strlen($u['filename']) + $u['size'] + 2; |
|
143 | + } |
|
144 | + return $length; |
|
145 | + } |
|
146 | 146 | |
147 | 147 | /** |
148 | 148 | * Returns the boundary to use in Content-Type header |
149 | 149 | * |
150 | 150 | * @return string |
151 | 151 | */ |
152 | - public function getBoundary() |
|
153 | - { |
|
154 | - if (empty($this->_boundary)) { |
|
155 | - $this->_boundary = '--' . md5('PEAR-HTTP_Request2-' . microtime()); |
|
156 | - } |
|
157 | - return $this->_boundary; |
|
158 | - } |
|
152 | + public function getBoundary() |
|
153 | + { |
|
154 | + if (empty($this->_boundary)) { |
|
155 | + $this->_boundary = '--' . md5('PEAR-HTTP_Request2-' . microtime()); |
|
156 | + } |
|
157 | + return $this->_boundary; |
|
158 | + } |
|
159 | 159 | |
160 | 160 | /** |
161 | 161 | * Returns next chunk of request body |
@@ -163,69 +163,69 @@ discard block |
||
163 | 163 | * @param integer Amount of bytes to read |
164 | 164 | * @return string Up to $length bytes of data, empty string if at end |
165 | 165 | */ |
166 | - public function read($length) |
|
167 | - { |
|
168 | - $ret = ''; |
|
169 | - $boundary = $this->getBoundary(); |
|
170 | - $paramCount = count($this->_params); |
|
171 | - $uploadCount = count($this->_uploads); |
|
172 | - while ($length > 0 && $this->_pos[0] <= $paramCount + $uploadCount) { |
|
173 | - $oldLength = $length; |
|
174 | - if ($this->_pos[0] < $paramCount) { |
|
175 | - $param = sprintf($this->_headerParam, $boundary, |
|
176 | - $this->_params[$this->_pos[0]][0]) . |
|
177 | - $this->_params[$this->_pos[0]][1] . "\r\n"; |
|
178 | - $ret .= substr($param, $this->_pos[1], $length); |
|
179 | - $length -= min(strlen($param) - $this->_pos[1], $length); |
|
166 | + public function read($length) |
|
167 | + { |
|
168 | + $ret = ''; |
|
169 | + $boundary = $this->getBoundary(); |
|
170 | + $paramCount = count($this->_params); |
|
171 | + $uploadCount = count($this->_uploads); |
|
172 | + while ($length > 0 && $this->_pos[0] <= $paramCount + $uploadCount) { |
|
173 | + $oldLength = $length; |
|
174 | + if ($this->_pos[0] < $paramCount) { |
|
175 | + $param = sprintf($this->_headerParam, $boundary, |
|
176 | + $this->_params[$this->_pos[0]][0]) . |
|
177 | + $this->_params[$this->_pos[0]][1] . "\r\n"; |
|
178 | + $ret .= substr($param, $this->_pos[1], $length); |
|
179 | + $length -= min(strlen($param) - $this->_pos[1], $length); |
|
180 | 180 | |
181 | - } elseif ($this->_pos[0] < $paramCount + $uploadCount) { |
|
182 | - $pos = $this->_pos[0] - $paramCount; |
|
183 | - $header = sprintf($this->_headerUpload, $boundary, |
|
184 | - $this->_uploads[$pos]['name'], |
|
185 | - $this->_uploads[$pos]['filename'], |
|
186 | - $this->_uploads[$pos]['type']); |
|
187 | - if ($this->_pos[1] < strlen($header)) { |
|
188 | - $ret .= substr($header, $this->_pos[1], $length); |
|
189 | - $length -= min(strlen($header) - $this->_pos[1], $length); |
|
190 | - } |
|
191 | - $filePos = max(0, $this->_pos[1] - strlen($header)); |
|
192 | - if ($length > 0 && $filePos < $this->_uploads[$pos]['size']) { |
|
193 | - $ret .= fread($this->_uploads[$pos]['fp'], $length); |
|
194 | - $length -= min($length, $this->_uploads[$pos]['size'] - $filePos); |
|
195 | - } |
|
196 | - if ($length > 0) { |
|
197 | - $start = $this->_pos[1] + ($oldLength - $length) - |
|
198 | - strlen($header) - $this->_uploads[$pos]['size']; |
|
199 | - $ret .= substr("\r\n", $start, $length); |
|
200 | - $length -= min(2 - $start, $length); |
|
201 | - } |
|
181 | + } elseif ($this->_pos[0] < $paramCount + $uploadCount) { |
|
182 | + $pos = $this->_pos[0] - $paramCount; |
|
183 | + $header = sprintf($this->_headerUpload, $boundary, |
|
184 | + $this->_uploads[$pos]['name'], |
|
185 | + $this->_uploads[$pos]['filename'], |
|
186 | + $this->_uploads[$pos]['type']); |
|
187 | + if ($this->_pos[1] < strlen($header)) { |
|
188 | + $ret .= substr($header, $this->_pos[1], $length); |
|
189 | + $length -= min(strlen($header) - $this->_pos[1], $length); |
|
190 | + } |
|
191 | + $filePos = max(0, $this->_pos[1] - strlen($header)); |
|
192 | + if ($length > 0 && $filePos < $this->_uploads[$pos]['size']) { |
|
193 | + $ret .= fread($this->_uploads[$pos]['fp'], $length); |
|
194 | + $length -= min($length, $this->_uploads[$pos]['size'] - $filePos); |
|
195 | + } |
|
196 | + if ($length > 0) { |
|
197 | + $start = $this->_pos[1] + ($oldLength - $length) - |
|
198 | + strlen($header) - $this->_uploads[$pos]['size']; |
|
199 | + $ret .= substr("\r\n", $start, $length); |
|
200 | + $length -= min(2 - $start, $length); |
|
201 | + } |
|
202 | 202 | |
203 | - } else { |
|
204 | - $closing = '--' . $boundary . "--\r\n"; |
|
205 | - $ret .= substr($closing, $this->_pos[1], $length); |
|
206 | - $length -= min(strlen($closing) - $this->_pos[1], $length); |
|
207 | - } |
|
208 | - if ($length > 0) { |
|
209 | - $this->_pos = array($this->_pos[0] + 1, 0); |
|
210 | - } else { |
|
211 | - $this->_pos[1] += $oldLength; |
|
212 | - } |
|
213 | - } |
|
214 | - return $ret; |
|
215 | - } |
|
203 | + } else { |
|
204 | + $closing = '--' . $boundary . "--\r\n"; |
|
205 | + $ret .= substr($closing, $this->_pos[1], $length); |
|
206 | + $length -= min(strlen($closing) - $this->_pos[1], $length); |
|
207 | + } |
|
208 | + if ($length > 0) { |
|
209 | + $this->_pos = array($this->_pos[0] + 1, 0); |
|
210 | + } else { |
|
211 | + $this->_pos[1] += $oldLength; |
|
212 | + } |
|
213 | + } |
|
214 | + return $ret; |
|
215 | + } |
|
216 | 216 | |
217 | 217 | /** |
218 | 218 | * Sets the current position to the start of the body |
219 | 219 | * |
220 | 220 | * This allows reusing the same body in another request |
221 | 221 | */ |
222 | - public function rewind() |
|
223 | - { |
|
224 | - $this->_pos = array(0, 0); |
|
225 | - foreach ($this->_uploads as $u) { |
|
226 | - rewind($u['fp']); |
|
227 | - } |
|
228 | - } |
|
222 | + public function rewind() |
|
223 | + { |
|
224 | + $this->_pos = array(0, 0); |
|
225 | + foreach ($this->_uploads as $u) { |
|
226 | + rewind($u['fp']); |
|
227 | + } |
|
228 | + } |
|
229 | 229 | |
230 | 230 | /** |
231 | 231 | * Returns the body as string |
@@ -235,11 +235,11 @@ discard block |
||
235 | 235 | * |
236 | 236 | * @return string |
237 | 237 | */ |
238 | - public function __toString() |
|
239 | - { |
|
240 | - $this->rewind(); |
|
241 | - return $this->read($this->getLength()); |
|
242 | - } |
|
238 | + public function __toString() |
|
239 | + { |
|
240 | + $this->rewind(); |
|
241 | + return $this->read($this->getLength()); |
|
242 | + } |
|
243 | 243 | |
244 | 244 | |
245 | 245 | /** |
@@ -251,24 +251,24 @@ discard block |
||
251 | 251 | * @param bool whether to append [] to array variables' names |
252 | 252 | * @return array array with the following items: array('item name', 'item value'); |
253 | 253 | */ |
254 | - private static function _flattenArray($name, $values, $useBrackets) |
|
255 | - { |
|
256 | - if (!is_array($values)) { |
|
257 | - return array(array($name, $values)); |
|
258 | - } else { |
|
259 | - $ret = array(); |
|
260 | - foreach ($values as $k => $v) { |
|
261 | - if (empty($name)) { |
|
262 | - $newName = $k; |
|
263 | - } elseif ($useBrackets) { |
|
264 | - $newName = $name . '[' . $k . ']'; |
|
265 | - } else { |
|
266 | - $newName = $name; |
|
267 | - } |
|
268 | - $ret = array_merge($ret, self::_flattenArray($newName, $v, $useBrackets)); |
|
269 | - } |
|
270 | - return $ret; |
|
271 | - } |
|
272 | - } |
|
254 | + private static function _flattenArray($name, $values, $useBrackets) |
|
255 | + { |
|
256 | + if (!is_array($values)) { |
|
257 | + return array(array($name, $values)); |
|
258 | + } else { |
|
259 | + $ret = array(); |
|
260 | + foreach ($values as $k => $v) { |
|
261 | + if (empty($name)) { |
|
262 | + $newName = $k; |
|
263 | + } elseif ($useBrackets) { |
|
264 | + $newName = $name . '[' . $k . ']'; |
|
265 | + } else { |
|
266 | + $newName = $name; |
|
267 | + } |
|
268 | + $ret = array_merge($ret, self::_flattenArray($newName, $v, $useBrackets)); |
|
269 | + } |
|
270 | + return $ret; |
|
271 | + } |
|
272 | + } |
|
273 | 273 | } |
274 | 274 | ?> |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | } else { |
113 | 113 | for ($i = 0; $i < count($f['fp']); $i++) { |
114 | 114 | $upload = array( |
115 | - 'name' => ($useBrackets? $fieldName . '[' . $i . ']': $fieldName) |
|
115 | + 'name' => ($useBrackets ? $fieldName.'['.$i.']' : $fieldName) |
|
116 | 116 | ); |
117 | 117 | foreach (array('fp', 'filename', 'size', 'type') as $key) { |
118 | 118 | $upload[$key] = $f[$key][$i]; |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | public function getBoundary() |
153 | 153 | { |
154 | 154 | if (empty($this->_boundary)) { |
155 | - $this->_boundary = '--' . md5('PEAR-HTTP_Request2-' . microtime()); |
|
155 | + $this->_boundary = '--'.md5('PEAR-HTTP_Request2-'.microtime()); |
|
156 | 156 | } |
157 | 157 | return $this->_boundary; |
158 | 158 | } |
@@ -173,8 +173,8 @@ discard block |
||
173 | 173 | $oldLength = $length; |
174 | 174 | if ($this->_pos[0] < $paramCount) { |
175 | 175 | $param = sprintf($this->_headerParam, $boundary, |
176 | - $this->_params[$this->_pos[0]][0]) . |
|
177 | - $this->_params[$this->_pos[0]][1] . "\r\n"; |
|
176 | + $this->_params[$this->_pos[0]][0]). |
|
177 | + $this->_params[$this->_pos[0]][1]."\r\n"; |
|
178 | 178 | $ret .= substr($param, $this->_pos[1], $length); |
179 | 179 | $length -= min(strlen($param) - $this->_pos[1], $length); |
180 | 180 | |
@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | $ret .= substr($header, $this->_pos[1], $length); |
189 | 189 | $length -= min(strlen($header) - $this->_pos[1], $length); |
190 | 190 | } |
191 | - $filePos = max(0, $this->_pos[1] - strlen($header)); |
|
191 | + $filePos = max(0, $this->_pos[1] - strlen($header)); |
|
192 | 192 | if ($length > 0 && $filePos < $this->_uploads[$pos]['size']) { |
193 | 193 | $ret .= fread($this->_uploads[$pos]['fp'], $length); |
194 | 194 | $length -= min($length, $this->_uploads[$pos]['size'] - $filePos); |
@@ -201,7 +201,7 @@ discard block |
||
201 | 201 | } |
202 | 202 | |
203 | 203 | } else { |
204 | - $closing = '--' . $boundary . "--\r\n"; |
|
204 | + $closing = '--'.$boundary."--\r\n"; |
|
205 | 205 | $ret .= substr($closing, $this->_pos[1], $length); |
206 | 206 | $length -= min(strlen($closing) - $this->_pos[1], $length); |
207 | 207 | } |
@@ -261,7 +261,7 @@ discard block |
||
261 | 261 | if (empty($name)) { |
262 | 262 | $newName = $k; |
263 | 263 | } elseif ($useBrackets) { |
264 | - $newName = $name . '[' . $k . ']'; |
|
264 | + $newName = $name.'['.$k.']'; |
|
265 | 265 | } else { |
266 | 266 | $newName = $name; |
267 | 267 | } |
@@ -157,36 +157,36 @@ |
||
157 | 157 | } |
158 | 158 | |
159 | 159 | switch ($event['name']) { |
160 | - case 'connect': |
|
161 | - $this->log('* Connected to ' . $event['data']); |
|
162 | - break; |
|
163 | - case 'sentHeaders': |
|
164 | - $headers = explode("\r\n", $event['data']); |
|
165 | - array_pop($headers); |
|
166 | - foreach ($headers as $header) { |
|
167 | - $this->log('> ' . $header); |
|
168 | - } |
|
169 | - break; |
|
170 | - case 'sentBodyPart': |
|
171 | - $this->log('> ' . $event['data'] . ' byte(s) sent'); |
|
172 | - break; |
|
173 | - case 'receivedHeaders': |
|
174 | - $this->log(sprintf('< HTTP/%s %s %s', |
|
175 | - $event['data']->getVersion(), |
|
176 | - $event['data']->getStatus(), |
|
177 | - $event['data']->getReasonPhrase())); |
|
178 | - $headers = $event['data']->getHeader(); |
|
179 | - foreach ($headers as $key => $val) { |
|
180 | - $this->log('< ' . $key . ': ' . $val); |
|
181 | - } |
|
182 | - $this->log('< '); |
|
183 | - break; |
|
184 | - case 'receivedBody': |
|
185 | - $this->log($event['data']->getBody()); |
|
186 | - break; |
|
187 | - case 'disconnect': |
|
188 | - $this->log('* Disconnected'); |
|
189 | - break; |
|
160 | + case 'connect': |
|
161 | + $this->log('* Connected to ' . $event['data']); |
|
162 | + break; |
|
163 | + case 'sentHeaders': |
|
164 | + $headers = explode("\r\n", $event['data']); |
|
165 | + array_pop($headers); |
|
166 | + foreach ($headers as $header) { |
|
167 | + $this->log('> ' . $header); |
|
168 | + } |
|
169 | + break; |
|
170 | + case 'sentBodyPart': |
|
171 | + $this->log('> ' . $event['data'] . ' byte(s) sent'); |
|
172 | + break; |
|
173 | + case 'receivedHeaders': |
|
174 | + $this->log(sprintf('< HTTP/%s %s %s', |
|
175 | + $event['data']->getVersion(), |
|
176 | + $event['data']->getStatus(), |
|
177 | + $event['data']->getReasonPhrase())); |
|
178 | + $headers = $event['data']->getHeader(); |
|
179 | + foreach ($headers as $key => $val) { |
|
180 | + $this->log('< ' . $key . ': ' . $val); |
|
181 | + } |
|
182 | + $this->log('< '); |
|
183 | + break; |
|
184 | + case 'receivedBody': |
|
185 | + $this->log($event['data']->getBody()); |
|
186 | + break; |
|
187 | + case 'disconnect': |
|
188 | + $this->log('* Disconnected'); |
|
189 | + break; |
|
190 | 190 | } |
191 | 191 | } |
192 | 192 |
@@ -92,124 +92,124 @@ |
||
92 | 92 | */ |
93 | 93 | class HTTP_Request2_Observer_Log implements SplObserver |
94 | 94 | { |
95 | - // properties {{{ |
|
95 | + // properties {{{ |
|
96 | 96 | |
97 | - /** |
|
98 | - * The log target, it can be a a resource or a PEAR Log instance. |
|
99 | - * |
|
100 | - * @var resource|Log $target |
|
101 | - */ |
|
102 | - protected $target = null; |
|
97 | + /** |
|
98 | + * The log target, it can be a a resource or a PEAR Log instance. |
|
99 | + * |
|
100 | + * @var resource|Log $target |
|
101 | + */ |
|
102 | + protected $target = null; |
|
103 | 103 | |
104 | - /** |
|
105 | - * The events to log. |
|
106 | - * |
|
107 | - * @var array $events |
|
108 | - */ |
|
109 | - public $events = array( |
|
110 | - 'connect', |
|
111 | - 'sentHeaders', |
|
112 | - 'sentBodyPart', |
|
113 | - 'receivedHeaders', |
|
114 | - 'receivedBody', |
|
115 | - 'disconnect', |
|
116 | - ); |
|
104 | + /** |
|
105 | + * The events to log. |
|
106 | + * |
|
107 | + * @var array $events |
|
108 | + */ |
|
109 | + public $events = array( |
|
110 | + 'connect', |
|
111 | + 'sentHeaders', |
|
112 | + 'sentBodyPart', |
|
113 | + 'receivedHeaders', |
|
114 | + 'receivedBody', |
|
115 | + 'disconnect', |
|
116 | + ); |
|
117 | 117 | |
118 | - // }}} |
|
119 | - // __construct() {{{ |
|
118 | + // }}} |
|
119 | + // __construct() {{{ |
|
120 | 120 | |
121 | - /** |
|
122 | - * Constructor. |
|
123 | - * |
|
124 | - * @param mixed $target Can be a file path (default: php://output), a resource, |
|
125 | - * or an instance of the PEAR Log class. |
|
126 | - * @param array $events Array of events to listen to (default: all events) |
|
127 | - * |
|
128 | - * @return void |
|
129 | - */ |
|
130 | - public function __construct($target = 'php://output', array $events = array()) |
|
131 | - { |
|
132 | - if (!empty($events)) { |
|
133 | - $this->events = $events; |
|
134 | - } |
|
135 | - if (is_resource($target) || $target instanceof Log) { |
|
136 | - $this->target = $target; |
|
137 | - } elseif (false === ($this->target = @fopen($target, 'ab'))) { |
|
138 | - throw new HTTP_Request2_Exception("Unable to open '{$target}'"); |
|
139 | - } |
|
140 | - } |
|
121 | + /** |
|
122 | + * Constructor. |
|
123 | + * |
|
124 | + * @param mixed $target Can be a file path (default: php://output), a resource, |
|
125 | + * or an instance of the PEAR Log class. |
|
126 | + * @param array $events Array of events to listen to (default: all events) |
|
127 | + * |
|
128 | + * @return void |
|
129 | + */ |
|
130 | + public function __construct($target = 'php://output', array $events = array()) |
|
131 | + { |
|
132 | + if (!empty($events)) { |
|
133 | + $this->events = $events; |
|
134 | + } |
|
135 | + if (is_resource($target) || $target instanceof Log) { |
|
136 | + $this->target = $target; |
|
137 | + } elseif (false === ($this->target = @fopen($target, 'ab'))) { |
|
138 | + throw new HTTP_Request2_Exception("Unable to open '{$target}'"); |
|
139 | + } |
|
140 | + } |
|
141 | 141 | |
142 | - // }}} |
|
143 | - // update() {{{ |
|
142 | + // }}} |
|
143 | + // update() {{{ |
|
144 | 144 | |
145 | - /** |
|
146 | - * Called when the request notifies us of an event. |
|
147 | - * |
|
148 | - * @param HTTP_Request2 $subject The HTTP_Request2 instance |
|
149 | - * |
|
150 | - * @return void |
|
151 | - */ |
|
152 | - public function update(SplSubject $subject) |
|
153 | - { |
|
154 | - $event = $subject->getLastEvent(); |
|
155 | - if (!in_array($event['name'], $this->events)) { |
|
156 | - return; |
|
157 | - } |
|
145 | + /** |
|
146 | + * Called when the request notifies us of an event. |
|
147 | + * |
|
148 | + * @param HTTP_Request2 $subject The HTTP_Request2 instance |
|
149 | + * |
|
150 | + * @return void |
|
151 | + */ |
|
152 | + public function update(SplSubject $subject) |
|
153 | + { |
|
154 | + $event = $subject->getLastEvent(); |
|
155 | + if (!in_array($event['name'], $this->events)) { |
|
156 | + return; |
|
157 | + } |
|
158 | 158 | |
159 | - switch ($event['name']) { |
|
160 | - case 'connect': |
|
161 | - $this->log('* Connected to ' . $event['data']); |
|
162 | - break; |
|
163 | - case 'sentHeaders': |
|
164 | - $headers = explode("\r\n", $event['data']); |
|
165 | - array_pop($headers); |
|
166 | - foreach ($headers as $header) { |
|
167 | - $this->log('> ' . $header); |
|
168 | - } |
|
169 | - break; |
|
170 | - case 'sentBodyPart': |
|
171 | - $this->log('> ' . $event['data'] . ' byte(s) sent'); |
|
172 | - break; |
|
173 | - case 'receivedHeaders': |
|
174 | - $this->log(sprintf('< HTTP/%s %s %s', |
|
175 | - $event['data']->getVersion(), |
|
176 | - $event['data']->getStatus(), |
|
177 | - $event['data']->getReasonPhrase())); |
|
178 | - $headers = $event['data']->getHeader(); |
|
179 | - foreach ($headers as $key => $val) { |
|
180 | - $this->log('< ' . $key . ': ' . $val); |
|
181 | - } |
|
182 | - $this->log('< '); |
|
183 | - break; |
|
184 | - case 'receivedBody': |
|
185 | - $this->log($event['data']->getBody()); |
|
186 | - break; |
|
187 | - case 'disconnect': |
|
188 | - $this->log('* Disconnected'); |
|
189 | - break; |
|
190 | - } |
|
191 | - } |
|
159 | + switch ($event['name']) { |
|
160 | + case 'connect': |
|
161 | + $this->log('* Connected to ' . $event['data']); |
|
162 | + break; |
|
163 | + case 'sentHeaders': |
|
164 | + $headers = explode("\r\n", $event['data']); |
|
165 | + array_pop($headers); |
|
166 | + foreach ($headers as $header) { |
|
167 | + $this->log('> ' . $header); |
|
168 | + } |
|
169 | + break; |
|
170 | + case 'sentBodyPart': |
|
171 | + $this->log('> ' . $event['data'] . ' byte(s) sent'); |
|
172 | + break; |
|
173 | + case 'receivedHeaders': |
|
174 | + $this->log(sprintf('< HTTP/%s %s %s', |
|
175 | + $event['data']->getVersion(), |
|
176 | + $event['data']->getStatus(), |
|
177 | + $event['data']->getReasonPhrase())); |
|
178 | + $headers = $event['data']->getHeader(); |
|
179 | + foreach ($headers as $key => $val) { |
|
180 | + $this->log('< ' . $key . ': ' . $val); |
|
181 | + } |
|
182 | + $this->log('< '); |
|
183 | + break; |
|
184 | + case 'receivedBody': |
|
185 | + $this->log($event['data']->getBody()); |
|
186 | + break; |
|
187 | + case 'disconnect': |
|
188 | + $this->log('* Disconnected'); |
|
189 | + break; |
|
190 | + } |
|
191 | + } |
|
192 | 192 | |
193 | - // }}} |
|
194 | - // log() {{{ |
|
193 | + // }}} |
|
194 | + // log() {{{ |
|
195 | 195 | |
196 | - /** |
|
197 | - * Logs the given message to the configured target. |
|
198 | - * |
|
199 | - * @param string $message Message to display |
|
200 | - * |
|
201 | - * @return void |
|
202 | - */ |
|
203 | - protected function log($message) |
|
204 | - { |
|
205 | - if ($this->target instanceof Log) { |
|
206 | - $this->target->debug($message); |
|
207 | - } elseif (is_resource($this->target)) { |
|
208 | - fwrite($this->target, $message . "\r\n"); |
|
209 | - } |
|
210 | - } |
|
196 | + /** |
|
197 | + * Logs the given message to the configured target. |
|
198 | + * |
|
199 | + * @param string $message Message to display |
|
200 | + * |
|
201 | + * @return void |
|
202 | + */ |
|
203 | + protected function log($message) |
|
204 | + { |
|
205 | + if ($this->target instanceof Log) { |
|
206 | + $this->target->debug($message); |
|
207 | + } elseif (is_resource($this->target)) { |
|
208 | + fwrite($this->target, $message . "\r\n"); |
|
209 | + } |
|
210 | + } |
|
211 | 211 | |
212 | - // }}} |
|
212 | + // }}} |
|
213 | 213 | } |
214 | 214 | |
215 | 215 | ?> |
216 | 216 | \ No newline at end of file |
@@ -158,17 +158,17 @@ discard block |
||
158 | 158 | |
159 | 159 | switch ($event['name']) { |
160 | 160 | case 'connect': |
161 | - $this->log('* Connected to ' . $event['data']); |
|
161 | + $this->log('* Connected to '.$event['data']); |
|
162 | 162 | break; |
163 | 163 | case 'sentHeaders': |
164 | 164 | $headers = explode("\r\n", $event['data']); |
165 | 165 | array_pop($headers); |
166 | 166 | foreach ($headers as $header) { |
167 | - $this->log('> ' . $header); |
|
167 | + $this->log('> '.$header); |
|
168 | 168 | } |
169 | 169 | break; |
170 | 170 | case 'sentBodyPart': |
171 | - $this->log('> ' . $event['data'] . ' byte(s) sent'); |
|
171 | + $this->log('> '.$event['data'].' byte(s) sent'); |
|
172 | 172 | break; |
173 | 173 | case 'receivedHeaders': |
174 | 174 | $this->log(sprintf('< HTTP/%s %s %s', |
@@ -177,7 +177,7 @@ discard block |
||
177 | 177 | $event['data']->getReasonPhrase())); |
178 | 178 | $headers = $event['data']->getHeader(); |
179 | 179 | foreach ($headers as $key => $val) { |
180 | - $this->log('< ' . $key . ': ' . $val); |
|
180 | + $this->log('< '.$key.': '.$val); |
|
181 | 181 | } |
182 | 182 | $this->log('< '); |
183 | 183 | break; |
@@ -205,7 +205,7 @@ discard block |
||
205 | 205 | if ($this->target instanceof Log) { |
206 | 206 | $this->target->debug($message); |
207 | 207 | } elseif (is_resource($this->target)) { |
208 | - fwrite($this->target, $message . "\r\n"); |
|
208 | + fwrite($this->target, $message."\r\n"); |
|
209 | 209 | } |
210 | 210 | } |
211 | 211 |
@@ -79,33 +79,33 @@ discard block |
||
79 | 79 | * HTTP protocol version (e.g. 1.0, 1.1) |
80 | 80 | * @var string |
81 | 81 | */ |
82 | - protected $version; |
|
82 | + protected $version; |
|
83 | 83 | |
84 | 84 | /** |
85 | 85 | * Status code |
86 | 86 | * @var integer |
87 | 87 | * @link http://tools.ietf.org/html/rfc2616#section-6.1.1 |
88 | 88 | */ |
89 | - protected $code; |
|
89 | + protected $code; |
|
90 | 90 | |
91 | 91 | /** |
92 | 92 | * Reason phrase |
93 | 93 | * @var string |
94 | 94 | * @link http://tools.ietf.org/html/rfc2616#section-6.1.1 |
95 | 95 | */ |
96 | - protected $reasonPhrase; |
|
96 | + protected $reasonPhrase; |
|
97 | 97 | |
98 | 98 | /** |
99 | 99 | * Associative array of response headers |
100 | 100 | * @var array |
101 | 101 | */ |
102 | - protected $headers = array(); |
|
102 | + protected $headers = array(); |
|
103 | 103 | |
104 | 104 | /** |
105 | 105 | * Cookies set in the response |
106 | 106 | * @var array |
107 | 107 | */ |
108 | - protected $cookies = array(); |
|
108 | + protected $cookies = array(); |
|
109 | 109 | |
110 | 110 | /** |
111 | 111 | * Name of last header processed by parseHederLine() |
@@ -114,13 +114,13 @@ discard block |
||
114 | 114 | * |
115 | 115 | * @var string |
116 | 116 | */ |
117 | - protected $lastHeader = null; |
|
117 | + protected $lastHeader = null; |
|
118 | 118 | |
119 | 119 | /** |
120 | 120 | * Response body |
121 | 121 | * @var string |
122 | 122 | */ |
123 | - protected $body = ''; |
|
123 | + protected $body = ''; |
|
124 | 124 | |
125 | 125 | /** |
126 | 126 | * Whether the body is still encoded by Content-Encoding |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | * |
131 | 131 | * @var bool |
132 | 132 | */ |
133 | - protected $bodyEncoded; |
|
133 | + protected $bodyEncoded; |
|
134 | 134 | |
135 | 135 | /** |
136 | 136 | * Associative array of HTTP status code / reason phrase. |
@@ -138,64 +138,64 @@ discard block |
||
138 | 138 | * @var array |
139 | 139 | * @link http://tools.ietf.org/html/rfc2616#section-10 |
140 | 140 | */ |
141 | - protected static $phrases = array( |
|
142 | - |
|
143 | - // 1xx: Informational - Request received, continuing process |
|
144 | - 100 => 'Continue', |
|
145 | - 101 => 'Switching Protocols', |
|
146 | - |
|
147 | - // 2xx: Success - The action was successfully received, understood and |
|
148 | - // accepted |
|
149 | - 200 => 'OK', |
|
150 | - 201 => 'Created', |
|
151 | - 202 => 'Accepted', |
|
152 | - 203 => 'Non-Authoritative Information', |
|
153 | - 204 => 'No Content', |
|
154 | - 205 => 'Reset Content', |
|
155 | - 206 => 'Partial Content', |
|
156 | - |
|
157 | - // 3xx: Redirection - Further action must be taken in order to complete |
|
158 | - // the request |
|
159 | - 300 => 'Multiple Choices', |
|
160 | - 301 => 'Moved Permanently', |
|
161 | - 302 => 'Found', // 1.1 |
|
162 | - 303 => 'See Other', |
|
163 | - 304 => 'Not Modified', |
|
164 | - 305 => 'Use Proxy', |
|
165 | - 307 => 'Temporary Redirect', |
|
166 | - |
|
167 | - // 4xx: Client Error - The request contains bad syntax or cannot be |
|
168 | - // fulfilled |
|
169 | - 400 => 'Bad Request', |
|
170 | - 401 => 'Unauthorized', |
|
171 | - 402 => 'Payment Required', |
|
172 | - 403 => 'Forbidden', |
|
173 | - 404 => 'Not Found', |
|
174 | - 405 => 'Method Not Allowed', |
|
175 | - 406 => 'Not Acceptable', |
|
176 | - 407 => 'Proxy Authentication Required', |
|
177 | - 408 => 'Request Timeout', |
|
178 | - 409 => 'Conflict', |
|
179 | - 410 => 'Gone', |
|
180 | - 411 => 'Length Required', |
|
181 | - 412 => 'Precondition Failed', |
|
182 | - 413 => 'Request Entity Too Large', |
|
183 | - 414 => 'Request-URI Too Long', |
|
184 | - 415 => 'Unsupported Media Type', |
|
185 | - 416 => 'Requested Range Not Satisfiable', |
|
186 | - 417 => 'Expectation Failed', |
|
187 | - |
|
188 | - // 5xx: Server Error - The server failed to fulfill an apparently |
|
189 | - // valid request |
|
190 | - 500 => 'Internal Server Error', |
|
191 | - 501 => 'Not Implemented', |
|
192 | - 502 => 'Bad Gateway', |
|
193 | - 503 => 'Service Unavailable', |
|
194 | - 504 => 'Gateway Timeout', |
|
195 | - 505 => 'HTTP Version Not Supported', |
|
196 | - 509 => 'Bandwidth Limit Exceeded', |
|
197 | - |
|
198 | - ); |
|
141 | + protected static $phrases = array( |
|
142 | + |
|
143 | + // 1xx: Informational - Request received, continuing process |
|
144 | + 100 => 'Continue', |
|
145 | + 101 => 'Switching Protocols', |
|
146 | + |
|
147 | + // 2xx: Success - The action was successfully received, understood and |
|
148 | + // accepted |
|
149 | + 200 => 'OK', |
|
150 | + 201 => 'Created', |
|
151 | + 202 => 'Accepted', |
|
152 | + 203 => 'Non-Authoritative Information', |
|
153 | + 204 => 'No Content', |
|
154 | + 205 => 'Reset Content', |
|
155 | + 206 => 'Partial Content', |
|
156 | + |
|
157 | + // 3xx: Redirection - Further action must be taken in order to complete |
|
158 | + // the request |
|
159 | + 300 => 'Multiple Choices', |
|
160 | + 301 => 'Moved Permanently', |
|
161 | + 302 => 'Found', // 1.1 |
|
162 | + 303 => 'See Other', |
|
163 | + 304 => 'Not Modified', |
|
164 | + 305 => 'Use Proxy', |
|
165 | + 307 => 'Temporary Redirect', |
|
166 | + |
|
167 | + // 4xx: Client Error - The request contains bad syntax or cannot be |
|
168 | + // fulfilled |
|
169 | + 400 => 'Bad Request', |
|
170 | + 401 => 'Unauthorized', |
|
171 | + 402 => 'Payment Required', |
|
172 | + 403 => 'Forbidden', |
|
173 | + 404 => 'Not Found', |
|
174 | + 405 => 'Method Not Allowed', |
|
175 | + 406 => 'Not Acceptable', |
|
176 | + 407 => 'Proxy Authentication Required', |
|
177 | + 408 => 'Request Timeout', |
|
178 | + 409 => 'Conflict', |
|
179 | + 410 => 'Gone', |
|
180 | + 411 => 'Length Required', |
|
181 | + 412 => 'Precondition Failed', |
|
182 | + 413 => 'Request Entity Too Large', |
|
183 | + 414 => 'Request-URI Too Long', |
|
184 | + 415 => 'Unsupported Media Type', |
|
185 | + 416 => 'Requested Range Not Satisfiable', |
|
186 | + 417 => 'Expectation Failed', |
|
187 | + |
|
188 | + // 5xx: Server Error - The server failed to fulfill an apparently |
|
189 | + // valid request |
|
190 | + 500 => 'Internal Server Error', |
|
191 | + 501 => 'Not Implemented', |
|
192 | + 502 => 'Bad Gateway', |
|
193 | + 503 => 'Service Unavailable', |
|
194 | + 504 => 'Gateway Timeout', |
|
195 | + 505 => 'HTTP Version Not Supported', |
|
196 | + 509 => 'Bandwidth Limit Exceeded', |
|
197 | + |
|
198 | + ); |
|
199 | 199 | |
200 | 200 | /** |
201 | 201 | * Constructor, parses the response status line |
@@ -204,20 +204,20 @@ discard block |
||
204 | 204 | * @param bool Whether body is still encoded by Content-Encoding |
205 | 205 | * @throws HTTP_Request2_Exception if status line is invalid according to spec |
206 | 206 | */ |
207 | - public function __construct($statusLine, $bodyEncoded = true) |
|
208 | - { |
|
209 | - if (!preg_match('!^HTTP/(\d\.\d) (\d{3})(?: (.+))?!', $statusLine, $m)) { |
|
210 | - throw new HTTP_Request2_Exception("Malformed response: {$statusLine}"); |
|
211 | - } |
|
212 | - $this->version = $m[1]; |
|
213 | - $this->code = intval($m[2]); |
|
214 | - if (!empty($m[3])) { |
|
215 | - $this->reasonPhrase = trim($m[3]); |
|
216 | - } elseif (!empty(self::$phrases[$this->code])) { |
|
217 | - $this->reasonPhrase = self::$phrases[$this->code]; |
|
218 | - } |
|
219 | - $this->bodyEncoded = (bool)$bodyEncoded; |
|
220 | - } |
|
207 | + public function __construct($statusLine, $bodyEncoded = true) |
|
208 | + { |
|
209 | + if (!preg_match('!^HTTP/(\d\.\d) (\d{3})(?: (.+))?!', $statusLine, $m)) { |
|
210 | + throw new HTTP_Request2_Exception("Malformed response: {$statusLine}"); |
|
211 | + } |
|
212 | + $this->version = $m[1]; |
|
213 | + $this->code = intval($m[2]); |
|
214 | + if (!empty($m[3])) { |
|
215 | + $this->reasonPhrase = trim($m[3]); |
|
216 | + } elseif (!empty(self::$phrases[$this->code])) { |
|
217 | + $this->reasonPhrase = self::$phrases[$this->code]; |
|
218 | + } |
|
219 | + $this->bodyEncoded = (bool)$bodyEncoded; |
|
220 | + } |
|
221 | 221 | |
222 | 222 | /** |
223 | 223 | * Parses the line from HTTP response filling $headers array |
@@ -229,51 +229,51 @@ discard block |
||
229 | 229 | * |
230 | 230 | * @param string Line from HTTP response |
231 | 231 | */ |
232 | - public function parseHeaderLine($headerLine) |
|
233 | - { |
|
234 | - $headerLine = trim($headerLine, "\r\n"); |
|
235 | - |
|
236 | - // empty string signals the end of headers, process the received ones |
|
237 | - if ('' == $headerLine) { |
|
238 | - if (!empty($this->headers['set-cookie'])) { |
|
239 | - $cookies = is_array($this->headers['set-cookie'])? |
|
240 | - $this->headers['set-cookie']: |
|
241 | - array($this->headers['set-cookie']); |
|
242 | - foreach ($cookies as $cookieString) { |
|
243 | - $this->parseCookie($cookieString); |
|
244 | - } |
|
245 | - unset($this->headers['set-cookie']); |
|
246 | - } |
|
247 | - foreach (array_keys($this->headers) as $k) { |
|
248 | - if (is_array($this->headers[$k])) { |
|
249 | - $this->headers[$k] = implode(', ', $this->headers[$k]); |
|
250 | - } |
|
251 | - } |
|
252 | - |
|
253 | - // string of the form header-name: header value |
|
254 | - } elseif (preg_match('!^([^\x00-\x1f\x7f-\xff()<>@,;:\\\\"/\[\]?={}\s]+):(.+)$!', $headerLine, $m)) { |
|
255 | - $name = strtolower($m[1]); |
|
256 | - $value = trim($m[2]); |
|
257 | - if (empty($this->headers[$name])) { |
|
258 | - $this->headers[$name] = $value; |
|
259 | - } else { |
|
260 | - if (!is_array($this->headers[$name])) { |
|
261 | - $this->headers[$name] = array($this->headers[$name]); |
|
262 | - } |
|
263 | - $this->headers[$name][] = $value; |
|
264 | - } |
|
265 | - $this->lastHeader = $name; |
|
266 | - |
|
267 | - // continuation of a previous header |
|
268 | - } elseif (preg_match('!^\s+(.+)$!', $headerLine, $m) && $this->lastHeader) { |
|
269 | - if (!is_array($this->headers[$this->lastHeader])) { |
|
270 | - $this->headers[$this->lastHeader] .= ' ' . trim($m[1]); |
|
271 | - } else { |
|
272 | - $key = count($this->headers[$this->lastHeader]) - 1; |
|
273 | - $this->headers[$this->lastHeader][$key] .= ' ' . trim($m[1]); |
|
274 | - } |
|
275 | - } |
|
276 | - } |
|
232 | + public function parseHeaderLine($headerLine) |
|
233 | + { |
|
234 | + $headerLine = trim($headerLine, "\r\n"); |
|
235 | + |
|
236 | + // empty string signals the end of headers, process the received ones |
|
237 | + if ('' == $headerLine) { |
|
238 | + if (!empty($this->headers['set-cookie'])) { |
|
239 | + $cookies = is_array($this->headers['set-cookie'])? |
|
240 | + $this->headers['set-cookie']: |
|
241 | + array($this->headers['set-cookie']); |
|
242 | + foreach ($cookies as $cookieString) { |
|
243 | + $this->parseCookie($cookieString); |
|
244 | + } |
|
245 | + unset($this->headers['set-cookie']); |
|
246 | + } |
|
247 | + foreach (array_keys($this->headers) as $k) { |
|
248 | + if (is_array($this->headers[$k])) { |
|
249 | + $this->headers[$k] = implode(', ', $this->headers[$k]); |
|
250 | + } |
|
251 | + } |
|
252 | + |
|
253 | + // string of the form header-name: header value |
|
254 | + } elseif (preg_match('!^([^\x00-\x1f\x7f-\xff()<>@,;:\\\\"/\[\]?={}\s]+):(.+)$!', $headerLine, $m)) { |
|
255 | + $name = strtolower($m[1]); |
|
256 | + $value = trim($m[2]); |
|
257 | + if (empty($this->headers[$name])) { |
|
258 | + $this->headers[$name] = $value; |
|
259 | + } else { |
|
260 | + if (!is_array($this->headers[$name])) { |
|
261 | + $this->headers[$name] = array($this->headers[$name]); |
|
262 | + } |
|
263 | + $this->headers[$name][] = $value; |
|
264 | + } |
|
265 | + $this->lastHeader = $name; |
|
266 | + |
|
267 | + // continuation of a previous header |
|
268 | + } elseif (preg_match('!^\s+(.+)$!', $headerLine, $m) && $this->lastHeader) { |
|
269 | + if (!is_array($this->headers[$this->lastHeader])) { |
|
270 | + $this->headers[$this->lastHeader] .= ' ' . trim($m[1]); |
|
271 | + } else { |
|
272 | + $key = count($this->headers[$this->lastHeader]) - 1; |
|
273 | + $this->headers[$this->lastHeader][$key] .= ' ' . trim($m[1]); |
|
274 | + } |
|
275 | + } |
|
276 | + } |
|
277 | 277 | |
278 | 278 | /** |
279 | 279 | * Parses a Set-Cookie header to fill $cookies array |
@@ -281,86 +281,86 @@ discard block |
||
281 | 281 | * @param string value of Set-Cookie header |
282 | 282 | * @link http://web.archive.org/web/20080331104521/http://cgi.netscape.com/newsref/std/cookie_spec.html |
283 | 283 | */ |
284 | - protected function parseCookie($cookieString) |
|
285 | - { |
|
286 | - $cookie = array( |
|
287 | - 'expires' => null, |
|
288 | - 'domain' => null, |
|
289 | - 'path' => null, |
|
290 | - 'secure' => false |
|
291 | - ); |
|
292 | - |
|
293 | - // Only a name=value pair |
|
294 | - if (!strpos($cookieString, ';')) { |
|
295 | - $pos = strpos($cookieString, '='); |
|
296 | - $cookie['name'] = trim(substr($cookieString, 0, $pos)); |
|
297 | - $cookie['value'] = trim(substr($cookieString, $pos + 1)); |
|
298 | - |
|
299 | - // Some optional parameters are supplied |
|
300 | - } else { |
|
301 | - $elements = explode(';', $cookieString); |
|
302 | - $pos = strpos($elements[0], '='); |
|
303 | - $cookie['name'] = trim(substr($elements[0], 0, $pos)); |
|
304 | - $cookie['value'] = trim(substr($elements[0], $pos + 1)); |
|
305 | - |
|
306 | - for ($i = 1; $i < count($elements); $i++) { |
|
307 | - if (false === strpos($elements[$i], '=')) { |
|
308 | - $elName = trim($elements[$i]); |
|
309 | - $elValue = null; |
|
310 | - } else { |
|
311 | - list ($elName, $elValue) = array_map('trim', explode('=', $elements[$i])); |
|
312 | - } |
|
313 | - $elName = strtolower($elName); |
|
314 | - if ('secure' == $elName) { |
|
315 | - $cookie['secure'] = true; |
|
316 | - } elseif ('expires' == $elName) { |
|
317 | - $cookie['expires'] = str_replace('"', '', $elValue); |
|
318 | - } elseif ('path' == $elName || 'domain' == $elName) { |
|
319 | - $cookie[$elName] = urldecode($elValue); |
|
320 | - } else { |
|
321 | - $cookie[$elName] = $elValue; |
|
322 | - } |
|
323 | - } |
|
324 | - } |
|
325 | - $this->cookies[] = $cookie; |
|
326 | - } |
|
284 | + protected function parseCookie($cookieString) |
|
285 | + { |
|
286 | + $cookie = array( |
|
287 | + 'expires' => null, |
|
288 | + 'domain' => null, |
|
289 | + 'path' => null, |
|
290 | + 'secure' => false |
|
291 | + ); |
|
292 | + |
|
293 | + // Only a name=value pair |
|
294 | + if (!strpos($cookieString, ';')) { |
|
295 | + $pos = strpos($cookieString, '='); |
|
296 | + $cookie['name'] = trim(substr($cookieString, 0, $pos)); |
|
297 | + $cookie['value'] = trim(substr($cookieString, $pos + 1)); |
|
298 | + |
|
299 | + // Some optional parameters are supplied |
|
300 | + } else { |
|
301 | + $elements = explode(';', $cookieString); |
|
302 | + $pos = strpos($elements[0], '='); |
|
303 | + $cookie['name'] = trim(substr($elements[0], 0, $pos)); |
|
304 | + $cookie['value'] = trim(substr($elements[0], $pos + 1)); |
|
305 | + |
|
306 | + for ($i = 1; $i < count($elements); $i++) { |
|
307 | + if (false === strpos($elements[$i], '=')) { |
|
308 | + $elName = trim($elements[$i]); |
|
309 | + $elValue = null; |
|
310 | + } else { |
|
311 | + list ($elName, $elValue) = array_map('trim', explode('=', $elements[$i])); |
|
312 | + } |
|
313 | + $elName = strtolower($elName); |
|
314 | + if ('secure' == $elName) { |
|
315 | + $cookie['secure'] = true; |
|
316 | + } elseif ('expires' == $elName) { |
|
317 | + $cookie['expires'] = str_replace('"', '', $elValue); |
|
318 | + } elseif ('path' == $elName || 'domain' == $elName) { |
|
319 | + $cookie[$elName] = urldecode($elValue); |
|
320 | + } else { |
|
321 | + $cookie[$elName] = $elValue; |
|
322 | + } |
|
323 | + } |
|
324 | + } |
|
325 | + $this->cookies[] = $cookie; |
|
326 | + } |
|
327 | 327 | |
328 | 328 | /** |
329 | 329 | * Appends a string to the response body |
330 | 330 | * @param string |
331 | 331 | */ |
332 | - public function appendBody($bodyChunk) |
|
333 | - { |
|
334 | - $this->body .= $bodyChunk; |
|
335 | - } |
|
332 | + public function appendBody($bodyChunk) |
|
333 | + { |
|
334 | + $this->body .= $bodyChunk; |
|
335 | + } |
|
336 | 336 | |
337 | 337 | /** |
338 | 338 | * Returns the status code |
339 | 339 | * @return integer |
340 | 340 | */ |
341 | - public function getStatus() |
|
342 | - { |
|
343 | - return $this->code; |
|
344 | - } |
|
341 | + public function getStatus() |
|
342 | + { |
|
343 | + return $this->code; |
|
344 | + } |
|
345 | 345 | |
346 | 346 | /** |
347 | 347 | * Returns the reason phrase |
348 | 348 | * @return string |
349 | 349 | */ |
350 | - public function getReasonPhrase() |
|
351 | - { |
|
352 | - return $this->reasonPhrase; |
|
353 | - } |
|
350 | + public function getReasonPhrase() |
|
351 | + { |
|
352 | + return $this->reasonPhrase; |
|
353 | + } |
|
354 | 354 | |
355 | 355 | /** |
356 | 356 | * Whether response is a redirect that can be automatically handled by HTTP_Request2 |
357 | 357 | * @return bool |
358 | 358 | */ |
359 | - public function isRedirect() |
|
360 | - { |
|
361 | - return in_array($this->code, array(300, 301, 302, 303, 307)) |
|
362 | - && isset($this->headers['location']); |
|
363 | - } |
|
359 | + public function isRedirect() |
|
360 | + { |
|
361 | + return in_array($this->code, array(300, 301, 302, 303, 307)) |
|
362 | + && isset($this->headers['location']); |
|
363 | + } |
|
364 | 364 | |
365 | 365 | /** |
366 | 366 | * Returns either the named header or all response headers |
@@ -370,25 +370,25 @@ discard block |
||
370 | 370 | * not present), array of all response headers if |
371 | 371 | * $headerName is null |
372 | 372 | */ |
373 | - public function getHeader($headerName = null) |
|
374 | - { |
|
375 | - if (null === $headerName) { |
|
376 | - return $this->headers; |
|
377 | - } else { |
|
378 | - $headerName = strtolower($headerName); |
|
379 | - return isset($this->headers[$headerName])? $this->headers[$headerName]: null; |
|
380 | - } |
|
381 | - } |
|
373 | + public function getHeader($headerName = null) |
|
374 | + { |
|
375 | + if (null === $headerName) { |
|
376 | + return $this->headers; |
|
377 | + } else { |
|
378 | + $headerName = strtolower($headerName); |
|
379 | + return isset($this->headers[$headerName])? $this->headers[$headerName]: null; |
|
380 | + } |
|
381 | + } |
|
382 | 382 | |
383 | 383 | /** |
384 | 384 | * Returns cookies set in response |
385 | 385 | * |
386 | 386 | * @return array |
387 | 387 | */ |
388 | - public function getCookies() |
|
389 | - { |
|
390 | - return $this->cookies; |
|
391 | - } |
|
388 | + public function getCookies() |
|
389 | + { |
|
390 | + return $this->cookies; |
|
391 | + } |
|
392 | 392 | |
393 | 393 | /** |
394 | 394 | * Returns the body of the response |
@@ -396,49 +396,49 @@ discard block |
||
396 | 396 | * @return string |
397 | 397 | * @throws HTTP_Request2_Exception if body cannot be decoded |
398 | 398 | */ |
399 | - public function getBody() |
|
400 | - { |
|
401 | - if (!$this->bodyEncoded || |
|
402 | - !in_array(strtolower($this->getHeader('content-encoding')), array('gzip', 'deflate')) |
|
403 | - ) { |
|
404 | - return $this->body; |
|
405 | - |
|
406 | - } else { |
|
407 | - if (extension_loaded('mbstring') && (2 & ini_get('mbstring.func_overload'))) { |
|
408 | - $oldEncoding = mb_internal_encoding(); |
|
409 | - mb_internal_encoding('iso-8859-1'); |
|
410 | - } |
|
411 | - |
|
412 | - try { |
|
413 | - switch (strtolower($this->getHeader('content-encoding'))) { |
|
414 | - case 'gzip': |
|
415 | - $decoded = self::decodeGzip($this->body); |
|
416 | - break; |
|
417 | - case 'deflate': |
|
418 | - $decoded = self::decodeDeflate($this->body); |
|
419 | - } |
|
420 | - } catch (Exception $e) { |
|
421 | - } |
|
422 | - |
|
423 | - if (!empty($oldEncoding)) { |
|
424 | - mb_internal_encoding($oldEncoding); |
|
425 | - } |
|
426 | - if (!empty($e)) { |
|
427 | - throw $e; |
|
428 | - } |
|
429 | - return $decoded; |
|
430 | - } |
|
431 | - } |
|
399 | + public function getBody() |
|
400 | + { |
|
401 | + if (!$this->bodyEncoded || |
|
402 | + !in_array(strtolower($this->getHeader('content-encoding')), array('gzip', 'deflate')) |
|
403 | + ) { |
|
404 | + return $this->body; |
|
405 | + |
|
406 | + } else { |
|
407 | + if (extension_loaded('mbstring') && (2 & ini_get('mbstring.func_overload'))) { |
|
408 | + $oldEncoding = mb_internal_encoding(); |
|
409 | + mb_internal_encoding('iso-8859-1'); |
|
410 | + } |
|
411 | + |
|
412 | + try { |
|
413 | + switch (strtolower($this->getHeader('content-encoding'))) { |
|
414 | + case 'gzip': |
|
415 | + $decoded = self::decodeGzip($this->body); |
|
416 | + break; |
|
417 | + case 'deflate': |
|
418 | + $decoded = self::decodeDeflate($this->body); |
|
419 | + } |
|
420 | + } catch (Exception $e) { |
|
421 | + } |
|
422 | + |
|
423 | + if (!empty($oldEncoding)) { |
|
424 | + mb_internal_encoding($oldEncoding); |
|
425 | + } |
|
426 | + if (!empty($e)) { |
|
427 | + throw $e; |
|
428 | + } |
|
429 | + return $decoded; |
|
430 | + } |
|
431 | + } |
|
432 | 432 | |
433 | 433 | /** |
434 | 434 | * Get the HTTP version of the response |
435 | 435 | * |
436 | 436 | * @return string |
437 | 437 | */ |
438 | - public function getVersion() |
|
439 | - { |
|
440 | - return $this->version; |
|
441 | - } |
|
438 | + public function getVersion() |
|
439 | + { |
|
440 | + return $this->version; |
|
441 | + } |
|
442 | 442 | |
443 | 443 | /** |
444 | 444 | * Decodes the message-body encoded by gzip |
@@ -452,89 +452,89 @@ discard block |
||
452 | 452 | * @throws HTTP_Request2_Exception |
453 | 453 | * @link http://tools.ietf.org/html/rfc1952 |
454 | 454 | */ |
455 | - public static function decodeGzip($data) |
|
456 | - { |
|
457 | - $length = strlen($data); |
|
458 | - // If it doesn't look like gzip-encoded data, don't bother |
|
459 | - if (18 > $length || strcmp(substr($data, 0, 2), "\x1f\x8b")) { |
|
460 | - return $data; |
|
461 | - } |
|
462 | - if (!function_exists('gzinflate')) { |
|
463 | - throw new HTTP_Request2_Exception('Unable to decode body: gzip extension not available'); |
|
464 | - } |
|
465 | - $method = ord(substr($data, 2, 1)); |
|
466 | - if (8 != $method) { |
|
467 | - throw new HTTP_Request2_Exception('Error parsing gzip header: unknown compression method'); |
|
468 | - } |
|
469 | - $flags = ord(substr($data, 3, 1)); |
|
470 | - if ($flags & 224) { |
|
471 | - throw new HTTP_Request2_Exception('Error parsing gzip header: reserved bits are set'); |
|
472 | - } |
|
473 | - |
|
474 | - // header is 10 bytes minimum. may be longer, though. |
|
475 | - $headerLength = 10; |
|
476 | - // extra fields, need to skip 'em |
|
477 | - if ($flags & 4) { |
|
478 | - if ($length - $headerLength - 2 < 8) { |
|
479 | - throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
480 | - } |
|
481 | - $extraLength = unpack('v', substr($data, 10, 2)); |
|
482 | - if ($length - $headerLength - 2 - $extraLength[1] < 8) { |
|
483 | - throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
484 | - } |
|
485 | - $headerLength += $extraLength[1] + 2; |
|
486 | - } |
|
487 | - // file name, need to skip that |
|
488 | - if ($flags & 8) { |
|
489 | - if ($length - $headerLength - 1 < 8) { |
|
490 | - throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
491 | - } |
|
492 | - $filenameLength = strpos(substr($data, $headerLength), chr(0)); |
|
493 | - if (false === $filenameLength || $length - $headerLength - $filenameLength - 1 < 8) { |
|
494 | - throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
495 | - } |
|
496 | - $headerLength += $filenameLength + 1; |
|
497 | - } |
|
498 | - // comment, need to skip that also |
|
499 | - if ($flags & 16) { |
|
500 | - if ($length - $headerLength - 1 < 8) { |
|
501 | - throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
502 | - } |
|
503 | - $commentLength = strpos(substr($data, $headerLength), chr(0)); |
|
504 | - if (false === $commentLength || $length - $headerLength - $commentLength - 1 < 8) { |
|
505 | - throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
506 | - } |
|
507 | - $headerLength += $commentLength + 1; |
|
508 | - } |
|
509 | - // have a CRC for header. let's check |
|
510 | - if ($flags & 2) { |
|
511 | - if ($length - $headerLength - 2 < 8) { |
|
512 | - throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
513 | - } |
|
514 | - $crcReal = 0xffff & crc32(substr($data, 0, $headerLength)); |
|
515 | - $crcStored = unpack('v', substr($data, $headerLength, 2)); |
|
516 | - if ($crcReal != $crcStored[1]) { |
|
517 | - throw new HTTP_Request2_Exception('Header CRC check failed'); |
|
518 | - } |
|
519 | - $headerLength += 2; |
|
520 | - } |
|
521 | - // unpacked data CRC and size at the end of encoded data |
|
522 | - $tmp = unpack('V2', substr($data, -8)); |
|
523 | - $dataCrc = $tmp[1]; |
|
524 | - $dataSize = $tmp[2]; |
|
525 | - |
|
526 | - // finally, call the gzinflate() function |
|
527 | - // don't pass $dataSize to gzinflate, see bugs #13135, #14370 |
|
528 | - $unpacked = gzinflate(substr($data, $headerLength, -8)); |
|
529 | - if (false === $unpacked) { |
|
530 | - throw new HTTP_Request2_Exception('gzinflate() call failed'); |
|
531 | - } elseif ($dataSize != strlen($unpacked)) { |
|
532 | - throw new HTTP_Request2_Exception('Data size check failed'); |
|
533 | - } elseif ((0xffffffff & $dataCrc) != (0xffffffff & crc32($unpacked))) { |
|
534 | - throw new HTTP_Request2_Exception('Data CRC check failed'); |
|
535 | - } |
|
536 | - return $unpacked; |
|
537 | - } |
|
455 | + public static function decodeGzip($data) |
|
456 | + { |
|
457 | + $length = strlen($data); |
|
458 | + // If it doesn't look like gzip-encoded data, don't bother |
|
459 | + if (18 > $length || strcmp(substr($data, 0, 2), "\x1f\x8b")) { |
|
460 | + return $data; |
|
461 | + } |
|
462 | + if (!function_exists('gzinflate')) { |
|
463 | + throw new HTTP_Request2_Exception('Unable to decode body: gzip extension not available'); |
|
464 | + } |
|
465 | + $method = ord(substr($data, 2, 1)); |
|
466 | + if (8 != $method) { |
|
467 | + throw new HTTP_Request2_Exception('Error parsing gzip header: unknown compression method'); |
|
468 | + } |
|
469 | + $flags = ord(substr($data, 3, 1)); |
|
470 | + if ($flags & 224) { |
|
471 | + throw new HTTP_Request2_Exception('Error parsing gzip header: reserved bits are set'); |
|
472 | + } |
|
473 | + |
|
474 | + // header is 10 bytes minimum. may be longer, though. |
|
475 | + $headerLength = 10; |
|
476 | + // extra fields, need to skip 'em |
|
477 | + if ($flags & 4) { |
|
478 | + if ($length - $headerLength - 2 < 8) { |
|
479 | + throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
480 | + } |
|
481 | + $extraLength = unpack('v', substr($data, 10, 2)); |
|
482 | + if ($length - $headerLength - 2 - $extraLength[1] < 8) { |
|
483 | + throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
484 | + } |
|
485 | + $headerLength += $extraLength[1] + 2; |
|
486 | + } |
|
487 | + // file name, need to skip that |
|
488 | + if ($flags & 8) { |
|
489 | + if ($length - $headerLength - 1 < 8) { |
|
490 | + throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
491 | + } |
|
492 | + $filenameLength = strpos(substr($data, $headerLength), chr(0)); |
|
493 | + if (false === $filenameLength || $length - $headerLength - $filenameLength - 1 < 8) { |
|
494 | + throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
495 | + } |
|
496 | + $headerLength += $filenameLength + 1; |
|
497 | + } |
|
498 | + // comment, need to skip that also |
|
499 | + if ($flags & 16) { |
|
500 | + if ($length - $headerLength - 1 < 8) { |
|
501 | + throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
502 | + } |
|
503 | + $commentLength = strpos(substr($data, $headerLength), chr(0)); |
|
504 | + if (false === $commentLength || $length - $headerLength - $commentLength - 1 < 8) { |
|
505 | + throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
506 | + } |
|
507 | + $headerLength += $commentLength + 1; |
|
508 | + } |
|
509 | + // have a CRC for header. let's check |
|
510 | + if ($flags & 2) { |
|
511 | + if ($length - $headerLength - 2 < 8) { |
|
512 | + throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
513 | + } |
|
514 | + $crcReal = 0xffff & crc32(substr($data, 0, $headerLength)); |
|
515 | + $crcStored = unpack('v', substr($data, $headerLength, 2)); |
|
516 | + if ($crcReal != $crcStored[1]) { |
|
517 | + throw new HTTP_Request2_Exception('Header CRC check failed'); |
|
518 | + } |
|
519 | + $headerLength += 2; |
|
520 | + } |
|
521 | + // unpacked data CRC and size at the end of encoded data |
|
522 | + $tmp = unpack('V2', substr($data, -8)); |
|
523 | + $dataCrc = $tmp[1]; |
|
524 | + $dataSize = $tmp[2]; |
|
525 | + |
|
526 | + // finally, call the gzinflate() function |
|
527 | + // don't pass $dataSize to gzinflate, see bugs #13135, #14370 |
|
528 | + $unpacked = gzinflate(substr($data, $headerLength, -8)); |
|
529 | + if (false === $unpacked) { |
|
530 | + throw new HTTP_Request2_Exception('gzinflate() call failed'); |
|
531 | + } elseif ($dataSize != strlen($unpacked)) { |
|
532 | + throw new HTTP_Request2_Exception('Data size check failed'); |
|
533 | + } elseif ((0xffffffff & $dataCrc) != (0xffffffff & crc32($unpacked))) { |
|
534 | + throw new HTTP_Request2_Exception('Data CRC check failed'); |
|
535 | + } |
|
536 | + return $unpacked; |
|
537 | + } |
|
538 | 538 | |
539 | 539 | /** |
540 | 540 | * Decodes the message-body encoded by deflate |
@@ -543,17 +543,17 @@ discard block |
||
543 | 543 | * @return string decoded data |
544 | 544 | * @throws HTTP_Request2_Exception |
545 | 545 | */ |
546 | - public static function decodeDeflate($data) |
|
547 | - { |
|
548 | - if (!function_exists('gzuncompress')) { |
|
549 | - throw new HTTP_Request2_Exception('Unable to decode body: gzip extension not available'); |
|
550 | - } |
|
551 | - // RFC 2616 defines 'deflate' encoding as zlib format from RFC 1950, |
|
552 | - // while many applications send raw deflate stream from RFC 1951. |
|
553 | - // We should check for presence of zlib header and use gzuncompress() or |
|
554 | - // gzinflate() as needed. See bug #15305 |
|
555 | - $header = unpack('n', substr($data, 0, 2)); |
|
556 | - return (0 == $header[1] % 31)? gzuncompress($data): gzinflate($data); |
|
557 | - } |
|
546 | + public static function decodeDeflate($data) |
|
547 | + { |
|
548 | + if (!function_exists('gzuncompress')) { |
|
549 | + throw new HTTP_Request2_Exception('Unable to decode body: gzip extension not available'); |
|
550 | + } |
|
551 | + // RFC 2616 defines 'deflate' encoding as zlib format from RFC 1950, |
|
552 | + // while many applications send raw deflate stream from RFC 1951. |
|
553 | + // We should check for presence of zlib header and use gzuncompress() or |
|
554 | + // gzinflate() as needed. See bug #15305 |
|
555 | + $header = unpack('n', substr($data, 0, 2)); |
|
556 | + return (0 == $header[1] % 31)? gzuncompress($data): gzinflate($data); |
|
557 | + } |
|
558 | 558 | } |
559 | 559 | ?> |
560 | 560 | \ No newline at end of file |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | // the request |
159 | 159 | 300 => 'Multiple Choices', |
160 | 160 | 301 => 'Moved Permanently', |
161 | - 302 => 'Found', // 1.1 |
|
161 | + 302 => 'Found', // 1.1 |
|
162 | 162 | 303 => 'See Other', |
163 | 163 | 304 => 'Not Modified', |
164 | 164 | 305 => 'Use Proxy', |
@@ -216,7 +216,7 @@ discard block |
||
216 | 216 | } elseif (!empty(self::$phrases[$this->code])) { |
217 | 217 | $this->reasonPhrase = self::$phrases[$this->code]; |
218 | 218 | } |
219 | - $this->bodyEncoded = (bool)$bodyEncoded; |
|
219 | + $this->bodyEncoded = (bool) $bodyEncoded; |
|
220 | 220 | } |
221 | 221 | |
222 | 222 | /** |
@@ -236,9 +236,8 @@ discard block |
||
236 | 236 | // empty string signals the end of headers, process the received ones |
237 | 237 | if ('' == $headerLine) { |
238 | 238 | if (!empty($this->headers['set-cookie'])) { |
239 | - $cookies = is_array($this->headers['set-cookie'])? |
|
240 | - $this->headers['set-cookie']: |
|
241 | - array($this->headers['set-cookie']); |
|
239 | + $cookies = is_array($this->headers['set-cookie']) ? |
|
240 | + $this->headers['set-cookie'] : array($this->headers['set-cookie']); |
|
242 | 241 | foreach ($cookies as $cookieString) { |
243 | 242 | $this->parseCookie($cookieString); |
244 | 243 | } |
@@ -267,10 +266,10 @@ discard block |
||
267 | 266 | // continuation of a previous header |
268 | 267 | } elseif (preg_match('!^\s+(.+)$!', $headerLine, $m) && $this->lastHeader) { |
269 | 268 | if (!is_array($this->headers[$this->lastHeader])) { |
270 | - $this->headers[$this->lastHeader] .= ' ' . trim($m[1]); |
|
269 | + $this->headers[$this->lastHeader] .= ' '.trim($m[1]); |
|
271 | 270 | } else { |
272 | 271 | $key = count($this->headers[$this->lastHeader]) - 1; |
273 | - $this->headers[$this->lastHeader][$key] .= ' ' . trim($m[1]); |
|
272 | + $this->headers[$this->lastHeader][$key] .= ' '.trim($m[1]); |
|
274 | 273 | } |
275 | 274 | } |
276 | 275 | } |
@@ -376,7 +375,7 @@ discard block |
||
376 | 375 | return $this->headers; |
377 | 376 | } else { |
378 | 377 | $headerName = strtolower($headerName); |
379 | - return isset($this->headers[$headerName])? $this->headers[$headerName]: null; |
|
378 | + return isset($this->headers[$headerName]) ? $this->headers[$headerName] : null; |
|
380 | 379 | } |
381 | 380 | } |
382 | 381 | |
@@ -553,7 +552,7 @@ discard block |
||
553 | 552 | // We should check for presence of zlib header and use gzuncompress() or |
554 | 553 | // gzinflate() as needed. See bug #15305 |
555 | 554 | $header = unpack('n', substr($data, 0, 2)); |
556 | - return (0 == $header[1] % 31)? gzuncompress($data): gzinflate($data); |
|
555 | + return (0 == $header[1] % 31) ? gzuncompress($data) : gzinflate($data); |
|
557 | 556 | } |
558 | 557 | } |
559 | 558 | ?> |
560 | 559 | \ No newline at end of file |
@@ -96,296 +96,296 @@ |
||
96 | 96 | */ |
97 | 97 | class PEAR_Exception extends Exception |
98 | 98 | { |
99 | - const OBSERVER_PRINT = -2; |
|
100 | - const OBSERVER_TRIGGER = -4; |
|
101 | - const OBSERVER_DIE = -8; |
|
102 | - protected $cause; |
|
103 | - private static $_observers = array(); |
|
104 | - private static $_uniqueid = 0; |
|
105 | - private $_trace; |
|
99 | + const OBSERVER_PRINT = -2; |
|
100 | + const OBSERVER_TRIGGER = -4; |
|
101 | + const OBSERVER_DIE = -8; |
|
102 | + protected $cause; |
|
103 | + private static $_observers = array(); |
|
104 | + private static $_uniqueid = 0; |
|
105 | + private $_trace; |
|
106 | 106 | |
107 | - /** |
|
108 | - * Supported signatures: |
|
109 | - * - PEAR_Exception(string $message); |
|
110 | - * - PEAR_Exception(string $message, int $code); |
|
111 | - * - PEAR_Exception(string $message, Exception $cause); |
|
112 | - * - PEAR_Exception(string $message, Exception $cause, int $code); |
|
113 | - * - PEAR_Exception(string $message, PEAR_Error $cause); |
|
114 | - * - PEAR_Exception(string $message, PEAR_Error $cause, int $code); |
|
115 | - * - PEAR_Exception(string $message, array $causes); |
|
116 | - * - PEAR_Exception(string $message, array $causes, int $code); |
|
117 | - * @param string exception message |
|
118 | - * @param int|Exception|PEAR_Error|array|null exception cause |
|
119 | - * @param int|null exception code or null |
|
120 | - */ |
|
121 | - public function __construct($message, $p2 = null, $p3 = null) |
|
122 | - { |
|
123 | - if (is_int($p2)) { |
|
124 | - $code = $p2; |
|
125 | - $this->cause = null; |
|
126 | - } elseif (is_object($p2) || is_array($p2)) { |
|
127 | - // using is_object allows both Exception and PEAR_Error |
|
128 | - if (is_object($p2) && !($p2 instanceof Exception)) { |
|
129 | - if (!class_exists('PEAR_Error') || !($p2 instanceof PEAR_Error)) { |
|
130 | - throw new PEAR_Exception('exception cause must be Exception, ' . |
|
131 | - 'array, or PEAR_Error'); |
|
132 | - } |
|
133 | - } |
|
134 | - $code = $p3; |
|
135 | - if (is_array($p2) && isset($p2['message'])) { |
|
136 | - // fix potential problem of passing in a single warning |
|
137 | - $p2 = array($p2); |
|
138 | - } |
|
139 | - $this->cause = $p2; |
|
140 | - } else { |
|
141 | - $code = null; |
|
142 | - $this->cause = null; |
|
143 | - } |
|
144 | - parent::__construct($message, $code); |
|
145 | - $this->signal(); |
|
146 | - } |
|
107 | + /** |
|
108 | + * Supported signatures: |
|
109 | + * - PEAR_Exception(string $message); |
|
110 | + * - PEAR_Exception(string $message, int $code); |
|
111 | + * - PEAR_Exception(string $message, Exception $cause); |
|
112 | + * - PEAR_Exception(string $message, Exception $cause, int $code); |
|
113 | + * - PEAR_Exception(string $message, PEAR_Error $cause); |
|
114 | + * - PEAR_Exception(string $message, PEAR_Error $cause, int $code); |
|
115 | + * - PEAR_Exception(string $message, array $causes); |
|
116 | + * - PEAR_Exception(string $message, array $causes, int $code); |
|
117 | + * @param string exception message |
|
118 | + * @param int|Exception|PEAR_Error|array|null exception cause |
|
119 | + * @param int|null exception code or null |
|
120 | + */ |
|
121 | + public function __construct($message, $p2 = null, $p3 = null) |
|
122 | + { |
|
123 | + if (is_int($p2)) { |
|
124 | + $code = $p2; |
|
125 | + $this->cause = null; |
|
126 | + } elseif (is_object($p2) || is_array($p2)) { |
|
127 | + // using is_object allows both Exception and PEAR_Error |
|
128 | + if (is_object($p2) && !($p2 instanceof Exception)) { |
|
129 | + if (!class_exists('PEAR_Error') || !($p2 instanceof PEAR_Error)) { |
|
130 | + throw new PEAR_Exception('exception cause must be Exception, ' . |
|
131 | + 'array, or PEAR_Error'); |
|
132 | + } |
|
133 | + } |
|
134 | + $code = $p3; |
|
135 | + if (is_array($p2) && isset($p2['message'])) { |
|
136 | + // fix potential problem of passing in a single warning |
|
137 | + $p2 = array($p2); |
|
138 | + } |
|
139 | + $this->cause = $p2; |
|
140 | + } else { |
|
141 | + $code = null; |
|
142 | + $this->cause = null; |
|
143 | + } |
|
144 | + parent::__construct($message, $code); |
|
145 | + $this->signal(); |
|
146 | + } |
|
147 | 147 | |
148 | - /** |
|
149 | - * @param mixed $callback - A valid php callback, see php func is_callable() |
|
150 | - * - A PEAR_Exception::OBSERVER_* constant |
|
151 | - * - An array(const PEAR_Exception::OBSERVER_*, |
|
152 | - * mixed $options) |
|
153 | - * @param string $label The name of the observer. Use this if you want |
|
154 | - * to remove it later with removeObserver() |
|
155 | - */ |
|
156 | - public static function addObserver($callback, $label = 'default') |
|
157 | - { |
|
158 | - self::$_observers[$label] = $callback; |
|
159 | - } |
|
148 | + /** |
|
149 | + * @param mixed $callback - A valid php callback, see php func is_callable() |
|
150 | + * - A PEAR_Exception::OBSERVER_* constant |
|
151 | + * - An array(const PEAR_Exception::OBSERVER_*, |
|
152 | + * mixed $options) |
|
153 | + * @param string $label The name of the observer. Use this if you want |
|
154 | + * to remove it later with removeObserver() |
|
155 | + */ |
|
156 | + public static function addObserver($callback, $label = 'default') |
|
157 | + { |
|
158 | + self::$_observers[$label] = $callback; |
|
159 | + } |
|
160 | 160 | |
161 | - public static function removeObserver($label = 'default') |
|
162 | - { |
|
163 | - unset(self::$_observers[$label]); |
|
164 | - } |
|
161 | + public static function removeObserver($label = 'default') |
|
162 | + { |
|
163 | + unset(self::$_observers[$label]); |
|
164 | + } |
|
165 | 165 | |
166 | - /** |
|
167 | - * @return int unique identifier for an observer |
|
168 | - */ |
|
169 | - public static function getUniqueId() |
|
170 | - { |
|
171 | - return self::$_uniqueid++; |
|
172 | - } |
|
166 | + /** |
|
167 | + * @return int unique identifier for an observer |
|
168 | + */ |
|
169 | + public static function getUniqueId() |
|
170 | + { |
|
171 | + return self::$_uniqueid++; |
|
172 | + } |
|
173 | 173 | |
174 | - private function signal() |
|
175 | - { |
|
176 | - foreach (self::$_observers as $func) { |
|
177 | - if (is_callable($func)) { |
|
178 | - call_user_func($func, $this); |
|
179 | - continue; |
|
180 | - } |
|
181 | - settype($func, 'array'); |
|
182 | - switch ($func[0]) { |
|
183 | - case self::OBSERVER_PRINT : |
|
184 | - $f = (isset($func[1])) ? $func[1] : '%s'; |
|
185 | - printf($f, $this->getMessage()); |
|
186 | - break; |
|
187 | - case self::OBSERVER_TRIGGER : |
|
188 | - $f = (isset($func[1])) ? $func[1] : E_USER_NOTICE; |
|
189 | - trigger_error($this->getMessage(), $f); |
|
190 | - break; |
|
191 | - case self::OBSERVER_DIE : |
|
192 | - $f = (isset($func[1])) ? $func[1] : '%s'; |
|
193 | - die(printf($f, $this->getMessage())); |
|
194 | - break; |
|
195 | - default: |
|
196 | - trigger_error('invalid observer type', E_USER_WARNING); |
|
197 | - } |
|
198 | - } |
|
199 | - } |
|
174 | + private function signal() |
|
175 | + { |
|
176 | + foreach (self::$_observers as $func) { |
|
177 | + if (is_callable($func)) { |
|
178 | + call_user_func($func, $this); |
|
179 | + continue; |
|
180 | + } |
|
181 | + settype($func, 'array'); |
|
182 | + switch ($func[0]) { |
|
183 | + case self::OBSERVER_PRINT : |
|
184 | + $f = (isset($func[1])) ? $func[1] : '%s'; |
|
185 | + printf($f, $this->getMessage()); |
|
186 | + break; |
|
187 | + case self::OBSERVER_TRIGGER : |
|
188 | + $f = (isset($func[1])) ? $func[1] : E_USER_NOTICE; |
|
189 | + trigger_error($this->getMessage(), $f); |
|
190 | + break; |
|
191 | + case self::OBSERVER_DIE : |
|
192 | + $f = (isset($func[1])) ? $func[1] : '%s'; |
|
193 | + die(printf($f, $this->getMessage())); |
|
194 | + break; |
|
195 | + default: |
|
196 | + trigger_error('invalid observer type', E_USER_WARNING); |
|
197 | + } |
|
198 | + } |
|
199 | + } |
|
200 | 200 | |
201 | - /** |
|
202 | - * Return specific error information that can be used for more detailed |
|
203 | - * error messages or translation. |
|
204 | - * |
|
205 | - * This method may be overridden in child exception classes in order |
|
206 | - * to add functionality not present in PEAR_Exception and is a placeholder |
|
207 | - * to define API |
|
208 | - * |
|
209 | - * The returned array must be an associative array of parameter => value like so: |
|
210 | - * <pre> |
|
211 | - * array('name' => $name, 'context' => array(...)) |
|
212 | - * </pre> |
|
213 | - * @return array |
|
214 | - */ |
|
215 | - public function getErrorData() |
|
216 | - { |
|
217 | - return array(); |
|
218 | - } |
|
201 | + /** |
|
202 | + * Return specific error information that can be used for more detailed |
|
203 | + * error messages or translation. |
|
204 | + * |
|
205 | + * This method may be overridden in child exception classes in order |
|
206 | + * to add functionality not present in PEAR_Exception and is a placeholder |
|
207 | + * to define API |
|
208 | + * |
|
209 | + * The returned array must be an associative array of parameter => value like so: |
|
210 | + * <pre> |
|
211 | + * array('name' => $name, 'context' => array(...)) |
|
212 | + * </pre> |
|
213 | + * @return array |
|
214 | + */ |
|
215 | + public function getErrorData() |
|
216 | + { |
|
217 | + return array(); |
|
218 | + } |
|
219 | 219 | |
220 | - /** |
|
221 | - * Returns the exception that caused this exception to be thrown |
|
222 | - * @access public |
|
223 | - * @return Exception|array The context of the exception |
|
224 | - */ |
|
225 | - public function getCause() |
|
226 | - { |
|
227 | - return $this->cause; |
|
228 | - } |
|
220 | + /** |
|
221 | + * Returns the exception that caused this exception to be thrown |
|
222 | + * @access public |
|
223 | + * @return Exception|array The context of the exception |
|
224 | + */ |
|
225 | + public function getCause() |
|
226 | + { |
|
227 | + return $this->cause; |
|
228 | + } |
|
229 | 229 | |
230 | - /** |
|
231 | - * Function must be public to call on caused exceptions |
|
232 | - * @param array |
|
233 | - */ |
|
234 | - public function getCauseMessage(&$causes) |
|
235 | - { |
|
236 | - $trace = $this->getTraceSafe(); |
|
237 | - $cause = array('class' => get_class($this), |
|
238 | - 'message' => $this->message, |
|
239 | - 'file' => 'unknown', |
|
240 | - 'line' => 'unknown'); |
|
241 | - if (isset($trace[0])) { |
|
242 | - if (isset($trace[0]['file'])) { |
|
243 | - $cause['file'] = $trace[0]['file']; |
|
244 | - $cause['line'] = $trace[0]['line']; |
|
245 | - } |
|
246 | - } |
|
247 | - $causes[] = $cause; |
|
248 | - if ($this->cause instanceof PEAR_Exception) { |
|
249 | - $this->cause->getCauseMessage($causes); |
|
250 | - } elseif ($this->cause instanceof Exception) { |
|
251 | - $causes[] = array('class' => get_class($this->cause), |
|
252 | - 'message' => $this->cause->getMessage(), |
|
253 | - 'file' => $this->cause->getFile(), |
|
254 | - 'line' => $this->cause->getLine()); |
|
255 | - } elseif (class_exists('PEAR_Error') && $this->cause instanceof PEAR_Error) { |
|
256 | - $causes[] = array('class' => get_class($this->cause), |
|
257 | - 'message' => $this->cause->getMessage(), |
|
258 | - 'file' => 'unknown', |
|
259 | - 'line' => 'unknown'); |
|
260 | - } elseif (is_array($this->cause)) { |
|
261 | - foreach ($this->cause as $cause) { |
|
262 | - if ($cause instanceof PEAR_Exception) { |
|
263 | - $cause->getCauseMessage($causes); |
|
264 | - } elseif ($cause instanceof Exception) { |
|
265 | - $causes[] = array('class' => get_class($cause), |
|
266 | - 'message' => $cause->getMessage(), |
|
267 | - 'file' => $cause->getFile(), |
|
268 | - 'line' => $cause->getLine()); |
|
269 | - } elseif (class_exists('PEAR_Error') && $cause instanceof PEAR_Error) { |
|
270 | - $causes[] = array('class' => get_class($cause), |
|
271 | - 'message' => $cause->getMessage(), |
|
272 | - 'file' => 'unknown', |
|
273 | - 'line' => 'unknown'); |
|
274 | - } elseif (is_array($cause) && isset($cause['message'])) { |
|
275 | - // PEAR_ErrorStack warning |
|
276 | - $causes[] = array( |
|
277 | - 'class' => $cause['package'], |
|
278 | - 'message' => $cause['message'], |
|
279 | - 'file' => isset($cause['context']['file']) ? |
|
280 | - $cause['context']['file'] : |
|
281 | - 'unknown', |
|
282 | - 'line' => isset($cause['context']['line']) ? |
|
283 | - $cause['context']['line'] : |
|
284 | - 'unknown', |
|
285 | - ); |
|
286 | - } |
|
287 | - } |
|
288 | - } |
|
289 | - } |
|
230 | + /** |
|
231 | + * Function must be public to call on caused exceptions |
|
232 | + * @param array |
|
233 | + */ |
|
234 | + public function getCauseMessage(&$causes) |
|
235 | + { |
|
236 | + $trace = $this->getTraceSafe(); |
|
237 | + $cause = array('class' => get_class($this), |
|
238 | + 'message' => $this->message, |
|
239 | + 'file' => 'unknown', |
|
240 | + 'line' => 'unknown'); |
|
241 | + if (isset($trace[0])) { |
|
242 | + if (isset($trace[0]['file'])) { |
|
243 | + $cause['file'] = $trace[0]['file']; |
|
244 | + $cause['line'] = $trace[0]['line']; |
|
245 | + } |
|
246 | + } |
|
247 | + $causes[] = $cause; |
|
248 | + if ($this->cause instanceof PEAR_Exception) { |
|
249 | + $this->cause->getCauseMessage($causes); |
|
250 | + } elseif ($this->cause instanceof Exception) { |
|
251 | + $causes[] = array('class' => get_class($this->cause), |
|
252 | + 'message' => $this->cause->getMessage(), |
|
253 | + 'file' => $this->cause->getFile(), |
|
254 | + 'line' => $this->cause->getLine()); |
|
255 | + } elseif (class_exists('PEAR_Error') && $this->cause instanceof PEAR_Error) { |
|
256 | + $causes[] = array('class' => get_class($this->cause), |
|
257 | + 'message' => $this->cause->getMessage(), |
|
258 | + 'file' => 'unknown', |
|
259 | + 'line' => 'unknown'); |
|
260 | + } elseif (is_array($this->cause)) { |
|
261 | + foreach ($this->cause as $cause) { |
|
262 | + if ($cause instanceof PEAR_Exception) { |
|
263 | + $cause->getCauseMessage($causes); |
|
264 | + } elseif ($cause instanceof Exception) { |
|
265 | + $causes[] = array('class' => get_class($cause), |
|
266 | + 'message' => $cause->getMessage(), |
|
267 | + 'file' => $cause->getFile(), |
|
268 | + 'line' => $cause->getLine()); |
|
269 | + } elseif (class_exists('PEAR_Error') && $cause instanceof PEAR_Error) { |
|
270 | + $causes[] = array('class' => get_class($cause), |
|
271 | + 'message' => $cause->getMessage(), |
|
272 | + 'file' => 'unknown', |
|
273 | + 'line' => 'unknown'); |
|
274 | + } elseif (is_array($cause) && isset($cause['message'])) { |
|
275 | + // PEAR_ErrorStack warning |
|
276 | + $causes[] = array( |
|
277 | + 'class' => $cause['package'], |
|
278 | + 'message' => $cause['message'], |
|
279 | + 'file' => isset($cause['context']['file']) ? |
|
280 | + $cause['context']['file'] : |
|
281 | + 'unknown', |
|
282 | + 'line' => isset($cause['context']['line']) ? |
|
283 | + $cause['context']['line'] : |
|
284 | + 'unknown', |
|
285 | + ); |
|
286 | + } |
|
287 | + } |
|
288 | + } |
|
289 | + } |
|
290 | 290 | |
291 | - public function getTraceSafe() |
|
292 | - { |
|
293 | - if (!isset($this->_trace)) { |
|
294 | - $this->_trace = $this->getTrace(); |
|
295 | - if (empty($this->_trace)) { |
|
296 | - $backtrace = debug_backtrace(); |
|
297 | - $this->_trace = array($backtrace[count($backtrace)-1]); |
|
298 | - } |
|
299 | - } |
|
300 | - return $this->_trace; |
|
301 | - } |
|
291 | + public function getTraceSafe() |
|
292 | + { |
|
293 | + if (!isset($this->_trace)) { |
|
294 | + $this->_trace = $this->getTrace(); |
|
295 | + if (empty($this->_trace)) { |
|
296 | + $backtrace = debug_backtrace(); |
|
297 | + $this->_trace = array($backtrace[count($backtrace)-1]); |
|
298 | + } |
|
299 | + } |
|
300 | + return $this->_trace; |
|
301 | + } |
|
302 | 302 | |
303 | - public function getErrorClass() |
|
304 | - { |
|
305 | - $trace = $this->getTraceSafe(); |
|
306 | - return $trace[0]['class']; |
|
307 | - } |
|
303 | + public function getErrorClass() |
|
304 | + { |
|
305 | + $trace = $this->getTraceSafe(); |
|
306 | + return $trace[0]['class']; |
|
307 | + } |
|
308 | 308 | |
309 | - public function getErrorMethod() |
|
310 | - { |
|
311 | - $trace = $this->getTraceSafe(); |
|
312 | - return $trace[0]['function']; |
|
313 | - } |
|
309 | + public function getErrorMethod() |
|
310 | + { |
|
311 | + $trace = $this->getTraceSafe(); |
|
312 | + return $trace[0]['function']; |
|
313 | + } |
|
314 | 314 | |
315 | - public function __toString() |
|
316 | - { |
|
317 | - if (isset($_SERVER['REQUEST_URI'])) { |
|
318 | - return $this->toHtml(); |
|
319 | - } |
|
320 | - return $this->toText(); |
|
321 | - } |
|
315 | + public function __toString() |
|
316 | + { |
|
317 | + if (isset($_SERVER['REQUEST_URI'])) { |
|
318 | + return $this->toHtml(); |
|
319 | + } |
|
320 | + return $this->toText(); |
|
321 | + } |
|
322 | 322 | |
323 | - public function toHtml() |
|
324 | - { |
|
325 | - $trace = $this->getTraceSafe(); |
|
326 | - $causes = array(); |
|
327 | - $this->getCauseMessage($causes); |
|
328 | - $html = '<table border="1" cellspacing="0">' . "\n"; |
|
329 | - foreach ($causes as $i => $cause) { |
|
330 | - $html .= '<tr><td colspan="3" bgcolor="#ff9999">' |
|
331 | - . str_repeat('-', $i) . ' <b>' . $cause['class'] . '</b>: ' |
|
332 | - . htmlspecialchars($cause['message'], ENT_COMPAT | ENT_HTML401, 'UTF-8', false) . ' in <b>' . $cause['file'] . '</b> ' |
|
333 | - . 'on line <b>' . $cause['line'] . '</b>' |
|
334 | - . "</td></tr>\n"; |
|
335 | - } |
|
336 | - $html .= '<tr><td colspan="3" bgcolor="#aaaaaa" align="center"><b>Exception trace</b></td></tr>' . "\n" |
|
337 | - . '<tr><td align="center" bgcolor="#cccccc" width="20"><b>#</b></td>' |
|
338 | - . '<td align="center" bgcolor="#cccccc"><b>Function</b></td>' |
|
339 | - . '<td align="center" bgcolor="#cccccc"><b>Location</b></td></tr>' . "\n"; |
|
323 | + public function toHtml() |
|
324 | + { |
|
325 | + $trace = $this->getTraceSafe(); |
|
326 | + $causes = array(); |
|
327 | + $this->getCauseMessage($causes); |
|
328 | + $html = '<table border="1" cellspacing="0">' . "\n"; |
|
329 | + foreach ($causes as $i => $cause) { |
|
330 | + $html .= '<tr><td colspan="3" bgcolor="#ff9999">' |
|
331 | + . str_repeat('-', $i) . ' <b>' . $cause['class'] . '</b>: ' |
|
332 | + . htmlspecialchars($cause['message'], ENT_COMPAT | ENT_HTML401, 'UTF-8', false) . ' in <b>' . $cause['file'] . '</b> ' |
|
333 | + . 'on line <b>' . $cause['line'] . '</b>' |
|
334 | + . "</td></tr>\n"; |
|
335 | + } |
|
336 | + $html .= '<tr><td colspan="3" bgcolor="#aaaaaa" align="center"><b>Exception trace</b></td></tr>' . "\n" |
|
337 | + . '<tr><td align="center" bgcolor="#cccccc" width="20"><b>#</b></td>' |
|
338 | + . '<td align="center" bgcolor="#cccccc"><b>Function</b></td>' |
|
339 | + . '<td align="center" bgcolor="#cccccc"><b>Location</b></td></tr>' . "\n"; |
|
340 | 340 | |
341 | - foreach ($trace as $k => $v) { |
|
342 | - $html .= '<tr><td align="center">' . $k . '</td>' |
|
343 | - . '<td>'; |
|
344 | - if (!empty($v['class'])) { |
|
345 | - $html .= $v['class'] . $v['type']; |
|
346 | - } |
|
347 | - $html .= $v['function']; |
|
348 | - $args = array(); |
|
349 | - if (!empty($v['args'])) { |
|
350 | - foreach ($v['args'] as $arg) { |
|
351 | - if (is_null($arg)) $args[] = 'null'; |
|
352 | - elseif (is_array($arg)) $args[] = 'Array'; |
|
353 | - elseif (is_object($arg)) $args[] = 'Object('.get_class($arg).')'; |
|
354 | - elseif (is_bool($arg)) $args[] = $arg ? 'true' : 'false'; |
|
355 | - elseif (is_int($arg) || is_double($arg)) $args[] = $arg; |
|
356 | - else { |
|
357 | - $arg = (string)$arg; |
|
358 | - $str = htmlspecialchars(substr($arg, 0, 16), ENT_COMPAT | ENT_HTML401, 'UTF-8', false); |
|
359 | - if (strlen($arg) > 16) $str .= '…'; |
|
360 | - $args[] = "'" . $str . "'"; |
|
361 | - } |
|
362 | - } |
|
363 | - } |
|
364 | - $html .= '(' . implode(', ',$args) . ')' |
|
365 | - . '</td>' |
|
366 | - . '<td>' . (isset($v['file']) ? $v['file'] : 'unknown') |
|
367 | - . ':' . (isset($v['line']) ? $v['line'] : 'unknown') |
|
368 | - . '</td></tr>' . "\n"; |
|
369 | - } |
|
370 | - $html .= '<tr><td align="center">' . ($k+1) . '</td>' |
|
371 | - . '<td>{main}</td>' |
|
372 | - . '<td> </td></tr>' . "\n" |
|
373 | - . '</table>'; |
|
374 | - return $html; |
|
375 | - } |
|
341 | + foreach ($trace as $k => $v) { |
|
342 | + $html .= '<tr><td align="center">' . $k . '</td>' |
|
343 | + . '<td>'; |
|
344 | + if (!empty($v['class'])) { |
|
345 | + $html .= $v['class'] . $v['type']; |
|
346 | + } |
|
347 | + $html .= $v['function']; |
|
348 | + $args = array(); |
|
349 | + if (!empty($v['args'])) { |
|
350 | + foreach ($v['args'] as $arg) { |
|
351 | + if (is_null($arg)) $args[] = 'null'; |
|
352 | + elseif (is_array($arg)) $args[] = 'Array'; |
|
353 | + elseif (is_object($arg)) $args[] = 'Object('.get_class($arg).')'; |
|
354 | + elseif (is_bool($arg)) $args[] = $arg ? 'true' : 'false'; |
|
355 | + elseif (is_int($arg) || is_double($arg)) $args[] = $arg; |
|
356 | + else { |
|
357 | + $arg = (string)$arg; |
|
358 | + $str = htmlspecialchars(substr($arg, 0, 16), ENT_COMPAT | ENT_HTML401, 'UTF-8', false); |
|
359 | + if (strlen($arg) > 16) $str .= '…'; |
|
360 | + $args[] = "'" . $str . "'"; |
|
361 | + } |
|
362 | + } |
|
363 | + } |
|
364 | + $html .= '(' . implode(', ',$args) . ')' |
|
365 | + . '</td>' |
|
366 | + . '<td>' . (isset($v['file']) ? $v['file'] : 'unknown') |
|
367 | + . ':' . (isset($v['line']) ? $v['line'] : 'unknown') |
|
368 | + . '</td></tr>' . "\n"; |
|
369 | + } |
|
370 | + $html .= '<tr><td align="center">' . ($k+1) . '</td>' |
|
371 | + . '<td>{main}</td>' |
|
372 | + . '<td> </td></tr>' . "\n" |
|
373 | + . '</table>'; |
|
374 | + return $html; |
|
375 | + } |
|
376 | 376 | |
377 | - public function toText() |
|
378 | - { |
|
379 | - $causes = array(); |
|
380 | - $this->getCauseMessage($causes); |
|
381 | - $causeMsg = ''; |
|
382 | - foreach ($causes as $i => $cause) { |
|
383 | - $causeMsg .= str_repeat(' ', $i) . $cause['class'] . ': ' |
|
384 | - . $cause['message'] . ' in ' . $cause['file'] |
|
385 | - . ' on line ' . $cause['line'] . "\n"; |
|
386 | - } |
|
387 | - return $causeMsg . $this->getTraceAsString(); |
|
388 | - } |
|
377 | + public function toText() |
|
378 | + { |
|
379 | + $causes = array(); |
|
380 | + $this->getCauseMessage($causes); |
|
381 | + $causeMsg = ''; |
|
382 | + foreach ($causes as $i => $cause) { |
|
383 | + $causeMsg .= str_repeat(' ', $i) . $cause['class'] . ': ' |
|
384 | + . $cause['message'] . ' in ' . $cause['file'] |
|
385 | + . ' on line ' . $cause['line'] . "\n"; |
|
386 | + } |
|
387 | + return $causeMsg . $this->getTraceAsString(); |
|
388 | + } |
|
389 | 389 | } |
390 | 390 | |
391 | 391 | ?> |
@@ -348,15 +348,22 @@ |
||
348 | 348 | $args = array(); |
349 | 349 | if (!empty($v['args'])) { |
350 | 350 | foreach ($v['args'] as $arg) { |
351 | - if (is_null($arg)) $args[] = 'null'; |
|
352 | - elseif (is_array($arg)) $args[] = 'Array'; |
|
353 | - elseif (is_object($arg)) $args[] = 'Object('.get_class($arg).')'; |
|
354 | - elseif (is_bool($arg)) $args[] = $arg ? 'true' : 'false'; |
|
355 | - elseif (is_int($arg) || is_double($arg)) $args[] = $arg; |
|
356 | - else { |
|
351 | + if (is_null($arg)) { |
|
352 | + $args[] = 'null'; |
|
353 | + } elseif (is_array($arg)) { |
|
354 | + $args[] = 'Array'; |
|
355 | + } elseif (is_object($arg)) { |
|
356 | + $args[] = 'Object('.get_class($arg).')'; |
|
357 | + } elseif (is_bool($arg)) { |
|
358 | + $args[] = $arg ? 'true' : 'false'; |
|
359 | + } elseif (is_int($arg) || is_double($arg)) { |
|
360 | + $args[] = $arg; |
|
361 | + } else { |
|
357 | 362 | $arg = (string)$arg; |
358 | 363 | $str = htmlspecialchars(substr($arg, 0, 16), ENT_COMPAT | ENT_HTML401, 'UTF-8', false); |
359 | - if (strlen($arg) > 16) $str .= '…'; |
|
364 | + if (strlen($arg) > 16) { |
|
365 | + $str .= '…'; |
|
366 | + } |
|
360 | 367 | $args[] = "'" . $str . "'"; |
361 | 368 | } |
362 | 369 | } |
@@ -127,7 +127,7 @@ discard block |
||
127 | 127 | // using is_object allows both Exception and PEAR_Error |
128 | 128 | if (is_object($p2) && !($p2 instanceof Exception)) { |
129 | 129 | if (!class_exists('PEAR_Error') || !($p2 instanceof PEAR_Error)) { |
130 | - throw new PEAR_Exception('exception cause must be Exception, ' . |
|
130 | + throw new PEAR_Exception('exception cause must be Exception, '. |
|
131 | 131 | 'array, or PEAR_Error'); |
132 | 132 | } |
133 | 133 | } |
@@ -277,11 +277,9 @@ discard block |
||
277 | 277 | 'class' => $cause['package'], |
278 | 278 | 'message' => $cause['message'], |
279 | 279 | 'file' => isset($cause['context']['file']) ? |
280 | - $cause['context']['file'] : |
|
281 | - 'unknown', |
|
280 | + $cause['context']['file'] : 'unknown', |
|
282 | 281 | 'line' => isset($cause['context']['line']) ? |
283 | - $cause['context']['line'] : |
|
284 | - 'unknown', |
|
282 | + $cause['context']['line'] : 'unknown', |
|
285 | 283 | ); |
286 | 284 | } |
287 | 285 | } |
@@ -294,7 +292,7 @@ discard block |
||
294 | 292 | $this->_trace = $this->getTrace(); |
295 | 293 | if (empty($this->_trace)) { |
296 | 294 | $backtrace = debug_backtrace(); |
297 | - $this->_trace = array($backtrace[count($backtrace)-1]); |
|
295 | + $this->_trace = array($backtrace[count($backtrace) - 1]); |
|
298 | 296 | } |
299 | 297 | } |
300 | 298 | return $this->_trace; |
@@ -325,24 +323,24 @@ discard block |
||
325 | 323 | $trace = $this->getTraceSafe(); |
326 | 324 | $causes = array(); |
327 | 325 | $this->getCauseMessage($causes); |
328 | - $html = '<table border="1" cellspacing="0">' . "\n"; |
|
326 | + $html = '<table border="1" cellspacing="0">'."\n"; |
|
329 | 327 | foreach ($causes as $i => $cause) { |
330 | 328 | $html .= '<tr><td colspan="3" bgcolor="#ff9999">' |
331 | - . str_repeat('-', $i) . ' <b>' . $cause['class'] . '</b>: ' |
|
332 | - . htmlspecialchars($cause['message'], ENT_COMPAT | ENT_HTML401, 'UTF-8', false) . ' in <b>' . $cause['file'] . '</b> ' |
|
333 | - . 'on line <b>' . $cause['line'] . '</b>' |
|
329 | + . str_repeat('-', $i).' <b>'.$cause['class'].'</b>: ' |
|
330 | + . htmlspecialchars($cause['message'], ENT_COMPAT | ENT_HTML401, 'UTF-8', false).' in <b>'.$cause['file'].'</b> ' |
|
331 | + . 'on line <b>'.$cause['line'].'</b>' |
|
334 | 332 | . "</td></tr>\n"; |
335 | 333 | } |
336 | - $html .= '<tr><td colspan="3" bgcolor="#aaaaaa" align="center"><b>Exception trace</b></td></tr>' . "\n" |
|
334 | + $html .= '<tr><td colspan="3" bgcolor="#aaaaaa" align="center"><b>Exception trace</b></td></tr>'."\n" |
|
337 | 335 | . '<tr><td align="center" bgcolor="#cccccc" width="20"><b>#</b></td>' |
338 | 336 | . '<td align="center" bgcolor="#cccccc"><b>Function</b></td>' |
339 | - . '<td align="center" bgcolor="#cccccc"><b>Location</b></td></tr>' . "\n"; |
|
337 | + . '<td align="center" bgcolor="#cccccc"><b>Location</b></td></tr>'."\n"; |
|
340 | 338 | |
341 | 339 | foreach ($trace as $k => $v) { |
342 | - $html .= '<tr><td align="center">' . $k . '</td>' |
|
340 | + $html .= '<tr><td align="center">'.$k.'</td>' |
|
343 | 341 | . '<td>'; |
344 | 342 | if (!empty($v['class'])) { |
345 | - $html .= $v['class'] . $v['type']; |
|
343 | + $html .= $v['class'].$v['type']; |
|
346 | 344 | } |
347 | 345 | $html .= $v['function']; |
348 | 346 | $args = array(); |
@@ -354,22 +352,22 @@ discard block |
||
354 | 352 | elseif (is_bool($arg)) $args[] = $arg ? 'true' : 'false'; |
355 | 353 | elseif (is_int($arg) || is_double($arg)) $args[] = $arg; |
356 | 354 | else { |
357 | - $arg = (string)$arg; |
|
355 | + $arg = (string) $arg; |
|
358 | 356 | $str = htmlspecialchars(substr($arg, 0, 16), ENT_COMPAT | ENT_HTML401, 'UTF-8', false); |
359 | 357 | if (strlen($arg) > 16) $str .= '…'; |
360 | - $args[] = "'" . $str . "'"; |
|
358 | + $args[] = "'".$str."'"; |
|
361 | 359 | } |
362 | 360 | } |
363 | 361 | } |
364 | - $html .= '(' . implode(', ',$args) . ')' |
|
362 | + $html .= '('.implode(', ', $args).')' |
|
365 | 363 | . '</td>' |
366 | - . '<td>' . (isset($v['file']) ? $v['file'] : 'unknown') |
|
367 | - . ':' . (isset($v['line']) ? $v['line'] : 'unknown') |
|
368 | - . '</td></tr>' . "\n"; |
|
364 | + . '<td>'.(isset($v['file']) ? $v['file'] : 'unknown') |
|
365 | + . ':'.(isset($v['line']) ? $v['line'] : 'unknown') |
|
366 | + . '</td></tr>'."\n"; |
|
369 | 367 | } |
370 | - $html .= '<tr><td align="center">' . ($k+1) . '</td>' |
|
368 | + $html .= '<tr><td align="center">'.($k + 1).'</td>' |
|
371 | 369 | . '<td>{main}</td>' |
372 | - . '<td> </td></tr>' . "\n" |
|
370 | + . '<td> </td></tr>'."\n" |
|
373 | 371 | . '</table>'; |
374 | 372 | return $html; |
375 | 373 | } |
@@ -380,11 +378,11 @@ discard block |
||
380 | 378 | $this->getCauseMessage($causes); |
381 | 379 | $causeMsg = ''; |
382 | 380 | foreach ($causes as $i => $cause) { |
383 | - $causeMsg .= str_repeat(' ', $i) . $cause['class'] . ': ' |
|
384 | - . $cause['message'] . ' in ' . $cause['file'] |
|
385 | - . ' on line ' . $cause['line'] . "\n"; |
|
381 | + $causeMsg .= str_repeat(' ', $i).$cause['class'].': ' |
|
382 | + . $cause['message'].' in '.$cause['file'] |
|
383 | + . ' on line '.$cause['line']."\n"; |
|
386 | 384 | } |
387 | - return $causeMsg . $this->getTraceAsString(); |
|
385 | + return $causeMsg.$this->getTraceAsString(); |
|
388 | 386 | } |
389 | 387 | } |
390 | 388 |
@@ -117,6 +117,8 @@ |
||
117 | 117 | * @param string exception message |
118 | 118 | * @param int|Exception|PEAR_Error|array|null exception cause |
119 | 119 | * @param int|null exception code or null |
120 | + * @param string|null $message |
|
121 | + * @param integer $p2 |
|
120 | 122 | */ |
121 | 123 | public function __construct($message, $p2 = null, $p3 = null) |
122 | 124 | { |
@@ -5,29 +5,29 @@ |
||
5 | 5 | */ |
6 | 6 | class PEAR5 |
7 | 7 | { |
8 | - /** |
|
9 | - * If you have a class that's mostly/entirely static, and you need static |
|
10 | - * properties, you can use this method to simulate them. Eg. in your method(s) |
|
11 | - * do this: $myVar = &PEAR5::getStaticProperty('myclass', 'myVar'); |
|
12 | - * You MUST use a reference, or they will not persist! |
|
13 | - * |
|
14 | - * @access public |
|
15 | - * @param string $class The calling classname, to prevent clashes |
|
16 | - * @param string $var The variable to retrieve. |
|
17 | - * @return mixed A reference to the variable. If not set it will be |
|
18 | - * auto initialised to NULL. |
|
19 | - */ |
|
20 | - static function &getStaticProperty($class, $var) |
|
21 | - { |
|
22 | - static $properties; |
|
23 | - if (!isset($properties[$class])) { |
|
24 | - $properties[$class] = array(); |
|
25 | - } |
|
8 | + /** |
|
9 | + * If you have a class that's mostly/entirely static, and you need static |
|
10 | + * properties, you can use this method to simulate them. Eg. in your method(s) |
|
11 | + * do this: $myVar = &PEAR5::getStaticProperty('myclass', 'myVar'); |
|
12 | + * You MUST use a reference, or they will not persist! |
|
13 | + * |
|
14 | + * @access public |
|
15 | + * @param string $class The calling classname, to prevent clashes |
|
16 | + * @param string $var The variable to retrieve. |
|
17 | + * @return mixed A reference to the variable. If not set it will be |
|
18 | + * auto initialised to NULL. |
|
19 | + */ |
|
20 | + static function &getStaticProperty($class, $var) |
|
21 | + { |
|
22 | + static $properties; |
|
23 | + if (!isset($properties[$class])) { |
|
24 | + $properties[$class] = array(); |
|
25 | + } |
|
26 | 26 | |
27 | - if (!array_key_exists($var, $properties[$class])) { |
|
28 | - $properties[$class][$var] = null; |
|
29 | - } |
|
27 | + if (!array_key_exists($var, $properties[$class])) { |
|
28 | + $properties[$class][$var] = null; |
|
29 | + } |
|
30 | 30 | |
31 | - return $properties[$class][$var]; |
|
32 | - } |
|
31 | + return $properties[$class][$var]; |
|
32 | + } |
|
33 | 33 | } |
34 | 34 | \ No newline at end of file |
@@ -34,495 +34,495 @@ |
||
34 | 34 | */ |
35 | 35 | class Net_Socket extends PEAR { |
36 | 36 | |
37 | - /** |
|
38 | - * Socket file pointer. |
|
39 | - * @var resource $fp |
|
40 | - */ |
|
41 | - var $fp = null; |
|
42 | - |
|
43 | - /** |
|
44 | - * Whether the socket is blocking. Defaults to true. |
|
45 | - * @var boolean $blocking |
|
46 | - */ |
|
47 | - var $blocking = true; |
|
48 | - |
|
49 | - /** |
|
50 | - * Whether the socket is persistent. Defaults to false. |
|
51 | - * @var boolean $persistent |
|
52 | - */ |
|
53 | - var $persistent = false; |
|
54 | - |
|
55 | - /** |
|
56 | - * The IP address to connect to. |
|
57 | - * @var string $addr |
|
58 | - */ |
|
59 | - var $addr = ''; |
|
60 | - |
|
61 | - /** |
|
62 | - * The port number to connect to. |
|
63 | - * @var integer $port |
|
64 | - */ |
|
65 | - var $port = 0; |
|
66 | - |
|
67 | - /** |
|
68 | - * Number of seconds to wait on socket connections before assuming |
|
69 | - * there's no more data. Defaults to no timeout. |
|
70 | - * @var integer $timeout |
|
71 | - */ |
|
72 | - var $timeout = false; |
|
73 | - |
|
74 | - /** |
|
75 | - * Number of bytes to read at a time in readLine() and |
|
76 | - * readAll(). Defaults to 2048. |
|
77 | - * @var integer $lineLength |
|
78 | - */ |
|
79 | - var $lineLength = 2048; |
|
80 | - |
|
81 | - /** |
|
82 | - * Connect to the specified port. If called when the socket is |
|
83 | - * already connected, it disconnects and connects again. |
|
84 | - * |
|
85 | - * @param string $addr IP address or host name. |
|
86 | - * @param integer $port TCP port number. |
|
87 | - * @param boolean $persistent (optional) Whether the connection is |
|
88 | - * persistent (kept open between requests |
|
89 | - * by the web server). |
|
90 | - * @param integer $timeout (optional) How long to wait for data. |
|
91 | - * @param array $options See options for stream_context_create. |
|
92 | - * |
|
93 | - * @access public |
|
94 | - * |
|
95 | - * @return boolean | PEAR_Error True on success or a PEAR_Error on failure. |
|
96 | - */ |
|
97 | - function connect($addr, $port = 0, $persistent = null, $timeout = null, $options = null) |
|
98 | - { |
|
99 | - if (is_resource($this->fp)) { |
|
100 | - @fclose($this->fp); |
|
101 | - $this->fp = null; |
|
102 | - } |
|
103 | - |
|
104 | - if (!$addr) { |
|
105 | - return $this->raiseError('$addr cannot be empty'); |
|
106 | - } elseif (strspn($addr, '.0123456789') == strlen($addr) || |
|
107 | - strstr($addr, '/') !== false) { |
|
108 | - $this->addr = $addr; |
|
109 | - } else { |
|
110 | - $this->addr = @gethostbyname($addr); |
|
111 | - } |
|
112 | - |
|
113 | - $this->port = $port % 65536; |
|
114 | - |
|
115 | - if ($persistent !== null) { |
|
116 | - $this->persistent = $persistent; |
|
117 | - } |
|
118 | - |
|
119 | - if ($timeout !== null) { |
|
120 | - $this->timeout = $timeout; |
|
121 | - } |
|
122 | - |
|
123 | - $openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen'; |
|
124 | - $errno = 0; |
|
125 | - $errstr = ''; |
|
126 | - if ($options && function_exists('stream_context_create')) { |
|
127 | - if ($this->timeout) { |
|
128 | - $timeout = $this->timeout; |
|
129 | - } else { |
|
130 | - $timeout = 0; |
|
131 | - } |
|
132 | - $context = stream_context_create($options); |
|
133 | - $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $timeout, $context); |
|
134 | - } else { |
|
135 | - if ($this->timeout) { |
|
136 | - $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $this->timeout); |
|
137 | - } else { |
|
138 | - $fp = @$openfunc($this->addr, $this->port, $errno, $errstr); |
|
139 | - } |
|
140 | - } |
|
141 | - |
|
142 | - if (!$fp) { |
|
143 | - return $this->raiseError($errstr, $errno); |
|
144 | - } |
|
145 | - |
|
146 | - $this->fp = $fp; |
|
147 | - |
|
148 | - return $this->setBlocking($this->blocking); |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Disconnects from the peer, closes the socket. |
|
153 | - * |
|
154 | - * @access public |
|
155 | - * @return mixed true on success or an error object otherwise |
|
156 | - */ |
|
157 | - function disconnect() |
|
158 | - { |
|
159 | - if (!is_resource($this->fp)) { |
|
160 | - return $this->raiseError('not connected'); |
|
161 | - } |
|
162 | - |
|
163 | - @fclose($this->fp); |
|
164 | - $this->fp = null; |
|
165 | - return true; |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Find out if the socket is in blocking mode. |
|
170 | - * |
|
171 | - * @access public |
|
172 | - * @return boolean The current blocking mode. |
|
173 | - */ |
|
174 | - function isBlocking() |
|
175 | - { |
|
176 | - return $this->blocking; |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Sets whether the socket connection should be blocking or |
|
181 | - * not. A read call to a non-blocking socket will return immediately |
|
182 | - * if there is no data available, whereas it will block until there |
|
183 | - * is data for blocking sockets. |
|
184 | - * |
|
185 | - * @param boolean $mode True for blocking sockets, false for nonblocking. |
|
186 | - * @access public |
|
187 | - * @return mixed true on success or an error object otherwise |
|
188 | - */ |
|
189 | - function setBlocking($mode) |
|
190 | - { |
|
191 | - if (!is_resource($this->fp)) { |
|
192 | - return $this->raiseError('not connected'); |
|
193 | - } |
|
194 | - |
|
195 | - $this->blocking = $mode; |
|
196 | - socket_set_blocking($this->fp, $this->blocking); |
|
197 | - return true; |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * Sets the timeout value on socket descriptor, |
|
202 | - * expressed in the sum of seconds and microseconds |
|
203 | - * |
|
204 | - * @param integer $seconds Seconds. |
|
205 | - * @param integer $microseconds Microseconds. |
|
206 | - * @access public |
|
207 | - * @return mixed true on success or an error object otherwise |
|
208 | - */ |
|
209 | - function setTimeout($seconds, $microseconds) |
|
210 | - { |
|
211 | - if (!is_resource($this->fp)) { |
|
212 | - return $this->raiseError('not connected'); |
|
213 | - } |
|
214 | - |
|
215 | - return socket_set_timeout($this->fp, $seconds, $microseconds); |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * Returns information about an existing socket resource. |
|
220 | - * Currently returns four entries in the result array: |
|
221 | - * |
|
222 | - * <p> |
|
223 | - * timed_out (bool) - The socket timed out waiting for data<br> |
|
224 | - * blocked (bool) - The socket was blocked<br> |
|
225 | - * eof (bool) - Indicates EOF event<br> |
|
226 | - * unread_bytes (int) - Number of bytes left in the socket buffer<br> |
|
227 | - * </p> |
|
228 | - * |
|
229 | - * @access public |
|
230 | - * @return mixed Array containing information about existing socket resource or an error object otherwise |
|
231 | - */ |
|
232 | - function getStatus() |
|
233 | - { |
|
234 | - if (!is_resource($this->fp)) { |
|
235 | - return $this->raiseError('not connected'); |
|
236 | - } |
|
237 | - |
|
238 | - return socket_get_status($this->fp); |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * Get a specified line of data |
|
243 | - * |
|
244 | - * @access public |
|
245 | - * @return $size bytes of data from the socket, or a PEAR_Error if |
|
246 | - * not connected. |
|
247 | - */ |
|
248 | - function gets($size) |
|
249 | - { |
|
250 | - if (!is_resource($this->fp)) { |
|
251 | - return $this->raiseError('not connected'); |
|
252 | - } |
|
253 | - |
|
254 | - return @fgets($this->fp, $size); |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * Read a specified amount of data. This is guaranteed to return, |
|
259 | - * and has the added benefit of getting everything in one fread() |
|
260 | - * chunk; if you know the size of the data you're getting |
|
261 | - * beforehand, this is definitely the way to go. |
|
262 | - * |
|
263 | - * @param integer $size The number of bytes to read from the socket. |
|
264 | - * @access public |
|
265 | - * @return $size bytes of data from the socket, or a PEAR_Error if |
|
266 | - * not connected. |
|
267 | - */ |
|
268 | - function read($size) |
|
269 | - { |
|
270 | - if (!is_resource($this->fp)) { |
|
271 | - return $this->raiseError('not connected'); |
|
272 | - } |
|
273 | - |
|
274 | - return @fread($this->fp, $size); |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * Write a specified amount of data. |
|
279 | - * |
|
280 | - * @param string $data Data to write. |
|
281 | - * @param integer $blocksize Amount of data to write at once. |
|
282 | - * NULL means all at once. |
|
283 | - * |
|
284 | - * @access public |
|
285 | - * @return mixed true on success or an error object otherwise |
|
286 | - */ |
|
287 | - function write($data, $blocksize = null) |
|
288 | - { |
|
289 | - if (!is_resource($this->fp)) { |
|
290 | - return $this->raiseError('not connected'); |
|
291 | - } |
|
292 | - |
|
293 | - if (is_null($blocksize) && !OS_WINDOWS) { |
|
294 | - return fwrite($this->fp, $data); |
|
295 | - } else { |
|
296 | - if (is_null($blocksize)) { |
|
297 | - $blocksize = 1024; |
|
298 | - } |
|
299 | - |
|
300 | - $pos = 0; |
|
301 | - $size = strlen($data); |
|
302 | - while ($pos < $size) { |
|
303 | - $written = @fwrite($this->fp, substr($data, $pos, $blocksize)); |
|
304 | - if ($written === false) { |
|
305 | - return false; |
|
306 | - } |
|
307 | - $pos += $written; |
|
308 | - } |
|
309 | - |
|
310 | - return $pos; |
|
311 | - } |
|
312 | - } |
|
313 | - |
|
314 | - /** |
|
315 | - * Write a line of data to the socket, followed by a trailing "\r\n". |
|
316 | - * |
|
317 | - * @access public |
|
318 | - * @return mixed fputs result, or an error |
|
319 | - */ |
|
320 | - function writeLine($data) |
|
321 | - { |
|
322 | - if (!is_resource($this->fp)) { |
|
323 | - return $this->raiseError('not connected'); |
|
324 | - } |
|
325 | - |
|
326 | - return fwrite($this->fp, $data . "\r\n"); |
|
327 | - } |
|
328 | - |
|
329 | - /** |
|
330 | - * Tests for end-of-file on a socket descriptor. |
|
331 | - * |
|
332 | - * @access public |
|
333 | - * @return bool |
|
334 | - */ |
|
335 | - function eof() |
|
336 | - { |
|
337 | - return (is_resource($this->fp) && feof($this->fp)); |
|
338 | - } |
|
339 | - |
|
340 | - /** |
|
341 | - * Reads a byte of data |
|
342 | - * |
|
343 | - * @access public |
|
344 | - * @return 1 byte of data from the socket, or a PEAR_Error if |
|
345 | - * not connected. |
|
346 | - */ |
|
347 | - function readByte() |
|
348 | - { |
|
349 | - if (!is_resource($this->fp)) { |
|
350 | - return $this->raiseError('not connected'); |
|
351 | - } |
|
352 | - |
|
353 | - return ord(@fread($this->fp, 1)); |
|
354 | - } |
|
355 | - |
|
356 | - /** |
|
357 | - * Reads a word of data |
|
358 | - * |
|
359 | - * @access public |
|
360 | - * @return 1 word of data from the socket, or a PEAR_Error if |
|
361 | - * not connected. |
|
362 | - */ |
|
363 | - function readWord() |
|
364 | - { |
|
365 | - if (!is_resource($this->fp)) { |
|
366 | - return $this->raiseError('not connected'); |
|
367 | - } |
|
368 | - |
|
369 | - $buf = @fread($this->fp, 2); |
|
370 | - return (ord($buf[0]) + (ord($buf[1]) << 8)); |
|
371 | - } |
|
372 | - |
|
373 | - /** |
|
374 | - * Reads an int of data |
|
375 | - * |
|
376 | - * @access public |
|
377 | - * @return integer 1 int of data from the socket, or a PEAR_Error if |
|
378 | - * not connected. |
|
379 | - */ |
|
380 | - function readInt() |
|
381 | - { |
|
382 | - if (!is_resource($this->fp)) { |
|
383 | - return $this->raiseError('not connected'); |
|
384 | - } |
|
385 | - |
|
386 | - $buf = @fread($this->fp, 4); |
|
387 | - return (ord($buf[0]) + (ord($buf[1]) << 8) + |
|
388 | - (ord($buf[2]) << 16) + (ord($buf[3]) << 24)); |
|
389 | - } |
|
390 | - |
|
391 | - /** |
|
392 | - * Reads a zero-terminated string of data |
|
393 | - * |
|
394 | - * @access public |
|
395 | - * @return string, or a PEAR_Error if |
|
396 | - * not connected. |
|
397 | - */ |
|
398 | - function readString() |
|
399 | - { |
|
400 | - if (!is_resource($this->fp)) { |
|
401 | - return $this->raiseError('not connected'); |
|
402 | - } |
|
403 | - |
|
404 | - $string = ''; |
|
405 | - while (($char = @fread($this->fp, 1)) != "\x00") { |
|
406 | - $string .= $char; |
|
407 | - } |
|
408 | - return $string; |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * Reads an IP Address and returns it in a dot formated string |
|
413 | - * |
|
414 | - * @access public |
|
415 | - * @return Dot formated string, or a PEAR_Error if |
|
416 | - * not connected. |
|
417 | - */ |
|
418 | - function readIPAddress() |
|
419 | - { |
|
420 | - if (!is_resource($this->fp)) { |
|
421 | - return $this->raiseError('not connected'); |
|
422 | - } |
|
423 | - |
|
424 | - $buf = @fread($this->fp, 4); |
|
425 | - return sprintf("%s.%s.%s.%s", ord($buf[0]), ord($buf[1]), |
|
426 | - ord($buf[2]), ord($buf[3])); |
|
427 | - } |
|
428 | - |
|
429 | - /** |
|
430 | - * Read until either the end of the socket or a newline, whichever |
|
431 | - * comes first. Strips the trailing newline from the returned data. |
|
432 | - * |
|
433 | - * @access public |
|
434 | - * @return All available data up to a newline, without that |
|
435 | - * newline, or until the end of the socket, or a PEAR_Error if |
|
436 | - * not connected. |
|
437 | - */ |
|
438 | - function readLine() |
|
439 | - { |
|
440 | - if (!is_resource($this->fp)) { |
|
441 | - return $this->raiseError('not connected'); |
|
442 | - } |
|
443 | - |
|
444 | - $line = ''; |
|
445 | - $timeout = time() + $this->timeout; |
|
446 | - while (!feof($this->fp) && (!$this->timeout || time() < $timeout)) { |
|
447 | - $line .= @fgets($this->fp, $this->lineLength); |
|
448 | - if (substr($line, -1) == "\n") { |
|
449 | - return rtrim($line, "\r\n"); |
|
450 | - } |
|
451 | - } |
|
452 | - return $line; |
|
453 | - } |
|
454 | - |
|
455 | - /** |
|
456 | - * Read until the socket closes, or until there is no more data in |
|
457 | - * the inner PHP buffer. If the inner buffer is empty, in blocking |
|
458 | - * mode we wait for at least 1 byte of data. Therefore, in |
|
459 | - * blocking mode, if there is no data at all to be read, this |
|
460 | - * function will never exit (unless the socket is closed on the |
|
461 | - * remote end). |
|
462 | - * |
|
463 | - * @access public |
|
464 | - * |
|
465 | - * @return string All data until the socket closes, or a PEAR_Error if |
|
466 | - * not connected. |
|
467 | - */ |
|
468 | - function readAll() |
|
469 | - { |
|
470 | - if (!is_resource($this->fp)) { |
|
471 | - return $this->raiseError('not connected'); |
|
472 | - } |
|
473 | - |
|
474 | - $data = ''; |
|
475 | - while (!feof($this->fp)) { |
|
476 | - $data .= @fread($this->fp, $this->lineLength); |
|
477 | - } |
|
478 | - return $data; |
|
479 | - } |
|
480 | - |
|
481 | - /** |
|
482 | - * Runs the equivalent of the select() system call on the socket |
|
483 | - * with a timeout specified by tv_sec and tv_usec. |
|
484 | - * |
|
485 | - * @param integer $state Which of read/write/error to check for. |
|
486 | - * @param integer $tv_sec Number of seconds for timeout. |
|
487 | - * @param integer $tv_usec Number of microseconds for timeout. |
|
488 | - * |
|
489 | - * @access public |
|
490 | - * @return False if select fails, integer describing which of read/write/error |
|
491 | - * are ready, or PEAR_Error if not connected. |
|
492 | - */ |
|
493 | - function select($state, $tv_sec, $tv_usec = 0) |
|
494 | - { |
|
495 | - if (!is_resource($this->fp)) { |
|
496 | - return $this->raiseError('not connected'); |
|
497 | - } |
|
498 | - |
|
499 | - $read = null; |
|
500 | - $write = null; |
|
501 | - $except = null; |
|
502 | - if ($state & NET_SOCKET_READ) { |
|
503 | - $read[] = $this->fp; |
|
504 | - } |
|
505 | - if ($state & NET_SOCKET_WRITE) { |
|
506 | - $write[] = $this->fp; |
|
507 | - } |
|
508 | - if ($state & NET_SOCKET_ERROR) { |
|
509 | - $except[] = $this->fp; |
|
510 | - } |
|
511 | - if (false === ($sr = stream_select($read, $write, $except, $tv_sec, $tv_usec))) { |
|
512 | - return false; |
|
513 | - } |
|
514 | - |
|
515 | - $result = 0; |
|
516 | - if (count($read)) { |
|
517 | - $result |= NET_SOCKET_READ; |
|
518 | - } |
|
519 | - if (count($write)) { |
|
520 | - $result |= NET_SOCKET_WRITE; |
|
521 | - } |
|
522 | - if (count($except)) { |
|
523 | - $result |= NET_SOCKET_ERROR; |
|
524 | - } |
|
525 | - return $result; |
|
526 | - } |
|
37 | + /** |
|
38 | + * Socket file pointer. |
|
39 | + * @var resource $fp |
|
40 | + */ |
|
41 | + var $fp = null; |
|
42 | + |
|
43 | + /** |
|
44 | + * Whether the socket is blocking. Defaults to true. |
|
45 | + * @var boolean $blocking |
|
46 | + */ |
|
47 | + var $blocking = true; |
|
48 | + |
|
49 | + /** |
|
50 | + * Whether the socket is persistent. Defaults to false. |
|
51 | + * @var boolean $persistent |
|
52 | + */ |
|
53 | + var $persistent = false; |
|
54 | + |
|
55 | + /** |
|
56 | + * The IP address to connect to. |
|
57 | + * @var string $addr |
|
58 | + */ |
|
59 | + var $addr = ''; |
|
60 | + |
|
61 | + /** |
|
62 | + * The port number to connect to. |
|
63 | + * @var integer $port |
|
64 | + */ |
|
65 | + var $port = 0; |
|
66 | + |
|
67 | + /** |
|
68 | + * Number of seconds to wait on socket connections before assuming |
|
69 | + * there's no more data. Defaults to no timeout. |
|
70 | + * @var integer $timeout |
|
71 | + */ |
|
72 | + var $timeout = false; |
|
73 | + |
|
74 | + /** |
|
75 | + * Number of bytes to read at a time in readLine() and |
|
76 | + * readAll(). Defaults to 2048. |
|
77 | + * @var integer $lineLength |
|
78 | + */ |
|
79 | + var $lineLength = 2048; |
|
80 | + |
|
81 | + /** |
|
82 | + * Connect to the specified port. If called when the socket is |
|
83 | + * already connected, it disconnects and connects again. |
|
84 | + * |
|
85 | + * @param string $addr IP address or host name. |
|
86 | + * @param integer $port TCP port number. |
|
87 | + * @param boolean $persistent (optional) Whether the connection is |
|
88 | + * persistent (kept open between requests |
|
89 | + * by the web server). |
|
90 | + * @param integer $timeout (optional) How long to wait for data. |
|
91 | + * @param array $options See options for stream_context_create. |
|
92 | + * |
|
93 | + * @access public |
|
94 | + * |
|
95 | + * @return boolean | PEAR_Error True on success or a PEAR_Error on failure. |
|
96 | + */ |
|
97 | + function connect($addr, $port = 0, $persistent = null, $timeout = null, $options = null) |
|
98 | + { |
|
99 | + if (is_resource($this->fp)) { |
|
100 | + @fclose($this->fp); |
|
101 | + $this->fp = null; |
|
102 | + } |
|
103 | + |
|
104 | + if (!$addr) { |
|
105 | + return $this->raiseError('$addr cannot be empty'); |
|
106 | + } elseif (strspn($addr, '.0123456789') == strlen($addr) || |
|
107 | + strstr($addr, '/') !== false) { |
|
108 | + $this->addr = $addr; |
|
109 | + } else { |
|
110 | + $this->addr = @gethostbyname($addr); |
|
111 | + } |
|
112 | + |
|
113 | + $this->port = $port % 65536; |
|
114 | + |
|
115 | + if ($persistent !== null) { |
|
116 | + $this->persistent = $persistent; |
|
117 | + } |
|
118 | + |
|
119 | + if ($timeout !== null) { |
|
120 | + $this->timeout = $timeout; |
|
121 | + } |
|
122 | + |
|
123 | + $openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen'; |
|
124 | + $errno = 0; |
|
125 | + $errstr = ''; |
|
126 | + if ($options && function_exists('stream_context_create')) { |
|
127 | + if ($this->timeout) { |
|
128 | + $timeout = $this->timeout; |
|
129 | + } else { |
|
130 | + $timeout = 0; |
|
131 | + } |
|
132 | + $context = stream_context_create($options); |
|
133 | + $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $timeout, $context); |
|
134 | + } else { |
|
135 | + if ($this->timeout) { |
|
136 | + $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $this->timeout); |
|
137 | + } else { |
|
138 | + $fp = @$openfunc($this->addr, $this->port, $errno, $errstr); |
|
139 | + } |
|
140 | + } |
|
141 | + |
|
142 | + if (!$fp) { |
|
143 | + return $this->raiseError($errstr, $errno); |
|
144 | + } |
|
145 | + |
|
146 | + $this->fp = $fp; |
|
147 | + |
|
148 | + return $this->setBlocking($this->blocking); |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Disconnects from the peer, closes the socket. |
|
153 | + * |
|
154 | + * @access public |
|
155 | + * @return mixed true on success or an error object otherwise |
|
156 | + */ |
|
157 | + function disconnect() |
|
158 | + { |
|
159 | + if (!is_resource($this->fp)) { |
|
160 | + return $this->raiseError('not connected'); |
|
161 | + } |
|
162 | + |
|
163 | + @fclose($this->fp); |
|
164 | + $this->fp = null; |
|
165 | + return true; |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Find out if the socket is in blocking mode. |
|
170 | + * |
|
171 | + * @access public |
|
172 | + * @return boolean The current blocking mode. |
|
173 | + */ |
|
174 | + function isBlocking() |
|
175 | + { |
|
176 | + return $this->blocking; |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Sets whether the socket connection should be blocking or |
|
181 | + * not. A read call to a non-blocking socket will return immediately |
|
182 | + * if there is no data available, whereas it will block until there |
|
183 | + * is data for blocking sockets. |
|
184 | + * |
|
185 | + * @param boolean $mode True for blocking sockets, false for nonblocking. |
|
186 | + * @access public |
|
187 | + * @return mixed true on success or an error object otherwise |
|
188 | + */ |
|
189 | + function setBlocking($mode) |
|
190 | + { |
|
191 | + if (!is_resource($this->fp)) { |
|
192 | + return $this->raiseError('not connected'); |
|
193 | + } |
|
194 | + |
|
195 | + $this->blocking = $mode; |
|
196 | + socket_set_blocking($this->fp, $this->blocking); |
|
197 | + return true; |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * Sets the timeout value on socket descriptor, |
|
202 | + * expressed in the sum of seconds and microseconds |
|
203 | + * |
|
204 | + * @param integer $seconds Seconds. |
|
205 | + * @param integer $microseconds Microseconds. |
|
206 | + * @access public |
|
207 | + * @return mixed true on success or an error object otherwise |
|
208 | + */ |
|
209 | + function setTimeout($seconds, $microseconds) |
|
210 | + { |
|
211 | + if (!is_resource($this->fp)) { |
|
212 | + return $this->raiseError('not connected'); |
|
213 | + } |
|
214 | + |
|
215 | + return socket_set_timeout($this->fp, $seconds, $microseconds); |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * Returns information about an existing socket resource. |
|
220 | + * Currently returns four entries in the result array: |
|
221 | + * |
|
222 | + * <p> |
|
223 | + * timed_out (bool) - The socket timed out waiting for data<br> |
|
224 | + * blocked (bool) - The socket was blocked<br> |
|
225 | + * eof (bool) - Indicates EOF event<br> |
|
226 | + * unread_bytes (int) - Number of bytes left in the socket buffer<br> |
|
227 | + * </p> |
|
228 | + * |
|
229 | + * @access public |
|
230 | + * @return mixed Array containing information about existing socket resource or an error object otherwise |
|
231 | + */ |
|
232 | + function getStatus() |
|
233 | + { |
|
234 | + if (!is_resource($this->fp)) { |
|
235 | + return $this->raiseError('not connected'); |
|
236 | + } |
|
237 | + |
|
238 | + return socket_get_status($this->fp); |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * Get a specified line of data |
|
243 | + * |
|
244 | + * @access public |
|
245 | + * @return $size bytes of data from the socket, or a PEAR_Error if |
|
246 | + * not connected. |
|
247 | + */ |
|
248 | + function gets($size) |
|
249 | + { |
|
250 | + if (!is_resource($this->fp)) { |
|
251 | + return $this->raiseError('not connected'); |
|
252 | + } |
|
253 | + |
|
254 | + return @fgets($this->fp, $size); |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * Read a specified amount of data. This is guaranteed to return, |
|
259 | + * and has the added benefit of getting everything in one fread() |
|
260 | + * chunk; if you know the size of the data you're getting |
|
261 | + * beforehand, this is definitely the way to go. |
|
262 | + * |
|
263 | + * @param integer $size The number of bytes to read from the socket. |
|
264 | + * @access public |
|
265 | + * @return $size bytes of data from the socket, or a PEAR_Error if |
|
266 | + * not connected. |
|
267 | + */ |
|
268 | + function read($size) |
|
269 | + { |
|
270 | + if (!is_resource($this->fp)) { |
|
271 | + return $this->raiseError('not connected'); |
|
272 | + } |
|
273 | + |
|
274 | + return @fread($this->fp, $size); |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * Write a specified amount of data. |
|
279 | + * |
|
280 | + * @param string $data Data to write. |
|
281 | + * @param integer $blocksize Amount of data to write at once. |
|
282 | + * NULL means all at once. |
|
283 | + * |
|
284 | + * @access public |
|
285 | + * @return mixed true on success or an error object otherwise |
|
286 | + */ |
|
287 | + function write($data, $blocksize = null) |
|
288 | + { |
|
289 | + if (!is_resource($this->fp)) { |
|
290 | + return $this->raiseError('not connected'); |
|
291 | + } |
|
292 | + |
|
293 | + if (is_null($blocksize) && !OS_WINDOWS) { |
|
294 | + return fwrite($this->fp, $data); |
|
295 | + } else { |
|
296 | + if (is_null($blocksize)) { |
|
297 | + $blocksize = 1024; |
|
298 | + } |
|
299 | + |
|
300 | + $pos = 0; |
|
301 | + $size = strlen($data); |
|
302 | + while ($pos < $size) { |
|
303 | + $written = @fwrite($this->fp, substr($data, $pos, $blocksize)); |
|
304 | + if ($written === false) { |
|
305 | + return false; |
|
306 | + } |
|
307 | + $pos += $written; |
|
308 | + } |
|
309 | + |
|
310 | + return $pos; |
|
311 | + } |
|
312 | + } |
|
313 | + |
|
314 | + /** |
|
315 | + * Write a line of data to the socket, followed by a trailing "\r\n". |
|
316 | + * |
|
317 | + * @access public |
|
318 | + * @return mixed fputs result, or an error |
|
319 | + */ |
|
320 | + function writeLine($data) |
|
321 | + { |
|
322 | + if (!is_resource($this->fp)) { |
|
323 | + return $this->raiseError('not connected'); |
|
324 | + } |
|
325 | + |
|
326 | + return fwrite($this->fp, $data . "\r\n"); |
|
327 | + } |
|
328 | + |
|
329 | + /** |
|
330 | + * Tests for end-of-file on a socket descriptor. |
|
331 | + * |
|
332 | + * @access public |
|
333 | + * @return bool |
|
334 | + */ |
|
335 | + function eof() |
|
336 | + { |
|
337 | + return (is_resource($this->fp) && feof($this->fp)); |
|
338 | + } |
|
339 | + |
|
340 | + /** |
|
341 | + * Reads a byte of data |
|
342 | + * |
|
343 | + * @access public |
|
344 | + * @return 1 byte of data from the socket, or a PEAR_Error if |
|
345 | + * not connected. |
|
346 | + */ |
|
347 | + function readByte() |
|
348 | + { |
|
349 | + if (!is_resource($this->fp)) { |
|
350 | + return $this->raiseError('not connected'); |
|
351 | + } |
|
352 | + |
|
353 | + return ord(@fread($this->fp, 1)); |
|
354 | + } |
|
355 | + |
|
356 | + /** |
|
357 | + * Reads a word of data |
|
358 | + * |
|
359 | + * @access public |
|
360 | + * @return 1 word of data from the socket, or a PEAR_Error if |
|
361 | + * not connected. |
|
362 | + */ |
|
363 | + function readWord() |
|
364 | + { |
|
365 | + if (!is_resource($this->fp)) { |
|
366 | + return $this->raiseError('not connected'); |
|
367 | + } |
|
368 | + |
|
369 | + $buf = @fread($this->fp, 2); |
|
370 | + return (ord($buf[0]) + (ord($buf[1]) << 8)); |
|
371 | + } |
|
372 | + |
|
373 | + /** |
|
374 | + * Reads an int of data |
|
375 | + * |
|
376 | + * @access public |
|
377 | + * @return integer 1 int of data from the socket, or a PEAR_Error if |
|
378 | + * not connected. |
|
379 | + */ |
|
380 | + function readInt() |
|
381 | + { |
|
382 | + if (!is_resource($this->fp)) { |
|
383 | + return $this->raiseError('not connected'); |
|
384 | + } |
|
385 | + |
|
386 | + $buf = @fread($this->fp, 4); |
|
387 | + return (ord($buf[0]) + (ord($buf[1]) << 8) + |
|
388 | + (ord($buf[2]) << 16) + (ord($buf[3]) << 24)); |
|
389 | + } |
|
390 | + |
|
391 | + /** |
|
392 | + * Reads a zero-terminated string of data |
|
393 | + * |
|
394 | + * @access public |
|
395 | + * @return string, or a PEAR_Error if |
|
396 | + * not connected. |
|
397 | + */ |
|
398 | + function readString() |
|
399 | + { |
|
400 | + if (!is_resource($this->fp)) { |
|
401 | + return $this->raiseError('not connected'); |
|
402 | + } |
|
403 | + |
|
404 | + $string = ''; |
|
405 | + while (($char = @fread($this->fp, 1)) != "\x00") { |
|
406 | + $string .= $char; |
|
407 | + } |
|
408 | + return $string; |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * Reads an IP Address and returns it in a dot formated string |
|
413 | + * |
|
414 | + * @access public |
|
415 | + * @return Dot formated string, or a PEAR_Error if |
|
416 | + * not connected. |
|
417 | + */ |
|
418 | + function readIPAddress() |
|
419 | + { |
|
420 | + if (!is_resource($this->fp)) { |
|
421 | + return $this->raiseError('not connected'); |
|
422 | + } |
|
423 | + |
|
424 | + $buf = @fread($this->fp, 4); |
|
425 | + return sprintf("%s.%s.%s.%s", ord($buf[0]), ord($buf[1]), |
|
426 | + ord($buf[2]), ord($buf[3])); |
|
427 | + } |
|
428 | + |
|
429 | + /** |
|
430 | + * Read until either the end of the socket or a newline, whichever |
|
431 | + * comes first. Strips the trailing newline from the returned data. |
|
432 | + * |
|
433 | + * @access public |
|
434 | + * @return All available data up to a newline, without that |
|
435 | + * newline, or until the end of the socket, or a PEAR_Error if |
|
436 | + * not connected. |
|
437 | + */ |
|
438 | + function readLine() |
|
439 | + { |
|
440 | + if (!is_resource($this->fp)) { |
|
441 | + return $this->raiseError('not connected'); |
|
442 | + } |
|
443 | + |
|
444 | + $line = ''; |
|
445 | + $timeout = time() + $this->timeout; |
|
446 | + while (!feof($this->fp) && (!$this->timeout || time() < $timeout)) { |
|
447 | + $line .= @fgets($this->fp, $this->lineLength); |
|
448 | + if (substr($line, -1) == "\n") { |
|
449 | + return rtrim($line, "\r\n"); |
|
450 | + } |
|
451 | + } |
|
452 | + return $line; |
|
453 | + } |
|
454 | + |
|
455 | + /** |
|
456 | + * Read until the socket closes, or until there is no more data in |
|
457 | + * the inner PHP buffer. If the inner buffer is empty, in blocking |
|
458 | + * mode we wait for at least 1 byte of data. Therefore, in |
|
459 | + * blocking mode, if there is no data at all to be read, this |
|
460 | + * function will never exit (unless the socket is closed on the |
|
461 | + * remote end). |
|
462 | + * |
|
463 | + * @access public |
|
464 | + * |
|
465 | + * @return string All data until the socket closes, or a PEAR_Error if |
|
466 | + * not connected. |
|
467 | + */ |
|
468 | + function readAll() |
|
469 | + { |
|
470 | + if (!is_resource($this->fp)) { |
|
471 | + return $this->raiseError('not connected'); |
|
472 | + } |
|
473 | + |
|
474 | + $data = ''; |
|
475 | + while (!feof($this->fp)) { |
|
476 | + $data .= @fread($this->fp, $this->lineLength); |
|
477 | + } |
|
478 | + return $data; |
|
479 | + } |
|
480 | + |
|
481 | + /** |
|
482 | + * Runs the equivalent of the select() system call on the socket |
|
483 | + * with a timeout specified by tv_sec and tv_usec. |
|
484 | + * |
|
485 | + * @param integer $state Which of read/write/error to check for. |
|
486 | + * @param integer $tv_sec Number of seconds for timeout. |
|
487 | + * @param integer $tv_usec Number of microseconds for timeout. |
|
488 | + * |
|
489 | + * @access public |
|
490 | + * @return False if select fails, integer describing which of read/write/error |
|
491 | + * are ready, or PEAR_Error if not connected. |
|
492 | + */ |
|
493 | + function select($state, $tv_sec, $tv_usec = 0) |
|
494 | + { |
|
495 | + if (!is_resource($this->fp)) { |
|
496 | + return $this->raiseError('not connected'); |
|
497 | + } |
|
498 | + |
|
499 | + $read = null; |
|
500 | + $write = null; |
|
501 | + $except = null; |
|
502 | + if ($state & NET_SOCKET_READ) { |
|
503 | + $read[] = $this->fp; |
|
504 | + } |
|
505 | + if ($state & NET_SOCKET_WRITE) { |
|
506 | + $write[] = $this->fp; |
|
507 | + } |
|
508 | + if ($state & NET_SOCKET_ERROR) { |
|
509 | + $except[] = $this->fp; |
|
510 | + } |
|
511 | + if (false === ($sr = stream_select($read, $write, $except, $tv_sec, $tv_usec))) { |
|
512 | + return false; |
|
513 | + } |
|
514 | + |
|
515 | + $result = 0; |
|
516 | + if (count($read)) { |
|
517 | + $result |= NET_SOCKET_READ; |
|
518 | + } |
|
519 | + if (count($write)) { |
|
520 | + $result |= NET_SOCKET_WRITE; |
|
521 | + } |
|
522 | + if (count($except)) { |
|
523 | + $result |= NET_SOCKET_ERROR; |
|
524 | + } |
|
525 | + return $result; |
|
526 | + } |
|
527 | 527 | |
528 | 528 | } |
@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | |
22 | 22 | require_once 'PEAR.php'; |
23 | 23 | |
24 | -define('NET_SOCKET_READ', 1); |
|
24 | +define('NET_SOCKET_READ', 1); |
|
25 | 25 | define('NET_SOCKET_WRITE', 2); |
26 | 26 | define('NET_SOCKET_ERROR', 3); |
27 | 27 | |
@@ -323,7 +323,7 @@ discard block |
||
323 | 323 | return $this->raiseError('not connected'); |
324 | 324 | } |
325 | 325 | |
326 | - return fwrite($this->fp, $data . "\r\n"); |
|
326 | + return fwrite($this->fp, $data."\r\n"); |
|
327 | 327 | } |
328 | 328 | |
329 | 329 | /** |
@@ -402,7 +402,7 @@ discard block |
||
402 | 402 | } |
403 | 403 | |
404 | 404 | $string = ''; |
405 | - while (($char = @fread($this->fp, 1)) != "\x00") { |
|
405 | + while (($char = @fread($this->fp, 1)) != "\x00") { |
|
406 | 406 | $string .= $char; |
407 | 407 | } |
408 | 408 | return $string; |