ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Slim\Http\Cookies Class Reference

Cookie helper. More...

+ Inheritance diagram for Slim\Http\Cookies:
+ Collaboration diagram for Slim\Http\Cookies:

Public Member Functions

 __construct (array $cookies=[])
 Create new cookies helper. More...
 
 setDefaults (array $settings)
 Set default cookie properties. More...
 
 get ($name, $default=null)
 Get request cookie. More...
 
 set ($name, $value)
 Set response cookie. More...
 
 toHeaders ()
 Convert to Set-Cookie headers. More...
 
 get ($name, $default=null)
 
 set ($name, $value)
 
 toHeaders ()
 

Static Public Member Functions

static parseHeader ($header)
 Parse HTTP request Cookie: header and extract into a PHP associative array. More...
 
static parseHeader ($header)
 

Protected Member Functions

 toHeader ($name, array $properties)
 Convert to Set-Cookie header. More...
 

Protected Attributes

 $requestCookies = []
 
 $responseCookies = []
 
 $defaults
 

Detailed Description

Cookie helper.

Definition at line 17 of file Cookies.php.

Constructor & Destructor Documentation

◆ __construct()

Slim\Http\Cookies::__construct ( array  $cookies = [])

Create new cookies helper.

Parameters
array$cookies

Definition at line 54 of file Cookies.php.

55 {
56 $this->requestCookies = $cookies;
57 }

Member Function Documentation

◆ get()

Slim\Http\Cookies::get (   $name,
  $default = null 
)

Get request cookie.

Parameters
string$nameCookie name
mixed$defaultCookie default value
Returns
mixed Cookie value if present, else default

Implements Slim\Interfaces\Http\CookiesInterface.

Definition at line 77 of file Cookies.php.

78 {
79 return isset($this->requestCookies[$name]) ? $this->requestCookies[$name] : $default;
80 }
$default
Definition: build.php:20

References $default, and $name.

◆ parseHeader()

static Slim\Http\Cookies::parseHeader (   $header)
static

Parse HTTP request Cookie: header and extract into a PHP associative array.

Parameters
string$headerThe raw HTTP request Cookie: header
Returns
array Associative array of cookie names and values
Exceptions
InvalidArgumentExceptionif the cookie data cannot be parsed

Implements Slim\Interfaces\Http\CookiesInterface.

Definition at line 172 of file Cookies.php.

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 }
$key
Definition: croninfo.php:18

References $header, and $key.

Referenced by Slim\Http\Request\createFromEnvironment().

+ Here is the caller graph for this function:

◆ set()

Slim\Http\Cookies::set (   $name,
  $value 
)

Set response cookie.

Parameters
string$nameCookie name
string | array$valueCookie value, or cookie properties

Implements Slim\Interfaces\Http\CookiesInterface.

Definition at line 88 of file Cookies.php.

89 {
90 if (!is_array($value)) {
91 $value = ['value' => (string)$value];
92 }
93 $this->responseCookies[$name] = array_replace($this->defaults, $value);
94 }

References $name.

◆ setDefaults()

Slim\Http\Cookies::setDefaults ( array  $settings)

Set default cookie properties.

Parameters
array$settings

Definition at line 64 of file Cookies.php.

65 {
66 $this->defaults = array_replace($this->defaults, $settings);
67 }

◆ toHeader()

Slim\Http\Cookies::toHeader (   $name,
array  $properties 
)
protected

Convert to Set-Cookie header.

Parameters
string$nameCookie name
array$propertiesCookie properties
Returns
string

Definition at line 119 of file Cookies.php.

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 }
$result
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:81

References $name, $result, and $timestamp.

Referenced by Slim\Http\Cookies\toHeaders().

+ Here is the caller graph for this function:

◆ toHeaders()

Slim\Http\Cookies::toHeaders ( )

Convert to Set-Cookie headers.

Returns
string[]

Implements Slim\Interfaces\Http\CookiesInterface.

Definition at line 101 of file Cookies.php.

102 {
103 $headers = [];
104 foreach ($this->responseCookies as $name => $properties) {
105 $headers[] = $this->toHeader($name, $properties);
106 }
107
108 return $headers;
109 }
toHeader($name, array $properties)
Convert to Set-Cookie header.
Definition: Cookies.php:119

References $name, and Slim\Http\Cookies\toHeader().

+ Here is the call graph for this function:

Field Documentation

◆ $defaults

Slim\Http\Cookies::$defaults
protected
Initial value:
= [
'value' => '',
'domain' => null,
'hostonly' => null,
'path' => null,
'expires' => null,
'secure' => false,
'httponly' => false,
'samesite' => null
]

Definition at line 38 of file Cookies.php.

◆ $requestCookies

Slim\Http\Cookies::$requestCookies = []
protected

Definition at line 24 of file Cookies.php.

◆ $responseCookies

Slim\Http\Cookies::$responseCookies = []
protected

Definition at line 31 of file Cookies.php.


The documentation for this class was generated from the following file: