3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * SOAP web service implementation classes and methods.
22 * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once("$CFG->dirroot/webservice/lib.php");
29 * SOAP service server implementation.
30 * @author Petr Skoda (skodak)
32 class webservice_soap_server extends webservice_zend_server {
35 * @param bool $simple use simple authentication
37 public function __construct($authmethod) {
38 // must not cache wsdl - the list of functions is created on the fly
39 ini_set('soap.wsdl_cache_enabled', '0');
40 require_once 'Zend/Soap/Server.php';
41 require_once 'Zend/Soap/AutoDiscover.php';
43 if (optional_param('wsdl', 0, PARAM_BOOL)) {
44 parent::__construct($authmethod, 'Zend_Soap_AutoDiscover');
46 parent::__construct($authmethod, 'Zend_Soap_Server');
48 $this->wsname = 'soap';
52 * Set up zend service class
55 protected function init_zend_server() {
58 parent::init_zend_server();
60 if ($this->authmethod == WEBSERVICE_AUTHMETHOD_USERNAME) {
61 $username = optional_param('wsusername', '', PARAM_RAW);
62 $password = optional_param('wspassword', '', PARAM_RAW);
63 // aparently some clients and zend soap server does not work well with "&" in urls :-(
64 //TODO: the zend error has been fixed in the last Zend SOAP version, check that is fixed and remove obsolete code
65 $url = $CFG->wwwroot.'/webservice/soap/simpleserver.php/'.urlencode($username).'/'.urlencode($password);
66 // the Zend server is using this uri directly in xml - weird :-(
67 $this->zend_server->setUri(htmlentities($url));
69 $wstoken = optional_param('wstoken', '', PARAM_RAW);
70 $url = $CFG->wwwroot.'/webservice/soap/server.php?wstoken='.urlencode($wstoken);
71 // the Zend server is using this uri directly in xml - weird :-(
72 $this->zend_server->setUri(htmlentities($url));
75 if (!optional_param('wsdl', 0, PARAM_BOOL)) {
76 $this->zend_server->setReturnResponse(true);
77 //TODO: the error handling in Zend Soap server is useless, XML-RPC is much, much better :-(
78 $this->zend_server->registerFaultException('moodle_exception');
79 $this->zend_server->registerFaultException('webservice_parameter_exception'); //deprecated since Moodle 2.2 - kept for backward compatibility
80 $this->zend_server->registerFaultException('invalid_parameter_exception');
81 $this->zend_server->registerFaultException('invalid_response_exception');
86 * This method parses the $_REQUEST superglobal and looks for
87 * the following information:
88 * 1/ user authentication - username+password or token (wsusername, wspassword and wstoken parameters)
92 protected function parse_request() {
93 parent::parse_request();
95 if (!$this->username or !$this->password) {
96 //note: this is the workaround for the trouble with & in soap urls
97 $authdata = get_file_argument();
98 $authdata = explode('/', trim($authdata, '/'));
99 if (count($authdata) == 2) {
100 list($this->username, $this->password) = $authdata;
106 * Send the error information to the WS client
107 * formatted as XML document.
108 * @param exception $ex
111 protected function send_error($ex=null) {
112 // Zend Soap server fault handling is incomplete compared to XML-RPC :-(
113 // we can not use: echo $this->zend_server->fault($ex);
114 //TODO: send some better response in XML
116 $info = $ex->getMessage();
117 if (debugging() and isset($ex->debuginfo)) {
118 $info .= ' - '.$ex->debuginfo;
121 $info = 'Unknown error';
124 $xml = '<?xml version="1.0" encoding="UTF-8"?>
125 <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
126 <SOAP-ENV:Body><SOAP-ENV:Fault>
127 <faultcode>MOODLE:error</faultcode>
128 <faultstring>'.$info.'</faultstring>
129 </SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>';
131 $this->send_headers();
132 header('Content-Type: application/xml; charset=utf-8');
133 header('Content-Disposition: inline; filename="response.xml"');
138 protected function generate_simple_struct_class(external_single_structure $structdesc) {
140 // let's use unique class name, there might be problem in unit tests
141 $classname = 'webservices_struct_class_000000';
142 while(class_exists($classname)) {
147 foreach ($structdesc->keys as $name => $fieldsdesc) {
148 $type = $this->get_phpdoc_type($fieldsdesc);
149 $fields[] = ' /** @var '.$type." */\n" .
150 ' public $'.$name.';';
155 * Virtual struct class for web services for user id '.$USER->id.' in context '.$this->restricted_context->id.'.
157 class '.$classname.' {
158 '.implode("\n", $fields).'
167 * SOAP test client class
169 class webservice_soap_test_client implements webservice_test_client_interface {
171 * Execute test client WS request
172 * @param string $serverurl
173 * @param string $function
174 * @param array $params
177 public function simpletest($serverurl, $function, $params) {
178 //zend expects 0 based array with numeric indexes
179 $params = array_values($params);
180 require_once 'Zend/Soap/Client.php';
181 $client = new Zend_Soap_Client($serverurl.'&wsdl=1');
182 return $client->__call($function, $params);