Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Static Public Member Functions | Protected Attributes
Zend_Http_Cookie Class Reference

Public Member Functions

 __construct ($name, $value, $domain, $expires=null, $path=null, $secure=false)
 
 getName ()
 
 getValue ()
 
 getDomain ()
 
 getPath ()
 
 getExpiryTime ()
 
 isSecure ()
 
 isExpired ($now=null)
 
 isSessionCookie ()
 
 match ($uri, $matchSessionCookies=true, $now=null)
 
 __toString ()
 

Static Public Member Functions

static fromString ($cookieStr, $refUri=null, $encodeValue=true)
 
static matchCookieDomain ($cookieDomain, $host)
 
static matchCookiePath ($cookiePath, $path)
 

Protected Attributes

 $name
 
 $value
 
 $expires
 
 $domain
 
 $path
 
 $secure
 
 $encodeValue
 

Detailed Description

Definition at line 47 of file Cookie.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $name,
  $value,
  $domain,
  $expires = null,
  $path = null,
  $secure = false 
)

Cookie object constructor

Todo:
Add validation of each one of the parameters (legal domain, etc.)
Parameters
string$name
string$value
string$domain
int$expires
string$path
bool$secure

Definition at line 110 of file Cookie.php.

111  {
112  if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
113  #require_once 'Zend/Http/Exception.php';
114  throw new Zend_Http_Exception("Cookie name cannot contain these characters: =,; \\t\\r\\n\\013\\014 ({$name})");
115  }
116 
117  if (! $this->name = (string) $name) {
118  #require_once 'Zend/Http/Exception.php';
119  throw new Zend_Http_Exception('Cookies must have a name');
120  }
121 
122  if (! $this->domain = (string) $domain) {
123  #require_once 'Zend/Http/Exception.php';
124  throw new Zend_Http_Exception('Cookies must have a domain');
125  }
126 
127  $this->value = (string) $value;
128  $this->expires = ($expires === null ? null : (int) $expires);
129  $this->path = ($path ? $path : '/');
130  $this->secure = $secure;
131  }
$block setTitle( 'CMS Block Title') -> setIdentifier('fixture_block') ->setContent('< h1 >Fixture Block Title</h1 >< a href=" store url</a><p> Config value
Definition: block.php:9

Member Function Documentation

◆ __toString()

__toString ( )

Get the cookie as a string, suitable for sending as a "Cookie" header in an HTTP request

Returns
string

Definition at line 266 of file Cookie.php.

267  {
268  if ($this->encodeValue) {
269  return $this->name . '=' . urlencode($this->value) . ';';
270  }
271  return $this->name . '=' . $this->value . ';';
272  }
$block setTitle( 'CMS Block Title') -> setIdentifier('fixture_block') ->setContent('< h1 >Fixture Block Title</h1 >< a href=" store url</a><p> Config value
Definition: block.php:9

◆ fromString()

static fromString (   $cookieStr,
  $refUri = null,
  $encodeValue = true 
)
static

Generate a new Cookie object from a cookie string (for example the value of the Set-Cookie HTTP header)

Parameters
string$cookieStr
Zend_Uri_Http | string$refUriReference URI for default values (domain, path)
boolean$encodeValueWhether or not the cookie's value should be passed through urlencode/urldecode
Returns
Zend_Http_Cookie A new Zend_Http_Cookie object or false on failure.

The expiration is past Tue, 19 Jan 2038 03:14:07 UTC the maximum for 32-bit signed integer. Zend_Date can get around that limit.

See also
Zend_Date

Definition at line 284 of file Cookie.php.

285  {
286  // Set default values
287  if (is_string($refUri)) {
288  $refUri = Zend_Uri_Http::factory($refUri);
289  }
290 
291  $name = '';
292  $value = '';
293  $domain = '';
294  $path = '';
295  $expires = null;
296  $secure = false;
297  $parts = explode(';', $cookieStr);
298 
299  // If first part does not include '=', fail
300  if (strpos($parts[0], '=') === false) return false;
301 
302  // Get the name and value of the cookie
303  list($name, $value) = explode('=', trim(array_shift($parts)), 2);
304  $name = trim($name);
305  if ($encodeValue) {
306  $value = urldecode(trim($value));
307  }
308 
309  // Set default domain and path
310  if ($refUri instanceof Zend_Uri_Http) {
311  $domain = $refUri->getHost();
312  $path = $refUri->getPath();
313  $path = substr($path, 0, strrpos($path, '/'));
314  }
315 
316  // Set other cookie parameters
317  foreach ($parts as $part) {
318  $part = trim($part);
319  if (strtolower($part) == 'secure') {
320  $secure = true;
321  continue;
322  }
323 
324  $keyValue = explode('=', $part, 2);
325  if (count($keyValue) == 2) {
326  list($k, $v) = $keyValue;
327  switch (strtolower($k)) {
328  case 'expires':
329  if(($expires = strtotime($v)) === false) {
337  #require_once 'Zend/Date.php';
338 
339  $expireDate = new Zend_Date($v);
340  $expires = $expireDate->getTimestamp();
341  }
342  break;
343 
344  case 'path':
345  $path = $v;
346  break;
347 
348  case 'domain':
349  $domain = $v;
350  break;
351 
352  default:
353  break;
354  }
355  }
356  }
357 
358  if ($name !== '') {
359  $ret = new self($name, $value, $domain, $expires, $path, $secure);
360  $ret->encodeValue = ($encodeValue) ? true : false;
361  return $ret;
362  } else {
363  return false;
364  }
365  }

◆ getDomain()

getDomain ( )

Get cookie domain

Returns
string

Definition at line 158 of file Cookie.php.

159  {
160  return $this->domain;
161  }

◆ getExpiryTime()

getExpiryTime ( )

Get the expiry time of the cookie, or null if no expiry time is set

Returns
int|null

Definition at line 178 of file Cookie.php.

179  {
180  return $this->expires;
181  }

◆ getName()

getName ( )

Get Cookie name

Returns
string

Definition at line 138 of file Cookie.php.

139  {
140  return $this->name;
141  }

◆ getPath()

getPath ( )

Get the cookie path

Returns
string

Definition at line 168 of file Cookie.php.

169  {
170  return $this->path;
171  }

◆ getValue()

getValue ( )

Get cookie value

Returns
string

Definition at line 148 of file Cookie.php.

149  {
150  return $this->value;
151  }

◆ isExpired()

isExpired (   $now = null)

Check whether the cookie has expired

Always returns false if the cookie is a session cookie (has no expiry time)

Parameters
int$nowTimestamp to consider as "now"
Returns
boolean

Definition at line 201 of file Cookie.php.

202  {
203  if ($now === null) $now = time();
204  if (is_int($this->expires) && $this->expires < $now) {
205  return true;
206  } else {
207  return false;
208  }
209  }

◆ isSecure()

isSecure ( )

Check whether the cookie should only be sent over secure connections

Returns
boolean

Definition at line 188 of file Cookie.php.

189  {
190  return $this->secure;
191  }

◆ isSessionCookie()

isSessionCookie ( )

Check whether the cookie is a session cookie (has no expiry time set)

Returns
boolean

Definition at line 216 of file Cookie.php.

217  {
218  return ($this->expires === null);
219  }

◆ match()

match (   $uri,
  $matchSessionCookies = true,
  $now = null 
)

Checks whether the cookie should be sent or not in a specific scenario

Parameters
string | Zend_Uri_Http$uriURI to check against (secure, domain, path)
boolean$matchSessionCookiesWhether to send session cookies
int$nowOverride the current time when checking for expiry time
Returns
boolean

Definition at line 229 of file Cookie.php.

230  {
231  if (is_string ($uri)) {
232  $uri = Zend_Uri_Http::factory($uri);
233  }
234 
235  // Make sure we have a valid Zend_Uri_Http object
236  if (! ($uri->valid() && ($uri->getScheme() == 'http' || $uri->getScheme() =='https'))) {
237  #require_once 'Zend/Http/Exception.php';
238  throw new Zend_Http_Exception('Passed URI is not a valid HTTP or HTTPS URI');
239  }
240 
241  // Check that the cookie is secure (if required) and not expired
242  if ($this->secure && $uri->getScheme() != 'https') return false;
243  if ($this->isExpired($now)) return false;
244  if ($this->isSessionCookie() && ! $matchSessionCookies) return false;
245 
246  // Check if the domain matches
247  if (! self::matchCookieDomain($this->getDomain(), $uri->getHost())) {
248  return false;
249  }
250 
251  // Check that path matches using prefix match
252  if (! self::matchCookiePath($this->getPath(), $uri->getPath())) {
253  return false;
254  }
255 
256  // If we didn't die until now, return true.
257  return true;
258  }

◆ matchCookieDomain()

static matchCookieDomain (   $cookieDomain,
  $host 
)
static

Check if a cookie's domain matches a host name.

Used by Zend_Http_Cookie and Zend_Http_CookieJar for cookie matching

Parameters
string$cookieDomain
string$host
Returns
boolean

Definition at line 377 of file Cookie.php.

378  {
379  if (! $cookieDomain) {
380  #require_once 'Zend/Http/Exception.php';
381  throw new Zend_Http_Exception("\$cookieDomain is expected to be a cookie domain");
382  }
383 
384  if (! $host) {
385  #require_once 'Zend/Http/Exception.php';
386  throw new Zend_Http_Exception("\$host is expected to be a host name");
387  }
388 
389  $cookieDomain = strtolower($cookieDomain);
390  $host = strtolower($host);
391 
392  if ($cookieDomain[0] == '.') {
393  $cookieDomain = substr($cookieDomain, 1);
394  }
395 
396  // Check for either exact match or suffix match
397  return ($cookieDomain == $host ||
398  preg_match('/\.' . preg_quote($cookieDomain) . '$/', $host));
399  }

◆ matchCookiePath()

static matchCookiePath (   $cookiePath,
  $path 
)
static

Check if a cookie's path matches a URL path

Used by Zend_Http_Cookie and Zend_Http_CookieJar for cookie matching

Parameters
string$cookiePath
string$path
Returns
boolean

Definition at line 410 of file Cookie.php.

411  {
412  if (! $cookiePath) {
413  #require_once 'Zend/Http/Exception.php';
414  throw new Zend_Http_Exception("\$cookiePath is expected to be a cookie path");
415  }
416 
417  if (! $path) {
418  #require_once 'Zend/Http/Exception.php';
419  throw new Zend_Http_Exception("\$path is expected to be a host name");
420  }
421 
422  return (strpos($path, $cookiePath) === 0);
423  }

Field Documentation

◆ $domain

$domain
protected

Definition at line 75 of file Cookie.php.

◆ $encodeValue

$encodeValue
protected

Definition at line 96 of file Cookie.php.

◆ $expires

$expires
protected

Definition at line 68 of file Cookie.php.

◆ $name

$name
protected

Definition at line 54 of file Cookie.php.

◆ $path

$path
protected

Definition at line 82 of file Cookie.php.

◆ $secure

$secure
protected

Definition at line 89 of file Cookie.php.

◆ $value

$value
protected

Definition at line 61 of file Cookie.php.


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