|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\HttpFoundation; |
|
|
13 |
|
|
|
14 |
/** |
|
|
15 |
* Represents a cookie |
|
|
16 |
* |
|
|
17 |
* @author Johannes M. Schmitt <schmittjoh@gmail.com> |
|
|
18 |
* |
|
|
19 |
* @api |
|
|
20 |
*/ |
|
|
21 |
class Cookie |
|
|
22 |
{ |
|
|
23 |
protected $name; |
|
|
24 |
protected $value; |
|
|
25 |
protected $domain; |
|
|
26 |
protected $expire; |
|
|
27 |
protected $path; |
|
|
28 |
protected $secure; |
|
|
29 |
protected $httpOnly; |
|
|
30 |
|
|
|
31 |
/** |
|
|
32 |
* Constructor. |
|
|
33 |
* |
|
|
34 |
* @param string $name The name of the cookie |
|
|
35 |
* @param string $value The value of the cookie |
|
|
36 |
* @param integer|string|\DateTime $expire The time the cookie expires |
|
|
37 |
* @param string $path The path on the server in which the cookie will be available on |
|
|
38 |
* @param string $domain The domain that the cookie is available to |
|
|
39 |
* @param Boolean $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client |
|
|
40 |
* @param Boolean $httpOnly Whether the cookie will be made accessible only through the HTTP protocol |
|
|
41 |
* |
|
|
42 |
* @api |
|
|
43 |
*/ |
|
|
44 |
public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true) |
|
|
45 |
{ |
|
|
46 |
// from PHP source code |
|
|
47 |
if (preg_match("/[=,; \t\r\n\013\014]/", $name)) { |
|
|
48 |
throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name)); |
|
|
49 |
} |
|
|
50 |
|
|
|
51 |
if (preg_match("/[,; \t\r\n\013\014]/", $value)) { |
|
|
52 |
throw new \InvalidArgumentException(sprintf('The cookie value "%s" contains invalid characters.', $value)); |
|
|
53 |
} |
|
|
54 |
|
|
|
55 |
if (empty($name)) { |
|
|
56 |
throw new \InvalidArgumentException('The cookie name cannot be empty.'); |
|
|
57 |
} |
|
|
58 |
|
|
|
59 |
// convert expiration time to a Unix timestamp |
|
|
60 |
if ($expire instanceof \DateTime) { |
|
|
61 |
$expire = $expire->format('U'); |
|
|
62 |
} elseif (!is_numeric($expire)) { |
|
|
63 |
$expire = strtotime($expire); |
|
|
64 |
|
|
|
65 |
if (false === $expire || -1 === $expire) { |
|
|
66 |
throw new \InvalidArgumentException('The cookie expiration time is not valid.'); |
|
|
67 |
} |
|
|
68 |
} |
|
|
69 |
|
|
|
70 |
$this->name = $name; |
|
|
71 |
$this->value = $value; |
|
|
72 |
$this->domain = $domain; |
|
|
73 |
$this->expire = $expire; |
|
|
74 |
$this->path = $path; |
|
|
75 |
$this->secure = (Boolean) $secure; |
|
|
76 |
$this->httpOnly = (Boolean) $httpOnly; |
|
|
77 |
} |
|
|
78 |
|
|
|
79 |
public function __toString() |
|
|
80 |
{ |
|
|
81 |
$str = urlencode($this->getName()).'='; |
|
|
82 |
|
|
|
83 |
if ('' === (string) $this->getValue()) { |
|
|
84 |
$str .= 'deleted; expires='.gmdate("D, d-M-Y H:i:s T", time() - 31536001); |
|
|
85 |
} else { |
|
|
86 |
$str .= urlencode($this->getValue()); |
|
|
87 |
|
|
|
88 |
if ($this->getExpiresTime() !== 0) { |
|
|
89 |
$str .= '; expires='.gmdate("D, d-M-Y H:i:s T", $this->getExpiresTime()); |
|
|
90 |
} |
|
|
91 |
} |
|
|
92 |
|
|
|
93 |
if (null !== $this->getPath()) { |
|
|
94 |
$str .= '; path='.$this->getPath(); |
|
|
95 |
} |
|
|
96 |
|
|
|
97 |
if (null !== $this->getDomain()) { |
|
|
98 |
$str .= '; domain='.$this->getDomain(); |
|
|
99 |
} |
|
|
100 |
|
|
|
101 |
if (true === $this->isSecure()) { |
|
|
102 |
$str .= '; secure'; |
|
|
103 |
} |
|
|
104 |
|
|
|
105 |
if (true === $this->isHttpOnly()) { |
|
|
106 |
$str .= '; httponly'; |
|
|
107 |
} |
|
|
108 |
|
|
|
109 |
return $str; |
|
|
110 |
} |
|
|
111 |
|
|
|
112 |
/** |
|
|
113 |
* Gets the name of the cookie. |
|
|
114 |
* |
|
|
115 |
* @return string |
|
|
116 |
* |
|
|
117 |
* @api |
|
|
118 |
*/ |
|
|
119 |
public function getName() |
|
|
120 |
{ |
|
|
121 |
return $this->name; |
|
|
122 |
} |
|
|
123 |
|
|
|
124 |
/** |
|
|
125 |
* Gets the value of the cookie. |
|
|
126 |
* |
|
|
127 |
* @return string |
|
|
128 |
* |
|
|
129 |
* @api |
|
|
130 |
*/ |
|
|
131 |
public function getValue() |
|
|
132 |
{ |
|
|
133 |
return $this->value; |
|
|
134 |
} |
|
|
135 |
|
|
|
136 |
/** |
|
|
137 |
* Gets the domain that the cookie is available to. |
|
|
138 |
* |
|
|
139 |
* @return string |
|
|
140 |
* |
|
|
141 |
* @api |
|
|
142 |
*/ |
|
|
143 |
public function getDomain() |
|
|
144 |
{ |
|
|
145 |
return $this->domain; |
|
|
146 |
} |
|
|
147 |
|
|
|
148 |
/** |
|
|
149 |
* Gets the time the cookie expires. |
|
|
150 |
* |
|
|
151 |
* @return integer |
|
|
152 |
* |
|
|
153 |
* @api |
|
|
154 |
*/ |
|
|
155 |
public function getExpiresTime() |
|
|
156 |
{ |
|
|
157 |
return $this->expire; |
|
|
158 |
} |
|
|
159 |
|
|
|
160 |
/** |
|
|
161 |
* Gets the path on the server in which the cookie will be available on. |
|
|
162 |
* |
|
|
163 |
* @return string |
|
|
164 |
* |
|
|
165 |
* @api |
|
|
166 |
*/ |
|
|
167 |
public function getPath() |
|
|
168 |
{ |
|
|
169 |
return $this->path; |
|
|
170 |
} |
|
|
171 |
|
|
|
172 |
/** |
|
|
173 |
* Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client. |
|
|
174 |
* |
|
|
175 |
* @return Boolean |
|
|
176 |
* |
|
|
177 |
* @api |
|
|
178 |
*/ |
|
|
179 |
public function isSecure() |
|
|
180 |
{ |
|
|
181 |
return $this->secure; |
|
|
182 |
} |
|
|
183 |
|
|
|
184 |
/** |
|
|
185 |
* Checks whether the cookie will be made accessible only through the HTTP protocol. |
|
|
186 |
* |
|
|
187 |
* @return Boolean |
|
|
188 |
* |
|
|
189 |
* @api |
|
|
190 |
*/ |
|
|
191 |
public function isHttpOnly() |
|
|
192 |
{ |
|
|
193 |
return $this->httpOnly; |
|
|
194 |
} |
|
|
195 |
|
|
|
196 |
/** |
|
|
197 |
* Whether this cookie is about to be cleared |
|
|
198 |
* |
|
|
199 |
* @return Boolean |
|
|
200 |
* |
|
|
201 |
* @api |
|
|
202 |
*/ |
|
|
203 |
public function isCleared() |
|
|
204 |
{ |
|
|
205 |
return $this->expire < time(); |
|
|
206 |
} |
|
|
207 |
} |