Skip to content

Commit

Permalink
created google captcha validation while sending anonimus ticket (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
strorch authored and SilverFire committed Oct 28, 2019
1 parent f043480 commit 4efcf78
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"hiqdev/hipanel-module-finance": "<2.0 || dev-master",
"hiqdev/hipanel-module-server": "<2.0 || dev-master",
"hiqdev/yii2-asset-icheck": "~1.0.2.5",
"vintage/yii2-invisible-recaptcha": "dev-master",
"hiqdev/hipanel-faq": "<2.0 || dev-master"
},
"require-dev": {
Expand Down
24 changes: 18 additions & 6 deletions src/controllers/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use hiqdev\yii2\cart\actions\AddToCartAction;
use hisite\actions\RedirectAction;
use hisite\actions\RenderAction;
use vintage\recaptcha\helpers\RecaptchaConfig;
use vintage\recaptcha\validators\InvisibleRecaptchaValidator;
use Yii;
use yii\helpers\ArrayHelper;

Expand Down Expand Up @@ -130,15 +132,25 @@ public function actionContact()
$thread = new Thread();
$thread->scenario = Thread::SCENARIO_SUBMIT;

if (Yii::$app->request->isPost && $thread->load(Yii::$app->request->post(), '') && $thread->save()) {
if (!Yii::$app->request->isPost) {
return $this->render('contact', [
'thread' => $thread,
'includeCaptcha' => !empty(Yii::$app->params[RecaptchaConfig::SITE_KEY]),
]);
}
if ($this->validateCaptha() && $thread->load(Yii::$app->request->post(), '') && $thread->save()) {
Yii::$app->session->setFlash('contactFormSubmitted', 1);

return $this->redirect(['/site/contact', '#' => 'sendstatus']);
}
return $this->redirect(['/site/contact', '#' => 'sendstatus']);
}

return $this->render('contact', [
'thread' => $thread,
]);
private function validateCaptha(): bool
{
if (empty(Yii::$app->params[RecaptchaConfig::SITE_KEY])) {
return true;
}
$validator = new InvisibleRecaptchaValidator(Yii::$app->getRequest()->post());
return (bool)$validator->validate();
}

public function actionVds()
Expand Down
8 changes: 8 additions & 0 deletions src/views/themes/dataserv/site/contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
/** @var yii\web\View $this */
/** @var \hipanel\site\models\Thread $thread */
/** @var yii\bootstrap\ActiveForm $form */
/** @var bool $includeCaptcha */

use hipanel\helpers\Url;
use hiqdev\thememanager\widgets\FancyPanel;
use vintage\recaptcha\widgets\InvisibleRecaptcha;
use yii\bootstrap\ActiveForm;
use yii\helpers\Html;

Expand Down Expand Up @@ -119,6 +121,12 @@
'id' => 'message',
'placeholder' => $thread->getAttributeLabel('message'),
]) ?>
<?php if ($includeCaptcha) : ?>
<?= InvisibleRecaptcha::widget([
'formSelector' => '#submit-ticket',
]) ?>
</br>
<?php endif ?>
<?= Html::submitButton(Yii::t('hipanel:site:pages', 'Submit'), ['id' => 'submit', 'class' => 'mtr-btn button-fab ripple']) ?>
<?php ActiveForm::end() ?>
</div>
Expand Down

0 comments on commit 4efcf78

Please sign in to comment.