ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilSoapAdministration.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 include_once './webservice/soap/lib/nusoap.php';
28 include_once("./Services/Authentication/classes/class.ilAuthUtils.php"); // to get auth mode constants
29 
32 
34 {
35  public const NUSOAP = 1;
36  public const PHP5 = 2;
37 
38  protected bool $soap_check = true;
39  protected string $message = '';
40  protected string $message_code = '';
41 
45  public int $error_method;
46 
47  public function __construct(bool $use_nusoap = true)
48  {
49  if (
50  defined('IL_SOAPMODE') &&
51  defined('IL_SOAPMODE_NUSOAP') &&
53  ) {
54  $this->error_method = self::NUSOAP;
55  } else {
56  $this->error_method = self::PHP5;
57  }
58 
59  $this->initAuthenticationObject();
60  }
61 
62  protected function checkSession(string $sid): bool
63  {
64  global $DIC;
65 
66  $ilUser = $DIC->user();
67 
68  [$sid, $client] = $this->explodeSid($sid);
69 
70  if ($sid === '') {
71  $this->setMessage('No session id given');
72  $this->setMessageCode('Client');
73  return false;
74  }
75  if (!$client) {
76  $this->setMessage('No client given');
77  $this->setMessageCode('Client');
78  return false;
79  }
80 
81  if (!$GLOBALS['DIC']['ilAuthSession']->isAuthenticated()) {
82  $this->setMessage('Session invalid');
83  $this->setMessageCode('Client');
84  return false;
85  }
86 
87  $can = $DIC['legalDocuments']->canUseSoapApi()->applyTo(new Ok($ilUser))->except(
88  fn($error) => new Error(is_string($error) ? $error : $error->getMessage())
89  );
90  if (!$can->isOk()) {
91  $this->setMessage($can->error());
92  $this->setMessageCode('Server');
93  return false;
94  }
95 
96  if ($this->soap_check) {
97  $set = new ilSetting();
98  $this->setMessage('SOAP is not enabled in ILIAS administration for this client');
99  $this->setMessageCode('Server');
100  return ((int) $set->get("soap_user_administration", '0')) === 1;
101  }
102 
103  return true;
104  }
105 
106  protected function explodeSid(string $sid): array
107  {
108  $exploded = explode('::', $sid);
109 
110  return is_array($exploded) ? $exploded : array('sid' => '', 'client' => '');
111  }
112 
113  protected function setMessage(string $a_str): void
114  {
115  $this->message = $a_str;
116  }
117 
118  public function getMessage(): string
119  {
120  return $this->message;
121  }
122 
123  public function appendMessage(string $a_str): void
124  {
125  $this->message .= isset($this->message) ? ' ' : '';
126  $this->message .= $a_str;
127  }
128 
129  public function setMessageCode(string $a_code): void
130  {
131  $this->message_code = $a_code;
132  }
133 
134  public function getMessageCode(): string
135  {
136  return $this->message_code;
137  }
138 
139  protected function initAuth(string $sid): void
140  {
141  [$sid, $client] = $this->explodeSid($sid);
142 
143  if (session_status() === PHP_SESSION_ACTIVE && $sid === session_id()) {
144  return;
145  }
146 
147  if (session_status() === PHP_SESSION_ACTIVE) {
148  session_destroy();
149  }
150 
151  session_id($sid);
152 
154  require_once("Services/Init/classes/class.ilInitialisation.php");
156  ilUtil::setCookie(session_name(), $sid);
157  }
158  }
159 
160  protected function initIlias(): void
161  {
163  try {
164  require_once("Services/Init/classes/class.ilInitialisation.php");
166  } catch (Exception $e) {
167  }
168  }
169  }
170 
171  public function reInitUser(): void
172  {
174  try {
175  require_once("Services/Init/classes/class.ilInitialisation.php");
177  } catch (Exception $e) {
178  }
179  }
180  }
181 
182  protected function initAuthenticationObject(): void
183  {
184  include_once './Services/Authentication/classes/class.ilAuthFactory.php';
186  }
187 
193  protected function raiseError(string $a_message, $a_code)
194  {
195  switch ($this->error_method) {
196  case self::NUSOAP:
197  return new soap_fault($a_code, '', $a_message);
198  case self::PHP5:
199  return new SoapFault((string) $a_code, $a_message);
200  }
201  return null;
202  }
203 
204  public function isFault($object): bool
205  {
206  switch ($this->error_method) {
207  case self::NUSOAP:
208  return $object instanceof soap_fault;
209  case self::PHP5:
210  return $object instanceof SoapFault;
211  }
212  return true;
213  }
214 
218  protected function checkObjectAccess(
219  int $ref_id,
220  array $expected_type,
221  string $permission,
222  bool $returnObject = false
223  ) {
224  global $DIC;
225 
226  $rbacsystem = $DIC->rbac()->system();
227 
228  if (!ilObject::_exists($ref_id, true)) {
229  return $this->raiseError(
230  'No object for id.',
231  'CLIENT_OBJECT_NOT_FOUND'
232  );
233  }
234 
235  if (ilObject::_isInTrash($ref_id)) {
236  return $this->raiseError(
237  'Object is already trashed.',
238  'CLIENT_OBJECT_DELETED'
239  );
240  }
241 
243  if (!in_array($type, $expected_type, true)) {
244  return $this->raiseError(
245  "Wrong type $type for id. Expected: " . implode(",", $expected_type),
246  'CLIENT_OBJECT_WRONG_TYPE'
247  );
248  }
249  if (!$rbacsystem->checkAccess($permission, $ref_id, $type)) {
250  return $this->raiseError(
251  'Missing permission $permission for type $type.',
252  'CLIENT_OBJECT_WRONG_PERMISSION'
253  );
254  }
255  if ($returnObject) {
256  try {
257  return ilObjectFactory::getInstanceByRefId($ref_id);
258  } catch (ilObjectNotFoundException $e) {
259  return $this->raiseError('No valid ref_id given', 'Client');
260  }
261  }
262  return $type;
263  }
264 
265  public function getInstallationInfoXML(): string
266  {
267  $this->initIlias();
268  if (!defined("ILIAS_WEB_DIR")) {
269  define('ILIAS_WEB_DIR', dirname(__DIR__, 3) . "/data/");
270  }
271 
272  $clientdirs = glob(ILIAS_WEB_DIR . "/*", GLOB_ONLYDIR);
273  require_once("webservice/soap/classes/class.ilSoapInstallationInfoXMLWriter.php");
274  $writer = new ilSoapInstallationInfoXMLWriter();
275  $writer->start();
276  if (is_array($clientdirs)) {
277  foreach ($clientdirs as $clientdir) {
278  $writer->addClient($clientdir);
279  }
280  }
281  $writer->end();
282  return $writer->getXML();
283  }
284 
288  public function getClientInfoXML(string $clientid)
289  {
290  $this->initIlias();
291  if (!defined("ILIAS_WEB_DIR")) {
292  define('ILIAS_WEB_DIR', dirname(__DIR__, 3) . "/data/");
293  }
294  $clientdir = ILIAS_WEB_DIR . "/" . $clientid;
295 
296  require_once("webservice/soap/classes/class.ilSoapInstallationInfoXMLWriter.php");
297  $writer = new ilSoapInstallationInfoXMLWriter();
298  $writer->start();
299  if (!$writer->addClient($clientdir)) {
300  return $this->raiseError(
301  'Client ID ' . $clientid . 'does not exist!',
302  'Client'
303  );
304  }
305  $writer->end();
306  return $writer->getXML();
307  }
308 }
const IL_SOAPMODE_NUSOAP
Definition: server.php:16
Backward compatibility.
Definition: nusoap.php:1110
raiseError(string $a_message, $a_code)
int $error_method
Defines type of error handling (PHP5 || NUSOAP)
static _lookupObjId(int $ref_id)
global $DIC
Definition: feed.php:28
$client
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
$ref_id
Definition: ltiauth.php:67
const CONTEXT_SOAP
SOAP based authentication.
$GLOBALS["DIC"]
Definition: wac.php:31
__construct(bool $use_nusoap=true)
static setCookie(string $a_cookie_name, string $a_cookie_value='', bool $a_also_set_super_global=true, bool $a_set_cookie_invalid=false)
static _isInTrash(int $ref_id)
static getInstanceByRefId(int $ref_id, bool $stop_on_error=true)
get an instance of an Ilias object by reference id
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:16
checkObjectAccess(int $ref_id, array $expected_type, string $permission, bool $returnObject=false)
check access for ref id: expected type, permission, return object instance if returnobject is true ...
static setContext(int $a_context)
set context
static getType()
Get context type.
static _lookupType(int $id, bool $reference=false)
const CONTEXT_SOAP
const ILIAS_WEB_DIR
Definition: constants.php:45
const IL_SOAPMODE
Definition: server.php:20