|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* THttpRequest, THttpCookie, THttpCookieCollection, TUri class file |
|
4
|
|
|
* |
|
5
|
|
|
* @author Qiang Xue <[email protected]> |
|
6
|
|
|
* @link https://github.com/pradosoft/prado |
|
7
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
|
8
|
|
|
* @package Prado\Web |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Prado\Web; |
|
12
|
|
|
|
|
13
|
|
|
use Prado\TPropertyValue; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* THttpCookie class. |
|
17
|
|
|
* |
|
18
|
|
|
* A THttpCookie instance stores a single cookie, including the cookie name, value, |
|
19
|
|
|
* domain, path, expire, and secure. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Qiang Xue <[email protected]> |
|
22
|
|
|
* @package Prado\Web |
|
23
|
|
|
* @since 3.0 |
|
24
|
|
|
*/ |
|
25
|
|
|
class THttpCookie extends \Prado\TComponent |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var string domain of the cookie |
|
29
|
|
|
*/ |
|
30
|
|
|
private $_domain = ''; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var string name of the cookie |
|
33
|
|
|
*/ |
|
34
|
|
|
private $_name; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var string value of the cookie |
|
37
|
|
|
*/ |
|
38
|
|
|
private $_value = ''; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var int expire of the cookie |
|
41
|
|
|
*/ |
|
42
|
|
|
private $_expire = 0; |
|
43
|
|
|
/** |
|
44
|
|
|
* @var string path of the cookie |
|
45
|
|
|
*/ |
|
46
|
|
|
private $_path = '/'; |
|
47
|
|
|
/** |
|
48
|
|
|
* @var bool whether cookie should be sent via secure connection |
|
49
|
|
|
*/ |
|
50
|
|
|
private $_secure = false; |
|
51
|
|
|
/** |
|
52
|
|
|
* @var bool if true the cookie value will be unavailable to JavaScript |
|
53
|
|
|
*/ |
|
54
|
|
|
private $_httpOnly = false; |
|
55
|
|
|
/** |
|
56
|
|
|
* @var THttpCookieSameSite SameSite prevents the browser from sending this cookie on cross-site requests. |
|
57
|
|
|
* @since 4.1.2 |
|
58
|
|
|
*/ |
|
59
|
|
|
private $_sameSite = THttpCookieSameSite::Lax; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Constructor. |
|
63
|
|
|
* @param string $name name of this cookie |
|
64
|
|
|
* @param string $value value of this cookie |
|
65
|
|
|
*/ |
|
66
|
|
|
public function __construct($name, $value) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->_name = $name; |
|
69
|
|
|
$this->_value = $value; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return string the domain to associate the cookie with |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getDomain() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->_domain; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $value the domain to associate the cookie with |
|
82
|
|
|
*/ |
|
83
|
|
|
public function setDomain($value) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->_domain = $value; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return int the time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getExpire() |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->_expire; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param int $value the time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. |
|
98
|
|
|
*/ |
|
99
|
|
|
public function setExpire($value) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->_expire = TPropertyValue::ensureInteger($value); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return bool if true the cookie value will be unavailable to JavaScript |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getHttpOnly() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->_httpOnly; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param bool $value if true the cookie value will be unavailable to JavaScript |
|
114
|
|
|
*/ |
|
115
|
|
|
public function setHttpOnly($value) |
|
116
|
|
|
{ |
|
117
|
|
|
$this->_httpOnly = TPropertyValue::ensureBoolean($value); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return string the name of the cookie |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getName() |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->_name; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param string $value the name of the cookie |
|
130
|
|
|
*/ |
|
131
|
|
|
public function setName($value) |
|
132
|
|
|
{ |
|
133
|
|
|
$this->_name = $value; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return string the value of the cookie |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getValue() |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->_value; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param string $value the value of the cookie |
|
146
|
|
|
*/ |
|
147
|
|
|
public function setValue($value) |
|
148
|
|
|
{ |
|
149
|
|
|
$this->_value = $value; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @return string the path on the server in which the cookie will be available on, default is '/' |
|
154
|
|
|
*/ |
|
155
|
|
|
public function getPath() |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->_path; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param string $value the path on the server in which the cookie will be available on |
|
162
|
|
|
*/ |
|
163
|
|
|
public function setPath($value) |
|
164
|
|
|
{ |
|
165
|
|
|
$this->_path = $value; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @return bool whether the cookie should only be transmitted over a secure HTTPS connection |
|
170
|
|
|
*/ |
|
171
|
|
|
public function getSecure() |
|
172
|
|
|
{ |
|
173
|
|
|
return $this->_secure; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @param bool $value ether the cookie should only be transmitted over a secure HTTPS connection |
|
178
|
|
|
*/ |
|
179
|
|
|
public function setSecure($value) |
|
180
|
|
|
{ |
|
181
|
|
|
$this->_secure = TPropertyValue::ensureBoolean($value); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @return THttpCookieSameSite SameSite policy for this cookie. Defaults to THttpCookieSameSite::None. |
|
186
|
|
|
*/ |
|
187
|
|
|
public function getSameSite() |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->_sameSite; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @param THttpCookieSameSite $value SameSite policy for this cookie |
|
194
|
|
|
*/ |
|
195
|
|
|
public function setSameSite($value) |
|
196
|
|
|
{ |
|
197
|
|
|
$this->_sameSite = TPropertyValue::ensureEnum($value, '\Prado\Web\THttpCookieSameSite'); |
|
|
|
|
|
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @param mixed $expiresKey |
|
202
|
|
|
* @return array cookie options as used in php's setcookie() and session_set_cookie_params(). |
|
203
|
|
|
* The 'expires' key can be customized since setcookie() uses 'expires' and |
|
204
|
|
|
* session_set_cookie_params() uses 'lifetime'. |
|
205
|
|
|
*/ |
|
206
|
|
|
public function getPhpOptions($expiresKey = 'expires') |
|
207
|
|
|
{ |
|
208
|
|
|
return [ |
|
209
|
|
|
$expiresKey => $this->_expire, |
|
210
|
|
|
'path' => $this->_path, |
|
211
|
|
|
'domain' => $this->_domain, |
|
212
|
|
|
'secure' => $this->_secure, |
|
213
|
|
|
'httponly' => $this->_httpOnly, |
|
214
|
|
|
'samesite' => $this->_sameSite, |
|
215
|
|
|
]; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..