1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Security\Encryptions; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Cookie |
20
|
|
|
* |
21
|
|
|
* @package O2System\Security\Encryptions |
22
|
|
|
*/ |
23
|
|
|
class Cookie |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Cookie::$crypt |
27
|
|
|
* |
28
|
|
|
* Crypt instance. |
29
|
|
|
* |
30
|
|
|
* @var Crypt |
31
|
|
|
*/ |
32
|
|
|
private $crypt; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Cookie::$options |
36
|
|
|
* |
37
|
|
|
* Cookie set options. |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private $options = []; |
42
|
|
|
|
43
|
|
|
// ------------------------------------------------------------------------ |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Cookie::__construct |
47
|
|
|
*/ |
48
|
|
|
public function __construct() |
49
|
|
|
{ |
50
|
|
|
$this->crypt = new Crypt(); |
51
|
|
|
|
52
|
|
|
$this->options = [ |
53
|
|
|
'expire' => 0, |
54
|
|
|
'path' => '/', |
55
|
|
|
'domain' => null, |
56
|
|
|
'secure' => false, |
57
|
|
|
'httpOnly' => false, |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
if (class_exists('\O2System\Framework', false) or class_exists('\O2System\Reactor', false)) { |
61
|
|
|
$this->options = config()->getItem('cookie')->getArrayCopy(); |
|
|
|
|
62
|
|
|
$this->options[ 'expire' ] = time() + $this->options[ 'lifetime' ]; |
63
|
|
|
unset($this->options[ 'lifetime' ]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->options[ 'domain' ] = empty($this->options[ 'domain' ]) |
67
|
|
|
? isset($_SERVER[ 'HTTP_HOST' ]) |
68
|
|
|
? $_SERVER[ 'HTTP_HOST' ] |
69
|
|
|
: $_SERVER[ 'SERVER_NAME' ] |
70
|
|
|
: $this->options[ 'domain' ]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// ------------------------------------------------------------------------ |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Cookie::setOptions |
77
|
|
|
* |
78
|
|
|
* Sets default cookie options. |
79
|
|
|
* |
80
|
|
|
* @param array $options Cookie set options. |
81
|
|
|
* |
82
|
|
|
* @return static |
83
|
|
|
*/ |
84
|
|
|
public function setOptions(array $options) |
85
|
|
|
{ |
86
|
|
|
foreach ($options as $key => $value) { |
87
|
|
|
if (array_key_exists($key, $this->options)) { |
88
|
|
|
$this->options[ $key ] = $value; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// ------------------------------------------------------------------------ |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Cookie::encrypt |
99
|
|
|
* |
100
|
|
|
* Encrypt a cookie. |
101
|
|
|
* |
102
|
|
|
* @param string $name Cookie name. |
103
|
|
|
* @param string $value Cookie value. |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function encrypt($name, $value) |
108
|
|
|
{ |
109
|
|
|
$value = is_array($value) || is_object($value) |
|
|
|
|
110
|
|
|
? serialize($value) |
111
|
|
|
: $value; |
112
|
|
|
|
113
|
|
|
$name = isset($this->options[ 'prefix' ]) |
114
|
|
|
? $this->options[ 'prefix' ] . $name |
115
|
|
|
: $name; |
116
|
|
|
|
117
|
|
|
$value = $this->crypt->encrypt($value); |
118
|
|
|
|
119
|
|
|
return setcookie( |
120
|
|
|
$name, |
121
|
|
|
$value, |
122
|
|
|
$this->options[ 'expire' ], |
123
|
|
|
$this->options[ 'path' ], |
124
|
|
|
'.' . ltrim($this->options[ 'domain' ], '.'), |
125
|
|
|
false, |
126
|
|
|
false |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// ------------------------------------------------------------------------ |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Cookie::decrypt |
134
|
|
|
* |
135
|
|
|
* Decrypt a cookie. |
136
|
|
|
* |
137
|
|
|
* @param string $name Cookie name. |
138
|
|
|
* |
139
|
|
|
* @return string|bool Returns FALSE if cookie is not exists or the decryption failure. |
140
|
|
|
*/ |
141
|
|
|
public function decrypt($name) |
142
|
|
|
{ |
143
|
|
|
$name = isset($this->options[ 'prefix' ]) |
144
|
|
|
? $this->options[ 'prefix' ] . $name |
145
|
|
|
: $name; |
146
|
|
|
|
147
|
|
|
if ($value = input()->cookie($name)) { |
148
|
|
|
return $this->crypt->decrypt($value); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// ------------------------------------------------------------------------ |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Cookie::setKey |
158
|
|
|
* |
159
|
|
|
* Sets cookie encryption protection key. |
160
|
|
|
* |
161
|
|
|
* @param string $key Custom encryption key. |
162
|
|
|
* |
163
|
|
|
* @return static |
164
|
|
|
*/ |
165
|
|
|
protected function setKey($key) |
166
|
|
|
{ |
167
|
|
|
$this->crypt->setKey($key); |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
} |