ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilClient.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2005 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
32 class ilClient
33 {
34  var $id; // client_id (md5 hash)
35  var $dir; // directory name in ilias/clients/
36  var $name; // installation name
37  var $db_exists = false; // db exists?
38  var $db_installed = false; // db installed?
39 
40  var $client_defaults; // default settings
41  var $status; // contains status infos about setup process (todo: move function to this class)
42  var $setup_ok = false; // if client setup was finished at least once, this is set to true
43  var $nic_status; // contains received data of ILIAS-NIC server when registering
44 
49  function ilClient($a_client_id, $a_db_connections)
50  {
51  if ($a_client_id)
52  {
53  $this->id = $a_client_id;
54  $this->ini_file_path = ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId()."/client.ini.php";
55  }
56 
57  $this->db_connections = $a_db_connections;
58 
59  // set path default.ini
60  $this->client_defaults = ILIAS_ABSOLUTE_PATH."/setup/client.master.ini.php";
61  }
62 
68  function init()
69  {
70  $this->ini = new ilIniFile($this->ini_file_path);
71 
72  // load defaults only if no client.ini was found
73  if (!@file_exists($this->ini_file_path))
74  {
75  $this->ini->GROUPS = parse_ini_file($this->client_defaults,true);
76  return false;
77  }
78 
79  // read client.ini
80  if (!$this->ini->read())
81  {
82  $this->error = get_class($this).": ".$this->ini->getError();
83  return false;
84  }
85 
86  // only for ilias main
87  define("CLIENT_WEB_DIR",ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId());
88  define("CLIENT_DATA_DIR",ILIAS_DATA_DIR."/".$this->getId());
89  define ("DEVMODE",$this->ini->readVariable('system','DEVMODE'));
90  define ("ROOT_FOLDER_ID",$this->ini->readVariable('system','ROOT_FOLDER_ID'));
91  define ("SYSTEM_FOLDER_ID",$this->ini->readVariable('system','SYSTEM_FOLDER_ID'));
92  define ("ROLE_FOLDER_ID",$this->ini->readVariable('system','ROLE_FOLDER_ID'));
93  define ("ANONYMOUS_USER_ID",13);
94  define ("ANONYMOUS_ROLE_ID",14);
95  define ("SYSTEM_USER_ID",6);
96  define ("SYSTEM_ROLE_ID",2);
97 
98  $this->db_exists = $this->connect();
99 
100  if ($this->db_exists)
101  {
102  $this->db_installed = $this->isInstalledDB($this->db);
103  }
104 
105  return true;
106  }
107 
112  function getId()
113  {
114  return $this->id;
115  }
116 
121  function setId($a_client_id)
122  {
123  $this->id = $a_client_id;
124  $this->webspace_dir = ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->id;
125  }
126 
131  function getName()
132  {
133  return $this->ini->readVariable("client","name");
134  }
135 
140  function setName($a_str)
141  {
142  $this->ini->setVariable("client","name",$a_str);
143  }
144 
149  function getDescription()
150  {
151  return $this->ini->readVariable("client","description");
152  }
153 
158  function setDescription($a_str)
159  {
160  $this->ini->setVariable("client","description",$a_str);
161  }
162 
166  function getMySQLVersion()
167  {
168  return mysql_get_server_info();
169  }
170 
180  {
181  $version = explode(".", $this->getMysqlVersion());
182  if ((int)$version[0] >= 5 ||
183  ((int)$version[0] == 4 && (int)$version[1] >= 1))
184  {
185  return true;
186  }
187 
188  return false;
189  }
190 
195  function connect()
196  {
197  // check parameters
198  if (!$this->getdbHost() || !$this->getdbName() || !$this->getdbUser())
199  {
200  $this->error = "empty_fields";
201  return false;
202  }
203 
204  $this->setDSN();
205 
206 //echo "A";
207 /* if (is_object($this->db))
208  {
209  if (!MDB2::isError($this->db))
210  {
211  $this->db->disconnect();
212  }
213  else
214  {
215  echo "+".$this->db->getMessage()."+";
216  }
217 //echo "A";
218  }
219 */
220 //if (!is_object($this->db))
221 //{
222  $this->db = $this->db_connections->connectDB($this->dsn);
223 //}
224 
225  if (MDB2::isError($this->db))
226  {
227  $this->error = $this->db->getMessage()."! not_connected_to_db";
228  return false;
229  }
230 
231  // NOTE: Three sourcecodes use this or a similar handling:
232  // - classes/class.ilDBx.php
233  // - calendar/classes/class.ilCalInterface.php->setNames
234  // - setup/classes/class.ilClient.php
235  if ($this->isMysql4_1OrHigher())
236  {
237  $this->db->query("SET NAMES utf8");
238  $this->db->query("SET SESSION SQL_MODE = ''");
239  }
240 
241  $this->db_exists = true;
242  return true;
243  }
244 
250  function isInstalledDB(&$a_db)
251  {
252  $q = "SHOW TABLES";
253  $r = $a_db->query($q);
254 
255  $tables = array();
256 
257  while ($row = $r->fetchRow(DB_FETCHMODE_ASSOC))
258  {
259  $tables[] = implode($row);
260  }
261 
262  // check existence of some basic tables from ilias3 to determine if ilias3 is already installed in given database
263  if (in_array("object_data",$tables) and in_array("object_reference",$tables) and in_array("usr_data",$tables) and in_array("rbac_ua",$tables))
264  {
265  $this->db_installed = true;
266  return true;
267  }
268 
269  $this->db_installed = false;
270  return false;
271  }
272 
276  function setDSN()
277  {
278 
279  $this->dsn_host = "mysql://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost();
280  $this->dsn = "mysql://".$this->getdbUser().":".$this->getdbPass()."@".$this->getdbHost()."/".$this->getdbName();
281  }
282 
287  function setDbHost($a_str)
288  {
289  $this->ini->setVariable("db","host",$a_str);
290  }
291 
297  function getDbHost()
298  {
299  return $this->ini->readVariable("db","host");
300  }
301 
306  function setDbName($a_str)
307  {
308  $this->ini->setVariable("db","name",$a_str);
309  }
310 
315  function getDbName()
316  {
317  return $this->ini->readVariable("db","name");
318  }
319 
324  function setDbUser($a_str)
325  {
326  $this->ini->setVariable("db","user",$a_str);
327  }
328 
333  function getDbUser()
334  {
335  return $this->ini->readVariable("db","user");
336  }
337 
342  function setDbPass($a_str)
343  {
344  $this->ini->setVariable("db","pass",$a_str);
345  }
346 
351  function getDbPass()
352  {
353  return $this->ini->readVariable("db","pass");
354  }
355 
360  function getDataDir()
361  {
362  return ILIAS_DATA_DIR."/".$this->getId();
363  }
364 
369  function getWebspaceDir()
370  {
371  return ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId();
372  }
373 
378  function checkDatabaseHost()
379  {
380  global $lng;
381 
382  //connect to databasehost
383  $db = $this->db_connections->connectHost($this->dsn_host);
384 
385  if (MDB2::isError($db))
386  {
387  //$this->error = $db->getMessage()."! Please check database hostname, username & password.";
388  $this->error = $db->getMessage()." - ".$lng->txt("db_error_please_check");
389  return false;
390  }
391 
392  return true;
393  }
394 
400  {
401  //try to connect to database
402  $db = $this->db_connections->connectDB($this->dsn);
403 
404  if (MDB2::isError($db))
405  {
406  return false;
407  }
408 
409  if (!$this->isInstalledDB($db))
410  {
411  return false;
412  }
413 
414  return true;
415  }
416 
417  function reconnect()
418  {
419  $this->db = $this->db_connections->connectDB($this->dsn);
420  }
421 
428  function getSetting($a_keyword)
429  {
430  $q = "SELECT value FROM settings WHERE keyword='".$a_keyword."'";
431  $r = $this->db->query($q);
432 
433 /*if ($r->db->database_name == "ilias3blabla")
434 {
435  var_dump($r);
436 }*/
437 
438  if ($r->numRows() > 0)
439  {
440  $row = $r->fetchRow();
441 /*if ($r->db->database_name == "ilias3blabla")
442 {//
443  var_dump($row);
444 }*/
445  return $row[0];
446  }
447  else
448  {
449 //echo "-nosetting-";
450  return false;
451  }
452  }
453 
459  function getAllSettings()
460  {
461  $q = "SELECT * FROM settings";
462  $r = $this->db->query($q);
463 
464  while ($row = $r->fetchRow(DB_FETCHMODE_ASSOC))
465  {
466  $arr[$row["keyword"]] = $row["value"];
467  }
468 
469  return $arr;
470  }
471 
479  function setSetting($a_key, $a_val)
480  {
481  //$q = "REPLACE INTO settings SET keyword = '".$a_key."', value = '".$a_val."'";
482  //$r = $this->db->query($q);
483 
484 //echo "<br><b>SETTING:$a_key-$a_val-</b>";
485 
486  $q = "REPLACE INTO settings (keyword,value) VALUES ('".$a_key."', '".$a_val."')";
487  $r = $this->db->query($q);
488 
489  $set = $this->db->query("SELECT * FROM settings WHERE keyword = '".$a_key."'");
490  $rec = $set->fetchRow(DB_FETCHMODE_ASSOC);
491 
492 if ($a_key == "setup_ok")
493 {
494 // nj();
495 }
496 
497 /*if ($set->db->database_name == "ilias3blabla")
498 {
499  var_dump($set);
500  var_dump($rec);
501 }*/
502 
503 
504  return true;
505  }
506 
511  function getURLStringForNIC($a_nic_url)
512  {
513  $settings = $this->getAllSettings();
514 
515  $inst_id = (empty($settings["inst_id"])) ? "0" : $settings["inst_id"];
516 
517  // send host information to ilias-nic
518  $url = $a_nic_url.
519  "?cmd=getid".
520  "&inst_id=".rawurlencode($inst_id).
521  "&hostname=".rawurlencode($_SERVER["SERVER_NAME"]).
522  "&ipadr=".rawurlencode($_SERVER["SERVER_ADDR"]).
523  "&server_port=".rawurlencode($_SERVER["SERVER_PORT"]).
524  "&server_software=".rawurlencode($_SERVER["SERVER_SOFTWARE"]).
525  "&inst_name=".rawurlencode($this->ini->readVariable("client","name")).
526  "&inst_info=".rawurlencode($this->ini->readVariable("client","description")).
527  "&institution=".rawurlencode($settings["inst_institution"]).
528  "&http_path=".rawurlencode(ILIAS_HTTP_PATH).
529  "&contact_firstname=".rawurlencode($settings["admin_firstname"]).
530  "&contact_lastname=".rawurlencode($settings["admin_lastname"]).
531  "&contact_title=".rawurlencode($settings["admin_title"]).
532  "&contact_position=".rawurlencode($settings["admin_position"]).
533  "&contact_institution=".rawurlencode($settings["admin_institution"]).
534  "&contact_street=".rawurlencode($settings["admin_street"]).
535  "&contact_pcode=".rawurlencode($settings["admin_zipcode"]).
536  "&contact_city=".rawurlencode($settings["admin_city"]).
537  "&contact_country=".rawurlencode($settings["admin_country"]).
538  "&contact_phone=".rawurlencode($settings["admin_phone"]).
539  "&contact_email=".rawurlencode($settings["admin_email"]).
540  "&nic_key=".rawurlencode($this->getNICkey()).
541  "&version=".rawurlencode($settings["ilias_version"]);
542 
543  return $url;
544  }
545 
560  function updateNIC($a_nic_url)
561  {
562  $url = $this->getURLStringForNIC($a_nic_url);
563 
564  $conn =fopen($url,"r");
565 
566  $input = "";
567 
568  if (!$conn)
569  {
570  return false;
571  }
572  else
573  {
574  while(!feof($conn))
575  {
576  $input.= fgets($conn, 4096);
577  }
578 
579  fclose($conn);
580  $line = explode("\n",$input);
581 
582  $ret = $line;
583  }
584 
585  $this->nic_status = $ret;
586 
587  return true;
588  }
589 
598  function setNICkey()
599  {
600  mt_srand((double)microtime()*1000000);
601  $nic_key = md5(str_replace(".","",$_SERVER["SERVER_ADDR"]) +
602  mt_rand(100000,999999));
603 
604  $this->setSetting("nic_key",$nic_key);
605 
606  $this->nic_key = $nic_key;
607 
608  return true;
609  }
610 
616  function getNICkey()
617  {
618  $this->nic_key = $this->getSetting("nic_key");
619 
620  if (empty($this->nic_key))
621  {
622  $this->setNICkey();
623  }
624 
625  return $this->nic_key;
626  }
627 
629  {
630  return $this->getSetting("language");
631  }
632 
633  function setDefaultLanguage($a_lang_key)
634  {
635  $this->setSetting("language",$a_lang_key);
636  $this->ini->setVariable("language","default",$a_lang_key);
637  $this->ini->write();
638 
639  return true;
640  }
641 
646  function getError()
647  {
648  $error = $this->error;
649  $this->error = "";
650 
651  return $error;
652  }
653 
662  function delete ($a_ini = true, $a_db = false, $a_files = false)
663  {
664  if ($a_ini === true and file_exists(ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->getId()."/client.ini.php"))
665  {
666  unlink(CLIENT_WEB_DIR."/client.ini.php");
667  $msg[] = "ini_deleted";
668  }
669 
670  if ($a_db === true and $this->db_exists)
671  {
672  $this->db->query("DROP DATABASE ".$this->getDbName());
673  $msg[] = "db_deleted";
674  }
675 
676  if ($a_files === true and file_exists(CLIENT_WEB_DIR) and is_dir(CLIENT_WEB_DIR))
677  {
678  // rmdir();
679  ilUtil::delDir(CLIENT_WEB_DIR);
680  ilUtil::delDir(CLIENT_DATA_DIR);
681  $msg[] = "files_deleted";
682  }
683 
684  return $msg;
685  }
686 
691  function create()
692  {
693  //var_dump($this->getDataDir());exit;
694  // create base data dir
695  if (!ilUtil::makeDir($this->getDataDir()))
696  {
697  $this->error = "could_not_create_base_data_dir :".$this->getDataDir();
698  return false;
699  }
700 
701  // create sub dirs in base data dir
702  if (!ilUtil::makeDir($this->getDataDir()."/mail"))
703  {
704  $this->error = "could_not_create_mail_data_dir :".$this->getDataDir()."/mail";
705  return false;
706  }
707 
708  if (!ilUtil::makeDir($this->getDataDir()."/lm_data"))
709  {
710  $this->error = "could_not_create_lm_data_dir :".$this->getDataDir()."/lm_data";
711  return false;
712  }
713 
714  if (!ilUtil::makeDir($this->getDataDir()."/forum"))
715  {
716  $this->error = "could_not_create_forum_data_dir :".$this->getDataDir()."/forum";
717  return false;
718  }
719 
720  if (!ilUtil::makeDir($this->getDataDir()."/files"))
721  {
722  $this->error = "could_not_create_files_data_dir :".$this->getDataDir()."/files";
723  return false;
724  }
725 
726  // create base webspace dir
727  if (!ilUtil::makeDir($this->getWebspaceDir()))
728  {
729  $this->error = "could_not_create_base_webspace_dir :".$this->getWebspaceDir();
730  return false;
731  }
732 
733  // create sub dirs in base webspace dir
734  if (!ilUtil::makeDir($this->getWebspaceDir()."/lm_data"))
735  {
736  $this->error = "could_not_create_lm_webspace_dir :".$this->getWebspaceDir()."/lm_data";
737  return false;
738  }
739 
740  if (!ilUtil::makeDir($this->getWebspaceDir()."/usr_images"))
741  {
742  $this->error = "could_not_create_usr_images_webspace_dir :".$this->getWebspaceDir()."/usr_images";
743  return false;
744  }
745 
746  if (!ilUtil::makeDir($this->getWebspaceDir()."/mobs"))
747  {
748  $this->error = "could_not_create_mobs_webspace_dir :".$this->getWebspaceDir()."/mobs";
749  return false;
750  }
751 
752  if (!ilUtil::makeDir($this->getWebspaceDir()."/css"))
753  {
754  $this->error = "could_not_create_css_webspace_dir :".$this->getWebspaceDir()."/css";
755  return false;
756  }
757 
758  // write client ini
759  if (!$this->ini->write())
760  {
761  $this->error = get_class($this).": ".$this->ini->getError();
762  return false;
763  }
764 
765  return true;
766  }
767 } // END class.ilClient
768 ?>