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