ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilClient.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
12 class ilClient
13 {
14  var $id; // client_id (md5 hash)
15  var $dir; // directory name in ilias/clients/
16  var $name; // installation name
17  var $db_exists = false; // db exists?
18  var $db_installed = false; // db installed?
19 
20  var $client_defaults; // default settings
21  var $status; // contains status infos about setup process (todo: move function to this class)
22  var $setup_ok = false; // if client setup was finished at least once, this is set to true
23  var $nic_status; // contains received data of ILIAS-NIC server when registering
24 
28  public $db;
29 
33  public $ini;
34 
39  function ilClient($a_client_id, $a_db_connections)
40  {
41  if ($a_client_id)
42  {
43  $this->id = $a_client_id;
44  $this->ini_file_path = ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId()."/client.ini.php";
45  }
46 
47  $this->db_connections = $a_db_connections;
48 
49  // set path default.ini
50  $this->client_defaults = ILIAS_ABSOLUTE_PATH."/setup/client.master.ini.php";
51  }
52 
58  function init()
59  {
60  $this->ini = new ilIniFile($this->ini_file_path);
61 
62  // load defaults only if no client.ini was found
63  if (!@file_exists($this->ini_file_path))
64  {
65 //echo "<br>A-".$this->ini_file_path."-";
66  $this->ini->GROUPS = parse_ini_file($this->client_defaults,true);
67  return false;
68  }
69 
70  // read client.ini
71  if (!$this->ini->read())
72  {
73  $this->error = get_class($this).": ".$this->ini->getError();
74  return false;
75  }
76 
77  // only for ilias main
78  define("CLIENT_WEB_DIR",ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId());
79  define("CLIENT_DATA_DIR",ILIAS_DATA_DIR."/".$this->getId());
80  define ("DEVMODE",$this->ini->readVariable('system','DEVMODE'));
81  define ("ROOT_FOLDER_ID",$this->ini->readVariable('system','ROOT_FOLDER_ID'));
82  define ("SYSTEM_FOLDER_ID",$this->ini->readVariable('system','SYSTEM_FOLDER_ID'));
83  define ("ROLE_FOLDER_ID",$this->ini->readVariable('system','ROLE_FOLDER_ID'));
84  define ("ANONYMOUS_USER_ID",13);
85  define ("ANONYMOUS_ROLE_ID",14);
86  define ("SYSTEM_USER_ID",6);
87  define ("SYSTEM_ROLE_ID",2);
88 
89  $this->db_exists = $this->connect();
90  if ($this->db_exists)
91  {
92  $this->db_installed = $this->isInstalledDB($this->db);
93  }
94 
95  return true;
96  }
97 
102  function getId()
103  {
104  return $this->id;
105  }
106 
111  function setId($a_client_id)
112  {
113  $this->id = $a_client_id;
114  $this->webspace_dir = ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->id;
115  }
116 
121  function getName()
122  {
123  return $this->ini->readVariable("client","name");
124  }
125 
130  function setName($a_str)
131  {
132  $this->ini->setVariable("client","name",$a_str);
133  }
134 
139  function getDescription()
140  {
141  return $this->ini->readVariable("client","description");
142  }
143 
148  function setDescription($a_str)
149  {
150  $this->ini->setVariable("client","description",$a_str);
151  }
152 
156 /* function getMySQLVersion()
157  {
158  return mysql_get_server_info();
159  }*/
160 
164  function getDB()
165  {
166  return $this->db;
167  }
168 
173  function connect()
174  {
175  // check parameters
176  // To support oracle tnsnames.ora dbname is not required
177  if (!$this->getdbHost() || !$this->getdbUser())
178  {
179  $this->error = "empty_fields";
180  return false;
181  }
182  /*
183  if (!$this->getdbHost() || !$this->getdbName() || !$this->getdbUser())
184  {
185  $this->error = "empty_fields";
186  return false;
187  }
188  */
189 
190  include_once("./Services/Database/classes/class.ilDBWrapperFactory.php");
191  $this->db = ilDBWrapperFactory::getWrapper($this->getdbType(),
192  $this->ini->readVariable("db","inactive_mysqli"));
193  $this->db->setDBUser($this->getdbUser());
194  $this->db->setDBPort($this->getdbPort());
195  $this->db->setDBPassword($this->getdbPass());
196  $this->db->setDBHost($this->getdbHost());
197  $this->db->setDBName($this->getdbName());
198  $con = $this->db->connect(true);
199 
200  if (!$con)
201  {
202  $this->error = "Database connection failed.";
203  return false;
204  }
205  $GLOBALS["ilDB"] = $this->db;
206 
207  $this->db_exists = true;
208  return true;
209  }
210 
216  function isInstalledDB(&$a_db)
217  {
218  if(method_exists($a_db,'loadModule'))
219  {
220  $a_db->loadModule('Manager');
221  }
222  if(!$tables = $a_db->listTables())
223  {
224  return false;
225  }
226 
227  // check existence of some basic tables from ilias3 to determine if ilias3 is already installed in given database
228  if (in_array("object_data",$tables) and in_array("object_reference",$tables) and in_array("usr_data",$tables) and in_array("rbac_ua",$tables))
229  {
230  $this->db_installed = true;
231  return true;
232  }
233  $this->db_installed = false;
234  return false;
235  }
236 
240  function setDSN()
241  {
242  switch($this->getDbType())
243  {
244  case "oracle":
245  //$this->dsn_host = "oci8://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost();
246  $this->dsn_host = array(
247  'phptype' => 'oci8',
248  'hostspec' => $this->getdbHost(),
249  'username' => $this->getdbUser(),
250  'port' => $this->getdbPort(),
251  'password' => $this->getdbPass(),
252  );
253  //$this->dsn = "oci8://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost()."/?service=".$this->getdbName();
254  $this->dsn = $this->dsn = array(
255  'phptype' => 'oci8',
256  'hostspec' => $this->getdbHost(),
257  'username' => $this->getdbUser(),
258  'port' => $this->getdbPort(),
259  'password' => $this->getdbPass(),
260  'service' => $this->getdbName()
261  );
262  break;
263 
264  case "postgres":
265  $db_port_str = "";
266  if (trim($this->getdbPort()) != "")
267  {
268  $db_port_str = ":".$this->getdbPort();
269  }
270  $this->dsn_host = "pgsql://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost().$db_port_str;
271  $this->dsn = "pgsql://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost().$db_port_str."/".$this->getdbName();
272  break;
273 
274  case "mysql":
275  case "innodb":
276  default:
277  $db_port_str = "";
278  if (trim($this->getdbPort()) != "")
279  {
280  $db_port_str = ":".$this->getdbPort();
281  }
282  $this->dsn_host = "mysql://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost().$db_port_str;
283  $this->dsn = "mysql://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost().$db_port_str."/".$this->getdbName();
284  break;
285  }
286  }
287 
292  function setDbHost($a_str)
293  {
294  $this->ini->setVariable("db","host",$a_str);
295  }
296 
302  function getDbHost()
303  {
304  return $this->ini->readVariable("db","host");
305  }
306 
311  function setDbName($a_str)
312  {
313  $this->ini->setVariable("db","name",$a_str);
314  }
315 
320  function getDbName()
321  {
322  return $this->ini->readVariable("db","name");
323  }
324 
329  function setDbUser($a_str)
330  {
331  $this->ini->setVariable("db","user",$a_str);
332  }
333 
338  function getDbUser()
339  {
340  return $this->ini->readVariable("db","user");
341  }
342 
347  function getDbPort()
348  {
349  return $this->ini->readVariable("db","port");
350  }
351 
356  function setDbPort($a_str)
357  {
358  $this->ini->setVariable("db","port",$a_str);
359  }
360 
365  function setDbPass($a_str)
366  {
367  $this->ini->setVariable("db","pass",$a_str);
368  }
369 
374  function getDbPass()
375  {
376  return $this->ini->readVariable("db","pass");
377  }
378 
383  function setDbSlaveActive($a_act)
384  {
385  $this->ini->setVariable("db","slave_active", (int) $a_act);
386  }
387 
393  function getDbSlaveActive()
394  {
395  return (int) $this->ini->readVariable("db","slave_active");
396  }
397 
402  function setDbSlaveHost($a_str)
403  {
404  $this->ini->setVariable("db","slave_host",$a_str);
405  }
406 
412  function getDbSlaveHost()
413  {
414  return $this->ini->readVariable("db","slave_host");
415  }
416 
421  function setDbSlaveName($a_str)
422  {
423  $this->ini->setVariable("db","slave_name",$a_str);
424  }
425 
430  function getDbSlaveName()
431  {
432  return $this->ini->readVariable("db","slave_name");
433  }
434 
439  function setDbSlaveUser($a_str)
440  {
441  $this->ini->setVariable("db","slave_user",$a_str);
442  }
443 
448  function getDbSlaveUser()
449  {
450  return $this->ini->readVariable("db","slave_user");
451  }
452 
457  function getDbSlavePort()
458  {
459  return $this->ini->readVariable("db","slave_port");
460  }
461 
466  function setDbSlavePort($a_str)
467  {
468  $this->ini->setVariable("db","slave_port",$a_str);
469  }
470 
475  function setDbSlavePass($a_str)
476  {
477  $this->ini->setVariable("db","slave_pass",$a_str);
478  }
479 
484  function getDbSlavePass()
485  {
486  return $this->ini->readVariable("db","slave_pass");
487  }
488 
493  function setDbType($a_str)
494  {
495  $this->ini->setVariable("db","type",$a_str);
496  }
497 
502  function getDbType()
503  {
504  $val = $this->ini->readVariable("db","type");
505  if ($val == "")
506  {
507  return "mysql";
508  }
509  else
510  {
511  return $val;
512  }
513  }
514 
519  function getDataDir()
520  {
521  return ILIAS_DATA_DIR."/".$this->getId();
522  }
523 
528  function getWebspaceDir()
529  {
530  return ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId();
531  }
532 
537  function checkDatabaseHost()
538  {
539  global $lng;
540 
541  if ($this->getDbType() == "oracle")
542  {
543  return true;
544  }
545 
546  //connect to databasehost
547  $db = $this->db_connections->connectHost($this->dsn_host);
548  if (MDB2::isError($db))
549  {
550  //$this->error = $db->getMessage()."! Please check database hostname, username & password.";
551  $this->error = $db->getMessage()." - ".$db->getUserInfo()." - ".$lng->txt("db_error_please_check");
552  return false;
553  }
554 
555  return true;
556  }
557 
562  function checkDatabaseExists($a_keep_connection = false)
563  {
564  //try to connect to database
565  $db = $this->db_connections->connectDB($this->dsn);
566  if (MDB2::isError($db))
567  {
568  return false;
569  }
570 
571  if (!$this->isInstalledDB($db))
572  {
573  return false;
574  }
575 
576  // #10633
577  if($a_keep_connection)
578  {
579  $GLOBALS["ilDB"] = $this->db;
580  }
581 
582  return true;
583  }
584 
585  function reconnect()
586  {
587  $this->connect();
588  }
589 
596  function getSetting($a_keyword)
597  {
598  include_once './Services/Administration/classes/class.ilSetting.php';
599  $set = new ilSetting("common", true);
600  return $set->get($a_keyword);
601  }
602 
608  function getAllSettings()
609  {
610  include_once './Services/Administration/classes/class.ilSetting.php';
611  $set = new ilSetting("common", true);
612  return $set->getAll();
613  }
614 
622  function setSetting($a_key, $a_val)
623  {
624  include_once './Services/Administration/classes/class.ilSetting.php';
625  $set = new ilSetting("common", true);
626  $set->set($a_key, $a_val);
627  }
628 
633  function getURLStringForNIC($a_nic_url)
634  {
635  $settings = $this->getAllSettings();
636 
637  $inst_id = (empty($settings["inst_id"])) ? "0" : $settings["inst_id"];
638 
639  // send host information to ilias-nic
640  //#18132: removed ipadr, server_port, server_software, institution, contact_title, contact_position,
641  // contact_institution, contact_street, contact_pcode, contact_city, contact_country, contact_phone
642  $url = $a_nic_url.
643  "?cmd=getid".
644  "&inst_id=".rawurlencode($inst_id).
645  "&hostname=".rawurlencode($_SERVER["SERVER_NAME"]).
646  "&inst_name=".rawurlencode($this->ini->readVariable("client","name")).
647  "&inst_info=".rawurlencode($this->ini->readVariable("client","description")).
648  "&http_path=".rawurlencode(ILIAS_HTTP_PATH).
649  "&contact_firstname=".rawurlencode($settings["admin_firstname"]).
650  "&contact_lastname=".rawurlencode($settings["admin_lastname"]).
651  "&contact_email=".rawurlencode($settings["admin_email"]).
652  "&nic_key=".rawurlencode($this->getNICkey());
653 
654  return $url;
655  }
656 
671  function updateNIC($a_nic_url)
672  {
673  $settings = $this->getAllSettings();
674  if((bool)$settings['proxy_status'] && strlen($settings['proxy_host']) && strlen($settings['proxy_port']))
675  {
676  $proxy_options = array(
677  'proxy_host' => $settings['proxy_host'],
678  'proxy_port' => $settings['proxy_port']
679  );
680  }
681  else
682  {
683  $proxy_options = array();
684  }
685 
686  include_once('HTTP/Request.php');
687  $url = $this->getURLStringForNIC($a_nic_url);
688  $req = new HTTP_Request($url, $proxy_options);
689 
690  $req->sendRequest();
691  $response = $req->getResponseBody();
692  $response = explode("\n", $response);
693 
694  $this->nic_status = $response;
695 
696  return true;
697  }
698 
707  function setNICkey()
708  {
709  mt_srand((double)microtime()*1000000);
710  $nic_key = md5(str_replace(".","",$_SERVER["SERVER_ADDR"]) +
711  mt_rand(100000,999999));
712 
713  $this->setSetting("nic_key",$nic_key);
714 
715  $this->nic_key = $nic_key;
716 
717  return true;
718  }
719 
725  function getNICkey()
726  {
727  $this->nic_key = $this->getSetting("nic_key");
728 
729  if (empty($this->nic_key))
730  {
731  $this->setNICkey();
732  }
733 
734  return $this->nic_key;
735  }
736 
738  {
739  return $this->getSetting("language");
740  }
741 
742  function setDefaultLanguage($a_lang_key)
743  {
744  $this->setSetting("language",$a_lang_key);
745  $this->ini->setVariable("language","default",$a_lang_key);
746  $this->ini->write();
747 
748  return true;
749  }
750 
755  function getError()
756  {
757  $error = $this->error;
758  $this->error = "";
759 
760  return $error;
761  }
762 
771  function delete ($a_ini = true, $a_db = false, $a_files = false)
772  {
773  if ($a_ini === true and file_exists(ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId()."/client.ini.php"))
774  {
775  unlink(CLIENT_WEB_DIR."/client.ini.php");
776  $msg[] = "ini_deleted";
777  }
778 
779  if ($a_db === true and $this->db_exists)
780  {
781  $this->db->query("DROP DATABASE ".$this->getDbName());
782  $msg[] = "db_deleted";
783  }
784 
785  if ($a_files === true and file_exists(CLIENT_WEB_DIR) and is_dir(CLIENT_WEB_DIR))
786  {
787  // rmdir();
788  ilUtil::delDir(CLIENT_WEB_DIR);
789  ilUtil::delDir(CLIENT_DATA_DIR);
790  $msg[] = "files_deleted";
791  }
792 
793  return $msg;
794  }
795 
800  function create()
801  {
802  //var_dump($this->getDataDir());exit;
803  // create base data dir
804  if (!ilUtil::makeDir($this->getDataDir()))
805  {
806  $this->error = "could_not_create_base_data_dir :".$this->getDataDir();
807  return false;
808  }
809 
810  // create sub dirs in base data dir
811  if (!ilUtil::makeDir($this->getDataDir()."/mail"))
812  {
813  $this->error = "could_not_create_mail_data_dir :".$this->getDataDir()."/mail";
814  return false;
815  }
816 
817  if (!ilUtil::makeDir($this->getDataDir()."/lm_data"))
818  {
819  $this->error = "could_not_create_lm_data_dir :".$this->getDataDir()."/lm_data";
820  return false;
821  }
822 
823  if (!ilUtil::makeDir($this->getDataDir()."/forum"))
824  {
825  $this->error = "could_not_create_forum_data_dir :".$this->getDataDir()."/forum";
826  return false;
827  }
828 
829  if (!ilUtil::makeDir($this->getDataDir()."/files"))
830  {
831  $this->error = "could_not_create_files_data_dir :".$this->getDataDir()."/files";
832  return false;
833  }
834 
835  // create base webspace dir
836  if (!ilUtil::makeDir($this->getWebspaceDir()))
837  {
838  $this->error = "could_not_create_base_webspace_dir :".$this->getWebspaceDir();
839  return false;
840  }
841 
842  // create sub dirs in base webspace dir
843  if (!ilUtil::makeDir($this->getWebspaceDir()."/lm_data"))
844  {
845  $this->error = "could_not_create_lm_webspace_dir :".$this->getWebspaceDir()."/lm_data";
846  return false;
847  }
848 
849  if (!ilUtil::makeDir($this->getWebspaceDir()."/usr_images"))
850  {
851  $this->error = "could_not_create_usr_images_webspace_dir :".$this->getWebspaceDir()."/usr_images";
852  return false;
853  }
854 
855  if (!ilUtil::makeDir($this->getWebspaceDir()."/mobs"))
856  {
857  $this->error = "could_not_create_mobs_webspace_dir :".$this->getWebspaceDir()."/mobs";
858  return false;
859  }
860 
861  if (!ilUtil::makeDir($this->getWebspaceDir()."/css"))
862  {
863  $this->error = "could_not_create_css_webspace_dir :".$this->getWebspaceDir()."/css";
864  return false;
865  }
866 
867  // write client ini
868  if (!$this->ini->write())
869  {
870  $this->error = get_class($this).": ".$this->ini->getError();
871  return false;
872  }
873 
874  return true;
875  }
876 
883  function writeIni()
884  {
885  $this->ini->write();
886  }
887 
888 } // END class.ilClient
889 ?>