ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilECSSetting.php
Go to the documentation of this file.
1 <?php
2 
18 declare(strict_types=1);
19 
24 {
25  public const DEFAULT_AUTH_MODE = 'ldap';
26 
27  public const ERROR_EXTRACT_SERIAL = 'ecs_error_extract_serial';
28  public const ERROR_REQUIRED = 'fill_out_all_required_fields';
29  public const ERROR_INVALID_IMPORT_ID = 'ecs_check_import_id';
30  public const ERROR_CERT_EXPIRED = 'ecs_certificate_expired';
31 
32  public const AUTH_CERTIFICATE = 1;
33  public const AUTH_APACHE = 2;
34 
35  public const DEFAULT_DURATION = 6;
36 
37 
38  public const PROTOCOL_HTTP = 0;
39  public const PROTOCOL_HTTPS = 1;
40 
41  protected static ?array $instances = null;
42 
43  private int $server_id;
44  private bool $active = false;
45  private string $title = '';
46  private int $auth_type = self::AUTH_CERTIFICATE;
47  private string $server = '';
48  private int $protocol = self::PROTOCOL_HTTPS;
49  private int $port = 0;
50  private string $client_cert_path = '';
51  private string $ca_cert_path = '';
52  private ?string $cert_serial_number = '';
53  private string $key_path = '';
54  private string $key_password = '';
55  private int $import_id = 0;
56  private int $global_role = 0;
57  private int $duration = 0;
58 
59  private string $auth_user = '';
60  private string $auth_pass = '';
61 
62  private array $user_recipients = [];
63  private array $econtent_recipients = [];
64  private array $approval_recipients = [];
65 
66  private ilDBInterface $db;
67  private ilLogger $log;
69  private ilTree $tree;
70 
74  private function __construct($a_server_id = 0)
75  {
76  global $DIC;
77 
78  $this->db = $DIC->database();
79  $this->log = $DIC->logger()->wsrv();
80  $this->objDataCache = $DIC['ilObjDataCache'];
81  $this->tree = $DIC->repositoryTree();
82 
83  $this->server_id = $a_server_id;
84  $this->read();
85  }
86 
92  public static function getInstanceByServerId(int $a_server_id): ilECSSetting
93  {
94  return self::$instances[$a_server_id] ?? (self::$instances[$a_server_id] = new ilECSSetting($a_server_id));
95  }
96 
100  public static function lookupAuthMode(): string
101  {
102  return self::DEFAULT_AUTH_MODE;
103  }
104 
109  public static function ecsConfigured(): bool
110  {
111  return ilECSServerSettings::getInstance()->serverExists();
112  }
113 
117  public function setTitle(string $a_title): void
118  {
119  $this->title = $a_title;
120  }
121 
126  public function getTitle(): string
127  {
128  return $this->title;
129  }
130 
134  public function setAuthType($a_auth_type): void
135  {
136  $this->auth_type = $a_auth_type;
137  }
138 
142  public function getAuthType(): int
143  {
144  return $this->auth_type;
145  }
146 
150  public function setAuthUser($a_user): void
151  {
152  $this->auth_user = $a_user;
153  }
154 
158  public function getAuthUser(): string
159  {
160  return $this->auth_user;
161  }
162 
166  public function setAuthPass($a_pass): void
167  {
168  $this->auth_pass = $a_pass;
169  }
170 
174  public function getAuthPass(): string
175  {
176  return $this->auth_pass;
177  }
178 
182  public function getServerId(): int
183  {
184  return (int) $this->server_id;
185  }
186 
191  public function setEnabledStatus(bool $status): void
192  {
193  $this->active = $status;
194  }
195 
199  public function isEnabled(): bool
200  {
201  return $this->active;
202  }
203 
207  public function setServer(string $a_server): void
208  {
209  $this->server = $a_server;
210  }
211 
215  public function getServer(): string
216  {
217  return $this->server;
218  }
219 
223  public function getServerURI()
224  {
225  $uri = "";
226  switch ($this->getProtocol()) {
227  case self::PROTOCOL_HTTP:
228  $uri .= 'http://';
229  break;
230 
231  case self::PROTOCOL_HTTPS:
232  $uri .= 'https://';
233  break;
234  }
235 
236  if (strpos($this->getServer(), '/') !== false) {
237  $counter = 0;
238  foreach ((array) explode('/', $this->getServer()) as $key => $part) {
239  $uri .= $part;
240  if (!$counter) {
241  $uri .= ':' . $this->getPort();
242  }
243  $uri .= '/';
244  ++$counter;
245  }
246  $uri = substr($uri, 0, -1);
247  } else {
248  $uri .= $this->getServer();
249  $uri .= (':' . $this->getPort());
250  }
251 
252  return $uri;
253  }
254 
258  public function setProtocol(int $a_prot): void
259  {
260  $this->protocol = $a_prot;
261  }
262 
266  public function getProtocol(): int
267  {
268  return $this->protocol;
269  }
270 
274  public function setPort(int $a_port): void
275  {
276  $this->port = $a_port;
277  }
278 
282  public function getPort(): int
283  {
284  return $this->port;
285  }
286 
287  public function setClientCertPath($a_path): void
288  {
289  $this->client_cert_path = $a_path;
290  }
291 
295  public function getClientCertPath(): string
296  {
298  }
299 
305  public function setCACertPath(string $a_ca): void
306  {
307  $this->ca_cert_path = $a_ca;
308  }
309 
313  public function getCACertPath(): string
314  {
315  return $this->ca_cert_path;
316  }
317 
321  public function getKeyPath(): string
322  {
323  return $this->key_path;
324  }
325 
331  public function setKeyPath(string $a_path): void
332  {
333  $this->key_path = $a_path;
334  }
335 
339  public function getKeyPassword(): string
340  {
341  return $this->key_password;
342  }
343 
349  public function setKeyPassword(string $a_pass): void
350  {
351  $this->key_password = $a_pass;
352  }
353 
358  public function setImportId(int $a_id): void
359  {
360  $this->import_id = $a_id;
361  }
362 
366  public function getImportId(): int
367  {
368  return $this->import_id;
369  }
370 
374  public function setCertSerialNumber(string $a_cert_serial): void
375  {
376  $this->cert_serial_number = $a_cert_serial;
377  }
378 
382  public function getCertSerialNumber(): ?string
383  {
385  }
386 
390  public function getGlobalRole(): int
391  {
392  return $this->global_role;
393  }
394 
398  public function setGlobalRole(int $a_role_id): void
399  {
400  $this->global_role = $a_role_id;
401  }
402 
406  public function setDuration(int $a_duration): void
407  {
408  $this->duration = $a_duration;
409  }
410 
414  public function getDuration(): int
415  {
416  return $this->duration ?: self::DEFAULT_DURATION;
417  }
418 
422  public function getUserRecipients(): array
423  {
424  return $this->user_recipients;
425  }
426 
430  public function getUserRecipientsAsString(): string
431  {
432  return implode(',', $this->user_recipients);
433  }
434 
441  public function setUserRecipients(array $a_logins): void
442  {
443  $this->user_recipients = $a_logins;
444  }
445 
449  public function getEContentRecipients(): array
450  {
452  }
453 
457  public function getEContentRecipientsAsString(): string
458  {
459  return implode(',', $this->econtent_recipients);
460  }
461 
467  public function setEContentRecipients(array $a_logins): void
468  {
469  $this->econtent_recipients = $a_logins;
470  }
471 
475  public function getApprovalRecipients(): array
476  {
478  }
479 
483  public function getApprovalRecipientsAsString(): string
484  {
485  return implode(',', $this->approval_recipients);
486  }
487 
491  public function setApprovalRecipients(array $a_rcp): void
492  {
493  $this->approval_recipients = $a_rcp;
494  }
495 
501  public function validate(): string
502  {
503  if (!$this->isEnabled()) {
504  return '';
505  }
506 
507  // Cert based authentication
508  if ($this->getAuthType() === self::AUTH_CERTIFICATE) {
509  if (!$this->getClientCertPath() || !$this->getCACertPath() || !$this->getKeyPath() || !$this->getKeyPassword()) {
510  return self::ERROR_REQUIRED;
511  }
512  // Check import id
513  if (!$this->fetchSerialID()) {
514  return self::ERROR_EXTRACT_SERIAL;
515  }
516  if (!$this->fetchCertificateExpiration()) {
517  return self::ERROR_CERT_EXPIRED;
518  }
519  }
520  // Apache auth
521  if ($this->getAuthType() === self::AUTH_APACHE) {
522  if (!$this->getAuthUser() || !$this->getAuthPass()) {
523  return self::ERROR_REQUIRED;
524  }
525  }
526 
527  // required fields
528  if (!$this->getServer() || !$this->getPort() || !$this->getImportId()
529  || !$this->getGlobalRole() || !$this->getDuration()) {
530  return self::ERROR_REQUIRED;
531  }
532 
533  if (!$this->checkImportId()) {
534  return self::ERROR_INVALID_IMPORT_ID;
535  }
536  return '';
537  }
538 
542  public function checkImportId(): bool
543  {
544  if (!$this->getImportId()) {
545  return false;
546  }
547  if ($this->objDataCache->lookupType($this->objDataCache->lookupObjId($this->getImportId())) !== 'cat') {
548  return false;
549  }
550  if ($this->tree->isDeleted($this->getImportId())) {
551  return false;
552  }
553  return true;
554  }
555 
559  public function save(): void
560  {
561  $this->server_id = $this->db->nextId('ecs_server');
562  $this->db->manipulate(
563  'INSERT INTO ecs_server (server_id,active,title,protocol,server,port,auth_type,client_cert_path,ca_cert_path,' .
564  'key_path,key_password,cert_serial,import_id,global_role,econtent_rcp,user_rcp,approval_rcp,duration,auth_user,auth_pass) ' .
565  'VALUES (' .
566  $this->db->quote($this->getServerId(), 'integer') . ', ' .
567  $this->db->quote((int) $this->isEnabled(), 'integer') . ', ' .
568  $this->db->quote($this->getTitle(), 'text') . ', ' .
569  $this->db->quote($this->getProtocol(), 'integer') . ', ' .
570  $this->db->quote($this->getServer(), 'text') . ', ' .
571  $this->db->quote($this->getPort(), 'integer') . ', ' .
572  $this->db->quote($this->getAuthType(), 'integer') . ', ' .
573  $this->db->quote($this->getClientCertPath(), 'text') . ', ' .
574  $this->db->quote($this->getCACertPath(), 'text') . ', ' .
575  $this->db->quote($this->getKeyPath(), 'text') . ', ' .
576  $this->db->quote($this->getKeyPassword(), 'text') . ', ' .
577  $this->db->quote($this->getCertSerialNumber(), 'text') . ', ' .
578  $this->db->quote($this->getImportId(), 'integer') . ', ' .
579  $this->db->quote($this->getGlobalRole(), 'integer') . ', ' .
580  $this->db->quote($this->getEContentRecipientsAsString(), 'text') . ', ' .
581  $this->db->quote($this->getUserRecipientsAsString(), 'text') . ', ' .
582  $this->db->quote($this->getApprovalRecipientsAsString(), 'text') . ', ' .
583  $this->db->quote($this->getDuration(), 'integer') . ', ' .
584  $this->db->quote($this->getAuthUser(), 'text') . ', ' .
585  $this->db->quote($this->getAuthPass(), 'text') . ' ' .
586  ')'
587  );
588  }
589 
593  public function update(): void
594  {
595  $this->db->manipulate(
596  'UPDATE ecs_server SET ' .
597  'server_id = ' . $this->db->quote($this->getServerId(), 'integer') . ', ' .
598  'active = ' . $this->db->quote((int) $this->isEnabled(), 'integer') . ', ' .
599  'title = ' . $this->db->quote($this->getTitle(), 'text') . ', ' .
600  'protocol = ' . $this->db->quote($this->getProtocol(), 'integer') . ', ' .
601  'server = ' . $this->db->quote($this->getServer(), 'text') . ', ' .
602  'port = ' . $this->db->quote($this->getPort(), 'integer') . ', ' .
603  'auth_type = ' . $this->db->quote($this->getAuthType(), 'integer') . ', ' .
604  'client_cert_path = ' . $this->db->quote($this->getClientCertPath(), 'text') . ', ' .
605  'ca_cert_path = ' . $this->db->quote($this->getCACertPath(), 'text') . ', ' .
606  'key_path = ' . $this->db->quote($this->getKeyPath(), 'text') . ', ' .
607  'key_password = ' . $this->db->quote($this->getKeyPassword(), 'text') . ', ' .
608  'cert_serial = ' . $this->db->quote($this->getCertSerialNumber(), 'text') . ', ' .
609  'import_id = ' . $this->db->quote($this->getImportId(), 'integer') . ', ' .
610  'global_role = ' . $this->db->quote($this->getGlobalRole(), 'integer') . ', ' .
611  'econtent_rcp = ' . $this->db->quote($this->getEContentRecipientsAsString(), 'text') . ', ' .
612  'user_rcp = ' . $this->db->quote($this->getUserRecipientsAsString(), 'text') . ', ' .
613  'approval_rcp = ' . $this->db->quote($this->getApprovalRecipientsAsString(), 'text') . ', ' .
614  'duration = ' . $this->db->quote($this->getDuration(), 'integer') . ', ' .
615  'auth_user = ' . $this->db->quote($this->getAuthUser(), 'text') . ', ' .
616  'auth_pass = ' . $this->db->quote($this->getAuthPass(), 'text') . ', ' .
617  'auth_type = ' . $this->db->quote($this->getAuthType(), 'integer') . ' ' .
618  'WHERE server_id = ' . $this->db->quote($this->getServerId(), 'integer')
619  );
620  }
621 
625  public function delete(): bool
626  {
627  // --- cascading delete
629 
630  //TODO fix properly
631  ilECSCommunityCache::getInstance($this->getServerId(), -1)->deleteByServerId($this->getServerId());
632 
634 
635  (new ilECSEventQueueReader($this))->deleteAll();
636 
638 
639  $query = 'DELETE FROM ecs_events' .
640  ' WHERE server_id = ' . $this->db->quote($this->getServerId(), 'integer');
641  $this->db->manipulate($query);
642 
643  ilECSExportManager::getInstance()->deleteByServer($this->getServerId());
644 
645  //TODO check which one we need
646  ilECSImportManager::getInstance()->deleteByServer($this->getServerId());
647 
648  // resetting server id to flag items in imported list
649  ilECSImportManager::getInstance()->resetServerId($this->getServerId());
650 
651  $this->db->manipulate(
652  'DELETE FROM ecs_server ' .
653  'WHERE server_id = ' . $this->db->quote($this->getServerId(), 'integer')
654  );
655 
656  $this->server_id = 0;
657  return true;
658  }
659 
660 
665  {
666  if ($this->getAuthType() !== self::AUTH_CERTIFICATE) {
667  return null;
668  }
669 
670  if ((function_exists('openssl_x509_parse') &&
671  ($cert = openssl_x509_parse('file://' . $this->getClientCertPath())) &&
672  $cert && isset($cert['validTo_time_t'])) && $cert['validTo_time_t']) {
673  $dt = new ilDateTime($cert['validTo_time_t'], IL_CAL_UNIX);
674 
675  $this->log->debug('Certificate expires at: ' . ilDatePresentation::formatDate($dt));
676  return $dt;
677  }
678  return null;
679  }
680 
684  private function fetchSerialID(): bool
685  {
686  if (function_exists('openssl_x509_parse') && ($cert = openssl_x509_parse('file://' . $this->getClientCertPath())) && $cert && isset($cert['serialNumber']) && $cert['serialNumber']) {
687  $this->setCertSerialNumber($cert['serialNumber']);
688  $this->log->debug('Searial number is: ' . $cert['serialNumber']);
689  return true;
690  }
691 
692  if (!file_exists($this->getClientCertPath()) || !is_readable($this->getClientCertPath())) {
693  return false;
694  }
695  $lines = file($this->getClientCertPath());
696  $found = false;
697  foreach ($lines as $line) {
698  if (strpos($line, 'Serial Number:') !== false) {
699  $found = true;
700  $serial_line = explode(':', $line);
701  $serial = trim($serial_line[1]);
702  break;
703  }
704  }
705  if ($found && isset($serial)) {
706  $this->setCertSerialNumber($serial);
707  return true;
708  }
709  return false;
710  }
711 
715  private function read(): void
716  {
717  if (!$this->getServerId()) {
718  return;
719  }
720 
721  $query = 'SELECT * FROM ecs_server ' .
722  'WHERE server_id = ' . $this->db->quote($this->getServerId(), 'integer');
723  $res = $this->db->query($query);
724  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
725  $this->setServer($row['server']);
726  $this->setTitle($row['title']);
727  $this->setProtocol((int) $row['protocol']);
728  $this->setPort((int) $row['port']);
729  $this->setClientCertPath($row['client_cert_path']);
730  $this->setCACertPath($row['ca_cert_path']);
731  $this->setKeyPath($row['key_path']);
732  $this->setKeyPassword($row['key_password']);
733  $this->setImportId((int) $row['import_id']);
734  $this->setEnabledStatus((bool) $row['active']);
735  if ($row['cert_serial']) {
736  $this->setCertSerialNumber($row['cert_serial']);
737  }
738  $this->setGlobalRole((int) $row['global_role']);
739  $this->econtent_recipients = explode(',', $row['econtent_rcp']);
740  $this->approval_recipients = explode(',', $row['approval_rcp']);
741  $this->user_recipients = explode(',', $row['user_rcp']);
742  $this->setDuration((int) $row['duration']);
743  $this->setAuthUser($row['auth_user']);
744  $this->setAuthPass($row['auth_pass']);
745  $this->setAuthType((int) $row['auth_type']);
746  }
747  }
748 
753  public function __clone()
754  {
755  $this->server_id = 0;
756  $this->setTitle($this->getTitle() . ' (Copy)');
757  $this->setEnabledStatus(false);
758  $this->setServer('');
759  $this->setProtocol(self::PROTOCOL_HTTPS);
760  $this->setPort(0);
761  $this->setClientCertPath('');
762  $this->setKeyPath('');
763  $this->setKeyPassword('');
764  $this->setCACertPath('');
765  $this->setCertSerialNumber('');
766  $this->setAuthType(self::AUTH_CERTIFICATE);
767  $this->setAuthUser('');
768  $this->setAuthPass('');
769  }
770 }
setEContentRecipients(array $a_logins)
set EContent recipients
$res
Definition: ltiservices.php:69
static array $instances
setServer(string $a_server)
set server
static getInstanceByServerId(int $a_server_id)
Get singleton instance.
setKeyPassword(string $a_pass)
set key password
getKeyPassword()
get key password
const ERROR_INVALID_IMPORT_ID
getCACertPath()
get ca cert path
setAuthUser($a_user)
Set apache auth user.
setDuration(int $a_duration)
set Duration
getServerId()
Get current server id.
static ecsConfigured()
Checks if an ecs server is configured.
getAuthType()
Get auth type.
getServerURI()
get complete server uri
getEContentRecipientsAsString()
get EContent recipients as string
setAuthPass($a_pass)
Set Apache auth password.
ilDBInterface $db
static getInstance(int $a_server_id, int $a_community_id)
Get instance.
save()
save settings
static getInstance()
Get the singelton instance of this ilECSExportManager.
getApprovalRecipientsAsString()
get approval recipients as string
setKeyPath(string $a_path)
set key path
getKeyPath()
get key path
static getInstance()
Get singleton instance.
__clone()
Overwritten clone method Reset all connection settings.
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
update()
Update setting.
getClientCertPath()
get certificate path
getAuthPass()
Get auth password.
getImportId()
get import id
const IL_CAL_UNIX
getEContentRecipients()
get Econtent recipients
setCACertPath(string $a_ca)
set ca cert path
setTitle(string $a_title)
Set title.
setPort(int $a_port)
set port
fetchCertificateExpiration()
Fetch validity (expired date)
setAuthType($a_auth_type)
Set auth type.
getProtocol()
get protocol
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
validate()
Validate settings.
string $cert_serial_number
getTitle()
Get title.
setProtocol(int $a_prot)
set protocol
static getInstance()
Get the singleton instance of this ilECSImportManager.
setEnabledStatus(bool $status)
en/disable ecs functionality
isEnabled()
is enabled
getUserRecipientsAsString()
Get new user recipients.
setUserRecipients(array $a_logins)
set user recipients
getCertSerialNumber()
get cert serial number
static getInstanceByServerId(int $a_server_id)
Get singleton instance per server.
string $key
Consumer key/client ID value.
Definition: System.php:193
Reads ECS events and stores them in the database.
setImportId(int $a_id)
set import id Object of category, that store new remote courses
getAuthUser()
Get apache auth user.
ilObjectDataCache $objDataCache
checkImportId()
check import id
$query
read()
Read settings.
setGlobalRole(int $a_role_id)
set default global role
getServer()
get server
getGlobalRole()
get global role
getDuration()
get duration
static lookupAuthMode()
Lookup auth mode.
setClientCertPath($a_path)
fetchSerialID()
Fetch serial ID from cert.
__construct($a_server_id=0)
Singleton contructor.
setApprovalRecipients(array $a_rcp)
set approval recipients
getApprovalRecipients()
get approval recipients
getUserRecipients()
Get new user recipients.
setCertSerialNumber(string $a_cert_serial)
set cert serial number
static deleteByServerId($a_server_id)