ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
53 
54  private $server_id = 0;
55  private $active = false;
56  private $title = '';
58  private $server;
59  private $protocol;
60  private $port;
62  private $ca_cert_path;
63  private $key_path;
64  private $key_password;
65  private $polling;
66  private $import_id;
67  private $cert_serial;
68  private $global_role;
69  private $duration;
70 
71  private $auth_user = '';
72  private $auth_pass = '';
73 
74  private $user_recipients = array();
75  private $econtent_recipients = array();
76  private $approval_recipients = array();
77 
83  private function __construct($a_server_id = 0)
84  {
85  $this->server_id = $a_server_id;
86  $this->read();
87  }
88 
97  public static function _getInstance()
98  {
99  $GLOBALS['ilLog']->write(__METHOD__.': Using deprecated call.');
100  $GLOBALS['ilLog']->logStack();
101  return self::getInstanceByServerId(15);
102  }
103 
109  public static function getInstanceByServerId($a_server_id)
110  {
111  if(self::$instances[$a_server_id])
112  {
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  {
124  }
125 
130  public function setTitle($a_title)
131  {
132  $this->title = $a_title;
133  }
134 
139  public function getTitle()
140  {
141  return $this->title;
142  }
143 
148  public function setAuthType($a_auth_type)
149  {
150  $this->auth_type = $a_auth_type;
151  }
152 
157  public function getAuthType()
158  {
159  return $this->auth_type;
160  }
161 
166  public function setAuthUser($a_user)
167  {
168  $this->auth_user = $a_user;
169  }
170 
175  public function getAuthUser()
176  {
177  return $this->auth_user;
178  }
179 
184  public function setAuthPass($a_pass)
185  {
186  $this->auth_pass = $a_pass;
187  }
188 
193  public function getAuthPass()
194  {
195  return $this->auth_pass;
196  }
197 
202  public function getServerId()
203  {
204  return (int) $this->server_id;
205  }
206 
214  public function setEnabledStatus($a_status)
215  {
216  $this->active = $a_status;
217  }
218 
225  public function isEnabled()
226  {
227  return $this->active;
228  }
229 
237  public function setServer($a_server)
238  {
239  $this->server = $a_server;
240  }
241 
249  public function getServer()
250  {
251  return $this->server;
252  }
253 
260  public function getServerURI()
261  {
262  switch($this->getProtocol())
263  {
264  case self::PROTOCOL_HTTP:
265  $uri = 'http://';
266  break;
267 
268  case self::PROTOCOL_HTTPS:
269  $uri = 'https://';
270  break;
271  }
272 
273  if(stristr($this->getServer(), '/'))
274  {
275  $counter = 0;
276  foreach((array) explode('/',$this->getServer()) as $key => $part)
277  {
278  $uri .= $part;
279  if(!$counter)
280  {
281  $uri .= ':'.$this->getPort();
282  }
283  $uri .= '/';
284  ++$counter;
285  }
286  $uri = substr($uri,0,-1);
287  }
288  else
289  {
290  $uri .= $this->getServer();
291  $uri .= (':'.$this->getPort());
292  }
293 
294  return $uri;
295  }
296 
304  public function setProtocol($a_prot)
305  {
306  $this->protocol = $a_prot;
307  }
308 
315  public function getProtocol()
316  {
317  return $this->protocol;
318  }
319 
327  public function setPort($a_port)
328  {
329  $this->port = $a_port;
330  }
331 
339  public function getPort()
340  {
341  return $this->port;
342  }
343 
351  public function setPollingTime($a_time)
352  {
353  $this->polling = $a_time;
354  }
355 
362  public function getPollingTime()
363  {
364  return $this->polling;
365  }
366 
373  public function getPollingTimeSeconds()
374  {
375  return (int) ($this->polling % 60);
376  }
377 
384  public function getPollingTimeMinutes()
385  {
386  return (int) ($this->polling / 60);
387  }
388 
397  public function setPollingTimeMS($a_min,$a_sec)
398  {
399  $this->setPollingTime(60 * $a_min + $a_sec);
400  }
401 
409  public function setClientCertPath($a_path)
410  {
411  $this->client_cert_path = $a_path;
412  }
413 
419  public function getClientCertPath()
420  {
422  }
423 
431  public function setCACertPath($a_ca)
432  {
433  $this->ca_cert_path = $a_ca;
434  }
435 
442  public function getCACertPath()
443  {
444  return $this->ca_cert_path;
445  }
446 
453  public function getKeyPath()
454  {
455  return $this->key_path;
456  }
457 
465  public function setKeyPath($a_path)
466  {
467  $this->key_path = $a_path;
468  }
469 
476  public function getKeyPassword()
477  {
478  return $this->key_password;
479  }
480 
488  public function setKeyPassword($a_pass)
489  {
490  $this->key_password = $a_pass;
491  }
492 
500  public function setImportId($a_id)
501  {
502  $this->import_id = $a_id;
503  }
504 
510  public function getImportId()
511  {
512  return $this->import_id;
513  }
514 
522  public function setCertSerialNumber($a_cert_serial)
523  {
524  $this->cert_serial_number = $a_cert_serial;
525  }
526 
533  public function getCertSerialNumber()
534  {
535  return $this->cert_serial_number;
536  }
537 
544  public function getGlobalRole()
545  {
546  return $this->global_role;
547  }
548 
556  public function setGlobalRole($a_role_id)
557  {
558  $this->global_role = $a_role_id;
559  }
560 
568  public function setDuration($a_duration)
569  {
570  $this->duration = $a_duration;
571  }
572 
579  public function getDuration()
580  {
581  return $this->duration ? $this->duration : self::DEFAULT_DURATION;
582  }
583 
590  public function getUserRecipients()
591  {
592  return explode(',',(string) $this->user_recipients);
593  }
594 
601  public function getUserRecipientsAsString()
602  {
603  return $this->user_recipients ? $this->user_recipients : '';
604  }
605 
613  public function setUserRecipients($a_logins)
614  {
615  $this->user_recipients = $a_logins;
616  }
617 
624  public function getEContentRecipients()
625  {
626  return explode(',',$this->econtent_recipients);
627  }
628 
636  {
637  return $this->econtent_recipients ? $this->econtent_recipients : '';
638  }
639 
647  public function setEContentRecipients($a_logins)
648  {
649  $this->econtent_recipients = $a_logins;
650  }
651 
658  public function getApprovalRecipients()
659  {
660  return explode(',',$this->approval_recipients);
661  }
662 
671  {
672  return $this->approval_recipients ? $this->approval_recipients : '';
673  }
674 
681  public function setApprovalRecipients($a_rcp)
682  {
683  $this->approval_recipients = $a_rcp;
684  }
685 
694  public function validate()
695  {
696  if(!$this->isEnabled())
697  {
698  return '';
699  }
700 
701  // Cert based authentication
702  if($this->getAuthType() == self::AUTH_CERTIFICATE)
703  {
704  if(!$this->getClientCertPath() or !$this->getCACertPath() or !$this->getKeyPath() or !$this->getKeyPassword())
705  {
706  return self::ERROR_REQUIRED;
707  }
708  // Check import id
709  if(!$this->fetchSerialID())
710  {
712  }
713  if(!$this->fetchCertificateExpiration())
714  {
716  }
717  }
718  // Apache auth
719  if($this->getAuthType() == self::AUTH_APACHE)
720  {
721  if(!$this->getAuthUser() or !$this->getAuthPass())
722  {
723  return self::ERROR_REQUIRED;
724  }
725  }
726 
727  // required fields
728  if(!$this->getServer() or !$this->getPort() or !$this->getPollingTime() or !$this->getImportId()
729  or !$this->getGlobalRole() or !$this->getDuration())
730  {
731  return self::ERROR_REQUIRED;
732  }
733 
734  if(!$this->checkImportId())
735  {
737  }
738  return '';
739  }
740 
747  public function checkImportId()
748  {
749  global $ilObjDataCache,$tree;
750 
751  if(!$this->getImportId())
752  {
753  return false;
754  }
755  if($ilObjDataCache->lookupType($ilObjDataCache->lookupObjId($this->getImportId())) != 'cat')
756  {
757  return false;
758  }
759  if($tree->isDeleted($this->getImportId()))
760  {
761  return false;
762  }
763  return true;
764  }
765 
772  public function save()
773  {
774  global $ilDB;
775 
776  $this->server_id = $ilDB->nextId('ecs_server');
777  $ilDB->manipulate($q = 'INSERT INTO ecs_server (server_id,active,title,protocol,server,port,auth_type,client_cert_path,ca_cert_path,'.
778  'key_path,key_password,cert_serial,polling_time,import_id,global_role,econtent_rcp,user_rcp,approval_rcp,duration,auth_user,auth_pass) '.
779  'VALUES ('.
780  $ilDB->quote($this->getServerId(),'integer').', '.
781  $ilDB->quote((int) $this->isEnabled(),'integer').', '.
782  $ilDB->quote($this->getTitle(),'text').', '.
783  $ilDB->quote((int) $this->getProtocol(),'integer').', '.
784  $ilDB->quote($this->getServer(),'text').', '.
785  $ilDB->quote($this->getPort(),'integer').', '.
786  $ilDB->quote($this->getAuthType(),'integer').', '.
787  $ilDB->quote($this->getClientCertPath(),'text').', '.
788  $ilDB->quote($this->getCACertPath(),'text').', '.
789  $ilDB->quote($this->getKeyPath(),'text').', '.
790  $ilDB->quote($this->getKeyPassword(),'text').', '.
791  $ilDB->quote($this->getCertSerialNumber(),'text').', '.
792  $ilDB->quote($this->getPollingTime(),'integer').', '.
793  $ilDB->quote($this->getImportId(),'integer').', '.
794  $ilDB->quote($this->getGlobalRole(),'integer').', '.
795  $ilDB->quote($this->getEContentRecipientsAsString(),'text').', '.
796  $ilDB->quote($this->getUserRecipientsAsString(),'text').', '.
797  $ilDB->quote($this->getApprovalRecipientsAsString(),'text').', '.
798  $ilDB->quote($this->getDuration(),'integer').', '.
799  $ilDB->quote($this->getAuthUser(),'text').', '.
800  $ilDB->quote($this->getAuthPass(),'text').' '.
801  ')'
802  );
803  }
804 
808  public function update()
809  {
810  global $ilDB;
811 
812  $ilDB->manipulate('UPDATE ecs_server SET '.
813  'server_id = '.$ilDB->quote($this->getServerId(),'integer').', '.
814  'active = '.$ilDB->quote((int) $this->isEnabled(),'integer').', '.
815  'title = '.$ilDB->quote($this->getTitle(),'text').', '.
816  'protocol = '.$ilDB->quote((int) $this->getProtocol(),'integer').', '.
817  'server = '.$ilDB->quote($this->getServer(),'text').', '.
818  'port = '.$ilDB->quote($this->getPort(),'integer').', '.
819  'auth_type = '.$ilDB->quote($this->getAuthType(),'integer').', '.
820  'client_cert_path = '.$ilDB->quote($this->getClientCertPath(),'text').', '.
821  'ca_cert_path = '.$ilDB->quote($this->getCACertPath(),'text').', '.
822  'key_path = '.$ilDB->quote($this->getKeyPath(),'text').', '.
823  'key_password = '.$ilDB->quote($this->getKeyPassword(),'text').', '.
824  'cert_serial = '.$ilDB->quote($this->getCertSerialNumber(),'text').', '.
825  'polling_time = '.$ilDB->quote($this->getPollingTime(),'integer').', '.
826  'import_id = '.$ilDB->quote($this->getImportId(),'integer').', '.
827  'global_role = '.$ilDB->quote($this->getGlobalRole(),'integer').', '.
828  'econtent_rcp = '.$ilDB->quote($this->getEContentRecipientsAsString(),'text').', '.
829  'user_rcp = '.$ilDB->quote($this->getUserRecipientsAsString(),'text').', '.
830  'approval_rcp = '.$ilDB->quote($this->getApprovalRecipientsAsString(),'text').', '.
831  'duration = '.$ilDB->quote($this->getDuration(),'integer').', '.
832  'auth_user = '.$ilDB->quote($this->getAuthUser(),'text').', '.
833  'auth_pass = '.$ilDB->quote($this->getAuthPass(),'text').', '.
834  'auth_type = '.$ilDB->quote($this->getAuthType(),'integer').' '.
835  'WHERE server_id = '.$ilDB->quote($this->getServerId(),'integer')
836  );
837  }
838 
842  public function delete()
843  {
844  global $ilDB;
845 
846  // --- cascading delete
847 
848  include_once 'Services/WebServices/ECS/classes/Tree/class.ilECSCmsData.php';
850 
851  include_once 'Services/WebServices/ECS/classes/class.ilECSCommunityCache.php';
853 
854  include_once 'Services/WebServices/ECS/classes/class.ilECSDataMappingSetting.php';
856 
857  include_once 'Services/WebServices/ECS/classes/class.ilECSEventQueueReader.php';
859 
860  include_once 'Services/WebServices/ECS/classes/Mapping/class.ilECSNodeMappingAssignment.php';
862 
863  include_once 'Services/WebServices/ECS/classes/class.ilECSParticipantSetting.php';
865 
866  include_once 'Services/WebServices/ECS/classes/class.ilECSExport.php';
868 
869  // resetting server id to flag items in imported list
870  include_once 'Services/WebServices/ECS/classes/class.ilECSImport.php';
872 
873  $ilDB->manipulate(
874  'DELETE FROM ecs_server '.
875  'WHERE server_id = '.$ilDB->quote($this->getServerId(),'integer')
876  );
877 
878  $this->server_id = NULL;
879  return true;
880  }
881 
882 
888  public function fetchCertificateExpiration()
889  {
890  global $ilLog;
891 
892  if($this->getAuthType() != self::AUTH_CERTIFICATE)
893  {
894  return null;
895  }
896 
897  if(function_exists('openssl_x509_parse') and $cert = openssl_x509_parse('file://'.$this->getClientCertPath()))
898  {
899  if(isset($cert['validTo_time_t']) and $cert['validTo_time_t'])
900  {
901  $dt = new ilDateTime($cert['validTo_time_t'], IL_CAL_UNIX);
902  $ilLog->write(__METHOD__.': Certificate expires at '.ilDatePresentation::formatDate($dt));
903  return $dt;
904  }
905  }
906  return null;
907  }
908 
915  private function fetchSerialID()
916  {
917  global $ilLog;
918 
919  if(function_exists('openssl_x509_parse') and $cert = openssl_x509_parse('file://'.$this->getClientCertPath()))
920  {
921  if(isset($cert['serialNumber']) and $cert['serialNumber'])
922  {
923  $this->setCertSerialNumber($cert['serialNumber']);
924  $ilLog->write(__METHOD__.': Serial number is '.$cert['serialNumber']);
925  return true;
926  }
927  }
928 
929  if(!file_exists($this->getClientCertPath()) or !is_readable($this->getClientCertPath()))
930  {
931  return false;
932  }
933  $lines = file($this->getClientCertPath());
934  $found = false;
935  foreach($lines as $line)
936  {
937  if(strpos($line,'Serial Number:') !== false)
938  {
939  $found = true;
940  $serial_line = explode(':',$line);
941  $serial = (int) trim($serial_line[1]);
942  break;
943 
944  }
945  }
946  if($found)
947  {
948  $this->setCertSerialNumber($serial);
949  return true;
950  }
951  else
952  {
953  return false;
954  }
955  }
956 
962  private function read()
963  {
964  global $ilDB;
965 
966  if(!$this->getServerId())
967  {
968  return false;
969  }
970 
971  $query = 'SELECT * FROM ecs_server '.
972  'WHERE server_id = '.$ilDB->quote($this->getServerId(),'integer');
973  $res = $ilDB->query($query);
974  while($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
975  {
976  $this->setServer($row['server']);
977  $this->setTitle($row['title']);
978  $this->setProtocol($row['protocol']);
979  $this->setPort($row['port']);
980  $this->setClientCertPath($row['client_cert_path']);
981  $this->setCACertPath($row['ca_cert_path']);
982  $this->setKeyPath($row['key_path']);
983  $this->setKeyPassword($row['key_password']);
984  $this->setPollingTime($row['polling_time']);
985  $this->setImportId($row['import_id']);
986  $this->setEnabledStatus((int) $row['active']);
987  $this->setCertSerialNumber($row['cert_serial']);
988  $this->setGlobalRole($row['global_role']);
989  $this->econtent_recipients = $row['econtent_rcp'];
990  $this->approval_recipients = $row['approval_rcp'];
991  $this->user_recipients = $row['user_rcp'];
992  $this->setDuration($row['duration']);
993  $this->setAuthUser($row['auth_user']);
994  $this->setAuthPass($row['auth_pass']);
995  $this->setAuthType($row['auth_type']);
996  }
997  }
998 
1003  public function __clone()
1004  {
1005  $this->server_id = 0;
1006  $this->setTitle($this->getTitle(). ' (Copy)');
1007  $this->setEnabledStatus(false);
1008  $this->setServer('');
1009  $this->setProtocol(self::PROTOCOL_HTTPS);
1010  $this->setPort(0);
1011  $this->setClientCertPath('');
1012  $this->setKeyPath('');
1013  $this->setKeyPassword('');
1014  $this->setCACertPath('');
1015  $this->setCertSerialNumber('');
1016  $this->setAuthType(self::AUTH_CERTIFICATE);
1017  $this->setAuthUser('');
1018  $this->setAuthPass('');
1019  }
1020 }
1021 ?>