index.php 23 KB

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