• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

setup/classes/class.ilClient.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2005 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00032 class ilClient
00033 {
00034         var $id;                                        // client_id (md5 hash)
00035         var $dir;                                       // directory name in ilias/clients/
00036         var $name;                                      // installation name
00037         var $db_exists = false;         // db exists?
00038         var $db_installed = false;      // db installed?
00039 
00040         var $client_defaults;           // default settings
00041         var $status;                            // contains status infos about setup process (todo: move function to this class)
00042         var $setup_ok = false;          // if client setup was finished at least once, this is set to true
00043         var $nic_status;                        // contains received data of ILIAS-NIC server when registering
00044 
00049         function ilClient($a_client_id = 0)
00050         {
00051                 if ($a_client_id)
00052                 {
00053                         $this->id = $a_client_id;
00054                         $this->ini_file_path = ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId()."/client.ini.php";
00055                 }
00056 
00057                 // set path default.ini
00058                 $this->client_defaults = ILIAS_ABSOLUTE_PATH."/setup/client.master.ini.php";
00059         }
00060         
00066         function init()
00067         {
00068                 $this->ini = new ilIniFile($this->ini_file_path);
00069         
00070                 // load defaults only if no client.ini was found
00071                 if (!@file_exists($this->ini_file_path))
00072                 {
00073                         $this->ini->GROUPS = parse_ini_file($this->client_defaults,true);
00074                         return false;
00075                 }
00076 
00077                 // read client.ini
00078                 if (!$this->ini->read())
00079                 {
00080                         $this->error = get_class($this).": ".$this->ini->getError();
00081                         return false;           
00082                 }
00083 
00084                 // only for ilias main
00085                 define("CLIENT_WEB_DIR",ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId());
00086                 define("CLIENT_DATA_DIR",ILIAS_DATA_DIR."/".$this->getId());
00087                 define ("DEVMODE",$this->ini->readVariable('system','DEVMODE'));
00088                 define ("ROOT_FOLDER_ID",$this->ini->readVariable('system','ROOT_FOLDER_ID'));
00089                 define ("SYSTEM_FOLDER_ID",$this->ini->readVariable('system','SYSTEM_FOLDER_ID'));
00090                 define ("ROLE_FOLDER_ID",$this->ini->readVariable('system','ROLE_FOLDER_ID'));
00091                 define ("ANONYMOUS_USER_ID",13);
00092                 define ("ANONYMOUS_ROLE_ID",14);
00093                 define ("SYSTEM_USER_ID",6);
00094                 define ("SYSTEM_ROLE_ID",2);
00095                 
00096                 $this->db_exists = $this->connect();
00097                 
00098                 if ($this->db_exists)
00099                 {
00100                         $this->db_installed = $this->isInstalledDB($this->db);
00101                 }
00102                 
00103                 return true;    
00104         }
00105         
00110         function getId()
00111         {
00112                 return $this->id;
00113         }
00114         
00119         function setId($a_client_id)
00120         {
00121                 $this->id = $a_client_id;
00122                 $this->webspace_dir = ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->id;
00123         }
00124         
00129         function getName()
00130         {
00131                 return $this->ini->readVariable("client","name");
00132         }
00133         
00138         function setName($a_str)
00139         {
00140                 $this->ini->setVariable("client","name",$a_str);
00141         }
00142         
00147         function getDescription()
00148         {
00149                 return $this->ini->readVariable("client","description");
00150         }
00151         
00156         function setDescription($a_str)
00157         {
00158                 $this->ini->setVariable("client","description",$a_str);
00159         }
00160 
00164         function getMySQLVersion()
00165         {
00166                 return mysql_get_server_info();
00167         }
00168 
00177         function isMysql4_1OrHigher()
00178         {
00179                 $version = explode(".", $this->getMysqlVersion());
00180                 if ((int)$version[0] >= 5 ||
00181                         ((int)$version[0] == 4 && (int)$version[1] >= 1))
00182                 {
00183                         return true;
00184                 }
00185                 
00186                 return false;
00187         }
00188 
00193         function connect()
00194         {
00195                 // check parameters
00196                 if (!$this->getdbHost() || !$this->getdbName() || !$this->getdbUser())
00197                 {
00198                         $this->error = "empty_fields";
00199                         return false;
00200                 }
00201 
00202                 $this->setDSN();
00203 
00204                 $this->db = DB::connect($this->dsn,true);
00205 
00206                 if (DB::isError($this->db))
00207                 {
00208                         $this->error = $this->db->getMessage()."! not_connected_to_db";
00209                         return false;
00210                 }
00211                 
00212                 // NOTE: Three sourcecodes use this or a similar handling:
00213                 // - classes/class.ilDBx.php
00214                 // - calendar/classes/class.ilCalInterface.php->setNames
00215                 // - setup/classes/class.ilClient.php
00216                 if ($this->isMysql4_1OrHigher())
00217                 {
00218                         $this->db->query("SET NAMES utf8");
00219                         $this->db->query("SET SESSION SQL_MODE = ''");
00220                 }
00221                 
00222                 $this->db_exists = true;
00223                 return true;
00224         }
00225 
00231         function isInstalledDB(&$a_db)
00232         {
00233                 $q = "SHOW TABLES";
00234                 $r = $a_db->query($q);
00235                 
00236                 $tables = array();
00237 
00238                 while ($row = $r->fetchRow(DB_FETCHMODE_ASSOC))
00239                 {
00240                         $tables[] = implode($row);
00241                 }
00242 
00243                 // check existence of some basic tables from ilias3 to determine if ilias3 is already installed in given database
00244                 if (in_array("object_data",$tables) and in_array("object_reference",$tables) and in_array("usr_data",$tables) and in_array("rbac_ua",$tables))
00245                 {
00246                         $this->db_installed = true;
00247                         return true;
00248                 }
00249                 
00250                 $this->db_installed = false;
00251                 return false;
00252         }
00253 
00257         function setDSN()
00258         {
00259 
00260                 $this->dsn_host = "mysql://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost();
00261                 $this->dsn = "mysql://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost()."/".$this->getdbName();
00262         }
00263 
00268         function setDbHost($a_str)
00269         {
00270                 $this->ini->setVariable("db","host",$a_str);
00271         }
00272         
00278         function getDbHost()
00279         {
00280                 return $this->ini->readVariable("db","host");
00281         }
00282 
00287         function setDbName($a_str)
00288         {
00289                 $this->ini->setVariable("db","name",$a_str);
00290         }
00291 
00296         function getDbName()
00297         {
00298                 return $this->ini->readVariable("db","name");
00299         }
00300 
00305         function setDbUser($a_str)
00306         {
00307                 $this->ini->setVariable("db","user",$a_str);
00308         }
00309         
00314         function getDbUser()
00315         {
00316                 return $this->ini->readVariable("db","user");
00317         }
00318 
00323         function setDbPass($a_str)
00324         {
00325                 $this->ini->setVariable("db","pass",$a_str);
00326         }
00327         
00332         function getDbPass()
00333         {
00334                 return $this->ini->readVariable("db","pass");
00335         }
00336 
00341         function getDataDir()
00342         {
00343                 return ILIAS_DATA_DIR."/".$this->getId();
00344         }
00345 
00350         function getWebspaceDir()
00351         {
00352                 return ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId();
00353         }
00354 
00359         function checkDatabaseHost()
00360         {
00361                 //connect to databasehost
00362                 $db = DB::connect($this->dsn_host);
00363 
00364                 if (DB::isError($db))
00365                 {
00366                         $this->error = $db->getMessage()."! Please check database hostname, username & password.";
00367                         return false;
00368                 }
00369                 
00370                 return true;
00371         }
00372 
00377         function checkDatabaseExists()
00378         {
00379                 //try to connect to database
00380                 $db = DB::connect($this->dsn);
00381 
00382                 if (DB::isError($db))
00383                 {
00384                         return false;
00385                 }
00386                 
00387                 if (!$this->isInstalledDB($db))
00388                 {
00389                         return false;
00390                 }
00391 
00392                 return true;
00393         }
00394 
00401         function getSetting($a_keyword)
00402         {
00403                 $q = "SELECT value FROM settings WHERE keyword='".$a_keyword."'";
00404                 $r = $this->db->query($q);
00405 
00406                 if ($r->numRows() > 0)
00407                 {
00408                         $row = $r->fetchRow();
00409                         return $row[0];
00410                 }
00411                 else
00412                 {
00413                         return false;
00414                 }
00415         }
00416 
00422         function getAllSettings()
00423         {
00424                 $q = "SELECT * FROM settings";
00425                 $r = $this->db->query($q);
00426 
00427                 while ($row = $r->fetchRow(DB_FETCHMODE_ASSOC))
00428                 {
00429                         $arr[$row["keyword"]] = $row["value"];
00430                 }
00431                 
00432                 return $arr;
00433         }
00434 
00442         function setSetting($a_key, $a_val)
00443         {
00444                 $q = "REPLACE INTO settings SET keyword = '".$a_key."', value = '".$a_val."'";
00445                 $this->db->query($q);
00446                 
00447                 return true;
00448         }
00449         
00454         function getURLStringForNIC($a_nic_url)
00455         {
00456                 $settings = $this->getAllSettings();
00457 
00458                 $inst_id = (empty($settings["inst_id"])) ? "0" : $settings["inst_id"];
00459 
00460                 // send host information to ilias-nic
00461                 $url =  $a_nic_url.
00462                                 "?cmd=getid".
00463                                 "&inst_id=".rawurlencode($inst_id).
00464                                 "&hostname=".rawurlencode($_SERVER["SERVER_NAME"]).
00465                                 "&ipadr=".rawurlencode($_SERVER["SERVER_ADDR"]).
00466                                 "&server_port=".rawurlencode($_SERVER["SERVER_PORT"]).
00467                                 "&server_software=".rawurlencode($_SERVER["SERVER_SOFTWARE"]).
00468                                 "&inst_name=".rawurlencode($this->ini->readVariable("client","name")).
00469                                 "&inst_info=".rawurlencode($this->ini->readVariable("client","description")).
00470                                 "&institution=".rawurlencode($settings["inst_institution"]).
00471                                 "&http_path=".rawurlencode(ILIAS_HTTP_PATH).
00472                                 "&contact_firstname=".rawurlencode($settings["admin_firstname"]).
00473                                 "&contact_lastname=".rawurlencode($settings["admin_lastname"]).
00474                                 "&contact_title=".rawurlencode($settings["admin_title"]).
00475                                 "&contact_position=".rawurlencode($settings["admin_position"]).                 
00476                                 "&contact_institution=".rawurlencode($settings["admin_institution"]).
00477                                 "&contact_street=".rawurlencode($settings["admin_street"]).
00478                                 "&contact_pcode=".rawurlencode($settings["admin_zipcode"]).
00479                                 "&contact_city=".rawurlencode($settings["admin_city"]).
00480                                 "&contact_country=".rawurlencode($settings["admin_country"]).
00481                                 "&contact_phone=".rawurlencode($settings["admin_phone"]).
00482                                 "&contact_email=".rawurlencode($settings["admin_email"]).
00483                                 "&nic_key=".rawurlencode($this->getNICkey()).
00484                                 "&version=".rawurlencode($settings["ilias_version"]);
00485                                 
00486                 return $url;
00487         }
00488         
00503         function updateNIC($a_nic_url)
00504         {
00505                 $url = $this->getURLStringForNIC($a_nic_url);
00506 
00507                 $conn =fopen($url,"r");
00508                 
00509                 $input = "";
00510         
00511                 if (!$conn) 
00512                 {
00513                         return false;
00514                 }
00515                 else
00516                 {
00517                         while(!feof($conn))
00518                         {
00519                                 $input.= fgets($conn, 4096);
00520                         }
00521 
00522                         fclose($conn);
00523                         $line = explode("\n",$input);
00524                         
00525                         $ret = $line;
00526                 }
00527 
00528                 $this->nic_status = $ret;
00529 
00530                 return true;
00531         }
00532         
00541         function setNICkey()
00542         {
00543                 mt_srand((double)microtime()*1000000);
00544                 $nic_key =      md5(str_replace(".","",$_SERVER["SERVER_ADDR"]) +
00545                                         mt_rand(100000,999999));
00546                 
00547                 $this->setSetting("nic_key",$nic_key);
00548                 
00549                 $this->nic_key = $nic_key;
00550                 
00551                 return true;
00552         }
00553         
00559         function getNICkey()
00560         {
00561                 $this->nic_key = $this->getSetting("nic_key");
00562                 
00563                 if (empty($this->nic_key))
00564                 {
00565                         $this->setNICkey();
00566                 }
00567                 
00568                 return $this->nic_key;
00569         }
00570         
00571         function getDefaultLanguage()
00572         {
00573                 return $this->getSetting("language");
00574         }
00575         
00576         function setDefaultLanguage($a_lang_key)
00577         {
00578                 $this->setSetting("language",$a_lang_key);
00579                 $this->ini->setVariable("language","default",$a_lang_key);
00580                 $this->ini->write();
00581                 
00582                 return true;
00583         }
00584 
00589         function getError()
00590         {
00591                 $error = $this->error;
00592                 $this->error = "";
00593 
00594                 return $error;
00595         }
00596         
00605         function delete ($a_ini = true, $a_db = false, $a_files = false)
00606         {
00607                 if ($a_ini === true and file_exists(ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId()."/client.ini.php"))
00608                 {
00609                         unlink(CLIENT_WEB_DIR."/client.ini.php");
00610                         $msg[] = "ini_deleted";
00611                 }
00612 
00613                 if ($a_db === true and $this->db_exists)
00614                 {
00615                         $this->db->query("DROP DATABASE ".$this->getDbName());
00616                         $msg[] = "db_deleted";
00617                 }
00618 
00619                 if ($a_files === true and file_exists(CLIENT_WEB_DIR) and is_dir(CLIENT_WEB_DIR))
00620                 {
00621                         // rmdir();
00622                         ilUtil::delDir(CLIENT_WEB_DIR);
00623                         ilUtil::delDir(CLIENT_DATA_DIR);
00624                         $msg[] = "files_deleted";
00625                 }
00626 
00627                 return $msg;
00628         }
00629 
00634         function create()
00635         {
00636                 //var_dump($this->getDataDir());exit;
00637                 // create base data dir
00638                 if (!ilUtil::makeDir($this->getDataDir()))
00639                 {
00640                         $this->error = "could_not_create_base_data_dir :".$this->getDataDir();
00641                         return false;
00642                 }
00643 
00644                 // create sub dirs in base data dir
00645                 if (!ilUtil::makeDir($this->getDataDir()."/mail"))
00646                 {
00647                         $this->error = "could_not_create_mail_data_dir :".$this->getDataDir()."/mail";
00648                         return false;
00649                 }
00650 
00651                 if (!ilUtil::makeDir($this->getDataDir()."/lm_data"))
00652                 {
00653                         $this->error = "could_not_create_lm_data_dir :".$this->getDataDir()."/lm_data";
00654                         return false;
00655                 }
00656 
00657                 if (!ilUtil::makeDir($this->getDataDir()."/forum"))
00658                 {
00659                         $this->error = "could_not_create_forum_data_dir :".$this->getDataDir()."/forum";
00660                         return false;
00661                 }
00662 
00663                 if (!ilUtil::makeDir($this->getDataDir()."/files"))
00664                 {
00665                         $this->error = "could_not_create_files_data_dir :".$this->getDataDir()."/files";
00666                         return false;
00667                 }
00668 
00669                 // create base webspace dir
00670                 if (!ilUtil::makeDir($this->getWebspaceDir()))
00671                 {
00672                         $this->error = "could_not_create_base_webspace_dir :".$this->getWebspaceDir();
00673                         return false;
00674                 }
00675 
00676                 // create sub dirs in base webspace dir
00677                 if (!ilUtil::makeDir($this->getWebspaceDir()."/lm_data"))
00678                 {
00679                         $this->error = "could_not_create_lm_webspace_dir :".$this->getWebspaceDir()."/lm_data";
00680                         return false;
00681                 }
00682 
00683                 if (!ilUtil::makeDir($this->getWebspaceDir()."/usr_images"))
00684                 {
00685                         $this->error = "could_not_create_usr_images_webspace_dir :".$this->getWebspaceDir()."/usr_images";
00686                         return false;
00687                 }
00688 
00689                 if (!ilUtil::makeDir($this->getWebspaceDir()."/mobs"))
00690                 {
00691                         $this->error = "could_not_create_mobs_webspace_dir :".$this->getWebspaceDir()."/mobs";
00692                         return false;
00693                 }
00694 
00695                 if (!ilUtil::makeDir($this->getWebspaceDir()."/css"))
00696                 {
00697                         $this->error = "could_not_create_css_webspace_dir :".$this->getWebspaceDir()."/css";
00698                         return false;
00699                 }
00700 
00701                 // write client ini
00702                 if (!$this->ini->write())
00703                 {
00704                         $this->error = get_class($this).": ".$this->ini->getError();
00705                         return false;
00706                 }
00707 
00708                 return true;
00709         }
00710 } // END class.ilClient
00711 ?>

Generated on Fri Dec 13 2013 17:57:03 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1