| 1 | efrain | 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 | /**
 | 
        
           | 1441 | ariadna | 19 |  * Web services wrapper library script.
 | 
        
           | 1 | efrain | 20 |  *
 | 
        
           |  |  | 21 |  * @package    core
 | 
        
           |  |  | 22 |  * @subpackage lib
 | 
        
           |  |  | 23 |  * @author     Alex Smith and others members of the Serving Mathematics project
 | 
        
           |  |  | 24 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 25 |  *             {@link http://maths.york.ac.uk/serving_maths}
 | 
        
           |  |  | 26 |  *             and others
 | 
        
           | 1441 | ariadna | 27 |  * @deprecated Moodle 4.5
 | 
        
           |  |  | 28 |  * @todo MDL-82194 Remove this file.
 | 
        
           | 1 | efrain | 29 |  */
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 | defined('MOODLE_INTERNAL') || die();
 | 
        
           |  |  | 32 |   | 
        
           | 1441 | ariadna | 33 | debugging(
 | 
        
           |  |  | 34 |     'The soaplib.php file is deprecated and should not be used any more. ',
 | 
        
           |  |  | 35 |     DEBUG_DEVELOPER,
 | 
        
           |  |  | 36 | );
 | 
        
           |  |  | 37 |   | 
        
           | 1 | efrain | 38 | /**
 | 
        
           |  |  | 39 | * Create a new SoapClient object
 | 
        
           |  |  | 40 | *
 | 
        
           |  |  | 41 | * @param string $wsdl   URI of the WSDL file
 | 
        
           |  |  | 42 | * @param boolean $trace indicates if the soap messages should be saved (i.e. if
 | 
        
           |  |  | 43 | *                       get_soap_messages is used) and should be used only for debugging
 | 
        
           |  |  | 44 | * @return mixed         Returns either a SoapClient object or, if the connection failed,
 | 
        
           |  |  | 45 | *                       a SoapFault object.
 | 
        
           |  |  | 46 | */
 | 
        
           |  |  | 47 | function soap_connect($wsdl, $trace=false) {
 | 
        
           |  |  | 48 |     try {
 | 
        
           |  |  | 49 |         $connection = new SoapClient($wsdl, array('soap_version'=>SOAP_1_1, 'exceptions'=>true, 'trace'=>$trace));
 | 
        
           |  |  | 50 |     }
 | 
        
           |  |  | 51 |     catch (SoapFault $f) {
 | 
        
           |  |  | 52 |         $connection = $f;
 | 
        
           |  |  | 53 |     }
 | 
        
           |  |  | 54 |     catch (Exception $e) {
 | 
        
           |  |  | 55 |         $connection = new SoapFault('client', 'Could not connect to the service');
 | 
        
           |  |  | 56 |     }
 | 
        
           |  |  | 57 |     return $connection;
 | 
        
           |  |  | 58 | }
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 | /**
 | 
        
           |  |  | 61 | * Make a call to a SoapClient
 | 
        
           |  |  | 62 | *
 | 
        
           |  |  | 63 | * @param SoapClient $connection  The SoapClient to call
 | 
        
           |  |  | 64 | * @param string $call            Operation to be performed by client
 | 
        
           |  |  | 65 | * @param array $params           Parameters for the call
 | 
        
           |  |  | 66 | * @return mixed                  The return parameters of the operation or a SoapFault
 | 
        
           |  |  | 67 | *                                If the operation returned several parameters then these
 | 
        
           |  |  | 68 | *                                are returned as an object rather than an array
 | 
        
           |  |  | 69 | */
 | 
        
           |  |  | 70 | function soap_call($connection, $call, $params) {
 | 
        
           |  |  | 71 |     try {
 | 
        
           |  |  | 72 |         $return = $connection->__soapCall($call, $params);
 | 
        
           |  |  | 73 |     }
 | 
        
           |  |  | 74 |     catch (SoapFault $f) {
 | 
        
           |  |  | 75 |         $return = $f;
 | 
        
           |  |  | 76 |     }
 | 
        
           |  |  | 77 |     catch (Exception $e) {
 | 
        
           |  |  | 78 |         $return = new SoapFault('client', 'Could call the method');
 | 
        
           |  |  | 79 |     }
 | 
        
           |  |  | 80 |     // return multiple parameters using an object rather than an array
 | 
        
           |  |  | 81 |     if (is_array($return)) {
 | 
        
           |  |  | 82 |         $keys = array_keys($return);
 | 
        
           |  |  | 83 |         $assoc = true;
 | 
        
           |  |  | 84 |         foreach ($keys as $key) {
 | 
        
           |  |  | 85 |             if (!is_string($key)) {
 | 
        
           |  |  | 86 |                 $assoc = false;
 | 
        
           |  |  | 87 |                 break;
 | 
        
           |  |  | 88 |             }
 | 
        
           |  |  | 89 |         }
 | 
        
           |  |  | 90 |         if ($assoc)
 | 
        
           |  |  | 91 |             $return = (object) $return;
 | 
        
           |  |  | 92 |     }
 | 
        
           |  |  | 93 |     return $return;
 | 
        
           |  |  | 94 | }
 | 
        
           |  |  | 95 |   | 
        
           |  |  | 96 | function soap_serve($wsdl, $functions) {
 | 
        
           |  |  | 97 |     // create server object
 | 
        
           |  |  | 98 |     $s = new SoapServer($wsdl);
 | 
        
           |  |  | 99 |     // export functions
 | 
        
           |  |  | 100 |     foreach ($functions as $func)
 | 
        
           |  |  | 101 |         $s->addFunction($func);
 | 
        
           |  |  | 102 |     // handle the request
 | 
        
           |  |  | 103 |     $s->handle();
 | 
        
           |  |  | 104 | }
 | 
        
           |  |  | 105 |   | 
        
           |  |  | 106 | function make_soap_fault($faultcode, $faultstring, $faultactor='', $detail='', $faultname='', $headerfault='') {
 | 
        
           |  |  | 107 |     return new SoapFault($faultcode, $faultstring, $faultactor, $detail, $faultname, $headerfault);
 | 
        
           |  |  | 108 | }
 | 
        
           |  |  | 109 |   | 
        
           |  |  | 110 | function get_last_soap_messages($connection) {
 | 
        
           |  |  | 111 |     return array('request'=>$connection->__getLastRequest(), 'response'=>$connection->__getLastResponse());
 | 
        
           |  |  | 112 | }
 | 
        
           |  |  | 113 |   | 
        
           |  |  | 114 | // 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
 | 
        
           |  |  | 115 | function soap_encode($value, $name, $type, $namespace, $encode=XSD_STRING) {
 | 
        
           |  |  | 116 |     $value = new SoapVar($value, $encode, $type, $namespace);
 | 
        
           |  |  | 117 |     if ('' === $name)
 | 
        
           |  |  | 118 |         return $value;
 | 
        
           |  |  | 119 |     return new SoapParam($value, $name);
 | 
        
           |  |  | 120 | }
 | 
        
           |  |  | 121 |   | 
        
           |  |  | 122 | // 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
 | 
        
           |  |  | 123 | function soap_encode_object($value, $name, $type, $namespace) {
 | 
        
           |  |  | 124 |     if (!is_object($value))
 | 
        
           |  |  | 125 |         return $value;
 | 
        
           |  |  | 126 |     $value = new SoapVar($value, SOAP_ENC_OBJECT, $type, $namespace);
 | 
        
           |  |  | 127 |     if ('' === $name)
 | 
        
           |  |  | 128 |         return $value;
 | 
        
           |  |  | 129 |     return new SoapParam($value, $name);
 | 
        
           |  |  | 130 | }
 | 
        
           |  |  | 131 |   | 
        
           |  |  | 132 | // Fix array encoding - work around a bug in early versions of PHP5 < 5.0.3, see http://bugs.php.net/bug.php?id=31832
 | 
        
           |  |  | 133 | function soap_encode_array($value, $name, $type, $namespace) {
 | 
        
           |  |  | 134 |     if (!is_array($value))
 | 
        
           |  |  | 135 |         return $value;
 | 
        
           |  |  | 136 |     $value = new SoapVar($value, SOAP_ENC_ARRAY, 'ArrayOf' . $type, $namespace);
 | 
        
           |  |  | 137 |     if ('' === $name)
 | 
        
           |  |  | 138 |         return $value;
 | 
        
           |  |  | 139 |     return new SoapParam($value, $name);
 | 
        
           |  |  | 140 | }
 | 
        
           |  |  | 141 |   | 
        
           |  |  | 142 | // In both cases...
 | 
        
           |  |  | 143 | function handle_soap_wsdl_request($wsdlfile, $address=false) {
 | 
        
           |  |  | 144 |     header('Content-type: application/wsdl+xml');
 | 
        
           |  |  | 145 |     $wsdl = file_get_contents($wsdlfile);
 | 
        
           |  |  | 146 |     if (false !== $address) {
 | 
        
           |  |  | 147 |         if (true === $address) {
 | 
        
           |  |  | 148 |             $address = (($_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://') . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'];
 | 
        
           |  |  | 149 |         }
 | 
        
           |  |  | 150 |         $wsdl = str_replace('###SERVER_ADDRESS###', $address, $wsdl);
 | 
        
           |  |  | 151 |     }
 | 
        
           |  |  | 152 |     echo $wsdl;
 | 
        
           |  |  | 153 |     exit;
 | 
        
           |  |  | 154 | }
 |