Commit | Line | Data |
---|---|---|
72f8324e DW |
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 | ||
32 | require_once(dirname(__FILE__) . '/../../config.php'); | |
33 | require_once($CFG->libdir . '/externallib.php'); | |
34 | ||
72f8324e DW |
35 | $rawjson = file_get_contents('php://input'); |
36 | ||
37 | $requests = json_decode($rawjson, true); | |
38 | if ($requests === null) { | |
39 | $lasterror = json_last_error_msg(); | |
40 | throw new coding_exception('Invalid json in request: ' . $lasterror); | |
41 | } | |
42 | $responses = array(); | |
43 | ||
44 | ||
45 | foreach ($requests as $request) { | |
46 | $response = array(); | |
47 | $methodname = clean_param($request['methodname'], PARAM_ALPHANUMEXT); | |
48 | $index = clean_param($request['index'], PARAM_INT); | |
49 | $args = $request['args']; | |
50 | ||
51 | try { | |
52 | $externalfunctioninfo = external_function_info($methodname); | |
53 | ||
54 | if (!$externalfunctioninfo->allowed_from_ajax) { | |
ba224fb4 | 55 | error_log('This external function is not available to ajax. Failed to call "' . $methodname . '"'); |
72f8324e DW |
56 | throw new moodle_exception('servicenotavailable', 'webservice'); |
57 | } | |
58 | ||
5d8c1987 DW |
59 | // Do not allow access to write or delete webservices as a public user. |
60 | if ($externalfunctioninfo->loginrequired) { | |
61 | if (!isloggedin()) { | |
62 | error_log('This external function is not available to public users. Failed to call "' . $methodname . '"'); | |
63 | throw new moodle_exception('servicenotavailable', 'webservice'); | |
ba224fb4 DW |
64 | } else { |
65 | require_sesskey(); | |
5d8c1987 DW |
66 | } |
67 | } | |
68 | ||
72f8324e DW |
69 | // Validate params, this also sorts the params properly, we need the correct order in the next part. |
70 | $callable = array($externalfunctioninfo->classname, 'validate_parameters'); | |
71 | $params = call_user_func($callable, | |
72 | $externalfunctioninfo->parameters_desc, | |
73 | $args); | |
74 | ||
75 | // Execute - gulp! | |
76 | $callable = array($externalfunctioninfo->classname, $externalfunctioninfo->methodname); | |
77 | $result = call_user_func_array($callable, | |
78 | array_values($params)); | |
79 | ||
80 | $response['error'] = false; | |
81 | $response['data'] = $result; | |
82 | $responses[$index] = $response; | |
83 | } catch (Exception $e) { | |
84 | $jsonexception = get_exception_info($e); | |
85 | unset($jsonexception->a); | |
86 | if (!debugging('', DEBUG_DEVELOPER)) { | |
87 | unset($jsonexception->debuginfo); | |
88 | unset($jsonexception->backtrace); | |
89 | } | |
90 | $response['error'] = true; | |
91 | $response['exception'] = $jsonexception; | |
92 | $responses[$index] = $response; | |
93 | // Do not process the remaining requests. | |
94 | break; | |
95 | } | |
96 | } | |
97 | ||
98 | echo json_encode($responses); |