2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
9 * @author Nicolas Connault <nicolasconnault@gmail.com>
18 class MoodleQuickForm_recaptcha
extends HTML_QuickForm_input
{
21 * html for help button, if empty then no help
31 * $form->addElement('textarea_counter', 'message', 'Message',
32 * array('cols'=>60, 'rows'=>10), 160);
35 function MoodleQuickForm_recaptcha($elementName = null, $elementLabel = null, $attributes = null) {
36 parent
::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
37 $this->_type
= 'recaptcha';
38 if (!empty($attributes['https'])) {
39 $this->_https
= $attributes['https'];
44 * Returns the recaptcha element in HTML
52 require_once $CFG->libdir
. '/recaptchalib.php';
54 $html = '<script type="text/javascript">
55 var RecaptchaOptions = {
58 custom_theme_widget : \'recaptcha_widget\'
62 $attributes = $this->getAttributes();
63 if (empty($attributes['error_message'])) {
64 $attributes['error_message'] = null;
65 $this->setAttributes($attributes);
67 $error = $attributes['error_message'];
68 unset($attributes['error_message']);
70 $strincorrectpleasetryagain = get_string('incorrectpleasetryagain', 'auth');
71 $strenterthewordsabove = get_string('enterthewordsabove', 'auth');
72 $strenterthenumbersyouhear = get_string('enterthenumbersyouhear', 'auth');
73 $strgetanothercaptcha = get_string('getanothercaptcha', 'auth');
74 $strgetanaudiocaptcha = get_string('getanaudiocaptcha', 'auth');
75 $strgetanimagecaptcha = get_string('getanimagecaptcha', 'auth');
78 <div id="recaptcha_widget" style="display:none">
80 <div id="recaptcha_image"></div>
81 <div class="recaptcha_only_if_incorrect_sol" style="color:red">' . $strincorrectpleasetryagain . '</div>
83 <span class="recaptcha_only_if_image"><label for="recaptcha_response_field">' . $strenterthewordsabove . '</label></span>
84 <span class="recaptcha_only_if_audio"><label for="recaptcha_response_field">' . $strenterthenumbersyouhear . '</label></span>
86 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
87 <input type="hidden" name="recaptcha_element" value="dummyvalue" /> <!-- Dummy value to fool formslib -->
88 <div><a href="javascript:Recaptcha.reload()">' . $strgetanothercaptcha . '</a></div>
89 <div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type(\'audio\')">' . $strgetanaudiocaptcha . '</a></div>
90 <div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type(\'image\')">' . $strgetanimagecaptcha . '</a></div>
93 return $html . recaptcha_get_html($CFG->recaptchapublickey
, $error, $this->_https
);
97 * set html for help button
100 * @param array $help array of arguments to make a help button
101 * @param string $function function name to call to get html
103 function setHelpButton($helpbuttonargs, $function='helpbutton'){
104 if (!is_array($helpbuttonargs)){
105 $helpbuttonargs=array($helpbuttonargs);
107 $helpbuttonargs=$helpbuttonargs;
109 //we do this to to return html instead of printing it
110 //without having to specify it in every call to make a button.
111 if ('helpbutton' == $function){
112 $defaultargs=array('', '', 'moodle', true, false, '', true);
113 $helpbuttonargs=$helpbuttonargs +
$defaultargs ;
115 $this->_helpbutton
=call_user_func_array($function, $helpbuttonargs);
119 * get html for help button
122 * @return string html for help button
124 function getHelpButton(){
125 return $this->_helpbutton
;
128 function verify($challenge_field, $response_field) {
130 require_once $CFG->libdir
. '/recaptchalib.php';
131 $response = recaptcha_check_answer($CFG->recaptchaprivatekey
,
132 $_SERVER['REMOTE_ADDR'],
136 if (!$response->is_valid
) {
137 $attributes = $this->getAttributes();
138 $attributes['error_message'] = $response->error
;
139 $this->setAttributes($attributes);
140 return $response->error
;