ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilSetup.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 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 
24 include_once("./classes/class.ilDBConnections.php");
25 
35 class ilSetup extends PEAR
36 {
37  var $ini; // ini file object
38  var $ini_file_path; // full path to setup.ini, containing the client list
39  var $error = ""; // error text
40 
41  var $ini_ilias_exists = false; // control flag ilias.ini
42  var $ini_client_exists = false; // control flag client.ini
43 
44  var $setup_defaults; // ilias.master.ini
45  var $ilias_nic_server = "http://homer.ilias.uni-koeln.de/ilias-nic/index.php"; // URL to ilias nic server
46 
47  var $preliminaries_result = array(); // preliminaries check results
48  var $preliminaries = true; //
49 
55  var $SQL_FILE = "../setup/sql/ilias3.sql";
56 
62  var $dsn = "";
63 
69  var $db;
70 
71  var $setup_password; // master setup password
72  var $default_client; // client id of default client
73 
74  var $safe_mode; // safe mode enabled (true) or disabled (false)
75  var $safe_mode_exec_dir; // contains exec_dir_path
76 
77  var $auth; // current user is authenticated? (true)
78  var $access_mode; // if "admin", admin functions are enabled
79 
80 
87  function ilSetup($a_auth,$a_auth_type)
88  {
89  global $log;
90 
91  $this->PEAR();
92 
93  $this->db_connections = new ilDBConnections();
94 
95  define("ILIAS_MODULE","setup");
96 
97  $this->auth = ($a_auth) ? true : false;
98  $this->access_mode = $a_auth_type;
99 
100  // safe mode status & exec_dir
101  if ($this->safe_mode = ini_get("safe_mode"))
102  {
103  $this->safe_mode_exec_dir = ilFile::deleteTrailingSlash(ini_get("safe_mode_exec_dir"));
104  }
105 
106  // Error Handling
107  $this->error_obj = new ilErrorHandling();
108  $this->setErrorHandling(PEAR_ERROR_CALLBACK,array($this->error_obj,'errorHandler'));
109 
110  // set path to ilias.ini
111  $this->ini_file_path = ILIAS_ABSOLUTE_PATH."/ilias.ini.php";
112  $this->setup_defaults = ILIAS_ABSOLUTE_PATH."/setup/ilias.master.ini.php";
113 
114  // init setup.ini
115  $this->ini_ilias_exists = $this->init();
116 
117  if ($this->ini_ilias_exists)
118  {
119  if ($this->ini->readVariable("log","path") != "")
120  {
121  $log->path = $this->ini->readVariable("log","path");
122  }
123 
124  if ($this->ini->readVariable("log","file") != "")
125  {
126  $log->filename = $this->ini->readVariable("log","file");
127  }
128 
129  if ($this->ini->readVariable("log","enabled") != "")
130  {
131  $log->enabled = $this->ini->readVariable("log","enabled");
132  }
133  }
134  }
135 
141  function init()
142  {
143  // load data from setup.ini file
144  $this->ini = new ilIniFile($this->ini_file_path);
145 
146  if (!$this->ini->read())
147  {
148  $this->ini->GROUPS = parse_ini_file($this->setup_defaults,true);
149  $this->error = get_class($this).": ".$this->ini->getError();
150  return false;
151  }
152 
153  $this->setup_password = $this->ini->readVariable("setup","pass");
154  $this->default_client = $this->ini->readVariable("clients","default");
155 
156  define("ILIAS_DATA_DIR",$this->ini->readVariable("clients","datadir"));
157  define("ILIAS_WEB_DIR",$this->ini->readVariable("clients","path"));
158 
159  return true;
160  }
161 
166  function saveNewClient()
167  {
168  // save client id to session
169  $_SESSION["ClientId"] = $this->client->getId();
170 
171  // create client
172  if (!$this->client->create())
173  {
174  $this->error = $this->client->getError();
175  return false;
176  }
177 
178  //everything okay
179  $this->ini_client_exists = true;
180 
181  return true;
182  }
183 
189  function updateNewClient($a_old_client_id)
190  {
191  return true;
192  //var_dump("<pre>",$this->client,"</pre>");exit;
193 
194  if ($a_old_client_id != $this->client->getId())
195  {
196  // check for existing client dir
197  if (file_exists(ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$this->client->getId()))
198  {
199  $this->raiseError($this->lng->txt("client_id_already_exists"),$this->error_obj->MESSAGE);
200  }
201 
202  if (!$this->saveNewClient())
203  {
204  $this->raiseError($this->lng->txt("save_error"),$this->error_obj->MESSAGE);
205  }
206 
207  ilUtil::delDir(ILIAS_ABSOLUTE_PATH."/".ILIAS_WEB_DIR."/".$a_old_client_id);
208  ilUtil::delDir(ILIAS_DATA_DIR."/".$a_old_client_id);
209  }
210 
211  //everything okay
212  $this->ini_client_exists = true;
213 
214  return true;
215  }
216 
223  function execQuery($db,$str)
224  {
225  $sql = explode("\n",trim($str));
226  for ($i=0; $i<count($sql); $i++)
227  {
228  $sql[$i] = trim($sql[$i]);
229  if ($sql[$i] != "" && substr($sql[$i],0,1)!="#")
230  {
231  //take line per line, until last char is ";"
232  if (substr($sql[$i],-1)==";")
233  {
234  //query is complete
235  $q .= " ".substr($sql[$i],0,-1);
236  $r = $db->query($q);
237  if ($r == false)
238  return false;
239  unset($q);
240  } //if
241  else
242  {
243  $q .= " ".$sql[$i];
244  } //else
245  } //if
246  } //for
247  return true;
248  }
249 
254  function createDatabase($a_collation = "")
255  {
256  if ($this->client->checkDatabaseExists())
257  {
258  $this->error = $this->lng->txt("database_exists");
259  return false;
260  }
261 
262  //create database
263  $db = $this->db_connections->connectHost($this->client->dsn_host);
264  if (MDB2::isError($db))
265  {
266  $this->error = "connection_failed";
267  return false;
268  }
269 
270  if ($a_collation != "")
271  {
272  $sql = "CREATE DATABASE ".$this->client->getdbName().
273  " CHARACTER SET utf8".
274  " COLLATE ".$a_collation;
275  }
276  else
277  {
278  $sql = "CREATE DATABASE ".$this->client->getdbName();
279  }
280  $r = $db->query($sql);
281 
282  if (MDB2::isError($r))
283  {
284  $this->error = "create_database_failed";
285  return false;
286  }
287 
288  //database is created, now disconnect and reconnect
289  $db->disconnect();
290 
291  $this->client->db_exists = true;
292  return true;
293  }
294 
299  function installDatabase()
300  {
301  if (!$this->client->checkDatabaseHost())
302  {
303  $this->error = "no_connection_to_host";
304  return false;
305  }
306 
307  if (!$this->client->connect())
308  {
309  return false;
310  }
311 
312  //take sql dump an put it in
313  if ($this->readDump($this->client->db, $this->SQL_FILE))
314  {
315  $this->client->db_installed = true;
316  return true;
317  }
318  else
319  {
320  return false;
321  }
322  }
323 
324  function getline( $fp, $delim )
325  {
326  $result = "";
327  while( !feof( $fp ) )
328  {
329  $tmp = fgetc( $fp );
330  if( $tmp == $delim )
331  return $result;
332  $result .= $tmp;
333  }
334  return $result;
335  }
336 
343  function readDump($db, $file)
344  {
345  $fp = fopen($file, 'r');
346 
347  while(!feof($fp))
348  {
349  //$line = trim(fgets($fp, 200000));
350  $line = trim($this->getline($fp, "\n"));
351 
352  if ($line != "" && substr($line,0,1)!="#"
353  && substr($line,0,1)!="-")
354  {
355  //take line per line, until last char is ";"
356  if (substr($line,-1)==";")
357  {
358  //query is complete
359  $q .= " ".substr($line,0,-1);
360  $r = $db->query($q);
361  if (mysql_errno() > 0)
362  {
363  echo "<br />ERROR: ".mysql_error().
364  "<br />SQL: $q";
365  return false;
366  }
367  unset($q);
368  unset($line);
369  } //if
370  else
371  {
372  $q .= " ".$line;
373  } //else
374  } //if
375  } //for
376 
377  fclose($fp);
378  return true;
379  }
380 
381 
387  {
388  $a = @file_exists($this->INI_FILE);
389  return $a;
390  }
391 
397  function checkWritable($a_dir = "..")
398  {
399  clearstatcache();
400  if (is_writable($a_dir))
401  {
402  $arr["status"] = true;
403  $cdir = getcwd();
404  chdir("..");
405  $arr["comment"] = getcwd();
406  chdir($cdir);
407  }
408  else
409  {
410  $arr["status"] = false;
411  $arr["comment"] = $this->lng->txt("pre_folder_write_error");
412  $cdir = getcwd();
413  chdir("..");
414  $arr["comment"] = getcwd().": ".$arr["comment"];
415  chdir($cdir);
416  }
417 
418  return $arr;
419  }
420 
426  function checkCreatable($a_dir = "..")
427  {
428  clearstatcache();
429  if (@mkdir($a_dir."/crst879dldsk9d", 0774))
430  {
431  $arr["status"] = true;
432  $arr["comment"] = "";
433 
434  @rmdir($a_dir."/crst879dldsk9d");
435  }
436  else
437  {
438  $arr["status"] = false;
439  $cdir = getcwd();
440  chdir("..");
441  $arr["comment"] = getcwd().": ".$this->lng->txt("pre_folder_create_error");
442  chdir($cdir);
443  }
444 
445  return $arr;
446  }
447 
453  {
454  global $sess;
455 
456  if ($sess->usesCookies)
457  {
458  $arr["status"] = true;
459  $arr["comment"] = "";
460  }
461  else
462  {
463  $arr["status"] = false;
464  $arr["comment"] = $this->lng->txt("pre_cookies_disabled");
465  }
466 
467  return $arr;
468  }
469 
474  function checkPHPVersion()
475  {
476  $version = phpversion();
477 
478  $arr["version"] = $version;
479  $version_comp = explode(".", $version);
480  $first = (integer) substr($version,0,1);
481 
482  switch ($first)
483  {
484  case 2:
485  case 3:
486  $arr["status"] = false;
487  $arr["comment"] = $this->lng->txt("pre_php_version_3");
488  break;
489 
490  case 4:
491  $arr["status"] = false;
492  $arr["comment"] = "PHP ".$version.". ".$this->lng->txt("pre_php_version_4");
493  break;
494 
495  case 5:
496  $arr["status"] = true;
497  $arr["comment"] = "PHP ".$version;
498  if ((int)$version_comp[1] < 2 || ($version_comp[1] == 2 && $version_comp[2] == 0))
499  {
500  $arr["comment"].= ". ".$this->lng->txt("pre_php_version_5");
501  }
502  break;
503 
504  case 6:
505  $arr["status"] = true;
506  $arr["comment"] = "PHP ".$version;
507 
508  default:
509  $arr["status"] = true;
510  $arr["comment"] = $this->lng->txt("pre_php_version_unknown");
511  break;
512  }
513 
514  return $arr;
515  }
516 
521  function checkMySQL()
522  {
523  global $ilDB;
524 
525  if (function_exists("mysql_query"))
526  {
527  $arr["status"] = true;
528  $arr["comment"] = $this->lng->txt("pre_mysql_4_1_or_higher");
529  }
530  else
531  {
532  $arr["status"] = false;
533  $arr["comment"] = $this->lng->txt("pre_mysql_missing");
534  }
535 
536  return $arr;
537  }
538 
543  function checkAuth()
544  {
545  if ($_SESSION["auth"] === true)
546  {
547  return true;
548  }
549 
550  return false;
551  }
552 
553 
558  function checkDom()
559  {
560  global $ilDB;
561 
562  if (class_exists("DOMDocument"))
563  {
564  $arr["status"] = true;
565  }
566  else
567  {
568  $arr["status"] = false;
569  $arr["comment"] = $this->lng->txt("pre_dom_missing");
570  }
571 
572  return $arr;
573  }
574 
579  function checkXsl()
580  {
581  global $ilDB;
582 
583  if (class_exists("XSLTProcessor"))
584  {
585  $arr["status"] = true;
586  }
587  else
588  {
589  $arr["status"] = false;
590  $arr["comment"] = sprintf($this->lng->txt("pre_xsl_missing"),
591  "http://php.net/manual/en/book.xsl.php");
592  }
593 
594  return $arr;
595  }
596 
601  function checkGd()
602  {
603  global $ilDB;
604 
605  if (function_exists("imagefill") && function_exists("imagecolorallocate"))
606  {
607  $arr["status"] = true;
608  }
609  else
610  {
611  $arr["status"] = false;
612  $arr["comment"] = sprintf($this->lng->txt("pre_gd_missing"),
613  "http://php.net/manual/en/book.image.php");
614  }
615 
616  return $arr;
617  }
618 
623  function checkMemoryLimit()
624  {
625  global $ilDB;
626 
627  $limit = ini_get("memory_limit");
628 
629  $limit_ok = true;
630  if (is_int(strpos($limit, "M")))
631  {
632  $limit_n = (int) $limit;
633  if ($limit_n < 40)
634  {
635  $limit_ok = false;
636  }
637  }
638 
639  if ($limit_ok)
640  {
641  $arr["status"] = true;
642  $arr["comment"] = $limit.". ".$this->lng->txt("pre_memory_limit_recommend");
643  }
644  else
645  {
646  $arr["status"] = false;
647  $arr["comment"] = $limit.". ".$this->lng->txt("pre_memory_limit_too_low");
648  }
649 
650  return $arr;
651  }
652 
661  {
662  $a = array();
663  $a["php"] = $this->checkPHPVersion();
664  $a["mysql"] = $this->checkMySQL();
665  $a["root"] = $this->checkWritable();
666  $a["folder_create"] = $this->checkCreatable();
667  $a["cookies_enabled"] = $this->checkCookiesEnabled();
668  $a["dom"] = $this->checkDom();
669  $a["xsl"] = $this->checkXsl();
670  $a["gd"] = $this->checkGd();
671  $a["memory"] = $this->checkMemoryLimit();
672 
673  return $a;
674  }
675 
681  {
682  $this->preliminaries_result = $this->queryPreliminaries();
683 
684  foreach ($this->preliminaries_result as $val)
685  {
686  if ($val["status"] === false)
687  {
688  $this->preliminaries = false;
689  return false;
690  }
691  }
692 
693  return true;
694  }
695 
700  function getPassword ()
701  {
702  return $this->ini->readVariable("setup","pass");
703  }
704 
710  function setPassword ($a_password)
711  {
712  $this->ini->setVariable("setup","pass",md5($a_password));
713 
714  if ($this->ini->write() == false)
715  {
716  $this->error = $this->ini->getError();
717  return false;
718  }
719 
720  return true;
721  }
722 
728  function loginAsClient($a_auth_data)
729  {
730  if (empty($a_auth_data["client_id"]))
731  {
732  $this->error = "no_client_id";
733  return false;
734  }
735 
736  if (empty($a_auth_data["username"]))
737  {
738  $this->error = "no_username";
739  return false;
740  }
741 
742  if (empty($a_auth_data["password"]))
743  {
744  $this->error = "no_password";
745  return false;
746  }
747 
748  if (!$this->newClient($a_auth_data["client_id"]))
749  {
750  $this->error = "unknown_client_id";
751  unset($this->client);
752  return false;
753  }
754 
755  if (!$this->client->db_exists)
756  {
757  $this->error = "no_db_connect_consult_admin";
758  unset($this->client);
759  return false;
760  }
761 
762  $q = "SELECT usr_data.usr_id FROM usr_data ".
763  "LEFT JOIN rbac_ua ON rbac_ua.usr_id=usr_data.usr_id ".
764  "LEFT JOIN settings ON settings.value = rbac_ua.rol_id ".
765  "WHERE settings.keyword='system_role_id' ".
766  "AND usr_data.login='".$a_auth_data["username"]."' ".
767  "AND usr_data.passwd='".md5($a_auth_data["password"])."'";
768  $r = $this->client->db->query($q);
769 
770  if (!$r->numRows())
771  {
772  $this->error = "login_invalid";
773  return false;
774  }
775 
776  // all checks passed -> user valid
777  $_SESSION["auth"] = true;
778  $_SESSION["access_mode"] = "client";
779  $_SESSION["ClientId"] = $this->client->getId();
780  return true;
781  }
782 
788  function loginAsAdmin($a_password)
789  {
790  $a_password = md5($a_password);
791 
792  if ($this->ini->readVariable("setup","pass") == $a_password)
793  {
794  $_SESSION["auth"] = true;
795  $_SESSION["access_mode"] = "admin";
796  return true;
797  }
798 
799  return false;
800  }
801 
807  function newClient($a_client_id = 0)
808  {
809  if (!$this->isInstalled())
810  {
811  return false;
812  }
813 
814  $this->client = new ilClient($a_client_id, $this->db_connections);
815 
816  if (!$this->client->init())
817  {
818  $this->error = get_class($this).": ".$this->client->getError();
819  $_SESSION["ClientId"] = "";
820  return false;
821  }
822 
823  $_SESSION["ClientId"] = $a_client_id;
824 
825  return true;
826  }
827 
833  function getStatus ($client = 0)
834  {
835  if (!is_object($client))
836  {
837  if ($this->ini_client_exists)
838  {
840  }
841  else
842  {
843  $client = new ilClient(0, $this->db_connections);
844  }
845  }
846 
847  $status = array();
848  $status["ini"] = $this->checkClientIni($client); // check this one
849  $status["db"] = $this->checkClientDatabase($client);
850 
851  if ($status["db"]["status"] === false and $status["db"]["update"] !== true)
852  {
853  $status["lang"]["status"] = false;
854  $status["lang"]["comment"] = $status["db"]["comment"];
855  $status["contact"]["status"] = false;
856  $status["contact"]["comment"] = $status["db"]["comment"];
857  $status["nic"]["status"] = false;
858  $status["nic"]["comment"] = $status["db"]["comment"];
859  }
860  else
861  {
862  $status["lang"] = $this->checkClientLanguages($client);
863  $status["contact"] = $this->checkClientContact($client);
864  $status["nic"] = $this->checkClientNIC($client);
865  $status["finish"] = $this->checkFinish($client);
866  $status["access"] = $this->checkAccess($client);
867  }
868 
869  //return value
870  return $status;
871  }
872 
878  function checkFinish(&$client)
879  {
880  if ($client->getSetting("setup_ok"))
881  {
882  $arr["status"] = true;
883  $arr["comment"] = $this->lng->txt("setup_finished");
884  }
885  else
886  {
887  $arr["status"] = false;
888  $arr["comment"] = $this->lng->txt("setup_not_finished");
889  }
890 
891  return $arr;
892  }
893 
899  function checkAccess(&$client)
900  {
901  if ($client->ini->readVariable("client","access") == "1")
902  {
903  $arr["status"] = true;
904  $arr["comment"] = $this->lng->txt("online");
905  }
906  else
907  {
908  $arr["status"] = false;
909  $arr["comment"] = $this->lng->txt("disabled");
910  }
911 
912  return $arr;
913  }
914 
921  {
922  if (!$arr["status"] = $client->init())
923  {
924  $arr["comment"] = $client->getError();
925  }
926  else
927  {
928  $arr["comment"] = "dir: /".ILIAS_WEB_DIR."/".$client->getId();
929  }
930 
931  return $arr;
932  }
933 
940  {
941  if (!$arr["status"] = $client->db_exists)
942  {
943  $arr["comment"] = $this->lng->txt("no_database");
944  return $arr;
945  }
946 
947  if (!$arr["status"] = $client->db_installed)
948  {
949  $arr["comment"] = $this->lng->txt("db_not_installed");
950  return $arr;
951  }
952 
953  // TODO: move this to client class!!
954  $client->setup_ok = (bool) $client->getSetting("setup_ok");
955 
956  $this->lng->setDbHandler($client->db);
957  include_once "../classes/class.ilDBUpdate.php";
958  $dbupdate = new ilDBUpdate($client->db);
959 
960  if (!$arr["status"] = $dbupdate->getDBVersionStatus())
961  {
962  $arr["comment"] = $this->lng->txt("db_needs_update");
963  $arr["update"] = true;
964  return $arr;
965  }
966 
967  // check control information
968  $q = "SELECT count(*) as cnt FROM ctrl_calls";
969  $cset = $client->db->query($q);
970  $crec = $cset->fetchRow(DB_FETCHMODE_ASSOC);
971  if ($crec["cnt"] == 0)
972  {
973  $arr["status"] = false;
974  $arr["comment"] = $this->lng->txt("db_control_structure_missing");
975  $arr["update"] = true;
976  return $arr;
977  }
978 
979  $arr["comment"] = "version ".$dbupdate->getCurrentVersion();
980  return $arr;
981  }
982 
989  {
990  $installed_langs = $this->lng->getInstalledLanguages();
991 
992  $count = count($installed_langs);
993 
994  if ($count < 1)
995  {
996  $arr["status"] = false;
997  $arr["comment"] = $this->lng->txt("lang_none_installed");
998  }
999  else
1000  {
1001  $arr["status"] = true;
1002  $arr["comment"] = $count." ".$this->lng->txt("languages_installed");
1003  }
1004 
1005  return $arr;
1006  }
1007 
1014  {
1015  $arr["status"] = true;
1016  $arr["comment"] = $this->lng->txt("filled_out");
1017 
1018  $settings = $client->getAllSettings();
1019  $client_name = $client->getName();
1020 
1021  // check required fields
1022  if (empty($settings["admin_firstname"]) or empty($settings["admin_lastname"])
1023  or empty($settings["admin_street"]) or empty($settings["admin_zipcode"])
1024  or empty($settings["admin_country"]) or empty($settings["admin_city"])
1025  or empty($settings["admin_phone"]) or empty($settings["admin_email"])
1026  or empty($client_name) or empty($settings["inst_institution"]))
1027  {
1028  $arr["status"] = false;
1029  $arr["comment"] = $this->lng->txt("missing_data");
1030  }
1031 
1032  // admin email
1033  if (!ilUtil::is_email($settings["admin_email"]) and $arr["status"] != false)
1034  {
1035  $arr["status"] = false;
1036  $arr["comment"] = $this->lng->txt("email_not_valid");
1037  }
1038 
1039  return $arr;
1040  }
1041 
1048  {
1049  $settings = $client->getAllSettings();
1050 
1051  if (!isset($settings["nic_enabled"]))
1052  {
1053  $arr["status"] = false;
1054  $arr["comment"] = $this->lng->txt("nic_not_disabled");
1055  return $arr;
1056  }
1057 
1058  $arr["status"] = true;
1059 
1060  if ($settings["nic_enabled"] == "-1")
1061  {
1062  $arr["comment"] = $this->lng->txt("nic_reg_failed");
1063  return $arr;
1064  }
1065 
1066  if (!$settings["nic_enabled"])
1067  {
1068  $arr["comment"] = $this->lng->txt("nic_reg_disabled");
1069  }
1070  else
1071  {
1072  $arr["comment"] = $this->lng->txt("nic_reg_enabled");
1073  if ($settings["inst_id"] <= 0)
1074  {
1075  $arr["status"] = false;
1076  }
1077  }
1078 
1079  return $arr;
1080  }
1081 
1086  function isInstalled()
1087  {
1088  return $this->ini_ilias_exists;
1089  }
1090 
1095  function isAuthenticated()
1096  {
1097  return $this->auth;
1098  }
1099 
1104  function isAdmin()
1105  {
1106  return ($this->access_mode == "admin") ? true : false;
1107  }
1108 
1114  function saveMasterSetup($a_formdata)
1115  {
1116  $datadir_path = preg_replace("/\\\\/","/",ilFile::deleteTrailingSlash(ilUtil::stripSlashes($a_formdata["datadir_path"])));
1117 
1118  if ($a_formdata["chk_datadir_path"] == 1) // mode create dir
1119  {
1120  if (!ilUtil::makeDir($datadir_path))
1121  {
1122  $this->error = "create_datadir_failed";
1123  return false;
1124  }
1125  }
1126 
1127  // create webspace dir if it does not exist
1128  if (!@file_exists(ILIAS_ABSOLUTE_PATH."/".$this->ini->readVariable("clients","path")) and !@is_dir(ILIAS_ABSOLUTE_PATH."/".$this->ini->readVariable("clients","path")))
1129  {
1130  if (!ilUtil::makeDir(ILIAS_ABSOLUTE_PATH."/".$this->ini->readVariable("clients","path")))
1131  {
1132  $this->error = "create_webdir_failed";
1133  return false;
1134  }
1135  }
1136 
1137  $form_log_path = preg_replace("/\\\\/","/",ilFile::deleteTrailingSlash(ilUtil::stripSlashes($a_formdata["log_path"])));
1138  $log_path = substr($form_log_path,0,strrpos($form_log_path,"/"));
1139  $log_file = substr($form_log_path,strlen($log_path)+1);
1140 
1141  $this->ini->setVariable("server","http_path",ILIAS_HTTP_PATH);
1142  $this->ini->setVariable("server","absolute_path",ILIAS_ABSOLUTE_PATH);
1143  $this->ini->setVariable("clients", "datadir", $datadir_path);
1144  $this->ini->setVariable("tools", "convert", preg_replace("/\\\\/","/",ilUtil::stripSlashes($a_formdata["convert_path"])));
1145  $this->ini->setVariable("tools", "zip", preg_replace("/\\\\/","/",ilUtil::stripSlashes($a_formdata["zip_path"])));
1146  $this->ini->setVariable("tools", "unzip", preg_replace("/\\\\/","/",ilUtil::stripSlashes($a_formdata["unzip_path"])));
1147  $this->ini->setVariable("tools", "java", preg_replace("/\\\\/","/",ilUtil::stripSlashes($a_formdata["java_path"])));
1148  $this->ini->setVariable("tools", "htmldoc", preg_replace("/\\\\/","/",ilUtil::stripSlashes($a_formdata["htmldoc_path"])));
1149  $this->ini->setVariable("tools", "latex", ilUtil::stripSlashes($a_formdata["latex_url"]));
1150  $this->ini->setVariable("tools", "vscantype", preg_replace("/\\\\/","/",ilUtil::stripSlashes($a_formdata["vscanner_type"])));
1151  $this->ini->setVariable("tools", "scancommand", preg_replace("/\\\\/","/",ilUtil::stripSlashes($a_formdata["scan_command"])));
1152  $this->ini->setVariable("tools", "cleancommand", preg_replace("/\\\\/","/",ilUtil::stripSlashes($a_formdata["clean_command"])));
1153  $this->ini->setVariable("setup", "pass", md5($a_formdata["setup_pass"]));
1154  $this->ini->setVariable("log", "path", $log_path);
1155  $this->ini->setVariable("log", "file", $log_file);
1156  $this->ini->setVariable("log", "enabled", (isset($a_formdata["chk_log_status"])) ? "0" : 1);
1157 
1158  if (!$this->ini->write())
1159  {
1160  $this->error = get_class($this).": ".$this->ini->getError();
1161  return false;
1162  }
1163 
1164  // everything is fine. so we authenticate the user and set access mode to 'admin'
1165  $_SESSION["auth"] = true;
1166  $_SESSION["access_mode"] = "admin";
1167 
1168  return true;
1169  }
1170 
1176  function updateMasterSettings($a_formdata)
1177  {
1178  $convert_path = preg_replace("/\\\\/","/",ilUtil::stripSlashes($a_formdata["convert_path"]));
1179  $zip_path = preg_replace("/\\\\/","/",ilUtil::stripSlashes($a_formdata["zip_path"]));
1180  $unzip_path = preg_replace("/\\\\/","/",ilUtil::stripSlashes($a_formdata["unzip_path"]));
1181  $java_path = preg_replace("/\\\\/","/",ilUtil::stripSlashes($a_formdata["java_path"]));
1182  $htmldoc_path = preg_replace("/\\\\/","/",ilUtil::stripSlashes($a_formdata["htmldoc_path"]));
1183  $latex_url = ilUtil::stripSlashes($a_formdata["latex_url"]);
1184  $fop_path = preg_replace("/\\\\/","/",ilUtil::stripSlashes($a_formdata["fop_path"]));
1185  $scan_type = preg_replace("/\\\\/","/",ilUtil::stripSlashes($a_formdata["vscanner_type"]));
1186  $scan_command = preg_replace("/\\\\/","/",ilUtil::stripSlashes($a_formdata["scan_command"]));
1187  $clean_command = preg_replace("/\\\\/","/",ilUtil::stripSlashes($a_formdata["clean_command"]));
1188 
1189  $this->ini->setVariable("tools", "convert", $convert_path);
1190  $this->ini->setVariable("tools", "zip", $zip_path);
1191  $this->ini->setVariable("tools", "unzip", $unzip_path);
1192  $this->ini->setVariable("tools", "java", $java_path);
1193  $this->ini->setVariable("tools", "htmldoc", $htmldoc_path);
1194  $this->ini->setVariable("tools", "latex", $latex_url);
1195  $this->ini->setVariable("tools", "fop", $fop_path);
1196  $this->ini->setVariable("tools", "vscantype", $scan_type);
1197  $this->ini->setVariable("tools", "scancommand", $scan_command);
1198  $this->ini->setVariable("tools", "cleancommand", $clean_command);
1199 
1200  $form_log_path = preg_replace("/\\\\/","/",ilFile::deleteTrailingSlash(ilUtil::stripSlashes($a_formdata["log_path"])));
1201  $log_path = substr($form_log_path,0,strrpos($form_log_path,"/"));
1202  $log_file = substr($form_log_path,strlen($log_path)+1);
1203 
1204  $this->ini->setVariable("log", "path", $log_path);
1205  $this->ini->setVariable("log", "file", $log_file);
1206  $this->ini->setVariable("log", "enabled", (isset($a_formdata["chk_log_status"])) ? "0" : 1);
1207 
1208  if (!$this->ini->write())
1209  {
1210  $this->error = get_class($this).": ".$this->ini->getError();
1211  return false;
1212  }
1213 
1214  return true;
1215  }
1216 
1222  function checkToolsSetup($a_formdata)
1223  {
1224  // convert path
1225  if (!isset($a_formdata["chk_convert_path"]))
1226  {
1227  // convert backslashes to forwardslashes
1228  $convert_path = preg_replace("/\\\\/","/",ilUtil::stripSlashes($a_formdata["convert_path"]));
1229 
1230  if (($err = $this->testConvert($convert_path)) != "")
1231  {
1232  $this->error = $err;
1233  return false;
1234  }
1235  }
1236 
1237  // zip path
1238  if (!isset($a_formdata["chk_zip_path"]))
1239  {
1240  // convert backslashes to forwardslashes
1241  $zip_path = preg_replace("/\\\\/","/",ilUtil::stripSlashes($a_formdata["zip_path"]));
1242 
1243  if (empty($zip_path))
1244  {
1245  $this->error = "no_path_zip";
1246  return false;
1247  }
1248 
1249  if (!$this->testZip($zip_path))
1250  {
1251  $this->error = "check_failed_zip";
1252  return false;
1253  }
1254  }
1255 
1256  // unzip path
1257  if (!isset($a_formdata["chk_unzip_path"]))
1258  {
1259  // convert backslashes to forwardslashes
1260  $unzip_path = preg_replace("/\\\\/","/",ilUtil::stripSlashes($a_formdata["unzip_path"]));
1261 
1262  if (empty($unzip_path))
1263  {
1264  $this->error = "no_path_unzip";
1265  return false;
1266  }
1267 
1268  if (!$this->testUnzip($unzip_path))
1269  {
1270  $this->error = "check_failed_unzip";
1271  return false;
1272  }
1273  }
1274 
1275  // java path
1276  if (!isset($a_formdata["chk_java_path"]))
1277  {
1278  // convert backslashes to forwardslashes
1279  $java_path = preg_replace("/\\\\/","/",ilUtil::stripSlashes($a_formdata["java_path"]));
1280 
1281  if (empty($java_path))
1282  {
1283  $this->error = "no_path_java";
1284  return false;
1285  }
1286 
1287  if (!$this->testJava($java_path))
1288  {
1289  $this->error = "check_failed_java";
1290  return false;
1291  }
1292  }
1293 
1294  // htmldoc path
1295  if (!isset($a_formdata["chk_htmldoc_path"]))
1296  {
1297  // convert backslashes to forwardslashes
1298  $htmldoc_path = preg_replace("/\\\\/","/",ilUtil::stripSlashes($a_formdata["htmldoc_path"]));
1299 
1300  if (empty($htmldoc_path))
1301  {
1302  $this->error = "no_path_htmldoc";
1303  return false;
1304  }
1305 
1306  if (!$this->testHtmldoc($htmldoc_path))
1307  {
1308  $this->error = "check_failed_htmldoc";
1309  return false;
1310  }
1311  }
1312 
1313  // latex url
1314  if (!isset($a_formdata["chk_latex_url"]))
1315  {
1316  $latex_url = ilUtil::stripSlashes($a_formdata["latex_url"]);
1317  if (empty($latex_url))
1318  {
1319  $this->error = "no_latex_url";
1320  return false;
1321  }
1322 
1323  if (!$this->testLatex($latex_url))
1324  {
1325  $this->error = "check_failed_latex";
1326  return false;
1327  }
1328  }
1329 
1330  return true;
1331  }
1332 
1338  function checkDataDirSetup($a_formdata)
1339  {
1340  // remove trailing slash & convert backslashes to forwardslashes
1341  $datadir_path = preg_replace("/\\\\/","/",ilFile::deleteTrailingSlash(ilUtil::stripSlashes($a_formdata["datadir_path"])));
1342 
1343  if (empty($datadir_path))
1344  {
1345  $this->error = "no_path_datadir";
1346  return false;
1347  }
1348 
1349  $webspace_dir = ILIAS_ABSOLUTE_PATH . "/data";
1350 
1351  // datadir may not point to webspace dir or to any place under webspace_dir
1352  if (strpos($datadir_path,$webspace_dir) !== false)
1353  {
1354  $this->error = "datadir_webspacedir_match";
1355  return false;
1356  }
1357 
1358  // create dir
1359  if ($a_formdata["chk_datadir_path"] == 1)
1360  {
1361  $dir_to_create = substr(strrchr($datadir_path, "/"), 1);
1362  $dir_to_check = substr($datadir_path,0,- strlen($dir_to_create)-1);
1363 
1364  if (is_writable($datadir_path))
1365  {
1366  $this->error = "dir_exists_create";
1367  return false;
1368  }
1369 
1370  if (!is_writable($dir_to_check))
1371  {
1372  $this->error = "cannot_create_datadir_no_write_access";
1373  return false;
1374  }
1375  }
1376  else // check set target dir
1377  {
1378  if (!is_writable($datadir_path))
1379  {
1380  $this->error = "cannot_create_datadir_no_write_access";
1381  return false;
1382  }
1383  }
1384 
1385  return true;
1386  }
1387 
1393  function checkPasswordSetup($a_formdata)
1394  {
1395  if (!$a_formdata["setup_pass"])
1396  {
1397  $this->error = "no_setup_pass_given";
1398  return false;
1399  }
1400 
1401  if ($a_formdata["setup_pass"] != $a_formdata["setup_pass2"])
1402  {
1403  $this->error = "pass_does_not_match";
1404  return false;
1405  }
1406 
1407  return true;
1408  }
1409 
1415  function checkLogSetup($a_formdata)
1416  {
1417  // log path
1418  if (!isset($a_formdata["chk_log_status"]))
1419  {
1420  // remove trailing slash & convert backslashes to forwardslashes
1421  $log_path = preg_replace("/\\\\/","/",ilFile::deleteTrailingSlash(ilUtil::stripSlashes($a_formdata["log_path"])));
1422 
1423  if (empty($log_path))
1424  {
1425  $this->error = "no_path_log";
1426  return false;
1427  }
1428 
1429  if (!@touch($log_path))
1430  {
1431  $this->error = "could_not_create_logfile";
1432  return false;
1433  }
1434  }
1435 
1436  return true;
1437  }
1438 
1443  function getError()
1444  {
1445  if (empty($this->error))
1446  {
1447  return false;
1448  }
1449 
1450  $error = $this->error;
1451  $this->error = "";
1452 
1453  return $error;
1454  }
1455 
1461  function _ilSetup()
1462  {
1463  //if ($this->ini->readVariable("db","type") != "")
1464  //{
1465  // $this->db->disconnect();
1466  //}
1467  return true;
1468  }
1469 
1476  function testConvert($a_convert_path)
1477  {
1478  if (trim($a_convert_path) == "")
1479  {
1480  return "no_path_convert";
1481  }
1482  if (!is_file($a_convert_path))
1483  {
1484  return "check_failed_convert";
1485  }
1486 
1487  return "";
1488 
1489 /*
1490  // generate gif with convert
1491  if (file_exists(ILIAS_ABSOLUTE_PATH."/images/test.gif"))
1492  {
1493  unlink(ILIAS_ABSOLUTE_PATH."/images/test.gif");
1494  }
1495 
1496  system($a_convert_path." ".ILIAS_ABSOLUTE_PATH."/setup/test/test.jpg GIF:".ILIAS_ABSOLUTE_PATH."/setup/test/test.gif");
1497 
1498  // check wether convert generated file
1499  if (file_exists(ILIAS_ABSOLUTE_PATH."/setup/test/test.gif"))
1500  {
1501  unlink(ILIAS_ABSOLUTE_PATH."/setup/test/test.gif");
1502  return true;
1503  }
1504  else
1505  {
1506  return false;
1507  }
1508 */
1509  }
1510 
1517  function testJava ($a_java_path)
1518  {
1519  // java is optional, so empty path is ok
1520  if (trim($a_java_path) == "")
1521  {
1522  return "";
1523  }
1524 
1525  if (!is_file($a_java_path))
1526  {
1527  return "check_failed_java";
1528  }
1529 
1530  return "";
1531 /*
1532  exec($a_java_path, $out, $back);
1533 
1534  unset($out);
1535 
1536  return ($back != 1) ? false : true;
1537 */
1538  }
1539 
1546  function testLatex($a_latex_url)
1547  {
1548  // latex is optional, so empty path is ok
1549  if (trim($a_latex_url) == "")
1550  {
1551  return "";
1552  }
1553 
1554  // open the URL
1555  include_once "class.ilHttpRequest.php";
1556  $http = new ilHttpRequest(ilUtil::stripSlashes($a_latex_url) . "?x_0");
1557  $result = @$http->downloadToString();
1558  if ((strpos((substr($result, 0, 5)), "PNG") !== FALSE) || (strpos((substr($result, 0, 5)), "GIF") !== FALSE))
1559  {
1560  return "";
1561  }
1562  else
1563  {
1564  return "check_failed_latex";;
1565  }
1566  }
1567 
1574  function testZip ($a_zip_path)
1575  {
1576  if (trim($a_zip_path) == "")
1577  {
1578  return "no_path_zip";
1579  }
1580  if (!is_file($a_zip_path))
1581  {
1582  return "check_failed_zip";
1583  }
1584 
1585  return "";
1586 /*
1587  // create test file and run zip
1588  $fp = fopen(ILIAS_ABSOLUTE_PATH."/test.dat", "w");
1589 
1590  fwrite($fp, "test");
1591  fclose($fp);
1592 
1593  if (file_exists(ILIAS_ABSOLUTE_PATH."/test.dat"))
1594  {
1595  $curDir = getcwd();
1596  chdir(ILIAS_ABSOLUTE_PATH);
1597 
1598  $zipCmd = $a_zip_path." -m zip_test_file.zip test.dat";
1599 
1600  exec($zipCmd);
1601 
1602  chdir($curDir);
1603 
1604  }
1605 
1606  // check wether zip generated test file or not
1607  if (file_exists(ILIAS_ABSOLUTE_PATH."/zip_test_file.zip"))
1608  {
1609  unlink(ILIAS_ABSOLUTE_PATH."/zip_test_file.zip");
1610  return true;
1611  }
1612  else
1613  {
1614  unlink(ILIAS_ABSOLUTE_PATH."/test.dat");
1615  return false;
1616  }
1617 */
1618  }
1619 
1620 
1627  function testUnzip ($a_unzip_path)
1628  {
1629  if (trim($a_unzip_path) == "")
1630  {
1631  return "no_path_unzip";
1632  }
1633  if (!is_file($a_unzip_path))
1634  {
1635  return "check_failed_unzip";
1636  }
1637 
1638  return "";
1639 /*
1640  $curDir = getcwd();
1641 
1642  chdir(ILIAS_ABSOLUTE_PATH);
1643 
1644  if (file_exists(ILIAS_ABSOLUTE_PATH."/unzip_test_file.zip"))
1645  {
1646  $unzipCmd = $a_unzip_path." unzip_test_file.zip";
1647  exec($unzipCmd);
1648  }
1649 
1650  chdir($curDir);
1651 
1652  // check wether unzip extracted the test file or not
1653  if (file_exists(ILIAS_ABSOLUTE_PATH."/unzip_test_file.txt"))
1654  {
1655  unlink(ILIAS_ABSOLUTE_PATH."/unzip_test_file.txt");
1656 
1657  return true;
1658  }
1659  else
1660  {
1661  return false;
1662  }
1663 */
1664  }
1665 
1672  function testHtmldoc($a_htmldoc_path)
1673  {
1674  // java is optional, so empty path is ok
1675  if (trim($a_htmldoc_path) == "")
1676  {
1677  return "";
1678  }
1679 
1680  if (!is_file($a_htmldoc_path))
1681  {
1682  return "check_failed_htmldoc";
1683  }
1684 
1685  return "";
1686 
1687 
1688  $curDir = getcwd();
1689 
1690  chdir(ILIAS_ABSOLUTE_PATH);
1691 
1692  $html = "<html><head><title></title></head><body><p>test</p></body></html>";
1693 
1694  $html_file = "htmldoc_test_file.html";
1695 
1696  $fp = fopen( $html_file ,"wb");
1697  fwrite($fp, $html);
1698  fclose($fp);
1699 
1700  $htmldoc = $a_htmldoc_path." ";
1701  $htmldoc .= "--no-toc ";
1702  $htmldoc .= "--no-jpeg ";
1703  $htmldoc .= "--webpage ";
1704  $htmldoc .= "--outfile htmldoc_test_file.pdf ";
1705  $htmldoc .= "--bodyfont Arial ";
1706  $htmldoc .= "--charset iso-8859-15 ";
1707  $htmldoc .= "--color ";
1708  $htmldoc .= "--size A4 "; // --landscape
1709  $htmldoc .= "--format pdf ";
1710  $htmldoc .= "--footer ... ";
1711  $htmldoc .= "--header ... ";
1712  $htmldoc .= "--left 60 ";
1713  // $htmldoc .= "--right 200 ";
1714  $htmldoc .= $html_file;
1715  exec($htmldoc);
1716 
1717  unlink(ILIAS_ABSOLUTE_PATH."/".$html_file);
1718 
1719  chdir($curDir);
1720 
1721  if (file_exists(ILIAS_ABSOLUTE_PATH."/htmldoc_test_file.pdf"))
1722  {
1723  unlink(ILIAS_ABSOLUTE_PATH."/htmldoc_test_file.pdf");
1724  return true;
1725  }
1726  else
1727  {
1728  return false;
1729  }
1730  }
1731 
1732  function unzipTiny()
1733  {
1734  $this->unzip(ILIAS_ABSOLUTE_PATH . "/Services/RTE/tiny_mce.zip", TRUE);
1735  $this->ini->setVariable("tools", "tiny_md5", md5_file(ILIAS_ABSOLUTE_PATH . "/Services/RTE/tiny_mce.zip"));
1736  if ($this->ini->write() == false)
1737  {
1738  $this->error = $this->ini->getError();
1739  return false;
1740  }
1741  return TRUE;
1742  }
1743 
1750  function unzip($a_file, $overwrite = false)
1751  {
1752  //global $ilias;
1753 
1754  $pathinfo = pathinfo($a_file);
1755  $dir = $pathinfo["dirname"];
1756  $file = $pathinfo["basename"];
1757 
1758  // unzip
1759  $cdir = getcwd();
1760  chdir($dir);
1761  $unzip = $this->ini->readVariable("tools","unzip");
1762  $unzipcmd = $unzip." -Z -1 ".ilUtil::escapeShellArg($file);
1763  exec($unzipcmd, $arr);
1764  $zdirs = array();
1765 
1766  foreach($arr as $line)
1767  {
1768  if(is_int(strpos($line, "/")))
1769  {
1770  $zdir = substr($line, 0, strrpos($line, "/"));
1771  $nr = substr_count($zdir, "/");
1772  //echo $zdir." ".$nr."<br>";
1773  while ($zdir != "")
1774  {
1775  $nr = substr_count($zdir, "/");
1776  $zdirs[$zdir] = $nr; // collect directories
1777  //echo $dir." ".$nr."<br>";
1778  $zdir = substr($zdir, 0, strrpos($zdir, "/"));
1779  }
1780  }
1781  }
1782 
1783  asort($zdirs);
1784 
1785  foreach($zdirs as $zdir => $nr) // create directories
1786  {
1787  ilUtil::createDirectory($zdir);
1788  }
1789 
1790  // real unzip
1791  if ($overvwrite)
1792  {
1793  $unzipcmd = $unzip." ".ilUtil::escapeShellArg($file);
1794  }
1795  else
1796  {
1797  $unzipcmd = $unzip." -o ".ilUtil::escapeShellArg($file);
1798  }
1799  exec($unzipcmd);
1800 
1801  chdir($cdir);
1802  }
1803 
1804 } // END class.ilSetup
1805 ?>