|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Quantum PHP Framework |
|
5
|
|
|
* |
|
6
|
|
|
* An open source software development framework for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @package Quantum |
|
9
|
|
|
* @author Arman Ag. <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
|
11
|
|
|
* @link http://quantum.softberg.org/ |
|
12
|
|
|
* @since 2.8.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Cookie; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Exceptions\CryptorException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Cookie |
|
21
|
|
|
* @package Quantum\Libraries\Cookie |
|
22
|
|
|
*/ |
|
23
|
|
|
class Cookie implements CookieStorageInterface |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Cookie storage |
|
28
|
|
|
* @var array $storage |
|
29
|
|
|
*/ |
|
30
|
|
|
private static $storage = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Cookie instance |
|
34
|
|
|
* @var Cookie|null |
|
35
|
|
|
*/ |
|
36
|
|
|
private static $instance = null; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Cookie constructor. |
|
40
|
|
|
*/ |
|
41
|
|
|
private function __construct() |
|
42
|
|
|
{ |
|
43
|
|
|
// Preventing to create new object through constructor |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Gets the cookie instance |
|
48
|
|
|
* @param array $storage |
|
49
|
|
|
* @return Cookie |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function getInstance(array &$storage): Cookie |
|
52
|
|
|
{ |
|
53
|
|
|
self::$storage = &$storage; |
|
54
|
|
|
|
|
55
|
|
|
if (self::$instance === null) { |
|
56
|
|
|
self::$instance = new self(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return self::$instance; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @inheritDoc |
|
64
|
|
|
* @throws CryptorException |
|
65
|
|
|
*/ |
|
66
|
|
|
public function all(): array |
|
67
|
|
|
{ |
|
68
|
|
|
$allCookies = []; |
|
69
|
|
|
|
|
70
|
|
|
foreach (self::$storage as $key => $value) { |
|
71
|
|
|
$allCookies[$key] = crypto_decode($value); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $allCookies; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @inheritDoc |
|
79
|
|
|
*/ |
|
80
|
|
|
public function has(string $key): bool |
|
81
|
|
|
{ |
|
82
|
|
|
return isset(self::$storage[$key]) && !empty(self::$storage[$key]); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @inheritDoc |
|
87
|
|
|
* @throws CryptorException |
|
88
|
|
|
*/ |
|
89
|
|
|
public function get(string $key) |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->has($key) ? crypto_decode(self::$storage[$key]) : null; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @inheritDoc |
|
96
|
|
|
* @throws CryptorException |
|
97
|
|
|
*/ |
|
98
|
|
|
public function set(string $key, $value = '', int $time = 0, string $path = '/', string $domain = '', bool $secure = false, bool $httponly = false) |
|
99
|
|
|
{ |
|
100
|
|
|
self::$storage[$key] = crypto_encode($value); |
|
101
|
|
|
setcookie($key, crypto_encode($value), $time ? time() + $time : $time, $path, $domain, $secure, $httponly); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @inheritDoc |
|
106
|
|
|
*/ |
|
107
|
|
|
public function delete(string $key, string $path = '/') |
|
108
|
|
|
{ |
|
109
|
|
|
if ($this->has($key)) { |
|
110
|
|
|
unset(self::$storage[$key]); |
|
111
|
|
|
setcookie($key, '', time() - 3600, $path); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @inheritDoc |
|
117
|
|
|
*/ |
|
118
|
|
|
public function flush() |
|
119
|
|
|
{ |
|
120
|
|
|
if (count(self::$storage)) { |
|
121
|
|
|
foreach (self::$storage as $key => $value) { |
|
122
|
|
|
$this->delete($key, '/'); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|