ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Cookies.php
Go to the documentation of this file.
1 <?php
9 namespace Slim\Http;
10 
13 
17 class Cookies implements CookiesInterface
18 {
24  protected $requestCookies = [];
25 
31  protected $responseCookies = [];
32 
38  protected $defaults = [
39  'value' => '',
40  'domain' => null,
41  'hostonly' => null,
42  'path' => null,
43  'expires' => null,
44  'secure' => false,
45  'httponly' => false,
46  'samesite' => null
47  ];
48 
54  public function __construct(array $cookies = [])
55  {
56  $this->requestCookies = $cookies;
57  }
58 
64  public function setDefaults(array $settings)
65  {
66  $this->defaults = array_replace($this->defaults, $settings);
67  }
68 
77  public function get($name, $default = null)
78  {
79  return isset($this->requestCookies[$name]) ? $this->requestCookies[$name] : $default;
80  }
81 
88  public function set($name, $value)
89  {
90  if (!is_array($value)) {
91  $value = ['value' => (string)$value];
92  }
93  $this->responseCookies[$name] = array_replace($this->defaults, $value);
94  }
95 
101  public function toHeaders()
102  {
103  $headers = [];
104  foreach ($this->responseCookies as $name => $properties) {
105  $headers[] = $this->toHeader($name, $properties);
106  }
107 
108  return $headers;
109  }
110 
119  protected function toHeader($name, array $properties)
120  {
121  $result = urlencode($name) . '=' . urlencode($properties['value']);
122 
123  if (isset($properties['domain'])) {
124  $result .= '; domain=' . $properties['domain'];
125  }
126 
127  if (isset($properties['path'])) {
128  $result .= '; path=' . $properties['path'];
129  }
130 
131  if (isset($properties['expires'])) {
132  if (is_string($properties['expires'])) {
133  $timestamp = strtotime($properties['expires']);
134  } else {
135  $timestamp = (int)$properties['expires'];
136  }
137  if ($timestamp !== 0) {
138  $result .= '; expires=' . gmdate('D, d-M-Y H:i:s e', $timestamp);
139  }
140  }
141 
142  if (isset($properties['secure']) && $properties['secure']) {
143  $result .= '; secure';
144  }
145 
146  if (isset($properties['hostonly']) && $properties['hostonly']) {
147  $result .= '; HostOnly';
148  }
149 
150  if (isset($properties['httponly']) && $properties['httponly']) {
151  $result .= '; HttpOnly';
152  }
153 
154  if (isset($properties['samesite']) && in_array(strtolower($properties['samesite']), ['lax', 'strict'], true)) {
155  // While strtolower is needed for correct comparison, the RFC doesn't care about case
156  $result .= '; SameSite=' . $properties['samesite'];
157  }
158 
159  return $result;
160  }
161 
172  public static function parseHeader($header)
173  {
174  if (is_array($header) === true) {
175  $header = isset($header[0]) ? $header[0] : '';
176  }
177 
178  if (is_string($header) === false) {
179  throw new InvalidArgumentException('Cannot parse Cookie data. Header value must be a string.');
180  }
181 
182  $header = rtrim($header, "\r\n");
183  $pieces = preg_split('@[;]\s*@', $header);
184  $cookies = [];
185 
186  foreach ($pieces as $cookie) {
187  $cookie = explode('=', $cookie, 2);
188 
189  if (count($cookie) === 2) {
190  $key = urldecode($cookie[0]);
191  $value = urldecode($cookie[1]);
192 
193  if (!isset($cookies[$key])) {
194  $cookies[$key] = $value;
195  }
196  }
197  }
198 
199  return $cookies;
200  }
201 }
toHeader($name, array $properties)
Convert to Set-Cookie header.
Definition: Cookies.php:119
$result
toHeaders()
Convert to Set-Cookie headers.
Definition: Cookies.php:101
Cookie helper.
Definition: Cookies.php:17
__construct(array $cookies=[])
Create new cookies helper.
Definition: Cookies.php:54
static parseHeader($header)
Parse HTTP request Cookie: header and extract into a PHP associative array.
Definition: Cookies.php:172
Slim Framework (https://slimframework.com)
Definition: Body.php:9
$default
Definition: build.php:20
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:81
setDefaults(array $settings)
Set default cookie properties.
Definition: Cookies.php:64
$key
Definition: croninfo.php:18