ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilSoapClient.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
28{
29 public const DEFAULT_CONNECT_TIMEOUT = 10;
30 public const DEFAULT_RESPONSE_TIMEOUT = 5;
31
32 public const MAX_DIRECTORY_SEARCH_DEPTH = 20;
33 private ilLogger $log;
35 private ?SoapClient $client = null;
36 private string $uri;
37 private bool $use_wsdl = true;
40 private ?int $stored_socket_timeout = null;
41
42 public function __construct(string $a_uri = '')
43 {
44 global $DIC;
45
46 $this->settings = $DIC->settings();
47 $this->log = $DIC->logger()->wsrv();
48 $this->uri = $a_uri;
49 $this->use_wsdl = true;
50 $timeout = (int) $this->settings->get('soap_connect_timeout', (string) self::DEFAULT_CONNECT_TIMEOUT);
51 if ($timeout) {
52 $this->connect_timeout = $timeout;
53 }
54 $this->connect_timeout = $timeout;
55
56 $this->response_timeout = (int) $this->settings->get('soap_response_timeout', (string) self::DEFAULT_RESPONSE_TIMEOUT);
57 }
58
59 public function getServer(): string
60 {
61 return $this->uri;
62 }
63
64 public function setTimeout(int $a_timeout): void
65 {
66 $this->connect_timeout = $a_timeout;
67 }
68
69 public function getTimeout(): int
70 {
72 }
73
74 public function setResponseTimeout(int $a_timeout): void
75 {
76 $this->response_timeout = $a_timeout;
77 }
78
79 public function getResponseTimeout(): int
80 {
82 }
83
84 public function enableWSDL(bool $a_stat): void
85 {
86 $this->use_wsdl = $a_stat;
87 }
88
89 public function enabledWSDL(): bool
90 {
91 return $this->use_wsdl;
92 }
93
94 public function init(): bool
95 {
96 $internal_path = $this->settings->get('soap_internal_wsdl_path');
97 if (trim($this->getServer()) === '') {
98 if ($internal_path) {
99 $uri = (new \ILIAS\Data\URI($internal_path));
100 parse_str($uri->getQuery() ?? '', $query);
101 $this->uri = (string) (isset($query['wsdl']) ?
102 $uri :
103 $uri->withQuery(http_build_query(array_merge($query, ['wsdl' => '']))));
104 } elseif (trim($this->settings->get('soap_wsdl_path', '')) !== '') {
105 $this->uri = $this->settings->get('soap_wsdl_path', '');
106 } else {
107 $request_path = ilUtil::_getHttpPath();
108 $depth = 0;
109 if (PHP_SAPI !== 'cli') {
110 $script_path = dirname($_SERVER['SCRIPT_FILENAME']);
111 while ($depth < self::MAX_DIRECTORY_SEARCH_DEPTH && !is_file($script_path . '/ilias.php')) {
112 $parent = dirname($script_path);
113 if ($parent === $script_path) {
114 break;
115 }
116 $script_path = $parent;
117 $depth++;
118 }
119 }
120
121 if ($depth > 0 && is_file($script_path . '/ilias.php')) {
122 $url_parts = parse_url($request_path);
123 $path = $url_parts['path'] ?? '';
124 $path_parts = explode('/', rtrim($path, '/'));
125 $path_parts = array_slice($path_parts, 0, -$depth);
126 $new_path = implode('/', $path_parts);
127
128 $request_path = $url_parts['scheme'] . '://' . $url_parts['host'];
129 if (isset($url_parts['port'])) {
130 $request_path .= ':' . $url_parts['port'];
131 }
132 $request_path .= $new_path;
133 }
134
135 $this->uri = rtrim($request_path, '/') . '/soap/server.php?wsdl';
136 }
137 }
138 try {
139 $this->log->debug('Using wsdl: ' . $this->getServer());
140 $this->log->debug('Using connection timeout: ' . $this->getTimeout());
141 $this->log->debug('Using response timeout: ' . $this->getResponseTimeout());
142
143 $this->setSocketTimeout(true);
144 $this->client = new SoapClient(
145 $this->uri,
146 array(
147 'exceptions' => true,
148 'trace' => 1,
149 'connection_timeout' => $this->getTimeout(),
150 'stream_context' => $this->uri === $internal_path ? stream_context_create([
151 'ssl' => [
152 'verify_peer' => (bool) $this->settings->get('soap_internal_wsdl_verify_peer', '1'),
153 'verify_peer_name' => (bool) $this->settings->get('soap_internal_wsdl_verify_peer_name', '1'),
154 'allow_self_signed' => (bool) $this->settings->get('soap_internal_wsdl_allow_self_signed', ''),
155 ]
156 ]) : null
157 )
158 );
159 return true;
160 } catch (SoapFault $ex) {
161 $this->log->warning('Soap init failed with message: ' . $ex->getMessage());
162 $this->resetSocketTimeout();
163 return false;
164 } finally {
165 $this->resetSocketTimeout();
166 }
167 }
168
169 protected function setSocketTimeout(bool $a_wsdl_mode): bool
170 {
171 $this->stored_socket_timeout = (int) ini_get('default_socket_timeout');
172 $this->log->debug('Default socket timeout is: ' . $this->stored_socket_timeout);
173
174 if ($a_wsdl_mode) {
175 $this->log->debug('WSDL mode, using socket timeout: ' . $this->getTimeout());
176 ini_set('default_socket_timeout', (string) $this->getTimeout());
177 } else {
178 $this->log->debug('Non WSDL mode, using socket timeout: ' . $this->getResponseTimeout());
179 ini_set('default_socket_timeout', (string) $this->getResponseTimeout());
180 }
181
182 return true;
183 }
184
188 protected function resetSocketTimeout(): bool
189 {
190 ini_set('default_socket_timeout', (string) $this->stored_socket_timeout);
191 $this->log->debug('Restoring default socket timeout to: ' . $this->stored_socket_timeout);
192 return true;
193 }
194
200 public function call(string $a_operation, array $a_params)
201 {
202 $this->log->debug('Calling webservice: ' . $a_operation);
203
204 $this->setSocketTimeout(false);
205 try {
206 return $this->client->__call($a_operation, $a_params);
207 } catch (SoapFault $exception) {
208 $this->log->error('Calling webservice failed with message: ' . $exception->getMessage());
209 $this->log->debug((string) $this->client->__getLastResponseHeaders());
210 $this->log->debug((string) $this->client->__getLastResponse());
211 return false;
212 } catch (Exception $exception) {
213 $this->log->error('Caught unknown exception with message: ' . $exception->getMessage());
214 $this->log->debug((string) $this->client->__getLastResponseHeaders());
215 $this->log->debug((string) $this->client->__getLastResponse());
216 } finally {
217 $this->resetSocketTimeout();
218 }
219
220 return false;
221 }
222}
Component logger with individual log levels by component id.
ILIAS Setting Class.
call(string $a_operation, array $a_params)
setSocketTimeout(bool $a_wsdl_mode)
setTimeout(int $a_timeout)
const DEFAULT_CONNECT_TIMEOUT
const MAX_DIRECTORY_SEARCH_DEPTH
SoapClient $client
setResponseTimeout(int $a_timeout)
enableWSDL(bool $a_stat)
resetSocketTimeout()
Reset socket default timeout to defaults.
__construct(string $a_uri='')
const DEFAULT_RESPONSE_TIMEOUT
static _getHttpPath()
$path
Definition: ltiservices.php:30
$_SERVER['HTTP_HOST']
Definition: raiseError.php:26
global $DIC
Definition: shib_login.php:26