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
|
|
|
return is_array($name) && isset($name['enabled']) && $name['enabled']; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getCode($name) |
59
|
|
|
{ |
60
|
|
|
return isset($this->config[$name]) && isset($this->config[$name]['code']) |
61
|
|
|
? $this->config[$name]['code'] |
62
|
|
|
: false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function output() |
66
|
|
|
{ |
67
|
|
|
echo $this->getOutput(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getOutput() |
71
|
|
|
{ |
72
|
|
|
$return = []; |
73
|
|
|
|
74
|
|
|
// Get decision window output |
75
|
|
|
$return[] = $this->getOutputHTML('decision'); |
76
|
|
|
|
77
|
|
|
// Get embed codes |
78
|
|
|
$embedCodes = [ |
79
|
|
|
'Google_Analytics' => 'googleAnalytics', |
80
|
|
|
'Smartsupp' => 'smartsupp', |
81
|
|
|
'Hotjar' => 'hotjar', |
82
|
|
|
'Tawk.to' => 'tawkto', |
83
|
|
|
]; |
84
|
|
|
foreach ($embedCodes as $configKey => $embedFile) { |
85
|
|
|
if ($this->config[$configKey]['enabled'] && $this->isAllowed($configKey)) { |
86
|
|
|
$return[] = $this->getOutputHTML($embedFile); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return implode("\n", $return); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getOutputHTML($filename) |
94
|
|
|
{ |
95
|
|
|
if (!file_exists(__DIR__.'/output/cookies/'.$filename.'.php')) { |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
ob_start(); |
100
|
|
|
include __DIR__.'/output/cookies/'.$filename.'.php'; |
101
|
|
|
return trim(ob_get_clean()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function enabledCookies() |
105
|
|
|
{ |
106
|
|
|
$return = []; |
107
|
|
|
foreach ($this->config as $name => $value) { |
108
|
|
|
if (!$this->isEnabled($name)) { |
109
|
|
|
continue; |
110
|
|
|
} |
111
|
|
|
$return[$name] = $value['label']; |
112
|
|
|
} |
113
|
|
|
return $return; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public static function setCookie($name, $value, $lifetime = 30, $lifetimePeriod = 'days', $domain = '/', $secure = false) |
117
|
|
|
{ |
118
|
|
|
// Validate parameters |
119
|
|
|
self::validateSetCookieParams($name, $value, $lifetime, $lifetimePeriod, $domain, $secure); |
120
|
|
|
|
121
|
|
|
// Calculate expiry time |
122
|
|
|
switch ($lifetimePeriod) { |
123
|
|
|
case 'minutes': |
124
|
|
|
$expiry = time() + (60 * $lifetime); // 60 = 1 minute |
125
|
|
|
break; |
126
|
|
|
|
127
|
|
|
case 'hours': |
128
|
|
|
$expiry = time() + (3600 * $lifetime); // 3600 = 1 hour |
129
|
|
|
break; |
130
|
|
|
|
131
|
|
|
case 'days': |
132
|
|
|
$expiry = time() + (86400 * $lifetime); // 86400 = 1 day |
133
|
|
|
break; |
134
|
|
|
|
135
|
|
|
case 'weeks': |
136
|
|
|
$expiry = time() + (604800 * $lifetime); // 604800 = 1 week |
137
|
|
|
break; |
138
|
|
|
|
139
|
|
|
default: |
140
|
|
|
header('HTTP/1.0 403 Forbidden'); |
141
|
|
|
throw new \Exception("Lifetime not recognised"); |
142
|
|
|
break; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// Set cookie |
146
|
|
|
return setcookie($name, $value, $expiry, $domain, $secure); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public static function validateSetCookieParams($name, $value, $lifetime, $lifetimePeriod, $domain, $secure) |
150
|
|
|
{ |
151
|
|
|
// Set allowed time periods |
152
|
|
|
$lifetimePeriods = array('minutes', 'hours', 'days', 'weeks'); |
153
|
|
|
|
154
|
|
|
// Check values passed |
155
|
|
|
$validParams = is_string($name); |
156
|
|
|
$validParams = is_string($value) ? $validParams : false; |
157
|
|
|
$validParams = is_int($lifetime) ? $validParams : false; |
158
|
|
|
$validParams = in_array($lifetimePeriod, $lifetimePeriods) ? $validParams : false; |
159
|
|
|
$validParams = is_string($domain) ? $validParams : false; |
160
|
|
|
$validParams = is_bool($secure) ? $validParams : false; |
161
|
|
|
|
162
|
|
|
if (!$validParams) { |
163
|
|
|
// Failed parameter check |
164
|
|
|
header('HTTP/1.0 403 Forbidden'); |
165
|
|
|
throw new \Exception("Incorrect parameter passed to Cookie::set"); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return true; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public static function getCookie($name) |
172
|
|
|
{ |
173
|
|
|
// If cookie exists - return it, otherwise return false |
174
|
|
|
return isset($_COOKIE[$name]) ? $_COOKIE[$name] : false; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public static function destroyCookie($name) |
178
|
|
|
{ |
179
|
|
|
// Set cookie expiration to 1 hour ago |
180
|
|
|
return setcookie($name, '', time() - 3600); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|