Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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   * Moodle XML-RPC library
  19   *
  20   * @package    webservice_xmlrpc
  21   * @copyright  2009 Jerome Mouneyrac
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Moodle XML-RPC client
  29   *
  30   * @package    webservice_xmlrpc
  31   * @copyright  2010 Jerome Mouneyrac
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class webservice_xmlrpc_client {
  35  
  36      /** @var moodle_url The XML-RPC server url. */
  37      protected $serverurl;
  38  
  39      /** @var string The token for the XML-RPC call. */
  40      protected $token;
  41  
  42      /**
  43       * Constructor
  44       *
  45       * @param string $serverurl a Moodle URL
  46       * @param string $token the token used to do the web service call
  47       */
  48      public function __construct($serverurl, $token) {
  49          $this->serverurl = new moodle_url($serverurl);
  50          $this->token = $token;
  51      }
  52  
  53      /**
  54       * Set the token used to do the XML-RPC call
  55       *
  56       * @param string $token the token used to do the web service call
  57       */
  58      public function set_token($token) {
  59          $this->token = $token;
  60      }
  61  
  62      /**
  63       * Execute client WS request with token authentication
  64       *
  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
  69       */
  70      public function call($functionname, $params = array()) {
  71          global $CFG;
  72          require_once($CFG->libdir . '/filelib.php');
  73  
  74          if ($this->token) {
  75              $this->serverurl->param('wstoken', $this->token);
  76          }
  77  
  78          $request = $this->encode_request($functionname, $params);
  79  
  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',
  86          );
  87  
  88          // Get the response.
  89          $response = download_file_content($this->serverurl->out(false), $headers, $request);
  90  
  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']);
  95          }
  96  
  97          return $result;
  98      }
  99  
 100      /**
 101       * Generates XML for a method request.
 102       *
 103       * @param string $functionname Name of the method to call.
 104       * @param mixed $params Method parameters compatible with the method signature.
 105       * @return string
 106       */
 107      protected function encode_request($functionname, $params) {
 108  
 109          $outputoptions = array(
 110              'encoding' => 'utf-8',
 111              'escaping' => 'markup',
 112          );
 113  
 114          // See MDL-53962 - needed for backwards compatibility on <= 3.0.
 115          $params = array_values($params);
 116  
 117          return xmlrpc_encode_request($functionname, $params, $outputoptions);
 118      }
 119  
 120      /**
 121       * Parses and decodes the response XML
 122       *
 123       * @param string $response
 124       * @return array
 125       */
 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];
 134          }
 135          $r = xmlrpc_decode_request($response, $method, $encoding);
 136          return $r;
 137      }
 138  }