[ Index ]

PHP Cross Reference of Unnamed Project

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 <nicolasconnault@gmail.com>
  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 <nicolasconnault@gmail.com>
  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      public function __construct($elementName = null, $elementLabel = null, $attributes = null) {
  57          global $CFG;
  58          parent::__construct($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       * Old syntax of class constructor. Deprecated in PHP7.
  69       *
  70       * @deprecated since Moodle 3.1
  71       */
  72      public function MoodleQuickForm_recaptcha($elementName = null, $elementLabel = null, $attributes = null) {
  73          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  74          self::__construct($elementName, $elementLabel, $attributes);
  75      }
  76  
  77      /**
  78       * Returns the recaptcha element in HTML
  79       *
  80       * @return string
  81       */
  82      function toHtml() {
  83          global $CFG, $PAGE;
  84          require_once $CFG->libdir . '/recaptchalib.php';
  85  
  86          $recaptureoptions = Array('theme'=>'custom', 'custom_theme_widget'=>'recaptcha_widget');
  87          $html = html_writer::script(js_writer::set_variable('RecaptchaOptions', $recaptureoptions));
  88  
  89          $attributes = $this->getAttributes();
  90          if (empty($attributes['error_message'])) {
  91              $attributes['error_message'] = null;
  92              $this->setAttributes($attributes);
  93          }
  94          $error = $attributes['error_message'];
  95          unset($attributes['error_message']);
  96  
  97          $strincorrectpleasetryagain = get_string('incorrectpleasetryagain', 'auth');
  98          $strenterthewordsabove = get_string('enterthewordsabove', 'auth');
  99          $strenterthenumbersyouhear = get_string('enterthenumbersyouhear', 'auth');
 100          $strgetanothercaptcha = get_string('getanothercaptcha', 'auth');
 101          $strgetanaudiocaptcha = get_string('getanaudiocaptcha', 'auth');
 102          $strgetanimagecaptcha = get_string('getanimagecaptcha', 'auth');
 103  
 104          $html .= '
 105  <div id="recaptcha_widget" style="display:none">
 106  
 107  <div id="recaptcha_image"></div>
 108  <div class="recaptcha_only_if_incorrect_sol" style="color:red">' . $strincorrectpleasetryagain . '</div>
 109  
 110  <span class="recaptcha_only_if_image"><label for="recaptcha_response_field">' . $strenterthewordsabove . '</label></span>
 111  <span class="recaptcha_only_if_audio"><label for="recaptcha_response_field">' . $strenterthenumbersyouhear . '</label></span>
 112  
 113  <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
 114  <input type="hidden" name="recaptcha_element" value="dummyvalue" /> <!-- Dummy value to fool formslib -->
 115  <div><a href="javascript:Recaptcha.reload()">' . $strgetanothercaptcha . '</a></div>
 116  <div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type(\'audio\')">' . $strgetanaudiocaptcha . '</a></div>
 117  <div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type(\'image\')">' . $strgetanimagecaptcha . '</a></div>
 118  </div>';
 119  
 120          return $html . recaptcha_get_html($CFG->recaptchapublickey, $error, $this->_https);
 121      }
 122  
 123      /**
 124       * get html for help button
 125       *
 126       * @return string html for help button
 127       */
 128      function getHelpButton(){
 129          return $this->_helpbutton;
 130      }
 131  
 132      /**
 133       * Checks input and challenged field
 134       *
 135       * @param string $challenge_field recaptcha shown  to user
 136       * @param string $response_field input value by user
 137       * @return bool
 138       */
 139      function verify($challenge_field, $response_field) {
 140          global $CFG;
 141          require_once $CFG->libdir . '/recaptchalib.php';
 142          $response = recaptcha_check_answer($CFG->recaptchaprivatekey,
 143                                             getremoteaddr(),
 144                                             $challenge_field,
 145                                             $response_field,
 146                                             $this->_https);
 147          if (!$response->is_valid) {
 148              $attributes = $this->getAttributes();
 149              $attributes['error_message'] = $response->error;
 150              $this->setAttributes($attributes);
 151              return $response->error;
 152          }
 153          return true;
 154      }
 155  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1