2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * recaptcha type form element
21 * Contains HTML class for a recaptcha type element
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');
31 * recaptcha type form element
33 * HTML class for a recaptcha type element
37 * @copyright 2008 Nicolas Connault <nicolasconnault@gmail.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class MoodleQuickForm_recaptcha
extends HTML_QuickForm_input
{
42 /** @var string html for help button, if empty then no help */
45 /** @var bool if true, recaptcha will be servered from https */
51 * @param string $elementName (optional) name of the recaptcha element
52 * @param string $elementLabel (optional) label for recaptcha element
53 * @param mixed $attributes (optional) Either a typical HTML attribute string
54 * or an associative array
56 function MoodleQuickForm_recaptcha($elementName = null, $elementLabel = null, $attributes = null) {
58 parent
::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
59 $this->_type
= 'recaptcha';
63 $this->_https
= false;
68 * Returns the recaptcha element in HTML
74 require_once $CFG->libdir
. '/recaptchalib.php';
76 $recaptureoptions = Array('theme'=>'custom', 'custom_theme_widget'=>'recaptcha_widget');
77 $html = html_writer
::script(js_writer
::set_variable('RecaptchaOptions', $recaptureoptions));
79 $attributes = $this->getAttributes();
80 if (empty($attributes['error_message'])) {
81 $attributes['error_message'] = null;
82 $this->setAttributes($attributes);
84 $error = $attributes['error_message'];
85 unset($attributes['error_message']);
87 $strincorrectpleasetryagain = get_string('incorrectpleasetryagain', 'auth');
88 $strenterthewordsabove = get_string('enterthewordsabove', 'auth');
89 $strenterthenumbersyouhear = get_string('enterthenumbersyouhear', 'auth');
90 $strgetanothercaptcha = get_string('getanothercaptcha', 'auth');
91 $strgetanaudiocaptcha = get_string('getanaudiocaptcha', 'auth');
92 $strgetanimagecaptcha = get_string('getanimagecaptcha', 'auth');
95 <div id="recaptcha_widget" style="display:none">
97 <div id="recaptcha_image"></div>
98 <div class="recaptcha_only_if_incorrect_sol" style="color:red">' . $strincorrectpleasetryagain . '</div>
100 <span class="recaptcha_only_if_image"><label for="recaptcha_response_field">' . $strenterthewordsabove . '</label></span>
101 <span class="recaptcha_only_if_audio"><label for="recaptcha_response_field">' . $strenterthenumbersyouhear . '</label></span>
103 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
104 <input type="hidden" name="recaptcha_element" value="dummyvalue" /> <!-- Dummy value to fool formslib -->
105 <div><a href="javascript:Recaptcha.reload()">' . $strgetanothercaptcha . '</a></div>
106 <div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type(\'audio\')">' . $strgetanaudiocaptcha . '</a></div>
107 <div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type(\'image\')">' . $strgetanimagecaptcha . '</a></div>
110 return $html . recaptcha_get_html($CFG->recaptchapublickey
, $error, $this->_https
);
114 * get html for help button
116 * @return string html for help button
118 function getHelpButton(){
119 return $this->_helpbutton
;
123 * Checks input and challenged field
125 * @param string $challenge_field recaptcha shown to user
126 * @param string $response_field input value by user
129 function verify($challenge_field, $response_field) {
131 require_once $CFG->libdir
. '/recaptchalib.php';
132 $response = recaptcha_check_answer($CFG->recaptchaprivatekey
,
137 if (!$response->is_valid
) {
138 $attributes = $this->getAttributes();
139 $attributes['error_message'] = $response->error
;
140 $this->setAttributes($attributes);
141 return $response->error
;