From c0a003ccda57262ee7f1f694703c6daa60c4ba25 Mon Sep 17 00:00:00 2001 From: Flavio Copes Date: Wed, 7 Oct 2015 20:18:44 +0200 Subject: [PATCH] Added optional captcha --- comments.php | 15 +++++++++++ comments.yaml | 3 +++ templates/partials/comments.html.twig | 39 +++++++++++++++++++++++++-- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/comments.php b/comments.php index 78709b3..7e83366 100644 --- a/comments.php +++ b/comments.php @@ -68,6 +68,21 @@ class CommentsPlugin extends Plugin $email = filter_var(urldecode($post['email']), FILTER_SANITIZE_STRING); $title = filter_var(urldecode($post['title']), FILTER_SANITIZE_STRING); + + if ($this->config->get('plugins.comments.use_captcha')) { + //Validate the captcha + $recaptchaResponse = filter_var(urldecode($post['recaptchaResponse']), FILTER_SANITIZE_STRING); + + $url = 'https://www.google.com/recaptcha/api/siteverify?secret='; + $url .= $this->config->get('plugins.comments.recatpcha_secret'); + $url .= '&response=' . $recaptchaResponse; + $response = json_decode(file_get_contents($url), true); + + if ($response['success'] == false) { + throw new \RuntimeException('Error validating the Captcha'); + } + } + $filename = DATA_DIR . 'comments'; $filename .= ($lang ? '/' . $lang : ''); $filename .= $path . '.yaml'; diff --git a/comments.yaml b/comments.yaml index d4ca941..5573775 100644 --- a/comments.yaml +++ b/comments.yaml @@ -1 +1,4 @@ enabled: true +use_captcha: true +recatpcha_site_key: '6Lde4gwTAAAAAAZuv4z2AgVU6Xamn5twDYzQr8hv' +recatpcha_secret: '6Lde4gwTAAAAAPpwVKuaYm53n2bWfFfxcDxSlI54' \ No newline at end of file diff --git a/templates/partials/comments.html.twig b/templates/partials/comments.html.twig index 657b985..2448520 100644 --- a/templates/partials/comments.html.twig +++ b/templates/partials/comments.html.twig @@ -1,3 +1,5 @@ +{% set use_captcha = grav.config.plugins.comments.use_captcha %} +

Add a Comment

+{% if use_captcha %} + + + +{% endif %} +
Name: Email: + {% if use_captcha %} +
+ {% endif %}