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