index.php 24 KB

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