• Main Page
  • Related Pages
  • 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-2001 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 
00033 class ilClient
00034 {
00035         var $id;                                        // client_id (md5 hash)
00036         var $dir;                                       // directory name in ilias/clients/
00037         var $name;                                      // installation name
00038         var $db_exists = false;         // db exists?
00039         var $db_installed = false;      // db installed?
00040 
00041         var $client_defaults;           // default settings
00042         var $status;                            // contains status infos about setup process (todo: move function to this class)
00043         var $setup_ok = false;          // if client setup was finished at least once, this is set to true
00044         var $nic_status;                        // contains received data of ILIAS-NIC server when registering
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                 // set path default.ini
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                 // load defaults only if no client.ini was found
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                 // read client.ini
00079                 if (!$this->ini->read())
00080                 {
00081                         $this->error = get_class($this).": ".$this->ini->getError();
00082                         return false;           
00083                 }
00084 
00085                 // only for ilias main
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 ("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 
00165         function connect()
00166         {
00167                 // check parameters
00168                 if (!$this->getdbHost() || !$this->getdbName() || !$this->getdbUser())
00169                 {
00170                         $this->error = "empty_fields";
00171                         return false;
00172                 }
00173 
00174                 $this->setDSN();
00175 
00176                 $this->db = DB::connect($this->dsn,true);
00177                 
00178                 if (DB::isError($this->db))
00179                 {
00180                         $this->error = $this->db->getMessage()."! not_connected_to_db";
00181                         return false;
00182                 }
00183                 
00184                 $this->db_exists = true;
00185                 return true;
00186         }
00187 
00193         function isInstalledDB(&$a_db)
00194         {
00195                 $q = "SHOW TABLES";
00196                 $r = $a_db->query($q);
00197                 
00198                 $tables = array();
00199 
00200                 while ($row = $r->fetchRow(DB_FETCHMODE_ASSOC))
00201                 {
00202                         $tables[] = implode($row);
00203                 }
00204 
00205                 // check existence of some basic tables from ilias3 to determine if ilias3 is already installed in given database
00206                 if (in_array("object_data",$tables) and in_array("object_reference",$tables) and in_array("usr_data",$tables) and in_array("rbac_ua",$tables))
00207                 {
00208                         $this->db_installed = true;
00209                         return true;
00210                 }
00211                 
00212                 $this->db_installed = false;
00213                 return false;
00214         }
00215 
00219         function setDSN()
00220         {
00221 
00222                 $this->dsn_host = "mysql://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost();
00223                 $this->dsn = "mysql://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost()."/".$this->getdbName();
00224         }
00225 
00230         function setDbHost($a_str)
00231         {
00232                 $this->ini->setVariable("db","host",$a_str);
00233         }
00234         
00240         function getDbHost()
00241         {
00242                 return $this->ini->readVariable("db","host");
00243         }
00244 
00249         function setDbName($a_str)
00250         {
00251                 $this->ini->setVariable("db","name",$a_str);
00252         }
00253 
00258         function getDbName()
00259         {
00260                 return $this->ini->readVariable("db","name");
00261         }
00262 
00267         function setDbUser($a_str)
00268         {
00269                 $this->ini->setVariable("db","user",$a_str);
00270         }
00271         
00276         function getDbUser()
00277         {
00278                 return $this->ini->readVariable("db","user");
00279         }
00280 
00285         function setDbPass($a_str)
00286         {
00287                 $this->ini->setVariable("db","pass",$a_str);
00288         }
00289         
00294         function getDbPass()
00295         {
00296                 return $this->ini->readVariable("db","pass");
00297         }
00298 
00303         function getDataDir()
00304         {
00305                 return ILIAS_DATA_DIR."/".$this->getId();
00306         }
00307 
00312         function getWebspaceDir()
00313         {
00314                 return ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId();
00315         }
00316 
00321         function checkDatabaseHost()
00322         {
00323                 //connect to databasehost
00324                 $db = DB::connect($this->dsn_host);
00325 
00326                 if (DB::isError($db))
00327                 {
00328                         $this->error = $db->getMessage()."! Please check database hostname, username & password.";
00329                         return false;
00330                 }
00331                 
00332                 return true;
00333         }
00334 
00339         function checkDatabaseExists()
00340         {
00341                 //try to connect to database
00342                 $db = DB::connect($this->dsn);
00343 
00344                 if (DB::isError($db))
00345                 {
00346                         return false;
00347                 }
00348                 
00349                 if (!$this->isInstalledDB($db))
00350                 {
00351                         return false;
00352                 }
00353 
00354                 return true;
00355         }
00356 
00363         function getSetting($a_keyword)
00364         {
00365                 $q = "SELECT value FROM settings WHERE keyword='".$a_keyword."'";
00366                 $r = $this->db->query($q);
00367 
00368                 if ($r->numRows() > 0)
00369                 {
00370                         $row = $r->fetchRow();
00371                         return $row[0];
00372                 }
00373                 else
00374                 {
00375                         return false;
00376                 }
00377         }
00378 
00384         function getAllSettings()
00385         {
00386                 $q = "SELECT * FROM settings";
00387                 $r = $this->db->query($q);
00388 
00389                 while ($row = $r->fetchRow(DB_FETCHMODE_ASSOC))
00390                 {
00391                         $arr[$row["keyword"]] = $row["value"];
00392                 }
00393                 
00394                 return $arr;
00395         }
00396 
00404         function setSetting($a_key, $a_val)
00405         {
00406                 $q = "REPLACE INTO settings SET keyword = '".$a_key."', value = '".$a_val."'";
00407                 $this->db->query($q);
00408                 
00409                 return true;
00410         }
00411         
00416         function getURLStringForNIC($a_nic_url)
00417         {
00418                 $settings = $this->getAllSettings();
00419 
00420                 $inst_id = (empty($settings["inst_id"])) ? "0" : $settings["inst_id"];
00421 
00422                 // send host information to ilias-nic
00423                 $url =  $a_nic_url.
00424                                 "?cmd=getid".
00425                                 "&inst_id=".rawurlencode($inst_id).
00426                                 "&hostname=".rawurlencode($_SERVER["SERVER_NAME"]).
00427                                 "&ipadr=".rawurlencode($_SERVER["SERVER_ADDR"]).
00428                                 "&server_port=".rawurlencode($_SERVER["SERVER_PORT"]).
00429                                 "&server_software=".rawurlencode($_SERVER["SERVER_SOFTWARE"]).
00430                                 "&inst_name=".rawurlencode($this->ini->readVariable("client","name")).
00431                                 "&inst_info=".rawurlencode($this->ini->readVariable("client","description")).
00432                                 "&institution=".rawurlencode($settings["inst_institution"]).
00433                                 "&http_path=".rawurlencode(ILIAS_HTTP_PATH).
00434                                 "&contact_firstname=".rawurlencode($settings["admin_firstname"]).
00435                                 "&contact_lastname=".rawurlencode($settings["admin_lastname"]).
00436                                 "&contact_title=".rawurlencode($settings["admin_title"]).
00437                                 "&contact_position=".rawurlencode($settings["admin_position"]).                 
00438                                 "&contact_institution=".rawurlencode($settings["admin_institution"]).
00439                                 "&contact_street=".rawurlencode($settings["admin_street"]).
00440                                 "&contact_pcode=".rawurlencode($settings["admin_zipcode"]).
00441                                 "&contact_city=".rawurlencode($settings["admin_city"]).
00442                                 "&contact_country=".rawurlencode($settings["admin_country"]).
00443                                 "&contact_phone=".rawurlencode($settings["admin_phone"]).
00444                                 "&contact_email=".rawurlencode($settings["admin_email"]).
00445                                 "&nic_key=".rawurlencode($this->getNICkey()).
00446                                 "&version=".rawurlencode($settings["ilias_version"]);
00447                                 
00448                 return $url;
00449         }
00450         
00465         function updateNIC($a_nic_url)
00466         {
00467                 $url = $this->getURLStringForNIC($a_nic_url);
00468 
00469                 $conn =fopen($url,"r");
00470                 
00471                 $input = "";
00472         
00473                 if (!$conn) 
00474                 {
00475                         return false;
00476                 }
00477                 else
00478                 {
00479                         while(!feof($conn))
00480                         {
00481                                 $input.= fgets($conn, 4096);
00482                         }
00483 
00484                         fclose($conn);
00485                         $line = explode("\n",$input);
00486                         
00487                         $ret = $line;
00488                 }
00489 
00490                 $this->nic_status = $ret;
00491 
00492                 return true;
00493         }
00494         
00503         function setNICkey()
00504         {
00505                 mt_srand((double)microtime()*1000000);
00506                 $nic_key =      md5(str_replace(".","",$_SERVER["SERVER_ADDR"]) +
00507                                         mt_rand(100000,999999));
00508                 
00509                 $this->setSetting("nic_key",$nic_key);
00510                 
00511                 $this->nic_key = $nic_key;
00512                 
00513                 return true;
00514         }
00515         
00521         function getNICkey()
00522         {
00523                 $this->nic_key = $this->getSetting("nic_key");
00524                 
00525                 if (empty($this->nic_key))
00526                 {
00527                         $this->setNICkey();
00528                 }
00529                 
00530                 return $this->nic_key;
00531         }
00532         
00533         function getDefaultLanguage()
00534         {
00535                 return $this->getSetting("language");
00536         }
00537         
00538         function setDefaultLanguage($a_lang_key)
00539         {
00540                 $this->setSetting("language",$a_lang_key);
00541                 $this->ini->setVariable("language","default",$a_lang_key);
00542                 $this->ini->write();
00543                 
00544                 return true;
00545         }
00546 
00551         function getError()
00552         {
00553                 $error = $this->error;
00554                 $this->error = "";
00555 
00556                 return $error;
00557         }
00558         
00567         function delete ($a_ini = true, $a_db = false, $a_files = false)
00568         {
00569                 if ($a_ini === true and file_exists(ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId()."/client.ini.php"))
00570                 {
00571                         unlink(CLIENT_WEB_DIR."/client.ini.php");
00572                         $msg[] = "ini_deleted";
00573                 }
00574 
00575                 if ($a_db === true and $this->db_exists)
00576                 {
00577                         $this->db->query("DROP DATABASE ".$this->getDbName());
00578                         $msg[] = "db_deleted";
00579                 }
00580 
00581                 if ($a_files === true and file_exists(CLIENT_WEB_DIR) and is_dir(CLIENT_WEB_DIR))
00582                 {
00583                         // rmdir();
00584                         ilUtil::delDir(CLIENT_WEB_DIR);
00585                         ilUtil::delDir(CLIENT_DATA_DIR);
00586                         $msg[] = "files_deleted";
00587                 }
00588 
00589                 return $msg;
00590         }
00591 
00596         function create()
00597         {
00598                 //var_dump($this->getDataDir());exit;
00599                 // create base data dir
00600                 if (!ilUtil::makeDir($this->getDataDir()))
00601                 {
00602                         $this->error = "could_not_create_base_data_dir :".$this->getDataDir();
00603                         return false;
00604                 }
00605 
00606                 // create sub dirs in base data dir
00607                 if (!ilUtil::makeDir($this->getDataDir()."/mail"))
00608                 {
00609                         $this->error = "could_not_create_mail_data_dir :".$this->getDataDir()."/mail";
00610                         return false;
00611                 }
00612 
00613                 if (!ilUtil::makeDir($this->getDataDir()."/lm_data"))
00614                 {
00615                         $this->error = "could_not_create_lm_data_dir :".$this->getDataDir()."/lm_data";
00616                         return false;
00617                 }
00618 
00619                 if (!ilUtil::makeDir($this->getDataDir()."/forum"))
00620                 {
00621                         $this->error = "could_not_create_forum_data_dir :".$this->getDataDir()."/forum";
00622                         return false;
00623                 }
00624 
00625                 if (!ilUtil::makeDir($this->getDataDir()."/files"))
00626                 {
00627                         $this->error = "could_not_create_files_data_dir :".$this->getDataDir()."/files";
00628                         return false;
00629                 }
00630 
00631                 // create base webspace dir
00632                 if (!ilUtil::makeDir($this->getWebspaceDir()))
00633                 {
00634                         $this->error = "could_not_create_base_webspace_dir :".$this->getWebspaceDir();
00635                         return false;
00636                 }
00637 
00638                 // create sub dirs in base webspace dir
00639                 if (!ilUtil::makeDir($this->getWebspaceDir()."/lm_data"))
00640                 {
00641                         $this->error = "could_not_create_lm_webspace_dir :".$this->getWebspaceDir()."/lm_data";
00642                         return false;
00643                 }
00644 
00645                 if (!ilUtil::makeDir($this->getWebspaceDir()."/usr_images"))
00646                 {
00647                         $this->error = "could_not_create_usr_images_webspace_dir :".$this->getWebspaceDir()."/usr_images";
00648                         return false;
00649                 }
00650 
00651                 if (!ilUtil::makeDir($this->getWebspaceDir()."/mobs"))
00652                 {
00653                         $this->error = "could_not_create_mobs_webspace_dir :".$this->getWebspaceDir()."/mobs";
00654                         return false;
00655                 }
00656 
00657                 if (!ilUtil::makeDir($this->getWebspaceDir()."/css"))
00658                 {
00659                         $this->error = "could_not_create_css_webspace_dir :".$this->getWebspaceDir()."/css";
00660                         return false;
00661                 }
00662 
00663                 // write client ini
00664                 if (!$this->ini->write())
00665                 {
00666                         $this->error = get_class($this).": ".$this->ini->getError();
00667                         return false;
00668                 }
00669 
00670                 return true;
00671         }
00672 } // END class.ilClient
00673 ?>

Generated on Fri Dec 13 2013 09:06:37 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1