forked from eschiendorfer/genzo_turnstile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenzo_turnstile.php
306 lines (253 loc) · 10.9 KB
/
genzo_turnstile.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/**
* Copyright (C) 2023 Emanuel Schiendorfer
*
* @author Emanuel Schiendorfer <https://github.com/eschiendorfer>
* @copyright 2023 Emanuel Schiendorfer
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
if (!defined('_PS_VERSION_'))
exit;
class Genzo_Turnstile extends Module
{
public $errors;
private $turnstile_site_key;
private $turnstile_secret_key;
private $turnstile_if_logged;
private $turnstile_controllers;
function __construct() {
$this->name = 'genzo_turnstile';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->author = 'Emanuel Schiendorfer';
$this->need_instance = 1;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Cloudflare Turnstile');
$this->description = $this->l('Secure your forms from spam');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
// Set global values
$this->turnstile_site_key = Configuration::get('GENZO_TURNSTILE_SITE_KEY');
$this->turnstile_secret_key = Configuration::get('GENZO_TURNSTILE_SECRET_KEY');
$this->turnstile_if_logged = Configuration::get('GENZO_TURNSTILE_IF_LOGGED');
$this->setControllerRules();
if (! $this->isTurnstileConfigured()) {
$this->warning = $this->l('Site or Secret key is not set');
}
}
public function install() {
if (!parent::install() OR
!$this->registerHook('actionFrontControllerSetMedia') OR
!$this->registerHook('displayFooter')
) {
return false;
}
return true;
}
// Backoffice
public function getContent() {
if (Tools::isSubmit('submitTurnstileSettings')) {
if ($this->saveTurnstileSettings()) {
$url = $this->context->link->getAdminLink('AdminModules', true, ['module_name' => $this->name, 'configure' => $this->name, 'conf' => 4]);
Tools::redirectAdmin($url);
}
}
return $this->renderSettingsForm();
}
private function renderSettingsForm() {
// Get Submits in correct form
$turnstileSubmits = [];
foreach ($this->turnstile_controllers as $controller) {
foreach ($controller as $submitName => $turnstileAction) {
$turnstileSubmits[] = [
'submit_name' => $submitName,
'turnstile_action' => $turnstileAction.' (submit: '.$submitName.')'
];
}
}
$fieldsForm[0]['form'] = [
'legend' => [
'title' => $this->l('Cloudflare Turnstile Settings'),
'icon' => 'icon-cogs',
],
'input' => [
[
'type' => 'text',
'label' => $this->l('Site key'),
'name' => 'turnstile_site_key',
'size' => 15,
'required' => true,
],
[
'type' => 'text',
'label' => $this->l('Secret key'),
'name' => 'turnstile_secret_key',
'size' => 15,
'required' => true,
],
[
'type' => 'switch',
'label' => $this->l('Logged Customer'),
'desc' => $this->l('Do you want to use the captcha also for logged in customers?'),
'name' => 'turnstile_if_logged',
'is_bool' => true,
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Yes')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('No')
)
),
],
[
'type' => 'checkbox',
'label' => $this->l('Active Submits'),
'name' => 'turnstile_submits',
'values' => [
'query' => $turnstileSubmits,
'id' => 'submit_name',
'name' => 'turnstile_action',
],
],
[
'type' => 'text',
'label' => $this->l('Custom Submits'),
'name' => 'turnstile_submits_custom',
'desc' => $this->l('Add custom forms (for example from modules), that you want to be checked before submission. For that you need to add the "submit name" of a form.').' '.
$this->l('Use Browser Inspection, select the submit button and search for the "name" value. (Example: submitGenzoQuestion,postReview)'),
'hint' => $this->l('Multiple values should be separated by ,'),
],
],
'submit' => [
'title' => $this->l('Save'),
'name' => 'submitTurnstileSettings'
],
];
$helper = new HelperForm();
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->fields_value = [
'turnstile_site_key' => Configuration::get('GENZO_TURNSTILE_SITE_KEY'),
'turnstile_secret_key' => Configuration::get('GENZO_TURNSTILE_SECRET_KEY'),
'turnstile_if_logged' => Configuration::get('GENZO_TURNSTILE_IF_LOGGED'),
'turnstile_submits_custom' => Configuration::get('GENZO_TURNSTILE_SUBMITS_CUSTOM'),
];
$turnstileSubmitsValues = explode(',', (string)Configuration::get('GENZO_TURNSTILE_SUBMITS'));
foreach ($turnstileSubmitsValues as $submitName) {
$helper->fields_value['turnstile_submits_'.$submitName] = true;
}
return $helper->generateForm($fieldsForm);
}
private function saveTurnstileSettings() {
$turnstileSubmitsActive = [];
foreach ($this->turnstile_controllers as $controller) {
foreach ($controller as $submitName => $turnstileAction) {
$inputName = 'turnstile_submits_'.$submitName;
if (Tools::getValue($inputName)) {
$turnstileSubmitsActive[] = $submitName;
}
}
}
return Configuration::updateValue('GENZO_TURNSTILE_SITE_KEY', Tools::getValue('turnstile_site_key')) &&
Configuration::updateValue('GENZO_TURNSTILE_SECRET_KEY', Tools::getValue('turnstile_secret_key')) &&
Configuration::updateValue('GENZO_TURNSTILE_IF_LOGGED', Tools::getValue('turnstile_if_logged')) &&
Configuration::updateValue('GENZO_TURNSTILE_SUBMITS_CUSTOM', Tools::getValue('turnstile_submits_custom')) &&
Configuration::updateValue('GENZO_TURNSTILE_SUBMITS', implode(',', $turnstileSubmitsActive));
}
//Hooks
public function hookActionFrontControllerSetMedia() {
if (! $this->isTurnstileConfigured()) {
return;
}
if ($submitsToCheck = $this->checkIfControllerNeedsValidation()) {
Media::addJsDef(['turnstileSiteKey' => $this->turnstile_site_key, 'submitsToCheck' => $submitsToCheck]);
$this->context->controller->addJS($this->_path.'/views/js/genzo_turnstile.js');
foreach ($submitsToCheck as $submitToCheck => $action) {
if (Tools::isSubmit($submitToCheck)) {
if (!$this->validateFormSubmitToken()) {
unset($_POST[$submitToCheck]);
$this->context->controller->errors[] = $this->l('Captcha Validation failed');
}
}
}
}
}
public function hookDisplayFooter() {
if ($this->isTurnstileConfigured()) {
return '<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer nonce="strict-dynamic"></script>';
}
return '';
}
private function checkIfControllerNeedsValidation() {
// Check if the customer is logged
if (!$this->turnstile_if_logged && $this->context->customer->isLogged()) {
return false;
}
// Get active submits (BO configuration)
$submitsActive = [];
$turnstileSubmitsValues = explode(',', (string)Configuration::get('GENZO_TURNSTILE_SUBMITS'));
// Add custom submits to the active array (note: customs submits aren't checked for controller)
$turnstileSubmitsCustomValues = explode(',', (string)Configuration::get('GENZO_TURNSTILE_SUBMITS_CUSTOM'));
foreach ($turnstileSubmitsCustomValues as $customSubmitName) {
$submitsActive[$customSubmitName] = $customSubmitName;
}
foreach ($this->turnstile_controllers as $instance => $submitsToCheck) {
// Check the current controller (has it any possible validation)
if ($this->context->controller instanceof $instance) {
$submitsActive = [];
// Check if the current controller has any active validations
foreach ($submitsToCheck as $submitName => $turnstileAction) {
if (in_array($submitName, $turnstileSubmitsValues)) {
$submitsActive[$submitName] = $turnstileAction;
}
}
return $submitsActive;
}
}
return $submitsActive;
}
private function setControllerRules() {
$this->turnstile_controllers = [
'ContactController' => [
'submitMessage' => 'ContactForm',
],
'AuthController' => [
'SubmitCreate' => 'CreateAccount',
'SubmitLogin' => 'Login',
],
];
}
private function validateFormSubmitToken() {
$data = [
'secret' => $this->turnstile_secret_key,
'response' => Tools::getValue('cf-turnstile-response'),
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://challenges.cloudflare.com/turnstile/v0/siteverify');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
$result = json_decode(curl_exec($ch), true);
if (isset($result['success'])) {
return (bool)$result['success'];
}
return false;
}
/**
* Returns true, if turnstile is configured properly.
*
* @return bool
*/
protected function isTurnstileConfigured()
{
return $this->turnstile_site_key && $this->turnstile_secret_key;
}
}