ILIAS  Release_4_2_x_branch Revision 61807
 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  include_once("./Services/Database/classes/class.ilDBWrapperFactory.php");
180  $this->db = ilDBWrapperFactory::getWrapper($this->getdbType());
181  $this->db->setDBUser($this->getdbUser());
182  $this->db->setDBPort($this->getdbPort());
183  $this->db->setDBPassword($this->getdbPass());
184  $this->db->setDBHost($this->getdbHost());
185  $this->db->setDBName($this->getdbName());
186 //if ($this->getdbType() != "oracle")
187 //{
188  $con = $this->db->connect(true);
189 //}
190 
191  if (!$con)
192  {
193  $this->error = "Database connection failed.";
194  return false;
195  }
196  $GLOBALS["ilDB"] = $this->db;
197 
198  $this->db_exists = true;
199  return true;
200  }
201 
207  function isInstalledDB(&$a_db)
208  {
209  if(method_exists($a_db,'loadModule'))
210  {
211  $a_db->loadModule('Manager');
212  }
213  if(!$tables = $a_db->listTables())
214  {
215  return false;
216  }
217 
218  // check existence of some basic tables from ilias3 to determine if ilias3 is already installed in given database
219  if (in_array("object_data",$tables) and in_array("object_reference",$tables) and in_array("usr_data",$tables) and in_array("rbac_ua",$tables))
220  {
221  $this->db_installed = true;
222  return true;
223  }
224  $this->db_installed = false;
225  return false;
226  }
227 
231  function setDSN()
232  {
233  switch($this->getDbType())
234  {
235  case "oracle":
236  //$this->dsn_host = "oci8://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost();
237  $this->dsn_host = array(
238  'phptype' => 'oci8',
239  'hostspec' => $this->getdbHost(),
240  'username' => $this->getdbUser(),
241  'port' => $this->getdbPort(),
242  'password' => $this->getdbPass(),
243  );
244  //$this->dsn = "oci8://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost()."/?service=".$this->getdbName();
245  $this->dsn = $this->dsn = array(
246  'phptype' => 'oci8',
247  'hostspec' => $this->getdbHost(),
248  'username' => $this->getdbUser(),
249  'port' => $this->getdbPort(),
250  'password' => $this->getdbPass(),
251  'service' => $this->getdbName()
252  );
253  break;
254 
255  case "mysql":
256  default:
257  $db_port_str = "";
258  if (trim($this->getdbPort()) != "")
259  {
260  $db_port_str = ":".$this->getdbPort();
261  }
262  $this->dsn_host = "mysql://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost().$db_port_str;
263  $this->dsn = "mysql://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost().$db_port_str."/".$this->getdbName();
264  break;
265  }
266  }
267 
272  function setDbHost($a_str)
273  {
274  $this->ini->setVariable("db","host",$a_str);
275  }
276 
282  function getDbHost()
283  {
284  return $this->ini->readVariable("db","host");
285  }
286 
291  function setDbName($a_str)
292  {
293  $this->ini->setVariable("db","name",$a_str);
294  }
295 
300  function getDbName()
301  {
302  return $this->ini->readVariable("db","name");
303  }
304 
309  function setDbUser($a_str)
310  {
311  $this->ini->setVariable("db","user",$a_str);
312  }
313 
318  function getDbUser()
319  {
320  return $this->ini->readVariable("db","user");
321  }
322 
327  function getDbPort()
328  {
329  return $this->ini->readVariable("db","port");
330  }
331 
336  function setDbPort($a_str)
337  {
338  $this->ini->setVariable("db","port",$a_str);
339  }
340 
345  function setDbPass($a_str)
346  {
347  $this->ini->setVariable("db","pass",$a_str);
348  }
349 
354  function getDbPass()
355  {
356  return $this->ini->readVariable("db","pass");
357  }
358 
363  function setDbType($a_str)
364  {
365  $this->ini->setVariable("db","type",$a_str);
366  }
367 
372  function getDbType()
373  {
374  $val = $this->ini->readVariable("db","type");
375  if ($val == "")
376  {
377  return "mysql";
378  }
379  else
380  {
381  return $val;
382  }
383  }
384 
389  function getDataDir()
390  {
391  return ILIAS_DATA_DIR."/".$this->getId();
392  }
393 
398  function getWebspaceDir()
399  {
400  return ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId();
401  }
402 
407  function checkDatabaseHost()
408  {
409  global $lng;
410 
411  if ($this->getDbType() == "oracle")
412  {
413  return true;
414  }
415 
416  //connect to databasehost
417  $db = $this->db_connections->connectHost($this->dsn_host);
418 //var_dump($db); exit;
419  if (MDB2::isError($db))
420  {
421  //$this->error = $db->getMessage()."! Please check database hostname, username & password.";
422  $this->error = $db->getMessage()." - ".$lng->txt("db_error_please_check");
423  return false;
424  }
425 
426  return true;
427  }
428 
434  {
435  //try to connect to database
436  $db = $this->db_connections->connectDB($this->dsn);
437  if (MDB2::isError($db))
438  {
439  return false;
440  }
441 
442  if (!$this->isInstalledDB($db))
443  {
444  return false;
445  }
446 
447  return true;
448  }
449 
450  function reconnect()
451  {
452  $this->connect();
453  }
454 
461  function getSetting($a_keyword)
462  {
463  include_once './Services/Administration/classes/class.ilSetting.php';
464  $set = new ilSetting("common", true);
465  return $set->get($a_keyword);
466  }
467 
473  function getAllSettings()
474  {
475  include_once './Services/Administration/classes/class.ilSetting.php';
476  $set = new ilSetting("common", true);
477  return $set->getAll();
478  }
479 
487  function setSetting($a_key, $a_val)
488  {
489  include_once './Services/Administration/classes/class.ilSetting.php';
490  $set = new ilSetting("common", true);
491  $set->set($a_key, $a_val);
492  }
493 
498  function getURLStringForNIC($a_nic_url)
499  {
500  $settings = $this->getAllSettings();
501 
502  $inst_id = (empty($settings["inst_id"])) ? "0" : $settings["inst_id"];
503 
504  // send host information to ilias-nic
505  $url = $a_nic_url.
506  "?cmd=getid".
507  "&inst_id=".rawurlencode($inst_id).
508  "&hostname=".rawurlencode($_SERVER["SERVER_NAME"]).
509  "&ipadr=".rawurlencode($_SERVER["SERVER_ADDR"]).
510  "&server_port=".rawurlencode($_SERVER["SERVER_PORT"]).
511  "&server_software=".rawurlencode($_SERVER["SERVER_SOFTWARE"]).
512  "&inst_name=".rawurlencode($this->ini->readVariable("client","name")).
513  "&inst_info=".rawurlencode($this->ini->readVariable("client","description")).
514  "&institution=".rawurlencode($settings["inst_institution"]).
515  "&http_path=".rawurlencode(ILIAS_HTTP_PATH).
516  "&contact_firstname=".rawurlencode($settings["admin_firstname"]).
517  "&contact_lastname=".rawurlencode($settings["admin_lastname"]).
518  "&contact_title=".rawurlencode($settings["admin_title"]).
519  "&contact_position=".rawurlencode($settings["admin_position"]).
520  "&contact_institution=".rawurlencode($settings["admin_institution"]).
521  "&contact_street=".rawurlencode($settings["admin_street"]).
522  "&contact_pcode=".rawurlencode($settings["admin_zipcode"]).
523  "&contact_city=".rawurlencode($settings["admin_city"]).
524  "&contact_country=".rawurlencode($settings["admin_country"]).
525  "&contact_phone=".rawurlencode($settings["admin_phone"]).
526  "&contact_email=".rawurlencode($settings["admin_email"]).
527  "&nic_key=".rawurlencode($this->getNICkey()).
528  "&version=".rawurlencode($settings["ilias_version"]);
529 
530  return $url;
531  }
532 
547  function updateNIC($a_nic_url)
548  {
549  $url = $this->getURLStringForNIC($a_nic_url);
550 
551  $conn = @fopen($url,"r");
552 
553  $input = "";
554 
555  if (!$conn)
556  {
557  return false;
558  }
559  else
560  {
561  while(!feof($conn))
562  {
563  $input.= fgets($conn, 4096);
564  }
565 
566  fclose($conn);
567  $line = explode("\n",$input);
568 
569  $ret = $line;
570  }
571 
572  $this->nic_status = $ret;
573 
574  return true;
575  }
576 
585  function setNICkey()
586  {
587  mt_srand((double)microtime()*1000000);
588  $nic_key = md5(str_replace(".","",$_SERVER["SERVER_ADDR"]) +
589  mt_rand(100000,999999));
590 
591  $this->setSetting("nic_key",$nic_key);
592 
593  $this->nic_key = $nic_key;
594 
595  return true;
596  }
597 
603  function getNICkey()
604  {
605  $this->nic_key = $this->getSetting("nic_key");
606 
607  if (empty($this->nic_key))
608  {
609  $this->setNICkey();
610  }
611 
612  return $this->nic_key;
613  }
614 
616  {
617  return $this->getSetting("language");
618  }
619 
620  function setDefaultLanguage($a_lang_key)
621  {
622  $this->setSetting("language",$a_lang_key);
623  $this->ini->setVariable("language","default",$a_lang_key);
624  $this->ini->write();
625 
626  return true;
627  }
628 
633  function getError()
634  {
635  $error = $this->error;
636  $this->error = "";
637 
638  return $error;
639  }
640 
649  function delete ($a_ini = true, $a_db = false, $a_files = false)
650  {
651  if ($a_ini === true and file_exists(ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId()."/client.ini.php"))
652  {
653  unlink(CLIENT_WEB_DIR."/client.ini.php");
654  $msg[] = "ini_deleted";
655  }
656 
657  if ($a_db === true and $this->db_exists)
658  {
659  $this->db->query("DROP DATABASE ".$this->getDbName());
660  $msg[] = "db_deleted";
661  }
662 
663  if ($a_files === true and file_exists(CLIENT_WEB_DIR) and is_dir(CLIENT_WEB_DIR))
664  {
665  // rmdir();
666  ilUtil::delDir(CLIENT_WEB_DIR);
667  ilUtil::delDir(CLIENT_DATA_DIR);
668  $msg[] = "files_deleted";
669  }
670 
671  return $msg;
672  }
673 
678  function create()
679  {
680  //var_dump($this->getDataDir());exit;
681  // create base data dir
682  if (!ilUtil::makeDir($this->getDataDir()))
683  {
684  $this->error = "could_not_create_base_data_dir :".$this->getDataDir();
685  return false;
686  }
687 
688  // create sub dirs in base data dir
689  if (!ilUtil::makeDir($this->getDataDir()."/mail"))
690  {
691  $this->error = "could_not_create_mail_data_dir :".$this->getDataDir()."/mail";
692  return false;
693  }
694 
695  if (!ilUtil::makeDir($this->getDataDir()."/lm_data"))
696  {
697  $this->error = "could_not_create_lm_data_dir :".$this->getDataDir()."/lm_data";
698  return false;
699  }
700 
701  if (!ilUtil::makeDir($this->getDataDir()."/forum"))
702  {
703  $this->error = "could_not_create_forum_data_dir :".$this->getDataDir()."/forum";
704  return false;
705  }
706 
707  if (!ilUtil::makeDir($this->getDataDir()."/files"))
708  {
709  $this->error = "could_not_create_files_data_dir :".$this->getDataDir()."/files";
710  return false;
711  }
712 
713  // create base webspace dir
714  if (!ilUtil::makeDir($this->getWebspaceDir()))
715  {
716  $this->error = "could_not_create_base_webspace_dir :".$this->getWebspaceDir();
717  return false;
718  }
719 
720  // create sub dirs in base webspace dir
721  if (!ilUtil::makeDir($this->getWebspaceDir()."/lm_data"))
722  {
723  $this->error = "could_not_create_lm_webspace_dir :".$this->getWebspaceDir()."/lm_data";
724  return false;
725  }
726 
727  if (!ilUtil::makeDir($this->getWebspaceDir()."/usr_images"))
728  {
729  $this->error = "could_not_create_usr_images_webspace_dir :".$this->getWebspaceDir()."/usr_images";
730  return false;
731  }
732 
733  if (!ilUtil::makeDir($this->getWebspaceDir()."/mobs"))
734  {
735  $this->error = "could_not_create_mobs_webspace_dir :".$this->getWebspaceDir()."/mobs";
736  return false;
737  }
738 
739  if (!ilUtil::makeDir($this->getWebspaceDir()."/css"))
740  {
741  $this->error = "could_not_create_css_webspace_dir :".$this->getWebspaceDir()."/css";
742  return false;
743  }
744 
745  // write client ini
746  if (!$this->ini->write())
747  {
748  $this->error = get_class($this).": ".$this->ini->getError();
749  return false;
750  }
751 
752  return true;
753  }
754 } // END class.ilClient
755 ?>