Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CookieJar.php
Go to the documentation of this file.
1 <?php
26 #require_once "Zend/Uri.php";
27 
30 #require_once "Zend/Http/Cookie.php";
31 
34 #require_once "Zend/Http/Response.php";
35 
60 class Zend_Http_CookieJar implements Countable, IteratorAggregate
61 {
66  const COOKIE_OBJECT = 0;
67 
73 
79 
86 
104  protected $cookies = array();
105 
111  protected $_rawCookies = array();
112 
117  public function __construct()
118  { }
119 
128  public function addCookie($cookie, $ref_uri = null, $encodeValue = true)
129  {
130  if (is_string($cookie)) {
131  $cookie = Zend_Http_Cookie::fromString($cookie, $ref_uri, $encodeValue);
132  }
133 
134  if ($cookie instanceof Zend_Http_Cookie) {
135  $domain = $cookie->getDomain();
136  $path = $cookie->getPath();
137  if (! isset($this->cookies[$domain])) $this->cookies[$domain] = array();
138  if (! isset($this->cookies[$domain][$path])) $this->cookies[$domain][$path] = array();
139  $this->cookies[$domain][$path][$cookie->getName()] = $cookie;
140  $this->_rawCookies[] = $cookie;
141  } else {
142  #require_once 'Zend/Http/Exception.php';
143  throw new Zend_Http_Exception('Supplient argument is not a valid cookie string or object');
144  }
145  }
146 
155  public function addCookiesFromResponse($response, $ref_uri, $encodeValue = true)
156  {
157  if (! $response instanceof Zend_Http_Response) {
158  #require_once 'Zend/Http/Exception.php';
159  throw new Zend_Http_Exception('$response is expected to be a Response object, ' .
160  gettype($response) . ' was passed');
161  }
162 
163  $cookie_hdrs = $response->getHeader('Set-Cookie');
164 
165  if (is_array($cookie_hdrs)) {
166  foreach ($cookie_hdrs as $cookie) {
167  $this->addCookie($cookie, $ref_uri, $encodeValue);
168  }
169  } elseif (is_string($cookie_hdrs)) {
170  $this->addCookie($cookie_hdrs, $ref_uri, $encodeValue);
171  }
172  }
173 
180  public function getAllCookies($ret_as = self::COOKIE_OBJECT)
181  {
182  $cookies = $this->_flattenCookiesArray($this->cookies, $ret_as);
183  if($ret_as == self::COOKIE_STRING_CONCAT_STRICT) {
184  $cookies = rtrim(trim($cookies), ';');
185  }
186  return $cookies;
187  }
188 
200  public function getMatchingCookies($uri, $matchSessionCookies = true,
201  $ret_as = self::COOKIE_OBJECT, $now = null)
202  {
203  if (is_string($uri)) $uri = Zend_Uri::factory($uri);
204  if (! $uri instanceof Zend_Uri_Http) {
205  #require_once 'Zend/Http/Exception.php';
206  throw new Zend_Http_Exception("Invalid URI string or object passed");
207  }
208 
209  // First, reduce the array of cookies to only those matching domain and path
210  $cookies = $this->_matchDomain($uri->getHost());
211  $cookies = $this->_matchPath($cookies, $uri->getPath());
212  $cookies = $this->_flattenCookiesArray($cookies, self::COOKIE_OBJECT);
213 
214  // Next, run Cookie->match on all cookies to check secure, time and session mathcing
215  $ret = array();
216  foreach ($cookies as $cookie)
217  if ($cookie->match($uri, $matchSessionCookies, $now))
218  $ret[] = $cookie;
219 
220  // Now, use self::_flattenCookiesArray again - only to convert to the return format ;)
221  $ret = $this->_flattenCookiesArray($ret, $ret_as);
222  if($ret_as == self::COOKIE_STRING_CONCAT_STRICT) {
223  $ret = rtrim(trim($ret), ';');
224  }
225 
226  return $ret;
227  }
228 
237  public function getCookie($uri, $cookie_name, $ret_as = self::COOKIE_OBJECT)
238  {
239  if (is_string($uri)) {
240  $uri = Zend_Uri::factory($uri);
241  }
242 
243  if (! $uri instanceof Zend_Uri_Http) {
244  #require_once 'Zend/Http/Exception.php';
245  throw new Zend_Http_Exception('Invalid URI specified');
246  }
247 
248  // Get correct cookie path
249  $path = $uri->getPath();
250  $path = substr($path, 0, strrpos($path, '/'));
251  if (! $path) $path = '/';
252 
253  if (isset($this->cookies[$uri->getHost()][$path][$cookie_name])) {
254  $cookie = $this->cookies[$uri->getHost()][$path][$cookie_name];
255 
256  switch ($ret_as) {
257  case self::COOKIE_OBJECT:
258  return $cookie;
259  break;
260 
262  return rtrim(trim($cookie->__toString()), ';');
263  break;
264 
267  return $cookie->__toString();
268  break;
269 
270  default:
271  #require_once 'Zend/Http/Exception.php';
272  throw new Zend_Http_Exception("Invalid value passed for \$ret_as: {$ret_as}");
273  break;
274  }
275  } else {
276  return false;
277  }
278  }
279 
288  protected function _flattenCookiesArray($ptr, $ret_as = self::COOKIE_OBJECT) {
289  if (is_array($ptr)) {
290  $ret = ($ret_as == self::COOKIE_STRING_CONCAT || $ret_as == self::COOKIE_STRING_CONCAT_STRICT) ? '' : array();
291  foreach ($ptr as $item) {
292  if ($ret_as == self::COOKIE_STRING_CONCAT_STRICT) {
293  $postfix_combine = (!is_array($item) ? ' ' : '');
294  $ret .= $this->_flattenCookiesArray($item, $ret_as) . $postfix_combine;
295  } elseif ($ret_as == self::COOKIE_STRING_CONCAT) {
296  $ret .= $this->_flattenCookiesArray($item, $ret_as);
297  } else {
298  $ret = array_merge($ret, $this->_flattenCookiesArray($item, $ret_as));
299  }
300  }
301  return $ret;
302  } elseif ($ptr instanceof Zend_Http_Cookie) {
303  switch ($ret_as) {
305  return array($ptr->__toString());
306  break;
307 
309  // break intentionally omitted
310 
312  return $ptr->__toString();
313  break;
314 
315  case self::COOKIE_OBJECT:
316  default:
317  return array($ptr);
318  break;
319  }
320  }
321 
322  return null;
323  }
324 
331  protected function _matchDomain($domain)
332  {
333  $ret = array();
334 
335  foreach (array_keys($this->cookies) as $cdom) {
336  if (Zend_Http_Cookie::matchCookieDomain($cdom, $domain)) {
337  $ret[$cdom] = $this->cookies[$cdom];
338  }
339  }
340 
341  return $ret;
342  }
343 
351  protected function _matchPath($domains, $path)
352  {
353  $ret = array();
354 
355  foreach ($domains as $dom => $paths_array) {
356  foreach (array_keys($paths_array) as $cpath) {
358  if (! isset($ret[$dom])) {
359  $ret[$dom] = array();
360  }
361 
362  $ret[$dom][$cpath] = $paths_array[$cpath];
363  }
364  }
365  }
366 
367  return $ret;
368  }
369 
381  public static function fromResponse(Zend_Http_Response $response, $ref_uri)
382  {
383  $jar = new self();
384  $jar->addCookiesFromResponse($response, $ref_uri);
385  return $jar;
386  }
387 
393  public function count()
394  {
395  return count($this->_rawCookies);
396  }
397 
403  public function getIterator()
404  {
405  return new ArrayIterator($this->_rawCookies);
406  }
407 
413  public function isEmpty()
414  {
415  return count($this) == 0;
416  }
417 
423  public function reset()
424  {
425  $this->cookies = $this->_rawCookies = array();
426  return $this;
427  }
428 }
$response
Definition: 404.php:11
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
static factory($uri='http', $className=null)
Definition: Uri.php:96