From bcf888423be3d5af2b28288e0b0ceed61ca3ecab Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Sat, 2 Apr 2016 13:30:06 +0200 Subject: [PATCH] support for Google reCAPTCHA --- lam/lib/account.inc | 26 ++++++++++++++ lam/lib/html.inc | 87 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) diff --git a/lam/lib/account.inc b/lam/lib/account.inc index 90dc3d2a..953c4c9b 100644 --- a/lam/lib/account.inc +++ b/lam/lib/account.inc @@ -1368,4 +1368,30 @@ function unformatShortFormatToSeconds($text) { return $text; } +/** + * Validates the Google reCAPTCHA input. + * + * @param String $secretKey secret key + * @return boolean valid + */ +function validateReCAPTCHA($secretKey) { + $url = 'https://www.google.com/recaptcha/api/siteverify'; + $vars = array('secret' => $secretKey, 'response' => $_POST['g-recaptcha-response']); + $options = array( + 'http' => array( + 'header' => "Content-type: application/x-www-form-urlencoded\r\n", + 'method' => 'POST', + 'content' => http_build_query($vars) + ) + ); + $context = stream_context_create($options); + $result = file_get_contents($url, false, $context); + if ($result === FALSE) { + logNewMessage(LOG_ERR, 'reCAPTCHA validation failed, invalid server response.'); + return false; + } + $responseJSON = json_decode($result); + return $responseJSON->{'success'} === true; +} + ?> diff --git a/lam/lib/html.inc b/lam/lib/html.inc index 3f826e13..24bba64e 100644 --- a/lam/lib/html.inc +++ b/lam/lib/html.inc @@ -2999,6 +2999,55 @@ class htmlJavaScript extends htmlElement { } +/** + * Creates a Script element to integrate external JavaScript files. + * + * @package metaHTML + */ +class htmlScript extends htmlElement { + + /** src value */ + private $src = null; + /** is async */ + private $async = false; + /** execute after page is parsed */ + private $defer = false; + + /** + * Constructor. + * + * @param String $src script path + * @param boolean $isAsync script will be executed while the page continues the parsing (default true) + * @param boolean $isDeferred script is executed when the page has finished parsing (default true) + */ + function __construct($src, $isAsync = true, $isDeferred = true) { + $this->src = $src; + $this->async = $isAsync; + $this->defer = $isDeferred; + } + + /** + * Prints the HTML code for this element. + * + * @param string $module Name of account module + * @param array $input List of meta-HTML elements + * @param array $values List of values which override the defaults in $input (name => value) + * @param boolean $restricted If true then no buttons will be displayed + * @param integer $tabindex Start value of tabulator index for input fields + * @param string $scope Account type + * @return array List of input field names and their type (name => type) + */ + function generateHTML($module, $input, $values, $restricted, &$tabindex, $scope) { + $return = array(); + $async = $this->async ? ' async' : ''; + $defer = $this->defer ? ' defer="defer"' : ''; + echo ''; + return $return; + } + +} + /** * Sets all given elements to the same width. * @@ -3266,6 +3315,44 @@ class htmlAccordion extends htmlElement { } +/** + * Creates a Google reCAPTCHA element. + * + * @package metaHTML + */ +class htmlReCAPTCHA extends htmlElement { + + /** site key */ + private $key = null; + + /** + * Constructor. + * + * @param String $key site key + */ + function __construct($key) { + $this->key = htmlspecialchars($key); + } + + /** + * Prints the HTML code for this element. + * + * @param string $module Name of account module + * @param array $input List of meta-HTML elements + * @param array $values List of values which override the defaults in $input (name => value) + * @param boolean $restricted If true then no buttons will be displayed + * @param integer $tabindex Start value of tabulator index for input fields + * @param string $scope Account type + * @return array List of input field names and their type (name => type) + */ + function generateHTML($module, $input, $values, $restricted, &$tabindex, $scope) { + $script = new htmlScript('https://www.google.com/recaptcha/api.js'); + $script->generateHTML($module, $input, $values, $restricted, $tabindex, $scope); + echo '
'; + return array(); + } +} + /** * Responsive row with 12 column layout. */