ILIAS  Release_4_4_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 
29  function ilClient($a_client_id, $a_db_connections)
30  {
31  if ($a_client_id)
32  {
33  $this->id = $a_client_id;
34  $this->ini_file_path = ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId()."/client.ini.php";
35  }
36 
37  $this->db_connections = $a_db_connections;
38 
39  // set path default.ini
40  $this->client_defaults = ILIAS_ABSOLUTE_PATH."/setup/client.master.ini.php";
41  }
42 
48  function init()
49  {
50  $this->ini = new ilIniFile($this->ini_file_path);
51 
52  // load defaults only if no client.ini was found
53  if (!@file_exists($this->ini_file_path))
54  {
55 //echo "<br>A-".$this->ini_file_path."-";
56  $this->ini->GROUPS = parse_ini_file($this->client_defaults,true);
57  return false;
58  }
59 
60  // read client.ini
61  if (!$this->ini->read())
62  {
63  $this->error = get_class($this).": ".$this->ini->getError();
64  return false;
65  }
66 
67  // only for ilias main
68  define("CLIENT_WEB_DIR",ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId());
69  define("CLIENT_DATA_DIR",ILIAS_DATA_DIR."/".$this->getId());
70  define ("DEVMODE",$this->ini->readVariable('system','DEVMODE'));
71  define ("ROOT_FOLDER_ID",$this->ini->readVariable('system','ROOT_FOLDER_ID'));
72  define ("SYSTEM_FOLDER_ID",$this->ini->readVariable('system','SYSTEM_FOLDER_ID'));
73  define ("ROLE_FOLDER_ID",$this->ini->readVariable('system','ROLE_FOLDER_ID'));
74  define ("ANONYMOUS_USER_ID",13);
75  define ("ANONYMOUS_ROLE_ID",14);
76  define ("SYSTEM_USER_ID",6);
77  define ("SYSTEM_ROLE_ID",2);
78 
79  $this->db_exists = $this->connect();
80  if ($this->db_exists)
81  {
82  $this->db_installed = $this->isInstalledDB($this->db);
83  }
84 
85  return true;
86  }
87 
92  function getId()
93  {
94  return $this->id;
95  }
96 
101  function setId($a_client_id)
102  {
103  $this->id = $a_client_id;
104  $this->webspace_dir = ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->id;
105  }
106 
111  function getName()
112  {
113  return $this->ini->readVariable("client","name");
114  }
115 
120  function setName($a_str)
121  {
122  $this->ini->setVariable("client","name",$a_str);
123  }
124 
129  function getDescription()
130  {
131  return $this->ini->readVariable("client","description");
132  }
133 
138  function setDescription($a_str)
139  {
140  $this->ini->setVariable("client","description",$a_str);
141  }
142 
146 /* function getMySQLVersion()
147  {
148  return mysql_get_server_info();
149  }*/
150 
154  function getDB()
155  {
156  return $this->db;
157  }
158 
163  function connect()
164  {
165  // check parameters
166  // To support oracle tnsnames.ora dbname is not required
167  if (!$this->getdbHost() || !$this->getdbUser())
168  {
169  $this->error = "empty_fields";
170  return false;
171  }
172  /*
173  if (!$this->getdbHost() || !$this->getdbName() || !$this->getdbUser())
174  {
175  $this->error = "empty_fields";
176  return false;
177  }
178  */
179 
180  include_once("./Services/Database/classes/class.ilDBWrapperFactory.php");
181  $this->db = ilDBWrapperFactory::getWrapper($this->getdbType(),
182  $this->ini->readVariable("db","inactive_mysqli"));
183  $this->db->setDBUser($this->getdbUser());
184  $this->db->setDBPort($this->getdbPort());
185  $this->db->setDBPassword($this->getdbPass());
186  $this->db->setDBHost($this->getdbHost());
187  $this->db->setDBName($this->getdbName());
188  $con = $this->db->connect(true);
189 
190  if (!$con)
191  {
192  $this->error = "Database connection failed.";
193  return false;
194  }
195  $GLOBALS["ilDB"] = $this->db;
196 
197  $this->db_exists = true;
198  return true;
199  }
200 
206  function isInstalledDB(&$a_db)
207  {
208  if(method_exists($a_db,'loadModule'))
209  {
210  $a_db->loadModule('Manager');
211  }
212  if(!$tables = $a_db->listTables())
213  {
214  return false;
215  }
216 
217  // check existence of some basic tables from ilias3 to determine if ilias3 is already installed in given database
218  if (in_array("object_data",$tables) and in_array("object_reference",$tables) and in_array("usr_data",$tables) and in_array("rbac_ua",$tables))
219  {
220  $this->db_installed = true;
221  return true;
222  }
223  $this->db_installed = false;
224  return false;
225  }
226 
230  function setDSN()
231  {
232  switch($this->getDbType())
233  {
234  case "oracle":
235  //$this->dsn_host = "oci8://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost();
236  $this->dsn_host = array(
237  'phptype' => 'oci8',
238  'hostspec' => $this->getdbHost(),
239  'username' => $this->getdbUser(),
240  'port' => $this->getdbPort(),
241  'password' => $this->getdbPass(),
242  );
243  //$this->dsn = "oci8://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost()."/?service=".$this->getdbName();
244  $this->dsn = $this->dsn = array(
245  'phptype' => 'oci8',
246  'hostspec' => $this->getdbHost(),
247  'username' => $this->getdbUser(),
248  'port' => $this->getdbPort(),
249  'password' => $this->getdbPass(),
250  'service' => $this->getdbName()
251  );
252  break;
253 
254  case "postgres":
255  $db_port_str = "";
256  if (trim($this->getdbPort()) != "")
257  {
258  $db_port_str = ":".$this->getdbPort();
259  }
260  $this->dsn_host = "pgsql://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost().$db_port_str;
261  $this->dsn = "pgsql://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost().$db_port_str."/".$this->getdbName();
262  break;
263 
264  case "mysql":
265  case "innodb":
266  default:
267  $db_port_str = "";
268  if (trim($this->getdbPort()) != "")
269  {
270  $db_port_str = ":".$this->getdbPort();
271  }
272  $this->dsn_host = "mysql://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost().$db_port_str;
273  $this->dsn = "mysql://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost().$db_port_str."/".$this->getdbName();
274  break;
275  }
276  }
277 
282  function setDbHost($a_str)
283  {
284  $this->ini->setVariable("db","host",$a_str);
285  }
286 
292  function getDbHost()
293  {
294  return $this->ini->readVariable("db","host");
295  }
296 
301  function setDbName($a_str)
302  {
303  $this->ini->setVariable("db","name",$a_str);
304  }
305 
310  function getDbName()
311  {
312  return $this->ini->readVariable("db","name");
313  }
314 
319  function setDbUser($a_str)
320  {
321  $this->ini->setVariable("db","user",$a_str);
322  }
323 
328  function getDbUser()
329  {
330  return $this->ini->readVariable("db","user");
331  }
332 
337  function getDbPort()
338  {
339  return $this->ini->readVariable("db","port");
340  }
341 
346  function setDbPort($a_str)
347  {
348  $this->ini->setVariable("db","port",$a_str);
349  }
350 
355  function setDbPass($a_str)
356  {
357  $this->ini->setVariable("db","pass",$a_str);
358  }
359 
364  function getDbPass()
365  {
366  return $this->ini->readVariable("db","pass");
367  }
368 
373  function setDbSlaveActive($a_act)
374  {
375  $this->ini->setVariable("db","slave_active", (int) $a_act);
376  }
377 
383  function getDbSlaveActive()
384  {
385  return (int) $this->ini->readVariable("db","slave_active");
386  }
387 
392  function setDbSlaveHost($a_str)
393  {
394  $this->ini->setVariable("db","slave_host",$a_str);
395  }
396 
402  function getDbSlaveHost()
403  {
404  return $this->ini->readVariable("db","slave_host");
405  }
406 
411  function setDbSlaveName($a_str)
412  {
413  $this->ini->setVariable("db","slave_name",$a_str);
414  }
415 
420  function getDbSlaveName()
421  {
422  return $this->ini->readVariable("db","slave_name");
423  }
424 
429  function setDbSlaveUser($a_str)
430  {
431  $this->ini->setVariable("db","slave_user",$a_str);
432  }
433 
438  function getDbSlaveUser()
439  {
440  return $this->ini->readVariable("db","slave_user");
441  }
442 
447  function getDbSlavePort()
448  {
449  return $this->ini->readVariable("db","slave_port");
450  }
451 
456  function setDbSlavePort($a_str)
457  {
458  $this->ini->setVariable("db","slave_port",$a_str);
459  }
460 
465  function setDbSlavePass($a_str)
466  {
467  $this->ini->setVariable("db","slave_pass",$a_str);
468  }
469 
474  function getDbSlavePass()
475  {
476  return $this->ini->readVariable("db","slave_pass");
477  }
478 
483  function setDbType($a_str)
484  {
485  $this->ini->setVariable("db","type",$a_str);
486  }
487 
492  function getDbType()
493  {
494  $val = $this->ini->readVariable("db","type");
495  if ($val == "")
496  {
497  return "mysql";
498  }
499  else
500  {
501  return $val;
502  }
503  }
504 
509  function getDataDir()
510  {
511  return ILIAS_DATA_DIR."/".$this->getId();
512  }
513 
518  function getWebspaceDir()
519  {
520  return ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId();
521  }
522 
527  function checkDatabaseHost()
528  {
529  global $lng;
530 
531  if ($this->getDbType() == "oracle")
532  {
533  return true;
534  }
535 
536  //connect to databasehost
537  $db = $this->db_connections->connectHost($this->dsn_host);
538  if (MDB2::isError($db))
539  {
540  //$this->error = $db->getMessage()."! Please check database hostname, username & password.";
541  $this->error = $db->getMessage()." - ".$db->getUserInfo()." - ".$lng->txt("db_error_please_check");
542  return false;
543  }
544 
545  return true;
546  }
547 
552  function checkDatabaseExists($a_keep_connection = false)
553  {
554  //try to connect to database
555  $db = $this->db_connections->connectDB($this->dsn);
556  if (MDB2::isError($db))
557  {
558  return false;
559  }
560 
561  if (!$this->isInstalledDB($db))
562  {
563  return false;
564  }
565 
566  // #10633
567  if($a_keep_connection)
568  {
569  $GLOBALS["ilDB"] = $this->db;
570  }
571 
572  return true;
573  }
574 
575  function reconnect()
576  {
577  $this->connect();
578  }
579 
586  function getSetting($a_keyword)
587  {
588  include_once './Services/Administration/classes/class.ilSetting.php';
589  $set = new ilSetting("common", true);
590  return $set->get($a_keyword);
591  }
592 
598  function getAllSettings()
599  {
600  include_once './Services/Administration/classes/class.ilSetting.php';
601  $set = new ilSetting("common", true);
602  return $set->getAll();
603  }
604 
612  function setSetting($a_key, $a_val)
613  {
614  include_once './Services/Administration/classes/class.ilSetting.php';
615  $set = new ilSetting("common", true);
616  $set->set($a_key, $a_val);
617  }
618 
623  function getURLStringForNIC($a_nic_url)
624  {
625  $settings = $this->getAllSettings();
626 
627  $inst_id = (empty($settings["inst_id"])) ? "0" : $settings["inst_id"];
628 
629  // send host information to ilias-nic
630  $url = $a_nic_url.
631  "?cmd=getid".
632  "&inst_id=".rawurlencode($inst_id).
633  "&hostname=".rawurlencode($_SERVER["SERVER_NAME"]).
634  "&ipadr=".rawurlencode($_SERVER["SERVER_ADDR"]).
635  "&server_port=".rawurlencode($_SERVER["SERVER_PORT"]).
636  "&server_software=".rawurlencode($_SERVER["SERVER_SOFTWARE"]).
637  "&inst_name=".rawurlencode($this->ini->readVariable("client","name")).
638  "&inst_info=".rawurlencode($this->ini->readVariable("client","description")).
639  "&institution=".rawurlencode($settings["inst_institution"]).
640  "&http_path=".rawurlencode(ILIAS_HTTP_PATH).
641  "&contact_firstname=".rawurlencode($settings["admin_firstname"]).
642  "&contact_lastname=".rawurlencode($settings["admin_lastname"]).
643  "&contact_title=".rawurlencode($settings["admin_title"]).
644  "&contact_position=".rawurlencode($settings["admin_position"]).
645  "&contact_institution=".rawurlencode($settings["admin_institution"]).
646  "&contact_street=".rawurlencode($settings["admin_street"]).
647  "&contact_pcode=".rawurlencode($settings["admin_zipcode"]).
648  "&contact_city=".rawurlencode($settings["admin_city"]).
649  "&contact_country=".rawurlencode($settings["admin_country"]).
650  "&contact_phone=".rawurlencode($settings["admin_phone"]).
651  "&contact_email=".rawurlencode($settings["admin_email"]).
652  "&nic_key=".rawurlencode($this->getNICkey()).
653  "&version=".rawurlencode($settings["ilias_version"]);
654 
655  return $url;
656  }
657 
672  function updateNIC($a_nic_url)
673  {
674  $settings = $this->getAllSettings();
675  if((bool)$settings['proxy_status'] && strlen($settings['proxy_host']) && strlen($settings['proxy_port']))
676  {
677  $proxy_options = array(
678  'proxy_host' => $settings['proxy_host'],
679  'proxy_port' => $settings['proxy_port']
680  );
681  }
682  else
683  {
684  $proxy_options = array();
685  }
686 
687  include_once('HTTP/Request.php');
688  $url = $this->getURLStringForNIC($a_nic_url);
689  $req = new HTTP_Request($url, $proxy_options);
690 
691  $req->sendRequest();
692  $response = $req->getResponseBody();
693  $response = explode("\n", $response);
694 
695  $this->nic_status = $response;
696 
697  return true;
698  }
699 
708  function setNICkey()
709  {
710  mt_srand((double)microtime()*1000000);
711  $nic_key = md5(str_replace(".","",$_SERVER["SERVER_ADDR"]) +
712  mt_rand(100000,999999));
713 
714  $this->setSetting("nic_key",$nic_key);
715 
716  $this->nic_key = $nic_key;
717 
718  return true;
719  }
720 
726  function getNICkey()
727  {
728  $this->nic_key = $this->getSetting("nic_key");
729 
730  if (empty($this->nic_key))
731  {
732  $this->setNICkey();
733  }
734 
735  return $this->nic_key;
736  }
737 
739  {
740  return $this->getSetting("language");
741  }
742 
743  function setDefaultLanguage($a_lang_key)
744  {
745  $this->setSetting("language",$a_lang_key);
746  $this->ini->setVariable("language","default",$a_lang_key);
747  $this->ini->write();
748 
749  return true;
750  }
751 
756  function getError()
757  {
758  $error = $this->error;
759  $this->error = "";
760 
761  return $error;
762  }
763 
772  function delete ($a_ini = true, $a_db = false, $a_files = false)
773  {
774  if ($a_ini === true and file_exists(ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId()."/client.ini.php"))
775  {
776  unlink(CLIENT_WEB_DIR."/client.ini.php");
777  $msg[] = "ini_deleted";
778  }
779 
780  if ($a_db === true and $this->db_exists)
781  {
782  $this->db->query("DROP DATABASE ".$this->getDbName());
783  $msg[] = "db_deleted";
784  }
785 
786  if ($a_files === true and file_exists(CLIENT_WEB_DIR) and is_dir(CLIENT_WEB_DIR))
787  {
788  // rmdir();
789  ilUtil::delDir(CLIENT_WEB_DIR);
790  ilUtil::delDir(CLIENT_DATA_DIR);
791  $msg[] = "files_deleted";
792  }
793 
794  return $msg;
795  }
796 
801  function create()
802  {
803  //var_dump($this->getDataDir());exit;
804  // create base data dir
805  if (!ilUtil::makeDir($this->getDataDir()))
806  {
807  $this->error = "could_not_create_base_data_dir :".$this->getDataDir();
808  return false;
809  }
810 
811  // create sub dirs in base data dir
812  if (!ilUtil::makeDir($this->getDataDir()."/mail"))
813  {
814  $this->error = "could_not_create_mail_data_dir :".$this->getDataDir()."/mail";
815  return false;
816  }
817 
818  if (!ilUtil::makeDir($this->getDataDir()."/lm_data"))
819  {
820  $this->error = "could_not_create_lm_data_dir :".$this->getDataDir()."/lm_data";
821  return false;
822  }
823 
824  if (!ilUtil::makeDir($this->getDataDir()."/forum"))
825  {
826  $this->error = "could_not_create_forum_data_dir :".$this->getDataDir()."/forum";
827  return false;
828  }
829 
830  if (!ilUtil::makeDir($this->getDataDir()."/files"))
831  {
832  $this->error = "could_not_create_files_data_dir :".$this->getDataDir()."/files";
833  return false;
834  }
835 
836  // create base webspace dir
837  if (!ilUtil::makeDir($this->getWebspaceDir()))
838  {
839  $this->error = "could_not_create_base_webspace_dir :".$this->getWebspaceDir();
840  return false;
841  }
842 
843  // create sub dirs in base webspace dir
844  if (!ilUtil::makeDir($this->getWebspaceDir()."/lm_data"))
845  {
846  $this->error = "could_not_create_lm_webspace_dir :".$this->getWebspaceDir()."/lm_data";
847  return false;
848  }
849 
850  if (!ilUtil::makeDir($this->getWebspaceDir()."/usr_images"))
851  {
852  $this->error = "could_not_create_usr_images_webspace_dir :".$this->getWebspaceDir()."/usr_images";
853  return false;
854  }
855 
856  if (!ilUtil::makeDir($this->getWebspaceDir()."/mobs"))
857  {
858  $this->error = "could_not_create_mobs_webspace_dir :".$this->getWebspaceDir()."/mobs";
859  return false;
860  }
861 
862  if (!ilUtil::makeDir($this->getWebspaceDir()."/css"))
863  {
864  $this->error = "could_not_create_css_webspace_dir :".$this->getWebspaceDir()."/css";
865  return false;
866  }
867 
868  // write client ini
869  if (!$this->ini->write())
870  {
871  $this->error = get_class($this).": ".$this->ini->getError();
872  return false;
873  }
874 
875  return true;
876  }
877 
884  function writeIni()
885  {
886  $this->ini->write();
887  }
888 
889 } // END class.ilClient
890 ?>