[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/form/ -> recaptcha.php (source)

   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.
  13  //
  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/>.
  16  
  17  
  18  /**
  19   * recaptcha type form element
  20   *
  21   * Contains HTML class for a recaptcha type element
  22   *
  23   * @package   core_form
  24   * @copyright 2008 Nicolas Connault <[email protected]>
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  require_once('HTML/QuickForm/input.php');
  29  
  30  /**
  31   * recaptcha type form element
  32   *
  33   * HTML class for a recaptcha type element
  34   *
  35   * @package   core_form
  36   * @category  form
  37   * @copyright 2008 Nicolas Connault <[email protected]>
  38   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class MoodleQuickForm_recaptcha extends HTML_QuickForm_input {
  41  
  42      /** @var string html for help button, if empty then no help */
  43      var $_helpbutton='';
  44  
  45      /** @var bool if true, recaptcha will be servered from https */
  46      var $_https=false;
  47  
  48      /**
  49       * constructor
  50       *
  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
  55       */
  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;
  64          }
  65      }
  66  
  67      /**
  68       * Returns the recaptcha element in HTML
  69       *
  70       * @return string
  71       */
  72      function toHtml() {
  73          global $CFG, $PAGE;
  74          require_once $CFG->libdir . '/recaptchalib.php';
  75  
  76          $recaptureoptions = Array('theme'=>'custom', 'custom_theme_widget'=>'recaptcha_widget');
  77          $html = html_writer::script(js_writer::set_variable('RecaptchaOptions', $recaptureoptions));
  78  
  79          $attributes = $this->getAttributes();
  80          if (empty($attributes['error_message'])) {
  81              $attributes['error_message'] = null;
  82              $this->setAttributes($attributes);
  83          }
  84          $error = $attributes['error_message'];
  85          unset($attributes['error_message']);
  86  
  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');
  93  
  94          $html .= '
  95  <div id="recaptcha_widget" style="display:none">
  96  
  97  <div id="recaptcha_image"></div>
  98  <div class="recaptcha_only_if_incorrect_sol" style="color:red">' . $strincorrectpleasetryagain . '</div>
  99  
 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>
 102  
 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>';
 109  
 110          return $html . recaptcha_get_html($CFG->recaptchapublickey, $error, $this->_https);
 111      }
 112  
 113      /**
 114       * get html for help button
 115       *
 116       * @return string html for help button
 117       */
 118      function getHelpButton(){
 119          return $this->_helpbutton;
 120      }
 121  
 122      /**
 123       * Checks input and challenged field
 124       *
 125       * @param string $challenge_field recaptcha shown  to user
 126       * @param string $response_field input value by user
 127       * @return bool
 128       */
 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;
 142          }
 143          return true;
 144      }
 145  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1