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