Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Cookie.php
Go to the documentation of this file.
1 <?php
2 
27 #require_once 'Zend/Uri/Http.php';
28 
29 
48 {
54  protected $name;
55 
61  protected $value;
62 
68  protected $expires;
69 
75  protected $domain;
76 
82  protected $path;
83 
89  protected $secure;
90 
96  protected $encodeValue;
97 
110  public function __construct($name, $value, $domain, $expires = null, $path = null, $secure = false)
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  }
132 
138  public function getName()
139  {
140  return $this->name;
141  }
142 
148  public function getValue()
149  {
150  return $this->value;
151  }
152 
158  public function getDomain()
159  {
160  return $this->domain;
161  }
162 
168  public function getPath()
169  {
170  return $this->path;
171  }
172 
178  public function getExpiryTime()
179  {
180  return $this->expires;
181  }
182 
188  public function isSecure()
189  {
190  return $this->secure;
191  }
192 
201  public function isExpired($now = null)
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  }
210 
216  public function isSessionCookie()
217  {
218  return ($this->expires === null);
219  }
220 
229  public function match($uri, $matchSessionCookies = true, $now = null)
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  }
259 
266  public function __toString()
267  {
268  if ($this->encodeValue) {
269  return $this->name . '=' . urlencode($this->value) . ';';
270  }
271  return $this->name . '=' . $this->value . ';';
272  }
273 
284  public static function fromString($cookieStr, $refUri = null, $encodeValue = true)
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  }
366 
377  public static function matchCookieDomain($cookieDomain, $host)
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  }
400 
410  public static function matchCookiePath($cookiePath, $path)
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  }
424 }
$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