1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Spiral\Cookies; |
12
|
|
|
|
13
|
|
|
use Psr\Container\ContainerInterface; |
14
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
15
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
16
|
|
|
use Spiral\Core\Container\SingletonInterface; |
17
|
|
|
use Spiral\Core\Exception\ScopeException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Cookies manages provides the ability to write and read cookies from the active request/response scope. |
21
|
|
|
*/ |
22
|
|
|
final class CookieManager implements SingletonInterface |
23
|
|
|
{ |
24
|
|
|
/** @var ContainerInterface */ |
25
|
|
|
private $container; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ContainerInterface $container |
29
|
|
|
*/ |
30
|
|
|
public function __construct(ContainerInterface $container) |
31
|
|
|
{ |
32
|
|
|
$this->container = $container; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $name |
37
|
|
|
* @return bool |
38
|
|
|
* |
39
|
|
|
* @throws ScopeException |
40
|
|
|
*/ |
41
|
|
|
public function has(string $name): bool |
42
|
|
|
{ |
43
|
|
|
return array_key_exists($name, $this->getRequest()->getCookieParams()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $name |
48
|
|
|
* @param null $default |
|
|
|
|
49
|
|
|
* @return mixed |
50
|
|
|
* |
51
|
|
|
* @throws ScopeException |
52
|
|
|
*/ |
53
|
|
|
public function get(string $name, $default = null) |
54
|
|
|
{ |
55
|
|
|
return $this->getRequest()->getCookieParams()[$name] ?? $default; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get all cookies. |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function getAll(): array |
64
|
|
|
{ |
65
|
|
|
return $this->getRequest()->getCookieParams(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Schedule new cookie. Cookie will be send while dispatching request. |
70
|
|
|
* |
71
|
|
|
* Domain, path, and secure values can be left in null state, in this case cookie manager will |
72
|
|
|
* populate them automatically. |
73
|
|
|
* |
74
|
|
|
* @link http://php.net/manual/en/function.setcookie.php |
75
|
|
|
* |
76
|
|
|
* @param string $name The name of the cookie. |
77
|
|
|
* @param string $value The value of the cookie. This value is stored on the clients |
78
|
|
|
* computer; do not store sensitive information. |
79
|
|
|
* @param int $lifetime Cookie lifetime. This value specified in seconds and declares period |
80
|
|
|
* of time in which cookie will expire relatively to current time() |
81
|
|
|
* value. |
82
|
|
|
* @param string $path The path on the server in which the cookie will be available on. |
83
|
|
|
* If set to '/', the cookie will be available within the entire |
84
|
|
|
* domain. |
85
|
|
|
* If set to '/foo/', the cookie will only be available within the |
86
|
|
|
* /foo/ |
87
|
|
|
* directory and all sub-directories such as /foo/bar/ of domain. The |
88
|
|
|
* default value is the current directory that the cookie is being set |
89
|
|
|
* in. |
90
|
|
|
* @param string $domain The domain that the cookie is available. To make the cookie |
91
|
|
|
* available |
92
|
|
|
* on all subdomains of example.com then you'd set it to |
93
|
|
|
* '.example.com'. |
94
|
|
|
* The . is not required but makes it compatible with more browsers. |
95
|
|
|
* Setting it to www.example.com will make the cookie only available in |
96
|
|
|
* the www subdomain. Refer to tail matching in the spec for details. |
97
|
|
|
* @param bool $secure Indicates that the cookie should only be transmitted over a secure |
98
|
|
|
* HTTPS connection from the client. When set to true, the cookie will |
99
|
|
|
* only be set if a secure connection exists. On the server-side, it's |
100
|
|
|
* on the programmer to send this kind of cookie only on secure |
101
|
|
|
* connection (e.g. with respect to $_SERVER["HTTPS"]). |
102
|
|
|
* @param bool $httpOnly When true the cookie will be made accessible only through the HTTP |
103
|
|
|
* protocol. This means that the cookie won't be accessible by |
104
|
|
|
* scripting |
105
|
|
|
* languages, such as JavaScript. This setting can effectively help to |
106
|
|
|
* reduce identity theft through XSS attacks (although it is not |
107
|
|
|
* supported by all browsers). |
108
|
|
|
* |
109
|
|
|
* @return $this |
110
|
|
|
* |
111
|
|
|
* @throws ScopeException |
112
|
|
|
*/ |
113
|
|
|
public function set( |
114
|
|
|
string $name, |
115
|
|
|
string $value = null, |
116
|
|
|
int $lifetime = null, |
117
|
|
|
string $path = null, |
118
|
|
|
string $domain = null, |
119
|
|
|
bool $secure = null, |
120
|
|
|
bool $httpOnly = true |
121
|
|
|
) { |
122
|
|
|
$this->getCookieQueue()->set($name, $value, $lifetime, $path, $domain, $secure, $httpOnly); |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Schedule new cookie instance to be send while dispatching request. |
129
|
|
|
* |
130
|
|
|
* @param Cookie $cookie |
131
|
|
|
* |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
|
|
public function schedule(Cookie $cookie): self |
135
|
|
|
{ |
136
|
|
|
$this->getCookieQueue()->schedule($cookie); |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Schedule cookie removal. |
143
|
|
|
* |
144
|
|
|
* @param string $name |
145
|
|
|
* |
146
|
|
|
* @throws ScopeException |
147
|
|
|
*/ |
148
|
|
|
public function delete(string $name): void |
149
|
|
|
{ |
150
|
|
|
$this->getCookieQueue()->delete($name); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Cookies has to be send (specified via global scope). |
155
|
|
|
* |
156
|
|
|
* @return Cookie[] |
157
|
|
|
* |
158
|
|
|
* @throws ScopeException |
159
|
|
|
*/ |
160
|
|
|
public function getScheduled(): array |
161
|
|
|
{ |
162
|
|
|
return $this->getCookieQueue()->getScheduled(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return ServerRequestInterface |
167
|
|
|
* |
168
|
|
|
* @throws ScopeException |
169
|
|
|
*/ |
170
|
|
|
private function getRequest(): ServerRequestInterface |
171
|
|
|
{ |
172
|
|
|
try { |
173
|
|
|
return $this->container->get(ServerRequestInterface::class); |
174
|
|
|
} catch (NotFoundExceptionInterface $e) { |
175
|
|
|
throw new ScopeException('Unable to receive active request', $e->getCode(), $e); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return CookieQueue |
181
|
|
|
* |
182
|
|
|
* @throws ScopeException |
183
|
|
|
*/ |
184
|
|
|
private function getCookieQueue(): CookieQueue |
185
|
|
|
{ |
186
|
|
|
$request = $this->getRequest(); |
187
|
|
|
$queue = $request->getAttribute(CookieQueue::ATTRIBUTE, null); |
188
|
|
|
if ($queue === null) { |
189
|
|
|
throw new ScopeException('Unable to receive cookie queue, invalid request scope'); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $queue; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|