-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtawkto.php
178 lines (156 loc) · 4.56 KB
/
tawkto.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
* Tawk.to
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to support@tawk.to so we can send you a copy immediately.
*
* @copyright Copyright (c) 2014 Tawk.to
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
if (!defined('_TB_VERSION_')) {
exit;
}
/**
* Class TawkTo
*/
class TawkTo extends Module
{
const TAWKTO_WIDGET_PAGE_ID = 'TAWKTO_WIDGET_PAGE_ID';
const TAWKTO_WIDGET_WIDGET_ID = 'TAWKTO_WIDGET_WIDGET_ID';
/**
* TawkTo constructor.
*/
public function __construct()
{
$this->name = 'tawkto';
$this->tab = 'front_office_features';
$this->version = '1.1.1';
$this->author = 'thirty bees';
$this->need_instance = 0;
$this->tb_min_version = '1.0.0';
$this->tb_versions_compliancy = '>= 1.0.0';
parent::__construct();
$this->displayName = $this->l('tawk.to');
$this->description = $this->l('tawk.to live chat integration.');
}
/**
* Install the module
*
* @return bool
*/
public function install()
{
return parent::install() && $this->registerHook('footer');
}
/**
* Uninstall the module
*
* @return bool
*/
public function uninstall()
{
Configuration::deleteByName(static::TAWKTO_WIDGET_PAGE_ID);
Configuration::deleteByName(static::TAWKTO_WIDGET_WIDGET_ID);
return parent::uninstall();
}
/**
* Module configuration page HTML
*
* @return string
*/
public function getContent()
{
$this->context->smarty->assign([
'iframe_url' => $this->getIframeUrl(),
'base_url' => $this->getBaseUrl(),
'module_url' => $this->context->link->getAdminLink('AdminModules', true).'&'.http_build_query([
'configure' => $this->name,
'tab_module' => $this->tab,
'module_name' => $this->name,
]),
]);
return $this->display(__FILE__, 'views/templates/admin/view.tpl');
}
/**
* Display footer
*
* @return string
*/
public function hookDisplayFooter()
{
$pageId = Configuration::get(static::TAWKTO_WIDGET_PAGE_ID);
$widgetId = Configuration::get(static::TAWKTO_WIDGET_WIDGET_ID);
if (empty($pageId) || empty($widgetId)) {
return '';
}
$this->context->smarty->assign(
[
'widget_id' => $widgetId,
'page_id' => $pageId,
]
);
return $this->display(__FILE__, 'widget.tpl');
}
/**
* Ajax process - set widget
*/
public function ajaxProcessSetWidget()
{
if (!isset($_POST['pageId']) || !isset($_POST['widgetId']) || !static::idsAreCorrect($_POST['pageId'], $_POST['widgetId'])) {
die(json_encode(['success' => false]));
}
Configuration::updateValue(static::TAWKTO_WIDGET_PAGE_ID, $_POST['pageId']);
Configuration::updateValue(static::TAWKTO_WIDGET_WIDGET_ID, $_POST['widgetId']);
die(json_encode(['success' => true]));
}
/**
* Ajax process - remove widget
*/
public function ajaxProcessRemoveWidget()
{
Configuration::deleteByName(static::TAWKTO_WIDGET_PAGE_ID);
Configuration::deleteByName(static::TAWKTO_WIDGET_WIDGET_ID);
die(json_encode(['success' => true]));
}
/**
* Get iframe URL
*
* @return string
*/
private function getIframeUrl()
{
return $this->getBaseUrl()
.'/generic/widgets'
.'?currentPageId='.Configuration::get(static::TAWKTO_WIDGET_PAGE_ID)
.'¤tWidgetId='.Configuration::get(static::TAWKTO_WIDGET_WIDGET_ID);
}
/**
* Get base url
*
* @return string
*/
private function getBaseUrl()
{
return 'https://plugins.tawk.to';
}
/**
* Validate IDs
*
* @param string $pageId
* @param string $widgetId
*
* @return bool
*/
private static function idsAreCorrect($pageId, $widgetId)
{
return preg_match('/^[0-9A-Fa-f]{24}$/', $pageId) === 1 && preg_match('/^[a-z0-9]{1,50}$/i', $widgetId) === 1;
}
}