MDL-57281 question: Correct path to Quiz admin in test
[moodle.git] / webservice / xmlrpc / lib.php
blobdc3aef1c1d0622d1c6395eacb876b3148e70d65f
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 * Moodle XML-RPC library
20 * @package webservice_xmlrpc
21 * @copyright 2009 Jerome Mouneyrac
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Moodle XML-RPC client
30 * @package webservice_xmlrpc
31 * @copyright 2010 Jerome Mouneyrac
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class webservice_xmlrpc_client {
36 /** @var moodle_url The XML-RPC server url. */
37 protected $serverurl;
39 /** @var string The token for the XML-RPC call. */
40 protected $token;
42 /**
43 * Constructor
45 * @param string $serverurl a Moodle URL
46 * @param string $token the token used to do the web service call
48 public function __construct($serverurl, $token) {
49 $this->serverurl = new moodle_url($serverurl);
50 $this->token = $token;
53 /**
54 * Set the token used to do the XML-RPC call
56 * @param string $token the token used to do the web service call
58 public function set_token($token) {
59 $this->token = $token;
62 /**
63 * Execute client WS request with token authentication
65 * @param string $functionname the function name
66 * @param array $params An associative array containing the the parameters of the function being called.
67 * @return mixed The decoded XML RPC response.
68 * @throws moodle_exception
70 public function call($functionname, $params = array()) {
71 global $CFG;
72 require_once($CFG->libdir . '/filelib.php');
74 if ($this->token) {
75 $this->serverurl->param('wstoken', $this->token);
78 $request = $this->encode_request($functionname, $params);
80 // Set the headers.
81 $headers = array(
82 'Content-Length' => strlen($request),
83 'Content-Type' => 'text/xml; charset=utf-8',
84 'Host' => $this->serverurl->get_host(),
85 'User-Agent' => 'Moodle XML-RPC Client/1.0',
88 // Get the response.
89 $response = download_file_content($this->serverurl->out(false), $headers, $request);
91 // Decode the response.
92 $result = $this->decode_response($response);
93 if (is_array($result) && xmlrpc_is_fault($result)) {
94 throw new Exception($result['faultString'], $result['faultCode']);
97 return $result;
101 * Generates XML for a method request.
103 * @param string $functionname Name of the method to call.
104 * @param mixed $params Method parameters compatible with the method signature.
105 * @return string
107 protected function encode_request($functionname, $params) {
109 $outputoptions = array(
110 'encoding' => 'utf-8',
111 'escaping' => 'markup',
114 // See MDL-53962 - needed for backwards compatibility on <= 3.0.
115 $params = array_values($params);
117 return xmlrpc_encode_request($functionname, $params, $outputoptions);
121 * Parses and decodes the response XML
123 * @param string $response
124 * @return array
126 protected function decode_response($response) {
127 // XMLRPC server in Moodle encodes response using function xmlrpc_encode_request() with method==null
128 // see {@link webservice_xmlrpc_server::prepare_response()} . We should use xmlrpc_decode_request() for decoding too.
129 $method = null;
130 $encoding = null;
131 if (preg_match('/^<\?xml version="1.0" encoding="([^"]*)"\?>/', $response, $matches)) {
132 // Sometimes xmlrpc_decode_request() fails to recognise encoding, let's help it.
133 $encoding = $matches[1];
135 $r = xmlrpc_decode_request($response, $method, $encoding);
136 return $r;