ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 public $id; // client_id (md5 hash)
15 public $dir; // directory name in ilias/clients/
16 public $name; // installation name
17 public $db_exists = false; // db exists?
18 public $db_installed = false; // db installed?
19
20 public $client_defaults; // default settings
21 public $status; // contains status infos about setup process (todo: move function to this class)
22 public $setup_ok = false; // if client setup was finished at least once, this is set to true
23 public $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 $this->id = $a_client_id;
48 $this->ini_file_path = ILIAS_ABSOLUTE_PATH . "/" . ILIAS_WEB_DIR . "/" . $this->getId() . "/client.ini.php";
49 }
50
51 $this->db_connections = $a_db_connections;
52
53 // set path default.ini
54 $this->client_defaults = ILIAS_ABSOLUTE_PATH . "/setup/client.master.ini.php";
55 }
56
57
61 public function getDBSetup()
62 {
63 require_once('./setup/classes/class.ilDbSetup.php');
64
66 }
67
73 public 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 {
116 $this->getDBSetup()->provideGlobalDB();
117 }
118
119
120 public function revokeGlobalDB()
121 {
122 $this->getDBSetup()->provideGlobalDB();
123 }
124
129 public function getId()
130 {
131 return $this->id;
132 }
133
138 public function setId($a_client_id)
139 {
140 $this->id = $a_client_id;
141 $this->webspace_dir = ILIAS_ABSOLUTE_PATH . "/" . ILIAS_WEB_DIR . "/" . $this->id;
142 }
143
148 public function getName()
149 {
150 return $this->ini->readVariable("client", "name");
151 }
152
157 public function setName($a_str)
158 {
159 $this->ini->setVariable("client", "name", $a_str);
160 }
161
166 public function getDescription()
167 {
168 return $this->ini->readVariable("client", "description");
169 }
170
175 public function setDescription($a_str)
176 {
177 $this->ini->setVariable("client", "description", $a_str);
178 }
179
183 /* function getMySQLVersion()
184 {
185 return mysql_get_server_info();
186 }*/
187
191 public function getDB()
192 {
193 return $this->db;
194 }
195
200 public function connect()
201 {
202 // check parameters
203 // To support oracle tnsnames.ora dbname is not required
204 if (!$this->getdbHost() || !$this->getdbUser()) {
205 $this->error = "empty_fields";
206
207 return false;
208 }
209
210 include_once("./Services/Database/classes/class.ilDBWrapperFactory.php");
211 $this->db = ilDBWrapperFactory::getWrapper(
212 $this->getdbType(),
213 $this->ini->readVariable("db", "inactive_mysqli")
214 );
215 $this->db->setDBUser($this->getdbUser());
216 $this->db->setDBPort($this->getdbPort());
217 $this->db->setDBPassword($this->getdbPass());
218 $this->db->setDBHost($this->getdbHost());
219 $this->db->setDBName($this->getdbName());
220 $con = $this->db->connect(true);
221
222 if (!$con) {
223 $this->error = "Database connection failed.";
224 return false;
225 }
226 $GLOBALS["ilDB"] = $this->db;
227
228 if ($GLOBALS["DIC"]->offsetExists("ilDB")) {
229 $GLOBALS["DIC"]->offsetUnset("ilDB");
230 }
231
232 $GLOBALS["DIC"]["ilDB"] = function ($c) {
233 return $GLOBALS["ilDB"];
234 };
235
236 $this->db_exists = true;
237 return true;
238 }
239
245 public function isInstalledDB(&$a_db)
246 {
247 if (method_exists($a_db, 'loadModule')) {
248 $a_db->loadModule('Manager');
249 }
250 if (!$tables = $a_db->listTables()) {
251 return false;
252 }
253
254 // check existence of some basic tables from ilias3 to determine if ilias3 is already installed in given database
255 if (in_array("object_data", $tables) and in_array("object_reference", $tables) and in_array("usr_data", $tables) and in_array("rbac_ua", $tables)) {
256 $this->db_installed = true;
257 return true;
258 }
259 $this->db_installed = false;
260 return false;
261 }
262
266 public function setDSN()
267 {
268 switch ($this->getDbType()) {
269 case "oracle":
270 //$this->dsn_host = "oci8://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost();
271 $this->dsn_host = array(
272 'phptype' => 'oci8',
273 'hostspec' => $this->getdbHost(),
274 'username' => $this->getdbUser(),
275 'port' => $this->getdbPort(),
276 'password' => $this->getdbPass(),
277 );
278 //$this->dsn = "oci8://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost()."/?service=".$this->getdbName();
279 $this->dsn = $this->dsn = array(
280 'phptype' => 'oci8',
281 'hostspec' => $this->getdbHost(),
282 'username' => $this->getdbUser(),
283 'port' => $this->getdbPort(),
284 'password' => $this->getdbPass(),
285 'service' => $this->getdbName()
286 );
287 break;
288
289 case "postgres":
290 $db_port_str = "";
291 if (trim($this->getdbPort()) != "") {
292 $db_port_str = ":" . $this->getdbPort();
293 }
294 $this->dsn_host = "pgsql://" . $this->getdbUser() . ":" . $this->getdbPass() . "@" . $this->getdbHost() . $db_port_str;
295 $this->dsn = "pgsql://" . $this->getdbUser() . ":" . $this->getdbPass() . "@" . $this->getdbHost() . $db_port_str . "/" . $this->getdbName();
296 break;
297
298 case "mysql":
299 case "innodb":
300 default:
301 $db_port_str = "";
302 if (trim($this->getdbPort()) != "") {
303 $db_port_str = ":" . $this->getdbPort();
304 }
305 $this->dsn_host = "mysql://" . $this->getdbUser() . ":" . $this->getdbPass() . "@" . $this->getdbHost() . $db_port_str;
306 $this->dsn = "mysql://" . $this->getdbUser() . ":" . $this->getdbPass() . "@" . $this->getdbHost() . $db_port_str . "/" . $this->getdbName();
307 break;
308 }
309 }
310
315 public function setDbHost($a_str)
316 {
317 $this->ini->setVariable("db", "host", $a_str);
318 }
319
325 public function getDbHost()
326 {
327 return $this->ini->readVariable("db", "host");
328 }
329
334 public function setDbName($a_str)
335 {
336 $this->ini->setVariable("db", "name", $a_str);
337 }
338
343 public function getDbName()
344 {
345 return $this->ini->readVariable("db", "name");
346 }
347
352 public function setDbUser($a_str)
353 {
354 $this->ini->setVariable("db", "user", $a_str);
355 }
356
361 public function getDbUser()
362 {
363 return $this->ini->readVariable("db", "user");
364 }
365
370 public function getDbPort()
371 {
372 return $this->ini->readVariable("db", "port");
373 }
374
379 public function setDbPort($a_str)
380 {
381 $this->ini->setVariable("db", "port", $a_str);
382 }
383
388 public function setDbPass($a_str)
389 {
390 $this->ini->setVariable("db", "pass", $a_str);
391 }
392
397 public function getDbPass()
398 {
399 return $this->ini->readVariable("db", "pass");
400 }
401
406 public function setDbSlaveActive($a_act)
407 {
408 $this->ini->setVariable("db", "slave_active", (int) $a_act);
409 }
410
416 public function getDbSlaveActive()
417 {
418 return (int) $this->ini->readVariable("db", "slave_active");
419 }
420
425 public function setDbSlaveHost($a_str)
426 {
427 $this->ini->setVariable("db", "slave_host", $a_str);
428 }
429
435 public function getDbSlaveHost()
436 {
437 return $this->ini->readVariable("db", "slave_host");
438 }
439
444 public function setDbSlaveName($a_str)
445 {
446 $this->ini->setVariable("db", "slave_name", $a_str);
447 }
448
453 public function getDbSlaveName()
454 {
455 return $this->ini->readVariable("db", "slave_name");
456 }
457
462 public function setDbSlaveUser($a_str)
463 {
464 $this->ini->setVariable("db", "slave_user", $a_str);
465 }
466
471 public function getDbSlaveUser()
472 {
473 return $this->ini->readVariable("db", "slave_user");
474 }
475
480 public function getDbSlavePort()
481 {
482 return $this->ini->readVariable("db", "slave_port");
483 }
484
489 public function setDbSlavePort($a_str)
490 {
491 $this->ini->setVariable("db", "slave_port", $a_str);
492 }
493
498 public function setDbSlavePass($a_str)
499 {
500 $this->ini->setVariable("db", "slave_pass", $a_str);
501 }
502
507 public function getDbSlavePass()
508 {
509 return $this->ini->readVariable("db", "slave_pass");
510 }
511
516 public function setDbType($a_str)
517 {
518 $this->ini->setVariable("db", "type", $a_str);
519 }
520
525 public function getDbType()
526 {
527 $val = $this->ini->readVariable("db", "type");
528 if ($val == "") {
529 return "mysql";
530 } else {
531 return $val;
532 }
533 }
534
539 public function getDataDir()
540 {
541 return ILIAS_DATA_DIR . "/" . $this->getId();
542 }
543
548 public function getWebspaceDir()
549 {
550 return ILIAS_ABSOLUTE_PATH . "/" . ILIAS_WEB_DIR . "/" . $this->getId();
551 }
552
553
558 public function checkDatabaseExists($a_keep_connection = false)
559 {
560 return $this->getDBSetup()->isConnectable();
561
562 //try to connect to database
563 $db = $this->db_connections->connectDB($this->dsn);
564 if (MDB2::isError($db)) {
565 return false;
566 }
567
568 if (!$this->isInstalledDB($db)) {
569 return false;
570 }
571
572 // #10633
573 if ($a_keep_connection) {
574 $GLOBALS["ilDB"] = $this->db;
575 $GLOBALS["DIC"]["ilDB"] = function ($c) {
576 return $GLOBALS["ilDB"];
577 };
578 }
579
580 return true;
581 }
582
583 public function reconnect()
584 {
585 $this->connect();
586 }
587
588
596 public function getSetting($a_keyword)
597 {
598 global $ilDB;
599 if (!$this->getDBSetup()->isDatabaseInstalled() || !$ilDB) {
600 return false;
601 }
602 include_once './Services/Administration/classes/class.ilSetting.php';
603 $set = new ilSetting("common", true);
604
605 return $set->get($a_keyword);
606 }
607
613 public function getAllSettings()
614 {
615 include_once './Services/Administration/classes/class.ilSetting.php';
616 $set = new ilSetting("common", true);
617 return $set->getAll();
618 }
619
627 public function setSetting($a_key, $a_val)
628 {
629 include_once './Services/Administration/classes/class.ilSetting.php';
630 $set = new ilSetting("common", true);
631 $set->set($a_key, $a_val);
632 }
633
638 public function getURLStringForNIC($a_nic_url)
639 {
640 $settings = $this->getAllSettings();
641
642 $inst_id = (empty($settings["inst_id"])) ? "0" : $settings["inst_id"];
643
644 // send host information to ilias-nic
645 //#18132: removed ipadr, server_port, server_software, institution, contact_title, contact_position,
646 // contact_institution, contact_street, contact_pcode, contact_city, contact_country, contact_phone
647 $url = $a_nic_url .
648 "?cmd=getid" .
649 "&inst_id=" . rawurlencode($inst_id) .
650 "&hostname=" . rawurlencode($_SERVER["SERVER_NAME"]) .
651 "&inst_name=" . rawurlencode($this->ini->readVariable("client", "name")) .
652 "&inst_info=" . rawurlencode($this->ini->readVariable("client", "description")) .
653 "&http_path=" . rawurlencode(ILIAS_HTTP_PATH) .
654 "&contact_firstname=" . rawurlencode($settings["admin_firstname"]) .
655 "&contact_lastname=" . rawurlencode($settings["admin_lastname"]) .
656 "&contact_email=" . rawurlencode($settings["admin_email"]) .
657 "&nic_key=" . rawurlencode($this->getNICkey());
658
659 return $url;
660 }
661
676 public function updateNIC($a_nic_url)
677 {
678 $max_redirects = 5;
679 $socket_timeout = 5;
680
681 require_once(__DIR__ . "/../../Services/WebServices/Curl/classes/class.ilCurlConnection.php");
683 $this->setError("CURL-extension not loaded.");
684 return false;
685 }
686
687 $url = $this->getURLStringForNIC($a_nic_url);
689 $req->init();
690
691 $settings = $this->getAllSettings();
692 if ((bool) $settings['proxy_status'] && strlen($settings['proxy_host']) && strlen($settings['proxy_port'])) {
693 $req->setOpt(CURLOPT_HTTPPROXYTUNNEL, true);
694 $req->setOpt(CURLOPT_PROXY, $settings["proxy_host"]);
695 $req->setOpt(CURLOPT_PROXYPORT, $settings["proxy_port"]);
696 }
697
698 $req->setOpt(CURLOPT_HEADER, 1);
699 $req->setOpt(CURLOPT_RETURNTRANSFER, 1);
700 $req->setOpt(CURLOPT_CONNECTTIMEOUT, $socket_timeout);
701 $req->setOpt(CURLOPT_MAXREDIRS, $max_redirects);
702 $response = $req->exec();
703
704 $req->parseResponse($response);
705 $response_body = $req->getResponseBody();
706
707 $info = $req->getInfo();
708 if ($info["http_code"] != "200") {
709 $this->setError("Could not connect to NIC-Server at '" . $url . "'");
710 return false;
711 }
712
713 $this->nic_status = explode("\n", $response_body);
714
716
717 return true;
718 }
719
728 public function setNICkey()
729 {
730 mt_srand((double) microtime()*1000000);
731 $nic_key = md5(str_replace(".", "", $_SERVER["SERVER_ADDR"]) +
732 mt_rand(100000, 999999));
733
734 $this->setSetting("nic_key", $nic_key);
735
736 $this->nic_key = $nic_key;
737
738 return true;
739 }
740
746 public function getNICkey()
747 {
748 $this->nic_key = $this->getSetting("nic_key");
749
750 if (empty($this->nic_key)) {
751 $this->setNICkey();
752 }
753
754 return $this->nic_key;
755 }
756
757 public function getDefaultLanguage()
758 {
759 return $this->getSetting("language");
760 }
761
762 public function setDefaultLanguage($a_lang_key)
763 {
764 $this->setSetting("language", $a_lang_key);
765 $this->ini->setVariable("language", "default", $a_lang_key);
766 $this->ini->write();
767
768 return true;
769 }
770
771
777 public function getError()
778 {
780 $this->error = "";
781
782 return $error;
783 }
784
785
789 public function setError($error_message)
790 {
791 $this->error = $error_message;
792 }
793
802 public function delete($a_ini = true, $a_db = false, $a_files = false)
803 {
804 if ($a_ini === true and file_exists(ILIAS_ABSOLUTE_PATH . "/" . ILIAS_WEB_DIR . "/" . $this->getId() . "/client.ini.php")) {
805 unlink(CLIENT_WEB_DIR . "/client.ini.php");
806 $msg[] = "ini_deleted";
807 }
808
809 if ($a_db === true and $this->db_exists) {
810 $this->db->query("DROP DATABASE " . $this->getDbName());
811 $msg[] = "db_deleted";
812 }
813
814 if ($a_files === true and file_exists(CLIENT_WEB_DIR) and is_dir(CLIENT_WEB_DIR)) {
815 // rmdir();
816 ilUtil::delDir(CLIENT_WEB_DIR);
817 ilUtil::delDir(CLIENT_DATA_DIR);
818 $msg[] = "files_deleted";
819 }
820
821 return $msg;
822 }
823
828 public function create()
829 {
830 //var_dump($this->getDataDir());exit;
831 // create base data dir
832 if (!ilUtil::makeDir($this->getDataDir())) {
833 $this->error = "could_not_create_base_data_dir :" . $this->getDataDir();
834 return false;
835 }
836
837 // create sub dirs in base data dir
838 if (!ilUtil::makeDir($this->getDataDir() . "/mail")) {
839 $this->error = "could_not_create_mail_data_dir :" . $this->getDataDir() . "/mail";
840 return false;
841 }
842
843 if (!ilUtil::makeDir($this->getDataDir() . "/lm_data")) {
844 $this->error = "could_not_create_lm_data_dir :" . $this->getDataDir() . "/lm_data";
845 return false;
846 }
847
848 if (!ilUtil::makeDir($this->getDataDir() . "/forum")) {
849 $this->error = "could_not_create_forum_data_dir :" . $this->getDataDir() . "/forum";
850 return false;
851 }
852
853 if (!ilUtil::makeDir($this->getDataDir() . "/files")) {
854 $this->error = "could_not_create_files_data_dir :" . $this->getDataDir() . "/files";
855 return false;
856 }
857
858 // create base webspace dir
859 if (!ilUtil::makeDir($this->getWebspaceDir())) {
860 $this->error = "could_not_create_base_webspace_dir :" . $this->getWebspaceDir();
861 return false;
862 }
863
864 // create sub dirs in base webspace dir
865 if (!ilUtil::makeDir($this->getWebspaceDir() . "/lm_data")) {
866 $this->error = "could_not_create_lm_webspace_dir :" . $this->getWebspaceDir() . "/lm_data";
867 return false;
868 }
869
870 if (!ilUtil::makeDir($this->getWebspaceDir() . "/usr_images")) {
871 $this->error = "could_not_create_usr_images_webspace_dir :" . $this->getWebspaceDir() . "/usr_images";
872 return false;
873 }
874
875 if (!ilUtil::makeDir($this->getWebspaceDir() . "/mobs")) {
876 $this->error = "could_not_create_mobs_webspace_dir :" . $this->getWebspaceDir() . "/mobs";
877 return false;
878 }
879
880 if (!ilUtil::makeDir($this->getWebspaceDir() . "/css")) {
881 $this->error = "could_not_create_css_webspace_dir :" . $this->getWebspaceDir() . "/css";
882 return false;
883 }
884
885 // write client ini
886 if (!$this->ini->write()) {
887 $this->error = get_class($this) . ": " . $this->ini->getError();
888 return false;
889 }
890
891 return true;
892 }
893
900 public function writeIni()
901 {
902 $this->ini->write();
903 }
904} // END class.ilClient
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 ...
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
$req
Definition: getUserInfo.php:20
$info
Definition: index.php:5
$url
$response
global $ilDB
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']