PluginProbe
Media Cloud Sync / 1.2.8
Media Cloud Sync v1.2.8
1.4.2 1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 All 36 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / Cookie / SetCookie.php

SetCookie.php in Media Cloud Sync 1.2.8, at includes/sdk/s3/GuzzleHttp/Cookie/SetCookie.php

349 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Cookie;
4
5 /**
6 * Set-Cookie object
7 */
8 class SetCookie
9 {
10 /** @var array */
11 private static $defaults = ['Name' => null, 'Value' => null, 'Domain' => null, 'Path' => '/', 'Max-Age' => null, 'Expires' => null, 'Secure' => \false, 'Discard' => \false, 'HttpOnly' => \false];
12 /** @var array Cookie data */
13 private $data;
14 /**
15 * Create a new SetCookie object from a string
16 *
17 * @param string $cookie Set-Cookie header string
18 *
19 * @return self
20 */
21 public static function fromString($cookie)
22 {
23 // Create the default return array
24 $data = self::$defaults;
25 // Explode the cookie string using a series of semicolons
26 $pieces = \array_filter(\array_map('trim', \explode(';', $cookie)));
27 // The name of the cookie (first kvp) must exist and include an equal sign.
28 if (empty($pieces[0]) || !\strpos($pieces[0], '=')) {
29 return new self($data);
30 }
31 // Add the cookie pieces into the parsed data array
32 foreach ($pieces as $part) {
33 $cookieParts = \explode('=', $part, 2);
34 $key = \trim($cookieParts[0]);
35 $value = isset($cookieParts[1]) ? \trim($cookieParts[1], " \n\r\t\x00\v") : \true;
36 // Only check for non-cookies when cookies have been found
37 if (empty($data['Name'])) {
38 $data['Name'] = $key;
39 $data['Value'] = $value;
40 } else {
41 foreach (\array_keys(self::$defaults) as $search) {
42 if (!\strcasecmp($search, $key)) {
43 $data[$search] = $value;
44 continue 2;
45 }
46 }
47 $data[$key] = $value;
48 }
49 }
50 return new self($data);
51 }
52 /**
53 * @param array $data Array of cookie data provided by a Cookie parser
54 */
55 public function __construct(array $data = [])
56 {
57 $this->data = \array_replace(self::$defaults, $data);
58 // Extract the Expires value and turn it into a UNIX timestamp if needed
59 if (!$this->getExpires() && $this->getMaxAge()) {
60 // Calculate the Expires date
61 $this->setExpires(\time() + $this->getMaxAge());
62 } elseif ($this->getExpires() && !\is_numeric($this->getExpires())) {
63 $this->setExpires($this->getExpires());
64 }
65 }
66 public function __toString()
67 {
68 $str = $this->data['Name'] . '=' . $this->data['Value'] . '; ';
69 foreach ($this->data as $k => $v) {
70 if ($k !== 'Name' && $k !== 'Value' && $v !== null && $v !== \false) {
71 if ($k === 'Expires') {
72 $str .= 'Expires=' . \gmdate('D, d M Y H:i:s \\G\\M\\T', $v) . '; ';
73 } else {
74 $str .= ($v === \true ? $k : "{$k}={$v}") . '; ';
75 }
76 }
77 }
78 return \rtrim($str, '; ');
79 }
80 public function toArray()
81 {
82 return $this->data;
83 }
84 /**
85 * Get the cookie name
86 *
87 * @return string
88 */
89 public function getName()
90 {
91 return $this->data['Name'];
92 }
93 /**
94 * Set the cookie name
95 *
96 * @param string $name Cookie name
97 */
98 public function setName($name)
99 {
100 $this->data['Name'] = $name;
101 }
102 /**
103 * Get the cookie value
104 *
105 * @return string
106 */
107 public function getValue()
108 {
109 return $this->data['Value'];
110 }
111 /**
112 * Set the cookie value
113 *
114 * @param string $value Cookie value
115 */
116 public function setValue($value)
117 {
118 $this->data['Value'] = $value;
119 }
120 /**
121 * Get the domain
122 *
123 * @return string|null
124 */
125 public function getDomain()
126 {
127 return $this->data['Domain'];
128 }
129 /**
130 * Set the domain of the cookie
131 *
132 * @param string $domain
133 */
134 public function setDomain($domain)
135 {
136 $this->data['Domain'] = $domain;
137 }
138 /**
139 * Get the path
140 *
141 * @return string
142 */
143 public function getPath()
144 {
145 return $this->data['Path'];
146 }
147 /**
148 * Set the path of the cookie
149 *
150 * @param string $path Path of the cookie
151 */
152 public function setPath($path)
153 {
154 $this->data['Path'] = $path;
155 }
156 /**
157 * Maximum lifetime of the cookie in seconds
158 *
159 * @return int|null
160 */
161 public function getMaxAge()
162 {
163 return $this->data['Max-Age'];
164 }
165 /**
166 * Set the max-age of the cookie
167 *
168 * @param int $maxAge Max age of the cookie in seconds
169 */
170 public function setMaxAge($maxAge)
171 {
172 $this->data['Max-Age'] = $maxAge;
173 }
174 /**
175 * The UNIX timestamp when the cookie Expires
176 *
177 * @return mixed
178 */
179 public function getExpires()
180 {
181 return $this->data['Expires'];
182 }
183 /**
184 * Set the unix timestamp for which the cookie will expire
185 *
186 * @param int $timestamp Unix timestamp
187 */
188 public function setExpires($timestamp)
189 {
190 $this->data['Expires'] = \is_numeric($timestamp) ? (int) $timestamp : \strtotime($timestamp);
191 }
192 /**
193 * Get whether or not this is a secure cookie
194 *
195 * @return bool|null
196 */
197 public function getSecure()
198 {
199 return $this->data['Secure'];
200 }
201 /**
202 * Set whether or not the cookie is secure
203 *
204 * @param bool $secure Set to true or false if secure
205 */
206 public function setSecure($secure)
207 {
208 $this->data['Secure'] = $secure;
209 }
210 /**
211 * Get whether or not this is a session cookie
212 *
213 * @return bool|null
214 */
215 public function getDiscard()
216 {
217 return $this->data['Discard'];
218 }
219 /**
220 * Set whether or not this is a session cookie
221 *
222 * @param bool $discard Set to true or false if this is a session cookie
223 */
224 public function setDiscard($discard)
225 {
226 $this->data['Discard'] = $discard;
227 }
228 /**
229 * Get whether or not this is an HTTP only cookie
230 *
231 * @return bool
232 */
233 public function getHttpOnly()
234 {
235 return $this->data['HttpOnly'];
236 }
237 /**
238 * Set whether or not this is an HTTP only cookie
239 *
240 * @param bool $httpOnly Set to true or false if this is HTTP only
241 */
242 public function setHttpOnly($httpOnly)
243 {
244 $this->data['HttpOnly'] = $httpOnly;
245 }
246 /**
247 * Check if the cookie matches a path value.
248 *
249 * A request-path path-matches a given cookie-path if at least one of
250 * the following conditions holds:
251 *
252 * - The cookie-path and the request-path are identical.
253 * - The cookie-path is a prefix of the request-path, and the last
254 * character of the cookie-path is %x2F ("/").
255 * - The cookie-path is a prefix of the request-path, and the first
256 * character of the request-path that is not included in the cookie-
257 * path is a %x2F ("/") character.
258 *
259 * @param string $requestPath Path to check against
260 *
261 * @return bool
262 */
263 public function matchesPath($requestPath)
264 {
265 $cookiePath = $this->getPath();
266 // Match on exact matches or when path is the default empty "/"
267 if ($cookiePath === '/' || $cookiePath == $requestPath) {
268 return \true;
269 }
270 // Ensure that the cookie-path is a prefix of the request path.
271 if (0 !== \strpos($requestPath, $cookiePath)) {
272 return \false;
273 }
274 // Match if the last character of the cookie-path is "/"
275 if (\substr($cookiePath, -1, 1) === '/') {
276 return \true;
277 }
278 // Match if the first character not included in cookie path is "/"
279 return \substr($requestPath, \strlen($cookiePath), 1) === '/';
280 }
281 /**
282 * Check if the cookie matches a domain value
283 *
284 * @param string $domain Domain to check against
285 *
286 * @return bool
287 */
288 public function matchesDomain($domain)
289 {
290 $cookieDomain = $this->getDomain();
291 if (null === $cookieDomain) {
292 return \true;
293 }
294 // Remove the leading '.' as per spec in RFC 6265.
295 // http://tools.ietf.org/html/rfc6265#section-5.2.3
296 $cookieDomain = \ltrim(\strtolower($cookieDomain), '.');
297 $domain = \strtolower($domain);
298 // Domain not set or exact match.
299 if ('' === $cookieDomain || $domain === $cookieDomain) {
300 return \true;
301 }
302 // Matching the subdomain according to RFC 6265.
303 // http://tools.ietf.org/html/rfc6265#section-5.1.3
304 if (\filter_var($domain, \FILTER_VALIDATE_IP)) {
305 return \false;
306 }
307 return (bool) \preg_match('/\\.' . \preg_quote($cookieDomain, '/') . '$/', $domain);
308 }
309 /**
310 * Check if the cookie is expired
311 *
312 * @return bool
313 */
314 public function isExpired()
315 {
316 return $this->getExpires() !== null && \time() > $this->getExpires();
317 }
318 /**
319 * Check if the cookie is valid according to RFC 6265
320 *
321 * @return bool|string Returns true if valid or an error message if invalid
322 */
323 public function validate()
324 {
325 // Names must not be empty, but can be 0
326 $name = $this->getName();
327 if (empty($name) && !\is_numeric($name)) {
328 return 'The cookie name must not be empty';
329 }
330 // Check if any of the invalid characters are present in the cookie name
331 if (\preg_match('/[\\x00-\\x20\\x22\\x28-\\x29\\x2c\\x2f\\x3a-\\x40\\x5c\\x7b\\x7d\\x7f]/', $name)) {
332 return 'Cookie name must not contain invalid characters: ASCII ' . 'Control characters (0-31;127), space, tab and the ' . 'following characters: ()<>@,;:\\"/?={}';
333 }
334 // Value must not be empty, but can be 0
335 $value = $this->getValue();
336 if (empty($value) && !\is_numeric($value)) {
337 return 'The cookie value must not be empty';
338 }
339 // Domains must not be empty, but can be 0
340 // A "0" is not a valid internet domain, but may be used as server name
341 // in a private network.
342 $domain = $this->getDomain();
343 if (empty($domain) && !\is_numeric($domain)) {
344 return 'The cookie domain must not be empty';
345 }
346 return \true;
347 }
348 }
349