Skip to content

Commit 5d2a80f

Browse files
committed
store visitor hash in session instead of api key
1 parent 9df855d commit 5d2a80f

File tree

2 files changed

+76
-38
lines changed

2 files changed

+76
-38
lines changed

prestashop1.7/tawkto.php

+38-19
Original file line numberDiff line numberDiff line change
@@ -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
/**

prestashop8.x/tawkto.php

+38-19
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Tawkto extends Module
3434
public const TAWKTO_WIDGET_OPTS = 'TAWKTO_WIDGET_OPTS';
3535
public const TAWKTO_WIDGET_USER = 'TAWKTO_WIDGET_USER';
3636
public const TAWKTO_SELECTED_WIDGET = 'TAWKTO_SELECTED_WIDGET';
37-
public const TAWKTO_JS_API_KEY = 'TAWKTO_JS_API_KEY';
37+
public const TAWKTO_VISITOR_SESSION = 'TAWKTO_VISITOR_SESSION';
3838

3939
/**
4040
* __construct
@@ -114,7 +114,11 @@ public function hookDisplayFooter()
114114
$widgetId = $current_widget['widget_id'];
115115

116116
$result = Configuration::get(self::TAWKTO_WIDGET_OPTS);
117-
$enable_visitor_recognition = true; // default value
117+
// default values
118+
$enable_visitor_recognition = true;
119+
$js_api_key = '';
120+
$config_version = 0;
121+
118122
if ($result) {
119123
$options = json_decode($result);
120124
$current_page = (string) $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
@@ -123,6 +127,14 @@ public function hookDisplayFooter()
123127
$enable_visitor_recognition = $options->enable_visitor_recognition;
124128
}
125129

130+
if (isset($options->js_api_key)) {
131+
$js_api_key = $options->js_api_key;
132+
}
133+
134+
if (isset($options->config_version)) {
135+
$config_version = $options->config_version;
136+
}
137+
126138
// prepare visibility
127139
if (false == $options->always_display) {
128140
// show on specified urls
@@ -180,12 +192,7 @@ public function hookDisplayFooter()
180192
$customer_name = $customer->firstname . ' ' . $customer->lastname;
181193
$customer_email = $customer->email;
182194

183-
try {
184-
$key = $this->getJsApiKey($options->js_api_key);
185-
$hash = hash_hmac('sha256', $customer_email, $key);
186-
} catch (Exception $e) {
187-
$hash = '';
188-
}
195+
$hash = $this->getVisitorHash($customer_email, $js_api_key, $config_version);
189196
}
190197

191198
$this->context->smarty->assign([
@@ -297,29 +304,41 @@ private function getArrayFromJson($data)
297304
}
298305

299306
/**
300-
* Retrieve JS API key
307+
* Get visitor hash
301308
*
302-
* @param string $js_api_key Encrypted JS API key
309+
* @param string $email Visitor email
310+
* @param string $js_api_key JS API key
311+
* @param int $config_version Config version
303312
*
304313
* @return string
305-
*
306-
* @throws Exception error retrieving JS API key
307314
*/
308-
private function getJsApiKey(string $js_api_key)
315+
private function getVisitorHash(string $email, string $js_api_key, int $config_version)
309316
{
310-
if (empty($js_api_key)) {
311-
throw new Exception('JS API key is empty');
317+
if (isset($_SESSION[self::TAWKTO_VISITOR_SESSION])) {
318+
$current_session = $_SESSION[self::TAWKTO_VISITOR_SESSION];
319+
320+
if (isset($current_session['hash'])
321+
&& $current_session['email'] === $email
322+
&& $current_session['config_version'] === $config_version) {
323+
return $current_session['hash'];
324+
}
312325
}
313326

314-
if (isset($_SESSION[self::TAWKTO_JS_API_KEY])) {
315-
return $_SESSION[self::TAWKTO_JS_API_KEY];
327+
if (empty($js_api_key)) {
328+
return '';
316329
}
317330

318331
$key = $this->getDecryptedData($js_api_key);
319332

320-
$_SESSION[self::TAWKTO_JS_API_KEY] = $key;
333+
$hash = hash_hmac('sha256', $email, $key);
334+
335+
$_SESSION[self::TAWKTO_VISITOR_SESSION] = [
336+
'hash' => $hash,
337+
'email' => $email,
338+
'config_version' => $config_version,
339+
];
321340

322-
return $key;
341+
return $hash;
323342
}
324343

325344
/**

0 commit comments

Comments
 (0)