forked from gregzawadzki/seigisecurecontact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseigisecurecontact.php
195 lines (172 loc) · 7.26 KB
/
seigisecurecontact.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
<?php
/*
* Created by / Stworzono przez SEIGI http://pl.seigi.eu/
* Modifed by / Mahdi Shad https://ipresta.ir/
* MIT License
* Utworzono : Feb 17, 2018
* Author : SEIGI - Grzegorz Zawadzki <[email protected]>
*/
if (!defined('_PS_VERSION_'))
exit;
class seigisecurecontact extends Module {
protected $_html = '';
protected $_postErrors = array();
public function __construct() {
$this->name = 'seigisecurecontact';
$this->tab = 'frontend';
$this->version = '1.1.2';
$this->author = 'SEIGI Grzegorz Zawadzki';
// Min 1.5 and all 1.6 Absolutely not compatible with 1.7
// Do not try!, 1.7 contact form is module and cannot be overriden
$this->ps_versions_compliancy = array('min' => '1.5', 'max' => '1.6.999');
parent::__construct();
$this->displayName = $this->l('Secure Contact Form');
$this->description = $this->l('Secure Contact Form with Google ReCaptcha.');
$this->confirmUninstall = $this->l('Are you sure about removing these details?');
}
public function install() {
if(parent::install() && $this->registerHook('displayHeader') && $this->registerHook('displayCustomerAccountForm'))
return true;
return false;
}
public function hookdisplayHeader($hook_args) {
if (isset($this->context->controller->php_self) && $this->context->controller->php_self == 'contact'){
// postProccess
if (Tools::isSubmit('submitMessage'))
{
$response = $this->verifyReCaptcha(array(
'secret' => Configuration::get('SRECAP_SECRET'),
'response' => Tools::getValue('g-recaptcha-response'),
'remoteip' => $_SERVER["REMOTE_ADDR"],
));
if(!$response['success']) {
//$this->context->controller->errors[] = Tools::displayError('You did not pass verification of reCaptcha and thus your form was not submitted. Verify yourself with reCaptcha first');
foreach ($response['error-codes'] as $erc) {
$this->context->controller->errors[] = $this->reCapchaErrorTrnslate($erc);
}
//unset($_POST['submitMessage']);
//unset($_GET['submitMessage']);
}
}
$this->context->controller->addJS($this->_path.'front.js');
$this->context->controller->addJS('https://www.google.com/recaptcha/api.js');
$this->smarty->assign(array(
'recap_public' => Configuration::get('SRECAP_PUBLIC')
));
return $this->display(__FILE__, 'hookheader.tpl');
}
}
public function reCapchaErrorTrnslate($error_code) {
$r = array(
'invalid-input-secret' => $this->l('The secret parameter is missing.'),
'missing-input-response' => $this->l('The secret parameter is invalid or malformed.'),
'invalid-input-response' => $this->l('The response parameter is missing.'),
'bad-request' => $this->l('The response parameter is invalid or malformed.'),
'missing-input-secret' => $this->l('The request is invalid or malformed'),
'timeout-or-duplicate' => $this->l('You have already submited this form or waited too long to submit it. Refresh page first')
);
return $r[$error_code];
}
public function verifyReCaptcha($param) {
/**
* Taken from: https://github.com/google/recaptcha/blob/master/src/ReCaptcha/RequestMethod/Post.php
* PHP 5.6.0 changed the way you specify the peer name for SSL context options.
* Using "CN_name" will still work, but it will raise deprecated errors.
*/
$peer_key = version_compare(PHP_VERSION, '5.6.0', '<') ? 'CN_name' : 'peer_name';
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($param),
// Force the peer to validate (not needed in 5.6.0+, but still works)
'verify_peer' => true,
// Force the peer validation to use www.google.com
$peer_key => 'www.google.com',
),
);
$context = stream_context_create($options);
return json_decode(file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context), true);
}
public function getContent() {
$output = null;
if (Tools::isSubmit('submit'.$this->name)) {
$srecap_secret = strval(Tools::getValue('SRECAP_SECRET'));
$srecap_public = strval(Tools::getValue('SRECAP_PUBLIC'));
if (!$srecap_secret || empty($srecap_secret) || !$srecap_public || empty($srecap_public)) {
$output .= $this->displayError($this->l('Invalid Configuration value'));
} else {
Configuration::updateValue('SRECAP_SECRET', $srecap_secret);
Configuration::updateValue('SRECAP_PUBLIC', $srecap_public);
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
}
$output .= $this->displayForm();
$output .= '<div style="font-size: 1.3em; padding: 15px;">';
$output .= '<p>'.$this->l('This module adds Google reCaptcha scripts to your site. Your site must have google API access to use ReCaptcha').'</p>';
$output .= '<p>'.$this->l('You can always get newest version from github or directly from our site: ').'<a href="http://pl.seigi.eu/module/seigisecurecontact.html">http://pl.seigi.eu/module/seigisecurecontact.html</a></p>';
$output .= '<p>'.$this->l('You can obtain them from this URL').' <a href="https://www.google.com/recaptcha/admin">https://www.google.com/recaptcha/admin</a></p>';
$output .= '</div>';
return $output;
}
public function displayForm() {
// Get default language
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
// Init Fields form array
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Settings'),
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Site Key'),
'name' => 'SRECAP_PUBLIC',
'size' => 60,
'required' => true
),
array(
'type' => 'text',
'label' => $this->l('Secret Key'),
'name' => 'SRECAP_SECRET',
'size' => 60,
'required' => true
)
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'button btn btn-default pull-right'
)
);
$helper = new HelperForm();
// Module, token and currentIndex
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
// Language
$helper->default_form_language = $default_lang;
$helper->allow_employee_form_lang = $default_lang;
// Title and toolbar
$helper->title = $this->displayName;
$helper->show_toolbar = true; // false -> remove toolbar
$helper->toolbar_scroll = true; // yes - > Toolbar is always visible on the top of the screen.
$helper->submit_action = 'submit'.$this->name;
$helper->toolbar_btn = array(
'save' =>
array(
'desc' => $this->l('Save'),
'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
'&token='.Tools::getAdminTokenLite('AdminModules'),
),
'back' => array(
'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
'desc' => $this->l('Back to list')
)
);
// Load current value
$helper->fields_value['SRECAP_SECRET'] = Configuration::get('SRECAP_SECRET');
$helper->fields_value['SRECAP_PUBLIC'] = Configuration::get('SRECAP_PUBLIC');
return $helper->generateForm($fields_form);
}
}