Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
/lib/ -> soaplib.php (source)
   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Web services wrapper library script
  20   *
  21   * Since Moodle 2.0 we rely only on native PHP Soap extension,
  22   * the original name of this file was lib/soap/phpsoap.php
  23   *
  24   * @package    core
  25   * @subpackage lib
  26   * @author     Alex Smith and others members of the Serving Mathematics project
  27   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   *             {@link http://maths.york.ac.uk/serving_maths}
  29   *             and others
  30   */
  31  
  32  defined('MOODLE_INTERNAL') || die();
  33  
  34  /**
  35  * Create a new SoapClient object
  36  *
  37  * @param string $wsdl   URI of the WSDL file
  38  * @param boolean $trace indicates if the soap messages should be saved (i.e. if
  39  *                       get_soap_messages is used) and should be used only for debugging
  40  * @return mixed         Returns either a SoapClient object or, if the connection failed,
  41  *                       a SoapFault object.
  42  */
  43  function soap_connect($wsdl, $trace=false) {
  44      try {
  45          $connection = new SoapClient($wsdl, array('soap_version'=>SOAP_1_1, 'exceptions'=>true, 'trace'=>$trace));
  46      }
  47      catch (SoapFault $f) {
  48          $connection = $f;
  49      }
  50      catch (Exception $e) {
  51          $connection = new SoapFault('client', 'Could not connect to the service');
  52      }
  53      return $connection;
  54  }
  55  
  56  /**
  57  * Make a call to a SoapClient
  58  *
  59  * @param SoapClient $connection  The SoapClient to call
  60  * @param string $call            Operation to be performed by client
  61  * @param array $params           Parameters for the call
  62  * @return mixed                  The return parameters of the operation or a SoapFault
  63  *                                If the operation returned several parameters then these
  64  *                                are returned as an object rather than an array
  65  */
  66  function soap_call($connection, $call, $params) {
  67      try {
  68          $return = $connection->__soapCall($call, $params);
  69      }
  70      catch (SoapFault $f) {
  71          $return = $f;
  72      }
  73      catch (Exception $e) {
  74          $return = new SoapFault('client', 'Could call the method');
  75      }
  76      // return multiple parameters using an object rather than an array
  77      if (is_array($return)) {
  78          $keys = array_keys($return);
  79          $assoc = true;
  80          foreach ($keys as $key) {
  81              if (!is_string($key)) {
  82                  $assoc = false;
  83                  break;
  84              }
  85          }
  86          if ($assoc)
  87              $return = (object) $return;
  88      }
  89      return $return;
  90  }
  91  
  92  function soap_serve($wsdl, $functions) {
  93      // create server object
  94      $s = new SoapServer($wsdl);
  95      // export functions
  96      foreach ($functions as $func)
  97          $s->addFunction($func);
  98      // handle the request
  99      $s->handle();
 100  }
 101  
 102  function make_soap_fault($faultcode, $faultstring, $faultactor='', $detail='', $faultname='', $headerfault='') {
 103      return new SoapFault($faultcode, $faultstring, $faultactor, $detail, $faultname, $headerfault);
 104  }
 105  
 106  function get_last_soap_messages($connection) {
 107      return array('request'=>$connection->__getLastRequest(), 'response'=>$connection->__getLastResponse());
 108  }
 109  
 110  // Fix simple type encoding - work around a bug in early versions of PHP5 < 5.0.3, see http://bugs.php.net/bug.php?id=31832
 111  function soap_encode($value, $name, $type, $namespace, $encode=XSD_STRING) {
 112      $value = new SoapVar($value, $encode, $type, $namespace);
 113      if ('' === $name)
 114          return $value;
 115      return new SoapParam($value, $name);
 116  }
 117  
 118  // Fix complex type encoding - work around a bug in early versions of PHP5 < 5.0.3, see http://bugs.php.net/bug.php?id=31832
 119  function soap_encode_object($value, $name, $type, $namespace) {
 120      if (!is_object($value))
 121          return $value;
 122      $value = new SoapVar($value, SOAP_ENC_OBJECT, $type, $namespace);
 123      if ('' === $name)
 124          return $value;
 125      return new SoapParam($value, $name);
 126  }
 127  
 128  // Fix array encoding - work around a bug in early versions of PHP5 < 5.0.3, see http://bugs.php.net/bug.php?id=31832
 129  function soap_encode_array($value, $name, $type, $namespace) {
 130      if (!is_array($value))
 131          return $value;
 132      $value = new SoapVar($value, SOAP_ENC_ARRAY, 'ArrayOf' . $type, $namespace);
 133      if ('' === $name)
 134          return $value;
 135      return new SoapParam($value, $name);
 136  }
 137  
 138  // In both cases...
 139  function handle_soap_wsdl_request($wsdlfile, $address=false) {
 140      header('Content-type: application/wsdl+xml');
 141      $wsdl = file_get_contents($wsdlfile);
 142      if (false !== $address) {
 143          if (true === $address) {
 144              $address = (($_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://') . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'];
 145          }
 146          $wsdl = str_replace('###SERVER_ADDRESS###', $address, $wsdl);
 147      }
 148      echo $wsdl;
 149      exit;
 150  }