1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * Zend Framework |
---|
5 | * |
---|
6 | * LICENSE |
---|
7 | * |
---|
8 | * This source file is subject to the new BSD license that is bundled |
---|
9 | * with this package in the file LICENSE.txt. |
---|
10 | * It is also available through the world-wide-web at this URL: |
---|
11 | * http://framework.zend.com/license/new-bsd |
---|
12 | * If you did not receive a copy of the license and are unable to |
---|
13 | * obtain it through the world-wide-web, please send an email |
---|
14 | * to license@zend.com so we can send you a copy immediately. |
---|
15 | * |
---|
16 | * @category Zend |
---|
17 | * @package Zend_Http |
---|
18 | * @subpackage Client |
---|
19 | * @version $Id: Client.php 24594 2012-01-05 21:27:01Z matthew $ |
---|
20 | * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
---|
21 | * @license http://framework.zend.com/license/new-bsd New BSD License |
---|
22 | */ |
---|
23 | |
---|
24 | /** |
---|
25 | * @see Zend_Loader |
---|
26 | */ |
---|
27 | require_once 'Zend/Loader.php'; |
---|
28 | |
---|
29 | |
---|
30 | /** |
---|
31 | * @see Zend_Uri |
---|
32 | */ |
---|
33 | require_once 'Zend/Uri.php'; |
---|
34 | |
---|
35 | |
---|
36 | /** |
---|
37 | * @see Zend_Http_Client_Adapter_Interface |
---|
38 | */ |
---|
39 | require_once 'Zend/Http/Client/Adapter/Interface.php'; |
---|
40 | |
---|
41 | |
---|
42 | /** |
---|
43 | * @see Zend_Http_Response |
---|
44 | */ |
---|
45 | require_once 'Zend/Http/Response.php'; |
---|
46 | |
---|
47 | /** |
---|
48 | * @see Zend_Http_Response_Stream |
---|
49 | */ |
---|
50 | require_once 'Zend/Http/Response/Stream.php'; |
---|
51 | |
---|
52 | /** |
---|
53 | * Zend_Http_Client is an implementation of an HTTP client in PHP. The client |
---|
54 | * supports basic features like sending different HTTP requests and handling |
---|
55 | * redirections, as well as more advanced features like proxy settings, HTTP |
---|
56 | * authentication and cookie persistence (using a Zend_Http_CookieJar object) |
---|
57 | * |
---|
58 | * @todo Implement proxy settings |
---|
59 | * @category Zend |
---|
60 | * @package Zend_Http |
---|
61 | * @subpackage Client |
---|
62 | * @throws Zend_Http_Client_Exception |
---|
63 | * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
---|
64 | * @license http://framework.zend.com/license/new-bsd New BSD License |
---|
65 | */ |
---|
66 | class Zend_Http_Client |
---|
67 | { |
---|
68 | /** |
---|
69 | * HTTP request methods |
---|
70 | */ |
---|
71 | const GET = 'GET'; |
---|
72 | const POST = 'POST'; |
---|
73 | const PUT = 'PUT'; |
---|
74 | const HEAD = 'HEAD'; |
---|
75 | const DELETE = 'DELETE'; |
---|
76 | const TRACE = 'TRACE'; |
---|
77 | const OPTIONS = 'OPTIONS'; |
---|
78 | const CONNECT = 'CONNECT'; |
---|
79 | const MERGE = 'MERGE'; |
---|
80 | |
---|
81 | /** |
---|
82 | * Supported HTTP Authentication methods |
---|
83 | */ |
---|
84 | const AUTH_BASIC = 'basic'; |
---|
85 | //const AUTH_DIGEST = 'digest'; <-- not implemented yet |
---|
86 | |
---|
87 | /** |
---|
88 | * HTTP protocol versions |
---|
89 | */ |
---|
90 | const HTTP_1 = '1.1'; |
---|
91 | const HTTP_0 = '1.0'; |
---|
92 | |
---|
93 | /** |
---|
94 | * Content attributes |
---|
95 | */ |
---|
96 | const CONTENT_TYPE = 'Content-Type'; |
---|
97 | const CONTENT_LENGTH = 'Content-Length'; |
---|
98 | |
---|
99 | /** |
---|
100 | * POST data encoding methods |
---|
101 | */ |
---|
102 | const ENC_URLENCODED = 'application/x-www-form-urlencoded'; |
---|
103 | const ENC_FORMDATA = 'multipart/form-data'; |
---|
104 | |
---|
105 | /** |
---|
106 | * Value types for Body key/value pairs |
---|
107 | */ |
---|
108 | const VTYPE_SCALAR = 'SCALAR'; |
---|
109 | const VTYPE_FILE = 'FILE'; |
---|
110 | |
---|
111 | /** |
---|
112 | * Configuration array, set using the constructor or using ::setConfig() |
---|
113 | * |
---|
114 | * @var array |
---|
115 | */ |
---|
116 | protected $config = array( |
---|
117 | 'maxredirects' => 5, |
---|
118 | 'strictredirects' => false, |
---|
119 | 'useragent' => 'Zend_Http_Client', |
---|
120 | 'timeout' => 10, |
---|
121 | 'adapter' => 'Zend_Http_Client_Adapter_Socket', |
---|
122 | 'httpversion' => self::HTTP_1, |
---|
123 | 'keepalive' => false, |
---|
124 | 'storeresponse' => true, |
---|
125 | 'strict' => true, |
---|
126 | 'output_stream' => false, |
---|
127 | 'encodecookies' => true, |
---|
128 | 'rfc3986_strict' => false |
---|
129 | ); |
---|
130 | |
---|
131 | /** |
---|
132 | * The adapter used to perform the actual connection to the server |
---|
133 | * |
---|
134 | * @var Zend_Http_Client_Adapter_Interface |
---|
135 | */ |
---|
136 | protected $adapter = null; |
---|
137 | |
---|
138 | /** |
---|
139 | * Request URI |
---|
140 | * |
---|
141 | * @var Zend_Uri_Http |
---|
142 | */ |
---|
143 | protected $uri = null; |
---|
144 | |
---|
145 | /** |
---|
146 | * Associative array of request headers |
---|
147 | * |
---|
148 | * @var array |
---|
149 | */ |
---|
150 | protected $headers = array(); |
---|
151 | |
---|
152 | /** |
---|
153 | * HTTP request method |
---|
154 | * |
---|
155 | * @var string |
---|
156 | */ |
---|
157 | protected $method = self::GET; |
---|
158 | |
---|
159 | /** |
---|
160 | * Associative array of GET parameters |
---|
161 | * |
---|
162 | * @var array |
---|
163 | */ |
---|
164 | protected $paramsGet = array(); |
---|
165 | |
---|
166 | /** |
---|
167 | * Associative array of POST parameters |
---|
168 | * |
---|
169 | * @var array |
---|
170 | */ |
---|
171 | protected $paramsPost = array(); |
---|
172 | |
---|
173 | /** |
---|
174 | * Request body content type (for POST requests) |
---|
175 | * |
---|
176 | * @var string |
---|
177 | */ |
---|
178 | protected $enctype = null; |
---|
179 | |
---|
180 | /** |
---|
181 | * The raw post data to send. Could be set by setRawData($data, $enctype). |
---|
182 | * |
---|
183 | * @var string |
---|
184 | */ |
---|
185 | protected $raw_post_data = null; |
---|
186 | |
---|
187 | /** |
---|
188 | * HTTP Authentication settings |
---|
189 | * |
---|
190 | * Expected to be an associative array with this structure: |
---|
191 | * $this->auth = array('user' => 'username', 'password' => 'password', 'type' => 'basic') |
---|
192 | * Where 'type' should be one of the supported authentication types (see the AUTH_* |
---|
193 | * constants), for example 'basic' or 'digest'. |
---|
194 | * |
---|
195 | * If null, no authentication will be used. |
---|
196 | * |
---|
197 | * @var array|null |
---|
198 | */ |
---|
199 | protected $auth; |
---|
200 | |
---|
201 | /** |
---|
202 | * File upload arrays (used in POST requests) |
---|
203 | * |
---|
204 | * An associative array, where each element is of the format: |
---|
205 | * 'name' => array('filename.txt', 'text/plain', 'This is the actual file contents') |
---|
206 | * |
---|
207 | * @var array |
---|
208 | */ |
---|
209 | protected $files = array(); |
---|
210 | |
---|
211 | /** |
---|
212 | * Ordered list of keys from key/value pair data to include in body |
---|
213 | * |
---|
214 | * An associative array, where each element is of the format: |
---|
215 | * '<field name>' => VTYPE_SCALAR | VTYPE_FILE |
---|
216 | * |
---|
217 | * @var array |
---|
218 | */ |
---|
219 | protected $body_field_order = array(); |
---|
220 | |
---|
221 | /** |
---|
222 | * The client's cookie jar |
---|
223 | * |
---|
224 | * @var Zend_Http_CookieJar |
---|
225 | */ |
---|
226 | protected $cookiejar = null; |
---|
227 | |
---|
228 | /** |
---|
229 | * The last HTTP request sent by the client, as string |
---|
230 | * |
---|
231 | * @var string |
---|
232 | */ |
---|
233 | protected $last_request = null; |
---|
234 | |
---|
235 | /** |
---|
236 | * The last HTTP response received by the client |
---|
237 | * |
---|
238 | * @var Zend_Http_Response |
---|
239 | */ |
---|
240 | protected $last_response = null; |
---|
241 | |
---|
242 | /** |
---|
243 | * Redirection counter |
---|
244 | * |
---|
245 | * @var int |
---|
246 | */ |
---|
247 | protected $redirectCounter = 0; |
---|
248 | |
---|
249 | /** |
---|
250 | * Status for unmasking GET array params |
---|
251 | * |
---|
252 | * @var boolean |
---|
253 | */ |
---|
254 | protected $_unmaskStatus = false; |
---|
255 | |
---|
256 | /** |
---|
257 | * Status if the http_build_query function escapes brackets |
---|
258 | * |
---|
259 | * @var boolean |
---|
260 | */ |
---|
261 | protected $_queryBracketsEscaped = true; |
---|
262 | |
---|
263 | /** |
---|
264 | * Fileinfo magic database resource |
---|
265 | * |
---|
266 | * This variable is populated the first time _detectFileMimeType is called |
---|
267 | * and is then reused on every call to this method |
---|
268 | * |
---|
269 | * @var resource |
---|
270 | */ |
---|
271 | static protected $_fileInfoDb = null; |
---|
272 | |
---|
273 | /** |
---|
274 | * Constructor method. Will create a new HTTP client. Accepts the target |
---|
275 | * URL and optionally configuration array. |
---|
276 | * |
---|
277 | * @param Zend_Uri_Http|string $uri |
---|
278 | * @param array $config Configuration key-value pairs. |
---|
279 | */ |
---|
280 | public function __construct($uri = null, $config = null) |
---|
281 | { |
---|
282 | if ($uri !== null) { |
---|
283 | $this->setUri($uri); |
---|
284 | } |
---|
285 | if ($config !== null) { |
---|
286 | $this->setConfig($config); |
---|
287 | } |
---|
288 | |
---|
289 | $this->_queryBracketsEscaped = version_compare(phpversion(), '5.1.3', '>='); |
---|
290 | } |
---|
291 | |
---|
292 | /** |
---|
293 | * Set the URI for the next request |
---|
294 | * |
---|
295 | * @param Zend_Uri_Http|string $uri |
---|
296 | * @return Zend_Http_Client |
---|
297 | * @throws Zend_Http_Client_Exception |
---|
298 | */ |
---|
299 | public function setUri($uri) |
---|
300 | { |
---|
301 | if ($uri instanceof Zend_Uri_Http) { |
---|
302 | // clone the URI in order to keep the passed parameter constant |
---|
303 | $uri = clone $uri; |
---|
304 | } elseif (is_string($uri)) { |
---|
305 | $uri = Zend_Uri::factory($uri); |
---|
306 | } |
---|
307 | |
---|
308 | if (!$uri instanceof Zend_Uri_Http) { |
---|
309 | /** @see Zend_Http_Client_Exception */ |
---|
310 | require_once 'Zend/Http/Client/Exception.php'; |
---|
311 | throw new Zend_Http_Client_Exception('Passed parameter is not a valid HTTP URI.'); |
---|
312 | } |
---|
313 | |
---|
314 | // Set auth if username and password has been specified in the uri |
---|
315 | if ($uri->getUsername() && $uri->getPassword()) { |
---|
316 | $this->setAuth($uri->getUsername(), $uri->getPassword()); |
---|
317 | } |
---|
318 | |
---|
319 | // We have no ports, set the defaults |
---|
320 | if (! $uri->getPort()) { |
---|
321 | $uri->setPort(($uri->getScheme() == 'https' ? 443 : 80)); |
---|
322 | } |
---|
323 | |
---|
324 | $this->uri = $uri; |
---|
325 | |
---|
326 | return $this; |
---|
327 | } |
---|
328 | |
---|
329 | /** |
---|
330 | * Get the URI for the next request |
---|
331 | * |
---|
332 | * @param boolean $as_string If true, will return the URI as a string |
---|
333 | * @return Zend_Uri_Http|string |
---|
334 | */ |
---|
335 | public function getUri($as_string = false) |
---|
336 | { |
---|
337 | if ($as_string && $this->uri instanceof Zend_Uri_Http) { |
---|
338 | return $this->uri->__toString(); |
---|
339 | } else { |
---|
340 | return $this->uri; |
---|
341 | } |
---|
342 | } |
---|
343 | |
---|
344 | /** |
---|
345 | * Set configuration parameters for this HTTP client |
---|
346 | * |
---|
347 | * @param Zend_Config | array $config |
---|
348 | * @return Zend_Http_Client |
---|
349 | * @throws Zend_Http_Client_Exception |
---|
350 | */ |
---|
351 | public function setConfig($config = array()) |
---|
352 | { |
---|
353 | if ($config instanceof Zend_Config) { |
---|
354 | $config = $config->toArray(); |
---|
355 | |
---|
356 | } elseif (! is_array($config)) { |
---|
357 | /** @see Zend_Http_Client_Exception */ |
---|
358 | require_once 'Zend/Http/Client/Exception.php'; |
---|
359 | throw new Zend_Http_Client_Exception('Array or Zend_Config object expected, got ' . gettype($config)); |
---|
360 | } |
---|
361 | |
---|
362 | foreach ($config as $k => $v) { |
---|
363 | $this->config[strtolower($k)] = $v; |
---|
364 | } |
---|
365 | |
---|
366 | // Pass configuration options to the adapter if it exists |
---|
367 | if ($this->adapter instanceof Zend_Http_Client_Adapter_Interface) { |
---|
368 | $this->adapter->setConfig($config); |
---|
369 | } |
---|
370 | |
---|
371 | return $this; |
---|
372 | } |
---|
373 | |
---|
374 | /** |
---|
375 | * Set the next request's method |
---|
376 | * |
---|
377 | * Validated the passed method and sets it. If we have files set for |
---|
378 | * POST requests, and the new method is not POST, the files are silently |
---|
379 | * dropped. |
---|
380 | * |
---|
381 | * @param string $method |
---|
382 | * @return Zend_Http_Client |
---|
383 | * @throws Zend_Http_Client_Exception |
---|
384 | */ |
---|
385 | public function setMethod($method = self::GET) |
---|
386 | { |
---|
387 | if (! preg_match('/^[^\x00-\x1f\x7f-\xff\(\)<>@,;:\\\\"\/\[\]\?={}\s]+$/', $method)) { |
---|
388 | /** @see Zend_Http_Client_Exception */ |
---|
389 | require_once 'Zend/Http/Client/Exception.php'; |
---|
390 | throw new Zend_Http_Client_Exception("'{$method}' is not a valid HTTP request method."); |
---|
391 | } |
---|
392 | |
---|
393 | if (($method == self::POST || $method == self::PUT || $method == self::DELETE) && $this->enctype === null) { |
---|
394 | $this->setEncType(self::ENC_URLENCODED); |
---|
395 | } |
---|
396 | |
---|
397 | $this->method = $method; |
---|
398 | |
---|
399 | return $this; |
---|
400 | } |
---|
401 | |
---|
402 | /** |
---|
403 | * Set one or more request headers |
---|
404 | * |
---|
405 | * This function can be used in several ways to set the client's request |
---|
406 | * headers: |
---|
407 | * 1. By providing two parameters: $name as the header to set (e.g. 'Host') |
---|
408 | * and $value as it's value (e.g. 'www.example.com'). |
---|
409 | * 2. By providing a single header string as the only parameter |
---|
410 | * e.g. 'Host: www.example.com' |
---|
411 | * 3. By providing an array of headers as the first parameter |
---|
412 | * e.g. array('host' => 'www.example.com', 'x-foo: bar'). In This case |
---|
413 | * the function will call itself recursively for each array item. |
---|
414 | * |
---|
415 | * @param string|array $name Header name, full header string ('Header: value') |
---|
416 | * or an array of headers |
---|
417 | * @param mixed $value Header value or null |
---|
418 | * @return Zend_Http_Client |
---|
419 | * @throws Zend_Http_Client_Exception |
---|
420 | */ |
---|
421 | public function setHeaders($name, $value = null) |
---|
422 | { |
---|
423 | // If we got an array, go recursive! |
---|
424 | if (is_array($name)) { |
---|
425 | foreach ($name as $k => $v) { |
---|
426 | if (is_string($k)) { |
---|
427 | $this->setHeaders($k, $v); |
---|
428 | } else { |
---|
429 | $this->setHeaders($v, null); |
---|
430 | } |
---|
431 | } |
---|
432 | } else { |
---|
433 | // Check if $name needs to be split |
---|
434 | if ($value === null && (strpos($name, ':') > 0)) { |
---|
435 | list($name, $value) = explode(':', $name, 2); |
---|
436 | } |
---|
437 | |
---|
438 | // Make sure the name is valid if we are in strict mode |
---|
439 | if ($this->config['strict'] && (! preg_match('/^[a-zA-Z0-9-]+$/', $name))) { |
---|
440 | /** @see Zend_Http_Client_Exception */ |
---|
441 | require_once 'Zend/Http/Client/Exception.php'; |
---|
442 | throw new Zend_Http_Client_Exception("{$name} is not a valid HTTP header name"); |
---|
443 | } |
---|
444 | |
---|
445 | $normalized_name = strtolower($name); |
---|
446 | |
---|
447 | // If $value is null or false, unset the header |
---|
448 | if ($value === null || $value === false) { |
---|
449 | unset($this->headers[$normalized_name]); |
---|
450 | |
---|
451 | // Else, set the header |
---|
452 | } else { |
---|
453 | // Header names are stored lowercase internally. |
---|
454 | if (is_string($value)) { |
---|
455 | $value = trim($value); |
---|
456 | } |
---|
457 | $this->headers[$normalized_name] = array($name, $value); |
---|
458 | } |
---|
459 | } |
---|
460 | |
---|
461 | return $this; |
---|
462 | } |
---|
463 | |
---|
464 | /** |
---|
465 | * Get the value of a specific header |
---|
466 | * |
---|
467 | * Note that if the header has more than one value, an array |
---|
468 | * will be returned. |
---|
469 | * |
---|
470 | * @param string $key |
---|
471 | * @return string|array|null The header value or null if it is not set |
---|
472 | */ |
---|
473 | public function getHeader($key) |
---|
474 | { |
---|
475 | $key = strtolower($key); |
---|
476 | if (isset($this->headers[$key])) { |
---|
477 | return $this->headers[$key][1]; |
---|
478 | } else { |
---|
479 | return null; |
---|
480 | } |
---|
481 | } |
---|
482 | |
---|
483 | /** |
---|
484 | * Set a GET parameter for the request. Wrapper around _setParameter |
---|
485 | * |
---|
486 | * @param string|array $name |
---|
487 | * @param string $value |
---|
488 | * @return Zend_Http_Client |
---|
489 | */ |
---|
490 | public function setParameterGet($name, $value = null) |
---|
491 | { |
---|
492 | if (is_array($name)) { |
---|
493 | foreach ($name as $k => $v) |
---|
494 | $this->_setParameter('GET', $k, $v); |
---|
495 | } else { |
---|
496 | $this->_setParameter('GET', $name, $value); |
---|
497 | } |
---|
498 | |
---|
499 | return $this; |
---|
500 | } |
---|
501 | |
---|
502 | /** |
---|
503 | * Set a POST parameter for the request. Wrapper around _setParameter |
---|
504 | * |
---|
505 | * @param string|array $name |
---|
506 | * @param string $value |
---|
507 | * @return Zend_Http_Client |
---|
508 | */ |
---|
509 | public function setParameterPost($name, $value = null) |
---|
510 | { |
---|
511 | if (is_array($name)) { |
---|
512 | foreach ($name as $k => $v) |
---|
513 | $this->_setParameter('POST', $k, $v); |
---|
514 | } else { |
---|
515 | $this->_setParameter('POST', $name, $value); |
---|
516 | } |
---|
517 | |
---|
518 | return $this; |
---|
519 | } |
---|
520 | |
---|
521 | /** |
---|
522 | * Set a GET or POST parameter - used by SetParameterGet and SetParameterPost |
---|
523 | * |
---|
524 | * @param string $type GET or POST |
---|
525 | * @param string $name |
---|
526 | * @param string $value |
---|
527 | * @return null |
---|
528 | */ |
---|
529 | protected function _setParameter($type, $name, $value) |
---|
530 | { |
---|
531 | $parray = array(); |
---|
532 | $type = strtolower($type); |
---|
533 | switch ($type) { |
---|
534 | case 'get': |
---|
535 | $parray = &$this->paramsGet; |
---|
536 | break; |
---|
537 | case 'post': |
---|
538 | $parray = &$this->paramsPost; |
---|
539 | if ( $value === null ) { |
---|
540 | if (isset($this->body_field_order[$name])) |
---|
541 | unset($this->body_field_order[$name]); |
---|
542 | } else { |
---|
543 | $this->body_field_order[$name] = self::VTYPE_SCALAR; |
---|
544 | } |
---|
545 | break; |
---|
546 | } |
---|
547 | |
---|
548 | if ($value === null) { |
---|
549 | if (isset($parray[$name])) unset($parray[$name]); |
---|
550 | } else { |
---|
551 | $parray[$name] = $value; |
---|
552 | } |
---|
553 | } |
---|
554 | |
---|
555 | /** |
---|
556 | * Get the number of redirections done on the last request |
---|
557 | * |
---|
558 | * @return int |
---|
559 | */ |
---|
560 | public function getRedirectionsCount() |
---|
561 | { |
---|
562 | return $this->redirectCounter; |
---|
563 | } |
---|
564 | |
---|
565 | /** |
---|
566 | * Set HTTP authentication parameters |
---|
567 | * |
---|
568 | * $type should be one of the supported types - see the self::AUTH_* |
---|
569 | * constants. |
---|
570 | * |
---|
571 | * To enable authentication: |
---|
572 | * <code> |
---|
573 | * $this->setAuth('shahar', 'secret', Zend_Http_Client::AUTH_BASIC); |
---|
574 | * </code> |
---|
575 | * |
---|
576 | * To disable authentication: |
---|
577 | * <code> |
---|
578 | * $this->setAuth(false); |
---|
579 | * </code> |
---|
580 | * |
---|
581 | * @see http://www.faqs.org/rfcs/rfc2617.html |
---|
582 | * @param string|false $user User name or false disable authentication |
---|
583 | * @param string $password Password |
---|
584 | * @param string $type Authentication type |
---|
585 | * @return Zend_Http_Client |
---|
586 | * @throws Zend_Http_Client_Exception |
---|
587 | */ |
---|
588 | public function setAuth($user, $password = '', $type = self::AUTH_BASIC) |
---|
589 | { |
---|
590 | // If we got false or null, disable authentication |
---|
591 | if ($user === false || $user === null) { |
---|
592 | $this->auth = null; |
---|
593 | |
---|
594 | // Clear the auth information in the uri instance as well |
---|
595 | if ($this->uri instanceof Zend_Uri_Http) { |
---|
596 | $this->getUri()->setUsername(''); |
---|
597 | $this->getUri()->setPassword(''); |
---|
598 | } |
---|
599 | // Else, set up authentication |
---|
600 | } else { |
---|
601 | // Check we got a proper authentication type |
---|
602 | if (! defined('self::AUTH_' . strtoupper($type))) { |
---|
603 | /** @see Zend_Http_Client_Exception */ |
---|
604 | require_once 'Zend/Http/Client/Exception.php'; |
---|
605 | throw new Zend_Http_Client_Exception("Invalid or not supported authentication type: '$type'"); |
---|
606 | } |
---|
607 | |
---|
608 | $this->auth = array( |
---|
609 | 'user' => (string) $user, |
---|
610 | 'password' => (string) $password, |
---|
611 | 'type' => $type |
---|
612 | ); |
---|
613 | } |
---|
614 | |
---|
615 | return $this; |
---|
616 | } |
---|
617 | |
---|
618 | /** |
---|
619 | * Set the HTTP client's cookie jar. |
---|
620 | * |
---|
621 | * A cookie jar is an object that holds and maintains cookies across HTTP requests |
---|
622 | * and responses. |
---|
623 | * |
---|
624 | * @param Zend_Http_CookieJar|boolean $cookiejar Existing cookiejar object, true to create a new one, false to disable |
---|
625 | * @return Zend_Http_Client |
---|
626 | * @throws Zend_Http_Client_Exception |
---|
627 | */ |
---|
628 | public function setCookieJar($cookiejar = true) |
---|
629 | { |
---|
630 | Zend_Loader::loadClass('Zend_Http_CookieJar'); |
---|
631 | |
---|
632 | if ($cookiejar instanceof Zend_Http_CookieJar) { |
---|
633 | $this->cookiejar = $cookiejar; |
---|
634 | } elseif ($cookiejar === true) { |
---|
635 | $this->cookiejar = new Zend_Http_CookieJar(); |
---|
636 | } elseif (! $cookiejar) { |
---|
637 | $this->cookiejar = null; |
---|
638 | } else { |
---|
639 | /** @see Zend_Http_Client_Exception */ |
---|
640 | require_once 'Zend/Http/Client/Exception.php'; |
---|
641 | throw new Zend_Http_Client_Exception('Invalid parameter type passed as CookieJar'); |
---|
642 | } |
---|
643 | |
---|
644 | return $this; |
---|
645 | } |
---|
646 | |
---|
647 | /** |
---|
648 | * Return the current cookie jar or null if none. |
---|
649 | * |
---|
650 | * @return Zend_Http_CookieJar|null |
---|
651 | */ |
---|
652 | public function getCookieJar() |
---|
653 | { |
---|
654 | return $this->cookiejar; |
---|
655 | } |
---|
656 | |
---|
657 | /** |
---|
658 | * Add a cookie to the request. If the client has no Cookie Jar, the cookies |
---|
659 | * will be added directly to the headers array as "Cookie" headers. |
---|
660 | * |
---|
661 | * @param Zend_Http_Cookie|string $cookie |
---|
662 | * @param string|null $value If "cookie" is a string, this is the cookie value. |
---|
663 | * @return Zend_Http_Client |
---|
664 | * @throws Zend_Http_Client_Exception |
---|
665 | */ |
---|
666 | public function setCookie($cookie, $value = null) |
---|
667 | { |
---|
668 | Zend_Loader::loadClass('Zend_Http_Cookie'); |
---|
669 | |
---|
670 | if (is_array($cookie)) { |
---|
671 | foreach ($cookie as $c => $v) { |
---|
672 | if (is_string($c)) { |
---|
673 | $this->setCookie($c, $v); |
---|
674 | } else { |
---|
675 | $this->setCookie($v); |
---|
676 | } |
---|
677 | } |
---|
678 | |
---|
679 | return $this; |
---|
680 | } |
---|
681 | |
---|
682 | if ($value !== null && $this->config['encodecookies']) { |
---|
683 | $value = urlencode($value); |
---|
684 | } |
---|
685 | |
---|
686 | if (isset($this->cookiejar)) { |
---|
687 | if ($cookie instanceof Zend_Http_Cookie) { |
---|
688 | $this->cookiejar->addCookie($cookie); |
---|
689 | } elseif (is_string($cookie) && $value !== null) { |
---|
690 | $cookie = Zend_Http_Cookie::fromString("{$cookie}={$value}", |
---|
691 | $this->uri, |
---|
692 | $this->config['encodecookies']); |
---|
693 | $this->cookiejar->addCookie($cookie); |
---|
694 | } |
---|
695 | } else { |
---|
696 | if ($cookie instanceof Zend_Http_Cookie) { |
---|
697 | $name = $cookie->getName(); |
---|
698 | $value = $cookie->getValue(); |
---|
699 | $cookie = $name; |
---|
700 | } |
---|
701 | |
---|
702 | if (preg_match("/[=,; \t\r\n\013\014]/", $cookie)) { |
---|
703 | /** @see Zend_Http_Client_Exception */ |
---|
704 | require_once 'Zend/Http/Client/Exception.php'; |
---|
705 | throw new Zend_Http_Client_Exception("Cookie name cannot contain these characters: =,; \t\r\n\013\014 ({$cookie})"); |
---|
706 | } |
---|
707 | |
---|
708 | $value = addslashes($value); |
---|
709 | |
---|
710 | if (! isset($this->headers['cookie'])) { |
---|
711 | $this->headers['cookie'] = array('Cookie', ''); |
---|
712 | } |
---|
713 | $this->headers['cookie'][1] .= $cookie . '=' . $value . '; '; |
---|
714 | } |
---|
715 | |
---|
716 | return $this; |
---|
717 | } |
---|
718 | |
---|
719 | /** |
---|
720 | * Set a file to upload (using a POST request) |
---|
721 | * |
---|
722 | * Can be used in two ways: |
---|
723 | * |
---|
724 | * 1. $data is null (default): $filename is treated as the name if a local file which |
---|
725 | * will be read and sent. Will try to guess the content type using mime_content_type(). |
---|
726 | * 2. $data is set - $filename is sent as the file name, but $data is sent as the file |
---|
727 | * contents and no file is read from the file system. In this case, you need to |
---|
728 | * manually set the Content-Type ($ctype) or it will default to |
---|
729 | * application/octet-stream. |
---|
730 | * |
---|
731 | * @param string $filename Name of file to upload, or name to save as |
---|
732 | * @param string $formname Name of form element to send as |
---|
733 | * @param string $data Data to send (if null, $filename is read and sent) |
---|
734 | * @param string $ctype Content type to use (if $data is set and $ctype is |
---|
735 | * null, will be application/octet-stream) |
---|
736 | * @return Zend_Http_Client |
---|
737 | * @throws Zend_Http_Client_Exception |
---|
738 | */ |
---|
739 | public function setFileUpload($filename, $formname, $data = null, $ctype = null) |
---|
740 | { |
---|
741 | if ($data === null) { |
---|
742 | if (($data = @file_get_contents($filename)) === false) { |
---|
743 | /** @see Zend_Http_Client_Exception */ |
---|
744 | require_once 'Zend/Http/Client/Exception.php'; |
---|
745 | throw new Zend_Http_Client_Exception("Unable to read file '{$filename}' for upload"); |
---|
746 | } |
---|
747 | |
---|
748 | if (! $ctype) { |
---|
749 | $ctype = $this->_detectFileMimeType($filename); |
---|
750 | } |
---|
751 | } |
---|
752 | |
---|
753 | // Force enctype to multipart/form-data |
---|
754 | $this->setEncType(self::ENC_FORMDATA); |
---|
755 | |
---|
756 | $this->files[] = array( |
---|
757 | 'formname' => $formname, |
---|
758 | 'filename' => basename($filename), |
---|
759 | 'ctype' => $ctype, |
---|
760 | 'data' => $data |
---|
761 | ); |
---|
762 | |
---|
763 | $this->body_field_order[$formname] = self::VTYPE_FILE; |
---|
764 | |
---|
765 | return $this; |
---|
766 | } |
---|
767 | |
---|
768 | /** |
---|
769 | * Set the encoding type for POST data |
---|
770 | * |
---|
771 | * @param string $enctype |
---|
772 | * @return Zend_Http_Client |
---|
773 | */ |
---|
774 | public function setEncType($enctype = self::ENC_URLENCODED) |
---|
775 | { |
---|
776 | $this->enctype = $enctype; |
---|
777 | |
---|
778 | return $this; |
---|
779 | } |
---|
780 | |
---|
781 | /** |
---|
782 | * Set the raw (already encoded) POST data. |
---|
783 | * |
---|
784 | * This function is here for two reasons: |
---|
785 | * 1. For advanced user who would like to set their own data, already encoded |
---|
786 | * 2. For backwards compatibilty: If someone uses the old post($data) method. |
---|
787 | * this method will be used to set the encoded data. |
---|
788 | * |
---|
789 | * $data can also be stream (such as file) from which the data will be read. |
---|
790 | * |
---|
791 | * @param string|resource $data |
---|
792 | * @param string $enctype |
---|
793 | * @return Zend_Http_Client |
---|
794 | */ |
---|
795 | public function setRawData($data, $enctype = null) |
---|
796 | { |
---|
797 | $this->raw_post_data = $data; |
---|
798 | $this->setEncType($enctype); |
---|
799 | if (is_resource($data)) { |
---|
800 | // We've got stream data |
---|
801 | $stat = @fstat($data); |
---|
802 | if($stat) { |
---|
803 | $this->setHeaders(self::CONTENT_LENGTH, $stat['size']); |
---|
804 | } |
---|
805 | } |
---|
806 | return $this; |
---|
807 | } |
---|
808 | |
---|
809 | /** |
---|
810 | * Set the unmask feature for GET parameters as array |
---|
811 | * |
---|
812 | * Example: |
---|
813 | * foo%5B0%5D=a&foo%5B1%5D=b |
---|
814 | * becomes |
---|
815 | * foo=a&foo=b |
---|
816 | * |
---|
817 | * This is usefull for some services |
---|
818 | * |
---|
819 | * @param boolean $status |
---|
820 | * @return Zend_Http_Client |
---|
821 | */ |
---|
822 | public function setUnmaskStatus($status = true) |
---|
823 | { |
---|
824 | $this->_unmaskStatus = (BOOL)$status; |
---|
825 | return $this; |
---|
826 | } |
---|
827 | |
---|
828 | /** |
---|
829 | * Returns the currently configured unmask status |
---|
830 | * |
---|
831 | * @return boolean |
---|
832 | */ |
---|
833 | public function getUnmaskStatus() |
---|
834 | { |
---|
835 | return $this->_unmaskStatus; |
---|
836 | } |
---|
837 | |
---|
838 | /** |
---|
839 | * Clear all GET and POST parameters |
---|
840 | * |
---|
841 | * Should be used to reset the request parameters if the client is |
---|
842 | * used for several concurrent requests. |
---|
843 | * |
---|
844 | * clearAll parameter controls if we clean just parameters or also |
---|
845 | * headers and last_* |
---|
846 | * |
---|
847 | * @param bool $clearAll Should all data be cleared? |
---|
848 | * @return Zend_Http_Client |
---|
849 | */ |
---|
850 | public function resetParameters($clearAll = false) |
---|
851 | { |
---|
852 | // Reset parameter data |
---|
853 | $this->paramsGet = array(); |
---|
854 | $this->paramsPost = array(); |
---|
855 | $this->files = array(); |
---|
856 | $this->raw_post_data = null; |
---|
857 | $this->enctype = null; |
---|
858 | |
---|
859 | if($clearAll) { |
---|
860 | $this->headers = array(); |
---|
861 | $this->last_request = null; |
---|
862 | $this->last_response = null; |
---|
863 | } else { |
---|
864 | // Clear outdated headers |
---|
865 | if (isset($this->headers[strtolower(self::CONTENT_TYPE)])) { |
---|
866 | unset($this->headers[strtolower(self::CONTENT_TYPE)]); |
---|
867 | } |
---|
868 | if (isset($this->headers[strtolower(self::CONTENT_LENGTH)])) { |
---|
869 | unset($this->headers[strtolower(self::CONTENT_LENGTH)]); |
---|
870 | } |
---|
871 | } |
---|
872 | |
---|
873 | return $this; |
---|
874 | } |
---|
875 | |
---|
876 | /** |
---|
877 | * Get the last HTTP request as string |
---|
878 | * |
---|
879 | * @return string |
---|
880 | */ |
---|
881 | public function getLastRequest() |
---|
882 | { |
---|
883 | return $this->last_request; |
---|
884 | } |
---|
885 | |
---|
886 | /** |
---|
887 | * Get the last HTTP response received by this client |
---|
888 | * |
---|
889 | * If $config['storeresponse'] is set to false, or no response was |
---|
890 | * stored yet, will return null |
---|
891 | * |
---|
892 | * @return Zend_Http_Response or null if none |
---|
893 | */ |
---|
894 | public function getLastResponse() |
---|
895 | { |
---|
896 | return $this->last_response; |
---|
897 | } |
---|
898 | |
---|
899 | /** |
---|
900 | * Load the connection adapter |
---|
901 | * |
---|
902 | * While this method is not called more than one for a client, it is |
---|
903 | * seperated from ->request() to preserve logic and readability |
---|
904 | * |
---|
905 | * @param Zend_Http_Client_Adapter_Interface|string $adapter |
---|
906 | * @return null |
---|
907 | * @throws Zend_Http_Client_Exception |
---|
908 | */ |
---|
909 | public function setAdapter($adapter) |
---|
910 | { |
---|
911 | if (is_string($adapter)) { |
---|
912 | try { |
---|
913 | Zend_Loader::loadClass($adapter); |
---|
914 | } catch (Zend_Exception $e) { |
---|
915 | /** @see Zend_Http_Client_Exception */ |
---|
916 | require_once 'Zend/Http/Client/Exception.php'; |
---|
917 | throw new Zend_Http_Client_Exception("Unable to load adapter '$adapter': {$e->getMessage()}", 0, $e); |
---|
918 | } |
---|
919 | |
---|
920 | $adapter = new $adapter; |
---|
921 | } |
---|
922 | |
---|
923 | if (! $adapter instanceof Zend_Http_Client_Adapter_Interface) { |
---|
924 | /** @see Zend_Http_Client_Exception */ |
---|
925 | require_once 'Zend/Http/Client/Exception.php'; |
---|
926 | throw new Zend_Http_Client_Exception('Passed adapter is not a HTTP connection adapter'); |
---|
927 | } |
---|
928 | |
---|
929 | $this->adapter = $adapter; |
---|
930 | $config = $this->config; |
---|
931 | unset($config['adapter']); |
---|
932 | $this->adapter->setConfig($config); |
---|
933 | } |
---|
934 | |
---|
935 | /** |
---|
936 | * Load the connection adapter |
---|
937 | * |
---|
938 | * @return Zend_Http_Client_Adapter_Interface $adapter |
---|
939 | */ |
---|
940 | public function getAdapter() |
---|
941 | { |
---|
942 | if (null === $this->adapter) { |
---|
943 | $this->setAdapter($this->config['adapter']); |
---|
944 | } |
---|
945 | |
---|
946 | return $this->adapter; |
---|
947 | } |
---|
948 | |
---|
949 | /** |
---|
950 | * Set streaming for received data |
---|
951 | * |
---|
952 | * @param string|boolean $streamfile Stream file, true for temp file, false/null for no streaming |
---|
953 | * @return Zend_Http_Client |
---|
954 | */ |
---|
955 | public function setStream($streamfile = true) |
---|
956 | { |
---|
957 | $this->setConfig(array("output_stream" => $streamfile)); |
---|
958 | return $this; |
---|
959 | } |
---|
960 | |
---|
961 | /** |
---|
962 | * Get status of streaming for received data |
---|
963 | * @return boolean|string |
---|
964 | */ |
---|
965 | public function getStream() |
---|
966 | { |
---|
967 | return $this->config["output_stream"]; |
---|
968 | } |
---|
969 | |
---|
970 | /** |
---|
971 | * Create temporary stream |
---|
972 | * |
---|
973 | * @return resource |
---|
974 | */ |
---|
975 | protected function _openTempStream() |
---|
976 | { |
---|
977 | $this->_stream_name = $this->config['output_stream']; |
---|
978 | if(!is_string($this->_stream_name)) { |
---|
979 | // If name is not given, create temp name |
---|
980 | $this->_stream_name = tempnam(isset($this->config['stream_tmp_dir'])?$this->config['stream_tmp_dir']:sys_get_temp_dir(), |
---|
981 | 'Zend_Http_Client'); |
---|
982 | } |
---|
983 | |
---|
984 | if (false === ($fp = @fopen($this->_stream_name, "w+b"))) { |
---|
985 | if ($this->adapter instanceof Zend_Http_Client_Adapter_Interface) { |
---|
986 | $this->adapter->close(); |
---|
987 | } |
---|
988 | require_once 'Zend/Http/Client/Exception.php'; |
---|
989 | throw new Zend_Http_Client_Exception("Could not open temp file {$this->_stream_name}"); |
---|
990 | } |
---|
991 | |
---|
992 | return $fp; |
---|
993 | } |
---|
994 | |
---|
995 | /** |
---|
996 | * Send the HTTP request and return an HTTP response object |
---|
997 | * |
---|
998 | * @param string $method |
---|
999 | * @return Zend_Http_Response |
---|
1000 | * @throws Zend_Http_Client_Exception |
---|
1001 | */ |
---|
1002 | public function request($method = null) |
---|
1003 | { |
---|
1004 | if (! $this->uri instanceof Zend_Uri_Http) { |
---|
1005 | /** @see Zend_Http_Client_Exception */ |
---|
1006 | require_once 'Zend/Http/Client/Exception.php'; |
---|
1007 | throw new Zend_Http_Client_Exception('No valid URI has been passed to the client'); |
---|
1008 | } |
---|
1009 | |
---|
1010 | if ($method) { |
---|
1011 | $this->setMethod($method); |
---|
1012 | } |
---|
1013 | $this->redirectCounter = 0; |
---|
1014 | $response = null; |
---|
1015 | |
---|
1016 | // Make sure the adapter is loaded |
---|
1017 | if ($this->adapter == null) { |
---|
1018 | $this->setAdapter($this->config['adapter']); |
---|
1019 | } |
---|
1020 | |
---|
1021 | // Send the first request. If redirected, continue. |
---|
1022 | do { |
---|
1023 | // Clone the URI and add the additional GET parameters to it |
---|
1024 | $uri = clone $this->uri; |
---|
1025 | if (! empty($this->paramsGet)) { |
---|
1026 | $query = $uri->getQuery(); |
---|
1027 | if (! empty($query)) { |
---|
1028 | $query .= '&'; |
---|
1029 | } |
---|
1030 | $query .= http_build_query($this->paramsGet, null, '&'); |
---|
1031 | if ($this->config['rfc3986_strict']) { |
---|
1032 | $query = str_replace('+', '%20', $query); |
---|
1033 | } |
---|
1034 | |
---|
1035 | // @see ZF-11671 to unmask for some services to foo=val1&foo=val2 |
---|
1036 | if ($this->getUnmaskStatus()) { |
---|
1037 | if ($this->_queryBracketsEscaped) { |
---|
1038 | $query = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $query); |
---|
1039 | } else { |
---|
1040 | $query = preg_replace('/\\[(?:[0-9]|[1-9][0-9]+)\\]=/', '=', $query); |
---|
1041 | } |
---|
1042 | } |
---|
1043 | |
---|
1044 | $uri->setQuery($query); |
---|
1045 | } |
---|
1046 | |
---|
1047 | $body = $this->_prepareBody(); |
---|
1048 | $headers = $this->_prepareHeaders(); |
---|
1049 | |
---|
1050 | // check that adapter supports streaming before using it |
---|
1051 | if(is_resource($body) && !($this->adapter instanceof Zend_Http_Client_Adapter_Stream)) { |
---|
1052 | /** @see Zend_Http_Client_Exception */ |
---|
1053 | require_once 'Zend/Http/Client/Exception.php'; |
---|
1054 | throw new Zend_Http_Client_Exception('Adapter does not support streaming'); |
---|
1055 | } |
---|
1056 | |
---|
1057 | // Open the connection, send the request and read the response |
---|
1058 | $this->adapter->connect($uri->getHost(), $uri->getPort(), |
---|
1059 | ($uri->getScheme() == 'https' ? true : false)); |
---|
1060 | |
---|
1061 | if($this->config['output_stream']) { |
---|
1062 | if($this->adapter instanceof Zend_Http_Client_Adapter_Stream) { |
---|
1063 | $stream = $this->_openTempStream(); |
---|
1064 | $this->adapter->setOutputStream($stream); |
---|
1065 | } else { |
---|
1066 | /** @see Zend_Http_Client_Exception */ |
---|
1067 | require_once 'Zend/Http/Client/Exception.php'; |
---|
1068 | throw new Zend_Http_Client_Exception('Adapter does not support streaming'); |
---|
1069 | } |
---|
1070 | } |
---|
1071 | |
---|
1072 | $this->last_request = $this->adapter->write($this->method, |
---|
1073 | $uri, $this->config['httpversion'], $headers, $body); |
---|
1074 | |
---|
1075 | $response = $this->adapter->read(); |
---|
1076 | if (! $response) { |
---|
1077 | /** @see Zend_Http_Client_Exception */ |
---|
1078 | require_once 'Zend/Http/Client/Exception.php'; |
---|
1079 | throw new Zend_Http_Client_Exception('Unable to read response, or response is empty'); |
---|
1080 | } |
---|
1081 | |
---|
1082 | if($this->config['output_stream']) { |
---|
1083 | $streamMetaData = stream_get_meta_data($stream); |
---|
1084 | if ($streamMetaData['seekable']) { |
---|
1085 | rewind($stream); |
---|
1086 | } |
---|
1087 | // cleanup the adapter |
---|
1088 | $this->adapter->setOutputStream(null); |
---|
1089 | $response = Zend_Http_Response_Stream::fromStream($response, $stream); |
---|
1090 | $response->setStreamName($this->_stream_name); |
---|
1091 | if(!is_string($this->config['output_stream'])) { |
---|
1092 | // we used temp name, will need to clean up |
---|
1093 | $response->setCleanup(true); |
---|
1094 | } |
---|
1095 | } else { |
---|
1096 | $response = Zend_Http_Response::fromString($response); |
---|
1097 | } |
---|
1098 | |
---|
1099 | if ($this->config['storeresponse']) { |
---|
1100 | $this->last_response = $response; |
---|
1101 | } |
---|
1102 | |
---|
1103 | // Load cookies into cookie jar |
---|
1104 | if (isset($this->cookiejar)) { |
---|
1105 | $this->cookiejar->addCookiesFromResponse($response, $uri, $this->config['encodecookies']); |
---|
1106 | } |
---|
1107 | |
---|
1108 | // If we got redirected, look for the Location header |
---|
1109 | if ($response->isRedirect() && ($location = $response->getHeader('location'))) { |
---|
1110 | |
---|
1111 | // Avoid problems with buggy servers that add whitespace at the |
---|
1112 | // end of some headers (See ZF-11283) |
---|
1113 | $location = trim($location); |
---|
1114 | |
---|
1115 | // Check whether we send the exact same request again, or drop the parameters |
---|
1116 | // and send a GET request |
---|
1117 | if ($response->getStatus() == 303 || |
---|
1118 | ((! $this->config['strictredirects']) && ($response->getStatus() == 302 || |
---|
1119 | $response->getStatus() == 301))) { |
---|
1120 | |
---|
1121 | $this->resetParameters(); |
---|
1122 | $this->setMethod(self::GET); |
---|
1123 | } |
---|
1124 | |
---|
1125 | // If we got a well formed absolute URI |
---|
1126 | if (($scheme = substr($location, 0, 6)) && ($scheme == 'http:/' || $scheme == 'https:')) { |
---|
1127 | $this->setHeaders('host', null); |
---|
1128 | $this->setUri($location); |
---|
1129 | |
---|
1130 | } else { |
---|
1131 | |
---|
1132 | // Split into path and query and set the query |
---|
1133 | if (strpos($location, '?') !== false) { |
---|
1134 | list($location, $query) = explode('?', $location, 2); |
---|
1135 | } else { |
---|
1136 | $query = ''; |
---|
1137 | } |
---|
1138 | $this->uri->setQuery($query); |
---|
1139 | |
---|
1140 | // Else, if we got just an absolute path, set it |
---|
1141 | if(strpos($location, '/') === 0) { |
---|
1142 | $this->uri->setPath($location); |
---|
1143 | |
---|
1144 | // Else, assume we have a relative path |
---|
1145 | } else { |
---|
1146 | // Get the current path directory, removing any trailing slashes |
---|
1147 | $path = $this->uri->getPath(); |
---|
1148 | $path = rtrim(substr($path, 0, strrpos($path, '/')), "/"); |
---|
1149 | $this->uri->setPath($path . '/' . $location); |
---|
1150 | } |
---|
1151 | } |
---|
1152 | ++$this->redirectCounter; |
---|
1153 | |
---|
1154 | } else { |
---|
1155 | // If we didn't get any location, stop redirecting |
---|
1156 | break; |
---|
1157 | } |
---|
1158 | |
---|
1159 | } while ($this->redirectCounter < $this->config['maxredirects']); |
---|
1160 | |
---|
1161 | return $response; |
---|
1162 | } |
---|
1163 | |
---|
1164 | /** |
---|
1165 | * Prepare the request headers |
---|
1166 | * |
---|
1167 | * @return array |
---|
1168 | */ |
---|
1169 | protected function _prepareHeaders() |
---|
1170 | { |
---|
1171 | $headers = array(); |
---|
1172 | |
---|
1173 | // Set the host header |
---|
1174 | if (! isset($this->headers['host'])) { |
---|
1175 | $host = $this->uri->getHost(); |
---|
1176 | |
---|
1177 | // If the port is not default, add it |
---|
1178 | if (! (($this->uri->getScheme() == 'http' && $this->uri->getPort() == 80) || |
---|
1179 | ($this->uri->getScheme() == 'https' && $this->uri->getPort() == 443))) { |
---|
1180 | $host .= ':' . $this->uri->getPort(); |
---|
1181 | } |
---|
1182 | |
---|
1183 | $headers[] = "Host: {$host}"; |
---|
1184 | } |
---|
1185 | |
---|
1186 | // Set the connection header |
---|
1187 | if (! isset($this->headers['connection'])) { |
---|
1188 | if (! $this->config['keepalive']) { |
---|
1189 | $headers[] = "Connection: close"; |
---|
1190 | } |
---|
1191 | } |
---|
1192 | |
---|
1193 | // Set the Accept-encoding header if not set - depending on whether |
---|
1194 | // zlib is available or not. |
---|
1195 | if (! isset($this->headers['accept-encoding'])) { |
---|
1196 | if (function_exists('gzinflate')) { |
---|
1197 | $headers[] = 'Accept-encoding: gzip, deflate'; |
---|
1198 | } else { |
---|
1199 | $headers[] = 'Accept-encoding: identity'; |
---|
1200 | } |
---|
1201 | } |
---|
1202 | |
---|
1203 | // Set the Content-Type header |
---|
1204 | if (($this->method == self::POST || $this->method == self::PUT) && |
---|
1205 | (! isset($this->headers[strtolower(self::CONTENT_TYPE)]) && isset($this->enctype))) { |
---|
1206 | |
---|
1207 | $headers[] = self::CONTENT_TYPE . ': ' . $this->enctype; |
---|
1208 | } |
---|
1209 | |
---|
1210 | // Set the user agent header |
---|
1211 | if (! isset($this->headers['user-agent']) && isset($this->config['useragent'])) { |
---|
1212 | $headers[] = "User-Agent: {$this->config['useragent']}"; |
---|
1213 | } |
---|
1214 | |
---|
1215 | // Set HTTP authentication if needed |
---|
1216 | if (is_array($this->auth)) { |
---|
1217 | $auth = self::encodeAuthHeader($this->auth['user'], $this->auth['password'], $this->auth['type']); |
---|
1218 | $headers[] = "Authorization: {$auth}"; |
---|
1219 | } |
---|
1220 | |
---|
1221 | // Load cookies from cookie jar |
---|
1222 | if (isset($this->cookiejar)) { |
---|
1223 | $cookstr = $this->cookiejar->getMatchingCookies($this->uri, |
---|
1224 | true, Zend_Http_CookieJar::COOKIE_STRING_CONCAT); |
---|
1225 | |
---|
1226 | if ($cookstr) { |
---|
1227 | $headers[] = "Cookie: {$cookstr}"; |
---|
1228 | } |
---|
1229 | } |
---|
1230 | |
---|
1231 | // Add all other user defined headers |
---|
1232 | foreach ($this->headers as $header) { |
---|
1233 | list($name, $value) = $header; |
---|
1234 | if (is_array($value)) { |
---|
1235 | $value = implode(', ', $value); |
---|
1236 | } |
---|
1237 | |
---|
1238 | $headers[] = "$name: $value"; |
---|
1239 | } |
---|
1240 | |
---|
1241 | return $headers; |
---|
1242 | } |
---|
1243 | |
---|
1244 | /** |
---|
1245 | * Prepare the request body (for POST and PUT requests) |
---|
1246 | * |
---|
1247 | * @return string |
---|
1248 | * @throws Zend_Http_Client_Exception |
---|
1249 | */ |
---|
1250 | protected function _prepareBody() |
---|
1251 | { |
---|
1252 | // According to RFC2616, a TRACE request should not have a body. |
---|
1253 | if ($this->method == self::TRACE) { |
---|
1254 | return ''; |
---|
1255 | } |
---|
1256 | |
---|
1257 | if (isset($this->raw_post_data) && is_resource($this->raw_post_data)) { |
---|
1258 | return $this->raw_post_data; |
---|
1259 | } |
---|
1260 | // If mbstring overloads substr and strlen functions, we have to |
---|
1261 | // override it's internal encoding |
---|
1262 | if (function_exists('mb_internal_encoding') && |
---|
1263 | ((int) ini_get('mbstring.func_overload')) & 2) { |
---|
1264 | |
---|
1265 | $mbIntEnc = mb_internal_encoding(); |
---|
1266 | mb_internal_encoding('ASCII'); |
---|
1267 | } |
---|
1268 | |
---|
1269 | // If we have raw_post_data set, just use it as the body. |
---|
1270 | if (isset($this->raw_post_data)) { |
---|
1271 | $this->setHeaders(self::CONTENT_LENGTH, strlen($this->raw_post_data)); |
---|
1272 | if (isset($mbIntEnc)) { |
---|
1273 | mb_internal_encoding($mbIntEnc); |
---|
1274 | } |
---|
1275 | |
---|
1276 | return $this->raw_post_data; |
---|
1277 | } |
---|
1278 | |
---|
1279 | $body = ''; |
---|
1280 | |
---|
1281 | // If we have files to upload, force enctype to multipart/form-data |
---|
1282 | if (count ($this->files) > 0) { |
---|
1283 | $this->setEncType(self::ENC_FORMDATA); |
---|
1284 | } |
---|
1285 | |
---|
1286 | // If we have POST parameters or files, encode and add them to the body |
---|
1287 | if (count($this->paramsPost) > 0 || count($this->files) > 0) { |
---|
1288 | switch($this->enctype) { |
---|
1289 | case self::ENC_FORMDATA: |
---|
1290 | // Encode body as multipart/form-data |
---|
1291 | $boundary = '---ZENDHTTPCLIENT-' . md5(microtime()); |
---|
1292 | $this->setHeaders(self::CONTENT_TYPE, self::ENC_FORMDATA . "; boundary={$boundary}"); |
---|
1293 | |
---|
1294 | // Encode all files and POST vars in the order they were given |
---|
1295 | foreach ($this->body_field_order as $fieldName=>$fieldType) { |
---|
1296 | switch ($fieldType) { |
---|
1297 | case self::VTYPE_FILE: |
---|
1298 | foreach ($this->files as $file) { |
---|
1299 | if ($file['formname']===$fieldName) { |
---|
1300 | $fhead = array(self::CONTENT_TYPE => $file['ctype']); |
---|
1301 | $body .= self::encodeFormData($boundary, $file['formname'], $file['data'], $file['filename'], $fhead); |
---|
1302 | } |
---|
1303 | } |
---|
1304 | break; |
---|
1305 | case self::VTYPE_SCALAR: |
---|
1306 | if (isset($this->paramsPost[$fieldName])) { |
---|
1307 | if (is_array($this->paramsPost[$fieldName])) { |
---|
1308 | $flattened = self::_flattenParametersArray($this->paramsPost[$fieldName], $fieldName); |
---|
1309 | foreach ($flattened as $pp) { |
---|
1310 | $body .= self::encodeFormData($boundary, $pp[0], $pp[1]); |
---|
1311 | } |
---|
1312 | } else { |
---|
1313 | $body .= self::encodeFormData($boundary, $fieldName, $this->paramsPost[$fieldName]); |
---|
1314 | } |
---|
1315 | } |
---|
1316 | break; |
---|
1317 | } |
---|
1318 | } |
---|
1319 | |
---|
1320 | $body .= "--{$boundary}--\r\n"; |
---|
1321 | break; |
---|
1322 | |
---|
1323 | case self::ENC_URLENCODED: |
---|
1324 | // Encode body as application/x-www-form-urlencoded |
---|
1325 | $this->setHeaders(self::CONTENT_TYPE, self::ENC_URLENCODED); |
---|
1326 | $body = http_build_query($this->paramsPost, '', '&'); |
---|
1327 | break; |
---|
1328 | |
---|
1329 | default: |
---|
1330 | if (isset($mbIntEnc)) { |
---|
1331 | mb_internal_encoding($mbIntEnc); |
---|
1332 | } |
---|
1333 | |
---|
1334 | /** @see Zend_Http_Client_Exception */ |
---|
1335 | require_once 'Zend/Http/Client/Exception.php'; |
---|
1336 | throw new Zend_Http_Client_Exception("Cannot handle content type '{$this->enctype}' automatically." . |
---|
1337 | " Please use Zend_Http_Client::setRawData to send this kind of content."); |
---|
1338 | break; |
---|
1339 | } |
---|
1340 | } |
---|
1341 | |
---|
1342 | // Set the Content-Length if we have a body or if request is POST/PUT |
---|
1343 | if ($body || $this->method == self::POST || $this->method == self::PUT) { |
---|
1344 | $this->setHeaders(self::CONTENT_LENGTH, strlen($body)); |
---|
1345 | } |
---|
1346 | |
---|
1347 | if (isset($mbIntEnc)) { |
---|
1348 | mb_internal_encoding($mbIntEnc); |
---|
1349 | } |
---|
1350 | |
---|
1351 | return $body; |
---|
1352 | } |
---|
1353 | |
---|
1354 | /** |
---|
1355 | * Helper method that gets a possibly multi-level parameters array (get or |
---|
1356 | * post) and flattens it. |
---|
1357 | * |
---|
1358 | * The method returns an array of (key, value) pairs (because keys are not |
---|
1359 | * necessarily unique. If one of the parameters in as array, it will also |
---|
1360 | * add a [] suffix to the key. |
---|
1361 | * |
---|
1362 | * This method is deprecated since Zend Framework 1.9 in favour of |
---|
1363 | * self::_flattenParametersArray() and will be dropped in 2.0 |
---|
1364 | * |
---|
1365 | * @deprecated since 1.9 |
---|
1366 | * |
---|
1367 | * @param array $parray The parameters array |
---|
1368 | * @param bool $urlencode Whether to urlencode the name and value |
---|
1369 | * @return array |
---|
1370 | */ |
---|
1371 | protected function _getParametersRecursive($parray, $urlencode = false) |
---|
1372 | { |
---|
1373 | // Issue a deprecated notice |
---|
1374 | trigger_error("The " . __METHOD__ . " method is deprecated and will be dropped in 2.0.", |
---|
1375 | E_USER_NOTICE); |
---|
1376 | |
---|
1377 | if (! is_array($parray)) { |
---|
1378 | return $parray; |
---|
1379 | } |
---|
1380 | $parameters = array(); |
---|
1381 | |
---|
1382 | foreach ($parray as $name => $value) { |
---|
1383 | if ($urlencode) { |
---|
1384 | $name = urlencode($name); |
---|
1385 | } |
---|
1386 | |
---|
1387 | // If $value is an array, iterate over it |
---|
1388 | if (is_array($value)) { |
---|
1389 | $name .= ($urlencode ? '%5B%5D' : '[]'); |
---|
1390 | foreach ($value as $subval) { |
---|
1391 | if ($urlencode) { |
---|
1392 | $subval = urlencode($subval); |
---|
1393 | } |
---|
1394 | $parameters[] = array($name, $subval); |
---|
1395 | } |
---|
1396 | } else { |
---|
1397 | if ($urlencode) { |
---|
1398 | $value = urlencode($value); |
---|
1399 | } |
---|
1400 | $parameters[] = array($name, $value); |
---|
1401 | } |
---|
1402 | } |
---|
1403 | |
---|
1404 | return $parameters; |
---|
1405 | } |
---|
1406 | |
---|
1407 | /** |
---|
1408 | * Attempt to detect the MIME type of a file using available extensions |
---|
1409 | * |
---|
1410 | * This method will try to detect the MIME type of a file. If the fileinfo |
---|
1411 | * extension is available, it will be used. If not, the mime_magic |
---|
1412 | * extension which is deprected but is still available in many PHP setups |
---|
1413 | * will be tried. |
---|
1414 | * |
---|
1415 | * If neither extension is available, the default application/octet-stream |
---|
1416 | * MIME type will be returned |
---|
1417 | * |
---|
1418 | * @param string $file File path |
---|
1419 | * @return string MIME type |
---|
1420 | */ |
---|
1421 | protected function _detectFileMimeType($file) |
---|
1422 | { |
---|
1423 | $type = null; |
---|
1424 | |
---|
1425 | // First try with fileinfo functions |
---|
1426 | if (function_exists('finfo_open')) { |
---|
1427 | if (self::$_fileInfoDb === null) { |
---|
1428 | self::$_fileInfoDb = @finfo_open(FILEINFO_MIME); |
---|
1429 | } |
---|
1430 | |
---|
1431 | if (self::$_fileInfoDb) { |
---|
1432 | $type = finfo_file(self::$_fileInfoDb, $file); |
---|
1433 | } |
---|
1434 | |
---|
1435 | } elseif (function_exists('mime_content_type')) { |
---|
1436 | $type = mime_content_type($file); |
---|
1437 | } |
---|
1438 | |
---|
1439 | // Fallback to the default application/octet-stream |
---|
1440 | if (! $type) { |
---|
1441 | $type = 'application/octet-stream'; |
---|
1442 | } |
---|
1443 | |
---|
1444 | return $type; |
---|
1445 | } |
---|
1446 | |
---|
1447 | /** |
---|
1448 | * Encode data to a multipart/form-data part suitable for a POST request. |
---|
1449 | * |
---|
1450 | * @param string $boundary |
---|
1451 | * @param string $name |
---|
1452 | * @param mixed $value |
---|
1453 | * @param string $filename |
---|
1454 | * @param array $headers Associative array of optional headers @example ("Content-Transfer-Encoding" => "binary") |
---|
1455 | * @return string |
---|
1456 | */ |
---|
1457 | public static function encodeFormData($boundary, $name, $value, $filename = null, $headers = array()) { |
---|
1458 | $ret = "--{$boundary}\r\n" . |
---|
1459 | 'Content-Disposition: form-data; name="' . $name .'"'; |
---|
1460 | |
---|
1461 | if ($filename) { |
---|
1462 | $ret .= '; filename="' . $filename . '"'; |
---|
1463 | } |
---|
1464 | $ret .= "\r\n"; |
---|
1465 | |
---|
1466 | foreach ($headers as $hname => $hvalue) { |
---|
1467 | $ret .= "{$hname}: {$hvalue}\r\n"; |
---|
1468 | } |
---|
1469 | $ret .= "\r\n"; |
---|
1470 | |
---|
1471 | $ret .= "{$value}\r\n"; |
---|
1472 | |
---|
1473 | return $ret; |
---|
1474 | } |
---|
1475 | |
---|
1476 | /** |
---|
1477 | * Create a HTTP authentication "Authorization:" header according to the |
---|
1478 | * specified user, password and authentication method. |
---|
1479 | * |
---|
1480 | * @see http://www.faqs.org/rfcs/rfc2617.html |
---|
1481 | * @param string $user |
---|
1482 | * @param string $password |
---|
1483 | * @param string $type |
---|
1484 | * @return string |
---|
1485 | * @throws Zend_Http_Client_Exception |
---|
1486 | */ |
---|
1487 | public static function encodeAuthHeader($user, $password, $type = self::AUTH_BASIC) |
---|
1488 | { |
---|
1489 | $authHeader = null; |
---|
1490 | |
---|
1491 | switch ($type) { |
---|
1492 | case self::AUTH_BASIC: |
---|
1493 | // In basic authentication, the user name cannot contain ":" |
---|
1494 | if (strpos($user, ':') !== false) { |
---|
1495 | /** @see Zend_Http_Client_Exception */ |
---|
1496 | require_once 'Zend/Http/Client/Exception.php'; |
---|
1497 | throw new Zend_Http_Client_Exception("The user name cannot contain ':' in 'Basic' HTTP authentication"); |
---|
1498 | } |
---|
1499 | |
---|
1500 | $authHeader = 'Basic ' . base64_encode($user . ':' . $password); |
---|
1501 | break; |
---|
1502 | |
---|
1503 | //case self::AUTH_DIGEST: |
---|
1504 | /** |
---|
1505 | * @todo Implement digest authentication |
---|
1506 | */ |
---|
1507 | // break; |
---|
1508 | |
---|
1509 | default: |
---|
1510 | /** @see Zend_Http_Client_Exception */ |
---|
1511 | require_once 'Zend/Http/Client/Exception.php'; |
---|
1512 | throw new Zend_Http_Client_Exception("Not a supported HTTP authentication type: '$type'"); |
---|
1513 | } |
---|
1514 | |
---|
1515 | return $authHeader; |
---|
1516 | } |
---|
1517 | |
---|
1518 | /** |
---|
1519 | * Convert an array of parameters into a flat array of (key, value) pairs |
---|
1520 | * |
---|
1521 | * Will flatten a potentially multi-dimentional array of parameters (such |
---|
1522 | * as POST parameters) into a flat array of (key, value) paris. In case |
---|
1523 | * of multi-dimentional arrays, square brackets ([]) will be added to the |
---|
1524 | * key to indicate an array. |
---|
1525 | * |
---|
1526 | * @since 1.9 |
---|
1527 | * |
---|
1528 | * @param array $parray |
---|
1529 | * @param string $prefix |
---|
1530 | * @return array |
---|
1531 | */ |
---|
1532 | static protected function _flattenParametersArray($parray, $prefix = null) |
---|
1533 | { |
---|
1534 | if (! is_array($parray)) { |
---|
1535 | return $parray; |
---|
1536 | } |
---|
1537 | |
---|
1538 | $parameters = array(); |
---|
1539 | |
---|
1540 | foreach($parray as $name => $value) { |
---|
1541 | |
---|
1542 | // Calculate array key |
---|
1543 | if ($prefix) { |
---|
1544 | if (is_int($name)) { |
---|
1545 | $key = $prefix . '[]'; |
---|
1546 | } else { |
---|
1547 | $key = $prefix . "[$name]"; |
---|
1548 | } |
---|
1549 | } else { |
---|
1550 | $key = $name; |
---|
1551 | } |
---|
1552 | |
---|
1553 | if (is_array($value)) { |
---|
1554 | $parameters = array_merge($parameters, self::_flattenParametersArray($value, $key)); |
---|
1555 | |
---|
1556 | } else { |
---|
1557 | $parameters[] = array($key, $value); |
---|
1558 | } |
---|
1559 | } |
---|
1560 | |
---|
1561 | return $parameters; |
---|
1562 | } |
---|
1563 | |
---|
1564 | } |
---|