@@ -35,7 +35,7 @@ class Tawkto extends Module
35
35
public const TAWKTO_WIDGET_OPTS = 'TAWKTO_WIDGET_OPTS ' ;
36
36
public const TAWKTO_WIDGET_USER = 'TAWKTO_WIDGET_USER ' ;
37
37
public const TAWKTO_SELECTED_WIDGET = 'TAWKTO_SELECTED_WIDGET ' ;
38
- public const TAWKTO_JS_API_KEY = 'TAWKTO_JS_API_KEY ' ;
38
+ public const TAWKTO_VISITOR_SESSION = 'TAWKTO_VISITOR_SESSION ' ;
39
39
40
40
/**
41
41
* __construct
@@ -115,7 +115,11 @@ public function hookDisplayFooter()
115
115
$ widgetId = $ current_widget ['widget_id ' ];
116
116
117
117
$ result = Configuration::get (self ::TAWKTO_WIDGET_OPTS );
118
- $ enable_visitor_recognition = true ; // default value
118
+ // default values
119
+ $ enable_visitor_recognition = true ;
120
+ $ js_api_key = '' ;
121
+ $ config_version = 0 ;
122
+
119
123
if ($ result ) {
120
124
$ options = json_decode ($ result );
121
125
$ current_page = (string ) $ _SERVER ['HTTP_HOST ' ] . $ _SERVER ['REQUEST_URI ' ];
@@ -124,6 +128,14 @@ public function hookDisplayFooter()
124
128
$ enable_visitor_recognition = $ options ->enable_visitor_recognition ;
125
129
}
126
130
131
+ if (isset ($ options ->js_api_key )) {
132
+ $ js_api_key = $ options ->js_api_key ;
133
+ }
134
+
135
+ if (isset ($ options ->config_version )) {
136
+ $ config_version = $ options ->config_version ;
137
+ }
138
+
127
139
// prepare visibility
128
140
if (false == $ options ->always_display ) {
129
141
// show on specified urls
@@ -181,12 +193,7 @@ public function hookDisplayFooter()
181
193
$ customer_name = $ customer ->firstname . ' ' . $ customer ->lastname ;
182
194
$ customer_email = $ customer ->email ;
183
195
184
- try {
185
- $ key = $ this ->getJsApiKey ($ options ->js_api_key );
186
- $ hash = hash_hmac ('sha256 ' , $ customer_email , $ key );
187
- } catch (Exception $ e ) {
188
- $ hash = '' ;
189
- }
196
+ $ hash = $ this ->getVisitorHash ($ customer_email , $ js_api_key , $ config_version );
190
197
}
191
198
192
199
$ this ->context ->smarty ->assign ([
@@ -299,29 +306,41 @@ private function getArrayFromJson($data)
299
306
}
300
307
301
308
/**
302
- * Retrieve JS API key
309
+ * Get visitor hash
303
310
*
304
- * @param string $js_api_key Encrypted JS API key
311
+ * @param string $email Visitor email
312
+ * @param string $js_api_key JS API key
313
+ * @param int $config_version Config version
305
314
*
306
315
* @return string
307
- *
308
- * @throws Exception error retrieving JS API key
309
316
*/
310
- private function getJsApiKey (string $ js_api_key )
317
+ private function getVisitorHash (string $ email , string $ js_api_key, int $ config_version )
311
318
{
312
- if (empty ($ js_api_key )) {
313
- throw new Exception ('JS API key is empty ' );
319
+ if (isset ($ _SESSION [self ::TAWKTO_VISITOR_SESSION ])) {
320
+ $ current_session = $ _SESSION [self ::TAWKTO_VISITOR_SESSION ];
321
+
322
+ if (isset ($ current_session ['hash ' ])
323
+ && $ current_session ['email ' ] === $ email
324
+ && $ current_session ['config_version ' ] === $ config_version ) {
325
+ return $ current_session ['hash ' ];
326
+ }
314
327
}
315
328
316
- if (isset ( $ _SESSION [ self :: TAWKTO_JS_API_KEY ] )) {
317
- return $ _SESSION [ self :: TAWKTO_JS_API_KEY ] ;
329
+ if (empty ( $ js_api_key )) {
330
+ return '' ;
318
331
}
319
332
320
333
$ key = $ this ->getDecryptedData ($ js_api_key );
321
334
322
- $ _SESSION [self ::TAWKTO_JS_API_KEY ] = $ key ;
335
+ $ hash = hash_hmac ('sha256 ' , $ email , $ key );
336
+
337
+ $ _SESSION [self ::TAWKTO_VISITOR_SESSION ] = [
338
+ 'hash ' => $ hash ,
339
+ 'email ' => $ email ,
340
+ 'config_version ' => $ config_version ,
341
+ ];
323
342
324
- return $ key ;
343
+ return $ hash ;
325
344
}
326
345
327
346
/**
0 commit comments