Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403]

   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   * A script to display a ReCaptcha for the site.
  20   *
  21   * ReCaptcha V2 is restricted by domain, so it cannot be displayed in mobile and desktop apps.
  22   *
  23   * This script will display and initialize the reCaptcha, setting some empty callbacks by default. The client can override
  24   * those Javascript callbacks (in the "window" object).
  25   *
  26   * This script won't work in mobile and desktop apps unless $CFG->allowframembedding is enabled.
  27   *
  28   * @package    core_webservice
  29   * @copyright  2018 Dani Palou
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  
  33  define('NO_MOODLE_COOKIES', true);
  34  
  35  require_once(__DIR__ . '/../config.php');
  36  
  37  $lang = optional_param('lang', '', PARAM_LANG);
  38  
  39  $content = '';
  40  
  41  // Check that reCAPTCHA is configured.
  42  if (!empty($CFG->recaptchapublickey) && !empty($CFG->recaptchaprivatekey)) {
  43      require_once($CFG->libdir . '/recaptchalib_v2.php');
  44  
  45      $apiurl = RECAPTCHA_API_URL;
  46      $pubkey = $CFG->recaptchapublickey;
  47  
  48      $jscode = "
  49          // Create empty callbacks by default. They can be overridden by the client.
  50          var recaptchacallback = function() {};
  51          var recaptchaexpiredcallback = function() {};
  52          var recaptchaerrorcallback = function() {};
  53  
  54          var recaptchaloaded = function() {
  55              grecaptcha.render('recaptcha_element', {
  56                  'sitekey' : '$pubkey',
  57                  'callback' : 'recaptchacallback',
  58                  'expired-callback' : 'recaptchaexpiredcallback',
  59                  'error-callback' : 'recaptchaerrorcallback'
  60              });
  61          }";
  62  
  63      $lang = recaptcha_lang($lang);
  64  
  65      $apicode = "\n<script type=\"text/javascript\" ";
  66      $apicode .= "src=\"$apiurl?onload=recaptchaloaded&render=explicit&hl=$lang\" async defer>";
  67      $apicode .= "</script>\n";
  68  
  69      $content = html_writer::script($jscode, '');
  70      $content .= html_writer::div('', 'recaptcha_element', array('id' => 'recaptcha_element'));
  71      $content .= $apicode;
  72  } else {
  73      // To use reCAPTCHA you must have an API key.
  74      require_once($CFG->libdir . '/filelib.php');
  75      send_header_404();
  76      throw new \moodle_exception('cannotusepage2');
  77  }
  78  
  79  $output = <<<OET
  80  <html>
  81      <head>
  82          <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  83      </head>
  84      <body style="margin:0; padding:0">
  85          $content
  86      </body>
  87  </html>
  88  OET;
  89  echo $output;