ILIAS  release_4-3 Revision
 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 setDbType($a_str)
374  {
375  $this->ini->setVariable("db","type",$a_str);
376  }
377 
382  function getDbType()
383  {
384  $val = $this->ini->readVariable("db","type");
385  if ($val == "")
386  {
387  return "mysql";
388  }
389  else
390  {
391  return $val;
392  }
393  }
394 
399  function getDataDir()
400  {
401  return ILIAS_DATA_DIR."/".$this->getId();
402  }
403 
408  function getWebspaceDir()
409  {
410  return ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId();
411  }
412 
417  function checkDatabaseHost()
418  {
419  global $lng;
420 
421  if ($this->getDbType() == "oracle")
422  {
423  return true;
424  }
425 
426  //connect to databasehost
427  $db = $this->db_connections->connectHost($this->dsn_host);
428  if (MDB2::isError($db))
429  {
430  //$this->error = $db->getMessage()."! Please check database hostname, username & password.";
431  $this->error = $db->getMessage()." - ".$db->getUserInfo()." - ".$lng->txt("db_error_please_check");
432  return false;
433  }
434 
435  return true;
436  }
437 
442  function checkDatabaseExists($a_keep_connection = false)
443  {
444  //try to connect to database
445  $db = $this->db_connections->connectDB($this->dsn);
446  if (MDB2::isError($db))
447  {
448  return false;
449  }
450 
451  if (!$this->isInstalledDB($db))
452  {
453  return false;
454  }
455 
456  // #10633
457  if($a_keep_connection)
458  {
459  $GLOBALS["ilDB"] = $this->db;
460  }
461 
462  return true;
463  }
464 
465  function reconnect()
466  {
467  $this->connect();
468  }
469 
476  function getSetting($a_keyword)
477  {
478  include_once './Services/Administration/classes/class.ilSetting.php';
479  $set = new ilSetting("common", true);
480  return $set->get($a_keyword);
481  }
482 
488  function getAllSettings()
489  {
490  include_once './Services/Administration/classes/class.ilSetting.php';
491  $set = new ilSetting("common", true);
492  return $set->getAll();
493  }
494 
502  function setSetting($a_key, $a_val)
503  {
504  include_once './Services/Administration/classes/class.ilSetting.php';
505  $set = new ilSetting("common", true);
506  $set->set($a_key, $a_val);
507  }
508 
513  function getURLStringForNIC($a_nic_url)
514  {
515  $settings = $this->getAllSettings();
516 
517  $inst_id = (empty($settings["inst_id"])) ? "0" : $settings["inst_id"];
518 
519  // send host information to ilias-nic
520  $url = $a_nic_url.
521  "?cmd=getid".
522  "&inst_id=".rawurlencode($inst_id).
523  "&hostname=".rawurlencode($_SERVER["SERVER_NAME"]).
524  "&ipadr=".rawurlencode($_SERVER["SERVER_ADDR"]).
525  "&server_port=".rawurlencode($_SERVER["SERVER_PORT"]).
526  "&server_software=".rawurlencode($_SERVER["SERVER_SOFTWARE"]).
527  "&inst_name=".rawurlencode($this->ini->readVariable("client","name")).
528  "&inst_info=".rawurlencode($this->ini->readVariable("client","description")).
529  "&institution=".rawurlencode($settings["inst_institution"]).
530  "&http_path=".rawurlencode(ILIAS_HTTP_PATH).
531  "&contact_firstname=".rawurlencode($settings["admin_firstname"]).
532  "&contact_lastname=".rawurlencode($settings["admin_lastname"]).
533  "&contact_title=".rawurlencode($settings["admin_title"]).
534  "&contact_position=".rawurlencode($settings["admin_position"]).
535  "&contact_institution=".rawurlencode($settings["admin_institution"]).
536  "&contact_street=".rawurlencode($settings["admin_street"]).
537  "&contact_pcode=".rawurlencode($settings["admin_zipcode"]).
538  "&contact_city=".rawurlencode($settings["admin_city"]).
539  "&contact_country=".rawurlencode($settings["admin_country"]).
540  "&contact_phone=".rawurlencode($settings["admin_phone"]).
541  "&contact_email=".rawurlencode($settings["admin_email"]).
542  "&nic_key=".rawurlencode($this->getNICkey()).
543  "&version=".rawurlencode($settings["ilias_version"]);
544 
545  return $url;
546  }
547 
562  function updateNIC($a_nic_url)
563  {
564  $settings = $this->getAllSettings();
565  if((bool)$settings['proxy_status'] && strlen($settings['proxy_host']) && strlen($settings['proxy_port']))
566  {
567  $proxy_options = array(
568  'proxy_host' => $settings['proxy_host'],
569  'proxy_port' => $settings['proxy_port']
570  );
571  }
572  else
573  {
574  $proxy_options = array();
575  }
576 
577  include_once('HTTP/Request.php');
578  $url = $this->getURLStringForNIC($a_nic_url);
579  $req = new HTTP_Request($url, $proxy_options);
580 
581  $req->sendRequest();
582  $response = $req->getResponseBody();
583  $response = explode("\n", $response);
584 
585  $this->nic_status = $response;
586 
587  return true;
588  }
589 
598  function setNICkey()
599  {
600  mt_srand((double)microtime()*1000000);
601  $nic_key = md5(str_replace(".","",$_SERVER["SERVER_ADDR"]) +
602  mt_rand(100000,999999));
603 
604  $this->setSetting("nic_key",$nic_key);
605 
606  $this->nic_key = $nic_key;
607 
608  return true;
609  }
610 
616  function getNICkey()
617  {
618  $this->nic_key = $this->getSetting("nic_key");
619 
620  if (empty($this->nic_key))
621  {
622  $this->setNICkey();
623  }
624 
625  return $this->nic_key;
626  }
627 
629  {
630  return $this->getSetting("language");
631  }
632 
633  function setDefaultLanguage($a_lang_key)
634  {
635  $this->setSetting("language",$a_lang_key);
636  $this->ini->setVariable("language","default",$a_lang_key);
637  $this->ini->write();
638 
639  return true;
640  }
641 
646  function getError()
647  {
648  $error = $this->error;
649  $this->error = "";
650 
651  return $error;
652  }
653 
662  function delete ($a_ini = true, $a_db = false, $a_files = false)
663  {
664  if ($a_ini === true and file_exists(ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId()."/client.ini.php"))
665  {
666  unlink(CLIENT_WEB_DIR."/client.ini.php");
667  $msg[] = "ini_deleted";
668  }
669 
670  if ($a_db === true and $this->db_exists)
671  {
672  $this->db->query("DROP DATABASE ".$this->getDbName());
673  $msg[] = "db_deleted";
674  }
675 
676  if ($a_files === true and file_exists(CLIENT_WEB_DIR) and is_dir(CLIENT_WEB_DIR))
677  {
678  // rmdir();
679  ilUtil::delDir(CLIENT_WEB_DIR);
680  ilUtil::delDir(CLIENT_DATA_DIR);
681  $msg[] = "files_deleted";
682  }
683 
684  return $msg;
685  }
686 
691  function create()
692  {
693  //var_dump($this->getDataDir());exit;
694  // create base data dir
695  if (!ilUtil::makeDir($this->getDataDir()))
696  {
697  $this->error = "could_not_create_base_data_dir :".$this->getDataDir();
698  return false;
699  }
700 
701  // create sub dirs in base data dir
702  if (!ilUtil::makeDir($this->getDataDir()."/mail"))
703  {
704  $this->error = "could_not_create_mail_data_dir :".$this->getDataDir()."/mail";
705  return false;
706  }
707 
708  if (!ilUtil::makeDir($this->getDataDir()."/lm_data"))
709  {
710  $this->error = "could_not_create_lm_data_dir :".$this->getDataDir()."/lm_data";
711  return false;
712  }
713 
714  if (!ilUtil::makeDir($this->getDataDir()."/forum"))
715  {
716  $this->error = "could_not_create_forum_data_dir :".$this->getDataDir()."/forum";
717  return false;
718  }
719 
720  if (!ilUtil::makeDir($this->getDataDir()."/files"))
721  {
722  $this->error = "could_not_create_files_data_dir :".$this->getDataDir()."/files";
723  return false;
724  }
725 
726  // create base webspace dir
727  if (!ilUtil::makeDir($this->getWebspaceDir()))
728  {
729  $this->error = "could_not_create_base_webspace_dir :".$this->getWebspaceDir();
730  return false;
731  }
732 
733  // create sub dirs in base webspace dir
734  if (!ilUtil::makeDir($this->getWebspaceDir()."/lm_data"))
735  {
736  $this->error = "could_not_create_lm_webspace_dir :".$this->getWebspaceDir()."/lm_data";
737  return false;
738  }
739 
740  if (!ilUtil::makeDir($this->getWebspaceDir()."/usr_images"))
741  {
742  $this->error = "could_not_create_usr_images_webspace_dir :".$this->getWebspaceDir()."/usr_images";
743  return false;
744  }
745 
746  if (!ilUtil::makeDir($this->getWebspaceDir()."/mobs"))
747  {
748  $this->error = "could_not_create_mobs_webspace_dir :".$this->getWebspaceDir()."/mobs";
749  return false;
750  }
751 
752  if (!ilUtil::makeDir($this->getWebspaceDir()."/css"))
753  {
754  $this->error = "could_not_create_css_webspace_dir :".$this->getWebspaceDir()."/css";
755  return false;
756  }
757 
758  // write client ini
759  if (!$this->ini->write())
760  {
761  $this->error = get_class($this).": ".$this->ini->getError();
762  return false;
763  }
764 
765  return true;
766  }
767 } // END class.ilClient
768 ?>