@@ -35,7 +35,7 @@ class Tawkto extends Module
3535 public const TAWKTO_WIDGET_OPTS = 'TAWKTO_WIDGET_OPTS ' ;
3636 public const TAWKTO_WIDGET_USER = 'TAWKTO_WIDGET_USER ' ;
3737 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 ' ;
3939
4040 /**
4141 * __construct
@@ -115,7 +115,11 @@ public function hookDisplayFooter()
115115 $ widgetId = $ current_widget ['widget_id ' ];
116116
117117 $ 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+
119123 if ($ result ) {
120124 $ options = json_decode ($ result );
121125 $ current_page = (string ) $ _SERVER ['HTTP_HOST ' ] . $ _SERVER ['REQUEST_URI ' ];
@@ -124,6 +128,14 @@ public function hookDisplayFooter()
124128 $ enable_visitor_recognition = $ options ->enable_visitor_recognition ;
125129 }
126130
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+
127139 // prepare visibility
128140 if (false == $ options ->always_display ) {
129141 // show on specified urls
@@ -181,12 +193,7 @@ public function hookDisplayFooter()
181193 $ customer_name = $ customer ->firstname . ' ' . $ customer ->lastname ;
182194 $ customer_email = $ customer ->email ;
183195
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 );
190197 }
191198
192199 $ this ->context ->smarty ->assign ([
@@ -299,29 +306,41 @@ private function getArrayFromJson($data)
299306 }
300307
301308 /**
302- * Retrieve JS API key
309+ * Get visitor hash
303310 *
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
305314 *
306315 * @return string
307- *
308- * @throws Exception error retrieving JS API key
309316 */
310- private function getJsApiKey (string $ js_api_key )
317+ private function getVisitorHash (string $ email , string $ js_api_key, int $ config_version )
311318 {
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+ }
314327 }
315328
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 '' ;
318331 }
319332
320333 $ key = $ this ->getDecryptedData ($ js_api_key );
321334
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+ ];
323342
324- return $ key ;
343+ return $ hash ;
325344 }
326345
327346 /**
0 commit comments