diff --git a/lib/form/recaptcha.php b/lib/form/recaptcha.php index 2080088..41c313f 100644 --- a/lib/form/recaptcha.php +++ b/lib/form/recaptcha.php @@ -42,9 +42,6 @@ class MoodleQuickForm_recaptcha extends HTML_QuickForm_input { /** @var string html for help button, if empty then no help */ var $_helpbutton=''; - /** @var bool if true, recaptcha will be servered from https */ - var $_https=false; - /** * constructor * @@ -54,14 +51,10 @@ class MoodleQuickForm_recaptcha extends HTML_QuickForm_input { * or an associative array */ function MoodleQuickForm_recaptcha($elementName = null, $elementLabel = null, $attributes = null) { - global $CFG; + global $CFG, $PAGE; + $PAGE->requires->js(new moodle_url('https://www.google.com/recaptcha/api.js'), true); parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes); $this->_type = 'recaptcha'; - if (is_https()) { - $this->_https = true; - } else { - $this->_https = false; - } } /** @@ -70,44 +63,8 @@ class MoodleQuickForm_recaptcha extends HTML_QuickForm_input { * @return string */ function toHtml() { - global $CFG, $PAGE; - require_once $CFG->libdir . '/recaptchalib.php'; - - $recaptureoptions = Array('theme'=>'custom', 'custom_theme_widget'=>'recaptcha_widget'); - $html = html_writer::script(js_writer::set_variable('RecaptchaOptions', $recaptureoptions)); - - $attributes = $this->getAttributes(); - if (empty($attributes['error_message'])) { - $attributes['error_message'] = null; - $this->setAttributes($attributes); - } - $error = $attributes['error_message']; - unset($attributes['error_message']); - - $strincorrectpleasetryagain = get_string('incorrectpleasetryagain', 'auth'); - $strenterthewordsabove = get_string('enterthewordsabove', 'auth'); - $strenterthenumbersyouhear = get_string('enterthenumbersyouhear', 'auth'); - $strgetanothercaptcha = get_string('getanothercaptcha', 'auth'); - $strgetanaudiocaptcha = get_string('getanaudiocaptcha', 'auth'); - $strgetanimagecaptcha = get_string('getanimagecaptcha', 'auth'); - - $html .= ' -
'; - - return $html . recaptcha_get_html($CFG->recaptchapublickey, $error, $this->_https); + global $CFG; + return html_writer::empty_tag('div', array('class'=>'g-recaptcha', 'data-sitekey'=> $CFG->recaptchapublickey)); } /** @@ -120,26 +77,29 @@ class MoodleQuickForm_recaptcha extends HTML_QuickForm_input { } /** - * Checks input and challenged field + * Checks recaptcha response with google. * - * @param string $challenge_field recaptcha shown to user - * @param string $response_field input value by user + * @param string $responsestr * @return bool */ - function verify($challenge_field, $response_field) { + function is_recaptcha_response_valid($responsestr) { global $CFG; - require_once $CFG->libdir . '/recaptchalib.php'; - $response = recaptcha_check_answer($CFG->recaptchaprivatekey, - getremoteaddr(), - $challenge_field, - $response_field, - $this->_https); - if (!$response->is_valid) { - $attributes = $this->getAttributes(); - $attributes['error_message'] = $response->error; - $this->setAttributes($attributes); - return $response->error; + require_once($CFG->libdir.'/filelib.php'); + + $remoteip = getremoteaddr(); + $params = "secret={$CFG->recaptchaprivatekey}&response={$responsestr}&remoteip={$remoteip}"; + $curl = new curl(); + $url = 'https://www.google.com/recaptcha/api/siteverify'; + $response = $curl->post($url, $params); + + if ($curl->errno == 0) { + $data = json_decode($response); + + if (isset($data->success) && $data->success == 'true') { + return true; + } } - return true; + + return false; } } diff --git a/login/signup_form.php b/login/signup_form.php index 71ad670..ef08ed2 100644 --- a/login/signup_form.php +++ b/login/signup_form.php @@ -164,18 +164,19 @@ class login_signup_form extends moodleform { $errors['password'] = $errmsg; } + // BEGIN moodle.org hack (MDLSITE-3878) if ($this->signup_captcha_enabled()) { $recaptcha_element = $this->_form->getElement('recaptcha_element'); - if (!empty($this->_form->_submitValues['recaptcha_challenge_field'])) { - $challenge_field = $this->_form->_submitValues['recaptcha_challenge_field']; - $response_field = $this->_form->_submitValues['recaptcha_response_field']; - if (true !== ($result = $recaptcha_element->verify($challenge_field, $response_field))) { - $errors['recaptcha'] = $result; + if (!empty($this->_form->_submitValues['g-recaptcha-response'])) { + $response = $this->_form->_submitValues['g-recaptcha-response']; + if (!$recaptcha_element->is_recaptcha_response_valid($response)) { + $errors['recaptcha_element'] = get_string('invaliddata', 'error'); } } else { - $errors['recaptcha'] = get_string('missingrecaptchachallengefield'); + $errors['recaptcha_element'] = get_string('missingrecaptchachallengefield'); } } + // END moodle.org hack (MDLSITE-3878) // Validate customisable profile fields. (profile_validation expects an object as the parameter with userid set) $dataobject = (object)$data; $dataobject->id = 0;