ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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
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
27 public $error = '';
31 public $db;
35 public $ini;
36
37
44 public function __construct($a_client_id, $a_db_connections)
45 {
46 if ($a_client_id)
47 {
48 $this->id = $a_client_id;
49 $this->ini_file_path = ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId()."/client.ini.php";
50 }
51
52 $this->db_connections = $a_db_connections;
53
54 // set path default.ini
55 $this->client_defaults = ILIAS_ABSOLUTE_PATH."/setup/client.master.ini.php";
56 }
57
58
62 public function getDBSetup() {
63 require_once('./setup/classes/class.ilDbSetup.php');
64
66 }
67
73 function init()
74 {
75 $this->ini = new ilIniFile($this->ini_file_path);
76
77 // load defaults only if no client.ini was found
78 if (!@file_exists($this->ini_file_path)) {
79 //echo "<br>A-".$this->ini_file_path."-";
80 $this->ini->GROUPS = parse_ini_file($this->client_defaults, true);
81
82 return false;
83 }
84
85 // read client.ini
86 if (!$this->ini->read()) {
87 $this->error = get_class($this) . ": " . $this->ini->getError();
88
89 return false;
90 }
91
92 // only for ilias main
93 define("CLIENT_WEB_DIR", ILIAS_ABSOLUTE_PATH . "/" . ILIAS_WEB_DIR . "/" . $this->getId());
94 define("CLIENT_DATA_DIR", ILIAS_DATA_DIR . "/" . $this->getId());
95 define("DEVMODE", $this->ini->readVariable('system', 'DEVMODE'));
96 define("ROOT_FOLDER_ID", $this->ini->readVariable('system', 'ROOT_FOLDER_ID'));
97 define("SYSTEM_FOLDER_ID", $this->ini->readVariable('system', 'SYSTEM_FOLDER_ID'));
98 define("ROLE_FOLDER_ID", $this->ini->readVariable('system', 'ROLE_FOLDER_ID'));
99 define("ANONYMOUS_USER_ID", 13);
100 define("ANONYMOUS_ROLE_ID", 14);
101 define("SYSTEM_USER_ID", 6);
102 define("SYSTEM_ROLE_ID", 2);
103
104 $this->db_exists = $this->getDBSetup()->isConnectable();
105 $this->getDBSetup()->provideGlobalDB();
106 if ($this->db_exists) {
107 $this->db_installed = $this->getDBSetup()->isDatabaseInstalled();
108 }
109
110 return true;
111 }
112
113
114 public function provideGlobalDB() {
115 $this->getDBSetup()->provideGlobalDB();
116 }
117
118
119 public function revokeGlobalDB() {
120 $this->getDBSetup()->provideGlobalDB();
121 }
122
127 function getId()
128 {
129 return $this->id;
130 }
131
136 function setId($a_client_id)
137 {
138 $this->id = $a_client_id;
139 $this->webspace_dir = ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->id;
140 }
141
146 function getName()
147 {
148 return $this->ini->readVariable("client","name");
149 }
150
155 function setName($a_str)
156 {
157 $this->ini->setVariable("client","name",$a_str);
158 }
159
164 function getDescription()
165 {
166 return $this->ini->readVariable("client","description");
167 }
168
173 function setDescription($a_str)
174 {
175 $this->ini->setVariable("client","description",$a_str);
176 }
177
181/* function getMySQLVersion()
182 {
183 return mysql_get_server_info();
184 }*/
185
189 function getDB()
190 {
191 return $this->db;
192 }
193
198 function connect()
199 {
200 // check parameters
201 // To support oracle tnsnames.ora dbname is not required
202 if (!$this->getdbHost() || !$this->getdbUser()) {
203 $this->error = "empty_fields";
204
205 return false;
206 }
207
208 include_once("./Services/Database/classes/class.ilDBWrapperFactory.php");
209 $this->db = ilDBWrapperFactory::getWrapper($this->getdbType(),
210 $this->ini->readVariable("db","inactive_mysqli"));
211 $this->db->setDBUser($this->getdbUser());
212 $this->db->setDBPort($this->getdbPort());
213 $this->db->setDBPassword($this->getdbPass());
214 $this->db->setDBHost($this->getdbHost());
215 $this->db->setDBName($this->getdbName());
216 $con = $this->db->connect(true);
217
218 if (!$con)
219 {
220 $this->error = "Database connection failed.";
221 return false;
222 }
223 $GLOBALS["ilDB"] = $this->db;
224 $GLOBALS["DIC"]["ilDB"] = function($c) {
225 return $GLOBALS["ilDB"];
226 };
227
228 $this->db_exists = true;
229 return true;
230 }
231
237 function isInstalledDB(&$a_db)
238 {
239 if(method_exists($a_db,'loadModule'))
240 {
241 $a_db->loadModule('Manager');
242 }
243 if(!$tables = $a_db->listTables())
244 {
245 return false;
246 }
247
248 // check existence of some basic tables from ilias3 to determine if ilias3 is already installed in given database
249 if (in_array("object_data",$tables) and in_array("object_reference",$tables) and in_array("usr_data",$tables) and in_array("rbac_ua",$tables))
250 {
251 $this->db_installed = true;
252 return true;
253 }
254 $this->db_installed = false;
255 return false;
256 }
257
261 function setDSN()
262 {
263 switch($this->getDbType())
264 {
265 case "oracle":
266 //$this->dsn_host = "oci8://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost();
267 $this->dsn_host = array(
268 'phptype' => 'oci8',
269 'hostspec' => $this->getdbHost(),
270 'username' => $this->getdbUser(),
271 'port' => $this->getdbPort(),
272 'password' => $this->getdbPass(),
273 );
274 //$this->dsn = "oci8://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost()."/?service=".$this->getdbName();
275 $this->dsn = $this->dsn = array(
276 'phptype' => 'oci8',
277 'hostspec' => $this->getdbHost(),
278 'username' => $this->getdbUser(),
279 'port' => $this->getdbPort(),
280 'password' => $this->getdbPass(),
281 'service' => $this->getdbName()
282 );
283 break;
284
285 case "postgres":
286 $db_port_str = "";
287 if (trim($this->getdbPort()) != "")
288 {
289 $db_port_str = ":".$this->getdbPort();
290 }
291 $this->dsn_host = "pgsql://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost().$db_port_str;
292 $this->dsn = "pgsql://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost().$db_port_str."/".$this->getdbName();
293 break;
294
295 case "mysql":
296 case "innodb":
297 default:
298 $db_port_str = "";
299 if (trim($this->getdbPort()) != "")
300 {
301 $db_port_str = ":".$this->getdbPort();
302 }
303 $this->dsn_host = "mysql://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost().$db_port_str;
304 $this->dsn = "mysql://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost().$db_port_str."/".$this->getdbName();
305 break;
306 }
307 }
308
313 function setDbHost($a_str)
314 {
315 $this->ini->setVariable("db","host",$a_str);
316 }
317
323 function getDbHost()
324 {
325 return $this->ini->readVariable("db","host");
326 }
327
332 function setDbName($a_str)
333 {
334 $this->ini->setVariable("db","name",$a_str);
335 }
336
341 function getDbName()
342 {
343 return $this->ini->readVariable("db","name");
344 }
345
350 function setDbUser($a_str)
351 {
352 $this->ini->setVariable("db","user",$a_str);
353 }
354
359 function getDbUser()
360 {
361 return $this->ini->readVariable("db","user");
362 }
363
368 function getDbPort()
369 {
370 return $this->ini->readVariable("db","port");
371 }
372
377 function setDbPort($a_str)
378 {
379 $this->ini->setVariable("db","port",$a_str);
380 }
381
386 function setDbPass($a_str)
387 {
388 $this->ini->setVariable("db","pass",$a_str);
389 }
390
395 function getDbPass()
396 {
397 return $this->ini->readVariable("db","pass");
398 }
399
404 function setDbSlaveActive($a_act)
405 {
406 $this->ini->setVariable("db","slave_active", (int) $a_act);
407 }
408
415 {
416 return (int) $this->ini->readVariable("db","slave_active");
417 }
418
423 function setDbSlaveHost($a_str)
424 {
425 $this->ini->setVariable("db","slave_host",$a_str);
426 }
427
433 function getDbSlaveHost()
434 {
435 return $this->ini->readVariable("db","slave_host");
436 }
437
442 function setDbSlaveName($a_str)
443 {
444 $this->ini->setVariable("db","slave_name",$a_str);
445 }
446
451 function getDbSlaveName()
452 {
453 return $this->ini->readVariable("db","slave_name");
454 }
455
460 function setDbSlaveUser($a_str)
461 {
462 $this->ini->setVariable("db","slave_user",$a_str);
463 }
464
469 function getDbSlaveUser()
470 {
471 return $this->ini->readVariable("db","slave_user");
472 }
473
478 function getDbSlavePort()
479 {
480 return $this->ini->readVariable("db","slave_port");
481 }
482
487 function setDbSlavePort($a_str)
488 {
489 $this->ini->setVariable("db","slave_port",$a_str);
490 }
491
496 function setDbSlavePass($a_str)
497 {
498 $this->ini->setVariable("db","slave_pass",$a_str);
499 }
500
505 function getDbSlavePass()
506 {
507 return $this->ini->readVariable("db","slave_pass");
508 }
509
514 function setDbType($a_str)
515 {
516 $this->ini->setVariable("db","type",$a_str);
517 }
518
523 function getDbType()
524 {
525 $val = $this->ini->readVariable("db","type");
526 if ($val == "")
527 {
528 return "mysql";
529 }
530 else
531 {
532 return $val;
533 }
534 }
535
540 function getDataDir()
541 {
542 return ILIAS_DATA_DIR."/".$this->getId();
543 }
544
549 function getWebspaceDir()
550 {
551 return ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId();
552 }
553
554
559 function checkDatabaseExists($a_keep_connection = false)
560 {
561 return $this->getDBSetup()->isConnectable();
562
563 //try to connect to database
564 $db = $this->db_connections->connectDB($this->dsn);
565 if (MDB2::isError($db))
566 {
567 return false;
568 }
569
570 if (!$this->isInstalledDB($db))
571 {
572 return false;
573 }
574
575 // #10633
576 if($a_keep_connection)
577 {
578 $GLOBALS["ilDB"] = $this->db;
579 $GLOBALS["DIC"]["ilDB"] = function($c) {
580 return $GLOBALS["ilDB"];
581 };
582 }
583
584 return true;
585 }
586
587 function reconnect()
588 {
589 $this->connect();
590 }
591
592
600 public function getSetting($a_keyword) {
601 global $ilDB;
602 if (!$this->getDBSetup()->isDatabaseInstalled() || !$ilDB) {
603 return false;
604 }
605 include_once './Services/Administration/classes/class.ilSetting.php';
606 $set = new ilSetting("common", true);
607
608 return $set->get($a_keyword);
609 }
610
616 function getAllSettings()
617 {
618 include_once './Services/Administration/classes/class.ilSetting.php';
619 $set = new ilSetting("common", true);
620 return $set->getAll();
621 }
622
630 function setSetting($a_key, $a_val)
631 {
632 include_once './Services/Administration/classes/class.ilSetting.php';
633 $set = new ilSetting("common", true);
634 $set->set($a_key, $a_val);
635 }
636
641 function getURLStringForNIC($a_nic_url)
642 {
643 $settings = $this->getAllSettings();
644
645 $inst_id = (empty($settings["inst_id"])) ? "0" : $settings["inst_id"];
646
647 // send host information to ilias-nic
648 //#18132: removed ipadr, server_port, server_software, institution, contact_title, contact_position,
649 // contact_institution, contact_street, contact_pcode, contact_city, contact_country, contact_phone
650 $url = $a_nic_url.
651 "?cmd=getid".
652 "&inst_id=".rawurlencode($inst_id).
653 "&hostname=".rawurlencode($_SERVER["SERVER_NAME"]).
654 "&inst_name=".rawurlencode($this->ini->readVariable("client","name")).
655 "&inst_info=".rawurlencode($this->ini->readVariable("client","description")).
656 "&http_path=".rawurlencode(ILIAS_HTTP_PATH).
657 "&contact_firstname=".rawurlencode($settings["admin_firstname"]).
658 "&contact_lastname=".rawurlencode($settings["admin_lastname"]).
659 "&contact_email=".rawurlencode($settings["admin_email"]).
660 "&nic_key=".rawurlencode($this->getNICkey());
661
662 return $url;
663 }
664
679 function updateNIC($a_nic_url)
680 {
681 $max_redirects = 5;
682 $socket_timeout = 5;
683
684 require_once(__DIR__."/../../Services/WebServices/Curl/classes/class.ilCurlConnection.php");
686 $this->setError("CURL-extension not loaded.");
687 return false;
688 }
689
690 $url = $this->getURLStringForNIC($a_nic_url);
691 $req = new ilCurlConnection($url);
692 $req->init();
693
694 $settings = $this->getAllSettings();
695 if((bool)$settings['proxy_status'] && strlen($settings['proxy_host']) && strlen($settings['proxy_port']))
696 {
697 $req->setOpt(CURLOPT_HTTPPROXYTUNNEL, true);
698 $req->setOpt(CURLOPT_PROXY, $settings["proxy_host"]);
699 $req->setOpt(CURLOPT_PROXYPORT, $settings["proxy_port"]);
700 }
701
702 $req->setOpt(CURLOPT_HEADER, 1);
703 $req->setOpt(CURLOPT_RETURNTRANSFER, 1);
704 $req->setOpt(CURLOPT_CONNECTTIMEOUT, $socket_timeout);
705 $req->setOpt(CURLOPT_MAXREDIRS, $max_redirects);
706 $response = $req->exec();
707
708 $req->parseResponse($response);
709 $response_body = $req->getResponseBody();
710
711 $info = $req->getInfo();
712 if ($info["http_code"] != "200") {
713 $this->setError("Could not connect to NIC-Server at '".$url."'");
714 return false;
715 }
716
717 $this->nic_status = explode("\n", $response_body);
718
720
721 return true;
722 }
723
732 function setNICkey()
733 {
734 mt_srand((double)microtime()*1000000);
735 $nic_key = md5(str_replace(".","",$_SERVER["SERVER_ADDR"]) +
736 mt_rand(100000,999999));
737
738 $this->setSetting("nic_key",$nic_key);
739
740 $this->nic_key = $nic_key;
741
742 return true;
743 }
744
750 function getNICkey()
751 {
752 $this->nic_key = $this->getSetting("nic_key");
753
754 if (empty($this->nic_key))
755 {
756 $this->setNICkey();
757 }
758
759 return $this->nic_key;
760 }
761
763 {
764 return $this->getSetting("language");
765 }
766
767 function setDefaultLanguage($a_lang_key)
768 {
769 $this->setSetting("language",$a_lang_key);
770 $this->ini->setVariable("language","default",$a_lang_key);
771 $this->ini->write();
772
773 return true;
774 }
775
776
782 function getError() {
784 $this->error = "";
785
786 return $error;
787 }
788
789
793 public function setError($error_message) {
794 $this->error = $error_message;
795 }
796
805 function delete ($a_ini = true, $a_db = false, $a_files = false)
806 {
807 if ($a_ini === true and file_exists(ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId()."/client.ini.php"))
808 {
809 unlink(CLIENT_WEB_DIR."/client.ini.php");
810 $msg[] = "ini_deleted";
811 }
812
813 if ($a_db === true and $this->db_exists)
814 {
815 $this->db->query("DROP DATABASE ".$this->getDbName());
816 $msg[] = "db_deleted";
817 }
818
819 if ($a_files === true and file_exists(CLIENT_WEB_DIR) and is_dir(CLIENT_WEB_DIR))
820 {
821 // rmdir();
822 ilUtil::delDir(CLIENT_WEB_DIR);
823 ilUtil::delDir(CLIENT_DATA_DIR);
824 $msg[] = "files_deleted";
825 }
826
827 return $msg;
828 }
829
834 function create()
835 {
836 //var_dump($this->getDataDir());exit;
837 // create base data dir
838 if (!ilUtil::makeDir($this->getDataDir()))
839 {
840 $this->error = "could_not_create_base_data_dir :".$this->getDataDir();
841 return false;
842 }
843
844 // create sub dirs in base data dir
845 if (!ilUtil::makeDir($this->getDataDir()."/mail"))
846 {
847 $this->error = "could_not_create_mail_data_dir :".$this->getDataDir()."/mail";
848 return false;
849 }
850
851 if (!ilUtil::makeDir($this->getDataDir()."/lm_data"))
852 {
853 $this->error = "could_not_create_lm_data_dir :".$this->getDataDir()."/lm_data";
854 return false;
855 }
856
857 if (!ilUtil::makeDir($this->getDataDir()."/forum"))
858 {
859 $this->error = "could_not_create_forum_data_dir :".$this->getDataDir()."/forum";
860 return false;
861 }
862
863 if (!ilUtil::makeDir($this->getDataDir()."/files"))
864 {
865 $this->error = "could_not_create_files_data_dir :".$this->getDataDir()."/files";
866 return false;
867 }
868
869 // create base webspace dir
870 if (!ilUtil::makeDir($this->getWebspaceDir()))
871 {
872 $this->error = "could_not_create_base_webspace_dir :".$this->getWebspaceDir();
873 return false;
874 }
875
876 // create sub dirs in base webspace dir
877 if (!ilUtil::makeDir($this->getWebspaceDir()."/lm_data"))
878 {
879 $this->error = "could_not_create_lm_webspace_dir :".$this->getWebspaceDir()."/lm_data";
880 return false;
881 }
882
883 if (!ilUtil::makeDir($this->getWebspaceDir()."/usr_images"))
884 {
885 $this->error = "could_not_create_usr_images_webspace_dir :".$this->getWebspaceDir()."/usr_images";
886 return false;
887 }
888
889 if (!ilUtil::makeDir($this->getWebspaceDir()."/mobs"))
890 {
891 $this->error = "could_not_create_mobs_webspace_dir :".$this->getWebspaceDir()."/mobs";
892 return false;
893 }
894
895 if (!ilUtil::makeDir($this->getWebspaceDir()."/css"))
896 {
897 $this->error = "could_not_create_css_webspace_dir :".$this->getWebspaceDir()."/css";
898 return false;
899 }
900
901 // write client ini
902 if (!$this->ini->write())
903 {
904 $this->error = get_class($this).": ".$this->ini->getError();
905 return false;
906 }
907
908 return true;
909 }
910
917 function writeIni()
918 {
919 $this->ini->write();
920 }
921
922} // END class.ilClient
923?>
An exception for terminatinating execution or to throw for unit testing.
isError($data, $code=null)
Tell whether a value is a MDB2 error.
Definition: MDB2.php:599
error($a_errmsg)
set error message @access public
Client Management.
getAllSettings()
read all values from settings table @access public
setName($a_str)
set client name
getDbUser()
get db user
getDbPort()
get db port
getDB()
get mysql version
init()
init client load client.ini and set some constants
isInstalledDB(&$a_db)
check if client db is installed
setDbSlaveHost($a_str)
set the slave host
setDbPort($a_str)
set db port
getDbSlaveName()
get name of slave database
setDescription($a_str)
set client description
setDbName($a_str)
set the name of database
setDbSlaveName($a_str)
set the name of slave database
__construct($a_client_id, $a_db_connections)
ilClient constructor.
create()
create a new client and its subdirectories
setNICkey()
set nic_key generate nic_key if nic_key field in cust table is empty.
getDataDir()
get client datadir path
writeIni()
write init
getWebspaceDir()
get client webspacedir path
getURLStringForNIC($a_nic_url)
setDbSlaveUser($a_str)
set slave db user
getDbName()
get name of database
getDbSlaveActive()
get slave active
getDbType()
get type of database
setId($a_client_id)
set client id
setError($error_message)
getDbSlaveUser()
get slave db user
setDbType($a_str)
set the type of database
getSetting($a_keyword)
read one value from settings table
setDbSlavePort($a_str)
set slave db port
getDbSlavePort()
get slave db port
getId()
get client id
getDbHost()
get db host
setDefaultLanguage($a_lang_key)
connect()
connect to client database
getName()
get client name
setDbSlavePass($a_str)
set slave db password
setDbPass($a_str)
set db password
setDbSlaveActive($a_act)
set the slave active
setSetting($a_key, $a_val)
write one value to settings table @access public
getDbPass()
get db password
getDbSlavePass()
get slave db password
getDbSlaveHost()
get db slave host
updateNIC($a_nic_url)
Connect to ILIAS-NIC.
setDbUser($a_str)
set db user
setDSN()
set the dsn and dsn_host
getNICkey()
get nic_key @access public
getDescription()
get client description
setDbHost($a_str)
set the host
getError()
get error message and clear error var
checkDatabaseExists($a_keep_connection=false)
check database connection with database name
static _isCurlExtensionLoaded()
Check if curl extension is loaded.
static getInstanceForClient(\ilClient $client)
INIFile Parser.
static getLogger($a_component_id)
Get component logger.
ILIAS Setting Class.
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static makeDir($a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
$info
Definition: example_052.php:80
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
$url
Definition: shib_logout.php:72
global $ilDB
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']