| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\Framework\Http; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
use DateTimeInterface; |
| 7 |
use InvalidArgumentException; |
| 8 |
use FluentForm\Framework\Foundation\App; |
| 9 |
|
| 10 |
class Cookie |
| 11 |
{ |
| 12 |
/** |
| 13 |
* Cookie queue |
| 14 |
* |
| 15 |
* @var array |
| 16 |
*/ |
| 17 |
protected static $queue = []; |
| 18 |
|
| 19 |
/** |
| 20 |
* Action registered indicator. |
| 21 |
* |
| 22 |
* @var bool |
| 23 |
*/ |
| 24 |
protected static $actionRegistered = false; |
| 25 |
|
| 26 |
/** |
| 27 |
* Set a cookie immediately. |
| 28 |
* |
| 29 |
* @param string $name |
| 30 |
* @param mixed $value |
| 31 |
* @param int|DateTimeInterface $minutes |
| 32 |
* @param string $path |
| 33 |
* @param string $domain |
| 34 |
* @param bool $secure |
| 35 |
* @param bool $httponly |
| 36 |
* @param string $samesite |
| 37 |
* |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
public static function set( |
| 41 |
$name, |
| 42 |
$value, |
| 43 |
$minutes = 0, |
| 44 |
$path = '/', |
| 45 |
$domain = '', |
| 46 |
$secure = false, |
| 47 |
$httponly = false, |
| 48 |
$samesite = 'Lax' |
| 49 |
) { |
| 50 |
$cookie = static::make( |
| 51 |
$name, |
| 52 |
$value, |
| 53 |
$minutes, |
| 54 |
$path = '/', |
| 55 |
$domain, |
| 56 |
$secure, |
| 57 |
$httponly, |
| 58 |
$samesite |
| 59 |
); |
| 60 |
|
| 61 |
if (App::request()->isRest()) { |
| 62 |
$cookie['options']['expires'] = $minutes; |
| 63 |
static::setCookieInResponse($cookie); |
| 64 |
} else { |
| 65 |
setcookie($cookie['name'], $cookie['value'], $cookie['options']); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Set a cookie whose value is base64(json($value)). |
| 71 |
* |
| 72 |
* Pairs with Request::cookie(), which auto-detects this exact format |
| 73 |
* and decodes back to the original value on read. Use this when you |
| 74 |
* need to store arrays/objects in a cookie — strings, numbers, and |
| 75 |
* scalars work just fine through plain set() and don't need encoding. |
| 76 |
* |
| 77 |
* @param string $name |
| 78 |
* @param mixed $value Anything json_encode can serialize |
| 79 |
* @param int|DateTimeInterface $minutes |
| 80 |
* @param string $path |
| 81 |
* @param string $domain |
| 82 |
* @param bool $secure |
| 83 |
* @param bool $httponly |
| 84 |
* @param string $samesite |
| 85 |
* |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
public static function setEncoded( |
| 89 |
$name, |
| 90 |
$value, |
| 91 |
$minutes = 0, |
| 92 |
$path = '/', |
| 93 |
$domain = '', |
| 94 |
$secure = false, |
| 95 |
$httponly = false, |
| 96 |
$samesite = 'Lax' |
| 97 |
) { |
| 98 |
static::set( |
| 99 |
$name, |
| 100 |
base64_encode(json_encode($value)), |
| 101 |
$minutes, |
| 102 |
$path, |
| 103 |
$domain, |
| 104 |
$secure, |
| 105 |
$httponly, |
| 106 |
$samesite |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Queue a cookie whose value is base64(json($value)) for send_headers. |
| 112 |
* |
| 113 |
* Same encoding contract as setEncoded() — pairs with Request::cookie() |
| 114 |
* auto-decode on the read side. |
| 115 |
* |
| 116 |
* @param string $name |
| 117 |
* @param mixed $value |
| 118 |
* @param int|DateTimeInterface $minutes |
| 119 |
* @param string $path |
| 120 |
* @param string $domain |
| 121 |
* @param bool $secure |
| 122 |
* @param bool $httponly |
| 123 |
* @param string $samesite |
| 124 |
* |
| 125 |
* @return void |
| 126 |
*/ |
| 127 |
public static function queueEncoded( |
| 128 |
$name, |
| 129 |
$value, |
| 130 |
$minutes = 0, |
| 131 |
$path = '/', |
| 132 |
$domain = '', |
| 133 |
$secure = false, |
| 134 |
$httponly = false, |
| 135 |
$samesite = 'Lax' |
| 136 |
) { |
| 137 |
static::queue( |
| 138 |
$name, |
| 139 |
base64_encode(json_encode($value)), |
| 140 |
$minutes, |
| 141 |
$path, |
| 142 |
$domain, |
| 143 |
$secure, |
| 144 |
$httponly, |
| 145 |
$samesite |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Set a cookie that lasts 5 years. |
| 151 |
* |
| 152 |
* @param string $name |
| 153 |
* @param mixed $value |
| 154 |
* @param string $path = '/' |
| 155 |
* @param string $domain |
| 156 |
* @param bool $secure |
| 157 |
* @param bool $httponly |
| 158 |
* @param string $samesite |
| 159 |
* |
| 160 |
* @return void |
| 161 |
*/ |
| 162 |
public static function setForever( |
| 163 |
$name, |
| 164 |
$value, |
| 165 |
$path = '/', |
| 166 |
$domain = '', |
| 167 |
$secure = false, |
| 168 |
$httponly = false, |
| 169 |
$samesite = 'Lax' |
| 170 |
) { |
| 171 |
static::set( |
| 172 |
$name, |
| 173 |
$value, |
| 174 |
new DateTime('+5 years'), |
| 175 |
$path, |
| 176 |
$domain, |
| 177 |
$secure, |
| 178 |
$httponly, |
| 179 |
$samesite |
| 180 |
); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Queue a cookie to be sent with headers. |
| 185 |
* |
| 186 |
* @param string $name |
| 187 |
* @param mixed $value |
| 188 |
* @param int|DateTimeInterface $minutes |
| 189 |
* @param string $path |
| 190 |
* @param string $domain |
| 191 |
* @param bool $secure |
| 192 |
* @param bool $httponly |
| 193 |
* @param string $samesite |
| 194 |
* |
| 195 |
* @return void |
| 196 |
*/ |
| 197 |
public static function queue( |
| 198 |
$name, |
| 199 |
$value, |
| 200 |
$minutes = 0, |
| 201 |
$path = '/', |
| 202 |
$domain = '', |
| 203 |
$secure = false, |
| 204 |
$httponly = false, |
| 205 |
$samesite = 'Lax' |
| 206 |
) { |
| 207 |
$cookie = static::make( |
| 208 |
$name, |
| 209 |
$value, |
| 210 |
$minutes, |
| 211 |
$path, |
| 212 |
$domain, |
| 213 |
$secure, |
| 214 |
$httponly, |
| 215 |
$samesite |
| 216 |
); |
| 217 |
|
| 218 |
if (App::request()->isRest()) { |
| 219 |
$cookie['options']['expires'] = $minutes; |
| 220 |
static::setCookieInResponse($cookie); |
| 221 |
} else { |
| 222 |
static::$queue[] = $cookie; |
| 223 |
static::maybeAddSendHeadersAction(); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Handle WordPress's send_headers action. |
| 229 |
* |
| 230 |
* @param mixed $wp |
| 231 |
* |
| 232 |
* @return void |
| 233 |
*/ |
| 234 |
public static function sendHeaders($wp) |
| 235 |
{ |
| 236 |
foreach (static::$queue as $cookie) { |
| 237 |
setcookie($cookie['name'], $cookie['value'], $cookie['options']); |
| 238 |
} |
| 239 |
|
| 240 |
static::$queue = []; |
| 241 |
static::$actionRegistered = false; |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Get a cookie value. |
| 246 |
* |
| 247 |
* @param string $name |
| 248 |
* @param mixed $default |
| 249 |
* |
| 250 |
* @return mixed |
| 251 |
*/ |
| 252 |
public static function get($name, $default = null) |
| 253 |
{ |
| 254 |
return array_key_exists($name, $_COOKIE) ? $_COOKIE[$name] : $default; |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Delete a cookie. |
| 259 |
* |
| 260 |
* @param string $name |
| 261 |
* @param string $path |
| 262 |
* @param string $domain |
| 263 |
* @param string $samesite |
| 264 |
* |
| 265 |
* @return void |
| 266 |
*/ |
| 267 |
public static function delete( |
| 268 |
$name, |
| 269 |
$path = '/', |
| 270 |
$domain = '', |
| 271 |
$samesite = 'Lax' |
| 272 |
) { |
| 273 |
$options = [ |
| 274 |
'expires' => time() - 3600, |
| 275 |
'path' => $path, |
| 276 |
'domain' => $domain, |
| 277 |
'secure' => false, |
| 278 |
'httponly' => false, |
| 279 |
'samesite' => $samesite, |
| 280 |
]; |
| 281 |
|
| 282 |
setcookie($name, '', $options); |
| 283 |
unset($_COOKIE[$name]); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Create a cookie array for setcookie(). |
| 288 |
* |
| 289 |
* @param string $name |
| 290 |
* @param mixed $value |
| 291 |
* @param int|DateTimeInterface $minutes |
| 292 |
* @param string $path |
| 293 |
* @param string $domain |
| 294 |
* @param bool $secure |
| 295 |
* @param bool $httponly |
| 296 |
* @param string $samesite |
| 297 |
* |
| 298 |
* @return array |
| 299 |
*/ |
| 300 |
public static function make( |
| 301 |
$name, |
| 302 |
$value, |
| 303 |
$minutes = 0, |
| 304 |
$path = '/', |
| 305 |
$domain = '', |
| 306 |
$secure = false, |
| 307 |
$httponly = false, |
| 308 |
$samesite = 'Lax' |
| 309 |
) { |
| 310 |
return [ |
| 311 |
'name' => $name, |
| 312 |
'value' => $value, |
| 313 |
'options' => [ |
| 314 |
'expires' => static::expiresAt($minutes), |
| 315 |
'path' => $path, |
| 316 |
'domain' => $domain, |
| 317 |
'secure' => $secure, |
| 318 |
'httponly' => $httponly, |
| 319 |
'samesite' => $samesite, |
| 320 |
], |
| 321 |
]; |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* Convert minutes to expiration timestamp. |
| 326 |
* |
| 327 |
* @param int|DateTimeInterface $minutes |
| 328 |
* |
| 329 |
* @return int |
| 330 |
* |
| 331 |
* @throws InvalidArgumentException |
| 332 |
*/ |
| 333 |
protected static function expiresAt($minutes = 0) |
| 334 |
{ |
| 335 |
if ($minutes === 0) { |
| 336 |
return 0; |
| 337 |
} |
| 338 |
|
| 339 |
if (is_int($minutes)) { |
| 340 |
$minutes = new DateTime("+{$minutes} minutes"); |
| 341 |
} |
| 342 |
|
| 343 |
if ($minutes instanceof DateTimeInterface) { |
| 344 |
return $minutes->getTimestamp(); |
| 345 |
} |
| 346 |
|
| 347 |
throw new InvalidArgumentException('Invalid expiration time provided.'); |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* Register the send_headers action if not already registered. |
| 352 |
* |
| 353 |
* @return void |
| 354 |
*/ |
| 355 |
protected static function maybeAddSendHeadersAction() |
| 356 |
{ |
| 357 |
if (!static::$actionRegistered) { |
| 358 |
static::$actionRegistered = true; |
| 359 |
App::addAction('send_headers', [static::class, 'sendHeaders']); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* Set cookie in Response when in rest context. |
| 365 |
* |
| 366 |
* @param array $cookie |
| 367 |
*/ |
| 368 |
protected static function setCookieInResponse($cookie) |
| 369 |
{ |
| 370 |
App::make('response')->withCookie( |
| 371 |
$cookie['name'], |
| 372 |
$cookie['value'], |
| 373 |
$cookie['options']['expires'], |
| 374 |
$cookie['options']['path'], |
| 375 |
$cookie['options']['domain'], |
| 376 |
$cookie['options']['secure'], |
| 377 |
$cookie['options']['httponly'], |
| 378 |
$cookie['options']['samesite'] |
| 379 |
); |
| 380 |
} |
| 381 |
} |
| 382 |
|