Merge branch 'MDL-51434-master' of git://github.com/jleyva/moodle
[moodle.git] / lib / form / recaptcha.php
blob2080088562779b3f5aa5f8163a25663080408d02
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');
30 /**
31 * recaptcha type form element
33 * HTML class for a recaptcha type element
35 * @package core_form
36 * @category form
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 */
43 var $_helpbutton='';
45 /** @var bool if true, recaptcha will be servered from https */
46 var $_https=false;
48 /**
49 * constructor
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) {
57 global $CFG;
58 parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
59 $this->_type = 'recaptcha';
60 if (is_https()) {
61 $this->_https = true;
62 } else {
63 $this->_https = false;
67 /**
68 * Returns the recaptcha element in HTML
70 * @return string
72 function toHtml() {
73 global $CFG, $PAGE;
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');
94 $html .= '
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>
108 </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
127 * @return bool
129 function verify($challenge_field, $response_field) {
130 global $CFG;
131 require_once $CFG->libdir . '/recaptchalib.php';
132 $response = recaptcha_check_answer($CFG->recaptchaprivatekey,
133 getremoteaddr(),
134 $challenge_field,
135 $response_field,
136 $this->_https);
137 if (!$response->is_valid) {
138 $attributes = $this->getAttributes();
139 $attributes['error_message'] = $response->error;
140 $this->setAttributes($attributes);
141 return $response->error;
143 return true;