Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Http / Client / Cookies.php
blobfafb3fa3a6ba9a73b8c2709be862c69554a55999
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Http\Client;
12 use ArrayIterator;
13 use Traversable;
14 use Zend\Http\Header\SetCookie;
15 use Zend\Http\Response;
16 use Zend\Stdlib\ArrayUtils;
17 use Zend\Uri;
19 /**
20 * A Cookies object is designed to contain and maintain HTTP cookies, and should
21 * be used along with Zend\Http\Client in order to manage cookies across HTTP requests and
22 * responses.
24 * The class contains an array of Zend\Http\Header\Cookie objects. Cookies can be added
25 * automatically from a request or manually. Then, the Cookies class can find and return the
26 * cookies needed for a specific HTTP request.
28 * A special parameter can be passed to all methods of this class that return cookies: Cookies
29 * can be returned either in their native form (as Zend\Http\Header\Cookie objects) or as strings -
30 * the later is suitable for sending as the value of the "Cookie" header in an HTTP request.
31 * You can also choose, when returning more than one cookie, whether to get an array of strings
32 * (by passing Zend\Http\Client\Cookies::COOKIE_STRING_ARRAY) or one unified string for all cookies
33 * (by passing Zend\Http\Client\Cookies::COOKIE_STRING_CONCAT).
35 * @link http://wp.netscape.com/newsref/std/cookie_spec.html for some specs.
37 class Cookies
39 /**
40 * Return cookie(s) as a Zend\Http\Header\Cookie object
43 const COOKIE_OBJECT = 0;
45 /**
46 * Return cookie(s) as a string (suitable for sending in an HTTP request)
49 const COOKIE_STRING_ARRAY = 1;
51 /**
52 * Return all cookies as one long string (suitable for sending in an HTTP request)
55 const COOKIE_STRING_CONCAT = 2;
57 /**
58 * Array storing cookies
60 * Cookies are stored according to domain and path:
61 * $cookies
62 * + www.mydomain.com
63 * + /
64 * - cookie1
65 * - cookie2
66 * + /somepath
67 * - othercookie
68 * + www.otherdomain.net
69 * + /
70 * - alsocookie
72 * @var array
74 protected $cookies = array();
76 /**
77 * The Zend\Http\Header\Cookie array
79 * @var array
81 protected $rawCookies = array();
83 /**
84 * Add a cookie to the class. Cookie should be passed either as a Zend\Http\Header\Cookie object
85 * or as a string - in which case an object is created from the string.
87 * @param SetCookie|string $cookie
88 * @param Uri\Uri|string $refUri Optional reference URI (for domain, path, secure)
89 * @throws Exception\InvalidArgumentException if invalid $cookie value
91 public function addCookie($cookie, $refUri = null)
93 if (is_string($cookie)) {
94 $cookie = SetCookie::fromString($cookie, $refUri);
97 if ($cookie instanceof SetCookie) {
98 $domain = $cookie->getDomain();
99 $path = $cookie->getPath();
100 if (!isset($this->cookies[$domain])) {
101 $this->cookies[$domain] = array();
103 if (!isset($this->cookies[$domain][$path])) {
104 $this->cookies[$domain][$path] = array();
106 $this->cookies[$domain][$path][$cookie->getName()] = $cookie;
107 $this->rawCookies[] = $cookie;
108 } else {
109 throw new Exception\InvalidArgumentException('Supplient argument is not a valid cookie string or object');
114 * Parse an HTTP response, adding all the cookies set in that response
116 * @param Response $response
117 * @param Uri\Uri|string $refUri Requested URI
119 public function addCookiesFromResponse(Response $response, $refUri)
121 $cookieHdrs = $response->getHeaders()->get('Set-Cookie');
122 if ($cookieHdrs instanceof Traversable) {
123 $cookieHdrs = ArrayUtils::iteratorToArray($cookieHdrs);
126 if (is_array($cookieHdrs)) {
127 foreach ($cookieHdrs as $cookie) {
128 $this->addCookie($cookie, $refUri);
130 } elseif (is_string($cookieHdrs)) {
131 $this->addCookie($cookieHdrs, $refUri);
136 * Get all cookies in the cookie jar as an array
138 * @param int $retAs Whether to return cookies as objects of \Zend\Http\Header\Cookie or as strings
139 * @return array|string
141 public function getAllCookies($retAs = self::COOKIE_OBJECT)
143 $cookies = $this->_flattenCookiesArray($this->cookies, $retAs);
144 return $cookies;
148 * Return an array of all cookies matching a specific request according to the request URI,
149 * whether session cookies should be sent or not, and the time to consider as "now" when
150 * checking cookie expiry time.
152 * @param string|Uri\Uri $uri URI to check against (secure, domain, path)
153 * @param bool $matchSessionCookies Whether to send session cookies
154 * @param int $retAs Whether to return cookies as objects of \Zend\Http\Header\Cookie or as strings
155 * @param int $now Override the current time when checking for expiry time
156 * @throws Exception\InvalidArgumentException if invalid URI
157 * @return array|string
159 public function getMatchingCookies($uri, $matchSessionCookies = true,
160 $retAs = self::COOKIE_OBJECT, $now = null)
162 if (is_string($uri)) {
163 $uri = Uri\UriFactory::factory($uri, 'http');
164 } elseif (!$uri instanceof Uri\Uri) {
165 throw new Exception\InvalidArgumentException("Invalid URI string or object passed");
168 $host = $uri->getHost();
169 if (empty($host)) {
170 throw new Exception\InvalidArgumentException('Invalid URI specified; does not contain a host');
173 // First, reduce the array of cookies to only those matching domain and path
174 $cookies = $this->_matchDomain($host);
175 $cookies = $this->_matchPath($cookies, $uri->getPath());
176 $cookies = $this->_flattenCookiesArray($cookies, self::COOKIE_OBJECT);
178 // Next, run Cookie->match on all cookies to check secure, time and session matching
179 $ret = array();
180 foreach ($cookies as $cookie)
181 if ($cookie->match($uri, $matchSessionCookies, $now))
182 $ret[] = $cookie;
184 // Now, use self::_flattenCookiesArray again - only to convert to the return format ;)
185 $ret = $this->_flattenCookiesArray($ret, $retAs);
187 return $ret;
191 * Get a specific cookie according to a URI and name
193 * @param Uri\Uri|string $uri The uri (domain and path) to match
194 * @param string $cookieName The cookie's name
195 * @param int $retAs Whether to return cookies as objects of \Zend\Http\Header\Cookie or as strings
196 * @throws Exception\InvalidArgumentException if invalid URI specified or invalid $retAs value
197 * @return Cookie|string
199 public function getCookie($uri, $cookieName, $retAs = self::COOKIE_OBJECT)
201 if (is_string($uri)) {
202 $uri = Uri\UriFactory::factory($uri, 'http');
203 } elseif (!$uri instanceof Uri\Uri) {
204 throw new Exception\InvalidArgumentException('Invalid URI specified');
207 $host = $uri->getHost();
208 if (empty($host)) {
209 throw new Exception\InvalidArgumentException('Invalid URI specified; host missing');
212 // Get correct cookie path
213 $path = $uri->getPath();
214 $path = substr($path, 0, strrpos($path, '/'));
215 if (! $path) $path = '/';
217 if (isset($this->cookies[$uri->getHost()][$path][$cookieName])) {
218 $cookie = $this->cookies[$uri->getHost()][$path][$cookieName];
220 switch ($retAs) {
221 case self::COOKIE_OBJECT:
222 return $cookie;
223 break;
225 case self::COOKIE_STRING_ARRAY:
226 case self::COOKIE_STRING_CONCAT:
227 return $cookie->__toString();
228 break;
230 default:
231 throw new Exception\InvalidArgumentException("Invalid value passed for \$retAs: {$retAs}");
232 break;
236 return false;
240 * Helper function to recursively flatten an array. Should be used when exporting the
241 * cookies array (or parts of it)
243 * @param \Zend\Http\Header\Cookie|array $ptr
244 * @param int $retAs What value to return
245 * @return array|string
247 protected function _flattenCookiesArray($ptr, $retAs = self::COOKIE_OBJECT)
249 if (is_array($ptr)) {
250 $ret = ($retAs == self::COOKIE_STRING_CONCAT ? '' : array());
251 foreach ($ptr as $item) {
252 if ($retAs == self::COOKIE_STRING_CONCAT) {
253 $ret .= $this->_flattenCookiesArray($item, $retAs);
254 } else {
255 $ret = array_merge($ret, $this->_flattenCookiesArray($item, $retAs));
258 return $ret;
259 } elseif ($ptr instanceof SetCookie) {
260 switch ($retAs) {
261 case self::COOKIE_STRING_ARRAY:
262 return array($ptr->__toString());
263 break;
265 case self::COOKIE_STRING_CONCAT:
266 return $ptr->__toString();
267 break;
269 case self::COOKIE_OBJECT:
270 default:
271 return array($ptr);
272 break;
276 return null;
280 * Return a subset of the cookies array matching a specific domain
282 * @param string $domain
283 * @return array
285 protected function _matchDomain($domain)
287 $ret = array();
289 foreach (array_keys($this->cookies) as $cdom) {
290 if (SetCookie::matchCookieDomain($cdom, $domain)) {
291 $ret[$cdom] = $this->cookies[$cdom];
295 return $ret;
299 * Return a subset of a domain-matching cookies that also match a specified path
301 * @param array $domains
302 * @param string $path
303 * @return array
305 protected function _matchPath($domains, $path)
307 $ret = array();
309 foreach ($domains as $dom => $pathsArray) {
310 foreach (array_keys($pathsArray) as $cpath) {
311 if (SetCookie::matchCookiePath($cpath, $path)) {
312 if (! isset($ret[$dom])) {
313 $ret[$dom] = array();
316 $ret[$dom][$cpath] = $pathsArray[$cpath];
321 return $ret;
325 * Create a new Cookies object and automatically load into it all the
326 * cookies set in an Http_Response object. If $uri is set, it will be
327 * considered as the requested URI for setting default domain and path
328 * of the cookie.
330 * @param Response $response HTTP Response object
331 * @param Uri\Uri|string $refUri The requested URI
332 * @return Cookies
333 * @todo Add the $uri functionality.
335 public static function fromResponse(Response $response, $refUri)
337 $jar = new static();
338 $jar->addCookiesFromResponse($response, $refUri);
339 return $jar;
343 * Required by Countable interface
345 * @return int
347 public function count()
349 return count($this->rawCookies);
353 * Required by IteratorAggregate interface
355 * @return ArrayIterator
357 public function getIterator()
359 return new ArrayIterator($this->rawCookies);
363 * Tells if the array of cookies is empty
365 * @return bool
367 public function isEmpty()
369 return count($this) == 0;
373 * Empties the cookieJar of any cookie
375 * @return Cookies
377 public function reset()
379 $this->cookies = $this->rawCookies = array();
380 return $this;
384 * (PHP 5 &gt;= 5.1.0)<br/>
385 * Whether a offset exists
386 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
387 * @param mixed $offset <p>
388 * An offset to check for.
389 * </p>
390 * @return bool Returns true on success or false on failure.
391 * </p>
392 * <p>
393 * The return value will be casted to boolean if non-boolean was returned.
395 public function offsetExists($offset)
397 // TODO: Implement offsetExists() method.
401 * (PHP 5 &gt;= 5.1.0)<br/>
402 * Offset to retrieve
403 * @link http://php.net/manual/en/arrayaccess.offsetget.php
404 * @param mixed $offset <p>
405 * The offset to retrieve.
406 * </p>
407 * @return mixed Can return all value types.
409 public function offsetGet($offset)
411 // TODO: Implement offsetGet() method.
415 * (PHP 5 &gt;= 5.1.0)<br/>
416 * Offset to set
417 * @link http://php.net/manual/en/arrayaccess.offsetset.php
418 * @param mixed $offset <p>
419 * The offset to assign the value to.
420 * </p>
421 * @param mixed $value <p>
422 * The value to set.
423 * </p>
424 * @return void
426 public function offsetSet($offset, $value)
428 // TODO: Implement offsetSet() method.
432 * (PHP 5 &gt;= 5.1.0)<br/>
433 * Offset to unset
434 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
435 * @param mixed $offset <p>
436 * The offset to unset.
437 * </p>
438 * @return void
440 public function offsetUnset($offset)
442 // TODO: Implement offsetUnset() method.
446 * (PHP 5 &gt;= 5.1.0)<br/>
447 * String representation of object
448 * @link http://php.net/manual/en/serializable.serialize.php
449 * @return string the string representation of the object or &null;
451 public function serialize()
453 // TODO: Implement serialize() method.
457 * (PHP 5 &gt;= 5.1.0)<br/>
458 * Constructs the object
459 * @link http://php.net/manual/en/serializable.unserialize.php
460 * @param string $serialized <p>
461 * The string representation of the object.
462 * </p>
463 * @return mixed the original value unserialized.
465 public function unserialize($serialized)
467 // TODO: Implement unserialize() method.
470 public function fromArray(array $values)
472 // TODO: Implement fromArray() method.
475 public function fromString($string)
477 // TODO: Implement fromString() method.
480 public function toArray()
482 // TODO: Implement toArray() method.
485 public function toString()
487 // TODO: Implement toString() method.
490 public function get($name, $default = null)
492 // TODO: Implement get() method.
495 public function set($name, $value)
497 // TODO: Implement set() method.