1
1
<?php
2
- namespace Ometria \Core \Helper ;
3
- use Magento \ Framework \ App \ Helper \ AbstractHelper ;
4
- use Magento \Framework \App \Helper \Context ;
5
- class Cookiechannel extends AbstractHelper
6
- {
2
+ namespace Ometria \Core \Helper ;
3
+
4
+ use Magento \Framework \App \Helper \AbstractHelper ;
5
+ use Magento \ Framework \ App \ Helper \ Context ;
6
+ use Ometria \ Core \ Helper \ Is \ Frontend as IsFrontend ;
7
7
8
- const COOKIE_NAME = 'ommage ' ;
8
+ class Cookiechannel extends AbstractHelper
9
+ {
10
+ const COOKIE_NAME = 'ommage ' ;
11
+ const COOKIEBOT_COOKIE_NAME = 'CookieConsent ' ;
9
12
const SEPERATOR_BETWEEN_COMMANDS = '; ' ;
10
- const SEPERATOR_IN_COMMANDS = ': ' ;
11
-
12
- protected $ helperConfig ;
13
-
14
- protected $ frontendAreaChecker ;
15
-
16
- protected $ cookie_did_change = false ;
17
-
13
+ const SEPERATOR_IN_COMMANDS = ': ' ;
14
+
15
+ /** @var Config */
16
+ private $ helperConfig ;
17
+
18
+ /** @var IsFrontend */
19
+ private $ frontendAreaChecker ;
20
+
21
+ /** @var bool */
22
+ private $ cookieDidChange = false ;
23
+
24
+ /**
25
+ * @param Context $context
26
+ * @param Config $helperConfig
27
+ * @param IsFrontend $frontendAreaChecker
28
+ */
18
29
public function __construct (
19
30
Context $ context ,
20
- \ Ometria \ Core \ Helper \ Config $ helperConfig ,
21
- \ Ometria \ Core \ Helper \ Is \ Frontend $ frontendAreaChecker
22
- )
23
- {
24
- $ this ->helperConfig = $ helperConfig ;
25
- $ this -> frontendAreaChecker = $ frontendAreaChecker ;
31
+ Config $ helperConfig ,
32
+ IsFrontend $ frontendAreaChecker
33
+ ) {
34
+ $ this -> helperConfig = $ helperConfig ;
35
+ $ this ->frontendAreaChecker = $ frontendAreaChecker ;
36
+
26
37
return parent ::__construct ($ context );
27
38
}
28
-
29
- public function addCommand ($ command , $ replace_if_exists =false , $ set_cookie =true ){
30
- if (!$ command || !is_array ($ command )) return ;
31
39
40
+ /**
41
+ * @param $command
42
+ * @param bool $replaceIfExists
43
+ */
44
+ public function addCommand ($ command , bool $ replaceIfExists = false )
45
+ {
46
+ if (!$ command || !is_array ($ command )) {
47
+ return ;
48
+ }
32
49
33
50
// Return if admin area or API call
34
- // if (Mage::app()->getStore()->isAdmin()) return;
35
- // if (Mage::getSingleton('api/server')->getAdapter() != null) return;
36
- if (!$ this ->frontendAreaChecker ->check ())
37
- {
51
+ if (!$ this ->frontendAreaChecker ->check ()) {
52
+ return ;
53
+ }
54
+
55
+ if (!$ this ->helperConfig ->isConfigured ()) {
38
56
return ;
39
57
}
40
58
41
- //$ometria_config_helper = Mage::helper('ometria/config');
42
- $ ometria_config_helper = $ this ->helperConfig ;
43
- if (!$ ometria_config_helper ->isConfigured ()) return ;
44
- if (!$ ometria_config_helper ->isUnivarEnabled ()) return ;
59
+ if (!$ this ->helperConfig ->isUnivarEnabled ()) {
60
+ return ;
61
+ }
62
+
63
+ if ($ command [0 ] === 'identify ' ) {
64
+ $ command [1 ] = '' ;
65
+ }
45
66
46
- if ($ command [0 ]=='identify ' ) $ command [1 ] = '' ;
47
-
48
67
$ str = implode (self ::SEPERATOR_IN_COMMANDS , $ command );
49
68
50
- $ this ->appendCookieCommand ($ command [0 ], $ str , $ replace_if_exists );
69
+ $ this ->appendCookieCommand ($ command [0 ], $ str , $ replaceIfExists );
51
70
}
52
71
53
- private function appendCookieCommand ($ command_name , $ str , $ replace_if_exists =false , $ set_cookie =true ){
54
- $ existing_cookie = isset ($ _COOKIE [self ::COOKIE_NAME ]) ? $ _COOKIE [self ::COOKIE_NAME ] : '' ;
55
- $ commands = explode (self ::SEPERATOR_BETWEEN_COMMANDS , $ existing_cookie );
56
- $ new_cookie = '' ;
72
+ /**
73
+ * @param $commandName
74
+ * @param $str
75
+ * @param bool $replaceIfExists
76
+ */
77
+ private function appendCookieCommand ($ commandName , $ str , bool $ replaceIfExists = false )
78
+ {
79
+ $ existingCookie = isset ($ _COOKIE [self ::COOKIE_NAME ]) ? $ _COOKIE [self ::COOKIE_NAME ] : '' ;
80
+ $ commands = explode (self ::SEPERATOR_BETWEEN_COMMANDS , $ existingCookie );
81
+ $ newCookie = '' ;
57
82
58
- if ($ replace_if_exists && $ commands ) {
59
- $ commands_filtered = array ();
83
+ if ($ replaceIfExists && $ commands ) {
84
+ $ commandsFiltered = array ();
60
85
foreach ($ commands as $ command ){
61
- if (strpos ($ command , $ command_name . self ::SEPERATOR_IN_COMMANDS )!==0 ) {
62
- $ commands_filtered [] = $ command ;
86
+ if (strpos ($ command , $ commandName . self ::SEPERATOR_IN_COMMANDS ) !== 0 ) {
87
+ $ commandsFiltered [] = $ command ;
63
88
}
64
89
}
65
- $ commands = $ commands_filtered ;
90
+ $ commands = $ commandsFiltered ;
66
91
$ commands = array_filter ($ commands );
67
92
}
68
93
69
94
$ commands [] = $ str ;
70
- if (count ($ commands )>6 ) $ commands = array_slice ($ commands , 0 , 6 );
95
+ if (count ($ commands ) > 6 ) {
96
+ $ commands = array_slice ($ commands , 0 , 6 );
97
+ }
71
98
72
99
$ commands = array_unique ($ commands );
73
100
$ commands = array_filter ($ commands );
74
101
$ commands = array_values ($ commands );
75
102
sort ($ commands );
76
103
$ commands = array_values ($ commands );
77
104
78
- $ new_cookie = implode (self ::SEPERATOR_BETWEEN_COMMANDS , $ commands );
79
- if (strlen ($ new_cookie )>1000 ) $ new_cookie = '' ;
105
+ $ newCookie = implode (self ::SEPERATOR_BETWEEN_COMMANDS , $ commands );
106
+ if (strlen ($ newCookie ) > 1000 ) {
107
+ $ newCookie = '' ;
108
+ }
80
109
81
- if (!headers_sent () && ($ new_cookie !=$ existing_cookie )) {
82
- $ this ->cookie_did_change = true ;
83
- $ _COOKIE [self ::COOKIE_NAME ] = $ new_cookie ;
84
- //setcookie(self::COOKIE_NAME, $new_cookie, 0, '/');
85
- //if ($set_cookie) Mage::getModel('core/cookie')
86
- // ->set(self::COOKIE_NAME, $new_cookie, 0, '/');
87
- if ($ set_cookie ) $ this ->sendCookie ();
110
+ if (!headers_sent () && ($ newCookie != $ existingCookie )) {
111
+ $ this ->cookieDidChange = true ;
112
+ $ _COOKIE [self ::COOKIE_NAME ] = $ newCookie ;
113
+
114
+ $ this ->sendCookie ();
88
115
}
89
116
}
90
-
91
- public function sendCookie (){
92
- if (!$ this ->cookie_did_change ) return ;
117
+
118
+ public function sendCookie ()
119
+ {
120
+ if (!$ this ->cookieDidChange ) {
121
+ return ;
122
+ }
123
+
124
+ if ($ this ->helperConfig ->isCookiebotEnabled () && $ this ->cookiebotCookieAllowed () === false ) {
125
+ return ;
126
+ }
127
+
93
128
$ cookie = isset ($ _COOKIE [self ::COOKIE_NAME ]) ? $ _COOKIE [self ::COOKIE_NAME ] : '' ;
94
129
setcookie (self ::COOKIE_NAME , $ cookie , 0 , '/ ' );
95
- $ this ->cookie_did_change = false ;
96
- }
97
- }
130
+ $ this ->cookieDidChange = false ;
131
+ }
132
+
133
+ /**
134
+ * @return bool
135
+ */
136
+ private function cookiebotCookieAllowed ()
137
+ {
138
+ $ cookieAllowed = false ;
139
+
140
+ if (isset ($ _COOKIE [self ::COOKIEBOT_COOKIE_NAME ])) {
141
+ switch ($ _COOKIE [self ::COOKIEBOT_COOKIE_NAME ]) {
142
+ case "-1 " :
143
+ //The user is not within a region that requires consent - all cookies are accepted
144
+ $ cookieAllowed = true ;
145
+ break ;
146
+
147
+ default :
148
+ // The user must give their consent
149
+ $ cookieConsent = $ this ->getCookiebotConsent ();
150
+ if ($ cookieConsent ) {
151
+ $ cookieClass = $ this ->helperConfig ->getCookiebotClass ();
152
+ // Consent cookie was found
153
+ if (isset ($ cookieConsent [$ cookieClass ]) && filter_var ($ cookieConsent [$ cookieClass ], FILTER_VALIDATE_BOOLEAN )) {
154
+ //Current user accepts Ometria cookies
155
+ $ cookieAllowed = true ;
156
+ }
157
+ }
158
+ break ;
159
+ }
160
+ }
161
+
162
+ return $ cookieAllowed ;
163
+ }
164
+
165
+ /**
166
+ * Read current user consent in encoded JavaScript format from Cookiebot cookie
167
+ *
168
+ * @see https://www.cookiebot.com/en/developer/
169
+ * @return mixed
170
+ */
171
+ private function getCookiebotConsent ()
172
+ {
173
+ $ json = preg_replace ('/\s*:\s*([a-zA-Z0-9_]+?)([}\[,])/ ' ,
174
+ ':"$1"$2 ' ,
175
+ preg_replace ('/([{\[,])\s*([a-zA-Z0-9_]+?):/ ' ,
176
+ '$1"$2": ' ,
177
+ str_replace ("' " ,
178
+ '" ' ,
179
+ stripslashes ($ _COOKIE [self ::COOKIEBOT_COOKIE_NAME ])
180
+ )
181
+ )
182
+ );
183
+ return json_decode ($ json , true );
184
+ }
185
+ }
0 commit comments