1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine HTTP |
5
|
|
|
* |
6
|
|
|
* Platine HTTP Message is the implementation of PSR 7 |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2020 Platine HTTP |
11
|
|
|
* Copyright (c) 2011 - 2017 rehyved.com |
12
|
|
|
* |
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
15
|
|
|
* in the Software without restriction, including without limitation the rights |
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
18
|
|
|
* furnished to do so, subject to the following conditions: |
19
|
|
|
* |
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
21
|
|
|
* copies or substantial portions of the Software. |
22
|
|
|
* |
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
29
|
|
|
* SOFTWARE. |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @file HttpCookie.php |
34
|
|
|
* |
35
|
|
|
* The Http Cookie class |
36
|
|
|
* |
37
|
|
|
* @package Platine\Http\Client |
38
|
|
|
* @author Platine Developers team |
39
|
|
|
* @copyright Copyright (c) 2020 |
40
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
41
|
|
|
* @link https://www.platine-php.com |
42
|
|
|
* @version 1.0.0 |
43
|
|
|
* @filesource |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
declare(strict_types=1); |
47
|
|
|
|
48
|
|
|
namespace Platine\Http\Client; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @class HttpCookie |
52
|
|
|
* @package Platine\Http\Client |
53
|
|
|
*/ |
54
|
|
|
class HttpCookie |
55
|
|
|
{ |
56
|
|
|
/** |
57
|
|
|
* The name of cookie |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
protected string $name; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The value of cookie |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
protected string $value; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The cookie expires time |
70
|
|
|
* @var int |
71
|
|
|
*/ |
72
|
|
|
protected int $expires = 0; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* The cookie max age |
76
|
|
|
* @var int |
77
|
|
|
*/ |
78
|
|
|
protected int $maxAge = 0; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* The cookie domain |
82
|
|
|
* @var string|null |
83
|
|
|
*/ |
84
|
|
|
protected ?string $domain = null; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* The cookie path |
88
|
|
|
* @var string |
89
|
|
|
*/ |
90
|
|
|
protected string $path = '/'; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Whether the cookie is secure or not |
94
|
|
|
* @var bool |
95
|
|
|
*/ |
96
|
|
|
protected bool $secure = false; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Whether the cookie is for HTTP or not |
100
|
|
|
* @var bool |
101
|
|
|
*/ |
102
|
|
|
protected bool $httpOnly = true; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* The value of the same site |
106
|
|
|
* |
107
|
|
|
* @var string|null |
108
|
|
|
*/ |
109
|
|
|
protected ?string $sameSite = null; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Create new instance |
113
|
|
|
* @param string $cookieString |
114
|
|
|
*/ |
115
|
|
|
public function __construct(string $cookieString) |
116
|
|
|
{ |
117
|
|
|
$parts = array_map('trim', explode(';', $cookieString)); |
118
|
|
|
foreach ($parts as $partStr) { |
119
|
|
|
$part = array_map('trim', explode('=', $partStr)); |
120
|
|
|
$name = $part[0]; |
121
|
|
|
$value = $part[1] ?? ''; |
122
|
|
|
if (stripos($name, 'Expires') !== false) { |
123
|
|
|
$this->expires = (int) strtotime($value); |
124
|
|
|
} elseif (stripos($name, 'Max-Age') !== false) { |
125
|
|
|
$this->maxAge = intval($value); |
126
|
|
|
} elseif (stripos($name, 'Domain') !== false) { |
127
|
|
|
$this->domain = $value; |
128
|
|
|
} elseif (stripos($name, 'Path') !== false) { |
129
|
|
|
$this->path = $value; |
130
|
|
|
} elseif (stripos($name, 'Secure') !== false) { |
131
|
|
|
$this->secure = true; |
132
|
|
|
} elseif (stripos($name, 'HttpOnly') !== false) { |
133
|
|
|
$this->httpOnly = true; |
134
|
|
|
} elseif (stripos($name, 'SameSite') !== false) { |
135
|
|
|
$this->sameSite = $value; |
136
|
|
|
} else { |
137
|
|
|
$this->name = $name; |
138
|
|
|
$this->value = $value; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public function getName(): string |
148
|
|
|
{ |
149
|
|
|
return $this->name; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
|
|
public function getValue(): string |
157
|
|
|
{ |
158
|
|
|
return $this->value; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* |
163
|
|
|
* @return int |
164
|
|
|
*/ |
165
|
|
|
public function getExpires(): int |
166
|
|
|
{ |
167
|
|
|
return $this->expires; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* |
172
|
|
|
* @return int |
173
|
|
|
*/ |
174
|
|
|
public function getMaxAge(): int |
175
|
|
|
{ |
176
|
|
|
return $this->maxAge; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* |
181
|
|
|
* @return string|null |
182
|
|
|
*/ |
183
|
|
|
public function getDomain(): ?string |
184
|
|
|
{ |
185
|
|
|
return $this->domain; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
public function getPath(): string |
193
|
|
|
{ |
194
|
|
|
return $this->path; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* |
199
|
|
|
* @return bool |
200
|
|
|
*/ |
201
|
|
|
public function isSecure(): bool |
202
|
|
|
{ |
203
|
|
|
return $this->secure; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* |
208
|
|
|
* @return bool |
209
|
|
|
*/ |
210
|
|
|
public function isHttpOnly(): bool |
211
|
|
|
{ |
212
|
|
|
return $this->httpOnly; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* |
217
|
|
|
* @return string|null |
218
|
|
|
*/ |
219
|
|
|
public function getSameSite(): ?string |
220
|
|
|
{ |
221
|
|
|
return $this->sameSite; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|