index.php 23 KB

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