Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 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   * This file is used to call any registered externallib function in Moodle.
  19   *
  20   * It will process more than one request and return more than one response if required.
  21   * It is recommended to add webservice functions and re-use this script instead of
  22   * writing any new custom ajax scripts.
  23   *
  24   * @since Moodle 2.9
  25   * @package core
  26   * @copyright 2015 Damyon Wiese
  27   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  
  30  define('AJAX_SCRIPT', true);
  31  // Services can declare 'readonlysession' in their config located in db/services.php, if not present will default to false.
  32  define('READ_ONLY_SESSION', true);
  33  
  34  if (!empty($_GET['nosessionupdate'])) {
  35      define('NO_SESSION_UPDATE', true);
  36  }
  37  
  38  require_once(__DIR__ . '/../../config.php');
  39  require_once($CFG->libdir . '/externallib.php');
  40  
  41  define('PREFERRED_RENDERER_TARGET', RENDERER_TARGET_GENERAL);
  42  
  43  $arguments = '';
  44  $cacherequest = false;
  45  if (defined('ALLOW_GET_PARAMETERS')) {
  46      $arguments = optional_param('args', '', PARAM_RAW);
  47      $cachekey = optional_param('cachekey', '', PARAM_INT);
  48      if ($cachekey && $cachekey > 0 && $cachekey <= time()) {
  49          $cacherequest = true;
  50      }
  51  }
  52  
  53  // Either we are not allowing GET parameters or we didn't use GET because
  54  // we did not pass a cache key or the URL was too long.
  55  if (empty($arguments)) {
  56      $arguments = file_get_contents('php://input');
  57  }
  58  
  59  $requests = json_decode($arguments, true);
  60  
  61  if ($requests === null) {
  62      $lasterror = json_last_error_msg();
  63      throw new coding_exception('Invalid json in request: ' . $lasterror);
  64  }
  65  $responses = array();
  66  
  67  // Defines the external settings required for Ajax processing.
  68  $settings = external_settings::get_instance();
  69  $settings->set_file('pluginfile.php');
  70  $settings->set_fileurl(true);
  71  $settings->set_filter(true);
  72  $settings->set_raw(false);
  73  
  74  $haserror = false;
  75  foreach ($requests as $request) {
  76      $response = array();
  77      $methodname = clean_param($request['methodname'], PARAM_ALPHANUMEXT);
  78      $index = clean_param($request['index'], PARAM_INT);
  79      $args = $request['args'];
  80  
  81      $response = external_api::call_external_function($methodname, $args, true);
  82      $responses[$index] = $response;
  83  
  84      if ($response['error']) {
  85          $haserror = true;
  86          if (!NO_MOODLE_COOKIES) {
  87              // If there was an error, and this HTTP request includes a Moodle cookie (and therefore a login), reject all
  88              // subsequent changes.
  89              //
  90              // The reason for this is that an earlier step may be performing a dependant action. Consider the following:
  91              // 1) Backup a thing
  92              // 2) Reset the thing to its initial state
  93              // 3) Restore the thing from the backup made in step 1.
  94              //
  95              // In the above example you do not want steps 2 and 3 to happen if step 1 fails.
  96              // Do not process the remaining requests.
  97  
  98              // If the request came through service-nologin.php which does not allow any kind of login,
  99              // then it is not possible to make changes to the DB, session, site, etc.
 100              // For all other cases, we *MUST* stop processing subsequent requests.
 101              break;
 102          }
 103      }
 104  }
 105  
 106  if ($cacherequest && !$haserror) {
 107      // 90 days only - based on Moodle point release cadence being every 3 months.
 108      $lifetime = 60 * 60 * 24 * 90;
 109  
 110      header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
 111      header('Pragma: ');
 112      header('Cache-Control: public, max-age=' . $lifetime . ', immutable');
 113      header('Accept-Ranges: none');
 114  }
 115  
 116  echo json_encode($responses);