From 5f4fc359be4da1210fc79f2f1e5f386455d148c1 Mon Sep 17 00:00:00 2001 From: Hadi Nategh Date: Fri, 9 Dec 2016 10:32:56 +0100 Subject: [PATCH] Implement google recaptcha for sitemanager forms --- .../inc/class.addressbook_contactform.inc.php | 32 ++++++++++++------- ...ass.module_addressbook_contactform.inc.php | 14 ++++++++ addressbook/templates/default/contactform.xet | 5 +++ admin/templates/default/config.xet | 17 ++++++++++ 4 files changed, 57 insertions(+), 11 deletions(-) diff --git a/addressbook/inc/class.addressbook_contactform.inc.php b/addressbook/inc/class.addressbook_contactform.inc.php index d174380b62..5beecbdd16 100644 --- a/addressbook/inc/class.addressbook_contactform.inc.php +++ b/addressbook/inc/class.addressbook_contactform.inc.php @@ -69,7 +69,9 @@ class addressbook_contactform // check if submitted if (is_array($content)) { - if ((isset($content['captcha_result']) && $content['captcha'] != $content['captcha_result']) || // no correct captcha OR + if (isset($_POST['g-recaptcha-response'])) $recaptcha = sitemgr_module::verify_recaptcha ($_POST['g-recaptcha-response']); + $captcha = (isset($content['captcha_result']) && $content['captcha'] != $content['captcha_result']) || ($recaptcha && $recaptcha->success == false); + if ($captcha || // no correct captcha OR (time() - $content['start_time'] < 10 && // bot indicator (less then 10 sec to fill out the form and !$GLOBALS['egw_info']['etemplate']['java_script'])) // javascript disabled) { @@ -202,20 +204,28 @@ class addressbook_contactform if ($addressbook) $preserv['owner'] = $addressbook; if ($msg) $preserv['msg'] = $msg; - - // a simple calculation captcha - $num1 = rand(1,99); - $num2 = rand(1,99); - if ($num2 > $num1) // keep the result positive + if (!sitemgr_module::get_recaptcha()) { - $n = $num1; $num1 = $num2; $num2 = $n; + // a simple calculation captcha + $num1 = rand(1,99); + $num2 = rand(1,99); + if ($num2 > $num1) // keep the result positive + { + $n = $num1; $num1 = $num2; $num2 = $n; + } + if (in_array('captcha',$fields)) + { + $content['captcha_task'] = sprintf('%d - %d =',$num1,$num2); + $preserv['captcha_result'] = $num1-$num2; + } } - if (in_array('captcha',$fields)) + else { - $content['captcha_task'] = sprintf('%d - %d =',$num1,$num2); - $preserv['captcha_result'] = $num1-$num2; + $content['show']['captcha'] = false; + $content['show']['recaptcha'] = true; + $recaptcha = sitemgr_module::get_recaptcha(); + $content['recaptcha'] = '
'; } - // allow to preset variables via get parameters if ($_SERVER['REQUEST_METHOD'] == 'GET') { diff --git a/addressbook/sitemgr/class.module_addressbook_contactform.inc.php b/addressbook/sitemgr/class.module_addressbook_contactform.inc.php index f9e73b563b..92e3521644 100644 --- a/addressbook/sitemgr/class.module_addressbook_contactform.inc.php +++ b/addressbook/sitemgr/class.module_addressbook_contactform.inc.php @@ -34,6 +34,20 @@ class module_addressbook_contactform extends sitemgr_module $this->etemplate_method = 'addressbook.addressbook_contactform.display'; } + function get_content (&$arguments,$properties) + { + $parent = parent::get_content($arguments, $properties); + + //Make sure that recaptcha keys are set before include it + if (($recaptcha = sitemgr_module::get_recaptcha())) + { + $extra .= ''."\n"; + return $extra.$parent; + } + // fallback to basic captcha + return $parent; + } + /** * Reimplemented to add the addressbook translations and fetch the addressbooks only if needed for the user-interface * diff --git a/addressbook/templates/default/contactform.xet b/addressbook/templates/default/contactform.xet index 344a133bd0..f131c1489c 100644 --- a/addressbook/templates/default/contactform.xet +++ b/addressbook/templates/default/contactform.xet @@ -129,6 +129,11 @@ + + + + + diff --git a/admin/templates/default/config.xet b/admin/templates/default/config.xet index a9e4b0cb12..76371ea5ee 100644 --- a/admin/templates/default/config.xet +++ b/admin/templates/default/config.xet @@ -250,6 +250,23 @@ + + + + + + + + + + + + + + + + +