|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
4
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
5
|
|
|
|
|
6
|
|
|
function handleRequest(ServerRequestInterface $req, ResponseInterface $resp): ResponseInterface |
|
|
|
|
|
|
7
|
|
|
{ |
|
8
|
|
|
$resp->getBody()->write(strtoupper($req->getCookieParams()['input'])); |
|
9
|
|
|
|
|
10
|
|
|
return $resp->withAddedHeader( |
|
11
|
|
|
"Set-Cookie", |
|
12
|
|
|
(new Cookie('output', 'cookie-output'))->createHeader() |
|
13
|
|
|
); |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
final class Cookie |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* The name of the cookie. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $name = ''; |
|
24
|
|
|
/** |
|
25
|
|
|
* The value of the cookie. This value is stored on the clients computer; do not store sensitive |
|
26
|
|
|
* information. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string|null |
|
29
|
|
|
*/ |
|
30
|
|
|
private $value = null; |
|
31
|
|
|
/** |
|
32
|
|
|
* Cookie lifetime. This value specified in seconds and declares period of time in which cookie |
|
33
|
|
|
* will expire relatively to current time() value. |
|
34
|
|
|
* |
|
35
|
|
|
* @var int|null |
|
36
|
|
|
*/ |
|
37
|
|
|
private $lifetime = null; |
|
38
|
|
|
/** |
|
39
|
|
|
* The path on the server in which the cookie will be available on. |
|
40
|
|
|
* |
|
41
|
|
|
* If set to '/', the cookie will be available within the entire domain. If set to '/foo/', |
|
42
|
|
|
* the cookie will only be available within the /foo/ directory and all sub-directories such as |
|
43
|
|
|
* /foo/bar/ of domain. The default value is the current directory that the cookie is being set |
|
44
|
|
|
* in. |
|
45
|
|
|
* |
|
46
|
|
|
* @var string|null |
|
47
|
|
|
*/ |
|
48
|
|
|
private $path = null; |
|
49
|
|
|
/** |
|
50
|
|
|
* The domain that the cookie is available. To make the cookie available on all subdomains of |
|
51
|
|
|
* example.com then you'd set it to '.example.com'. The . is not required but makes it |
|
52
|
|
|
* compatible with more browsers. Setting it to www.example.com will make the cookie only |
|
53
|
|
|
* available in the www subdomain. Refer to tail matching in the spec for details. |
|
54
|
|
|
* |
|
55
|
|
|
* @var string|null |
|
56
|
|
|
*/ |
|
57
|
|
|
private $domain = null; |
|
58
|
|
|
/** |
|
59
|
|
|
* Indicates that the cookie should only be transmitted over a secure HTTPS connection from the |
|
60
|
|
|
* client. When set to true, the cookie will only be set if a secure connection exists. |
|
61
|
|
|
* On the server-side, it's on the programmer to send this kind of cookie only on secure |
|
62
|
|
|
* connection |
|
63
|
|
|
* (e.g. with respect to $_SERVER["HTTPS"]). |
|
64
|
|
|
* |
|
65
|
|
|
* @var bool|null |
|
66
|
|
|
*/ |
|
67
|
|
|
private $secure = null; |
|
68
|
|
|
/** |
|
69
|
|
|
* When true the cookie will be made accessible only through the HTTP protocol. This means that |
|
70
|
|
|
* the cookie won't be accessible by scripting languages, such as JavaScript. This setting can |
|
71
|
|
|
* effectively help to reduce identity theft through XSS attacks (although it is not supported |
|
72
|
|
|
* by all browsers). |
|
73
|
|
|
* |
|
74
|
|
|
* @var bool |
|
75
|
|
|
*/ |
|
76
|
|
|
private $httpOnly = true; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* New Cookie instance, cookies used to schedule cookie set while dispatching Response. |
|
80
|
|
|
* |
|
81
|
|
|
* @link http://php.net/manual/en/function.setcookie.php |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $name The name of the cookie. |
|
84
|
|
|
* @param string $value The value of the cookie. This value is stored on the clients |
|
85
|
|
|
* computer; do not store sensitive information. |
|
86
|
|
|
* @param int $lifetime Cookie lifetime. This value specified in seconds and declares period |
|
87
|
|
|
* of time in which cookie will expire relatively to current time() |
|
88
|
|
|
* value. |
|
89
|
|
|
* @param string $path The path on the server in which the cookie will be available on. |
|
90
|
|
|
* If set to '/', the cookie will be available within the entire |
|
91
|
|
|
* domain. |
|
92
|
|
|
* If set to '/foo/', the cookie will only be available within the |
|
93
|
|
|
* /foo/ |
|
94
|
|
|
* directory and all sub-directories such as /foo/bar/ of domain. The |
|
95
|
|
|
* default value is the current directory that the cookie is being set |
|
96
|
|
|
* in. |
|
97
|
|
|
* @param string $domain The domain that the cookie is available. To make the cookie |
|
98
|
|
|
* available |
|
99
|
|
|
* on all subdomains of example.com then you'd set it to |
|
100
|
|
|
* '.example.com'. |
|
101
|
|
|
* The . is not required but makes it compatible with more browsers. |
|
102
|
|
|
* Setting it to www.example.com will make the cookie only available in |
|
103
|
|
|
* the www subdomain. Refer to tail matching in the spec for details. |
|
104
|
|
|
* @param bool $secure Indicates that the cookie should only be transmitted over a secure |
|
105
|
|
|
* HTTPS connection from the client. When set to true, the cookie will |
|
106
|
|
|
* only be set if a secure connection exists. On the server-side, it's |
|
107
|
|
|
* on the programmer to send this kind of cookie only on secure |
|
108
|
|
|
* connection (e.g. with respect to $_SERVER["HTTPS"]). |
|
109
|
|
|
* @param bool $httpOnly When true the cookie will be made accessible only through the HTTP |
|
110
|
|
|
* protocol. This means that the cookie won't be accessible by |
|
111
|
|
|
* scripting |
|
112
|
|
|
* languages, such as JavaScript. This setting can effectively help to |
|
113
|
|
|
* reduce identity theft through XSS attacks (although it is not |
|
114
|
|
|
* supported by all browsers). |
|
115
|
|
|
*/ |
|
116
|
|
|
public function __construct( |
|
117
|
|
|
string $name, |
|
118
|
|
|
string $value = null, |
|
119
|
|
|
int $lifetime = null, |
|
120
|
|
|
string $path = null, |
|
121
|
|
|
string $domain = null, |
|
122
|
|
|
bool $secure = false, |
|
123
|
|
|
bool $httpOnly = true |
|
124
|
|
|
) { |
|
125
|
|
|
$this->name = $name; |
|
126
|
|
|
$this->value = $value; |
|
127
|
|
|
$this->lifetime = $lifetime; |
|
128
|
|
|
$this->path = $path; |
|
129
|
|
|
$this->domain = $domain; |
|
130
|
|
|
$this->secure = $secure; |
|
131
|
|
|
$this->httpOnly = $httpOnly; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* The name of the cookie. |
|
136
|
|
|
* |
|
137
|
|
|
* @return string |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getName(): string |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->name; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* The value of the cookie. This value is stored on the clients computer; do not store sensitive |
|
146
|
|
|
* information. |
|
147
|
|
|
* |
|
148
|
|
|
* @return string|null |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getValue() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->value; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* The time the cookie expires. This is a Unix timestamp so is in number of seconds since the |
|
157
|
|
|
* epoch. In other words, you'll most likely set this with the time function plus the number of |
|
158
|
|
|
* seconds before you want it to expire. Or you might use mktime. |
|
159
|
|
|
* |
|
160
|
|
|
* Will return null if lifetime is not specified. |
|
161
|
|
|
* |
|
162
|
|
|
* @return int|null |
|
163
|
|
|
*/ |
|
164
|
|
|
public function getExpires() |
|
165
|
|
|
{ |
|
166
|
|
|
if ($this->lifetime === null) { |
|
167
|
|
|
return null; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return time() + $this->lifetime; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* The path on the server in which the cookie will be available on. |
|
175
|
|
|
* |
|
176
|
|
|
* If set to '/', the cookie will be available within the entire domain. If set to '/foo/', |
|
177
|
|
|
* the cookie will only be available within the /foo/ directory and all sub-directories such as |
|
178
|
|
|
* /foo/bar/ of domain. The default value is the current directory that the cookie is being set |
|
179
|
|
|
* in. |
|
180
|
|
|
* |
|
181
|
|
|
* @return string|null |
|
182
|
|
|
*/ |
|
183
|
|
|
public function getPath() |
|
184
|
|
|
{ |
|
185
|
|
|
return $this->path; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* The domain that the cookie is available. To make the cookie available on all subdomains of |
|
190
|
|
|
* example.com then you'd set it to '.example.com'. The . is not required but makes it |
|
191
|
|
|
* compatible with more browsers. Setting it to www.example.com will make the cookie only |
|
192
|
|
|
* available in the www subdomain. Refer to tail matching in the spec for details. |
|
193
|
|
|
* |
|
194
|
|
|
* @return string|null |
|
195
|
|
|
*/ |
|
196
|
|
|
public function getDomain() |
|
197
|
|
|
{ |
|
198
|
|
|
return $this->domain; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Indicates that the cookie should only be transmitted over a secure HTTPS connection from the |
|
203
|
|
|
* client. When set to true, the cookie will only be set if a secure connection exists. |
|
204
|
|
|
* On the server-side, it's on the programmer to send this kind of cookie only on secure |
|
205
|
|
|
* connection |
|
206
|
|
|
* (e.g. with respect to $_SERVER["HTTPS"]). |
|
207
|
|
|
* |
|
208
|
|
|
* @return bool |
|
209
|
|
|
*/ |
|
210
|
|
|
public function isSecure(): bool |
|
211
|
|
|
{ |
|
212
|
|
|
return $this->secure; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* When true the cookie will be made accessible only through the HTTP protocol. This means that |
|
217
|
|
|
* the cookie won't be accessible by scripting languages, such as JavaScript. This setting can |
|
218
|
|
|
* effectively help to reduce identity theft through XSS attacks (although it is not supported |
|
219
|
|
|
* by all browsers). |
|
220
|
|
|
* |
|
221
|
|
|
* @return bool |
|
222
|
|
|
*/ |
|
223
|
|
|
public function isHttpOnly(): bool |
|
224
|
|
|
{ |
|
225
|
|
|
return $this->httpOnly; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Get new cookie with altered value. Original cookie object should not be changed. |
|
230
|
|
|
* |
|
231
|
|
|
* @param string $value |
|
232
|
|
|
* |
|
233
|
|
|
* @return Cookie |
|
234
|
|
|
*/ |
|
235
|
|
|
public function withValue(string $value): self |
|
236
|
|
|
{ |
|
237
|
|
|
$cookie = clone $this; |
|
238
|
|
|
$cookie->value = $value; |
|
239
|
|
|
|
|
240
|
|
|
return $cookie; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Convert cookie instance to string. |
|
245
|
|
|
* |
|
246
|
|
|
* @link http://www.w3.org/Protocols/rfc2109/rfc2109 |
|
247
|
|
|
* @return string |
|
248
|
|
|
*/ |
|
249
|
|
|
public function createHeader(): string |
|
250
|
|
|
{ |
|
251
|
|
|
$header = [ |
|
252
|
|
|
rawurlencode($this->name) . '=' . rawurlencode($this->value) |
|
253
|
|
|
]; |
|
254
|
|
|
if ($this->lifetime !== null) { |
|
255
|
|
|
$header[] = 'Expires=' . gmdate(\DateTime::COOKIE, $this->getExpires()); |
|
256
|
|
|
$header[] = 'Max-Age=' . $this->lifetime; |
|
257
|
|
|
} |
|
258
|
|
|
if (!empty($this->path)) { |
|
259
|
|
|
$header[] = 'Path=' . $this->path; |
|
260
|
|
|
} |
|
261
|
|
|
if (!empty($this->domain)) { |
|
262
|
|
|
$header[] = 'Domain=' . $this->domain; |
|
263
|
|
|
} |
|
264
|
|
|
if ($this->secure) { |
|
265
|
|
|
$header[] = 'Secure'; |
|
266
|
|
|
} |
|
267
|
|
|
if ($this->httpOnly) { |
|
268
|
|
|
$header[] = 'HttpOnly'; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
return join('; ', $header); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* New Cookie instance, cookies used to schedule cookie set while dispatching Response. |
|
276
|
|
|
* Static constructor. |
|
277
|
|
|
* |
|
278
|
|
|
* @link http://php.net/manual/en/function.setcookie.php |
|
279
|
|
|
* |
|
280
|
|
|
* @param string $name The name of the cookie. |
|
281
|
|
|
* @param string $value The value of the cookie. This value is stored on the clients |
|
282
|
|
|
* computer; do not store sensitive information. |
|
283
|
|
|
* @param int $lifetime Cookie lifetime. This value specified in seconds and declares period |
|
284
|
|
|
* of time in which cookie will expire relatively to current time() |
|
285
|
|
|
* value. |
|
286
|
|
|
* @param string $path The path on the server in which the cookie will be available on. |
|
287
|
|
|
* If set to '/', the cookie will be available within the entire |
|
288
|
|
|
* domain. |
|
289
|
|
|
* If set to '/foo/', the cookie will only be available within the |
|
290
|
|
|
* /foo/ |
|
291
|
|
|
* directory and all sub-directories such as /foo/bar/ of domain. The |
|
292
|
|
|
* default value is the current directory that the cookie is being set |
|
293
|
|
|
* in. |
|
294
|
|
|
* @param string $domain The domain that the cookie is available. To make the cookie |
|
295
|
|
|
* available |
|
296
|
|
|
* on all subdomains of example.com then you'd set it to |
|
297
|
|
|
* '.example.com'. |
|
298
|
|
|
* The . is not required but makes it compatible with more browsers. |
|
299
|
|
|
* Setting it to www.example.com will make the cookie only available in |
|
300
|
|
|
* the www subdomain. Refer to tail matching in the spec for details. |
|
301
|
|
|
* @param bool $secure Indicates that the cookie should only be transmitted over a secure |
|
302
|
|
|
* HTTPS connection from the client. When set to true, the cookie will |
|
303
|
|
|
* only be set if a secure connection exists. On the server-side, it's |
|
304
|
|
|
* on the programmer to send this kind of cookie only on secure |
|
305
|
|
|
* connection (e.g. with respect to $_SERVER["HTTPS"]). |
|
306
|
|
|
* @param bool $httpOnly When true the cookie will be made accessible only through the HTTP |
|
307
|
|
|
* protocol. This means that the cookie won't be accessible by |
|
308
|
|
|
* scripting |
|
309
|
|
|
* languages, such as JavaScript. This setting can effectively help to |
|
310
|
|
|
* reduce identity theft through XSS attacks (although it is not |
|
311
|
|
|
* supported by all browsers). |
|
312
|
|
|
* |
|
313
|
|
|
* @return Cookie |
|
314
|
|
|
*/ |
|
315
|
|
|
public static function create( |
|
316
|
|
|
string $name, |
|
317
|
|
|
string $value = null, |
|
318
|
|
|
int $lifetime = null, |
|
319
|
|
|
string $path = null, |
|
320
|
|
|
string $domain = null, |
|
321
|
|
|
bool $secure = false, |
|
322
|
|
|
bool $httpOnly = true |
|
323
|
|
|
): self { |
|
324
|
|
|
return new self($name, $value, $lifetime, $path, $domain, $secure, $httpOnly); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* @return string |
|
329
|
|
|
*/ |
|
330
|
|
|
public function __toString(): string |
|
331
|
|
|
{ |
|
332
|
|
|
return $this->createHeader(); |
|
333
|
|
|
} |
|
334
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.