index.php 24 KB

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