Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Static Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Zend_Http_CookieJar Class Reference
Inheritance diagram for Zend_Http_CookieJar:

Public Member Functions

 __construct ()
 
 addCookie ($cookie, $ref_uri=null, $encodeValue=true)
 
 addCookiesFromResponse ($response, $ref_uri, $encodeValue=true)
 
 getAllCookies ($ret_as=self::COOKIE_OBJECT)
 
 getMatchingCookies ($uri, $matchSessionCookies=true, $ret_as=self::COOKIE_OBJECT, $now=null)
 
 getCookie ($uri, $cookie_name, $ret_as=self::COOKIE_OBJECT)
 
 count ()
 
 getIterator ()
 
 isEmpty ()
 
 reset ()
 

Static Public Member Functions

static fromResponse (Zend_Http_Response $response, $ref_uri)
 

Data Fields

const COOKIE_OBJECT = 0
 
const COOKIE_STRING_ARRAY = 1
 
const COOKIE_STRING_CONCAT = 2
 
const COOKIE_STRING_CONCAT_STRICT = 3
 

Protected Member Functions

 _flattenCookiesArray ($ptr, $ret_as=self::COOKIE_OBJECT)
 
 _matchDomain ($domain)
 
 _matchPath ($domains, $path)
 

Protected Attributes

 $cookies = array()
 
 $_rawCookies = array()
 

Detailed Description

Definition at line 60 of file CookieJar.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( )

Construct a new CookieJar object

Definition at line 117 of file CookieJar.php.

118  { }

Member Function Documentation

◆ _flattenCookiesArray()

_flattenCookiesArray (   $ptr,
  $ret_as = self::COOKIE_OBJECT 
)
protected

Helper function to recursivly flatten an array. Shoud be used when exporting the cookies array (or parts of it)

Parameters
Zend_Http_Cookie | array$ptr
int$ret_asWhat value to return
Returns
array|string

Definition at line 288 of file CookieJar.php.

288  {
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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ _matchDomain()

_matchDomain (   $domain)
protected

Return a subset of the cookies array matching a specific domain

Parameters
string$domain
Returns
array

Definition at line 331 of file CookieJar.php.

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  }

◆ _matchPath()

_matchPath (   $domains,
  $path 
)
protected

Return a subset of a domain-matching cookies that also match a specified path

Parameters
array$dom_array
string$path
Returns
array

Definition at line 351 of file CookieJar.php.

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  }

◆ addCookie()

addCookie (   $cookie,
  $ref_uri = null,
  $encodeValue = true 
)

Add a cookie to the jar. Cookie should be passed either as a Zend_Http_Cookie object or as a string - in which case an object is created from the string.

Parameters
Zend_Http_Cookie | string$cookie
Zend_Uri_Http | string$ref_uriOptional reference URI (for domain, path, secure)
boolean$encodeValue

Definition at line 128 of file CookieJar.php.

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  }

◆ addCookiesFromResponse()

addCookiesFromResponse (   $response,
  $ref_uri,
  $encodeValue = true 
)

Parse an HTTP response, adding all the cookies set in that response to the cookie jar.

Parameters
Zend_Http_Response$response
Zend_Uri_Http | string$ref_uriRequested URI
boolean$encodeValue

Definition at line 155 of file CookieJar.php.

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  }
$response
Definition: 404.php:11
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ count()

count ( )

Required by Countable interface

Returns
int

Definition at line 393 of file CookieJar.php.

394  {
395  return count($this->_rawCookies);
396  }

◆ fromResponse()

static fromResponse ( Zend_Http_Response  $response,
  $ref_uri 
)
static

Create a new CookieJar object and automatically load into it all the cookies set in an Http_Response object. If $uri is set, it will be considered as the requested URI for setting default domain and path of the cookie.

Parameters
Zend_Http_Response$responseHTTP Response object
Zend_Uri_Http | string$uriThe requested URI
Returns
Zend_Http_CookieJar
Todo:
Add the $uri functionality.

Definition at line 381 of file CookieJar.php.

382  {
383  $jar = new self();
384  $jar->addCookiesFromResponse($response, $ref_uri);
385  return $jar;
386  }
$response
Definition: 404.php:11

◆ getAllCookies()

getAllCookies (   $ret_as = self::COOKIE_OBJECT)

Get all cookies in the cookie jar as an array

Parameters
int$ret_asWhether to return cookies as objects of Zend_Http_Cookie or as strings
Returns
array|string

Definition at line 180 of file CookieJar.php.

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  }

◆ getCookie()

getCookie (   $uri,
  $cookie_name,
  $ret_as = self::COOKIE_OBJECT 
)

Get a specific cookie according to a URI and name

Parameters
Zend_Uri_Http | string$uriThe uri (domain and path) to match
string$cookie_nameThe cookie's name
int$ret_asWhether to return cookies as objects of Zend_Http_Cookie or as strings
Returns
Zend_Http_Cookie|string

Definition at line 237 of file CookieJar.php.

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  }
static factory($uri='http', $className=null)
Definition: Uri.php:96

◆ getIterator()

getIterator ( )

Required by IteratorAggregate interface

Returns
ArrayIterator

Definition at line 403 of file CookieJar.php.

404  {
405  return new ArrayIterator($this->_rawCookies);
406  }

◆ getMatchingCookies()

getMatchingCookies (   $uri,
  $matchSessionCookies = true,
  $ret_as = self::COOKIE_OBJECT,
  $now = null 
)

Return an array of all cookies matching a specific request according to the request URI, whether session cookies should be sent or not, and the time to consider as "now" when checking cookie expiry time.

Parameters
string | Zend_Uri_Http$uriURI to check against (secure, domain, path)
boolean$matchSessionCookiesWhether to send session cookies
int$ret_asWhether to return cookies as objects of Zend_Http_Cookie or as strings
int$nowOverride the current time when checking for expiry time
Returns
array|string

Definition at line 200 of file CookieJar.php.

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  }
static factory($uri='http', $className=null)
Definition: Uri.php:96

◆ isEmpty()

isEmpty ( )

Tells if the jar is empty of any cookie

Returns
bool

Definition at line 413 of file CookieJar.php.

414  {
415  return count($this) == 0;
416  }

◆ reset()

reset ( )

Empties the cookieJar of any cookie

Returns
Zend_Http_CookieJar

Definition at line 423 of file CookieJar.php.

424  {
425  $this->cookies = $this->_rawCookies = array();
426  return $this;
427  }

Field Documentation

◆ $_rawCookies

$_rawCookies = array()
protected

Definition at line 111 of file CookieJar.php.

◆ $cookies

$cookies = array()
protected

Definition at line 104 of file CookieJar.php.

◆ COOKIE_OBJECT

const COOKIE_OBJECT = 0

Return cookie(s) as a Zend_Http_Cookie object

Definition at line 66 of file CookieJar.php.

◆ COOKIE_STRING_ARRAY

const COOKIE_STRING_ARRAY = 1

Return cookie(s) as a string (suitable for sending in an HTTP request)

Definition at line 72 of file CookieJar.php.

◆ COOKIE_STRING_CONCAT

const COOKIE_STRING_CONCAT = 2

Return all cookies as one long string (suitable for sending in an HTTP request)

Definition at line 78 of file CookieJar.php.

◆ COOKIE_STRING_CONCAT_STRICT

const COOKIE_STRING_CONCAT_STRICT = 3

Return all cookies as one long string (strict mode)

  • Single space after the semi-colon separating each cookie
  • Remove trailing semi-colon, if any

Definition at line 85 of file CookieJar.php.


The documentation for this class was generated from the following file: