| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\Framework\Http; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
use DateTimeInterface; |
| 7 |
use InvalidArgumentException; |
| 8 |
use FluentBoards\Framework\Foundation\App; |
| 9 |
|
| 10 |
class Cookie |
| 11 |
{ |
| 12 |
/** |
| 13 |
* Cookie queue |
| 14 |
* @var array |
| 15 |
*/ |
| 16 |
protected static $queue = []; |
| 17 |
|
| 18 |
/** |
| 19 |
* Action registered iindicator. |
| 20 |
* |
| 21 |
* @var boolean |
| 22 |
*/ |
| 23 |
protected static $actionRegistered = false; |
| 24 |
|
| 25 |
/** |
| 26 |
* Sets the cookie. |
| 27 |
* |
| 28 |
* @param string $name |
| 29 |
* @param mixed $value |
| 30 |
* @param integer $minutes |
| 31 |
* @param string $path |
| 32 |
* @param string $domain |
| 33 |
* @param boolean $secure |
| 34 |
* @param boolean $httponly |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public static function set( |
| 38 |
$name, |
| 39 |
$value, |
| 40 |
$minutes = 0, |
| 41 |
$path = '', |
| 42 |
$domain = '', |
| 43 |
$secure = false, |
| 44 |
$httponly = false |
| 45 |
) { |
| 46 |
$time = static::expiresAt($minutes); |
| 47 |
|
| 48 |
setcookie($name, $value, $time, $path, $domain, $secure, $httponly); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Sets the cookie forever (5 years). |
| 53 |
* |
| 54 |
* @param string $name |
| 55 |
* @param mixed $value |
| 56 |
* @param string $path |
| 57 |
* @param string $domain |
| 58 |
* @param boolean $secure |
| 59 |
* @param boolean $httponly |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public static function setForever( |
| 63 |
$name, |
| 64 |
$value, |
| 65 |
$path = '', |
| 66 |
$domain = '', |
| 67 |
$secure = false, |
| 68 |
$httponly = false |
| 69 |
) { |
| 70 |
$fiveYears = time() + (5 * 365 * 24 * 60 * 60); |
| 71 |
|
| 72 |
setcookie($name, $value, $fiveYears, $path, $domain, $secure, $httponly); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Add a cookie into the queue. |
| 77 |
* |
| 78 |
* @param string $name |
| 79 |
* @param mixed $value |
| 80 |
* @param integer $minutes |
| 81 |
* @param string $path |
| 82 |
* @param string $domain |
| 83 |
* @param boolean $secure |
| 84 |
* @param boolean $httponly |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
public static function queue( |
| 88 |
$name, |
| 89 |
$value, |
| 90 |
$minutes = 0, |
| 91 |
$path = '', |
| 92 |
$domain = '', |
| 93 |
$secure = false, |
| 94 |
$httponly = false |
| 95 |
) { |
| 96 |
if (!static::$actionRegistered) { |
| 97 |
static::$actionRegistered = true; |
| 98 |
App::addAction( |
| 99 |
'send_headers', [static::class, 'sendHeaders'] |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
$time = static::expiresAt($minutes); |
| 104 |
|
| 105 |
static::$queue[] = [ |
| 106 |
'name' => $name, |
| 107 |
'value' => $value, |
| 108 |
'expires' => $time, |
| 109 |
'path' => $path, |
| 110 |
'domain' => $domain, |
| 111 |
'port' => $secure, |
| 112 |
'host_only' => $httponly |
| 113 |
]; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* WordPress's send_headers action handler. |
| 118 |
* |
| 119 |
* @param WordPress Object $wp |
| 120 |
* @return void |
| 121 |
*/ |
| 122 |
public static function sendHeaders($wp) |
| 123 |
{ |
| 124 |
foreach (static::$queue as $cookie) { |
| 125 |
static::set( |
| 126 |
$cookie['name'], |
| 127 |
$cookie['value'], |
| 128 |
$cookie['expires'], |
| 129 |
$cookie['path'], |
| 130 |
$cookie['domain'], |
| 131 |
$cookie['port'], |
| 132 |
$cookie['host_only'], |
| 133 |
); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Retrieve a cookie. |
| 139 |
* |
| 140 |
* @param string $name |
| 141 |
* @param mixed $default |
| 142 |
* @return mixed |
| 143 |
*/ |
| 144 |
public static function get($name, $default = null) |
| 145 |
{ |
| 146 |
if (array_key_exists($name, $_COOKIE)) { |
| 147 |
return $_COOKIE[$name]; |
| 148 |
} |
| 149 |
|
| 150 |
return $default; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Delete a cookie. |
| 155 |
* |
| 156 |
* @param string $name |
| 157 |
* @param string $path |
| 158 |
* @param string $domain |
| 159 |
* @return void |
| 160 |
*/ |
| 161 |
public static function delete($name, $path = '', $domain = '') { |
| 162 |
setcookie($name, '', time() - 3600, $path, $domain); |
| 163 |
|
| 164 |
if (array_key_exists($name, $_COOKIE)) { |
| 165 |
unset($_COOKIE[$name]); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Prepares expiration time in minutes. |
| 171 |
* |
| 172 |
* @param int|DateTimeInterface $minutes |
| 173 |
* @return int |
| 174 |
*/ |
| 175 |
protected static function expiresAt($minutes = 0) |
| 176 |
{ |
| 177 |
if (is_int($minutes)) { |
| 178 |
if ($minutes === 0) { |
| 179 |
return $minutes; |
| 180 |
} |
| 181 |
|
| 182 |
$minutes = new DateTime('+' . $minutes . ' minutes'); |
| 183 |
} |
| 184 |
|
| 185 |
if (!($minutes instanceof DateTimeInterface)) { |
| 186 |
throw new InvalidArgumentException( |
| 187 |
'Invalid expiration time provided.' |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
return $minutes->getTimestamp(); |
| 192 |
} |
| 193 |
} |
| 194 |
|