ILIAS  Release_4_2_x_branch Revision 61807
 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 ERROR_EXTRACT_SERIAL = 'ecs_error_extract_serial';
36  const ERROR_REQUIRED = 'fill_out_all_required_fields';
37  const ERROR_INVALID_IMPORT_ID = 'ecs_check_import_id';
38  const ERROR_CERT_EXPIRED = 'ecs_certificate_expired';
39 
40  const AUTH_CERTIFICATE = 1;
41  const AUTH_APACHE = 2;
42 
43  const DEFAULT_DURATION = 6;
44 
45 
46  const PROTOCOL_HTTP = 0;
47  const PROTOCOL_HTTPS = 1;
48 
49  protected static $instances = null;
50 
51 
52  private $server_id = 0;
53  private $active = false;
54  private $title = '';
56  private $server;
57  private $protocol;
58  private $port;
60  private $ca_cert_path;
61  private $key_path;
62  private $key_password;
63  private $polling;
64  private $import_id;
65  private $cert_serial;
66  private $global_role;
67  private $duration;
68 
69  private $auth_user = '';
70  private $auth_pass = '';
71 
72  private $user_recipients = array();
73  private $econtent_recipients = array();
74  private $approval_recipients = array();
75 
81  private function __construct($a_server_id = 0)
82  {
83  $this->server_id = $a_server_id;
84  $this->read();
85  }
86 
95  public static function _getInstance()
96  {
97  $GLOBALS['ilLog']->write(__METHOD__.': Using deprecated call.');
98  $GLOBALS['ilLog']->logStack();
99  return self::getInstanceByServerId(15);
100  }
101 
107  public static function getInstanceByServerId($a_server_id)
108  {
109  if(self::$instances[$a_server_id])
110  {
111  return self::$instances[$a_server_id];
112  }
113  return self::$instances[$a_server_id] = new ilECSSetting($a_server_id);
114  }
115 
120  public function setTitle($a_title)
121  {
122  $this->title = $a_title;
123  }
124 
129  public function getTitle()
130  {
131  return $this->title;
132  }
133 
138  public function setAuthType($a_auth_type)
139  {
140  $this->auth_type = $a_auth_type;
141  }
142 
147  public function getAuthType()
148  {
149  return $this->auth_type;
150  }
151 
156  public function setAuthUser($a_user)
157  {
158  $this->auth_user = $a_user;
159  }
160 
165  public function getAuthUser()
166  {
167  return $this->auth_user;
168  }
169 
174  public function setAuthPass($a_pass)
175  {
176  $this->auth_pass = $a_pass;
177  }
178 
183  public function getAuthPass()
184  {
185  return $this->auth_pass;
186  }
187 
192  public function getServerId()
193  {
194  return (int) $this->server_id;
195  }
196 
204  public function setEnabledStatus($a_status)
205  {
206  $this->active = $a_status;
207  }
208 
215  public function isEnabled()
216  {
217  return $this->active;
218  }
219 
227  public function setServer($a_server)
228  {
229  $this->server = $a_server;
230  }
231 
239  public function getServer()
240  {
241  return $this->server;
242  }
243 
250  public function getServerURI()
251  {
252  switch($this->getProtocol())
253  {
254  case self::PROTOCOL_HTTP:
255  $uri = 'http://';
256  break;
257 
258  case self::PROTOCOL_HTTPS:
259  $uri = 'https://';
260  break;
261  }
262 
263  if(stristr($this->getServer(), '/'))
264  {
265  $counter = 0;
266  foreach((array) explode('/',$this->getServer()) as $key => $part)
267  {
268  $uri .= $part;
269  if(!$counter)
270  {
271  $uri .= ':'.$this->getPort();
272  }
273  $uri .= '/';
274  ++$counter;
275  }
276  $uri = substr($uri,0,-1);
277  }
278  else
279  {
280  $uri .= $this->getServer();
281  $uri .= (':'.$this->getPort());
282  }
283 
284  return $uri;
285  }
286 
294  public function setProtocol($a_prot)
295  {
296  $this->protocol = $a_prot;
297  }
298 
305  public function getProtocol()
306  {
307  return $this->protocol;
308  }
309 
317  public function setPort($a_port)
318  {
319  $this->port = $a_port;
320  }
321 
329  public function getPort()
330  {
331  return $this->port;
332  }
333 
341  public function setPollingTime($a_time)
342  {
343  $this->polling = $a_time;
344  }
345 
352  public function getPollingTime()
353  {
354  return $this->polling;
355  }
356 
363  public function getPollingTimeSeconds()
364  {
365  return (int) ($this->polling % 60);
366  }
367 
374  public function getPollingTimeMinutes()
375  {
376  return (int) ($this->polling / 60);
377  }
378 
387  public function setPollingTimeMS($a_min,$a_sec)
388  {
389  $this->setPollingTime(60 * $a_min + $a_sec);
390  }
391 
399  public function setClientCertPath($a_path)
400  {
401  $this->client_cert_path = $a_path;
402  }
403 
409  public function getClientCertPath()
410  {
412  }
413 
421  public function setCACertPath($a_ca)
422  {
423  $this->ca_cert_path = $a_ca;
424  }
425 
432  public function getCACertPath()
433  {
434  return $this->ca_cert_path;
435  }
436 
443  public function getKeyPath()
444  {
445  return $this->key_path;
446  }
447 
455  public function setKeyPath($a_path)
456  {
457  $this->key_path = $a_path;
458  }
459 
466  public function getKeyPassword()
467  {
468  return $this->key_password;
469  }
470 
478  public function setKeyPassword($a_pass)
479  {
480  $this->key_password = $a_pass;
481  }
482 
490  public function setImportId($a_id)
491  {
492  $this->import_id = $a_id;
493  }
494 
500  public function getImportId()
501  {
502  return $this->import_id;
503  }
504 
512  public function setCertSerialNumber($a_cert_serial)
513  {
514  $this->cert_serial_number = $a_cert_serial;
515  }
516 
523  public function getCertSerialNumber()
524  {
525  return $this->cert_serial_number;
526  }
527 
534  public function getGlobalRole()
535  {
536  return $this->global_role;
537  }
538 
546  public function setGlobalRole($a_role_id)
547  {
548  $this->global_role = $a_role_id;
549  }
550 
558  public function setDuration($a_duration)
559  {
560  $this->duration = $a_duration;
561  }
562 
569  public function getDuration()
570  {
571  return $this->duration ? $this->duration : self::DEFAULT_DURATION;
572  }
573 
580  public function getUserRecipients()
581  {
582  return explode(',',$this->user_recipients);
583  }
584 
591  public function getUserRecipientsAsString()
592  {
593  return $this->user_recipients ? $this->user_recipients : '';
594  }
595 
603  public function setUserRecipients($a_logins)
604  {
605  $this->user_recipients = $a_logins;
606  }
607 
614  public function getEContentRecipients()
615  {
616  return explode(',',$this->econtent_recipients);
617  }
618 
626  {
627  return $this->econtent_recipients ? $this->econtent_recipients : '';
628  }
629 
637  public function setEContentRecipients($a_logins)
638  {
639  $this->econtent_recipients = $a_logins;
640  }
641 
648  public function getApprovalRecipients()
649  {
650  return explode(',',$this->approval_recipients);
651  }
652 
661  {
662  return $this->approval_recipients ? $this->approval_recipients : '';
663  }
664 
671  public function setApprovalRecipients($a_rcp)
672  {
673  $this->approval_recipients = $a_rcp;
674  }
675 
684  public function validate()
685  {
686  if(!$this->isEnabled())
687  {
688  return '';
689  }
690 
691  // Cert based authentication
692  if($this->getAuthType() == self::AUTH_CERTIFICATE)
693  {
694  if(!$this->getClientCertPath() or !$this->getCACertPath() or !$this->getKeyPath() or !$this->getKeyPassword())
695  {
696  return self::ERROR_REQUIRED;
697  }
698  // Check import id
699  if(!$this->fetchSerialID())
700  {
702  }
703  if(!$this->fetchCertificateExpiration())
704  {
706  }
707  }
708  // Apache auth
709  if($this->getAuthType() == self::AUTH_APACHE)
710  {
711  if(!$this->getAuthUser() or !$this->getAuthPass())
712  {
713  return self::ERROR_REQUIRED;
714  }
715  }
716 
717  // required fields
718  if(!$this->getServer() or !$this->getPort() or !$this->getPollingTime() or !$this->getImportId()
719  or !$this->getGlobalRole() or !$this->getDuration())
720  {
721  return self::ERROR_REQUIRED;
722  }
723 
724  if(!$this->checkImportId())
725  {
727  }
728  return '';
729  }
730 
737  public function checkImportId()
738  {
739  global $ilObjDataCache,$tree;
740 
741  if(!$this->getImportId())
742  {
743  return false;
744  }
745  if($ilObjDataCache->lookupType($ilObjDataCache->lookupObjId($this->getImportId())) != 'cat')
746  {
747  return false;
748  }
749  if($tree->isDeleted($this->getImportId()))
750  {
751  return false;
752  }
753  return true;
754  }
755 
762  public function save()
763  {
764  global $ilDB;
765 
766  $this->server_id = $ilDB->nextId('ecs_server');
767  $ilDB->manipulate($q = 'INSERT INTO ecs_server (server_id,active,title,protocol,server,port,auth_type,client_cert_path,ca_cert_path,'.
768  'key_path,key_password,cert_serial,polling_time,import_id,global_role,econtent_rcp,user_rcp,approval_rcp,duration,auth_user,auth_pass) '.
769  'VALUES ('.
770  $ilDB->quote($this->getServerId(),'integer').', '.
771  $ilDB->quote((int) $this->isEnabled(),'integer').', '.
772  $ilDB->quote($this->getTitle(),'text').', '.
773  $ilDB->quote((int) $this->getProtocol(),'integer').', '.
774  $ilDB->quote($this->getServer(),'text').', '.
775  $ilDB->quote($this->getPort(),'integer').', '.
776  $ilDB->quote($this->getAuthType(),'integer').', '.
777  $ilDB->quote($this->getClientCertPath(),'text').', '.
778  $ilDB->quote($this->getCACertPath(),'text').', '.
779  $ilDB->quote($this->getKeyPath(),'text').', '.
780  $ilDB->quote($this->getKeyPassword(),'text').', '.
781  $ilDB->quote($this->getCertSerialNumber(),'text').', '.
782  $ilDB->quote($this->getPollingTime(),'integer').', '.
783  $ilDB->quote($this->getImportId(),'integer').', '.
784  $ilDB->quote($this->getGlobalRole(),'integer').', '.
785  $ilDB->quote($this->getEContentRecipientsAsString(),'text').', '.
786  $ilDB->quote($this->getUserRecipientsAsString(),'text').', '.
787  $ilDB->quote($this->getApprovalRecipientsAsString(),'text').', '.
788  $ilDB->quote($this->getDuration(),'integer').', '.
789  $ilDB->quote($this->getAuthUser(),'text').', '.
790  $ilDB->quote($this->getAuthPass(),'text').' '.
791  ')'
792  );
793  }
794 
798  public function update()
799  {
800  global $ilDB;
801 
802  $ilDB->manipulate('UPDATE ecs_server SET '.
803  'server_id = '.$ilDB->quote($this->getServerId(),'integer').', '.
804  'active = '.$ilDB->quote((int) $this->isEnabled(),'integer').', '.
805  'title = '.$ilDB->quote($this->getTitle(),'text').', '.
806  'protocol = '.$ilDB->quote((int) $this->getProtocol(),'integer').', '.
807  'server = '.$ilDB->quote($this->getServer(),'text').', '.
808  'port = '.$ilDB->quote($this->getPort(),'integer').', '.
809  'auth_type = '.$ilDB->quote($this->getAuthType(),'integer').', '.
810  'client_cert_path = '.$ilDB->quote($this->getClientCertPath(),'text').', '.
811  'ca_cert_path = '.$ilDB->quote($this->getCACertPath(),'text').', '.
812  'key_path = '.$ilDB->quote($this->getKeyPath(),'text').', '.
813  'key_password = '.$ilDB->quote($this->getKeyPassword(),'text').', '.
814  'cert_serial = '.$ilDB->quote($this->getCertSerialNumber(),'text').', '.
815  'polling_time = '.$ilDB->quote($this->getPollingTime(),'integer').', '.
816  'import_id = '.$ilDB->quote($this->getImportId(),'integer').', '.
817  'global_role = '.$ilDB->quote($this->getGlobalRole(),'integer').', '.
818  'econtent_rcp = '.$ilDB->quote($this->getEContentRecipientsAsString(),'text').', '.
819  'user_rcp = '.$ilDB->quote($this->getUserRecipientsAsString(),'text').', '.
820  'approval_rcp = '.$ilDB->quote($this->getApprovalRecipientsAsString(),'text').', '.
821  'duration = '.$ilDB->quote($this->getDuration(),'integer').', '.
822  'auth_user = '.$ilDB->quote($this->getAuthUser(),'text').', '.
823  'auth_pass = '.$ilDB->quote($this->getAuthPass(),'text').', '.
824  'auth_type = '.$ilDB->quote($this->getAuthType(),'integer').' '.
825  'WHERE server_id = '.$ilDB->quote($this->getServerId(),'integer')
826  );
827  }
828 
832  public function delete()
833  {
834  global $ilDB;
835 
836  $ilDB->manipulate(
837  'DELETE FROM ecs_server '.
838  'WHERE server_id = '.$ilDB->quote($this->getServerId(),'integer')
839  );
840  return true;
841  }
842 
843 
849  public function fetchCertificateExpiration()
850  {
851  global $ilLog;
852 
853  if($this->getAuthType() != self::AUTH_CERTIFICATE)
854  {
855  return null;
856  }
857 
858  if(function_exists('openssl_x509_parse') and $cert = openssl_x509_parse('file://'.$this->getClientCertPath()))
859  {
860  if(isset($cert['validTo_time_t']) and $cert['validTo_time_t'])
861  {
862  $dt = new ilDateTime($cert['validTo_time_t'], IL_CAL_UNIX);
863  $ilLog->write(__METHOD__.': Certificate expires at '.ilDatePresentation::formatDate($dt));
864  return $dt;
865  }
866  }
867  return null;
868  }
869 
876  private function fetchSerialID()
877  {
878  global $ilLog;
879 
880  if(function_exists('openssl_x509_parse') and $cert = openssl_x509_parse('file://'.$this->getClientCertPath()))
881  {
882  if(isset($cert['serialNumber']) and $cert['serialNumber'])
883  {
884  $this->setCertSerialNumber($cert['serialNumber']);
885  $ilLog->write(__METHOD__.': Serial number is '.$cert['serialNumber']);
886  return true;
887  }
888  }
889 
890  if(!file_exists($this->getClientCertPath()) or !is_readable($this->getClientCertPath()))
891  {
892  return false;
893  }
894  $lines = file($this->getClientCertPath());
895  $found = false;
896  foreach($lines as $line)
897  {
898  if(strpos($line,'Serial Number:') !== false)
899  {
900  $found = true;
901  $serial_line = explode(':',$line);
902  $serial = (int) trim($serial_line[1]);
903  break;
904 
905  }
906  }
907  if($found)
908  {
909  $this->setCertSerialNumber($serial);
910  return true;
911  }
912  else
913  {
914  return false;
915  }
916  }
917 
923  private function read()
924  {
925  global $ilDB;
926 
927  if(!$this->getServerId())
928  {
929  return false;
930  }
931 
932  $query = 'SELECT * FROM ecs_server '.
933  'WHERE server_id = '.$ilDB->quote($this->getServerId(),'integer');
934  $res = $ilDB->query($query);
935  while($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
936  {
937  $this->setServer($row['server']);
938  $this->setTitle($row['title']);
939  $this->setProtocol($row['protocol']);
940  $this->setPort($row['port']);
941  $this->setClientCertPath($row['client_cert_path']);
942  $this->setCACertPath($row['ca_cert_path']);
943  $this->setKeyPath($row['key_path']);
944  $this->setKeyPassword($row['key_password']);
945  $this->setPollingTime($row['polling_time']);
946  $this->setImportId($row['import_id']);
947  $this->setEnabledStatus((int) $row['active']);
948  $this->setCertSerialNumber($row['cert_serial']);
949  $this->setGlobalRole($row['global_role']);
950  $this->econtent_recipients = $row['econtent_rcp'];
951  $this->approval_recipients = $row['approval_rcp'];
952  $this->user_recipients = $row['user_rcp'];
953  $this->setDuration($row['duration']);
954  $this->setAuthUser($row['auth_user']);
955  $this->setAuthPass($row['auth_pass']);
956  $this->setAuthType($row['auth_type']);
957  }
958  }
959 
964  public function __clone()
965  {
966  $this->server_id = 0;
967  $this->setTitle($this->getTitle(). ' (Copy)');
968  $this->setEnabledStatus(false);
969  $this->setServer('');
970  $this->setProtocol(self::PROTOCOL_HTTPS);
971  $this->setPort(0);
972  $this->setClientCertPath('');
973  $this->setKeyPath('');
974  $this->setKeyPassword('');
975  $this->setCACertPath('');
976  $this->setCertSerialNumber('');
977  $this->setAuthType(self::AUTH_CERTIFICATE);
978  $this->setAuthUser('');
979  $this->setAuthPass('');
980  }
981 }
982 ?>