ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
12 class ilClient
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 
40  protected $db_setup = null;
41 
42 
48  public function __construct($a_client_id)
49  {
50  if ($a_client_id) {
51  $this->id = $a_client_id;
52  $this->ini_file_path = ILIAS_ABSOLUTE_PATH . "/" . ILIAS_WEB_DIR . "/" . $this->getId() . "/client.ini.php";
53  }
54 
55  // set path default.ini
56  $this->client_defaults = ILIAS_ABSOLUTE_PATH . "/setup/client.master.ini.php";
57  }
58 
59 
64  public function getDBSetup($cached = true)
65  {
66  require_once('./setup/classes/class.ilDbSetup.php');
67 
68  if ($cached) {
69  if (is_null($this->db_setup)) {
70  $this->db_setup = \ilDbSetup::getNewInstanceForClient($this);
71  }
72  return $this->db_setup;
73  }
74 
75 
76  return \ilDbSetup::getNewInstanceForClient($this);
77  }
78 
84  public function init()
85  {
86  $this->ini = new ilIniFile($this->ini_file_path);
87 
88  // load defaults only if no client.ini was found
89  if (!@file_exists($this->ini_file_path)) {
90  //echo "<br>A-".$this->ini_file_path."-";
91  $this->ini->GROUPS = parse_ini_file($this->client_defaults, true);
92 
93  return false;
94  }
95 
96  // read client.ini
97  if (!$this->ini->read()) {
98  $this->error = get_class($this) . ": " . $this->ini->getError();
99 
100  return false;
101  }
102 
103  // only for ilias main
104  define("CLIENT_WEB_DIR", ILIAS_ABSOLUTE_PATH . "/" . ILIAS_WEB_DIR . "/" . $this->getId());
105  define("CLIENT_DATA_DIR", ILIAS_DATA_DIR . "/" . $this->getId());
106  define("DEVMODE", $this->ini->readVariable('system', 'DEVMODE'));
107  define("ROOT_FOLDER_ID", $this->ini->readVariable('system', 'ROOT_FOLDER_ID'));
108  define("SYSTEM_FOLDER_ID", $this->ini->readVariable('system', 'SYSTEM_FOLDER_ID'));
109  define("ROLE_FOLDER_ID", $this->ini->readVariable('system', 'ROLE_FOLDER_ID'));
110  define("ANONYMOUS_USER_ID", 13);
111  define("ANONYMOUS_ROLE_ID", 14);
112  define("SYSTEM_USER_ID", 6);
113  define("SYSTEM_ROLE_ID", 2);
114 
115  $this->db_exists = $this->getDBSetup()->isConnectable();
116  $this->getDBSetup()->provideGlobalDB();
117  if ($this->db_exists) {
118  $this->db_installed = $this->getDBSetup()->isDatabaseInstalled();
119  }
120 
121  return true;
122  }
123 
124 
125  public function provideGlobalDB()
126  {
127  $this->getDBSetup()->provideGlobalDB();
128  }
129 
130 
131  public function revokeGlobalDB()
132  {
133  $this->getDBSetup()->provideGlobalDB();
134  }
135 
140  public function getId()
141  {
142  return $this->id;
143  }
144 
149  public function setId($a_client_id)
150  {
151  $this->id = $a_client_id;
152  $this->webspace_dir = ILIAS_ABSOLUTE_PATH . "/" . ILIAS_WEB_DIR . "/" . $this->id;
153  }
154 
159  public function getName()
160  {
161  return $this->ini->readVariable("client", "name");
162  }
163 
168  public function setName($a_str)
169  {
170  $this->ini->setVariable("client", "name", $a_str);
171  }
172 
177  public function getDescription()
178  {
179  return $this->ini->readVariable("client", "description");
180  }
181 
186  public function setDescription($a_str)
187  {
188  $this->ini->setVariable("client", "description", $a_str);
189  }
190 
194  /* function getMySQLVersion()
195  {
196  return mysql_get_server_info();
197  }*/
198 
202  public function getDB()
203  {
204  return $this->db;
205  }
206 
211  public function connect()
212  {
213  // check parameters
214  // To support oracle tnsnames.ora dbname is not required
215  if (!$this->getdbHost() || !$this->getdbUser()) {
216  $this->error = "empty_fields";
217 
218  return false;
219  }
220 
221  $this->db = ilDBWrapperFactory::getWrapper($this->getdbType());
222  $this->db->setDBUser($this->getdbUser());
223  $this->db->setDBPort($this->getdbPort());
224  $this->db->setDBPassword($this->getdbPass());
225  $this->db->setDBHost($this->getdbHost());
226  $this->db->setDBName($this->getdbName());
227  $con = $this->db->connect(true);
228 
229  if (!$con) {
230  $this->error = "Database connection failed.";
231  return false;
232  }
233  $GLOBALS["ilDB"] = $this->db;
234 
235  if ($GLOBALS["DIC"]->offsetExists("ilDB")) {
236  $GLOBALS["DIC"]->offsetUnset("ilDB");
237  }
238 
239  $GLOBALS["DIC"]["ilDB"] = function ($c) {
240  return $GLOBALS["ilDB"];
241  };
242 
243  $this->db_exists = true;
244  return true;
245  }
246 
252  public function isInstalledDB(&$a_db)
253  {
254  if (method_exists($a_db, 'loadModule')) {
255  $a_db->loadModule('Manager');
256  }
257  if (!$tables = $a_db->listTables()) {
258  return false;
259  }
260 
261  // check existence of some basic tables from ilias3 to determine if ilias3 is already installed in given database
262  if (in_array("object_data", $tables) and in_array("object_reference", $tables) and in_array("usr_data", $tables) and in_array("rbac_ua", $tables)) {
263  $this->db_installed = true;
264  return true;
265  }
266  $this->db_installed = false;
267  return false;
268  }
269 
273  public function setDSN()
274  {
275  switch ($this->getDbType()) {
276  case "postgres":
277  $db_port_str = "";
278  if (trim($this->getdbPort()) != "") {
279  $db_port_str = ":" . $this->getdbPort();
280  }
281  $this->dsn_host = "pgsql://" . $this->getdbUser() . ":" . $this->getdbPass() . "@" . $this->getdbHost() . $db_port_str;
282  $this->dsn = "pgsql://" . $this->getdbUser() . ":" . $this->getdbPass() . "@" . $this->getdbHost() . $db_port_str . "/" . $this->getdbName();
283  break;
284 
285  case "mysql":
286  case "innodb":
287  default:
288  $db_port_str = "";
289  if (trim($this->getdbPort()) != "") {
290  $db_port_str = ":" . $this->getdbPort();
291  }
292  $this->dsn_host = "mysql://" . $this->getdbUser() . ":" . $this->getdbPass() . "@" . $this->getdbHost() . $db_port_str;
293  $this->dsn = "mysql://" . $this->getdbUser() . ":" . $this->getdbPass() . "@" . $this->getdbHost() . $db_port_str . "/" . $this->getdbName();
294  break;
295  }
296  }
297 
302  public function setDbHost($a_str)
303  {
304  $this->ini->setVariable("db", "host", $a_str);
305  }
306 
312  public function getDbHost()
313  {
314  return $this->ini->readVariable("db", "host");
315  }
316 
321  public function setDbName($a_str)
322  {
323  $this->ini->setVariable("db", "name", $a_str);
324  }
325 
330  public function getDbName()
331  {
332  return $this->ini->readVariable("db", "name");
333  }
334 
339  public function setDbUser($a_str)
340  {
341  $this->ini->setVariable("db", "user", $a_str);
342  }
343 
348  public function getDbUser()
349  {
350  return $this->ini->readVariable("db", "user");
351  }
352 
357  public function getDbPort()
358  {
359  return $this->ini->readVariable("db", "port");
360  }
361 
366  public function setDbPort($a_str)
367  {
368  $this->ini->setVariable("db", "port", $a_str);
369  }
370 
375  public function setDbPass($a_str)
376  {
377  $this->ini->setVariable("db", "pass", $a_str);
378  }
379 
384  public function getDbPass()
385  {
386  return $this->ini->readVariable("db", "pass");
387  }
388 
393  public function setDbSlaveActive($a_act)
394  {
395  $this->ini->setVariable("db", "slave_active", (int) $a_act);
396  }
397 
403  public function getDbSlaveActive()
404  {
405  return (int) $this->ini->readVariable("db", "slave_active");
406  }
407 
412  public function setDbSlaveHost($a_str)
413  {
414  $this->ini->setVariable("db", "slave_host", $a_str);
415  }
416 
422  public function getDbSlaveHost()
423  {
424  return $this->ini->readVariable("db", "slave_host");
425  }
426 
431  public function setDbSlaveName($a_str)
432  {
433  $this->ini->setVariable("db", "slave_name", $a_str);
434  }
435 
440  public function getDbSlaveName()
441  {
442  return $this->ini->readVariable("db", "slave_name");
443  }
444 
449  public function setDbSlaveUser($a_str)
450  {
451  $this->ini->setVariable("db", "slave_user", $a_str);
452  }
453 
458  public function getDbSlaveUser()
459  {
460  return $this->ini->readVariable("db", "slave_user");
461  }
462 
467  public function getDbSlavePort()
468  {
469  return $this->ini->readVariable("db", "slave_port");
470  }
471 
476  public function setDbSlavePort($a_str)
477  {
478  $this->ini->setVariable("db", "slave_port", $a_str);
479  }
480 
485  public function setDbSlavePass($a_str)
486  {
487  $this->ini->setVariable("db", "slave_pass", $a_str);
488  }
489 
494  public function getDbSlavePass()
495  {
496  return $this->ini->readVariable("db", "slave_pass");
497  }
498 
503  public function setDbType($a_str)
504  {
505  $this->ini->setVariable("db", "type", $a_str);
506  }
507 
512  public function getDbType()
513  {
514  $val = $this->ini->readVariable("db", "type");
515  if ($val == "") {
516  return "mysql";
517  } else {
518  return $val;
519  }
520  }
521 
526  public function getDataDir()
527  {
528  return ILIAS_DATA_DIR . "/" . $this->getId();
529  }
530 
535  public function getWebspaceDir()
536  {
537  return ILIAS_ABSOLUTE_PATH . "/" . ILIAS_WEB_DIR . "/" . $this->getId();
538  }
539 
540 
545  public function checkDatabaseExists($a_keep_connection = false)
546  {
547  return $this->getDBSetup()->isConnectable();
548  }
549 
550  public function reconnect()
551  {
552  $this->connect();
553  }
554 
555 
563  public function getSetting($a_keyword)
564  {
565  global $ilDB;
566  if (!$this->getDBSetup()->isDatabaseInstalled() || !$ilDB) {
567  return false;
568  }
569  include_once './Services/Administration/classes/class.ilSetting.php';
570  $set = new ilSetting("common", true);
571 
572  return $set->get($a_keyword);
573  }
574 
580  public function getAllSettings()
581  {
582  include_once './Services/Administration/classes/class.ilSetting.php';
583  $set = new ilSetting("common", true);
584  return $set->getAll();
585  }
586 
594  public function setSetting($a_key, $a_val)
595  {
596  include_once './Services/Administration/classes/class.ilSetting.php';
597  $set = new ilSetting("common", true);
598  $set->set($a_key, $a_val);
599  }
600 
605  public function getURLStringForNIC($a_nic_url)
606  {
607  $settings = $this->getAllSettings();
608 
609  $inst_id = (empty($settings["inst_id"])) ? "0" : $settings["inst_id"];
610 
611  // send host information to ilias-nic
612  //#18132: removed ipadr, server_port, server_software, institution, contact_title, contact_position,
613  // contact_institution, contact_street, contact_pcode, contact_city, contact_country, contact_phone
614  $url = $a_nic_url .
615  "?cmd=getid" .
616  "&inst_id=" . rawurlencode($inst_id) .
617  "&hostname=" . rawurlencode($_SERVER["SERVER_NAME"]) .
618  "&inst_name=" . rawurlencode($this->ini->readVariable("client", "name")) .
619  "&inst_info=" . rawurlencode($this->ini->readVariable("client", "description")) .
620  "&http_path=" . rawurlencode(ILIAS_HTTP_PATH) .
621  "&contact_firstname=" . rawurlencode($settings["admin_firstname"]) .
622  "&contact_lastname=" . rawurlencode($settings["admin_lastname"]) .
623  "&contact_email=" . rawurlencode($settings["admin_email"]) .
624  "&nic_key=" . rawurlencode($this->getNICkey());
625 
626  return $url;
627  }
628 
643  public function updateNIC($a_nic_url)
644  {
645  $max_redirects = 5;
646  $socket_timeout = 5;
647 
648  require_once(__DIR__ . "/../../Services/WebServices/Curl/classes/class.ilCurlConnection.php");
650  $this->setError("CURL-extension not loaded.");
651  return false;
652  }
653 
654  if (!isset($GLOBALS["ilSetting"])) {
655  $GLOBALS["ilSetting"] = new ilSetting();
656  }
657 
658  $url = $this->getURLStringForNIC($a_nic_url);
659  $req = new ilCurlConnection($url);
660  $req->init();
661 
662  $req->setOpt(CURLOPT_HEADER, 1);
663  $req->setOpt(CURLOPT_RETURNTRANSFER, 1);
664  $req->setOpt(CURLOPT_CONNECTTIMEOUT, $socket_timeout);
665  $req->setOpt(CURLOPT_MAXREDIRS, $max_redirects);
666  $response = $req->exec();
667 
668  $req->parseResponse($response);
669  $response_body = $req->getResponseBody();
670 
671  $info = $req->getInfo();
672  if ($info["http_code"] != "200") {
673  $this->setError("Could not connect to NIC-Server at '" . $url . "'");
674  return false;
675  }
676 
677  $this->nic_status = explode("\n", $response_body);
678 
679  ilLoggerFactory::getLogger('setup')->dump($this->nic_status);
680 
681  return true;
682  }
683 
692  public function setNICkey()
693  {
694  mt_srand((double) microtime() * 1000000);
695  $nic_key = md5(str_replace(".", "", $_SERVER["SERVER_ADDR"]) +
696  mt_rand(100000, 999999));
697 
698  $this->setSetting("nic_key", $nic_key);
699 
700  $this->nic_key = $nic_key;
701 
702  return true;
703  }
704 
710  public function getNICkey()
711  {
712  $this->nic_key = $this->getSetting("nic_key");
713 
714  if (empty($this->nic_key)) {
715  $this->setNICkey();
716  }
717 
718  return $this->nic_key;
719  }
720 
721  public function getDefaultLanguage()
722  {
723  return $this->getSetting("language");
724  }
725 
726  public function setDefaultLanguage($a_lang_key)
727  {
728  $this->setSetting("language", $a_lang_key);
729  $this->ini->setVariable("language", "default", $a_lang_key);
730  $this->ini->write();
731 
732  return true;
733  }
734 
735 
741  public function getError()
742  {
744  $this->error = "";
745 
746  return $error;
747  }
748 
749 
753  public function setError($error_message)
754  {
755  $this->error = $error_message;
756  }
757 
766  public function delete($a_ini = true, $a_db = false, $a_files = false)
767  {
768  if ($a_ini === true and file_exists(ILIAS_ABSOLUTE_PATH . "/" . ILIAS_WEB_DIR . "/" . $this->getId() . "/client.ini.php")) {
769  unlink(CLIENT_WEB_DIR . "/client.ini.php");
770  $msg[] = "ini_deleted";
771  }
772 
773  if ($a_db === true and $this->db_exists) {
774  $this->db->query("DROP DATABASE " . $this->getDbName());
775  $msg[] = "db_deleted";
776  }
777 
778  if ($a_files === true and file_exists(CLIENT_WEB_DIR) and is_dir(CLIENT_WEB_DIR)) {
779  // rmdir();
780  ilUtil::delDir(CLIENT_WEB_DIR);
781  ilUtil::delDir(CLIENT_DATA_DIR);
782  $msg[] = "files_deleted";
783  }
784 
785  return $msg;
786  }
787 
792  public function create()
793  {
794  //var_dump($this->getDataDir());exit;
795  // create base data dir
796  if (!ilUtil::makeDir($this->getDataDir())) {
797  $this->error = "could_not_create_base_data_dir :" . $this->getDataDir();
798  return false;
799  }
800 
801  // create sub dirs in base data dir
802  if (!ilUtil::makeDir($this->getDataDir() . "/mail")) {
803  $this->error = "could_not_create_mail_data_dir :" . $this->getDataDir() . "/mail";
804  return false;
805  }
806 
807  if (!ilUtil::makeDir($this->getDataDir() . "/lm_data")) {
808  $this->error = "could_not_create_lm_data_dir :" . $this->getDataDir() . "/lm_data";
809  return false;
810  }
811 
812  if (!ilUtil::makeDir($this->getDataDir() . "/forum")) {
813  $this->error = "could_not_create_forum_data_dir :" . $this->getDataDir() . "/forum";
814  return false;
815  }
816 
817  if (!ilUtil::makeDir($this->getDataDir() . "/files")) {
818  $this->error = "could_not_create_files_data_dir :" . $this->getDataDir() . "/files";
819  return false;
820  }
821 
822  // create base webspace dir
823  if (!ilUtil::makeDir($this->getWebspaceDir())) {
824  $this->error = "could_not_create_base_webspace_dir :" . $this->getWebspaceDir();
825  return false;
826  }
827 
828  // create sub dirs in base webspace dir
829  if (!ilUtil::makeDir($this->getWebspaceDir() . "/lm_data")) {
830  $this->error = "could_not_create_lm_webspace_dir :" . $this->getWebspaceDir() . "/lm_data";
831  return false;
832  }
833 
834  if (!ilUtil::makeDir($this->getWebspaceDir() . "/usr_images")) {
835  $this->error = "could_not_create_usr_images_webspace_dir :" . $this->getWebspaceDir() . "/usr_images";
836  return false;
837  }
838 
839  if (!ilUtil::makeDir($this->getWebspaceDir() . "/mobs")) {
840  $this->error = "could_not_create_mobs_webspace_dir :" . $this->getWebspaceDir() . "/mobs";
841  return false;
842  }
843 
844  if (!ilUtil::makeDir($this->getWebspaceDir() . "/css")) {
845  $this->error = "could_not_create_css_webspace_dir :" . $this->getWebspaceDir() . "/css";
846  return false;
847  }
848 
849  // write client ini
850  if (!$this->ini->write()) {
851  $this->error = get_class($this) . ": " . $this->ini->getError();
852  return false;
853  }
854 
855  return true;
856  }
857 
864  public function writeIni()
865  {
866  $this->ini->write();
867  }
868 } // END class.ilClient
setDSN()
set the dsn and dsn_host
__construct($a_client_id)
ilClient constructor.
getDbSlaveName()
get name of slave database
setDescription($a_str)
set client description
isInstalledDB(&$a_db)
check if client db is installed
setDbHost($a_str)
set the host
setSetting($a_key, $a_val)
write one value to settings table public
getDbPass()
get db password
setName($a_str)
set client name
getDbName()
get name of database
setDbType($a_str)
set the type of database
getDbSlaveHost()
get db slave host
connect()
connect to client database
setError($error_message)
getDbUser()
get db user
getError()
get error message and clear error var
writeIni()
write init
getDB()
get mysql version
setDefaultLanguage($a_lang_key)
getDbHost()
get db host
setId($a_client_id)
set client id
getNICkey()
get nic_key public
setDbUser($a_str)
set db user
getDescription()
get client description
setDbName($a_str)
set the name of database
setDbPass($a_str)
set db password
setNICkey()
set nic_key generate nic_key if nic_key field in cust table is empty.
checkDatabaseExists($a_keep_connection=false)
check database connection with database name
getURLStringForNIC($a_nic_url)
static getNewInstanceForClient(\ilClient $client)
getDbSlavePort()
get slave db port
init()
init client load client.ini and set some constants
setDbSlavePass($a_str)
set slave db password
setDbSlaveUser($a_str)
set slave db user
setDbSlaveName($a_str)
set the name of slave database
getName()
get client name
static _isCurlExtensionLoaded()
Check if curl extension is loaded.
$_SERVER['HTTP_HOST']
Definition: raiseError.php:10
getDbPort()
get db port
setDbSlaveActive($a_act)
set the slave active
getDbSlaveActive()
get slave active
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
getId()
get client id
getDbSlaveUser()
get slave db user
getAllSettings()
read all values from settings table public
getDbType()
get type of database
static makeDir($a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
setDbSlaveHost($a_str)
set the slave host
getDataDir()
get client datadir path
getWebspaceDir()
get client webspacedir path
Client Management.
setDbPort($a_str)
set db port
global $ilDB
getSetting($a_keyword)
read one value from settings table
static getLogger($a_component_id)
Get component logger.
$url
$response
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
create()
create a new client and its subdirectories
INIFile Parser.
getDBSetup($cached=true)
getDbSlavePass()
get slave db password
updateNIC($a_nic_url)
Connect to ILIAS-NIC.
setDbSlavePort($a_str)
set slave db port