1 | <?php |
---|
2 | /** |
---|
3 | * Class for performing HTTP requests |
---|
4 | * |
---|
5 | * PHP versions 4 and 5 |
---|
6 | * |
---|
7 | * LICENSE: |
---|
8 | * |
---|
9 | * Copyright (c) 2002-2007, Richard Heyes |
---|
10 | * All rights reserved. |
---|
11 | * |
---|
12 | * Redistribution and use in source and binary forms, with or without |
---|
13 | * modification, are permitted provided that the following conditions |
---|
14 | * are met: |
---|
15 | * |
---|
16 | * o Redistributions of source code must retain the above copyright |
---|
17 | * notice, this list of conditions and the following disclaimer. |
---|
18 | * o Redistributions in binary form must reproduce the above copyright |
---|
19 | * notice, this list of conditions and the following disclaimer in the |
---|
20 | * documentation and/or other materials provided with the distribution. |
---|
21 | * o The names of the authors may not be used to endorse or promote |
---|
22 | * products derived from this software without specific prior written |
---|
23 | * permission. |
---|
24 | * |
---|
25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
26 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
27 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
28 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
29 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
30 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
31 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
32 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
33 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
34 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
35 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
36 | * |
---|
37 | * @category HTTP |
---|
38 | * @package HTTP_Request |
---|
39 | * @author Richard Heyes <richard@phpguru.org> |
---|
40 | * @author Alexey Borzov <avb@php.net> |
---|
41 | * @copyright 2002-2007 Richard Heyes |
---|
42 | * @license http://opensource.org/licenses/bsd-license.php New BSD License |
---|
43 | * @version CVS: $Id$ |
---|
44 | * @link http://pear.php.net/package/HTTP_Request/ |
---|
45 | */ |
---|
46 | |
---|
47 | /** |
---|
48 | * PEAR and PEAR_Error classes (for error handling) |
---|
49 | */ |
---|
50 | require_once 'PEAR.php'; |
---|
51 | /** |
---|
52 | * Socket class |
---|
53 | */ |
---|
54 | require_once 'Net/Socket.php'; |
---|
55 | /** |
---|
56 | * URL handling class |
---|
57 | */ |
---|
58 | require_once 'Net/URL.php'; |
---|
59 | |
---|
60 | /**#@+ |
---|
61 | * Constants for HTTP request methods |
---|
62 | */ |
---|
63 | define('HTTP_REQUEST_METHOD_GET', 'GET', true); |
---|
64 | define('HTTP_REQUEST_METHOD_HEAD', 'HEAD', true); |
---|
65 | define('HTTP_REQUEST_METHOD_POST', 'POST', true); |
---|
66 | define('HTTP_REQUEST_METHOD_PUT', 'PUT', true); |
---|
67 | define('HTTP_REQUEST_METHOD_DELETE', 'DELETE', true); |
---|
68 | define('HTTP_REQUEST_METHOD_OPTIONS', 'OPTIONS', true); |
---|
69 | define('HTTP_REQUEST_METHOD_TRACE', 'TRACE', true); |
---|
70 | /**#@-*/ |
---|
71 | |
---|
72 | /**#@+ |
---|
73 | * Constants for HTTP request error codes |
---|
74 | */ |
---|
75 | define('HTTP_REQUEST_ERROR_FILE', 1); |
---|
76 | define('HTTP_REQUEST_ERROR_URL', 2); |
---|
77 | define('HTTP_REQUEST_ERROR_PROXY', 4); |
---|
78 | define('HTTP_REQUEST_ERROR_REDIRECTS', 8); |
---|
79 | define('HTTP_REQUEST_ERROR_RESPONSE', 16); |
---|
80 | define('HTTP_REQUEST_ERROR_GZIP_METHOD', 32); |
---|
81 | define('HTTP_REQUEST_ERROR_GZIP_READ', 64); |
---|
82 | define('HTTP_REQUEST_ERROR_GZIP_DATA', 128); |
---|
83 | define('HTTP_REQUEST_ERROR_GZIP_CRC', 256); |
---|
84 | /**#@-*/ |
---|
85 | |
---|
86 | /**#@+ |
---|
87 | * Constants for HTTP protocol versions |
---|
88 | */ |
---|
89 | define('HTTP_REQUEST_HTTP_VER_1_0', '1.0', true); |
---|
90 | define('HTTP_REQUEST_HTTP_VER_1_1', '1.1', true); |
---|
91 | /**#@-*/ |
---|
92 | |
---|
93 | if (extension_loaded('mbstring') && (2 & ini_get('mbstring.func_overload'))) { |
---|
94 | /** |
---|
95 | * Whether string functions are overloaded by their mbstring equivalents |
---|
96 | */ |
---|
97 | define('HTTP_REQUEST_MBSTRING', true); |
---|
98 | } else { |
---|
99 | /** |
---|
100 | * @ignore |
---|
101 | */ |
---|
102 | define('HTTP_REQUEST_MBSTRING', false); |
---|
103 | } |
---|
104 | |
---|
105 | /** |
---|
106 | * Class for performing HTTP requests |
---|
107 | * |
---|
108 | * Simple example (fetches yahoo.com and displays it): |
---|
109 | * <code> |
---|
110 | * $a = &new HTTP_Request('http://www.yahoo.com/'); |
---|
111 | * $a->sendRequest(); |
---|
112 | * echo $a->getResponseBody(); |
---|
113 | * </code> |
---|
114 | * |
---|
115 | * @category HTTP |
---|
116 | * @package HTTP_Request |
---|
117 | * @author Richard Heyes <richard@phpguru.org> |
---|
118 | * @author Alexey Borzov <avb@php.net> |
---|
119 | * @version Release: 1.4.4 |
---|
120 | */ |
---|
121 | class HTTP_Request |
---|
122 | { |
---|
123 | /**#@+ |
---|
124 | * @access private |
---|
125 | */ |
---|
126 | /** |
---|
127 | * Instance of Net_URL |
---|
128 | * @var Net_URL |
---|
129 | */ |
---|
130 | var $_url; |
---|
131 | |
---|
132 | /** |
---|
133 | * Type of request |
---|
134 | * @var string |
---|
135 | */ |
---|
136 | var $_method; |
---|
137 | |
---|
138 | /** |
---|
139 | * HTTP Version |
---|
140 | * @var string |
---|
141 | */ |
---|
142 | var $_http; |
---|
143 | |
---|
144 | /** |
---|
145 | * Request headers |
---|
146 | * @var array |
---|
147 | */ |
---|
148 | var $_requestHeaders; |
---|
149 | |
---|
150 | /** |
---|
151 | * Basic Auth Username |
---|
152 | * @var string |
---|
153 | */ |
---|
154 | var $_user; |
---|
155 | |
---|
156 | /** |
---|
157 | * Basic Auth Password |
---|
158 | * @var string |
---|
159 | */ |
---|
160 | var $_pass; |
---|
161 | |
---|
162 | /** |
---|
163 | * Socket object |
---|
164 | * @var Net_Socket |
---|
165 | */ |
---|
166 | var $_sock; |
---|
167 | |
---|
168 | /** |
---|
169 | * Proxy server |
---|
170 | * @var string |
---|
171 | */ |
---|
172 | var $_proxy_host; |
---|
173 | |
---|
174 | /** |
---|
175 | * Proxy port |
---|
176 | * @var integer |
---|
177 | */ |
---|
178 | var $_proxy_port; |
---|
179 | |
---|
180 | /** |
---|
181 | * Proxy username |
---|
182 | * @var string |
---|
183 | */ |
---|
184 | var $_proxy_user; |
---|
185 | |
---|
186 | /** |
---|
187 | * Proxy password |
---|
188 | * @var string |
---|
189 | */ |
---|
190 | var $_proxy_pass; |
---|
191 | |
---|
192 | /** |
---|
193 | * Post data |
---|
194 | * @var array |
---|
195 | */ |
---|
196 | var $_postData; |
---|
197 | |
---|
198 | /** |
---|
199 | * Request body |
---|
200 | * @var string |
---|
201 | */ |
---|
202 | var $_body; |
---|
203 | |
---|
204 | /** |
---|
205 | * A list of methods that MUST NOT have a request body, per RFC 2616 |
---|
206 | * @var array |
---|
207 | */ |
---|
208 | var $_bodyDisallowed = array('TRACE'); |
---|
209 | |
---|
210 | /** |
---|
211 | * Methods having defined semantics for request body |
---|
212 | * |
---|
213 | * Content-Length header (indicating that the body follows, section 4.3 of |
---|
214 | * RFC 2616) will be sent for these methods even if no body was added |
---|
215 | * |
---|
216 | * @var array |
---|
217 | */ |
---|
218 | var $_bodyRequired = array('POST', 'PUT'); |
---|
219 | |
---|
220 | /** |
---|
221 | * Files to post |
---|
222 | * @var array |
---|
223 | */ |
---|
224 | var $_postFiles = array(); |
---|
225 | |
---|
226 | /** |
---|
227 | * Connection timeout. |
---|
228 | * @var float |
---|
229 | */ |
---|
230 | var $_timeout; |
---|
231 | |
---|
232 | /** |
---|
233 | * HTTP_Response object |
---|
234 | * @var HTTP_Response |
---|
235 | */ |
---|
236 | var $_response; |
---|
237 | |
---|
238 | /** |
---|
239 | * Whether to allow redirects |
---|
240 | * @var boolean |
---|
241 | */ |
---|
242 | var $_allowRedirects; |
---|
243 | |
---|
244 | /** |
---|
245 | * Maximum redirects allowed |
---|
246 | * @var integer |
---|
247 | */ |
---|
248 | var $_maxRedirects; |
---|
249 | |
---|
250 | /** |
---|
251 | * Current number of redirects |
---|
252 | * @var integer |
---|
253 | */ |
---|
254 | var $_redirects; |
---|
255 | |
---|
256 | /** |
---|
257 | * Whether to append brackets [] to array variables |
---|
258 | * @var bool |
---|
259 | */ |
---|
260 | var $_useBrackets = true; |
---|
261 | |
---|
262 | /** |
---|
263 | * Attached listeners |
---|
264 | * @var array |
---|
265 | */ |
---|
266 | var $_listeners = array(); |
---|
267 | |
---|
268 | /** |
---|
269 | * Whether to save response body in response object property |
---|
270 | * @var bool |
---|
271 | */ |
---|
272 | var $_saveBody = true; |
---|
273 | |
---|
274 | /** |
---|
275 | * Timeout for reading from socket (array(seconds, microseconds)) |
---|
276 | * @var array |
---|
277 | */ |
---|
278 | var $_readTimeout = null; |
---|
279 | |
---|
280 | /** |
---|
281 | * Options to pass to Net_Socket::connect. See stream_context_create |
---|
282 | * @var array |
---|
283 | */ |
---|
284 | var $_socketOptions = null; |
---|
285 | /**#@-*/ |
---|
286 | |
---|
287 | /** |
---|
288 | * Constructor |
---|
289 | * |
---|
290 | * Sets up the object |
---|
291 | * @param string The url to fetch/access |
---|
292 | * @param array Associative array of parameters which can have the following keys: |
---|
293 | * <ul> |
---|
294 | * <li>method - Method to use, GET, POST etc (string)</li> |
---|
295 | * <li>http - HTTP Version to use, 1.0 or 1.1 (string)</li> |
---|
296 | * <li>user - Basic Auth username (string)</li> |
---|
297 | * <li>pass - Basic Auth password (string)</li> |
---|
298 | * <li>proxy_host - Proxy server host (string)</li> |
---|
299 | * <li>proxy_port - Proxy server port (integer)</li> |
---|
300 | * <li>proxy_user - Proxy auth username (string)</li> |
---|
301 | * <li>proxy_pass - Proxy auth password (string)</li> |
---|
302 | * <li>timeout - Connection timeout in seconds (float)</li> |
---|
303 | * <li>allowRedirects - Whether to follow redirects or not (bool)</li> |
---|
304 | * <li>maxRedirects - Max number of redirects to follow (integer)</li> |
---|
305 | * <li>useBrackets - Whether to append [] to array variable names (bool)</li> |
---|
306 | * <li>saveBody - Whether to save response body in response object property (bool)</li> |
---|
307 | * <li>readTimeout - Timeout for reading / writing data over the socket (array (seconds, microseconds))</li> |
---|
308 | * <li>socketOptions - Options to pass to Net_Socket object (array)</li> |
---|
309 | * </ul> |
---|
310 | * @access public |
---|
311 | */ |
---|
312 | function HTTP_Request($url = '', $params = array()) |
---|
313 | { |
---|
314 | $this->_method = HTTP_REQUEST_METHOD_GET; |
---|
315 | $this->_http = HTTP_REQUEST_HTTP_VER_1_1; |
---|
316 | $this->_requestHeaders = array(); |
---|
317 | $this->_postData = array(); |
---|
318 | $this->_body = null; |
---|
319 | |
---|
320 | $this->_user = null; |
---|
321 | $this->_pass = null; |
---|
322 | |
---|
323 | $this->_proxy_host = null; |
---|
324 | $this->_proxy_port = null; |
---|
325 | $this->_proxy_user = null; |
---|
326 | $this->_proxy_pass = null; |
---|
327 | |
---|
328 | $this->_allowRedirects = false; |
---|
329 | $this->_maxRedirects = 3; |
---|
330 | $this->_redirects = 0; |
---|
331 | |
---|
332 | $this->_timeout = null; |
---|
333 | $this->_response = null; |
---|
334 | |
---|
335 | foreach ($params as $key => $value) { |
---|
336 | $this->{'_' . $key} = $value; |
---|
337 | } |
---|
338 | |
---|
339 | if (!empty($url)) { |
---|
340 | $this->setURL($url); |
---|
341 | } |
---|
342 | |
---|
343 | // Default useragent |
---|
344 | $this->addHeader('User-Agent', 'PEAR HTTP_Request class ( http://pear.php.net/ )'); |
---|
345 | |
---|
346 | // We don't do keep-alives by default |
---|
347 | $this->addHeader('Connection', 'close'); |
---|
348 | |
---|
349 | // Basic authentication |
---|
350 | if (!empty($this->_user)) { |
---|
351 | $this->addHeader('Authorization', 'Basic ' . base64_encode($this->_user . ':' . $this->_pass)); |
---|
352 | } |
---|
353 | |
---|
354 | // Proxy authentication (see bug #5913) |
---|
355 | if (!empty($this->_proxy_user)) { |
---|
356 | $this->addHeader('Proxy-Authorization', 'Basic ' . base64_encode($this->_proxy_user . ':' . $this->_proxy_pass)); |
---|
357 | } |
---|
358 | |
---|
359 | // Use gzip encoding if possible |
---|
360 | if (HTTP_REQUEST_HTTP_VER_1_1 == $this->_http && extension_loaded('zlib')) { |
---|
361 | $this->addHeader('Accept-Encoding', 'gzip'); |
---|
362 | } |
---|
363 | } |
---|
364 | |
---|
365 | /** |
---|
366 | * Generates a Host header for HTTP/1.1 requests |
---|
367 | * |
---|
368 | * @access private |
---|
369 | * @return string |
---|
370 | */ |
---|
371 | function _generateHostHeader() |
---|
372 | { |
---|
373 | if ($this->_url->port != 80 AND strcasecmp($this->_url->protocol, 'http') == 0) { |
---|
374 | $host = $this->_url->host . ':' . $this->_url->port; |
---|
375 | |
---|
376 | } elseif ($this->_url->port != 443 AND strcasecmp($this->_url->protocol, 'https') == 0) { |
---|
377 | $host = $this->_url->host . ':' . $this->_url->port; |
---|
378 | |
---|
379 | } elseif ($this->_url->port == 443 AND strcasecmp($this->_url->protocol, 'https') == 0 AND strpos($this->_url->url, ':443') !== false) { |
---|
380 | $host = $this->_url->host . ':' . $this->_url->port; |
---|
381 | |
---|
382 | } else { |
---|
383 | $host = $this->_url->host; |
---|
384 | } |
---|
385 | |
---|
386 | return $host; |
---|
387 | } |
---|
388 | |
---|
389 | /** |
---|
390 | * Resets the object to its initial state (DEPRECATED). |
---|
391 | * Takes the same parameters as the constructor. |
---|
392 | * |
---|
393 | * @param string $url The url to be requested |
---|
394 | * @param array $params Associative array of parameters |
---|
395 | * (see constructor for details) |
---|
396 | * @access public |
---|
397 | * @deprecated deprecated since 1.2, call the constructor if this is necessary |
---|
398 | */ |
---|
399 | function reset($url, $params = array()) |
---|
400 | { |
---|
401 | $this->HTTP_Request($url, $params); |
---|
402 | } |
---|
403 | |
---|
404 | /** |
---|
405 | * Sets the URL to be requested |
---|
406 | * |
---|
407 | * @param string The url to be requested |
---|
408 | * @access public |
---|
409 | */ |
---|
410 | function setURL($url) |
---|
411 | { |
---|
412 | $this->_url = &new Net_URL($url, $this->_useBrackets); |
---|
413 | |
---|
414 | if (!empty($this->_url->user) || !empty($this->_url->pass)) { |
---|
415 | $this->setBasicAuth($this->_url->user, $this->_url->pass); |
---|
416 | } |
---|
417 | |
---|
418 | if (HTTP_REQUEST_HTTP_VER_1_1 == $this->_http) { |
---|
419 | $this->addHeader('Host', $this->_generateHostHeader()); |
---|
420 | } |
---|
421 | |
---|
422 | // set '/' instead of empty path rather than check later (see bug #8662) |
---|
423 | if (empty($this->_url->path)) { |
---|
424 | $this->_url->path = '/'; |
---|
425 | } |
---|
426 | } |
---|
427 | |
---|
428 | /** |
---|
429 | * Returns the current request URL |
---|
430 | * |
---|
431 | * @return string Current request URL |
---|
432 | * @access public |
---|
433 | */ |
---|
434 | function getUrl() |
---|
435 | { |
---|
436 | return empty($this->_url)? '': $this->_url->getUrl(); |
---|
437 | } |
---|
438 | |
---|
439 | /** |
---|
440 | * Sets a proxy to be used |
---|
441 | * |
---|
442 | * @param string Proxy host |
---|
443 | * @param int Proxy port |
---|
444 | * @param string Proxy username |
---|
445 | * @param string Proxy password |
---|
446 | * @access public |
---|
447 | */ |
---|
448 | function setProxy($host, $port = 8080, $user = null, $pass = null) |
---|
449 | { |
---|
450 | $this->_proxy_host = $host; |
---|
451 | $this->_proxy_port = $port; |
---|
452 | $this->_proxy_user = $user; |
---|
453 | $this->_proxy_pass = $pass; |
---|
454 | |
---|
455 | if (!empty($user)) { |
---|
456 | $this->addHeader('Proxy-Authorization', 'Basic ' . base64_encode($user . ':' . $pass)); |
---|
457 | } |
---|
458 | } |
---|
459 | |
---|
460 | /** |
---|
461 | * Sets basic authentication parameters |
---|
462 | * |
---|
463 | * @param string Username |
---|
464 | * @param string Password |
---|
465 | */ |
---|
466 | function setBasicAuth($user, $pass) |
---|
467 | { |
---|
468 | $this->_user = $user; |
---|
469 | $this->_pass = $pass; |
---|
470 | |
---|
471 | $this->addHeader('Authorization', 'Basic ' . base64_encode($user . ':' . $pass)); |
---|
472 | } |
---|
473 | |
---|
474 | /** |
---|
475 | * Sets the method to be used, GET, POST etc. |
---|
476 | * |
---|
477 | * @param string Method to use. Use the defined constants for this |
---|
478 | * @access public |
---|
479 | */ |
---|
480 | function setMethod($method) |
---|
481 | { |
---|
482 | $this->_method = $method; |
---|
483 | } |
---|
484 | |
---|
485 | /** |
---|
486 | * Sets the HTTP version to use, 1.0 or 1.1 |
---|
487 | * |
---|
488 | * @param string Version to use. Use the defined constants for this |
---|
489 | * @access public |
---|
490 | */ |
---|
491 | function setHttpVer($http) |
---|
492 | { |
---|
493 | $this->_http = $http; |
---|
494 | } |
---|
495 | |
---|
496 | /** |
---|
497 | * Adds a request header |
---|
498 | * |
---|
499 | * @param string Header name |
---|
500 | * @param string Header value |
---|
501 | * @access public |
---|
502 | */ |
---|
503 | function addHeader($name, $value) |
---|
504 | { |
---|
505 | $this->_requestHeaders[strtolower($name)] = $value; |
---|
506 | } |
---|
507 | |
---|
508 | /** |
---|
509 | * Removes a request header |
---|
510 | * |
---|
511 | * @param string Header name to remove |
---|
512 | * @access public |
---|
513 | */ |
---|
514 | function removeHeader($name) |
---|
515 | { |
---|
516 | if (isset($this->_requestHeaders[strtolower($name)])) { |
---|
517 | unset($this->_requestHeaders[strtolower($name)]); |
---|
518 | } |
---|
519 | } |
---|
520 | |
---|
521 | /** |
---|
522 | * Adds a querystring parameter |
---|
523 | * |
---|
524 | * @param string Querystring parameter name |
---|
525 | * @param string Querystring parameter value |
---|
526 | * @param bool Whether the value is already urlencoded or not, default = not |
---|
527 | * @access public |
---|
528 | */ |
---|
529 | function addQueryString($name, $value, $preencoded = false) |
---|
530 | { |
---|
531 | $this->_url->addQueryString($name, $value, $preencoded); |
---|
532 | } |
---|
533 | |
---|
534 | /** |
---|
535 | * Sets the querystring to literally what you supply |
---|
536 | * |
---|
537 | * @param string The querystring data. Should be of the format foo=bar&x=y etc |
---|
538 | * @param bool Whether data is already urlencoded or not, default = already encoded |
---|
539 | * @access public |
---|
540 | */ |
---|
541 | function addRawQueryString($querystring, $preencoded = true) |
---|
542 | { |
---|
543 | $this->_url->addRawQueryString($querystring, $preencoded); |
---|
544 | } |
---|
545 | |
---|
546 | /** |
---|
547 | * Adds postdata items |
---|
548 | * |
---|
549 | * @param string Post data name |
---|
550 | * @param string Post data value |
---|
551 | * @param bool Whether data is already urlencoded or not, default = not |
---|
552 | * @access public |
---|
553 | */ |
---|
554 | function addPostData($name, $value, $preencoded = false) |
---|
555 | { |
---|
556 | if ($preencoded) { |
---|
557 | $this->_postData[$name] = $value; |
---|
558 | } else { |
---|
559 | $this->_postData[$name] = $this->_arrayMapRecursive('urlencode', $value); |
---|
560 | } |
---|
561 | } |
---|
562 | |
---|
563 | function addPostDataArray($array, $preencoded = false) |
---|
564 | { |
---|
565 | foreach($array as $key => $val){ |
---|
566 | $this->addPostData($key, $val, $preencoded); |
---|
567 | } |
---|
568 | } |
---|
569 | |
---|
570 | /** |
---|
571 | * Recursively applies the callback function to the value |
---|
572 | * |
---|
573 | * @param mixed Callback function |
---|
574 | * @param mixed Value to process |
---|
575 | * @access private |
---|
576 | * @return mixed Processed value |
---|
577 | */ |
---|
578 | function _arrayMapRecursive($callback, $value) |
---|
579 | { |
---|
580 | if (!is_array($value)) { |
---|
581 | return call_user_func($callback, $value); |
---|
582 | } else { |
---|
583 | $map = array(); |
---|
584 | foreach ($value as $k => $v) { |
---|
585 | $map[$k] = $this->_arrayMapRecursive($callback, $v); |
---|
586 | } |
---|
587 | return $map; |
---|
588 | } |
---|
589 | } |
---|
590 | |
---|
591 | /** |
---|
592 | * Adds a file to form-based file upload |
---|
593 | * |
---|
594 | * Used to emulate file upload via a HTML form. The method also sets |
---|
595 | * Content-Type of HTTP request to 'multipart/form-data'. |
---|
596 | * |
---|
597 | * If you just want to send the contents of a file as the body of HTTP |
---|
598 | * request you should use setBody() method. |
---|
599 | * |
---|
600 | * @access public |
---|
601 | * @param string name of file-upload field |
---|
602 | * @param mixed file name(s) |
---|
603 | * @param mixed content-type(s) of file(s) being uploaded |
---|
604 | * @return bool true on success |
---|
605 | * @throws PEAR_Error |
---|
606 | */ |
---|
607 | function addFile($inputName, $fileName, $contentType = 'application/octet-stream') |
---|
608 | { |
---|
609 | if (!is_array($fileName) && !is_readable($fileName)) { |
---|
610 | return PEAR::raiseError("File '{$fileName}' is not readable", HTTP_REQUEST_ERROR_FILE); |
---|
611 | } elseif (is_array($fileName)) { |
---|
612 | foreach ($fileName as $name) { |
---|
613 | if (!is_readable($name)) { |
---|
614 | return PEAR::raiseError("File '{$name}' is not readable", HTTP_REQUEST_ERROR_FILE); |
---|
615 | } |
---|
616 | } |
---|
617 | } |
---|
618 | $this->addHeader('Content-Type', 'multipart/form-data'); |
---|
619 | $this->_postFiles[$inputName] = array( |
---|
620 | 'name' => $fileName, |
---|
621 | 'type' => $contentType |
---|
622 | ); |
---|
623 | return true; |
---|
624 | } |
---|
625 | |
---|
626 | /** |
---|
627 | * Adds raw postdata (DEPRECATED) |
---|
628 | * |
---|
629 | * @param string The data |
---|
630 | * @param bool Whether data is preencoded or not, default = already encoded |
---|
631 | * @access public |
---|
632 | * @deprecated deprecated since 1.3.0, method setBody() should be used instead |
---|
633 | */ |
---|
634 | function addRawPostData($postdata, $preencoded = true) |
---|
635 | { |
---|
636 | $this->_body = $preencoded ? $postdata : urlencode($postdata); |
---|
637 | } |
---|
638 | |
---|
639 | /** |
---|
640 | * Sets the request body (for POST, PUT and similar requests) |
---|
641 | * |
---|
642 | * @param string Request body |
---|
643 | * @access public |
---|
644 | */ |
---|
645 | function setBody($body) |
---|
646 | { |
---|
647 | $this->_body = $body; |
---|
648 | } |
---|
649 | |
---|
650 | /** |
---|
651 | * Clears any postdata that has been added (DEPRECATED). |
---|
652 | * |
---|
653 | * Useful for multiple request scenarios. |
---|
654 | * |
---|
655 | * @access public |
---|
656 | * @deprecated deprecated since 1.2 |
---|
657 | */ |
---|
658 | function clearPostData() |
---|
659 | { |
---|
660 | $this->_postData = null; |
---|
661 | } |
---|
662 | |
---|
663 | /** |
---|
664 | * Appends a cookie to "Cookie:" header |
---|
665 | * |
---|
666 | * @param string $name cookie name |
---|
667 | * @param string $value cookie value |
---|
668 | * @access public |
---|
669 | */ |
---|
670 | function addCookie($name, $value) |
---|
671 | { |
---|
672 | $cookies = isset($this->_requestHeaders['cookie']) ? $this->_requestHeaders['cookie']. '; ' : ''; |
---|
673 | $this->addHeader('Cookie', $cookies . $name . '=' . $value); |
---|
674 | } |
---|
675 | |
---|
676 | /** |
---|
677 | * Clears any cookies that have been added (DEPRECATED). |
---|
678 | * |
---|
679 | * Useful for multiple request scenarios |
---|
680 | * |
---|
681 | * @access public |
---|
682 | * @deprecated deprecated since 1.2 |
---|
683 | */ |
---|
684 | function clearCookies() |
---|
685 | { |
---|
686 | $this->removeHeader('Cookie'); |
---|
687 | } |
---|
688 | |
---|
689 | /** |
---|
690 | * Sends the request |
---|
691 | * |
---|
692 | * @access public |
---|
693 | * @param bool Whether to store response body in Response object property, |
---|
694 | * set this to false if downloading a LARGE file and using a Listener |
---|
695 | * @return mixed PEAR error on error, true otherwise |
---|
696 | */ |
---|
697 | function sendRequest($saveBody = true) |
---|
698 | { |
---|
699 | if (!is_a($this->_url, 'Net_URL')) { |
---|
700 | return PEAR::raiseError('No URL given', HTTP_REQUEST_ERROR_URL); |
---|
701 | } |
---|
702 | |
---|
703 | $host = isset($this->_proxy_host) ? $this->_proxy_host : $this->_url->host; |
---|
704 | $port = isset($this->_proxy_port) ? $this->_proxy_port : $this->_url->port; |
---|
705 | |
---|
706 | if (strcasecmp($this->_url->protocol, 'https') == 0) { |
---|
707 | // Bug #14127, don't try connecting to HTTPS sites without OpenSSL |
---|
708 | if (version_compare(PHP_VERSION, '4.3.0', '<') || !extension_loaded('openssl')) { |
---|
709 | return PEAR::raiseError('Need PHP 4.3.0 or later with OpenSSL support for https:// requests', |
---|
710 | HTTP_REQUEST_ERROR_URL); |
---|
711 | } elseif (isset($this->_proxy_host)) { |
---|
712 | return PEAR::raiseError('HTTPS proxies are not supported', HTTP_REQUEST_ERROR_PROXY); |
---|
713 | } |
---|
714 | $host = 'ssl://' . $host; |
---|
715 | } |
---|
716 | |
---|
717 | // magic quotes may fuck up file uploads and chunked response processing |
---|
718 | $magicQuotes = ini_get('magic_quotes_runtime'); |
---|
719 | ini_set('magic_quotes_runtime', false); |
---|
720 | |
---|
721 | // RFC 2068, section 19.7.1: A client MUST NOT send the Keep-Alive |
---|
722 | // connection token to a proxy server... |
---|
723 | if (isset($this->_proxy_host) && !empty($this->_requestHeaders['connection']) && |
---|
724 | 'Keep-Alive' == $this->_requestHeaders['connection']) |
---|
725 | { |
---|
726 | $this->removeHeader('connection'); |
---|
727 | } |
---|
728 | |
---|
729 | $keepAlive = (HTTP_REQUEST_HTTP_VER_1_1 == $this->_http && empty($this->_requestHeaders['connection'])) || |
---|
730 | (!empty($this->_requestHeaders['connection']) && 'Keep-Alive' == $this->_requestHeaders['connection']); |
---|
731 | $sockets = &PEAR::getStaticProperty('HTTP_Request', 'sockets'); |
---|
732 | $sockKey = $host . ':' . $port; |
---|
733 | unset($this->_sock); |
---|
734 | |
---|
735 | // There is a connected socket in the "static" property? |
---|
736 | if ($keepAlive && !empty($sockets[$sockKey]) && |
---|
737 | !empty($sockets[$sockKey]->fp)) |
---|
738 | { |
---|
739 | $this->_sock =& $sockets[$sockKey]; |
---|
740 | $err = null; |
---|
741 | } else { |
---|
742 | $this->_notify('connect'); |
---|
743 | $this->_sock =& new Net_Socket(); |
---|
744 | $err = $this->_sock->connect($host, $port, null, $this->_timeout, $this->_socketOptions); |
---|
745 | } |
---|
746 | PEAR::isError($err) or $err = $this->_sock->write($this->_buildRequest()); |
---|
747 | |
---|
748 | if (!PEAR::isError($err)) { |
---|
749 | if (!empty($this->_readTimeout)) { |
---|
750 | $this->_sock->setTimeout($this->_readTimeout[0], $this->_readTimeout[1]); |
---|
751 | } |
---|
752 | |
---|
753 | $this->_notify('sentRequest'); |
---|
754 | |
---|
755 | // Read the response |
---|
756 | $this->_response = &new HTTP_Response($this->_sock, $this->_listeners); |
---|
757 | $err = $this->_response->process( |
---|
758 | $this->_saveBody && $saveBody, |
---|
759 | HTTP_REQUEST_METHOD_HEAD != $this->_method |
---|
760 | ); |
---|
761 | |
---|
762 | if ($keepAlive) { |
---|
763 | $keepAlive = (isset($this->_response->_headers['content-length']) |
---|
764 | || (isset($this->_response->_headers['transfer-encoding']) |
---|
765 | && strtolower($this->_response->_headers['transfer-encoding']) == 'chunked')); |
---|
766 | if ($keepAlive) { |
---|
767 | if (isset($this->_response->_headers['connection'])) { |
---|
768 | $keepAlive = strtolower($this->_response->_headers['connection']) == 'keep-alive'; |
---|
769 | } else { |
---|
770 | $keepAlive = 'HTTP/'.HTTP_REQUEST_HTTP_VER_1_1 == $this->_response->_protocol; |
---|
771 | } |
---|
772 | } |
---|
773 | } |
---|
774 | } |
---|
775 | |
---|
776 | ini_set('magic_quotes_runtime', $magicQuotes); |
---|
777 | |
---|
778 | if (PEAR::isError($err)) { |
---|
779 | return $err; |
---|
780 | } |
---|
781 | |
---|
782 | if (!$keepAlive) { |
---|
783 | $this->disconnect(); |
---|
784 | // Store the connected socket in "static" property |
---|
785 | } elseif (empty($sockets[$sockKey]) || empty($sockets[$sockKey]->fp)) { |
---|
786 | $sockets[$sockKey] =& $this->_sock; |
---|
787 | } |
---|
788 | |
---|
789 | // Check for redirection |
---|
790 | if ( $this->_allowRedirects |
---|
791 | AND $this->_redirects <= $this->_maxRedirects |
---|
792 | AND $this->getResponseCode() > 300 |
---|
793 | AND $this->getResponseCode() < 399 |
---|
794 | AND !empty($this->_response->_headers['location'])) { |
---|
795 | |
---|
796 | |
---|
797 | $redirect = $this->_response->_headers['location']; |
---|
798 | |
---|
799 | // Absolute URL |
---|
800 | if (preg_match('/^https?:\/\//i', $redirect)) { |
---|
801 | $this->_url = &new Net_URL($redirect); |
---|
802 | $this->addHeader('Host', $this->_generateHostHeader()); |
---|
803 | // Absolute path |
---|
804 | } elseif ($redirect{0} == '/') { |
---|
805 | $this->_url->path = $redirect; |
---|
806 | |
---|
807 | // Relative path |
---|
808 | } elseif (substr($redirect, 0, 3) == '../' OR substr($redirect, 0, 2) == './') { |
---|
809 | if (substr($this->_url->path, -1) == '/') { |
---|
810 | $redirect = $this->_url->path . $redirect; |
---|
811 | } else { |
---|
812 | $redirect = dirname($this->_url->path) . '/' . $redirect; |
---|
813 | } |
---|
814 | $redirect = Net_URL::resolvePath($redirect); |
---|
815 | $this->_url->path = $redirect; |
---|
816 | |
---|
817 | // Filename, no path |
---|
818 | } else { |
---|
819 | if (substr($this->_url->path, -1) == '/') { |
---|
820 | $redirect = $this->_url->path . $redirect; |
---|
821 | } else { |
---|
822 | $redirect = dirname($this->_url->path) . '/' . $redirect; |
---|
823 | } |
---|
824 | $this->_url->path = $redirect; |
---|
825 | } |
---|
826 | |
---|
827 | $this->_redirects++; |
---|
828 | return $this->sendRequest($saveBody); |
---|
829 | |
---|
830 | // Too many redirects |
---|
831 | } elseif ($this->_allowRedirects AND $this->_redirects > $this->_maxRedirects) { |
---|
832 | return PEAR::raiseError('Too many redirects', HTTP_REQUEST_ERROR_REDIRECTS); |
---|
833 | } |
---|
834 | |
---|
835 | return true; |
---|
836 | } |
---|
837 | |
---|
838 | /** |
---|
839 | * Disconnect the socket, if connected. Only useful if using Keep-Alive. |
---|
840 | * |
---|
841 | * @access public |
---|
842 | */ |
---|
843 | function disconnect() |
---|
844 | { |
---|
845 | if (!empty($this->_sock) && !empty($this->_sock->fp)) { |
---|
846 | $this->_notify('disconnect'); |
---|
847 | $this->_sock->disconnect(); |
---|
848 | } |
---|
849 | } |
---|
850 | |
---|
851 | /** |
---|
852 | * Returns the response code |
---|
853 | * |
---|
854 | * @access public |
---|
855 | * @return mixed Response code, false if not set |
---|
856 | */ |
---|
857 | function getResponseCode() |
---|
858 | { |
---|
859 | return isset($this->_response->_code) ? $this->_response->_code : false; |
---|
860 | } |
---|
861 | |
---|
862 | /** |
---|
863 | * Returns the response reason phrase |
---|
864 | * |
---|
865 | * @access public |
---|
866 | * @return mixed Response reason phrase, false if not set |
---|
867 | */ |
---|
868 | function getResponseReason() |
---|
869 | { |
---|
870 | return isset($this->_response->_reason) ? $this->_response->_reason : false; |
---|
871 | } |
---|
872 | |
---|
873 | /** |
---|
874 | * Returns either the named header or all if no name given |
---|
875 | * |
---|
876 | * @access public |
---|
877 | * @param string The header name to return, do not set to get all headers |
---|
878 | * @return mixed either the value of $headername (false if header is not present) |
---|
879 | * or an array of all headers |
---|
880 | */ |
---|
881 | function getResponseHeader($headername = null) |
---|
882 | { |
---|
883 | if (!isset($headername)) { |
---|
884 | return isset($this->_response->_headers)? $this->_response->_headers: array(); |
---|
885 | } else { |
---|
886 | $headername = strtolower($headername); |
---|
887 | return isset($this->_response->_headers[$headername]) ? $this->_response->_headers[$headername] : false; |
---|
888 | } |
---|
889 | } |
---|
890 | |
---|
891 | /** |
---|
892 | * Returns the body of the response |
---|
893 | * |
---|
894 | * @access public |
---|
895 | * @return mixed response body, false if not set |
---|
896 | */ |
---|
897 | function getResponseBody() |
---|
898 | { |
---|
899 | return isset($this->_response->_body) ? $this->_response->_body : false; |
---|
900 | } |
---|
901 | |
---|
902 | /** |
---|
903 | * Returns cookies set in response |
---|
904 | * |
---|
905 | * @access public |
---|
906 | * @return mixed array of response cookies, false if none are present |
---|
907 | */ |
---|
908 | function getResponseCookies() |
---|
909 | { |
---|
910 | return isset($this->_response->_cookies) ? $this->_response->_cookies : false; |
---|
911 | } |
---|
912 | |
---|
913 | /** |
---|
914 | * Builds the request string |
---|
915 | * |
---|
916 | * @access private |
---|
917 | * @return string The request string |
---|
918 | */ |
---|
919 | function _buildRequest() |
---|
920 | { |
---|
921 | $separator = ini_get('arg_separator.output'); |
---|
922 | ini_set('arg_separator.output', '&'); |
---|
923 | $querystring = ($querystring = $this->_url->getQueryString()) ? '?' . $querystring : ''; |
---|
924 | ini_set('arg_separator.output', $separator); |
---|
925 | |
---|
926 | $host = isset($this->_proxy_host) ? $this->_url->protocol . '://' . $this->_url->host : ''; |
---|
927 | $port = (isset($this->_proxy_host) AND $this->_url->port != 80) ? ':' . $this->_url->port : ''; |
---|
928 | $path = $this->_url->path . $querystring; |
---|
929 | $url = $host . $port . $path; |
---|
930 | |
---|
931 | if (!strlen($url)) { |
---|
932 | $url = '/'; |
---|
933 | } |
---|
934 | |
---|
935 | $request = $this->_method . ' ' . $url . ' HTTP/' . $this->_http . "\r\n"; |
---|
936 | |
---|
937 | if (in_array($this->_method, $this->_bodyDisallowed) || |
---|
938 | (0 == strlen($this->_body) && (HTTP_REQUEST_METHOD_POST != $this->_method || |
---|
939 | (empty($this->_postData) && empty($this->_postFiles))))) |
---|
940 | { |
---|
941 | $this->removeHeader('Content-Type'); |
---|
942 | } else { |
---|
943 | if (empty($this->_requestHeaders['content-type'])) { |
---|
944 | // Add default content-type |
---|
945 | $this->addHeader('Content-Type', 'application/x-www-form-urlencoded'); |
---|
946 | } elseif ('multipart/form-data' == $this->_requestHeaders['content-type']) { |
---|
947 | $boundary = 'HTTP_Request_' . md5(uniqid('request') . microtime()); |
---|
948 | $this->addHeader('Content-Type', 'multipart/form-data; boundary=' . $boundary); |
---|
949 | } |
---|
950 | } |
---|
951 | |
---|
952 | // Request Headers |
---|
953 | if (!empty($this->_requestHeaders)) { |
---|
954 | foreach ($this->_requestHeaders as $name => $value) { |
---|
955 | $canonicalName = implode('-', array_map('ucfirst', explode('-', $name))); |
---|
956 | $request .= $canonicalName . ': ' . $value . "\r\n"; |
---|
957 | } |
---|
958 | } |
---|
959 | |
---|
960 | // Method does not allow a body, simply add a final CRLF |
---|
961 | if (in_array($this->_method, $this->_bodyDisallowed)) { |
---|
962 | |
---|
963 | $request .= "\r\n"; |
---|
964 | |
---|
965 | // Post data if it's an array |
---|
966 | } elseif (HTTP_REQUEST_METHOD_POST == $this->_method && |
---|
967 | (!empty($this->_postData) || !empty($this->_postFiles))) { |
---|
968 | |
---|
969 | // "normal" POST request |
---|
970 | if (!isset($boundary)) { |
---|
971 | $postdata = implode('&', array_map( |
---|
972 | create_function('$a', 'return $a[0] . \'=\' . $a[1];'), |
---|
973 | $this->_flattenArray('', $this->_postData) |
---|
974 | )); |
---|
975 | |
---|
976 | // multipart request, probably with file uploads |
---|
977 | } else { |
---|
978 | $postdata = ''; |
---|
979 | if (!empty($this->_postData)) { |
---|
980 | $flatData = $this->_flattenArray('', $this->_postData); |
---|
981 | foreach ($flatData as $item) { |
---|
982 | $postdata .= '--' . $boundary . "\r\n"; |
---|
983 | $postdata .= 'Content-Disposition: form-data; name="' . $item[0] . '"'; |
---|
984 | $postdata .= "\r\n\r\n" . urldecode($item[1]) . "\r\n"; |
---|
985 | } |
---|
986 | } |
---|
987 | foreach ($this->_postFiles as $name => $value) { |
---|
988 | if (is_array($value['name'])) { |
---|
989 | $varname = $name . ($this->_useBrackets? '[]': ''); |
---|
990 | } else { |
---|
991 | $varname = $name; |
---|
992 | $value['name'] = array($value['name']); |
---|
993 | } |
---|
994 | foreach ($value['name'] as $key => $filename) { |
---|
995 | $fp = fopen($filename, 'r'); |
---|
996 | $basename = basename($filename); |
---|
997 | $type = is_array($value['type'])? @$value['type'][$key]: $value['type']; |
---|
998 | |
---|
999 | $postdata .= '--' . $boundary . "\r\n"; |
---|
1000 | $postdata .= 'Content-Disposition: form-data; name="' . $varname . '"; filename="' . $basename . '"'; |
---|
1001 | $postdata .= "\r\nContent-Type: " . $type; |
---|
1002 | $postdata .= "\r\n\r\n" . fread($fp, filesize($filename)) . "\r\n"; |
---|
1003 | fclose($fp); |
---|
1004 | } |
---|
1005 | } |
---|
1006 | $postdata .= '--' . $boundary . "--\r\n"; |
---|
1007 | } |
---|
1008 | $request .= 'Content-Length: ' . |
---|
1009 | (HTTP_REQUEST_MBSTRING? mb_strlen($postdata, 'iso-8859-1'): strlen($postdata)) . |
---|
1010 | "\r\n\r\n"; |
---|
1011 | $request .= $postdata; |
---|
1012 | |
---|
1013 | // Explicitly set request body |
---|
1014 | } elseif (0 < strlen($this->_body)) { |
---|
1015 | |
---|
1016 | $request .= 'Content-Length: ' . |
---|
1017 | (HTTP_REQUEST_MBSTRING? mb_strlen($this->_body, 'iso-8859-1'): strlen($this->_body)) . |
---|
1018 | "\r\n\r\n"; |
---|
1019 | $request .= $this->_body; |
---|
1020 | |
---|
1021 | // No body: send a Content-Length header nonetheless (request #12900), |
---|
1022 | // but do that only for methods that require a body (bug #14740) |
---|
1023 | } else { |
---|
1024 | |
---|
1025 | if (in_array($this->_method, $this->_bodyRequired)) { |
---|
1026 | $request .= "Content-Length: 0\r\n"; |
---|
1027 | } |
---|
1028 | $request .= "\r\n"; |
---|
1029 | } |
---|
1030 | |
---|
1031 | return $request; |
---|
1032 | } |
---|
1033 | |
---|
1034 | /** |
---|
1035 | * Helper function to change the (probably multidimensional) associative array |
---|
1036 | * into the simple one. |
---|
1037 | * |
---|
1038 | * @param string name for item |
---|
1039 | * @param mixed item's values |
---|
1040 | * @return array array with the following items: array('item name', 'item value'); |
---|
1041 | * @access private |
---|
1042 | */ |
---|
1043 | function _flattenArray($name, $values) |
---|
1044 | { |
---|
1045 | if (!is_array($values)) { |
---|
1046 | return array(array($name, $values)); |
---|
1047 | } else { |
---|
1048 | $ret = array(); |
---|
1049 | foreach ($values as $k => $v) { |
---|
1050 | if (empty($name)) { |
---|
1051 | $newName = $k; |
---|
1052 | } elseif ($this->_useBrackets) { |
---|
1053 | $newName = $name . '[' . $k . ']'; |
---|
1054 | } else { |
---|
1055 | $newName = $name; |
---|
1056 | } |
---|
1057 | $ret = array_merge($ret, $this->_flattenArray($newName, $v)); |
---|
1058 | } |
---|
1059 | return $ret; |
---|
1060 | } |
---|
1061 | } |
---|
1062 | |
---|
1063 | |
---|
1064 | /** |
---|
1065 | * Adds a Listener to the list of listeners that are notified of |
---|
1066 | * the object's events |
---|
1067 | * |
---|
1068 | * Events sent by HTTP_Request object |
---|
1069 | * - 'connect': on connection to server |
---|
1070 | * - 'sentRequest': after the request was sent |
---|
1071 | * - 'disconnect': on disconnection from server |
---|
1072 | * |
---|
1073 | * Events sent by HTTP_Response object |
---|
1074 | * - 'gotHeaders': after receiving response headers (headers are passed in $data) |
---|
1075 | * - 'tick': on receiving a part of response body (the part is passed in $data) |
---|
1076 | * - 'gzTick': on receiving a gzip-encoded part of response body (ditto) |
---|
1077 | * - 'gotBody': after receiving the response body (passes the decoded body in $data if it was gzipped) |
---|
1078 | * |
---|
1079 | * @param HTTP_Request_Listener listener to attach |
---|
1080 | * @return boolean whether the listener was successfully attached |
---|
1081 | * @access public |
---|
1082 | */ |
---|
1083 | function attach(&$listener) |
---|
1084 | { |
---|
1085 | if (!is_a($listener, 'HTTP_Request_Listener')) { |
---|
1086 | return false; |
---|
1087 | } |
---|
1088 | $this->_listeners[$listener->getId()] =& $listener; |
---|
1089 | return true; |
---|
1090 | } |
---|
1091 | |
---|
1092 | |
---|
1093 | /** |
---|
1094 | * Removes a Listener from the list of listeners |
---|
1095 | * |
---|
1096 | * @param HTTP_Request_Listener listener to detach |
---|
1097 | * @return boolean whether the listener was successfully detached |
---|
1098 | * @access public |
---|
1099 | */ |
---|
1100 | function detach(&$listener) |
---|
1101 | { |
---|
1102 | if (!is_a($listener, 'HTTP_Request_Listener') || |
---|
1103 | !isset($this->_listeners[$listener->getId()])) { |
---|
1104 | return false; |
---|
1105 | } |
---|
1106 | unset($this->_listeners[$listener->getId()]); |
---|
1107 | return true; |
---|
1108 | } |
---|
1109 | |
---|
1110 | |
---|
1111 | /** |
---|
1112 | * Notifies all registered listeners of an event. |
---|
1113 | * |
---|
1114 | * @param string Event name |
---|
1115 | * @param mixed Additional data |
---|
1116 | * @access private |
---|
1117 | * @see HTTP_Request::attach() |
---|
1118 | */ |
---|
1119 | function _notify($event, $data = null) |
---|
1120 | { |
---|
1121 | foreach (array_keys($this->_listeners) as $id) { |
---|
1122 | $this->_listeners[$id]->update($this, $event, $data); |
---|
1123 | } |
---|
1124 | } |
---|
1125 | } |
---|
1126 | |
---|
1127 | |
---|
1128 | /** |
---|
1129 | * Response class to complement the Request class |
---|
1130 | * |
---|
1131 | * @category HTTP |
---|
1132 | * @package HTTP_Request |
---|
1133 | * @author Richard Heyes <richard@phpguru.org> |
---|
1134 | * @author Alexey Borzov <avb@php.net> |
---|
1135 | * @version Release: 1.4.4 |
---|
1136 | */ |
---|
1137 | class HTTP_Response |
---|
1138 | { |
---|
1139 | /** |
---|
1140 | * Socket object |
---|
1141 | * @var Net_Socket |
---|
1142 | */ |
---|
1143 | var $_sock; |
---|
1144 | |
---|
1145 | /** |
---|
1146 | * Protocol |
---|
1147 | * @var string |
---|
1148 | */ |
---|
1149 | var $_protocol; |
---|
1150 | |
---|
1151 | /** |
---|
1152 | * Return code |
---|
1153 | * @var string |
---|
1154 | */ |
---|
1155 | var $_code; |
---|
1156 | |
---|
1157 | /** |
---|
1158 | * Response reason phrase |
---|
1159 | * @var string |
---|
1160 | */ |
---|
1161 | var $_reason; |
---|
1162 | |
---|
1163 | /** |
---|
1164 | * Response headers |
---|
1165 | * @var array |
---|
1166 | */ |
---|
1167 | var $_headers; |
---|
1168 | |
---|
1169 | /** |
---|
1170 | * Cookies set in response |
---|
1171 | * @var array |
---|
1172 | */ |
---|
1173 | var $_cookies; |
---|
1174 | |
---|
1175 | /** |
---|
1176 | * Response body |
---|
1177 | * @var string |
---|
1178 | */ |
---|
1179 | var $_body = ''; |
---|
1180 | |
---|
1181 | /** |
---|
1182 | * Used by _readChunked(): remaining length of the current chunk |
---|
1183 | * @var string |
---|
1184 | */ |
---|
1185 | var $_chunkLength = 0; |
---|
1186 | |
---|
1187 | /** |
---|
1188 | * Attached listeners |
---|
1189 | * @var array |
---|
1190 | */ |
---|
1191 | var $_listeners = array(); |
---|
1192 | |
---|
1193 | /** |
---|
1194 | * Bytes left to read from message-body |
---|
1195 | * @var null|int |
---|
1196 | */ |
---|
1197 | var $_toRead; |
---|
1198 | |
---|
1199 | /** |
---|
1200 | * Constructor |
---|
1201 | * |
---|
1202 | * @param Net_Socket socket to read the response from |
---|
1203 | * @param array listeners attached to request |
---|
1204 | */ |
---|
1205 | function HTTP_Response(&$sock, &$listeners) |
---|
1206 | { |
---|
1207 | $this->_sock =& $sock; |
---|
1208 | $this->_listeners =& $listeners; |
---|
1209 | } |
---|
1210 | |
---|
1211 | |
---|
1212 | /** |
---|
1213 | * Processes a HTTP response |
---|
1214 | * |
---|
1215 | * This extracts response code, headers, cookies and decodes body if it |
---|
1216 | * was encoded in some way |
---|
1217 | * |
---|
1218 | * @access public |
---|
1219 | * @param bool Whether to store response body in object property, set |
---|
1220 | * this to false if downloading a LARGE file and using a Listener. |
---|
1221 | * This is assumed to be true if body is gzip-encoded. |
---|
1222 | * @param bool Whether the response can actually have a message-body. |
---|
1223 | * Will be set to false for HEAD requests. |
---|
1224 | * @throws PEAR_Error |
---|
1225 | * @return mixed true on success, PEAR_Error in case of malformed response |
---|
1226 | */ |
---|
1227 | function process($saveBody = true, $canHaveBody = true) |
---|
1228 | { |
---|
1229 | do { |
---|
1230 | $line = $this->_sock->readLine(); |
---|
1231 | if (!preg_match('!^(HTTP/\d\.\d) (\d{3})(?: (.+))?!', $line, $s)) { |
---|
1232 | return PEAR::raiseError('Malformed response', HTTP_REQUEST_ERROR_RESPONSE); |
---|
1233 | } else { |
---|
1234 | $this->_protocol = $s[1]; |
---|
1235 | $this->_code = intval($s[2]); |
---|
1236 | $this->_reason = empty($s[3])? null: $s[3]; |
---|
1237 | } |
---|
1238 | while ('' !== ($header = $this->_sock->readLine())) { |
---|
1239 | $this->_processHeader($header); |
---|
1240 | } |
---|
1241 | } while (100 == $this->_code); |
---|
1242 | |
---|
1243 | $this->_notify('gotHeaders', $this->_headers); |
---|
1244 | |
---|
1245 | // RFC 2616, section 4.4: |
---|
1246 | // 1. Any response message which "MUST NOT" include a message-body ... |
---|
1247 | // is always terminated by the first empty line after the header fields |
---|
1248 | // 3. ... If a message is received with both a |
---|
1249 | // Transfer-Encoding header field and a Content-Length header field, |
---|
1250 | // the latter MUST be ignored. |
---|
1251 | $canHaveBody = $canHaveBody && $this->_code >= 200 && |
---|
1252 | $this->_code != 204 && $this->_code != 304; |
---|
1253 | |
---|
1254 | // If response body is present, read it and decode |
---|
1255 | $chunked = isset($this->_headers['transfer-encoding']) && ('chunked' == $this->_headers['transfer-encoding']); |
---|
1256 | $gzipped = isset($this->_headers['content-encoding']) && ('gzip' == $this->_headers['content-encoding']); |
---|
1257 | $hasBody = false; |
---|
1258 | if ($canHaveBody && ($chunked || !isset($this->_headers['content-length']) || |
---|
1259 | 0 != $this->_headers['content-length'])) |
---|
1260 | { |
---|
1261 | if ($chunked || !isset($this->_headers['content-length'])) { |
---|
1262 | $this->_toRead = null; |
---|
1263 | } else { |
---|
1264 | $this->_toRead = $this->_headers['content-length']; |
---|
1265 | } |
---|
1266 | while (!$this->_sock->eof() && (is_null($this->_toRead) || 0 < $this->_toRead)) { |
---|
1267 | if ($chunked) { |
---|
1268 | $data = $this->_readChunked(); |
---|
1269 | } elseif (is_null($this->_toRead)) { |
---|
1270 | $data = $this->_sock->read(4096); |
---|
1271 | } else { |
---|
1272 | $data = $this->_sock->read(min(4096, $this->_toRead)); |
---|
1273 | $this->_toRead -= HTTP_REQUEST_MBSTRING? mb_strlen($data, 'iso-8859-1'): strlen($data); |
---|
1274 | } |
---|
1275 | if ('' == $data && (!$this->_chunkLength || $this->_sock->eof())) { |
---|
1276 | break; |
---|
1277 | } else { |
---|
1278 | $hasBody = true; |
---|
1279 | if ($saveBody || $gzipped) { |
---|
1280 | $this->_body .= $data; |
---|
1281 | } |
---|
1282 | $this->_notify($gzipped? 'gzTick': 'tick', $data); |
---|
1283 | } |
---|
1284 | } |
---|
1285 | } |
---|
1286 | |
---|
1287 | if ($hasBody) { |
---|
1288 | // Uncompress the body if needed |
---|
1289 | if ($gzipped) { |
---|
1290 | $body = $this->_decodeGzip($this->_body); |
---|
1291 | if (PEAR::isError($body)) { |
---|
1292 | return $body; |
---|
1293 | } |
---|
1294 | $this->_body = $body; |
---|
1295 | $this->_notify('gotBody', $this->_body); |
---|
1296 | } else { |
---|
1297 | $this->_notify('gotBody'); |
---|
1298 | } |
---|
1299 | } |
---|
1300 | return true; |
---|
1301 | } |
---|
1302 | |
---|
1303 | |
---|
1304 | /** |
---|
1305 | * Processes the response header |
---|
1306 | * |
---|
1307 | * @access private |
---|
1308 | * @param string HTTP header |
---|
1309 | */ |
---|
1310 | function _processHeader($header) |
---|
1311 | { |
---|
1312 | if (false === strpos($header, ':')) { |
---|
1313 | return; |
---|
1314 | } |
---|
1315 | list($headername, $headervalue) = explode(':', $header, 2); |
---|
1316 | $headername = strtolower($headername); |
---|
1317 | $headervalue = ltrim($headervalue); |
---|
1318 | |
---|
1319 | if ('set-cookie' != $headername) { |
---|
1320 | if (isset($this->_headers[$headername])) { |
---|
1321 | $this->_headers[$headername] .= ',' . $headervalue; |
---|
1322 | } else { |
---|
1323 | $this->_headers[$headername] = $headervalue; |
---|
1324 | } |
---|
1325 | } else { |
---|
1326 | $this->_parseCookie($headervalue); |
---|
1327 | } |
---|
1328 | } |
---|
1329 | |
---|
1330 | |
---|
1331 | /** |
---|
1332 | * Parse a Set-Cookie header to fill $_cookies array |
---|
1333 | * |
---|
1334 | * @access private |
---|
1335 | * @param string value of Set-Cookie header |
---|
1336 | */ |
---|
1337 | function _parseCookie($headervalue) |
---|
1338 | { |
---|
1339 | $cookie = array( |
---|
1340 | 'expires' => null, |
---|
1341 | 'domain' => null, |
---|
1342 | 'path' => null, |
---|
1343 | 'secure' => false |
---|
1344 | ); |
---|
1345 | |
---|
1346 | // Only a name=value pair |
---|
1347 | if (!strpos($headervalue, ';')) { |
---|
1348 | $pos = strpos($headervalue, '='); |
---|
1349 | $cookie['name'] = trim(substr($headervalue, 0, $pos)); |
---|
1350 | $cookie['value'] = trim(substr($headervalue, $pos + 1)); |
---|
1351 | |
---|
1352 | // Some optional parameters are supplied |
---|
1353 | } else { |
---|
1354 | $elements = explode(';', $headervalue); |
---|
1355 | $pos = strpos($elements[0], '='); |
---|
1356 | $cookie['name'] = trim(substr($elements[0], 0, $pos)); |
---|
1357 | $cookie['value'] = trim(substr($elements[0], $pos + 1)); |
---|
1358 | |
---|
1359 | for ($i = 1; $i < count($elements); $i++) { |
---|
1360 | if (false === strpos($elements[$i], '=')) { |
---|
1361 | $elName = trim($elements[$i]); |
---|
1362 | $elValue = null; |
---|
1363 | } else { |
---|
1364 | list ($elName, $elValue) = array_map('trim', explode('=', $elements[$i])); |
---|
1365 | } |
---|
1366 | $elName = strtolower($elName); |
---|
1367 | if ('secure' == $elName) { |
---|
1368 | $cookie['secure'] = true; |
---|
1369 | } elseif ('expires' == $elName) { |
---|
1370 | $cookie['expires'] = str_replace('"', '', $elValue); |
---|
1371 | } elseif ('path' == $elName || 'domain' == $elName) { |
---|
1372 | $cookie[$elName] = urldecode($elValue); |
---|
1373 | } else { |
---|
1374 | $cookie[$elName] = $elValue; |
---|
1375 | } |
---|
1376 | } |
---|
1377 | } |
---|
1378 | $this->_cookies[] = $cookie; |
---|
1379 | } |
---|
1380 | |
---|
1381 | |
---|
1382 | /** |
---|
1383 | * Read a part of response body encoded with chunked Transfer-Encoding |
---|
1384 | * |
---|
1385 | * @access private |
---|
1386 | * @return string |
---|
1387 | */ |
---|
1388 | function _readChunked() |
---|
1389 | { |
---|
1390 | // at start of the next chunk? |
---|
1391 | if (0 == $this->_chunkLength) { |
---|
1392 | $line = $this->_sock->readLine(); |
---|
1393 | if (preg_match('/^([0-9a-f]+)/i', $line, $matches)) { |
---|
1394 | $this->_chunkLength = hexdec($matches[1]); |
---|
1395 | // Chunk with zero length indicates the end |
---|
1396 | if (0 == $this->_chunkLength) { |
---|
1397 | $this->_sock->readLine(); // make this an eof() |
---|
1398 | return ''; |
---|
1399 | } |
---|
1400 | } else { |
---|
1401 | return ''; |
---|
1402 | } |
---|
1403 | } |
---|
1404 | $data = $this->_sock->read($this->_chunkLength); |
---|
1405 | $this->_chunkLength -= HTTP_REQUEST_MBSTRING? mb_strlen($data, 'iso-8859-1'): strlen($data); |
---|
1406 | if (0 == $this->_chunkLength) { |
---|
1407 | $this->_sock->readLine(); // Trailing CRLF |
---|
1408 | } |
---|
1409 | return $data; |
---|
1410 | } |
---|
1411 | |
---|
1412 | |
---|
1413 | /** |
---|
1414 | * Notifies all registered listeners of an event. |
---|
1415 | * |
---|
1416 | * @param string Event name |
---|
1417 | * @param mixed Additional data |
---|
1418 | * @access private |
---|
1419 | * @see HTTP_Request::_notify() |
---|
1420 | */ |
---|
1421 | function _notify($event, $data = null) |
---|
1422 | { |
---|
1423 | foreach (array_keys($this->_listeners) as $id) { |
---|
1424 | $this->_listeners[$id]->update($this, $event, $data); |
---|
1425 | } |
---|
1426 | } |
---|
1427 | |
---|
1428 | |
---|
1429 | /** |
---|
1430 | * Decodes the message-body encoded by gzip |
---|
1431 | * |
---|
1432 | * The real decoding work is done by gzinflate() built-in function, this |
---|
1433 | * method only parses the header and checks data for compliance with |
---|
1434 | * RFC 1952 |
---|
1435 | * |
---|
1436 | * @access private |
---|
1437 | * @param string gzip-encoded data |
---|
1438 | * @return string decoded data |
---|
1439 | */ |
---|
1440 | function _decodeGzip($data) |
---|
1441 | { |
---|
1442 | if (HTTP_REQUEST_MBSTRING) { |
---|
1443 | $oldEncoding = mb_internal_encoding(); |
---|
1444 | mb_internal_encoding('iso-8859-1'); |
---|
1445 | } |
---|
1446 | $length = strlen($data); |
---|
1447 | // If it doesn't look like gzip-encoded data, don't bother |
---|
1448 | if (18 > $length || strcmp(substr($data, 0, 2), "\x1f\x8b")) { |
---|
1449 | return $data; |
---|
1450 | } |
---|
1451 | $method = ord(substr($data, 2, 1)); |
---|
1452 | if (8 != $method) { |
---|
1453 | return PEAR::raiseError('_decodeGzip(): unknown compression method', HTTP_REQUEST_ERROR_GZIP_METHOD); |
---|
1454 | } |
---|
1455 | $flags = ord(substr($data, 3, 1)); |
---|
1456 | if ($flags & 224) { |
---|
1457 | return PEAR::raiseError('_decodeGzip(): reserved bits are set', HTTP_REQUEST_ERROR_GZIP_DATA); |
---|
1458 | } |
---|
1459 | |
---|
1460 | // header is 10 bytes minimum. may be longer, though. |
---|
1461 | $headerLength = 10; |
---|
1462 | // extra fields, need to skip 'em |
---|
1463 | if ($flags & 4) { |
---|
1464 | if ($length - $headerLength - 2 < 8) { |
---|
1465 | return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA); |
---|
1466 | } |
---|
1467 | $extraLength = unpack('v', substr($data, 10, 2)); |
---|
1468 | if ($length - $headerLength - 2 - $extraLength[1] < 8) { |
---|
1469 | return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA); |
---|
1470 | } |
---|
1471 | $headerLength += $extraLength[1] + 2; |
---|
1472 | } |
---|
1473 | // file name, need to skip that |
---|
1474 | if ($flags & 8) { |
---|
1475 | if ($length - $headerLength - 1 < 8) { |
---|
1476 | return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA); |
---|
1477 | } |
---|
1478 | $filenameLength = strpos(substr($data, $headerLength), chr(0)); |
---|
1479 | if (false === $filenameLength || $length - $headerLength - $filenameLength - 1 < 8) { |
---|
1480 | return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA); |
---|
1481 | } |
---|
1482 | $headerLength += $filenameLength + 1; |
---|
1483 | } |
---|
1484 | // comment, need to skip that also |
---|
1485 | if ($flags & 16) { |
---|
1486 | if ($length - $headerLength - 1 < 8) { |
---|
1487 | return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA); |
---|
1488 | } |
---|
1489 | $commentLength = strpos(substr($data, $headerLength), chr(0)); |
---|
1490 | if (false === $commentLength || $length - $headerLength - $commentLength - 1 < 8) { |
---|
1491 | return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA); |
---|
1492 | } |
---|
1493 | $headerLength += $commentLength + 1; |
---|
1494 | } |
---|
1495 | // have a CRC for header. let's check |
---|
1496 | if ($flags & 1) { |
---|
1497 | if ($length - $headerLength - 2 < 8) { |
---|
1498 | return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA); |
---|
1499 | } |
---|
1500 | $crcReal = 0xffff & crc32(substr($data, 0, $headerLength)); |
---|
1501 | $crcStored = unpack('v', substr($data, $headerLength, 2)); |
---|
1502 | if ($crcReal != $crcStored[1]) { |
---|
1503 | return PEAR::raiseError('_decodeGzip(): header CRC check failed', HTTP_REQUEST_ERROR_GZIP_CRC); |
---|
1504 | } |
---|
1505 | $headerLength += 2; |
---|
1506 | } |
---|
1507 | // unpacked data CRC and size at the end of encoded data |
---|
1508 | $tmp = unpack('V2', substr($data, -8)); |
---|
1509 | $dataCrc = $tmp[1]; |
---|
1510 | $dataSize = $tmp[2]; |
---|
1511 | |
---|
1512 | // finally, call the gzinflate() function |
---|
1513 | // don't pass $dataSize to gzinflate, see bugs #13135, #14370 |
---|
1514 | $unpacked = gzinflate(substr($data, $headerLength, -8)); |
---|
1515 | if (false === $unpacked) { |
---|
1516 | return PEAR::raiseError('_decodeGzip(): gzinflate() call failed', HTTP_REQUEST_ERROR_GZIP_READ); |
---|
1517 | } elseif ($dataSize != strlen($unpacked)) { |
---|
1518 | return PEAR::raiseError('_decodeGzip(): data size check failed', HTTP_REQUEST_ERROR_GZIP_READ); |
---|
1519 | } elseif ((0xffffffff & $dataCrc) != (0xffffffff & crc32($unpacked))) { |
---|
1520 | return PEAR::raiseError('_decodeGzip(): data CRC check failed', HTTP_REQUEST_ERROR_GZIP_CRC); |
---|
1521 | } |
---|
1522 | if (HTTP_REQUEST_MBSTRING) { |
---|
1523 | mb_internal_encoding($oldEncoding); |
---|
1524 | } |
---|
1525 | return $unpacked; |
---|
1526 | } |
---|
1527 | } // End class HTTP_Response |
---|
1528 | ?> |
---|