ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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 private ilLogger $log;
34 private ?SoapClient $client = null;
35 private string $uri;
36 private bool $use_wsdl = true;
39 private ?int $stored_socket_timeout = null;
40
41 public function __construct(string $a_uri = '')
42 {
43 global $DIC;
44
45 $this->settings = $DIC->settings();
46 $this->log = $DIC->logger()->wsrv();
47 $this->uri = $a_uri;
48 $this->use_wsdl = true;
49 $timeout = (int) $this->settings->get('soap_connect_timeout', (string) self::DEFAULT_CONNECT_TIMEOUT);
50 if ($timeout) {
51 $this->connect_timeout = $timeout;
52 }
53 $this->connect_timeout = $timeout;
54
55 $this->response_timeout = (int) $this->settings->get('soap_response_timeout', (string) self::DEFAULT_RESPONSE_TIMEOUT);
56 }
57
58 public function getServer(): string
59 {
60 return $this->uri;
61 }
62
63 public function setTimeout(int $a_timeout): void
64 {
65 $this->connect_timeout = $a_timeout;
66 }
67
68 public function getTimeout(): int
69 {
71 }
72
73 public function setResponseTimeout(int $a_timeout): void
74 {
75 $this->response_timeout = $a_timeout;
76 }
77
78 public function getResponseTimeout(): int
79 {
81 }
82
83 public function enableWSDL(bool $a_stat): void
84 {
85 $this->use_wsdl = $a_stat;
86 }
87
88 public function enabledWSDL(): bool
89 {
90 return $this->use_wsdl;
91 }
92
93 public function init(): bool
94 {
95 $internal_path = $this->settings->get('soap_internal_wsdl_path');
96 if (trim($this->getServer()) === '') {
97 if ($internal_path) {
98 $uri = (new \ILIAS\Data\URI($internal_path));
99 parse_str($uri->getQuery() ?? '', $query);
100 $this->uri = (string) (isset($query['wsdl']) ?
101 $uri :
102 $uri->withQuery(http_build_query(array_merge($query, ['wsdl' => '']))));
103 } elseif (trim($this->settings->get('soap_wsdl_path', '')) !== '') {
104 $this->uri = $this->settings->get('soap_wsdl_path', '');
105 } else {
106 $this->uri = ilUtil::_getHttpPath() . '/public/soap/server.php?wsdl';
107 }
108 }
109 try {
110 $this->log->debug('Using wsdl: ' . $this->getServer());
111 $this->log->debug('Using connection timeout: ' . $this->getTimeout());
112 $this->log->debug('Using response timeout: ' . $this->getResponseTimeout());
113
114 $this->setSocketTimeout(true);
115 $this->client = new SoapClient(
116 $this->uri,
117 array(
118 'exceptions' => true,
119 'trace' => 1,
120 'connection_timeout' => $this->getTimeout(),
121 'stream_context' => $this->uri === $internal_path ? stream_context_create([
122 'ssl' => [
123 'verify_peer' => (bool) $this->settings->get('soap_internal_wsdl_verify_peer', '1'),
124 'verify_peer_name' => (bool) $this->settings->get('soap_internal_wsdl_verify_peer_name', '1'),
125 'allow_self_signed' => (bool) $this->settings->get('soap_internal_wsdl_allow_self_signed', ''),
126 ]
127 ]) : null
128 )
129 );
130 return true;
131 } catch (SoapFault $ex) {
132 $this->log->warning('Soap init failed with message: ' . $ex->getMessage());
133 $this->resetSocketTimeout();
134 return false;
135 } finally {
136 $this->resetSocketTimeout();
137 }
138 }
139
140 protected function setSocketTimeout(bool $a_wsdl_mode): bool
141 {
142 $this->stored_socket_timeout = (int) ini_get('default_socket_timeout');
143 $this->log->debug('Default socket timeout is: ' . $this->stored_socket_timeout);
144
145 if ($a_wsdl_mode) {
146 $this->log->debug('WSDL mode, using socket timeout: ' . $this->getTimeout());
147 ini_set('default_socket_timeout', (string) $this->getTimeout());
148 } else {
149 $this->log->debug('Non WSDL mode, using socket timeout: ' . $this->getResponseTimeout());
150 ini_set('default_socket_timeout', (string) $this->getResponseTimeout());
151 }
152
153 return true;
154 }
155
159 protected function resetSocketTimeout(): bool
160 {
161 ini_set('default_socket_timeout', (string) $this->stored_socket_timeout);
162 $this->log->debug('Restoring default socket timeout to: ' . $this->stored_socket_timeout);
163 return true;
164 }
165
171 public function call(string $a_operation, array $a_params)
172 {
173 $this->log->debug('Calling webservice: ' . $a_operation);
174
175 $this->setSocketTimeout(false);
176 try {
177 return $this->client->__call($a_operation, $a_params);
178 } catch (SoapFault $exception) {
179 $this->log->error('Calling webservice failed with message: ' . $exception->getMessage());
180 $this->log->debug((string) $this->client->__getLastResponseHeaders());
181 $this->log->debug((string) $this->client->__getLastResponse());
182 return false;
183 } catch (Exception $exception) {
184 $this->log->error('Caught unknown exception with message: ' . $exception->getMessage());
185 $this->log->debug((string) $this->client->__getLastResponseHeaders());
186 $this->log->debug((string) $this->client->__getLastResponse());
187 } finally {
188 $this->resetSocketTimeout();
189 }
190
191 return false;
192 }
193}
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
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()
global $DIC
Definition: shib_login.php:26