00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00033 class ilClient
00034 {
00035 var $id;
00036 var $dir;
00037 var $name;
00038 var $db_exists = false;
00039 var $db_installed = false;
00040
00041 var $client_defaults;
00042 var $status;
00043 var $setup_ok = false;
00044 var $nic_status;
00045
00050 function ilClient($a_client_id = 0)
00051 {
00052 if ($a_client_id)
00053 {
00054 $this->id = $a_client_id;
00055 $this->ini_file_path = ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId()."/client.ini.php";
00056 }
00057
00058
00059 $this->client_defaults = ILIAS_ABSOLUTE_PATH."/setup/client.master.ini.php";
00060 }
00061
00067 function init()
00068 {
00069 $this->ini = new ilIniFile($this->ini_file_path);
00070
00071
00072 if (!@file_exists($this->ini_file_path))
00073 {
00074 $this->ini->GROUPS = parse_ini_file($this->client_defaults,true);
00075 return false;
00076 }
00077
00078
00079 if (!$this->ini->read())
00080 {
00081 $this->error = get_class($this).": ".$this->ini->getError();
00082 return false;
00083 }
00084
00085
00086 define("CLIENT_WEB_DIR",ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId());
00087 define("CLIENT_DATA_DIR",ILIAS_DATA_DIR."/".$this->getId());
00088 define ("DEVMODE",$this->ini->readVariable('system','DEVMODE'));
00089 define ("ROOT_FOLDER_ID",$this->ini->readVariable('system','ROOT_FOLDER_ID'));
00090 define ("SYSTEM_FOLDER_ID",$this->ini->readVariable('system','SYSTEM_FOLDER_ID'));
00091 define ("ROLE_FOLDER_ID",$this->ini->readVariable('system','ROLE_FOLDER_ID'));
00092 define ("ANONYMOUS_USER_ID",13);
00093 define ("ANONYMOUS_ROLE_ID",14);
00094 define ("SYSTEM_USER_ID",6);
00095 define ("SYSTEM_ROLE_ID",2);
00096
00097 $this->db_exists = $this->connect();
00098
00099 if ($this->db_exists)
00100 {
00101 $this->db_installed = $this->isInstalledDB($this->db);
00102 }
00103
00104 return true;
00105 }
00106
00111 function getId()
00112 {
00113 return $this->id;
00114 }
00115
00120 function setId($a_client_id)
00121 {
00122 $this->id = $a_client_id;
00123 $this->webspace_dir = ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->id;
00124 }
00125
00130 function getName()
00131 {
00132 return $this->ini->readVariable("client","name");
00133 }
00134
00139 function setName($a_str)
00140 {
00141 $this->ini->setVariable("client","name",$a_str);
00142 }
00143
00148 function getDescription()
00149 {
00150 return $this->ini->readVariable("client","description");
00151 }
00152
00157 function setDescription($a_str)
00158 {
00159 $this->ini->setVariable("client","description",$a_str);
00160 }
00161
00165 function getMySQLVersion()
00166 {
00167 return mysql_get_server_info();
00168 }
00169
00178 function isMysql4_1OrHigher()
00179 {
00180 $version = explode(".", $this->getMysqlVersion());
00181 if ((int)$version[0] >= 5 ||
00182 ((int)$version[0] == 4 && (int)$version[1] >= 1))
00183 {
00184 return true;
00185 }
00186
00187 return false;
00188 }
00189
00194 function connect()
00195 {
00196
00197 if (!$this->getdbHost() || !$this->getdbName() || !$this->getdbUser())
00198 {
00199 $this->error = "empty_fields";
00200 return false;
00201 }
00202
00203 $this->setDSN();
00204
00205 $this->db = DB::connect($this->dsn,true);
00206
00207 if (DB::isError($this->db))
00208 {
00209 $this->error = $this->db->getMessage()."! not_connected_to_db";
00210 return false;
00211 }
00212
00213
00214
00215
00216
00217 if ($this->isMysql4_1OrHigher())
00218 {
00219 $this->db->query("SET NAMES utf8");
00220 $this->db->query("SET SESSION SQL_MODE = ''");
00221 }
00222
00223 $this->db_exists = true;
00224 return true;
00225 }
00226
00232 function isInstalledDB(&$a_db)
00233 {
00234 $q = "SHOW TABLES";
00235 $r = $a_db->query($q);
00236
00237 $tables = array();
00238
00239 while ($row = $r->fetchRow(DB_FETCHMODE_ASSOC))
00240 {
00241 $tables[] = implode($row);
00242 }
00243
00244
00245 if (in_array("object_data",$tables) and in_array("object_reference",$tables) and in_array("usr_data",$tables) and in_array("rbac_ua",$tables))
00246 {
00247 $this->db_installed = true;
00248 return true;
00249 }
00250
00251 $this->db_installed = false;
00252 return false;
00253 }
00254
00258 function setDSN()
00259 {
00260
00261 $this->dsn_host = "mysql://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost();
00262 $this->dsn = "mysql://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost()."/".$this->getdbName();
00263 }
00264
00269 function setDbHost($a_str)
00270 {
00271 $this->ini->setVariable("db","host",$a_str);
00272 }
00273
00279 function getDbHost()
00280 {
00281 return $this->ini->readVariable("db","host");
00282 }
00283
00288 function setDbName($a_str)
00289 {
00290 $this->ini->setVariable("db","name",$a_str);
00291 }
00292
00297 function getDbName()
00298 {
00299 return $this->ini->readVariable("db","name");
00300 }
00301
00306 function setDbUser($a_str)
00307 {
00308 $this->ini->setVariable("db","user",$a_str);
00309 }
00310
00315 function getDbUser()
00316 {
00317 return $this->ini->readVariable("db","user");
00318 }
00319
00324 function setDbPass($a_str)
00325 {
00326 $this->ini->setVariable("db","pass",$a_str);
00327 }
00328
00333 function getDbPass()
00334 {
00335 return $this->ini->readVariable("db","pass");
00336 }
00337
00342 function getDataDir()
00343 {
00344 return ILIAS_DATA_DIR."/".$this->getId();
00345 }
00346
00351 function getWebspaceDir()
00352 {
00353 return ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId();
00354 }
00355
00360 function checkDatabaseHost()
00361 {
00362
00363 $db = DB::connect($this->dsn_host);
00364
00365 if (DB::isError($db))
00366 {
00367 $this->error = $db->getMessage()."! Please check database hostname, username & password.";
00368 return false;
00369 }
00370
00371 return true;
00372 }
00373
00378 function checkDatabaseExists()
00379 {
00380
00381 $db = DB::connect($this->dsn);
00382
00383 if (DB::isError($db))
00384 {
00385 return false;
00386 }
00387
00388 if (!$this->isInstalledDB($db))
00389 {
00390 return false;
00391 }
00392
00393 return true;
00394 }
00395
00402 function getSetting($a_keyword)
00403 {
00404 $q = "SELECT value FROM settings WHERE keyword='".$a_keyword."'";
00405 $r = $this->db->query($q);
00406
00407 if ($r->numRows() > 0)
00408 {
00409 $row = $r->fetchRow();
00410 return $row[0];
00411 }
00412 else
00413 {
00414 return false;
00415 }
00416 }
00417
00423 function getAllSettings()
00424 {
00425 $q = "SELECT * FROM settings";
00426 $r = $this->db->query($q);
00427
00428 while ($row = $r->fetchRow(DB_FETCHMODE_ASSOC))
00429 {
00430 $arr[$row["keyword"]] = $row["value"];
00431 }
00432
00433 return $arr;
00434 }
00435
00443 function setSetting($a_key, $a_val)
00444 {
00445 $q = "REPLACE INTO settings SET keyword = '".$a_key."', value = '".$a_val."'";
00446 $this->db->query($q);
00447
00448 return true;
00449 }
00450
00455 function getURLStringForNIC($a_nic_url)
00456 {
00457 $settings = $this->getAllSettings();
00458
00459 $inst_id = (empty($settings["inst_id"])) ? "0" : $settings["inst_id"];
00460
00461
00462 $url = $a_nic_url.
00463 "?cmd=getid".
00464 "&inst_id=".rawurlencode($inst_id).
00465 "&hostname=".rawurlencode($_SERVER["SERVER_NAME"]).
00466 "&ipadr=".rawurlencode($_SERVER["SERVER_ADDR"]).
00467 "&server_port=".rawurlencode($_SERVER["SERVER_PORT"]).
00468 "&server_software=".rawurlencode($_SERVER["SERVER_SOFTWARE"]).
00469 "&inst_name=".rawurlencode($this->ini->readVariable("client","name")).
00470 "&inst_info=".rawurlencode($this->ini->readVariable("client","description")).
00471 "&institution=".rawurlencode($settings["inst_institution"]).
00472 "&http_path=".rawurlencode(ILIAS_HTTP_PATH).
00473 "&contact_firstname=".rawurlencode($settings["admin_firstname"]).
00474 "&contact_lastname=".rawurlencode($settings["admin_lastname"]).
00475 "&contact_title=".rawurlencode($settings["admin_title"]).
00476 "&contact_position=".rawurlencode($settings["admin_position"]).
00477 "&contact_institution=".rawurlencode($settings["admin_institution"]).
00478 "&contact_street=".rawurlencode($settings["admin_street"]).
00479 "&contact_pcode=".rawurlencode($settings["admin_zipcode"]).
00480 "&contact_city=".rawurlencode($settings["admin_city"]).
00481 "&contact_country=".rawurlencode($settings["admin_country"]).
00482 "&contact_phone=".rawurlencode($settings["admin_phone"]).
00483 "&contact_email=".rawurlencode($settings["admin_email"]).
00484 "&nic_key=".rawurlencode($this->getNICkey()).
00485 "&version=".rawurlencode($settings["ilias_version"]);
00486
00487 return $url;
00488 }
00489
00504 function updateNIC($a_nic_url)
00505 {
00506 $url = $this->getURLStringForNIC($a_nic_url);
00507
00508 $conn =fopen($url,"r");
00509
00510 $input = "";
00511
00512 if (!$conn)
00513 {
00514 return false;
00515 }
00516 else
00517 {
00518 while(!feof($conn))
00519 {
00520 $input.= fgets($conn, 4096);
00521 }
00522
00523 fclose($conn);
00524 $line = explode("\n",$input);
00525
00526 $ret = $line;
00527 }
00528
00529 $this->nic_status = $ret;
00530
00531 return true;
00532 }
00533
00542 function setNICkey()
00543 {
00544 mt_srand((double)microtime()*1000000);
00545 $nic_key = md5(str_replace(".","",$_SERVER["SERVER_ADDR"]) +
00546 mt_rand(100000,999999));
00547
00548 $this->setSetting("nic_key",$nic_key);
00549
00550 $this->nic_key = $nic_key;
00551
00552 return true;
00553 }
00554
00560 function getNICkey()
00561 {
00562 $this->nic_key = $this->getSetting("nic_key");
00563
00564 if (empty($this->nic_key))
00565 {
00566 $this->setNICkey();
00567 }
00568
00569 return $this->nic_key;
00570 }
00571
00572 function getDefaultLanguage()
00573 {
00574 return $this->getSetting("language");
00575 }
00576
00577 function setDefaultLanguage($a_lang_key)
00578 {
00579 $this->setSetting("language",$a_lang_key);
00580 $this->ini->setVariable("language","default",$a_lang_key);
00581 $this->ini->write();
00582
00583 return true;
00584 }
00585
00590 function getError()
00591 {
00592 $error = $this->error;
00593 $this->error = "";
00594
00595 return $error;
00596 }
00597
00606 function delete ($a_ini = true, $a_db = false, $a_files = false)
00607 {
00608 if ($a_ini === true and file_exists(ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId()."/client.ini.php"))
00609 {
00610 unlink(CLIENT_WEB_DIR."/client.ini.php");
00611 $msg[] = "ini_deleted";
00612 }
00613
00614 if ($a_db === true and $this->db_exists)
00615 {
00616 $this->db->query("DROP DATABASE ".$this->getDbName());
00617 $msg[] = "db_deleted";
00618 }
00619
00620 if ($a_files === true and file_exists(CLIENT_WEB_DIR) and is_dir(CLIENT_WEB_DIR))
00621 {
00622
00623 ilUtil::delDir(CLIENT_WEB_DIR);
00624 ilUtil::delDir(CLIENT_DATA_DIR);
00625 $msg[] = "files_deleted";
00626 }
00627
00628 return $msg;
00629 }
00630
00635 function create()
00636 {
00637
00638
00639 if (!ilUtil::makeDir($this->getDataDir()))
00640 {
00641 $this->error = "could_not_create_base_data_dir :".$this->getDataDir();
00642 return false;
00643 }
00644
00645
00646 if (!ilUtil::makeDir($this->getDataDir()."/mail"))
00647 {
00648 $this->error = "could_not_create_mail_data_dir :".$this->getDataDir()."/mail";
00649 return false;
00650 }
00651
00652 if (!ilUtil::makeDir($this->getDataDir()."/lm_data"))
00653 {
00654 $this->error = "could_not_create_lm_data_dir :".$this->getDataDir()."/lm_data";
00655 return false;
00656 }
00657
00658 if (!ilUtil::makeDir($this->getDataDir()."/forum"))
00659 {
00660 $this->error = "could_not_create_forum_data_dir :".$this->getDataDir()."/forum";
00661 return false;
00662 }
00663
00664 if (!ilUtil::makeDir($this->getDataDir()."/files"))
00665 {
00666 $this->error = "could_not_create_files_data_dir :".$this->getDataDir()."/files";
00667 return false;
00668 }
00669
00670
00671 if (!ilUtil::makeDir($this->getWebspaceDir()))
00672 {
00673 $this->error = "could_not_create_base_webspace_dir :".$this->getWebspaceDir();
00674 return false;
00675 }
00676
00677
00678 if (!ilUtil::makeDir($this->getWebspaceDir()."/lm_data"))
00679 {
00680 $this->error = "could_not_create_lm_webspace_dir :".$this->getWebspaceDir()."/lm_data";
00681 return false;
00682 }
00683
00684 if (!ilUtil::makeDir($this->getWebspaceDir()."/usr_images"))
00685 {
00686 $this->error = "could_not_create_usr_images_webspace_dir :".$this->getWebspaceDir()."/usr_images";
00687 return false;
00688 }
00689
00690 if (!ilUtil::makeDir($this->getWebspaceDir()."/mobs"))
00691 {
00692 $this->error = "could_not_create_mobs_webspace_dir :".$this->getWebspaceDir()."/mobs";
00693 return false;
00694 }
00695
00696 if (!ilUtil::makeDir($this->getWebspaceDir()."/css"))
00697 {
00698 $this->error = "could_not_create_css_webspace_dir :".$this->getWebspaceDir()."/css";
00699 return false;
00700 }
00701
00702
00703 if (!$this->ini->write())
00704 {
00705 $this->error = get_class($this).": ".$this->ini->getError();
00706 return false;
00707 }
00708
00709 return true;
00710 }
00711 }
00712 ?>