Merge branch 'MDL-64012' of https://github.com/timhunt/moodle
[moodle.git] / lib / form / recaptcha.php
blob2eb732624e85cb0046569b860bcec4b863d882cc
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * recaptcha type form element
21 * Contains HTML class for a recaptcha type element
23 * @package core_form
24 * @copyright 2008 Nicolas Connault <nicolasconnault@gmail.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once('HTML/QuickForm/input.php');
29 require_once('templatable_form_element.php');
31 /**
32 * recaptcha type form element
34 * HTML class for a recaptcha type element
36 * @package core_form
37 * @category form
38 * @copyright 2008 Nicolas Connault <nicolasconnault@gmail.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class MoodleQuickForm_recaptcha extends HTML_QuickForm_input implements templatable {
42 use templatable_form_element {
43 export_for_template as export_for_template_base;
46 /** @var string html for help button, if empty then no help */
47 var $_helpbutton='';
49 /**
50 * constructor
52 * @param string $elementName (optional) name of the recaptcha element
53 * @param string $elementLabel (optional) label for recaptcha element
54 * @param mixed $attributes (optional) Either a typical HTML attribute string
55 * or an associative array
57 public function __construct($elementName = null, $elementLabel = null, $attributes = null) {
58 parent::__construct($elementName, $elementLabel, $attributes);
59 $this->_type = 'recaptcha';
62 /**
63 * Old syntax of class constructor. Deprecated in PHP7.
65 * @deprecated since Moodle 3.1
67 public function MoodleQuickForm_recaptcha($elementName = null, $elementLabel = null, $attributes = null) {
68 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
69 self::__construct($elementName, $elementLabel, $attributes);
72 /**
73 * Returns the reCAPTCHA element in HTML
75 * @return string The HTML to render
77 public function toHtml() {
78 global $CFG;
79 require_once($CFG->libdir . '/recaptchalib_v2.php');
81 return recaptcha_get_challenge_html(RECAPTCHA_API_URL, $CFG->recaptchapublickey);
84 /**
85 * get html for help button
87 * @return string html for help button
89 function getHelpButton(){
90 return $this->_helpbutton;
93 /**
94 * Checks recaptcha response with Google.
96 * @param string $responsestr
97 * @return bool
99 public function verify($responsestr) {
100 global $CFG;
101 require_once($CFG->libdir . '/recaptchalib_v2.php');
103 $response = recaptcha_check_response(RECAPTCHA_VERIFY_URL, $CFG->recaptchaprivatekey,
104 getremoteaddr(), $responsestr);
105 if (!$response['isvalid']) {
106 $attributes = $this->getAttributes();
107 $attributes['error_message'] = $response['error'];
108 $this->setAttributes($attributes);
109 return $response['error'];
111 return true;
114 public function export_for_template(renderer_base $output) {
115 $context = $this->export_for_template_base($output);
116 $context['html'] = $this->toHtml();
117 return $context;
121 * Get force LTR option.
123 * @return bool
125 public function get_force_ltr() {
126 return true;