ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilECSSetting.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
34 {
35  const DEFAULT_AUTH_MODE = 'ldap';
36 
37  const ERROR_EXTRACT_SERIAL = 'ecs_error_extract_serial';
38  const ERROR_REQUIRED = 'fill_out_all_required_fields';
39  const ERROR_INVALID_IMPORT_ID = 'ecs_check_import_id';
40  const ERROR_CERT_EXPIRED = 'ecs_certificate_expired';
41 
42  const AUTH_CERTIFICATE = 1;
43  const AUTH_APACHE = 2;
44 
45  const DEFAULT_DURATION = 6;
46 
47 
48  const PROTOCOL_HTTP = 0;
49  const PROTOCOL_HTTPS = 1;
50 
51  protected static $instances = null;
52  protected static $configured;
53 
54 
55  private $server_id = 0;
56  private $active = false;
57  private $title = '';
58  private $auth_type = self::AUTH_CERTIFICATE;
59  private $server;
60  private $protocol;
61  private $port;
63  private $ca_cert_path;
64  private $key_path;
65  private $key_password;
66  private $polling;
67  private $import_id;
68  private $cert_serial;
69  private $global_role;
70  private $duration;
71 
72  private $auth_user = '';
73  private $auth_pass = '';
74 
75  private $user_recipients = array();
76  private $econtent_recipients = array();
77  private $approval_recipients = array();
78 
84  private function __construct($a_server_id = 0)
85  {
86  $this->server_id = $a_server_id;
87  $this->read();
88  }
89 
98  public static function _getInstance()
99  {
100  $GLOBALS['DIC']->logger()->wsrv()->warning('Using deprecated call');
101  $GLOBALS['DIC']->logger()->wsrv()->logStack(ilLogLevel::WARNING);
102  return self::getInstanceByServerId(null);
103  }
104 
110  public static function getInstanceByServerId($a_server_id)
111  {
112  if (self::$instances[$a_server_id]) {
113  return self::$instances[$a_server_id];
114  }
115  return self::$instances[$a_server_id] = new ilECSSetting($a_server_id);
116  }
117 
121  public static function lookupAuthMode()
122  {
123  return self::DEFAULT_AUTH_MODE;
124  }
125 
131  public static function ecsConfigured()
132  {
133  if (self::$configured === null) {
134  global $DIC;
135  $ilDB = $DIC->database();
136 
137  $query = "SELECT count(*) count FROM ecs_server";
138  $ret = $ilDB->query($query);
139  $c = $ret->fetchObject()->count;
140 
141  self::$configured = $c > 0;
142  }
143  return self::$configured;
144  }
145 
150  public function setTitle($a_title)
151  {
152  $this->title = $a_title;
153  }
154 
159  public function getTitle()
160  {
161  return $this->title;
162  }
163 
168  public function setAuthType($a_auth_type)
169  {
170  $this->auth_type = $a_auth_type;
171  }
172 
177  public function getAuthType()
178  {
179  return $this->auth_type;
180  }
181 
186  public function setAuthUser($a_user)
187  {
188  $this->auth_user = $a_user;
189  }
190 
195  public function getAuthUser()
196  {
197  return $this->auth_user;
198  }
199 
204  public function setAuthPass($a_pass)
205  {
206  $this->auth_pass = $a_pass;
207  }
208 
213  public function getAuthPass()
214  {
215  return $this->auth_pass;
216  }
217 
222  public function getServerId()
223  {
224  return (int) $this->server_id;
225  }
226 
234  public function setEnabledStatus($a_status)
235  {
236  $this->active = $a_status;
237  }
238 
245  public function isEnabled()
246  {
247  return $this->active;
248  }
249 
257  public function setServer($a_server)
258  {
259  $this->server = $a_server;
260  }
261 
269  public function getServer()
270  {
271  return $this->server;
272  }
273 
280  public function getServerURI()
281  {
282  switch ($this->getProtocol()) {
283  case self::PROTOCOL_HTTP:
284  $uri = 'http://';
285  break;
286 
287  case self::PROTOCOL_HTTPS:
288  $uri = 'https://';
289  break;
290  }
291 
292  if (stristr($this->getServer(), '/')) {
293  $counter = 0;
294  foreach ((array) explode('/', $this->getServer()) as $key => $part) {
295  $uri .= $part;
296  if (!$counter) {
297  $uri .= ':' . $this->getPort();
298  }
299  $uri .= '/';
300  ++$counter;
301  }
302  $uri = substr($uri, 0, -1);
303  } else {
304  $uri .= $this->getServer();
305  $uri .= (':' . $this->getPort());
306  }
307 
308  return $uri;
309  }
310 
318  public function setProtocol($a_prot)
319  {
320  $this->protocol = $a_prot;
321  }
322 
329  public function getProtocol()
330  {
331  return $this->protocol;
332  }
333 
341  public function setPort($a_port)
342  {
343  $this->port = $a_port;
344  }
345 
353  public function getPort()
354  {
355  return $this->port;
356  }
357 
365  public function setPollingTime($a_time)
366  {
367  $this->polling = $a_time;
368  }
369 
376  public function getPollingTime()
377  {
378  return $this->polling;
379  }
380 
387  public function getPollingTimeSeconds()
388  {
389  return (int) ($this->polling % 60);
390  }
391 
398  public function getPollingTimeMinutes()
399  {
400  return (int) ($this->polling / 60);
401  }
402 
411  public function setPollingTimeMS($a_min, $a_sec)
412  {
413  $this->setPollingTime(60 * $a_min + $a_sec);
414  }
415 
423  public function setClientCertPath($a_path)
424  {
425  $this->client_cert_path = $a_path;
426  }
427 
433  public function getClientCertPath()
434  {
436  }
437 
445  public function setCACertPath($a_ca)
446  {
447  $this->ca_cert_path = $a_ca;
448  }
449 
456  public function getCACertPath()
457  {
458  return $this->ca_cert_path;
459  }
460 
467  public function getKeyPath()
468  {
469  return $this->key_path;
470  }
471 
479  public function setKeyPath($a_path)
480  {
481  $this->key_path = $a_path;
482  }
483 
490  public function getKeyPassword()
491  {
492  return $this->key_password;
493  }
494 
502  public function setKeyPassword($a_pass)
503  {
504  $this->key_password = $a_pass;
505  }
506 
514  public function setImportId($a_id)
515  {
516  $this->import_id = $a_id;
517  }
518 
524  public function getImportId()
525  {
526  return $this->import_id;
527  }
528 
536  public function setCertSerialNumber($a_cert_serial)
537  {
538  $this->cert_serial_number = $a_cert_serial;
539  }
540 
547  public function getCertSerialNumber()
548  {
549  return $this->cert_serial_number;
550  }
551 
558  public function getGlobalRole()
559  {
560  return $this->global_role;
561  }
562 
570  public function setGlobalRole($a_role_id)
571  {
572  $this->global_role = $a_role_id;
573  }
574 
582  public function setDuration($a_duration)
583  {
584  $this->duration = $a_duration;
585  }
586 
593  public function getDuration()
594  {
595  return $this->duration ? $this->duration : self::DEFAULT_DURATION;
596  }
597 
604  public function getUserRecipients()
605  {
606  return explode(',', (string) $this->user_recipients);
607  }
608 
615  public function getUserRecipientsAsString()
616  {
617  return $this->user_recipients ? $this->user_recipients : '';
618  }
619 
627  public function setUserRecipients($a_logins)
628  {
629  $this->user_recipients = $a_logins;
630  }
631 
638  public function getEContentRecipients()
639  {
640  return explode(',', $this->econtent_recipients);
641  }
642 
650  {
651  return $this->econtent_recipients ? $this->econtent_recipients : '';
652  }
653 
661  public function setEContentRecipients($a_logins)
662  {
663  $this->econtent_recipients = $a_logins;
664  }
665 
672  public function getApprovalRecipients()
673  {
674  return explode(',', $this->approval_recipients);
675  }
676 
685  {
686  return $this->approval_recipients ? $this->approval_recipients : '';
687  }
688 
695  public function setApprovalRecipients($a_rcp)
696  {
697  $this->approval_recipients = $a_rcp;
698  }
699 
708  public function validate()
709  {
710  if (!$this->isEnabled()) {
711  return '';
712  }
713 
714  // Cert based authentication
715  if ($this->getAuthType() == self::AUTH_CERTIFICATE) {
716  if (!$this->getClientCertPath() or !$this->getCACertPath() or !$this->getKeyPath() or !$this->getKeyPassword()) {
717  return self::ERROR_REQUIRED;
718  }
719  // Check import id
720  if (!$this->fetchSerialID()) {
721  return self::ERROR_EXTRACT_SERIAL;
722  }
723  if (!$this->fetchCertificateExpiration()) {
724  return self::ERROR_CERT_EXPIRED;
725  }
726  }
727  // Apache auth
728  if ($this->getAuthType() == self::AUTH_APACHE) {
729  if (!$this->getAuthUser() or !$this->getAuthPass()) {
730  return self::ERROR_REQUIRED;
731  }
732  }
733 
734  // required fields
735  if (!$this->getServer() or !$this->getPort() or !$this->getImportId()
736  or !$this->getGlobalRole() or !$this->getDuration()) {
737  return self::ERROR_REQUIRED;
738  }
739 
740  if (!$this->checkImportId()) {
741  return self::ERROR_INVALID_IMPORT_ID;
742  }
743  return '';
744  }
745 
752  public function checkImportId()
753  {
754  global $DIC;
755 
756  $ilObjDataCache = $DIC['ilObjDataCache'];
757  $tree = $DIC['tree'];
758 
759  if (!$this->getImportId()) {
760  return false;
761  }
762  if ($ilObjDataCache->lookupType($ilObjDataCache->lookupObjId($this->getImportId())) != 'cat') {
763  return false;
764  }
765  if ($tree->isDeleted($this->getImportId())) {
766  return false;
767  }
768  return true;
769  }
770 
777  public function save()
778  {
779  global $DIC;
780 
781  $ilDB = $DIC['ilDB'];
782 
783  $this->server_id = $ilDB->nextId('ecs_server');
784  $ilDB->manipulate(
785  $q = 'INSERT INTO ecs_server (server_id,active,title,protocol,server,port,auth_type,client_cert_path,ca_cert_path,' .
786  'key_path,key_password,cert_serial,polling_time,import_id,global_role,econtent_rcp,user_rcp,approval_rcp,duration,auth_user,auth_pass) ' .
787  'VALUES (' .
788  $ilDB->quote($this->getServerId(), 'integer') . ', ' .
789  $ilDB->quote((int) $this->isEnabled(), 'integer') . ', ' .
790  $ilDB->quote($this->getTitle(), 'text') . ', ' .
791  $ilDB->quote((int) $this->getProtocol(), 'integer') . ', ' .
792  $ilDB->quote($this->getServer(), 'text') . ', ' .
793  $ilDB->quote($this->getPort(), 'integer') . ', ' .
794  $ilDB->quote($this->getAuthType(), 'integer') . ', ' .
795  $ilDB->quote($this->getClientCertPath(), 'text') . ', ' .
796  $ilDB->quote($this->getCACertPath(), 'text') . ', ' .
797  $ilDB->quote($this->getKeyPath(), 'text') . ', ' .
798  $ilDB->quote($this->getKeyPassword(), 'text') . ', ' .
799  $ilDB->quote($this->getCertSerialNumber(), 'text') . ', ' .
800  $ilDB->quote($this->getPollingTime(), 'integer') . ', ' .
801  $ilDB->quote($this->getImportId(), 'integer') . ', ' .
802  $ilDB->quote($this->getGlobalRole(), 'integer') . ', ' .
803  $ilDB->quote($this->getEContentRecipientsAsString(), 'text') . ', ' .
804  $ilDB->quote($this->getUserRecipientsAsString(), 'text') . ', ' .
805  $ilDB->quote($this->getApprovalRecipientsAsString(), 'text') . ', ' .
806  $ilDB->quote($this->getDuration(), 'integer') . ', ' .
807  $ilDB->quote($this->getAuthUser(), 'text') . ', ' .
808  $ilDB->quote($this->getAuthPass(), 'text') . ' ' .
809  ')'
810  );
811  }
812 
816  public function update()
817  {
818  global $DIC;
819 
820  $ilDB = $DIC['ilDB'];
821 
822  $ilDB->manipulate(
823  'UPDATE ecs_server SET ' .
824  'server_id = ' . $ilDB->quote($this->getServerId(), 'integer') . ', ' .
825  'active = ' . $ilDB->quote((int) $this->isEnabled(), 'integer') . ', ' .
826  'title = ' . $ilDB->quote($this->getTitle(), 'text') . ', ' .
827  'protocol = ' . $ilDB->quote((int) $this->getProtocol(), 'integer') . ', ' .
828  'server = ' . $ilDB->quote($this->getServer(), 'text') . ', ' .
829  'port = ' . $ilDB->quote($this->getPort(), 'integer') . ', ' .
830  'auth_type = ' . $ilDB->quote($this->getAuthType(), 'integer') . ', ' .
831  'client_cert_path = ' . $ilDB->quote($this->getClientCertPath(), 'text') . ', ' .
832  'ca_cert_path = ' . $ilDB->quote($this->getCACertPath(), 'text') . ', ' .
833  'key_path = ' . $ilDB->quote($this->getKeyPath(), 'text') . ', ' .
834  'key_password = ' . $ilDB->quote($this->getKeyPassword(), 'text') . ', ' .
835  'cert_serial = ' . $ilDB->quote($this->getCertSerialNumber(), 'text') . ', ' .
836  'polling_time = ' . $ilDB->quote($this->getPollingTime(), 'integer') . ', ' .
837  'import_id = ' . $ilDB->quote($this->getImportId(), 'integer') . ', ' .
838  'global_role = ' . $ilDB->quote($this->getGlobalRole(), 'integer') . ', ' .
839  'econtent_rcp = ' . $ilDB->quote($this->getEContentRecipientsAsString(), 'text') . ', ' .
840  'user_rcp = ' . $ilDB->quote($this->getUserRecipientsAsString(), 'text') . ', ' .
841  'approval_rcp = ' . $ilDB->quote($this->getApprovalRecipientsAsString(), 'text') . ', ' .
842  'duration = ' . $ilDB->quote($this->getDuration(), 'integer') . ', ' .
843  'auth_user = ' . $ilDB->quote($this->getAuthUser(), 'text') . ', ' .
844  'auth_pass = ' . $ilDB->quote($this->getAuthPass(), 'text') . ', ' .
845  'auth_type = ' . $ilDB->quote($this->getAuthType(), 'integer') . ' ' .
846  'WHERE server_id = ' . $ilDB->quote($this->getServerId(), 'integer')
847  );
848  }
849 
853  public function delete()
854  {
855  global $DIC;
856 
857  $ilDB = $DIC['ilDB'];
858 
859  // --- cascading delete
860 
861  include_once 'Services/WebServices/ECS/classes/Tree/class.ilECSCmsData.php';
863 
864  include_once 'Services/WebServices/ECS/classes/class.ilECSCommunityCache.php';
866 
867  include_once 'Services/WebServices/ECS/classes/class.ilECSDataMappingSetting.php';
869 
870  include_once 'Services/WebServices/ECS/classes/class.ilECSEventQueueReader.php';
872 
873  include_once 'Services/WebServices/ECS/classes/Mapping/class.ilECSNodeMappingAssignment.php';
875 
876  include_once 'Services/WebServices/ECS/classes/class.ilECSParticipantSetting.php';
878 
879  include_once 'Services/WebServices/ECS/classes/class.ilECSExport.php';
881 
882  // resetting server id to flag items in imported list
883  include_once 'Services/WebServices/ECS/classes/class.ilECSImport.php';
885 
886  $ilDB->manipulate(
887  'DELETE FROM ecs_server ' .
888  'WHERE server_id = ' . $ilDB->quote($this->getServerId(), 'integer')
889  );
890 
891  $this->server_id = null;
892  return true;
893  }
894 
895 
900  public function fetchCertificateExpiration()
901  {
902  if ($this->getAuthType() != self::AUTH_CERTIFICATE) {
903  return null;
904  }
905 
906  if (function_exists('openssl_x509_parse') and $cert = openssl_x509_parse('file://' . $this->getClientCertPath())) {
907  if (isset($cert['validTo_time_t']) and $cert['validTo_time_t']) {
908  $dt = new ilDateTime($cert['validTo_time_t'], IL_CAL_UNIX);
909 
910  $GLOBALS['DIC']->logger()->wsrv()->debug('Certificate expires at: ' . ilDatePresentation::formatDate($dt));
911  return $dt;
912  }
913  }
914  return null;
915  }
916 
923  private function fetchSerialID()
924  {
925  if (function_exists('openssl_x509_parse') and $cert = openssl_x509_parse('file://' . $this->getClientCertPath())) {
926  if (isset($cert['serialNumber']) and $cert['serialNumber']) {
927  $this->setCertSerialNumber($cert['serialNumber']);
928  $GLOBALS['DIC']->logger()->wsrv()->debug('Searial number is: ' . $cert['serialNumber']);
929  return true;
930  }
931  }
932 
933  if (!file_exists($this->getClientCertPath()) or !is_readable($this->getClientCertPath())) {
934  return false;
935  }
936  $lines = file($this->getClientCertPath());
937  $found = false;
938  foreach ($lines as $line) {
939  if (strpos($line, 'Serial Number:') !== false) {
940  $found = true;
941  $serial_line = explode(':', $line);
942  $serial = (int) trim($serial_line[1]);
943  break;
944  }
945  }
946  if ($found) {
947  $this->setCertSerialNumber($serial);
948  return true;
949  } else {
950  return false;
951  }
952  }
953 
959  private function read()
960  {
961  global $DIC;
962 
963  $ilDB = $DIC['ilDB'];
964 
965  if (!$this->getServerId()) {
966  return false;
967  }
968 
969  $query = 'SELECT * FROM ecs_server ' .
970  'WHERE server_id = ' . $ilDB->quote($this->getServerId(), 'integer');
971  $res = $ilDB->query($query);
972  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
973  $this->setServer($row['server']);
974  $this->setTitle($row['title']);
975  $this->setProtocol($row['protocol']);
976  $this->setPort($row['port']);
977  $this->setClientCertPath($row['client_cert_path']);
978  $this->setCACertPath($row['ca_cert_path']);
979  $this->setKeyPath($row['key_path']);
980  $this->setKeyPassword($row['key_password']);
981  $this->setPollingTime($row['polling_time']);
982  $this->setImportId($row['import_id']);
983  $this->setEnabledStatus((int) $row['active']);
984  $this->setCertSerialNumber($row['cert_serial']);
985  $this->setGlobalRole($row['global_role']);
986  $this->econtent_recipients = $row['econtent_rcp'];
987  $this->approval_recipients = $row['approval_rcp'];
988  $this->user_recipients = $row['user_rcp'];
989  $this->setDuration($row['duration']);
990  $this->setAuthUser($row['auth_user']);
991  $this->setAuthPass($row['auth_pass']);
992  $this->setAuthType($row['auth_type']);
993  }
994  }
995 
1000  public function __clone()
1001  {
1002  $this->server_id = 0;
1003  $this->setTitle($this->getTitle() . ' (Copy)');
1004  $this->setEnabledStatus(false);
1005  $this->setServer('');
1006  $this->setProtocol(self::PROTOCOL_HTTPS);
1007  $this->setPort(0);
1008  $this->setClientCertPath('');
1009  $this->setKeyPath('');
1010  $this->setKeyPassword('');
1011  $this->setCACertPath('');
1012  $this->setCertSerialNumber('');
1013  $this->setAuthType(self::AUTH_CERTIFICATE);
1014  $this->setAuthUser('');
1015  $this->setAuthPass('');
1016  }
1017 }
static getInstanceByServerId($a_server_id)
Get singleton instance per server.
getKeyPassword()
get key password
const ERROR_INVALID_IMPORT_ID
getCACertPath()
get ca cert path
setAuthUser($a_user)
Set apache auth user.
setKeyPassword($a_pass)
set key password
setGlobalRole($a_role_id)
set default global role
setKeyPath($a_path)
set key path
getServerId()
Get current server id.
static ecsConfigured()
Checks if an ecs server is configured.
global $DIC
Definition: saml.php:7
getAuthType()
Get auth type.
getServerURI()
get complete server uri
getEContentRecipientsAsString()
get EContent recipients as string
setAuthPass($a_pass)
Set Apache auth password.
save()
save settings
setTitle($a_title)
Set title.
getApprovalRecipientsAsString()
get approval recipients as string
getKeyPath()
get key path
__clone()
Overwritten clone method Reset all connection settings.
setApprovalRecipients($a_rcp)
set approval recipients
update()
Update setting.
setUserRecipients($a_logins)
set user recipients
getPollingTime()
get polling time
getClientCertPath()
get certificate path
getAuthPass()
Get auth password.
getImportId()
get import id
const AUTH_APACHE
const IL_CAL_UNIX
getEContentRecipients()
get Econtent recipients
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false, $include_seconds=false)
Format a date public.
fetchCertificateExpiration()
Fetch validity (expired date)
setAuthType($a_auth_type)
Set auth type.
getProtocol()
get protocol
setPollingTimeMS($a_min, $a_sec)
Set polling time.
validate()
Validate settings.
static resetServerId($a_server_id)
getTitle()
Get title.
setDuration($a_duration)
set Duration
foreach($_POST as $key=> $value) $res
static _getInstance()
singleton getInstance
isEnabled()
is enabled
getUserRecipientsAsString()
Get new user recipients.
getCertSerialNumber()
get cert serial number
getAuthUser()
Get apache auth user.
Date and time handling
setImportId($a_id)
set import id Object of category, that store new remote courses
static deleteByServerId($a_server_id)
checkImportId()
check import id
$query
static deleteByServerId($a_server_id)
read()
Read settings.
setProtocol($a_prot)
set protocol
getServer()
get server
setEContentRecipients($a_logins)
set EContent recipients
setPort($a_port)
set port
getGlobalRole()
get global role
$row
getDuration()
get duration
static deleteByServerId($a_server_id)
static lookupAuthMode()
Lookup auth mode.
setPollingTime($a_time)
set polling time
setClientCertPath($a_path)
set
fetchSerialID()
Fetch serial ID from cert.
__construct($a_server_id=0)
Singleton contructor.
global $ilDB
$ret
Definition: parser.php:6
setCACertPath($a_ca)
set ca cert path
getApprovalRecipients()
get approval recipients
setCertSerialNumber($a_cert_serial)
set cert serial number
getPollingTimeMinutes()
get polling time minutes
setServer($a_server)
set server
getPollingTimeSeconds()
get polling time seconds (<60)
$key
Definition: croninfo.php:18
setEnabledStatus($a_status)
en/disable ecs functionality
getUserRecipients()
Get new user recipients.
$GLOBALS['JPEG_Segment_Names']
Global Variable: XMP_tag_captions.
static deleteByServerId($a_server_id)