Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/form/recaptcha.php
Go to the documentation of this file.
00001 <?php
00002 require_once('HTML/QuickForm/input.php');
00003 
00004 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
00005 
00019 class MoodleQuickForm_recaptcha extends HTML_QuickForm_input {
00020 
00026     var $_helpbutton='';
00027 
00028     var $_https=false;
00029 
00036     function MoodleQuickForm_recaptcha($elementName = null, $elementLabel = null, $attributes = null) {
00037         global $CFG;
00038         parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
00039         $this->_type = 'recaptcha';
00040         if (!empty($attributes['https']) or strpos($CFG->httpswwwroot, 'https:') === 0) {
00041             $this->_https = true;
00042         } else {
00043             $this->_https = false;
00044         }
00045     }
00046 
00054     function toHtml() {
00055         global $CFG, $PAGE;
00056         require_once $CFG->libdir . '/recaptchalib.php';
00057 
00058         $recaptureoptions = Array('theme'=>'custom', 'custom_theme_widget'=>'recaptcha_widget');
00059         $html = html_writer::script(js_writer::set_variable('RecaptchaOptions', $recaptureoptions));
00060 
00061         $attributes = $this->getAttributes();
00062         if (empty($attributes['error_message'])) {
00063             $attributes['error_message'] = null;
00064             $this->setAttributes($attributes);
00065         }
00066         $error = $attributes['error_message'];
00067         unset($attributes['error_message']);
00068 
00069         $strincorrectpleasetryagain = get_string('incorrectpleasetryagain', 'auth');
00070         $strenterthewordsabove = get_string('enterthewordsabove', 'auth');
00071         $strenterthenumbersyouhear = get_string('enterthenumbersyouhear', 'auth');
00072         $strgetanothercaptcha = get_string('getanothercaptcha', 'auth');
00073         $strgetanaudiocaptcha = get_string('getanaudiocaptcha', 'auth');
00074         $strgetanimagecaptcha = get_string('getanimagecaptcha', 'auth');
00075 
00076         $html .= '
00077 <div id="recaptcha_widget" style="display:none">
00078 
00079 <div id="recaptcha_image"></div>
00080 <div class="recaptcha_only_if_incorrect_sol" style="color:red">' . $strincorrectpleasetryagain . '</div>
00081 
00082 <span class="recaptcha_only_if_image"><label for="recaptcha_response_field">' . $strenterthewordsabove . '</label></span>
00083 <span class="recaptcha_only_if_audio"><label for="recaptcha_response_field">' . $strenterthenumbersyouhear . '</label></span>
00084 
00085 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
00086 <input type="hidden" name="recaptcha_element" value="dummyvalue" /> <!-- Dummy value to fool formslib -->
00087 <div><a href="javascript:Recaptcha.reload()">' . $strgetanothercaptcha . '</a></div>
00088 <div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type(\'audio\')">' . $strgetanaudiocaptcha . '</a></div>
00089 <div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type(\'image\')">' . $strgetanimagecaptcha . '</a></div>
00090 </div>';
00091 
00092         return $html . recaptcha_get_html($CFG->recaptchapublickey, $error, $this->_https);
00093     }
00094 
00102     function setHelpButton($helpbuttonargs, $function='helpbutton'){
00103         debugging('component setHelpButton() is not used any more, please use $mform->setHelpButton() instead');
00104     }
00105 
00112     function getHelpButton(){
00113         return $this->_helpbutton;
00114     }
00115 
00116     function verify($challenge_field, $response_field) {
00117         global $CFG;
00118         require_once $CFG->libdir . '/recaptchalib.php';
00119         $response = recaptcha_check_answer($CFG->recaptchaprivatekey,
00120                                            getremoteaddr(),
00121                                            $challenge_field,
00122                                            $response_field,
00123                                            $this->_https);
00124         if (!$response->is_valid) {
00125             $attributes = $this->getAttributes();
00126             $attributes['error_message'] = $response->error;
00127             $this->setAttributes($attributes);
00128             return $response->error;
00129         }
00130         return true;
00131     }
00132 }
 All Data Structures Namespaces Files Functions Variables Enumerations