Skip to content

Commit 4a0d0ba

Browse files
authored
Merge pull request #381 from CakeDC/feature/39633
REF #39633 add google captcha in business solutions - find out more form
2 parents 012df41 + e5a22f1 commit 4a0d0ba

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/Controller/ContactsController.php

+12-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Cake\Core\Configure;
55
use Cake\Mailer\Mailer;
6+
use ReCaptcha\ReCaptcha;
67

78
/**
89
* @property \App\Model\Table\ContactsTable $Contacts
@@ -24,12 +25,17 @@ public function rapid()
2425
{
2526
$this->autoRender = false;
2627

27-
$contact = $this->Contacts->createRapidContact($this->getRequest()->getData());
28+
if ($this->getRequest()->is('post')) {
29+
$contact = $this->Contacts->createRapidContact($this->getRequest()->getData());
30+
$recaptcha = new ReCaptcha(Configure::read('ReCaptcha.secret_key'));
31+
$resp = $recaptcha->verify($this->getRequest()->getData('g-recaptcha-response'), $this->getRequest()->clientIp());
32+
if ($resp->isSuccess()) {
33+
if ($this->Contacts->save($contact)) {
34+
$this->sendEmail($contact);
2835

29-
if ($this->Contacts->save($contact)) {
30-
$this->sendEmail($contact);
31-
32-
return;
36+
return;
37+
}
38+
}
3339
}
3440

3541
$this->response = $this->response->withStatus(422);
@@ -64,7 +70,7 @@ private function sendEmail($contact)
6470
$email
6571
->setEmailFormat('text')
6672
->setReplyTo($contact->email, $contact->name)
67-
->setFrom([Configure::read('Site.contact.email') => 'CakeDC Website'])
73+
->setFrom([Configure::read('Site.contact.email') => 'CakePHP.org Website'])
6874
->setTo(Configure::read('Site.contact.email'))
6975
->setSubject($contact->subject)
7076
->deliver($contact->body);

templates/element/rapid_response.php

+12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
* @var string $modalId
77
* @var string $secondTitle
88
*/
9+
10+
use Cake\Core\Configure;
11+
912
?>
1013
<div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="gridSystemModalLabel" id="<?= $modalId ?>">
1114
<div class="modal-dialog" role="document">
@@ -127,6 +130,15 @@
127130
</div>
128131
</div>
129132
</div>
133+
<div class="row">
134+
<div class="col-md-12">
135+
<div class="fv-row mb-10 mt-10 text-center">
136+
<div class="g-recaptcha" data-sitekey="<?= Configure::read('ReCaptcha.site_key') ?>"></div>
137+
<?php $this->Html->script('https://www.google.com/recaptcha/api.js', ['block' => true]); ?>
138+
<?php $this->Form->unlockField('g-recaptcha-response'); ?>
139+
</div>
140+
</div>
141+
</div>
130142
</div>
131143

132144
<div class="modal-footer back-modal-footer-show">

0 commit comments

Comments
 (0)