2011-01-15 17:53:30 +00:00
|
|
|
<?php
|
|
|
|
|
2011-01-29 14:45:10 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-29 14:45:10 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-01-15 17:53:30 +00:00
|
|
|
namespace Symfony\Component\HttpFoundation;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a cookie
|
|
|
|
*
|
|
|
|
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-01-15 17:53:30 +00:00
|
|
|
*/
|
|
|
|
class Cookie
|
|
|
|
{
|
|
|
|
protected $name;
|
|
|
|
protected $value;
|
|
|
|
protected $domain;
|
|
|
|
protected $expire;
|
|
|
|
protected $path;
|
|
|
|
protected $secure;
|
2011-03-03 13:49:34 +00:00
|
|
|
protected $httpOnly;
|
2011-01-15 17:53:30 +00:00
|
|
|
|
2011-06-10 07:59:49 +01:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param string $name The name of the cookie
|
|
|
|
* @param string $value The value of the cookie
|
2014-04-16 07:51:57 +01:00
|
|
|
* @param int|string|\DateTime $expire The time the cookie expires
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param string $path The path on the server in which the cookie will be available on
|
|
|
|
* @param string $domain The domain that the cookie is available to
|
2014-04-12 18:54:57 +01:00
|
|
|
* @param bool $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client
|
|
|
|
* @param bool $httpOnly Whether the cookie will be made accessible only through the HTTP protocol
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
2012-12-16 12:02:54 +00:00
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*
|
2011-07-20 09:06:02 +01:00
|
|
|
* @api
|
2011-06-10 07:59:49 +01:00
|
|
|
*/
|
2011-05-04 07:44:59 +01:00
|
|
|
public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true)
|
2011-01-15 17:53:30 +00:00
|
|
|
{
|
|
|
|
// from PHP source code
|
|
|
|
if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($name)) {
|
2011-06-10 07:59:49 +01:00
|
|
|
throw new \InvalidArgumentException('The cookie name cannot be empty.');
|
2011-01-15 17:53:30 +00:00
|
|
|
}
|
2011-03-06 09:39:33 +00:00
|
|
|
|
2011-04-13 19:12:14 +01:00
|
|
|
// convert expiration time to a Unix timestamp
|
|
|
|
if ($expire instanceof \DateTime) {
|
|
|
|
$expire = $expire->format('U');
|
|
|
|
} elseif (!is_numeric($expire)) {
|
|
|
|
$expire = strtotime($expire);
|
|
|
|
|
|
|
|
if (false === $expire || -1 === $expire) {
|
|
|
|
throw new \InvalidArgumentException('The cookie expiration time is not valid.');
|
|
|
|
}
|
2011-03-05 23:13:47 +00:00
|
|
|
}
|
2011-01-15 17:53:30 +00:00
|
|
|
|
|
|
|
$this->name = $name;
|
|
|
|
$this->value = $value;
|
|
|
|
$this->domain = $domain;
|
2011-03-05 23:13:47 +00:00
|
|
|
$this->expire = $expire;
|
2011-09-28 09:49:50 +01:00
|
|
|
$this->path = empty($path) ? '/' : $path;
|
2014-04-12 18:44:00 +01:00
|
|
|
$this->secure = (bool) $secure;
|
|
|
|
$this->httpOnly = (bool) $httpOnly;
|
2011-01-15 17:53:30 +00:00
|
|
|
}
|
2011-05-19 16:38:16 +01:00
|
|
|
|
2012-06-25 17:21:41 +01:00
|
|
|
/**
|
|
|
|
* Returns the cookie as a string.
|
|
|
|
*
|
|
|
|
* @return string The cookie
|
|
|
|
*/
|
2011-05-19 08:48:58 +01:00
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
$str = urlencode($this->getName()).'=';
|
|
|
|
|
2011-05-19 16:12:16 +01:00
|
|
|
if ('' === (string) $this->getValue()) {
|
2011-05-19 16:38:16 +01:00
|
|
|
$str .= 'deleted; expires='.gmdate("D, d-M-Y H:i:s T", time() - 31536001);
|
2011-05-19 08:48:58 +01:00
|
|
|
} else {
|
|
|
|
$str .= urlencode($this->getValue());
|
|
|
|
|
2011-05-19 15:06:59 +01:00
|
|
|
if ($this->getExpiresTime() !== 0) {
|
2011-05-19 08:48:58 +01:00
|
|
|
$str .= '; expires='.gmdate("D, d-M-Y H:i:s T", $this->getExpiresTime());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-20 19:04:59 +01:00
|
|
|
if ($this->path) {
|
2011-09-28 09:49:50 +01:00
|
|
|
$str .= '; path='.$this->path;
|
2011-05-19 08:48:58 +01:00
|
|
|
}
|
|
|
|
|
2013-04-20 18:50:53 +01:00
|
|
|
if ($this->getDomain()) {
|
2011-05-19 08:48:58 +01:00
|
|
|
$str .= '; domain='.$this->getDomain();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (true === $this->isSecure()) {
|
|
|
|
$str .= '; secure';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (true === $this->isHttpOnly()) {
|
|
|
|
$str .= '; httponly';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $str;
|
|
|
|
}
|
2011-01-15 17:53:30 +00:00
|
|
|
|
2011-06-10 07:59:49 +01:00
|
|
|
/**
|
|
|
|
* Gets the name of the cookie.
|
|
|
|
*
|
|
|
|
* @return string
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-06-10 07:59:49 +01:00
|
|
|
*/
|
2011-01-15 17:53:30 +00:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
2011-06-10 07:59:49 +01:00
|
|
|
/**
|
|
|
|
* Gets the value of the cookie.
|
|
|
|
*
|
|
|
|
* @return string
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-06-10 07:59:49 +01:00
|
|
|
*/
|
2011-01-15 17:53:30 +00:00
|
|
|
public function getValue()
|
|
|
|
{
|
|
|
|
return $this->value;
|
|
|
|
}
|
|
|
|
|
2011-06-10 07:59:49 +01:00
|
|
|
/**
|
|
|
|
* Gets the domain that the cookie is available to.
|
|
|
|
*
|
|
|
|
* @return string
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-06-10 07:59:49 +01:00
|
|
|
*/
|
2011-01-15 17:53:30 +00:00
|
|
|
public function getDomain()
|
|
|
|
{
|
|
|
|
return $this->domain;
|
|
|
|
}
|
|
|
|
|
2011-06-10 07:59:49 +01:00
|
|
|
/**
|
|
|
|
* Gets the time the cookie expires.
|
|
|
|
*
|
2014-04-16 11:30:19 +01:00
|
|
|
* @return int
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-06-10 07:59:49 +01:00
|
|
|
*/
|
2011-04-13 19:09:04 +01:00
|
|
|
public function getExpiresTime()
|
2011-01-15 17:53:30 +00:00
|
|
|
{
|
|
|
|
return $this->expire;
|
|
|
|
}
|
|
|
|
|
2011-06-10 07:59:49 +01:00
|
|
|
/**
|
|
|
|
* Gets the path on the server in which the cookie will be available on.
|
|
|
|
*
|
|
|
|
* @return string
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-06-10 07:59:49 +01:00
|
|
|
*/
|
2011-01-15 17:53:30 +00:00
|
|
|
public function getPath()
|
|
|
|
{
|
2011-09-28 09:49:50 +01:00
|
|
|
return $this->path;
|
2011-01-15 17:53:30 +00:00
|
|
|
}
|
|
|
|
|
2011-06-10 07:59:49 +01:00
|
|
|
/**
|
|
|
|
* Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client.
|
|
|
|
*
|
2014-04-16 11:30:19 +01:00
|
|
|
* @return bool
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-06-10 07:59:49 +01:00
|
|
|
*/
|
2011-01-15 17:53:30 +00:00
|
|
|
public function isSecure()
|
|
|
|
{
|
|
|
|
return $this->secure;
|
|
|
|
}
|
|
|
|
|
2011-06-10 07:59:49 +01:00
|
|
|
/**
|
|
|
|
* Checks whether the cookie will be made accessible only through the HTTP protocol.
|
|
|
|
*
|
2014-04-16 11:30:19 +01:00
|
|
|
* @return bool
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-06-10 07:59:49 +01:00
|
|
|
*/
|
2011-03-03 13:49:34 +00:00
|
|
|
public function isHttpOnly()
|
2011-01-15 17:53:30 +00:00
|
|
|
{
|
2011-03-03 13:49:34 +00:00
|
|
|
return $this->httpOnly;
|
2011-01-15 17:53:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this cookie is about to be cleared
|
|
|
|
*
|
2014-04-16 11:30:19 +01:00
|
|
|
* @return bool
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-01-15 17:53:30 +00:00
|
|
|
*/
|
|
|
|
public function isCleared()
|
|
|
|
{
|
|
|
|
return $this->expire < time();
|
|
|
|
}
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|