Merge branch 'MDL-75012-39-5' of https://github.com/andrewnicols/moodle into MOODLE_3...
[moodle.git] / lib / ajax / service.php
blob977b5e4a4b875b4dc8e611a0168c968c71246896
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.
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/>.
17 /**
18 * This file is used to call any registered externallib function in Moodle.
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.
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
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);
34 if (!empty($_GET['nosessionupdate'])) {
35 define('NO_SESSION_UPDATE', true);
38 require_once(__DIR__ . '/../../config.php');
39 require_once($CFG->libdir . '/externallib.php');
41 define('PREFERRED_RENDERER_TARGET', RENDERER_TARGET_GENERAL);
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;
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');
59 $requests = json_decode($arguments, true);
61 if ($requests === null) {
62 $lasterror = json_last_error_msg();
63 throw new coding_exception('Invalid json in request: ' . $lasterror);
65 $responses = array();
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);
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'];
81 $response = external_api::call_external_function($methodname, $args, true);
82 $responses[$index] = $response;
83 if ($response['error']) {
84 // Do not process the remaining requests.
85 $haserror = true;
86 break;
90 if ($cacherequest && !$haserror) {
91 // 90 days only - based on Moodle point release cadence being every 3 months.
92 $lifetime = 60 * 60 * 24 * 90;
94 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
95 header('Pragma: ');
96 header('Cache-Control: public, max-age=' . $lifetime . ', immutable');
97 header('Accept-Ranges: none');
100 echo json_encode($responses);