index.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. <?php
  2. /**
  3. * Wizard to configure and initialize the iTop application
  4. */
  5. require_once('../application/utils.inc.php');
  6. require_once('../core/config.class.inc.php');
  7. require_once('../core/cmdbsource.class.inc.php');
  8. require_once('./setuppage.class.inc.php');
  9. define('TMP_CONFIG_FILE', '../tmp-config-itop.php');
  10. define('FINAL_CONFIG_FILE', '../config-itop.php');
  11. define('SETUP_STRUCTURE_DATA_DIR', './data/structure');
  12. define('SETUP_SAMPLE_DATA_DIR', './data');
  13. define('PHP_MIN_VERSION', '5.2.0');
  14. define('MYSQL_MIN_VERSION', '5.0.0');
  15. $sOperation = Utils::ReadParam('operation', 'step1');
  16. $oP = new SetupWebPage('iTop configuration wizard');
  17. /**
  18. * Helper function to retrieve the system's temporary directory
  19. * Emulates sys_get_temp_dir if neeed (PHP < 5.2.1)
  20. * @return string Path to the system's temp directory
  21. */
  22. function GetTmpDir()
  23. {
  24. // try to figure out what is the temporary directory
  25. // prior to PHP 5.2.1 the function sys_get_temp_dir
  26. // did not exist
  27. if ( !function_exists('sys_get_temp_dir'))
  28. {
  29. if( $temp=getenv('TMP') ) return realpath($temp);
  30. if( $temp=getenv('TEMP') ) return realpath($temp);
  31. if( $temp=getenv('TMPDIR') ) return realpath($temp);
  32. $temp=tempnam(__FILE__,'');
  33. if (file_exists($temp))
  34. {
  35. unlink($temp);
  36. return realpath(dirname($temp));
  37. }
  38. return null;
  39. }
  40. else
  41. {
  42. return realpath(sys_get_temp_dir());
  43. }
  44. }
  45. /**
  46. * Helper function to retrieve the directory where files are to be uploaded
  47. * @return string Path to the temp directory used for uploading files
  48. */
  49. function GetUploadTmpDir()
  50. {
  51. $sPath = ini_get('upload_tmp_dir');
  52. if (empty($sPath))
  53. {
  54. $sPath = GetTmpDir();
  55. }
  56. return $sPath;
  57. }
  58. /**
  59. * Helper function to check if the current version of PHP
  60. * is compatible with the application
  61. * @return boolean true if this is Ok, false otherwise
  62. */
  63. function CheckPHPVersion(SetupWebPage $oP)
  64. {
  65. $bResult = true;
  66. $oP->log('Info - CheckPHPVersion');
  67. if (version_compare(phpversion(), PHP_MIN_VERSION, '>='))
  68. {
  69. $oP->ok("The current PHP Version (".phpversion().") is greater than the minimum required version (".PHP_MIN_VERSION.")");
  70. }
  71. else
  72. {
  73. $oP->error("Error: The current PHP Version (".phpversion().") is lower than the minimum required version (".PHP_MIN_VERSION.")");
  74. return false;
  75. }
  76. $aMandatoryExtensions = array('mysql', 'iconv', 'simplexml', 'soap');
  77. asort($aMandatoryExtensions); // Sort the list to look clean !
  78. $aExtensionsOk = array();
  79. $aMissingExtensions = array();
  80. $aMissingExtensionsLinks = array();
  81. foreach($aMandatoryExtensions as $sExtension)
  82. {
  83. if (extension_loaded($sExtension))
  84. {
  85. $aExtensionsOk[] = $sExtension;
  86. }
  87. else
  88. {
  89. $aMissingExtensions[] = $sExtension;
  90. $aMissingExtensionsLinks[] = "<a href=\"http://www.php.net/manual/en/book.$sExtension.php\">$sExtension</a>";
  91. }
  92. }
  93. if (count($aExtensionsOk) > 0)
  94. {
  95. $oP->ok("Required PHP extension(s): ".implode(', ', $aExtensionsOk).".");
  96. }
  97. if (count($aMissingExtensions) > 0)
  98. {
  99. $oP->error("Missing PHP extension(s): ".implode(', ', $aMissingExtensionsLinks).".");
  100. $bResult = false;
  101. }
  102. // Check some ini settings here
  103. if (function_exists('php_ini_loaded_file')) // PHP >= 5.2.4
  104. {
  105. $sPhpIniFile = php_ini_loaded_file();
  106. $oP->log("Info - php.ini path: '$sPhpIniFile'");
  107. }
  108. else
  109. {
  110. $sPhpIniFile = 'php.ini';
  111. }
  112. if (!ini_get('file_uploads'))
  113. {
  114. $oP->error("Files upload is not allowed on this server (file_uploads = ".ini_get('file_uploads').").");
  115. $bResult = false;
  116. }
  117. $sUploadTmpDir = GetUploadTmpDir();
  118. if (empty($sUploadTmpDir))
  119. {
  120. $sUploadTmpDir = '/tmp';
  121. $oP->warning("Temporary directory for files upload is not defined (upload_tmp_dir), assuming that $sUploadTmpDir is used.");
  122. }
  123. // check that the upload directory is indeed writable from PHP
  124. if (!empty($sUploadTmpDir))
  125. {
  126. if (!file_exists($sUploadTmpDir))
  127. {
  128. $oP->error("Temporary directory for files upload ($sUploadTmpDir) does not exist or cannot be read by PHP.");
  129. $bResult = false;
  130. }
  131. else if (!is_writable($sUploadTmpDir))
  132. {
  133. $oP->error("Temporary directory for files upload ($sUploadTmpDir) is not writable.");
  134. $bResult = false;
  135. }
  136. else
  137. {
  138. $oP->log("Info - Temporary directory for files upload ($sUploadTmpDir) is writable.");
  139. }
  140. }
  141. if (!ini_get('upload_max_filesize'))
  142. {
  143. $oP->error("File upload is not allowed on this server (file_uploads = ".ini_get('file_uploads').").");
  144. }
  145. $iMaxFileUploads = ini_get('max_file_uploads');
  146. if (!empty($iMaxFileUploads) && ($iMaxFileUploads < 1))
  147. {
  148. $oP->error("File upload is not allowed on this server (max_file_uploads = ".ini_get('max_file_uploads').").");
  149. $bResult = false;
  150. }
  151. $oP->log("Info - upload_max_filesize: ".ini_get('upload_max_filesize'));
  152. $oP->log("Info - max_file_uploads: ".ini_get('max_file_uploads'));
  153. // Check some more ini settings here, needed for file upload
  154. if (get_magic_quotes_gpc())
  155. {
  156. $oP->error("'magic_quotes_gpc' is set to On in '$sPhpIniFile', please turn if Off before continuing.");
  157. $bResult = false;
  158. }
  159. return $bResult;
  160. }
  161. /**
  162. * Helper function check the connection to the database and (if connected) to enumerate
  163. * the existing databases
  164. * @return Array The list of databases found in the server
  165. */
  166. function CheckServerConnection(SetupWebPage $oP, $sDBServer, $sDBUser, $sDBPwd)
  167. {
  168. $aResult = array();
  169. $oP->log('Info - CheckServerConnection');
  170. try
  171. {
  172. $oDBSource = new CMDBSource;
  173. $oDBSource->Init($sDBServer, $sDBUser, $sDBPwd);
  174. $oP->ok("Connection to '$sDBServer' as '$sDBUser' successful.");
  175. $sDBVersion = $oDBSource->GetDBVersion();
  176. if (version_compare($sDBVersion, MYSQL_MIN_VERSION, '>='))
  177. {
  178. $oP->ok("Current MySQL version ($sDBVersion), greater than minimum required version (".MYSQL_MIN_VERSION.")");
  179. // Check some server variables
  180. $iMaxAllowedPacket = $oDBSource->GetServerVariable('max_allowed_packet');
  181. $iMaxUploadSize = utils::ConvertToBytes(ini_get('upload_max_filesize'));
  182. if ($iMaxAllowedPacket >= (500 + $iMaxUploadSize)) // Allow some space for the query + the file to upload
  183. {
  184. $oP->ok("MySQL server's max_allowed_packet is big enough.");
  185. }
  186. else if($iMaxAllowedPacket < $iMaxUploadSize)
  187. {
  188. $oP->warning("MySQL server's max_allowed_packet ($iMaxAllowedPacket) is not big enough. Please, consider setting it to at least ".(500 + $iMaxUploadSize).".");
  189. }
  190. $oP->log("Info - MySQL max_allowed_packet: $iMaxAllowedPacket");
  191. $iMaxConnections = $oDBSource->GetServerVariable('max_connections');
  192. if ($iMaxConnections < 5)
  193. {
  194. $oP->warning("MySQL server's max_connections ($iMaxConnections) is not enough. Please, consider setting it to at least 5.");
  195. }
  196. $oP->log("Info - MySQL max_connections: ".($oDBSource->GetServerVariable('max_connections')));
  197. }
  198. else
  199. {
  200. $oP->error("Error: Current MySQL version is ($sDBVersion), minimum required version (".MYSQL_MIN_VERSION.")");
  201. return false;
  202. }
  203. try
  204. {
  205. $aResult = $oDBSource->ListDB();
  206. }
  207. catch(Exception $e)
  208. {
  209. $oP->warning("Warning: unable to enumerate the current databases.");
  210. $aResult = true; // Not an array to differentiate with an empty array
  211. }
  212. }
  213. catch(Exception $e)
  214. {
  215. $oP->error("Error: Connection to '$sDBServer' as '$sDBUser' failed.");
  216. $oP->p($e->GetHtmlDesc());
  217. $aResult = false;
  218. }
  219. return $aResult;
  220. }
  221. /**
  222. * Helper function to initialize the ORM and load the data model
  223. * from the given file
  224. * @param $sConfigFileName string The name of the configuration file to load
  225. * @param $bAllowMissingDatabase boolean Whether or not to allow loading a data model with no corresponding DB
  226. * @return none
  227. */
  228. function InitDataModel(SetupWebPage $oP, $sConfigFileName, $bAllowMissingDatabase = true)
  229. {
  230. require_once('../core/coreexception.class.inc.php');
  231. require_once('../core/attributedef.class.inc.php');
  232. require_once('../core/filterdef.class.inc.php');
  233. require_once('../core/stimulus.class.inc.php');
  234. require_once('../core/MyHelpers.class.inc.php');
  235. require_once('../core/expression.class.inc.php');
  236. require_once('../core/cmdbsource.class.inc.php');
  237. require_once('../core/sqlquery.class.inc.php');
  238. require_once('../core/dbobject.class.php');
  239. require_once('../core/dbobjectsearch.class.php');
  240. require_once('../core/dbobjectset.class.php');
  241. require_once('../core/userrights.class.inc.php');
  242. $oP->log("Info - MetaModel::Startup from file '$sConfigFileName' (AllowMissingDB = $bAllowMissingDatabase)");
  243. MetaModel::Startup($sConfigFileName, $bAllowMissingDatabase);
  244. }
  245. /**
  246. * Helper function to create the database structure
  247. * @return boolean true on success, false otherwise
  248. */
  249. function CreateDatabaseStructure(SetupWebPage $oP, Config $oConfig, $sDBName, $sDBPrefix)
  250. {
  251. InitDataModel($oP, TMP_CONFIG_FILE, true); // Allow the DB to NOT exist since we're about to create it !
  252. $oP->log('Info - CreateDatabaseStructure');
  253. $oP->info("Creating the structure in '$sDBName' (prefix = '$sDBPrefix').");
  254. //MetaModel::CheckDefinitions();
  255. if (!MetaModel::DBExists())
  256. {
  257. MetaModel::DBCreate();
  258. $oP->ok("Database structure created in '$sDBName' (prefix = '$sDBPrefix').");
  259. }
  260. else
  261. {
  262. $oP->error("Error: database '$sDBName' (prefix = '$sDBPrefix') already exists.");
  263. $oP->p("Tables with conflicting names already exist in the database.
  264. Try selecting another database instance or specifiy a prefix to prevent conflicting table names.");
  265. return false;
  266. }
  267. return true;
  268. }
  269. /**
  270. * Helper function to create and administrator account for iTop
  271. * @return boolean true on success, false otherwise
  272. */
  273. function CreateAdminAccount(SetupWebPage $oP, Config $oConfig, $sAdminUser, $sAdminPwd)
  274. {
  275. $oP->log('Info - CreateAdminAccount');
  276. InitDataModel($oP, TMP_CONFIG_FILE, true); // allow missing DB
  277. if (UserRights::CreateAdministrator($sAdminUser, $sAdminPwd))
  278. {
  279. $oP->ok("Administrator account '$sAdminUser' created.");
  280. return true;
  281. }
  282. else
  283. {
  284. $oP->error("Failed to create the administrator account '$sAdminUser'.");
  285. return false;
  286. }
  287. }
  288. //aFilesToLoad[aFilesToLoad.length] = './menus.xml'; // First load the menus
  289. function ListDataFiles($sDirectory, SetupWebPage $oP)
  290. {
  291. $aFilesToLoad = array();
  292. if ($hDir = @opendir($sDirectory))
  293. {
  294. // This is the correct way to loop over the directory. (according to the documentation)
  295. while (($sFile = readdir($hDir)) !== false)
  296. {
  297. $sExtension = pathinfo($sFile, PATHINFO_EXTENSION );
  298. if (strcasecmp($sExtension, 'xml') == 0)
  299. {
  300. $aFilesToLoad[] = $sDirectory.'/'.$sFile;
  301. }
  302. }
  303. closedir($hDir);
  304. // Load order is important we expect the files to be ordered
  305. // like numbered 1.Organizations.xml 2.Locations.xml , etc.
  306. asort($aFilesToLoad);
  307. }
  308. else
  309. {
  310. $oP->error("Data directory (".$sDirectory.") not found or not readable.");
  311. }
  312. return $aFilesToLoad;
  313. }
  314. /**
  315. * Scans the ./data directory for XML files and output them as a Javascript array
  316. */
  317. function PopulateDataFilesList(SetupWebPage $oP)
  318. {
  319. $oP->add("<script type=\"text/javascript\">\n");
  320. $oP->add("function PopulateDataFilesList()\n");
  321. $oP->add("{\n");
  322. // Structure data
  323. //
  324. $aStructureDataFiles = ListDataFiles(SETUP_STRUCTURE_DATA_DIR, $oP);
  325. foreach($aStructureDataFiles as $sFile)
  326. {
  327. $oP->add("aFilesToLoad[aFilesToLoad.length] = '$sFile';\n");
  328. }
  329. // Sample data - loaded IIF wished by the user
  330. //
  331. $oP->add("if (($(\"#sample_data:checked\").length == 1))");
  332. $oP->add("{");
  333. $aSampleDataFiles = ListDataFiles(SETUP_SAMPLE_DATA_DIR, $oP);
  334. foreach($aSampleDataFiles as $sFile)
  335. {
  336. $oP->add("aFilesToLoad[aFilesToLoad.length] = '$sFile';\n");
  337. }
  338. $oP->add("}\n");
  339. $oP->add("}\n");
  340. $oP->add("</script>\n");
  341. }
  342. /**
  343. * Display the form for the first step of the configuration wizard
  344. * which consists in the database server selection
  345. */
  346. function DisplayStep1(SetupWebPage $oP)
  347. {
  348. $sNextOperation = 'step2';
  349. $oP->add("<h1>iTop configuration wizard</h1>\n");
  350. $oP->add("<h2>Checking prerequisites</h2>\n");
  351. if (CheckPHPVersion($oP))
  352. {
  353. $sRedStar = '<span class="hilite">*</span>';
  354. $oP->add("<h2>Step 1: Configuration of the database connection</h2>\n");
  355. $oP->add("<form method=\"post\" onSubmit=\"return DoSubmit('Connection to the database...', 1)\">\n");
  356. // Form goes here
  357. $oP->add("<fieldset><legend>Database connection</legend>\n");
  358. $aForm = array();
  359. $aForm[] = array('label' => "Server name$sRedStar:", 'input' => "<input id=\"db_server\" type=\"text\" name=\"db_server\" value=\"\">",
  360. 'help' => 'E.g. "localhost", "dbserver.mycompany.com" or "192.142.10.23".');
  361. $aForm[] = array('label' => "User name$sRedStar:", 'input' => "<input id=\"db_user\" type=\"text\" name=\"db_user\" value=\"\">");
  362. $aForm[] = array('label' => 'Password:', 'input' => "<input id=\"db_pwd\" type=\"password\" name=\"db_pwd\" value=\"\">");
  363. $oP->form($aForm);
  364. $oP->add("</fieldset>\n");
  365. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  366. $oP->add("<button type=\"submit\">Next >></button>\n");
  367. $oP->add("</form>\n");
  368. }
  369. }
  370. /**
  371. * Display the form for the second step of the configuration wizard
  372. * which consists in
  373. * 1) Validating the parameters by connecting to the database server
  374. * 2) Prompting to select an existing database or to create a new one
  375. */
  376. function DisplayStep2(SetupWebPage $oP, Config $oConfig, $sDBServer, $sDBUser, $sDBPwd)
  377. {
  378. $sNextOperation = 'step3';
  379. $oP->add("<h1>iTop configuration wizard</h1>\n");
  380. $oP->add("<h2>Step 2: Database selection</h2>\n");
  381. $oP->add("<form method=\"post\" onSubmit=\"return DoSubmit('Creating database structure...', 2);\">\n");
  382. $aDatabases = CheckServerConnection($oP, $sDBServer, $sDBUser, $sDBPwd);
  383. if ($aDatabases === false)
  384. {
  385. // Connection failed, invalid credentials ? Go back
  386. $oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
  387. }
  388. else
  389. {
  390. // Connection is Ok, save it and continue the setup wizard
  391. $oConfig->SetDBHost($sDBServer);
  392. $oConfig->SetDBUser($sDBUser);
  393. $oConfig->SetDBPwd($sDBPwd);
  394. $oConfig->WriteToFile();
  395. $oP->add("<fieldset><legend>Specify a database<span class=\"hilite\">*</span></legend>\n");
  396. $aForm = array();
  397. if (is_array($aDatabases))
  398. {
  399. foreach($aDatabases as $sDBName)
  400. {
  401. $aForm[] = array('label' => "<input id=\"db_$sDBName\" type=\"radio\" name=\"db_name\" value=\"$sDBName\" /><label for=\"db_$sDBName\"> $sDBName</label>");
  402. }
  403. }
  404. else
  405. {
  406. $aForm[] = array('label' => "<input id=\"current_db\" type=\"radio\" name=\"db_name\" value=\"-1\" /><label for=\"current_db\"> Use the existing database:</label> <input type=\"text\" id=\"current_db_name\" name=\"current_db_name\" value=\"\" maxlength=\"32\"/>");
  407. $oP->add_ready_script("$('#current_db_name').click( function() { $('#current_db').attr('checked', true); });");
  408. }
  409. $aForm[] = array('label' => "<input id=\"new_db\" type=\"radio\" name=\"db_name\" value=\"\" /><label for=\"new_db\"> Create a new database:</label> <input type=\"text\" id=\"new_db_name\" name=\"new_db_name\" value=\"\" maxlength=\"32\"/>");
  410. $oP->form($aForm);
  411. $oP->add_ready_script("$('#new_db_name').click( function() { $('#new_db').attr('checked', true); })");
  412. $oP->add("</fieldset>\n");
  413. $aForm = array();
  414. $aForm[] = array('label' => "Add a prefix to all the tables: <input id=\"db_prefix\" type=\"text\" name=\"db_prefix\" value=\"\" maxlength=\"32\"/>");
  415. $oP->form($aForm);
  416. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  417. $oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
  418. $oP->add("&nbsp;&nbsp;&nbsp;&nbsp;\n");
  419. $oP->add("<button type=\"submit\">Next >></button>\n");
  420. }
  421. $oP->add("</form>\n");
  422. }
  423. /**
  424. * Display the form for the third step of the configuration wizard
  425. * which consists in
  426. * 1) Validating the parameters by connecting to the database server & selecting the database
  427. * 2) Creating the database structure
  428. * 3) Prompting for the admin account to be created
  429. */
  430. function DisplayStep3(SetupWebPage $oP, Config $oConfig, $sDBName, $sDBPrefix)
  431. {
  432. $sNextOperation = 'step4';
  433. $oP->add("<h1>iTop configuration wizard</h1>\n");
  434. $oP->add("<h2>Creation of the database structure</h2>\n");
  435. $oP->add("<form method=\"post\" onSubmit=\"return DoSubmit('Creating user and profiles...', 3);\">\n");
  436. $oConfig->SetDBName($sDBName);
  437. $oConfig->SetDBSubname($sDBPrefix);
  438. $oConfig->WriteToFile(TMP_CONFIG_FILE);
  439. if (CreateDatabaseStructure($oP, $oConfig, $sDBName, $sDBPrefix))
  440. {
  441. $sRedStar = "<span class=\"hilite\">*</span>";
  442. $oP->add("<h2>Step 3: Definition of the administrator account</h2>\n");
  443. // Database created, continue with admin creation
  444. $oP->add("<fieldset><legend>Administrator account</legend>\n");
  445. $aForm = array();
  446. $aForm[] = array('label' => "Login$sRedStar:", 'input' => "<input id=\"auth_user\" type=\"text\" name=\"auth_user\" value=\"\">");
  447. $aForm[] = array('label' => "Password$sRedStar:", 'input' => "<input id=\"auth_pwd\" type=\"password\" name=\"auth_pwd\" value=\"\">");
  448. $aForm[] = array('label' => "Retype password$sRedStar:", 'input' => "<input id=\"auth_pwd2\" type=\"password\" name=\"auth_pwd2\" value=\"\">");
  449. $oP->form($aForm);
  450. $oP->add("</fieldset>\n");
  451. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  452. $oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
  453. $oP->add("&nbsp;&nbsp;&nbsp;&nbsp;\n");
  454. $oP->add("<button type=\"submit\">Next >></button>\n");
  455. }
  456. else
  457. {
  458. $oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
  459. }
  460. // Form goes here
  461. $oP->add("</form>\n");
  462. }
  463. /**
  464. * Display the form for the fourth step of the configuration wizard
  465. * which consists in
  466. * 1) Creating the admin user account
  467. * 2) Prompting to load some sample data
  468. */
  469. function DisplayStep4(SetupWebPage $oP, Config $oConfig, $sAdminUser, $sAdminPwd)
  470. {
  471. $sNextOperation = 'step5';
  472. $oP->add("<h1>iTop configuration wizard</h1>\n");
  473. $oP->add("<h2>Creation of the administrator account</h2>\n");
  474. $oP->add("<form method=\"post\"\">\n");
  475. if (CreateAdminAccount($oP, $oConfig, $sAdminUser, $sAdminPwd) && UserRights::Setup())
  476. {
  477. $oP->add("<h2>Step 4: Loading of sample data</h2>\n");
  478. $oP->p("<fieldset><legend> Do you want to load sample data into the database ? </legend>\n");
  479. $oP->p("<input type=\"radio\" id=\"sample_data\" name=\"sample_data\" checked value=\"yes\"> Yes, for testing purposes, populate the database with sample data.\n");
  480. $oP->p("<input type=\"radio\" name=\"sample_data\" unchecked value=\"no\"> No, this is a production system, load only the data required by the application.\n");
  481. $oP->p("</fieldset>\n");
  482. $oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
  483. $oP->add("&nbsp;&nbsp;&nbsp;&nbsp;\n");
  484. $oP->add("<button onclick=\"DoSubmit('Finalizing configuration and loading data...', 4);\">Finish</button>\n");
  485. }
  486. else
  487. {
  488. // Creation failed
  489. $oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
  490. }
  491. // End of visible form
  492. $oP->add("</form>\n");
  493. // Hidden form
  494. $oP->add("<form id=\"GoToNextStep\" method=\"post\">\n");
  495. $oP->add("<input type=\"hidden\" name=\"auth_user\" value=\"$sAdminUser\">\n"); // To be compatible with login page
  496. $oP->add("<input type=\"hidden\" name=\"auth_pwd\" value=\"$sAdminPwd\">\n"); // To be compatible with login page
  497. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  498. $oP->add("</form>\n");
  499. $oP->add_linked_script('./jquery.progression.js');
  500. PopulateDataFilesList($oP);
  501. }
  502. /**
  503. * Display the form for the fifth (and final) step of the configuration wizard
  504. * which consists in
  505. * 1) Creating the final configuration file
  506. * 2) Prompting the user to make the file read-only
  507. */
  508. function DisplayStep5(SetupWebPage $oP, Config $oConfig, $sAuthUser, $sAuthPwd)
  509. {
  510. try
  511. {
  512. session_start();
  513. // Write the final configuration file
  514. $oConfig->WriteToFile(FINAL_CONFIG_FILE);
  515. // Start the application
  516. InitDataModel($oP, FINAL_CONFIG_FILE, false); // DO NOT allow missing DB
  517. if (UserRights::Login($sAuthUser, $sAuthPwd))
  518. {
  519. $_SESSION['auth_user'] = $sAuthUser;
  520. $_SESSION['auth_pwd'] = $sAuthPwd;
  521. // remove the tmp config file
  522. @unlink(TMP_CONFIG_FILE);
  523. // try to make the final config file read-only
  524. @chmod(FINAL_CONFIG_FILE, 0440); // Read-only for owner and group, nothing for others
  525. $oP->add("<h1>iTop configuration wizard</h1>\n");
  526. $oP->add("<h2>Configuration completed</h2>\n");
  527. $oP->add("<form method=\"get\" action=\"../index.php\">\n");
  528. $oP->ok("The initialization completed successfully.");
  529. // Form goes here
  530. $oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
  531. $oP->add("&nbsp;&nbsp;&nbsp;&nbsp;\n");
  532. $oP->add("<button type=\"submit\">Enter iTop</button>\n");
  533. $oP->add("</form>\n");
  534. }
  535. else
  536. {
  537. $oP->add("<h1>iTop configuration wizard</h1>\n");
  538. $oP->add("<h2>Step 5: Configuration completed</h2>\n");
  539. @unlink(FINAL_CONFIG_FILE); // remove the aborted config
  540. $oP->error("Error: Failed to login for user: '$sAuthUser'\n");
  541. $oP->add("<form method=\"get\" action=\"../index.php\">\n");
  542. $oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
  543. $oP->add("&nbsp;&nbsp;&nbsp;&nbsp;\n");
  544. $oP->add("</form>\n");
  545. }
  546. }
  547. catch(Exception $e)
  548. {
  549. $oP->error("Error: unable to create the configuration file.");
  550. $oP->p($e->getHtmlDesc());
  551. $oP->p("Did you forget to remove the previous (read-only) configuration file ?");
  552. }
  553. }
  554. /**
  555. * Main program
  556. */
  557. clearstatcache(); // Make sure we know what we are doing !
  558. if (file_exists(FINAL_CONFIG_FILE))
  559. {
  560. // The configuration file already exists
  561. if (is_writable(FINAL_CONFIG_FILE))
  562. {
  563. $oP->warning("<b>Warning:</b> a configuration file '".FINAL_CONFIG_FILE."' already exists, and will be overwritten.");
  564. }
  565. else
  566. {
  567. $oP->add("<h1>iTop configuration wizard</h1>\n");
  568. $oP->add("<h2>Fatal error</h2>\n");
  569. $oP->error("<b>Error:</b> the configuration file '".FINAL_CONFIG_FILE."' already exists and cannot be overwritten.");
  570. $oP->p("The wizard cannot create the configuration file for you. Please remove the file '<b>".realpath(FINAL_CONFIG_FILE)."</b>' or change its access-rights/read-only flag before continuing.");
  571. $oP->output();
  572. exit;
  573. }
  574. }
  575. else
  576. {
  577. // No configuration file yet
  578. // Check that the wizard can write into the root dir to create the configuration file
  579. if (!is_writable(dirname(FINAL_CONFIG_FILE)))
  580. {
  581. $oP->add("<h1>iTop configuration wizard</h1>\n");
  582. $oP->add("<h2>Fatal error</h2>\n");
  583. $oP->error("<b>Error:</b> the directory where to store the configuration file is not writable.");
  584. $oP->p("The wizard cannot create the configuration file for you. Please make sure that the directory '<b>".realpath(dirname(FINAL_CONFIG_FILE))."</b>' is writable for the web server.");
  585. $oP->output();
  586. exit;
  587. }
  588. }
  589. try
  590. {
  591. $oConfig = new Config(TMP_CONFIG_FILE);
  592. }
  593. catch(Exception $e)
  594. {
  595. // We'll end here when the tmp config file does not exist. It's normal
  596. $oConfig = new Config(TMP_CONFIG_FILE, false /* Don't try to load it */);
  597. }
  598. try
  599. {
  600. switch($sOperation)
  601. {
  602. case 'step1':
  603. $oP->log("Info - ========= Wizard step 1 ========");
  604. DisplayStep1($oP);
  605. break;
  606. case 'step2':
  607. $oP->no_cache();
  608. $oP->log("Info - ========= Wizard step 2 ========");
  609. $sDBServer = Utils::ReadParam('db_server');
  610. $sDBUser = Utils::ReadParam('db_user');
  611. $sDBPwd = Utils::ReadParam('db_pwd');
  612. DisplayStep2($oP, $oConfig, $sDBServer, $sDBUser, $sDBPwd);
  613. break;
  614. case 'step3':
  615. $oP->no_cache();
  616. $oP->log("Info - ========= Wizard step 3 ========");
  617. $sDBName = Utils::ReadParam('db_name');
  618. if (empty($sDBName))
  619. {
  620. $sDBName = Utils::ReadParam('new_db_name');
  621. }
  622. $sDBPrefix = Utils::ReadParam('db_prefix');
  623. DisplayStep3($oP, $oConfig, $sDBName, $sDBPrefix);
  624. break;
  625. case 'step4':
  626. $oP->no_cache();
  627. $oP->log("Info - ========= Wizard step 4 ========");
  628. $sAdminUser = Utils::ReadParam('auth_user');
  629. $sAdminPwd = Utils::ReadParam('auth_pwd');
  630. DisplayStep4($oP, $oConfig, $sAdminUser, $sAdminPwd);
  631. break;
  632. case 'step5':
  633. $oP->no_cache();
  634. $oP->log("Info - ========= Wizard step 5 ========");
  635. $sAdminUser = Utils::ReadParam('auth_user');
  636. $sAdminPwd = Utils::ReadParam('auth_pwd');
  637. DisplayStep5($oP, $oConfig, $sAdminUser, $sAdminPwd);
  638. break;
  639. default:
  640. $oP->error("Error: unsupported operation '$sOperation'");
  641. }
  642. }
  643. catch(Exception $e)
  644. {
  645. $oP->error("Error: '".$e->getMessage()."'");
  646. }
  647. catch(CoreException $e)
  648. {
  649. $oP->error("Error: '".$e->getHtmlDesc()."'");
  650. }
  651. $oP->output();
  652. ?>