1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ScwCookie; |
4
|
|
|
|
5
|
|
|
class ScwCookie |
6
|
|
|
{ |
7
|
|
|
public $config = []; |
8
|
|
|
private $decisionMade = false; |
9
|
|
|
private $choices = []; |
10
|
|
|
|
11
|
|
|
public function __construct() |
12
|
|
|
{ |
13
|
|
|
$this->config = parse_ini_file("config.ini", true); |
14
|
|
|
|
15
|
|
|
$this->decisionMade = self::getCookie('scwCookieHidden') == 'true'; |
16
|
|
|
$this->choices = $this->getChoices(); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function getChoices() |
20
|
|
|
{ |
21
|
|
|
if (self::getCookie('scwCookie') !== false) { |
22
|
|
|
$cookie = self::getCookie('scwCookie'); |
23
|
|
|
$cookie = self::decrypt($cookie); |
24
|
|
|
return $cookie; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$return = []; |
28
|
|
|
foreach ($this->enabledCookies() as $name => $label) { |
29
|
|
|
$return[$name] = $this->config['unsetDefault']; |
30
|
|
|
} |
31
|
|
|
return $return; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public static function encrypt($value) |
35
|
|
|
{ |
36
|
|
|
$value = json_encode($value); |
37
|
|
|
$return = base64_encode($value); |
38
|
|
|
return $return; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public static function decrypt($value) |
42
|
|
|
{ |
43
|
|
|
$value = base64_decode($value); |
44
|
|
|
$value = str_replace('\"', '"', $value); |
45
|
|
|
$return = json_decode($value, true); |
46
|
|
|
return $return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function isAllowed($name) |
50
|
|
|
{ |
51
|
|
|
$choices = $this->getChoices(); |
52
|
|
|
return isset($choices[$name]) && $choices[$name] == 'allowed'; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function isEnabled($name) |
56
|
|
|
{ |
57
|
|
|
$check = $this->config[$name]; |
58
|
|
|
return is_array($check) && isset($check['enabled']) && $check['enabled']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getConfig($name, $attribute) |
62
|
|
|
{ |
63
|
|
|
return isset($this->config[$name]) && isset($this->config[$name][$attribute]) |
64
|
|
|
? $this->config[$name][$attribute] |
65
|
|
|
: false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function output() |
69
|
|
|
{ |
70
|
|
|
echo $this->getOutput(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getOutput() |
74
|
|
|
{ |
75
|
|
|
$return = []; |
76
|
|
|
|
77
|
|
|
// Get popup output |
78
|
|
|
$return[] = $this->getOutputHTML('popup'); |
79
|
|
|
|
80
|
|
|
// Get embed codes |
81
|
|
|
foreach ($this->config as $configKey => $configValue) { |
82
|
|
|
if (!is_array($configValue) || !$configValue['enabled'] || !$this->isAllowed($configKey)) { |
83
|
|
|
continue; |
84
|
|
|
} |
85
|
|
|
$return[] = $this->getOutputHTML('/cookies/'.$configKey.'/output'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return implode("\n", $return); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getOutputHTML($filename) |
92
|
|
|
{ |
93
|
|
|
if (!file_exists(__DIR__.'/output/'.$filename.'.php')) { |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
ob_start(); |
98
|
|
|
include __DIR__.'/output/'.$filename.'.php'; |
99
|
|
|
return trim(ob_get_clean()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function enabledCookies() |
103
|
|
|
{ |
104
|
|
|
$return = []; |
105
|
|
|
foreach ($this->config as $name => $value) { |
106
|
|
|
if (!$this->isEnabled($name)) { |
107
|
|
|
continue; |
108
|
|
|
} |
109
|
|
|
$return[$name] = $value['label']; |
110
|
|
|
} |
111
|
|
|
return $return; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function disabledCookies() |
115
|
|
|
{ |
116
|
|
|
$return = []; |
117
|
|
|
foreach ($this->config as $name => $value) { |
118
|
|
|
if (!$this->isEnabled($name) || !is_array($value) || $this->isAllowed($name)) { |
119
|
|
|
continue; |
120
|
|
|
} |
121
|
|
|
$return[$name] = $value['label']; |
122
|
|
|
} |
123
|
|
|
return $return; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public static function setCookie( |
127
|
|
|
$name, |
128
|
|
|
$value, |
129
|
|
|
$lifetime = 30, |
130
|
|
|
$lifetimePeriod = 'days', |
131
|
|
|
$domain = '/', |
132
|
|
|
$secure = false |
133
|
|
|
) { |
134
|
|
|
// Validate parameters |
135
|
|
|
self::validateSetCookieParams($name, $value, $lifetime, $domain, $secure); |
136
|
|
|
|
137
|
|
|
// Calculate expiry |
138
|
|
|
$expiry = strtotime('+'.$lifetime.' '.$lifetimePeriod); |
139
|
|
|
|
140
|
|
|
// Set cookie |
141
|
|
|
return setcookie($name, $value, $expiry, $domain, $secure); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public static function validateSetCookieParams($name, $value, $lifetime, $domain, $secure) |
145
|
|
|
{ |
146
|
|
|
// Types of parameters to check |
147
|
|
|
$paramTypes = [ |
148
|
|
|
// Type => Array of variables |
149
|
|
|
'string' => [$name, $value, $domain], |
150
|
|
|
'int' => [$lifetime], |
151
|
|
|
'bool' => [$secure], |
152
|
|
|
]; |
153
|
|
|
|
154
|
|
|
// Validate basic parameters |
155
|
|
|
$validParams = self::basicValidationChecks($paramTypes); |
156
|
|
|
|
157
|
|
|
// Ensure parameters are still valid |
158
|
|
|
if (!$validParams) { |
159
|
|
|
// Failed parameter check |
160
|
|
|
header('HTTP/1.0 403 Forbidden'); |
161
|
|
|
throw new \Exception("Incorrect parameter passed to Cookie::set"); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return true; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public static function basicValidationChecks($paramTypes) |
168
|
|
|
{ |
169
|
|
|
foreach ($paramTypes as $type => $variables) { |
170
|
|
|
$functionName = 'is_'.$type; |
171
|
|
|
foreach ($variables as $variable) { |
172
|
|
|
if (!$functionName($variable)) { |
173
|
|
|
return false; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
return true; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function clearCookieGroup($groupName) |
181
|
|
|
{ |
182
|
|
|
if (!file_exists(__DIR__.'/output/cookies/'.$groupName.'/cookies.php')) { |
183
|
|
|
return false; |
184
|
|
|
} |
185
|
|
|
$clearCookies = include __DIR__.'/output/cookies/'.$groupName.'/cookies.php'; |
186
|
|
|
|
187
|
|
|
$defaults = [ |
188
|
|
|
'path' => '/', |
189
|
|
|
'domain' => $_SERVER['HTTP_HOST'], |
190
|
|
|
]; |
191
|
|
|
|
192
|
|
|
if (isset($clearCookies['defaults'])) { |
193
|
|
|
$defaults = array_merge($defaults, $clearCookies['defaults']); |
194
|
|
|
unset($clearCookies['defaults']); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$return = []; |
198
|
|
|
|
199
|
|
|
foreach ($clearCookies as $cookie) { |
200
|
|
|
$cookie['path'] = isset($cookie['path']) ? $cookie['path'] : $defaults['path']; |
201
|
|
|
$cookie['domain'] = isset($cookie['domain']) ? $cookie['domain'] : $defaults['domain']; |
202
|
|
|
self::destroyCookie($cookie['name'], $cookie['path'], $cookie['domain']); |
203
|
|
|
$return[] = $cookie; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $return; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public static function getCookie($name) |
210
|
|
|
{ |
211
|
|
|
// If cookie exists - return it, otherwise return false |
212
|
|
|
return isset($_COOKIE[$name]) ? $_COOKIE[$name] : false; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public static function destroyCookie($name, $path = '', $domain = '') |
216
|
|
|
{ |
217
|
|
|
// Set cookie expiration to 1 hour ago |
218
|
|
|
return setcookie($name, '', time() - 3600, $path, $domain); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|