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