PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.6
Booking for Appointments and Events Calendar – Amelia v2.4.6
2.4.7 2.4.6 2.4.5 2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / vendor / slim / psr7 / src / Cookies.php
ameliabooking / vendor / slim / psr7 / src Last commit date
Factory 2 months ago Interfaces 2 months ago Cookies.php 2 months ago Environment.php 2 months ago Header.php 2 months ago Headers.php 2 months ago Message.php 2 months ago NonBufferedBody.php 2 months ago Request.php 2 months ago Response.php 2 months ago Stream.php 2 months ago UploadedFile.php 2 months ago Uri.php 2 months ago
Cookies.php
219 lines
1 <?php
2
3 /**
4 * Slim Framework (https://slimframework.com)
5 *
6 * @license https://github.com/slimphp/Slim-Psr7/blob/master/LICENSE.md (MIT License)
7 */
8
9 declare(strict_types=1);
10
11 namespace Slim\Psr7;
12
13 use InvalidArgumentException;
14
15 use function array_key_exists;
16 use function array_replace;
17 use function count;
18 use function explode;
19 use function gmdate;
20 use function in_array;
21 use function is_array;
22 use function is_string;
23 use function preg_split;
24 use function rtrim;
25 use function strtolower;
26 use function strtotime;
27 use function urldecode;
28 use function urlencode;
29
30 class Cookies
31 {
32 /**
33 * Cookies from HTTP request
34 */
35 protected array $requestCookies = [];
36
37 /**
38 * Cookies for HTTP response
39 */
40 protected array $responseCookies = [];
41
42 /**
43 * Default cookie properties
44 */
45 protected array $defaults = [
46 'value' => '',
47 'domain' => null,
48 'hostonly' => null,
49 'path' => null,
50 'expires' => null,
51 'secure' => false,
52 'httponly' => false,
53 'samesite' => null
54 ];
55
56 /**
57 * @param array $cookies
58 */
59 public function __construct(array $cookies = [])
60 {
61 $this->requestCookies = $cookies;
62 }
63
64 /**
65 * Set default cookie properties
66 *
67 * @param array $settings
68 *
69 * @return static
70 */
71 public function setDefaults(array $settings): self
72 {
73 $this->defaults = array_replace($this->defaults, $settings);
74
75 return $this;
76 }
77
78 /**
79 * Get cookie
80 *
81 * @param string $name
82 * @param string|array|null $default
83 * @return mixed|null
84 */
85 public function get(string $name, $default = null)
86 {
87 return array_key_exists($name, $this->requestCookies) ? $this->requestCookies[$name] : $default;
88 }
89
90 /**
91 * Set cookie
92 *
93 * @param string $name
94 * @param string|array $value
95 * @return static
96 */
97 public function set(string $name, $value): self
98 {
99 if (!is_array($value)) {
100 $value = ['value' => $value];
101 }
102
103 $this->responseCookies[$name] = array_replace($this->defaults, $value);
104
105 return $this;
106 }
107
108 /**
109 * Convert all response cookies into an associate array of header values
110 *
111 * @return array
112 */
113 public function toHeaders(): array
114 {
115 $headers = [];
116
117 foreach ($this->responseCookies as $name => $properties) {
118 $headers[] = $this->toHeader($name, $properties);
119 }
120
121 return $headers;
122 }
123
124 /**
125 * Convert to `Set-Cookie` header
126 *
127 * @param string $name Cookie name
128 * @param array $properties Cookie properties
129 *
130 * @return string
131 */
132 protected function toHeader(string $name, array $properties): string
133 {
134 $result = urlencode($name) . '=' . urlencode($properties['value']);
135
136 if (isset($properties['domain'])) {
137 $result .= '; domain=' . $properties['domain'];
138 }
139
140 if (isset($properties['path'])) {
141 $result .= '; path=' . $properties['path'];
142 }
143
144 if (isset($properties['expires'])) {
145 if (is_string($properties['expires'])) {
146 $timestamp = strtotime($properties['expires']);
147 } else {
148 $timestamp = (int) $properties['expires'];
149 }
150 if ($timestamp && $timestamp !== 0) {
151 $result .= '; expires=' . gmdate('D, d-M-Y H:i:s e', $timestamp);
152 }
153 }
154
155 if (isset($properties['secure']) && $properties['secure']) {
156 $result .= '; secure';
157 }
158
159 if (isset($properties['hostonly']) && $properties['hostonly']) {
160 $result .= '; HostOnly';
161 }
162
163 if (isset($properties['httponly']) && $properties['httponly']) {
164 $result .= '; HttpOnly';
165 }
166
167 if (
168 isset($properties['samesite'])
169 && in_array(strtolower($properties['samesite']), ['lax', 'strict', 'none'], true)
170 ) {
171 // While strtolower is needed for correct comparison, the RFC doesn't care about case
172 $result .= '; SameSite=' . $properties['samesite'];
173 }
174
175 return $result;
176 }
177
178 /**
179 * Parse cookie values from header value
180 *
181 * Returns an associative array of cookie names and values
182 *
183 * @param string|array $header
184 *
185 * @return array
186 */
187 public static function parseHeader($header): array
188 {
189 if (is_array($header)) {
190 $header = $header[0] ?? '';
191 }
192
193 if (!is_string($header)) {
194 throw new InvalidArgumentException('Cannot parse Cookie data. Header value must be a string.');
195 }
196
197 $header = rtrim($header, "\r\n");
198 $pieces = preg_split('@[;]\s*@', $header);
199 $cookies = [];
200
201 if (is_array($pieces)) {
202 foreach ($pieces as $cookie) {
203 $cookie = explode('=', $cookie, 2);
204
205 if (count($cookie) === 2) {
206 $key = urldecode($cookie[0]);
207 $value = urldecode($cookie[1]);
208
209 if (!isset($cookies[$key])) {
210 $cookies[$key] = $value;
211 }
212 }
213 }
214 }
215
216 return $cookies;
217 }
218 }
219