Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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  require_once ('templatable_form_element.php');
  30  
  31  /**
  32   * recaptcha type form element
  33   *
  34   * HTML class for a recaptcha type element
  35   *
  36   * @package   core_form
  37   * @category  form
  38   * @copyright 2008 Nicolas Connault <nicolasconnault@gmail.com>
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class MoodleQuickForm_recaptcha extends HTML_QuickForm_input implements templatable {
  42      use templatable_form_element {
  43          export_for_template as export_for_template_base;
  44      }
  45  
  46      /** @var string html for help button, if empty then no help */
  47      var $_helpbutton='';
  48  
  49      /**
  50       * constructor
  51       *
  52       * @param string $elementName (optional) name of the recaptcha element
  53       * @param string $elementLabel (optional) label for recaptcha element
  54       * @param mixed $attributes (optional) Either a typical HTML attribute string
  55       *              or an associative array
  56       */
  57      public function __construct($elementName = null, $elementLabel = null, $attributes = null) {
  58          parent::__construct($elementName, $elementLabel, $attributes);
  59          $this->_type = 'recaptcha';
  60      }
  61  
  62      /**
  63       * Old syntax of class constructor. Deprecated in PHP7.
  64       *
  65       * @deprecated since Moodle 3.1
  66       */
  67      public function MoodleQuickForm_recaptcha($elementName = null, $elementLabel = null, $attributes = null) {
  68          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  69          self::__construct($elementName, $elementLabel, $attributes);
  70      }
  71  
  72      /**
  73       * Returns the reCAPTCHA element in HTML
  74       *
  75       * @return string The HTML to render
  76       */
  77      public function toHtml() {
  78          global $CFG;
  79          require_once($CFG->libdir . '/recaptchalib_v2.php');
  80  
  81          return recaptcha_get_challenge_html(RECAPTCHA_API_URL, $CFG->recaptchapublickey);
  82      }
  83  
  84      /**
  85       * get html for help button
  86       *
  87       * @return string html for help button
  88       */
  89      function getHelpButton(){
  90          return $this->_helpbutton;
  91      }
  92  
  93      /**
  94       * Checks recaptcha response with Google.
  95       *
  96       * @param string $responsestr
  97       * @return bool
  98       */
  99      public function verify($responsestr) {
 100          global $CFG;
 101          require_once($CFG->libdir . '/recaptchalib_v2.php');
 102  
 103          $response = recaptcha_check_response(RECAPTCHA_VERIFY_URL, $CFG->recaptchaprivatekey,
 104                                             getremoteaddr(), $responsestr);
 105          if (!$response['isvalid']) {
 106              $attributes = $this->getAttributes();
 107              $attributes['error_message'] = $response['error'];
 108              $this->setAttributes($attributes);
 109              return $response['error'];
 110          }
 111          return true;
 112      }
 113  
 114      public function export_for_template(renderer_base $output) {
 115          $context = $this->export_for_template_base($output);
 116          $context['html'] = $this->toHtml();
 117          return $context;
 118      }
 119  
 120      /**
 121       * Get force LTR option.
 122       *
 123       * @return bool
 124       */
 125      public function get_force_ltr() {
 126          return true;
 127      }
 128  
 129  }