-
Notifications
You must be signed in to change notification settings - Fork 12
/
recaptchacontact.php
335 lines (279 loc) · 10.9 KB
/
recaptchacontact.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
/**
* reCAPTCHA Contact v2.1.0
*
* This plugin adds contact form features for sending email with
* google reCAPTCHA 2.0 validation.
*
* Licensed under the MIT license, see LICENSE.
*
* @package recaptchacontact
* @version 2.1.0
* @link <https://github.com/aradianoff/recaptchacontact>
* @author aRadianOff - Inés Naya <inesnaya@aradianoff.com>
* @copyright 2017, Inés Naya - aRadianOff
* @license <http://opensource.org/licenses/MIT> MIT
*/
namespace Grav\Plugin;
use Grav\Common\Page\Page;
use Grav\Common\Plugin;
use Grav\Common\Uri;
class ReCaptchaContactPlugin extends Plugin
{
/**
* Allows an optional override for the default CSS loading of whether or
* not the plugin is enabled on the page or not.
*
* Used for modular pages where one page may have it enabled and not the rest.
*
* @var bool
*/
protected $shouldLoadCss = false;
private $email;
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
'onGetPageTemplates' => ['onGetPageTemplates', 0]
];
}
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->active = false;
return;
}
$this->enable([
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
'onPageInitialized' => ['onPageInitialized', 0]
]);
$this->email = new \Grav\Plugin\Email\Email();
}
public function onGetPageTemplates($event)
{
$event->types->scanBlueprints('plugin://' . $this->name . '/blueprints');
}
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
public function onTwigSiteVariables() // Esto se procesa después de onPageInitialized
{
$config = $this->grav['config'];
if ($config->get('plugins.recaptchacontact.enabled') || $this->shouldLoadCss) {
if (!$config->get('plugins.recaptchacontact.disable_css')) {
$this->grav['assets']->addCss('plugin://recaptchacontact/assets/recaptchacontact.css');
} else {
$this->grav['assets']->addCss('theme://assets/recaptchacontact.css');
}
$this->grav['twig']->twig_vars['recaptchacontact'] = $this->grav['config']->get('plugins.recaptchacontact');
$this->grav['twig']->twig_vars['recaptchacontact']['message'] = $this->grav['session']->contact_message;
$this->grav['twig']->twig_vars['recaptchacontact']['session'] = $this->grav['session']->form;
}
}
public function onPageInitialized()
{
if (!empty($this->grav['page']->collection()) && $this->grav['page']->collection()->count() > 0){
$collection = $this->grav['page']->collection();
/** @var $page Page */
foreach ($collection as $page) {
if (isset($page->header()->recaptchacontact)){
$this->setupRecaptchaContact($page, true);
}
}
} else {
$this->setupRecaptchaContact($this->grav['page']);
}
}
/**
* Automatically add the contact form to the Page being loaded
*
* @param \Grav\Common\Page\Page $page
*/
protected function injectTemplate(Page $page)
{
/** @var $twig \Grav\Common\Twig\Twig */
$twig = $this->grav['twig'];
$original_content = $page->content();
$template = 'partials/recaptcha_container.html.twig';
$data = [
'recaptchacontact' => $this->grav['config']->get('plugins.recaptchacontact'),
'page' => $page
];
$data['recaptchacontact']['message'] = $this->grav['session']->contact_message;
$data['recaptchacontact']['session'] = $this->grav['session']->form;
// The surrounding div tags are SOLELY a workaround for a
// Parsedown bug that throws away anything after the page content
// which, in this case is the entire form, if it is not surrounded.
$page->content('<div>' . $original_content . $twig->processTemplate($template, $data) . '</div>');
}
/**
* Setup the Recaptcha Contact form
*
* @param \Grav\Common\Page\Page $page
*/
protected function setupRecaptchaContact(Page $page)
{
$this->mergePluginConfig($page);
$options = $this->grav['config']->get('plugins.recaptchacontact');
if ($options['enabled']) {
$uri = $this->grav['uri'];
$this->processFormAction($uri);
if ($options['inject_template'] === true) {
$this->injectTemplate($page);
}
}
}
/**
* Handle the Form Process
*
* @param \Grav\Common\Uri $uri
*/
protected function processFormAction(Uri $uri)
{
$message_success = $this->overwriteConfigVariable('plugins.recaptchacontact.messages.success', 'RECAPTCHACONTACT.MESSAGES.SUCCESS');
$message_error = $this->overwriteConfigVariable('plugins.recaptchacontact.messages.error', 'RECAPTCHACONTACT.MESSAGES.ERROR');
$message_fail = $this->overwriteConfigVariable('plugins.recaptchacontact.messages.fail', 'RECAPTCHACONTACT.MESSAGES.FAIL');
if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['g-recaptcha-response'])) {
$this->clearSession();
if (false === $this->validateFormData()) {
$this->setSubmissionMessage('error', $message_error);
$this->setSessionFields();
} else {
if (false === $this->sendEmail()) {
$this->setSubmissionMessage('fail', $message_fail);
$this->setSessionFields();
} else {
$this->setSubmissionMessage('success', $message_success);
}
}
$this->grav->redirectLangSafe($uri->url());
}
}
protected function setSessionFields()
{
$fields = [];
$fields['name'] = htmlspecialchars($_POST['name']);
$fields['email'] = htmlspecialchars($_POST['email']);
$fields['message'] = htmlspecialchars($_POST['message']);
$this->grav['session']->form = $fields;
}
/**
* @param $type: Type of message to be displayed
* @param $text: Text of message for user
*/
protected function setSubmissionMessage($type, $text)
{
$this->grav['session']->contact_message = [
'type' => $type,
'text' => $text
];
}
protected function clearSession()
{
$this->grav['session']->contact_message = null;
$this->grav['session']->form = null;
}
/**
* Make sure that the form is valid
*
* @return bool
*/
protected function validateFormData()
{
$form_data = $this->filterFormData($_POST);
$name = $form_data['name'];
$email = $form_data['email'];
$message = $form_data['message'];
$antispam = $form_data['antispam'];
$grecaptcha = $form_data['g-recaptcha-response'];
$secretkey = $this->grav['config']->get('plugins.recaptchacontact.grecaptcha_secret');
if (!empty($grecaptcha)) {
$response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretkey."&response=".$grecaptcha), true);
}
return (empty($name) or empty($message) or empty($email) or $antispam or empty($grecaptcha) or $response['success']==false) ? false : true;
}
/**
* Clean up and abstract form data
*
* @param $form
*
* @return array
*/
protected function filterFormData($form)
{
$defaults = [
'name' => '',
'email' => '',
'message' => '',
'antispam' => '',
'g-recaptcha-response' => ''
];
$data = array_merge($defaults, $form);
return [
'name' => $data['name'],
'email' => filter_var($data['email'], FILTER_SANITIZE_EMAIL),
'message' => $data['message'],
'antispam' => $data['antispam'],
'g-recaptcha-response' => $data['g-recaptcha-response']
];
}
/**
* Send the email
*
* @return bool: returns true if email was sent | return false if it failed to send
*/
protected function sendEmail()
{
$form = $this->filterFormData($_POST);
$recipient = $this->getEmailRecipient();
$subject = $this->overwriteConfigVariable('plugins.recaptchacontact.subject','RECAPTCHACONTACT.SUBJECT');
$email_content = "Name: {$form['name']}\n";
$email_content .= "Email: {$form['email']}\n\n";
$email_content .= "Message:\n{$form['message']}\n";
$email_headers = "From: {$form['name']} <{$form['email']}>";
if ($this->grav['config']->get('plugins.email.enabled')) {
$message = $this->email->message($subject, $email_content, 'text/plain')
->setFrom($form['email'])
->setTo($recipient);
return $this->email->send($message);
} else {
return (mail($recipient, $subject, $email_content, $email_headers)) ? true : false;
}
}
protected function getEmailRecipient()
{
$recipient = $this->overwriteConfigVariable('plugins.recaptchacontact.recipient','RECAPTCHACONTACT.RECIPIENT');
if ((!$recipient || $recipient === 'hello@example.com' || $recipient === 'name@provider.de') &&
isset($this->grav['config']['site']['author']['email'])) {
$recipient = $this->grav['config']['site']['author']['email'];
}
return $recipient;
}
protected function mergePluginConfig(Page $page)
{
$defaults = (array) $this->grav['config']->get('plugins.recaptchacontact');
if (isset($page->header()->recaptchacontact)) {
if (is_array($page->header()->recaptchacontact)) {
$this->grav['config']->set('plugins.recaptchacontact', array_replace_recursive($defaults, $page->header()->recaptchacontact));
} else {
$this->grav['config']->set('plugins.recaptchacontact.enabled', $page->header()->recaptchacontact);
}
} else {
$this->grav['config']->set('plugins.recaptchacontact.enabled', false);
}
$this->enableCssLoading();
}
protected function enableCssLoading()
{
if ($this->grav['config']->get('plugins.recaptchacontact')['enabled']) {
$this->shouldLoadCss = true;
}
}
private function overwriteConfigVariable($pageconfigvar, $langconfigvar)
{
$language = $this->grav['language'];
return $this->grav['config']->get($pageconfigvar) ?: $language->translate([$langconfigvar]);
}
}