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