MDL-48501 mod_feedback: update feedback to use reCAPTCHA v2

This commit is contained in:
Jeff Webster 2018-03-04 01:08:35 -05:00
parent 83fa59a38c
commit f567f34b92

View File

@ -25,13 +25,13 @@ class feedback_item_captcha extends feedback_item_base {
$editurl = new moodle_url('/mod/feedback/edit.php', array('id'=>$cm->id));
//ther are no settings for recaptcha
// There are no settings for recaptcha.
if (isset($item->id) AND $item->id > 0) {
notice(get_string('there_are_no_settings_for_recaptcha', 'feedback'), $editurl->out());
exit;
}
//only one recaptcha can be in a feedback
// Only one recaptcha can be in a feedback.
$params = array('feedback' => $feedback->id, 'typ' => $this->type);
if ($DB->record_exists('feedback_item', $params)) {
notice(get_string('only_one_captcha_allowed', 'feedback'), $editurl->out());
@ -39,7 +39,7 @@ class feedback_item_captcha extends feedback_item_base {
}
$this->item = $item;
$this->item_form = true; //dummy
$this->item_form = true; // Dummy.
$lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
@ -140,16 +140,13 @@ class feedback_item_captcha extends feedback_item_base {
$form->add_validation_rule(function($values, $files) use ($item, $form) {
$elementname = $item->typ . '_' . $item->id . 'recaptcha';
$recaptchaelement = $form->get_form_element($elementname);
if (empty($values['recaptcha_response_field'])) {
if (empty($values['g-recaptcha-response'])) {
return array($elementname => get_string('required'));
} else if (!empty($values['recaptcha_challenge_field'])) {
$challengefield = $values['recaptcha_challenge_field'];
$responsefield = $values['recaptcha_response_field'];
if (true !== ($result = $recaptchaelement->verify($challengefield, $responsefield))) {
} else {
$response = $values['g-recaptcha-response'];
if (true !== ($result = $recaptchaelement->verify($response))) {
return array($elementname => $result);
}
} else {
return array($elementname => get_string('missingrecaptchachallengefield'));
}
return true;
});
@ -164,7 +161,7 @@ class feedback_item_captcha extends feedback_item_base {
public function get_hasvalue() {
global $CFG;
//is recaptcha configured in moodle?
// Is recaptcha configured in moodle?
if (empty($CFG->recaptchaprivatekey) OR empty($CFG->recaptchapublickey)) {
return 0;
}
@ -196,9 +193,10 @@ class feedback_item_captcha extends feedback_item_base {
return null;
}
require_once($CFG->libdir . '/recaptchalib.php');
// We return the public key, maybe we want to use the javascript api to get the image.
$data = recaptcha_get_challenge_hash_and_urls(RECAPTCHA_API_SECURE_SERVER, $CFG->recaptchapublickey);
// With reCAPTCHA v2 the captcha will be rendered by the mobile client using just the publickey.
// For now include placeholders for the v1 paramaters to support older mobile app versions.
// recaptchachallengehash, recaptchachallengeimage and recaptchachallengejs.
$data = array('', '', '');
$data[] = $CFG->recaptchapublickey;
return json_encode($data);
}