index.php 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Wizard to configure and initialize the iTop application
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once('../application/utils.inc.php');
  25. require_once('../core/config.class.inc.php');
  26. require_once('../core/log.class.inc.php');
  27. require_once('../core/cmdbsource.class.inc.php');
  28. require_once('./setuppage.class.inc.php');
  29. define('TMP_CONFIG_FILE', '../tmp-config-itop.php');
  30. define('FINAL_CONFIG_FILE', '../config-itop.php');
  31. define('PHP_MIN_VERSION', '5.2.0');
  32. define('MYSQL_MIN_VERSION', '5.0.0');
  33. define('MIN_MEMORY_LIMIT', 32*1024*1024);
  34. $sOperation = Utils::ReadParam('operation', 'step0');
  35. $oP = new SetupWebPage('iTop configuration wizard');
  36. ///////////////////////////////////////////////////////////////////////////////////////////////////
  37. // Various helper function
  38. ///////////////////////////////////////////////////////////////////////////////////////////////////
  39. /**
  40. * Get a nicely formatted version string
  41. */
  42. function GetITopVersion($bShort = true)
  43. {
  44. $sVersionString = '';
  45. if ($bShort)
  46. {
  47. $sVersionString = "iTop Version ".ITOP_VERSION;
  48. }
  49. else
  50. {
  51. if (ITOP_REVISION == '$WCREV$')
  52. {
  53. // This is NOT a version built using the buil system, just display the main version
  54. $sVersionString = "iTop Version ".ITOP_VERSION;
  55. }
  56. else
  57. {
  58. // This is a build made from SVN, let display the full information
  59. $sVersionString = "iTop Version ".ITOP_VERSION." revision ".ITOP_REVISION.", built on: ".ITOP_BUILD_DATE;
  60. }
  61. }
  62. return $sVersionString;
  63. }
  64. /**
  65. * Helper function to retrieve the system's temporary directory
  66. * Emulates sys_get_temp_dir if neeed (PHP < 5.2.1)
  67. * @return string Path to the system's temp directory
  68. */
  69. function GetTmpDir()
  70. {
  71. // try to figure out what is the temporary directory
  72. // prior to PHP 5.2.1 the function sys_get_temp_dir
  73. // did not exist
  74. if ( !function_exists('sys_get_temp_dir'))
  75. {
  76. if( $temp=getenv('TMP') ) return realpath($temp);
  77. if( $temp=getenv('TEMP') ) return realpath($temp);
  78. if( $temp=getenv('TMPDIR') ) return realpath($temp);
  79. $temp=tempnam(__FILE__,'');
  80. if (file_exists($temp))
  81. {
  82. unlink($temp);
  83. return realpath(dirname($temp));
  84. }
  85. return null;
  86. }
  87. else
  88. {
  89. return realpath(sys_get_temp_dir());
  90. }
  91. }
  92. /**
  93. * Check the value of the PHP setting 'memory_limit'
  94. * against the minimum recommended value
  95. * @param SetpWebPage $oP The current web page
  96. * @param integer $iMinMemoryRequired The minimum memory for the test to pass
  97. * @return boolean Whether or not it's Ok to continue
  98. */
  99. function CheckMemoryLimit(SetupWebPage $oP, $iMinMemoryRequired)
  100. {
  101. $sMemoryLimit = trim(ini_get('memory_limit'));
  102. $bResult = true;
  103. if (empty($sMemoryLimit))
  104. {
  105. // On some PHP installations, memory_limit does not exist as a PHP setting!
  106. // (encountered on a 5.2.0 under Windows)
  107. // In that case, ini_set will not work, let's keep track of this and proceed anyway
  108. $oP->warning("No memory limit has been defined in this instance of PHP");
  109. }
  110. else
  111. {
  112. // Check that the limit will allow us to load the data
  113. //
  114. $iMemoryLimit = utils::ConvertToBytes($sMemoryLimit);
  115. if ($iMemoryLimit < $iMinMemoryRequired)
  116. {
  117. $oP->error("memory_limit ($iMemoryLimit) is too small, the minimum value to run iTop is $iMinMemoryRequired.");
  118. $bResult = false;
  119. }
  120. else
  121. {
  122. $oP->log_info("memory_limit is $iMemoryLimit, ok.");
  123. }
  124. }
  125. return $bResult;
  126. }
  127. /**
  128. * Helper function to retrieve the directory where files are to be uploaded
  129. * @return string Path to the temp directory used for uploading files
  130. */
  131. function GetUploadTmpDir()
  132. {
  133. $sPath = ini_get('upload_tmp_dir');
  134. if (empty($sPath))
  135. {
  136. $sPath = GetTmpDir();
  137. }
  138. return $sPath;
  139. }
  140. /**
  141. * Helper function to check if the current version of PHP
  142. * is compatible with the application
  143. * @return boolean true if this is Ok, false otherwise
  144. */
  145. function CheckPHPVersion(SetupWebPage $oP)
  146. {
  147. $bResult = true;
  148. $oP->log('Info - CheckPHPVersion');
  149. if (version_compare(phpversion(), PHP_MIN_VERSION, '>='))
  150. {
  151. $oP->ok("The current PHP Version (".phpversion().") is greater than the minimum required version (".PHP_MIN_VERSION.")");
  152. }
  153. else
  154. {
  155. $oP->error("Error: The current PHP Version (".phpversion().") is lower than the minimum required version (".PHP_MIN_VERSION.")");
  156. return false;
  157. }
  158. $aMandatoryExtensions = array('mysql', 'iconv', 'simplexml', 'soap');
  159. asort($aMandatoryExtensions); // Sort the list to look clean !
  160. $aExtensionsOk = array();
  161. $aMissingExtensions = array();
  162. $aMissingExtensionsLinks = array();
  163. foreach($aMandatoryExtensions as $sExtension)
  164. {
  165. if (extension_loaded($sExtension))
  166. {
  167. $aExtensionsOk[] = $sExtension;
  168. }
  169. else
  170. {
  171. $aMissingExtensions[] = $sExtension;
  172. $aMissingExtensionsLinks[] = "<a href=\"http://www.php.net/manual/en/book.$sExtension.php\">$sExtension</a>";
  173. }
  174. }
  175. if (count($aExtensionsOk) > 0)
  176. {
  177. $oP->ok("Required PHP extension(s): ".implode(', ', $aExtensionsOk).".");
  178. }
  179. if (count($aMissingExtensions) > 0)
  180. {
  181. $oP->error("Missing PHP extension(s): ".implode(', ', $aMissingExtensionsLinks).".");
  182. $bResult = false;
  183. }
  184. // Check some ini settings here
  185. if (function_exists('php_ini_loaded_file')) // PHP >= 5.2.4
  186. {
  187. $sPhpIniFile = php_ini_loaded_file();
  188. // Other included/scanned files
  189. if ($sFileList = php_ini_scanned_files())
  190. {
  191. if (strlen($sFileList) > 0)
  192. {
  193. $aFiles = explode(',', $sFileList);
  194. foreach ($aFiles as $sFile)
  195. {
  196. $sPhpIniFile .= ', '.trim($sFile);
  197. }
  198. }
  199. }
  200. $oP->log("Info - php.ini file(s): '$sPhpIniFile'");
  201. }
  202. else
  203. {
  204. $sPhpIniFile = 'php.ini';
  205. }
  206. if (!ini_get('file_uploads'))
  207. {
  208. $oP->error("Files upload is not allowed on this server (file_uploads = ".ini_get('file_uploads').").");
  209. $bResult = false;
  210. }
  211. $sUploadTmpDir = GetUploadTmpDir();
  212. if (empty($sUploadTmpDir))
  213. {
  214. $sUploadTmpDir = '/tmp';
  215. $oP->warning("Temporary directory for files upload is not defined (upload_tmp_dir), assuming that $sUploadTmpDir is used.");
  216. }
  217. // check that the upload directory is indeed writable from PHP
  218. if (!empty($sUploadTmpDir))
  219. {
  220. if (!file_exists($sUploadTmpDir))
  221. {
  222. $oP->error("Temporary directory for files upload ($sUploadTmpDir) does not exist or cannot be read by PHP.");
  223. $bResult = false;
  224. }
  225. else if (!is_writable($sUploadTmpDir))
  226. {
  227. $oP->error("Temporary directory for files upload ($sUploadTmpDir) is not writable.");
  228. $bResult = false;
  229. }
  230. else
  231. {
  232. $oP->log("Info - Temporary directory for files upload ($sUploadTmpDir) is writable.");
  233. }
  234. }
  235. if (!ini_get('upload_max_filesize'))
  236. {
  237. $oP->error("File upload is not allowed on this server (file_uploads = ".ini_get('file_uploads').").");
  238. }
  239. $iMaxFileUploads = ini_get('max_file_uploads');
  240. if (!empty($iMaxFileUploads) && ($iMaxFileUploads < 1))
  241. {
  242. $oP->error("File upload is not allowed on this server (max_file_uploads = ".ini_get('max_file_uploads').").");
  243. $bResult = false;
  244. }
  245. $oP->log("Info - upload_max_filesize: ".ini_get('upload_max_filesize'));
  246. $oP->log("Info - max_file_uploads: ".ini_get('max_file_uploads'));
  247. // Check some more ini settings here, needed for file upload
  248. if (get_magic_quotes_gpc())
  249. {
  250. $oP->error("'magic_quotes_gpc' is set to On. Please turn it Off before continuing. You may want to check the PHP configuration file(s): '$sPhpIniFile'. Be aware that this setting can also be overridden in the apache configuration.");
  251. $bResult = false;
  252. }
  253. $bResult = $bResult & CheckMemoryLimit($oP, MIN_MEMORY_LIMIT);
  254. return $bResult;
  255. }
  256. /**
  257. * Helper function check the connection to the database and (if connected) to enumerate
  258. * the existing databases
  259. * @return Array The list of databases found in the server
  260. */
  261. function CheckServerConnection(SetupWebPage $oP, $sDBServer, $sDBUser, $sDBPwd)
  262. {
  263. $aResult = array();
  264. $oP->log('Info - CheckServerConnection');
  265. try
  266. {
  267. $oDBSource = new CMDBSource;
  268. $oDBSource->Init($sDBServer, $sDBUser, $sDBPwd);
  269. $oP->ok("Connection to '$sDBServer' as '$sDBUser' successful.");
  270. $oP->log("Info - User privileges: ".($oDBSource->GetRawPrivileges()));
  271. $sDBVersion = $oDBSource->GetDBVersion();
  272. if (version_compare($sDBVersion, MYSQL_MIN_VERSION, '>='))
  273. {
  274. $oP->ok("Current MySQL version ($sDBVersion), greater than minimum required version (".MYSQL_MIN_VERSION.")");
  275. // Check some server variables
  276. $iMaxAllowedPacket = $oDBSource->GetServerVariable('max_allowed_packet');
  277. $iMaxUploadSize = utils::ConvertToBytes(ini_get('upload_max_filesize'));
  278. if ($iMaxAllowedPacket >= (500 + $iMaxUploadSize)) // Allow some space for the query + the file to upload
  279. {
  280. $oP->ok("MySQL server's max_allowed_packet is big enough.");
  281. }
  282. else if($iMaxAllowedPacket < $iMaxUploadSize)
  283. {
  284. $oP->warning("MySQL server's max_allowed_packet ($iMaxAllowedPacket) is not big enough. Please, consider setting it to at least ".(500 + $iMaxUploadSize).".");
  285. }
  286. $oP->log("Info - MySQL max_allowed_packet: $iMaxAllowedPacket");
  287. $iMaxConnections = $oDBSource->GetServerVariable('max_connections');
  288. if ($iMaxConnections < 5)
  289. {
  290. $oP->warning("MySQL server's max_connections ($iMaxConnections) is not enough. Please, consider setting it to at least 5.");
  291. }
  292. $oP->log("Info - MySQL max_connections: ".($oDBSource->GetServerVariable('max_connections')));
  293. }
  294. else
  295. {
  296. $oP->error("Error: Current MySQL version is ($sDBVersion), minimum required version (".MYSQL_MIN_VERSION.")");
  297. return false;
  298. }
  299. try
  300. {
  301. $aResult = $oDBSource->ListDB();
  302. }
  303. catch(Exception $e)
  304. {
  305. $oP->warning("Warning: unable to enumerate the current databases.");
  306. $aResult = true; // Not an array to differentiate with an empty array
  307. }
  308. }
  309. catch(Exception $e)
  310. {
  311. $oP->error("Error: Connection to '$sDBServer' as '$sDBUser' failed.");
  312. $oP->p($e->GetHtmlDesc());
  313. $aResult = false;
  314. }
  315. return $aResult;
  316. }
  317. /**
  318. * Helper function to initialize the ORM and load the data model
  319. * from the given file
  320. * @param $sConfigFileName string The name of the configuration file to load
  321. * @param $bModelOnly boolean Whether or not to allow loading a data model with no corresponding DB
  322. * @return none
  323. */
  324. function InitDataModel(SetupWebPage $oP, $sConfigFileName, $bModelOnly = true)
  325. {
  326. require_once('../core/log.class.inc.php');
  327. require_once('../core/coreexception.class.inc.php');
  328. require_once('../core/dict.class.inc.php');
  329. require_once('../core/attributedef.class.inc.php');
  330. require_once('../core/filterdef.class.inc.php');
  331. require_once('../core/stimulus.class.inc.php');
  332. require_once('../core/MyHelpers.class.inc.php');
  333. require_once('../core/expression.class.inc.php');
  334. require_once('../core/cmdbsource.class.inc.php');
  335. require_once('../core/sqlquery.class.inc.php');
  336. require_once('../core/dbobject.class.php');
  337. require_once('../core/dbobjectsearch.class.php');
  338. require_once('../core/dbobjectset.class.php');
  339. require_once('../application/cmdbabstract.class.inc.php');
  340. require_once('../core/userrights.class.inc.php');
  341. $oP->log("Info - MetaModel::Startup from file '$sConfigFileName' (ModelOnly = $bModelOnly)");
  342. MetaModel::Startup($sConfigFileName, $bModelOnly);
  343. }
  344. /**
  345. * Helper function to create the database structure
  346. * @return boolean true on success, false otherwise
  347. */
  348. function CreateDatabaseStructure(SetupWebPage $oP, Config $oConfig, $sDBName, $sDBPrefix)
  349. {
  350. InitDataModel($oP, TMP_CONFIG_FILE, true); // Allow the DB to NOT exist since we're about to create it !
  351. $oP->log('Info - CreateDatabaseStructure');
  352. if (strlen($sDBPrefix) > 0)
  353. {
  354. $oP->info("Creating the structure in '$sDBName' (table names prefixed by '$sDBPrefix').");
  355. }
  356. else
  357. {
  358. $oP->info("Creating the structure in '$sDBName'.");
  359. }
  360. //MetaModel::CheckDefinitions();
  361. if (!MetaModel::DBExists(/* bMustBeComplete */ false))
  362. {
  363. MetaModel::DBCreate();
  364. $oP->ok("Database structure successfully created.");
  365. }
  366. else
  367. {
  368. if (strlen($sDBPrefix) > 0)
  369. {
  370. $oP->error("Error: found iTop tables into the database '$sDBName' (prefix: '$sDBPrefix'). Please, try selecting another database instance or specify another prefix to prevent conflicting table names.");
  371. }
  372. else
  373. {
  374. $oP->error("Error: found iTop tables into the database '$sDBName'. Please, try selecting another database instance or specify a prefix to prevent conflicting table names.");
  375. }
  376. return false;
  377. }
  378. return true;
  379. }
  380. /**
  381. * Helper function to create and administrator account for iTop
  382. * @return boolean true on success, false otherwise
  383. */
  384. function CreateAdminAccount(SetupWebPage $oP, Config $oConfig, $sAdminUser, $sAdminPwd, $sLanguage)
  385. {
  386. $oP->log('Info - CreateAdminAccount');
  387. InitDataModel($oP, TMP_CONFIG_FILE, false); // load data model and connect to the database
  388. if (UserRights::CreateAdministrator($sAdminUser, $sAdminPwd, $sLanguage))
  389. {
  390. $oP->ok("Administrator account '$sAdminUser' created.");
  391. return true;
  392. }
  393. else
  394. {
  395. $oP->error("Failed to create the administrator account '$sAdminUser'.");
  396. return false;
  397. }
  398. }
  399. function ListModuleFiles($sDirectory, SetupWebPage $oP)
  400. {
  401. //echo "<p>$sDirectory</p>\n";
  402. if ($hDir = opendir($sDirectory))
  403. {
  404. // This is the correct way to loop over the directory. (according to the documentation)
  405. while (($sFile = readdir($hDir)) !== false)
  406. {
  407. $aMatches = array();
  408. if (is_dir($sDirectory.'/'.$sFile))
  409. {
  410. if (($sFile != '.') && ($sFile != '..') && ($sFile != '.svn'))
  411. {
  412. ListModuleFiles($sDirectory.'/'.$sFile, $oP);
  413. }
  414. }
  415. else if (preg_match('/^module\.(.*).php$/i', $sFile, $aMatches))
  416. {
  417. try
  418. {
  419. //echo "<p>Loading: $sDirectory/$sFile...</p>\n";
  420. require_once($sDirectory.'/'.$sFile);
  421. //echo "<p>Done.</p>\n";
  422. }
  423. catch(Exception $e)
  424. {
  425. // Continue...
  426. }
  427. }
  428. }
  429. closedir($hDir);
  430. }
  431. else
  432. {
  433. $oP->error("Data directory (".$sDirectory.") not found or not readable.");
  434. }
  435. }
  436. /**
  437. * Scans the ./data directory for XML files and output them as a Javascript array
  438. */
  439. function PopulateDataFilesList(SetupWebPage $oP, $aParamValues)
  440. {
  441. $oP->add("<script type=\"text/javascript\">\n");
  442. $oP->add("function PopulateDataFilesList()\n");
  443. $oP->add("{\n");
  444. $oP->add("if (aFilesToLoad.length > 0) return;"); // Populate the list only once...
  445. $aAvailableModules = GetAvailableModules($oP);
  446. $aStructureDataFiles = array();
  447. $aSampleDataFiles = array();
  448. foreach($aParamValues['module'] as $sModuleId)
  449. {
  450. $aModuleStruct = $aAvailableModules[$sModuleId]['data.struct'];
  451. $aModuleSamples = $aAvailableModules[$sModuleId]['data.sample'];
  452. $aStructureDataFiles = array_merge($aStructureDataFiles, $aModuleStruct);
  453. $aSampleDataFiles = array_merge($aSampleDataFiles, $aModuleSamples);
  454. }
  455. // Structure data
  456. //
  457. foreach($aStructureDataFiles as $sFile)
  458. {
  459. // Under Windows, it is a must to escape backslashes (not an issue until a folder name starts with t or n, etc...)
  460. $sFile = str_replace('\\', '\\\\', $sFile);
  461. $oP->add("aFilesToLoad[aFilesToLoad.length] = '$sFile';\n");
  462. }
  463. // Sample data - loaded IIF wished by the user
  464. //
  465. $oP->add("if (($(\"#sample_data:checked\").length == 1))");
  466. $oP->add("{");
  467. foreach($aSampleDataFiles as $sFile)
  468. {
  469. // Under Windows, it is a must to escape backslashes (not an issue until a folder name starts with t or n, etc...)
  470. $sFile = str_replace('\\', '\\\\', $sFile);
  471. $oP->add("aFilesToLoad[aFilesToLoad.length] = '$sFile';\n");
  472. }
  473. $oP->add("}\n");
  474. $oP->add("}\n");
  475. $oP->add("</script>\n");
  476. }
  477. /**
  478. * Add some parameters as hidden inputs into a form
  479. * @param SetupWebpage $oP The page to insert the form elements into
  480. * @param Hash $aParamValues The pairs name/value to be stored in the form
  481. * @param Array $aExcludeParams A list of parameters to exclude from the previous hash
  482. */
  483. function AddParamsToForm(SetupWebpage $oP, $aParamValues, $aExcludeParams = array())
  484. {
  485. foreach($aParamValues as $sName => $value)
  486. {
  487. if(!in_array($sName, $aExcludeParams))
  488. {
  489. if (is_array($value))
  490. {
  491. foreach($value as $sKey => $sItem)
  492. {
  493. $oP->add('<input type="hidden" name="'.$sName.'['.$sKey.']'.'" value="'.$sItem.'">');
  494. }
  495. }
  496. else
  497. {
  498. $oP->add('<input type="hidden" name="'.$sName.'" value="'.$value.'">');
  499. }
  500. }
  501. }
  502. }
  503. /**
  504. * Search (on the disk) for all defined iTop modules, load them and returns the list (as an array)
  505. * of the possible iTop modules to install
  506. * @param none
  507. * @return Hash A big array moduleID => ModuleData
  508. */
  509. function GetAvailableModules(SetupWebpage $oP)
  510. {
  511. clearstatcache();
  512. ListModuleFiles('../modules/', $oP);
  513. return $oP->GetModules();
  514. }
  515. /**
  516. * Build the config file from the parameters (especially the selected modules)
  517. */
  518. function BuildConfig(SetupWebpage $oP, Config &$oConfig, $aParamValues)
  519. {
  520. $aAvailableModules = GetAvailableModules($oP);
  521. // Initialize the arrays below with default values for the application...
  522. $aAddOns = $oConfig->GetAddOns();
  523. $aAppModules = $oConfig->GetAppModules();
  524. $aDataModels = $oConfig->GetDataModels();
  525. $sDictionaries = $oConfig->GetDictionaries();
  526. // Merge the values with the ones provided by the modules
  527. // Make sure when don't load the same file twice...
  528. foreach($aParamValues['module'] as $sModuleId)
  529. {
  530. $oP->log('Installed iTop module: '. $sModuleId);
  531. $aDataModels = array_unique(array_merge($aDataModels, $aAvailableModules[$sModuleId]['datamodel']));
  532. $sDictionaries = array_unique(array_merge($sDictionaries, $aAvailableModules[$sModuleId]['dictionary']));
  533. foreach($aAvailableModules[$sModuleId]['settings'] as $sProperty => $value)
  534. {
  535. $oConfig->SetModuleSetting($sModuleId, $sProperty, $value);
  536. }
  537. }
  538. $oConfig->SetAddOns($aAddOns);
  539. $oConfig->SetAppModules($aAppModules);
  540. $oConfig->SetDataModels($aDataModels);
  541. $oConfig->SetDictionaries($sDictionaries);
  542. }
  543. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  544. // Handling of the different steps of the setup wizard
  545. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  546. /**
  547. * Displays the welcome screen and check some basic prerequisites
  548. */
  549. function WelcomeAndCheckPrerequisites(SetupWebPage $oP, $aParamValues, $iCurrentStep)
  550. {
  551. $sNextOperation = 'step'.($iCurrentStep+1);
  552. $oP->add("<h1>iTop configuration wizard</h1>\n");
  553. $sVersionStringShort = GetITopVersion(true);
  554. $sVersionStringLong = GetITopVersion(false);
  555. $oP->set_title('Welcome to '.$sVersionStringShort);
  556. $oP->log($sVersionStringLong);
  557. $oP->add("<h2>Checking prerequisites</h2>\n");
  558. if (CheckPHPVersion($oP))
  559. {
  560. $oP->add("<h2 class=\"next\">Next: Licence agreement</h2>\n");
  561. $oP->add("<form id=\"theForm\" method=\"post\" onSubmit=\"return DoSubmit('', 0)\">\n");
  562. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  563. AddParamsToForm($oP, $aParamValues);
  564. $oP->add("<table style=\"width:100%\"><tr>\n");
  565. $oP->add("<td style=\"text-align:right;\"><button type=\"submit\" type=\"submit\">Next >></button></td>\n");
  566. $oP->add("</tr></table>\n");
  567. $oP->add("</form>\n");
  568. }
  569. }
  570. function LicenceAcknowledgement($oP, $aParamValues, $iCurrentStep)
  571. {
  572. $sNextOperation = 'step'.($iCurrentStep+1);
  573. $oP->set_title('License Agreement');
  574. $oP->add('<h2>iTop is released by <a href="http://www.combodo.com">Combodo SARL</a> under the terms of the GPL V3 license. In order to use iTop you must accept the terms of this license.</h2>');
  575. $oP->add("<iframe style=\"width: 100%; height: 350px; overflow-y:auto; font-size:0.8em;\" src=\"./licence.html\">Next: Database server selection</iframe>\n");
  576. $oP->add("<form id=\"theForm\" method=\"post\">\n");
  577. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  578. AddParamsToForm($oP, $aParamValues, array('licence_ok'));
  579. $sChecked = $aParamValues['licence_ok'] == 1 ? 'checked' : '';
  580. $oP->add("<h2><input id=\"licence_ok\" type=\"checkbox\" name=\"licence_ok\" value=\"1\" $sChecked><label for=\"licence_ok\">I accept the terms of this licence agreement</label></h2>\n");
  581. $oP->add("<h2 class=\"next\">Next: Database server selection</h2>\n");
  582. $oP->add("<table style=\"width:100%\"><tr>\n");
  583. $oP->add("<td style=\"text-align:left;\"><button type=\"button\" onClick=\"return DoGoBack($iCurrentStep)\"><< Back</button></td>\n");
  584. $oP->add("<td style=\"text-align:right;\"><button type=\"submit\" onClick=\"return DoSubmit('', $iCurrentStep)\">Next >></button></td>\n");
  585. $oP->add("</tr></table>\n");
  586. $oP->add("</form>\n");
  587. }
  588. /**
  589. * Display the form for the first step of the configuration wizard
  590. * which consists in the database server selection
  591. */
  592. function DatabaseServerSelection(SetupWebPage $oP, $aParamValues, $iCurrentStep)
  593. {
  594. $sNextOperation = 'step'.($iCurrentStep+1);
  595. $oP->add("<form id=\"theForm\" method=\"post\">\n");
  596. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  597. AddParamsToForm($oP, $aParamValues, array('db_server', 'db_user', 'db_pwd'));
  598. if ($aParamValues['licence_ok'] == 1)
  599. {
  600. $sRedStar = '<span class="hilite">*</span>';
  601. $oP->set_title("Configuration of the database connection\n");
  602. $oP->add("<h2>Please enter the name of the MySQL database server you want to use for iTop and supply valid credentials to connect to it</h2>\n");
  603. // Form goes here
  604. $oP->add("<fieldset><legend>Database connection</legend>\n");
  605. $aForm = array();
  606. $aForm[] = array('label' => "Server name$sRedStar:", 'input' => "<input id=\"db_server\" type=\"text\" name=\"db_server\" value=\"{$aParamValues['db_server']}\">",
  607. 'help' => 'E.g. "localhost", "dbserver.mycompany.com" or "192.142.10.23"');
  608. $aForm[] = array('label' => "User name$sRedStar:", 'input' => "<input id=\"db_user\" type=\"text\" name=\"db_user\" value=\"{$aParamValues['db_user']}\">",
  609. 'help' => 'The account must have the following privileges: SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER');
  610. $aForm[] = array('label' => 'Password:', 'input' => "<input id=\"db_pwd\" type=\"password\" name=\"db_pwd\" value=\"{$aParamValues['db_pwd']}\">");
  611. $oP->form($aForm);
  612. $oP->add("</fieldset>\n");
  613. $oP->add("<h2 class=\"next\">Next: Database Instance Selection</h2>\n");
  614. $oP->add("<table style=\"width:100%\"><tr>\n");
  615. $oP->add("<td style=\"text-align:left;\"><button type=\"button\" onClick=\"return DoGoBack($iCurrentStep)\"><< Back</button></td>\n");
  616. $oP->add("<td style=\"text-align:right;\"><button type=\"submit\" onClick=\"return DoSubmit('Connecting to the database...', $iCurrentStep);\">Next >></button></td>\n");
  617. $oP->add("</tr></table>\n");
  618. }
  619. else
  620. {
  621. $oP->add("<button type=\"button\" onClick=\"return DoGoBack($iCurrentStep);\"><< Back</button>\n");
  622. }
  623. $oP->add("</form>\n");
  624. }
  625. /**
  626. * Display the form for the second step of the configuration wizard
  627. * which consists in
  628. * 1) Validating the parameters by connecting to the database server
  629. * 2) Prompting to select an existing database or to create a new one
  630. */
  631. function DatabaseInstanceSelection(SetupWebPage $oP, $aParamValues, $iCurrentStep, $oConfig)
  632. {
  633. $sNextOperation = 'step'.($iCurrentStep+1);
  634. $oP->set_title("Database Instance Selection\n");
  635. $oP->add("<form id=\"theForm\" method=\"post\">\n");
  636. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  637. AddParamsToForm($oP, $aParamValues, array('db_name', 'db_prefix', 'new_db_name'));
  638. $sDBServer = $aParamValues['db_server'];
  639. $sDBUser = $aParamValues['db_user'];
  640. $sDBPwd = $aParamValues['db_pwd'];
  641. $aDatabases = CheckServerConnection($oP, $sDBServer, $sDBUser, $sDBPwd);
  642. if ($aDatabases === false)
  643. {
  644. // Connection failed, invalid credentials ? Go back
  645. $oP->add("<button type=\"button\" onClick=\"return DoGoBack($iCurrentStep);\"><< Back</button>\n");
  646. }
  647. else
  648. {
  649. // Connection is Ok, save it and continue the setup wizard
  650. $oConfig->SetDBHost($sDBServer);
  651. $oConfig->SetDBUser($sDBUser);
  652. $oConfig->SetDBPwd($sDBPwd);
  653. $oConfig->WriteToFile();
  654. $oP->add("<fieldset><legend>Select the database instance to use for iTop<span class=\"hilite\">*</span></legend>\n");
  655. $aForm = array();
  656. $bExistingChecked = false;
  657. if (is_array($aDatabases))
  658. {
  659. foreach($aDatabases as $sDBName)
  660. {
  661. $sChecked = '';
  662. if ($aParamValues['db_name'] == $sDBName)
  663. {
  664. $sChecked = 'checked';
  665. $bExistingChecked = true;
  666. }
  667. $aForm[] = array('label' => "<input id=\"db_$sDBName\" type=\"radio\" name=\"db_name\" value=\"$sDBName\" $sChecked/><label for=\"db_$sDBName\"> $sDBName</label>");
  668. }
  669. }
  670. else
  671. {
  672. $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\"/>");
  673. $oP->add_ready_script("$('#current_db_name').click( function() { $('#current_db').attr('checked', true); });");
  674. }
  675. $sChecked = '';
  676. $sDBName = '';
  677. // If the 'Create Database' option was checked... and the database still does not exist
  678. if (!$bExistingChecked && !empty($aParamValues['new_db_name']))
  679. {
  680. $sChecked = 'checked';
  681. $sDBName = $aParamValues['new_db_name'];
  682. }
  683. $aForm[] = array('label' => "<input id=\"new_db\" type=\"radio\" name=\"db_name\" value=\"\" $sChecked/><label for=\"new_db\"> Create a new database:</label> <input type=\"text\" id=\"new_db_name\" name=\"new_db_name\" value=\"$sDBName\" maxlength=\"32\"/>");
  684. $oP->form($aForm);
  685. $oP->add_ready_script("$('#new_db_name').click( function() { $('#new_db').attr('checked', true); })");
  686. $oP->add("</fieldset>\n");
  687. $aForm = array();
  688. $aForm[] = array('label' => "Add a prefix to all the tables: <input id=\"db_prefix\" type=\"text\" name=\"db_prefix\" value=\"{$aParamValues['db_prefix']}\" maxlength=\"32\"/>");
  689. $oP->form($aForm);
  690. $oP->add("<h2 class=\"next\">Next: iTop Modules Selection</h2>\n");
  691. $oP->add("<table style=\"width:100%\"><tr>\n");
  692. $oP->add("<td style=\"text-align:left;\"><button type=\"button\" onClick=\"return DoGoBack($iCurrentStep)\"><< Back</button></td>\n");
  693. $oP->add("<td style=\"text-align:right;\"><button type=\"submit\" onClick=\"return DoSubmit('', $iCurrentStep);\">Next >></button></td>\n");
  694. $oP->add("</tr></table>\n");
  695. }
  696. $oP->add("</form>\n");
  697. }
  698. /**
  699. * Display the form to select the iTop modules to be installed
  700. */
  701. function ModulesSelection(SetupWebPage $oP, $aParamValues, $iCurrentStep, $oConfig)
  702. {
  703. $sNextOperation = 'step'.($iCurrentStep+1);
  704. $sPrevOperation = 'step'.($iCurrentStep-1);
  705. $sDBName = $aParamValues['db_name'];
  706. if ($sDBName == '')
  707. {
  708. $sDBName = $aParamValues['new_db_name'];
  709. }
  710. $sDBPrefix = $aParamValues['db_prefix'];
  711. $oConfig->SetDBName($sDBName);
  712. $oConfig->SetDBSubname($sDBPrefix);
  713. $oConfig->WriteToFile(TMP_CONFIG_FILE);
  714. $oP->add("<form id=\"theForm\" method=\"post\">\n");
  715. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  716. AddParamsToForm($oP, $aParamValues, array('module'));
  717. $sRedStar = '<span class="hilite">*</span>';
  718. $oP->set_title("Selection of the iTop Modules\n");
  719. $oP->add("<h2>Customize your iTop installation to fit your needs</h2>\n");
  720. $aAvailableModules = GetAvailableModules($oP);
  721. // Form goes here
  722. $oP->add("<fieldset><legend>Select the iTop modules you want to install:</legend>\n");
  723. $oP->add("<div style=\"border: 0;width:100%; height: 350px; overflow-y:auto;\">");
  724. $sRedStar = '<span class="hilite">*</span>';
  725. $index = 0;
  726. $aSelectedModules = $aParamValues['module'];
  727. if ($aSelectedModules == '')
  728. {
  729. // Make sure it gets initialized as an array, default value: all modules selected !
  730. $aSelectedModules = array();
  731. foreach($aAvailableModules as $sModuleId => $aModule)
  732. {
  733. $aSelectedModules[] = $sModuleId;
  734. }
  735. }
  736. foreach($aAvailableModules as $sModuleId => $aModule)
  737. {
  738. $sModuleLabel = $aModule['label'];
  739. $sModuleHelp = $aModule['doc.more_information'];
  740. $sClass = ($aModule['mandatory']) ? 'class="read-only"' : '';
  741. $sChecked = ($aModule['mandatory'] || in_array($sModuleId, $aSelectedModules) ) ? 'checked' : '';
  742. $sMoreInfo = (!empty($aModule['doc.more_information'])) ? "<a href=\"{$aModule['doc.more_information']}\" target=\"_blank\">more info</a>": '';
  743. if ($aModule['category'] == 'authentication')
  744. {
  745. // For now authentication modules are always on and hidden
  746. $oP->add("<input type=\"hidden\" id=\"module[$index]\" name=\"module[$index]\" value=\"$sModuleId\">\n");
  747. $index++;
  748. }
  749. elseif ($aModule['visible'])
  750. {
  751. $oP->add("<p><input type=\"checkbox\" $sClass $sChecked id=\"module[$index]\" name=\"module[$index]\" value=\"$sModuleId\"><label $sClass for=\"module[$index]\"> {$aModule['label']}</label> $sMoreInfo</p>\n");
  752. $index++;
  753. }
  754. else
  755. {
  756. // For now hidden modules are always on !
  757. $oP->add("<input type=\"hidden\" id=\"module[$index]\" name=\"module[$index]\" value=\"$sModuleId\">\n");
  758. $index++;
  759. }
  760. }
  761. $oP->add("</div>");
  762. $oP->add("</fieldset>\n");
  763. $oP->add("<h2 class=\"next\">Next: Administrator Account Creation</h2>\n");
  764. $oP->add("<table style=\"width:100%\"><tr>\n");
  765. $oP->add("<td style=\"text-align:left;\"><button type=\"button\" onClick=\"return DoGoBack($iCurrentStep)\"><< Back</button></td>\n");
  766. $oP->add("<td style=\"text-align:right;\"><button type=\"submit\" onClick=\"return DoSubmit('Creating the database structure...', $iCurrentStep);\">Next >></button></td>\n");
  767. $oP->add("</tr></table>\n");
  768. $oP->add("</form>\n");
  769. $oP->add_ready_script("$('.read-only').click( function() { $(this).attr('checked','checked'); } );");
  770. }
  771. /**
  772. * Display the form for the third step of the configuration wizard
  773. * which consists in
  774. * 1) Validating the parameters by connecting to the database server & selecting the database
  775. * 2) Creating the database structure
  776. * 3) Prompting for the admin account to be created
  777. */
  778. function AdminAccountDefinition(SetupWebPage $oP, $aParamValues, $iCurrentStep, Config $oConfig)
  779. {
  780. $sNextOperation = 'step'.($iCurrentStep+1);
  781. $oP->set_title("Configuration of the admin account");
  782. $oP->add("<h2>Creation of the database structure</h2>");
  783. $oP->add("<form id=\"theForm\" method=\"post\">\n");
  784. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  785. AddParamsToForm($oP, $aParamValues, array('auth_user', 'auth_pwd', 'language'));
  786. $sDBName = $aParamValues['db_name'];
  787. if ($sDBName == '')
  788. {
  789. $sDBName = $aParamValues['new_db_name'];
  790. }
  791. $sDBPrefix = $aParamValues['db_prefix'];
  792. $oConfig->SetDBName($sDBName);
  793. $oConfig->SetDBSubname($sDBPrefix);
  794. BuildConfig($oP, $oConfig, $aParamValues); // Load all the includes based on the modules selected
  795. $oConfig->WriteToFile(TMP_CONFIG_FILE);
  796. if (CreateDatabaseStructure($oP, $oConfig, $sDBName, $sDBPrefix))
  797. {
  798. $sRedStar = "<span class=\"hilite\">*</span>";
  799. $oP->add("<h2>Default language for the application:</h2>\n");
  800. // Possible languages (depends on the dictionaries loaded in the config)
  801. $aForm = array();
  802. $aAvailableLanguages = Dict::GetLanguages();
  803. $sLanguages = '';
  804. $sDefaultCode = $oConfig->GetDefaultLanguage();
  805. foreach($aAvailableLanguages as $sLangCode => $aInfo)
  806. {
  807. $sSelected = ($sLangCode == $sDefaultCode ) ? 'selected ' : '';
  808. $sLanguages.="<option value=\"{$sLangCode}\">{$aInfo['description']} ({$aInfo['localized_description']})</option>";
  809. }
  810. $aForm[] = array('label' => "Default Language$sRedStar:", 'input' => "<select id=\"language\" name=\"language\">$sLanguages</option>");
  811. $oP->form($aForm);
  812. $oP->add("<h2>Definition of the administrator account</h2>\n");
  813. // Database created, continue with admin creation
  814. $oP->add("<fieldset><legend>Administrator account</legend>\n");
  815. $aForm = array();
  816. $aForm[] = array('label' => "Login$sRedStar:", 'input' => "<input id=\"auth_user\" type=\"text\" name=\"auth_user\" value=\"{$aParamValues['auth_user']}\">");
  817. $aForm[] = array('label' => "Password$sRedStar:", 'input' => "<input id=\"auth_pwd\" type=\"password\" name=\"auth_pwd\" value=\"{$aParamValues['auth_pwd']}\">");
  818. $aForm[] = array('label' => "Retype password$sRedStar:", 'input' => "<input id=\"auth_pwd2\" type=\"password\" name=\"auth_pwd2\" value=\"{$aParamValues['auth_pwd']}\">");
  819. $oP->form($aForm);
  820. $oP->add("</fieldset>\n");
  821. $oP->add("<h2 class=\"next\">Next: Administrator Account Creation</h2>\n");
  822. $oP->add("<table style=\"width:100%\"><tr>\n");
  823. $oP->add("<td style=\"text-align:left;\"><button type=\"button\" onClick=\"return DoGoBack($iCurrentStep)\"><< Back</button></td>\n");
  824. $oP->add("<td style=\"text-align:right;\"><button type=\"submit\" onClick=\"return DoSubmit('Creating the admin account and profiles...', $iCurrentStep);\">Next >></button></td>\n");
  825. $oP->add("</tr></table>\n");
  826. }
  827. else
  828. {
  829. $oP->add("<button type=\"button\" onClick=\"return DoGoBack($iCurrentStep)\"><< Back</button>\n");
  830. }
  831. // Form goes here
  832. $oP->add("</form>\n");
  833. }
  834. /**
  835. * Display the form for the fourth step of the configuration wizard
  836. * which consists in
  837. * 1) Creating the admin user account
  838. * 2) Prompting to load some sample data
  839. */
  840. function SampleDataSelection(SetupWebPage $oP, $aParamValues, $iCurrentStep, Config $oConfig)
  841. {
  842. $sNextOperation = 'step'.($iCurrentStep+1);
  843. $oP->set_title("Application Initialization");
  844. $sAdminUser = $aParamValues['auth_user'];
  845. $sAdminPwd = $aParamValues['auth_pwd'];
  846. $sLanguage = $aParamValues['language'];
  847. $oConfig->SetDefaultLanguage($aParamValues['language']);
  848. $oConfig->WriteToFile(TMP_CONFIG_FILE);
  849. $oP->add("<form id=\"theForm\" method=\"post\"\">\n");
  850. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  851. AddParamsToForm($oP, $aParamValues, array('sample_data'));
  852. if (CreateAdminAccount($oP, $oConfig, $sAdminUser, $sAdminPwd, $sLanguage) && UserRights::Setup())
  853. {
  854. $oP->add("<h2>Loading of sample data</h2>\n");
  855. $oP->p("<fieldset><legend> Do you want to load sample data into the database ? </legend>\n");
  856. $oP->p("<input type=\"radio\" id=\"sample_data\" name=\"sample_data\" id=\"sample_data_no\" checked value=\"yes\"><label for=\"sample_data_yes\"> Yes, for testing purposes, populate the database with sample data.</label>\n");
  857. $oP->p("<input type=\"radio\" name=\"sample_data\" unchecked id=\"sample_data_no\" value=\"no\"><label for=\"sample_data_no\"> No, this is a production system, load only the data required by the application.</label>\n");
  858. $oP->p("</fieldset>\n");
  859. $oP->add("<h2 class=\"next\">Next: Application Initialization</h2>\n");
  860. $oP->add("<table style=\"width:100%\"><tr>\n");
  861. $oP->add("<td style=\"text-align:left;\"><button type=\"button\" onClick=\"return DoGoBack($iCurrentStep)\"><< Back</button></td>\n");
  862. $oP->add("<td style=\"text-align:right;\"><button type=\"submit\" onClick=\"return DoSubmit('Finalizing configuration and loading data...', $iCurrentStep)\">Next >></button></td>\n");
  863. $oP->add("</tr></table>\n");
  864. }
  865. else
  866. {
  867. // Creation failed
  868. $oP->error("Internal error: Failed to create the admin account or to setup the user rights");
  869. $oP->add("<button type=\"button\" onClick=\"return DoGoBack($iCurrentStep)\"><< Back</button>\n");
  870. }
  871. // End of visible form
  872. $oP->add("</form>\n");
  873. // Hidden form submitted when moving on to the next page, once all the data files
  874. // have been processed
  875. $oP->add("<form id=\"GoToNextStep\" method=\"post\">\n");
  876. AddParamsToForm($oP, $aParamValues, array('sample_data'));
  877. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  878. $oP->add("</form>\n");
  879. $oP->add("<div id=\"log\" style=\"color:#F00;\"></div>\n");
  880. $oP->add_linked_script('./jquery.progression.js');
  881. PopulateDataFilesList($oP, $aParamValues);
  882. }
  883. /**
  884. * Display the form for the fifth (and final) step of the configuration wizard
  885. * which consists in
  886. * 1) Creating the final configuration file
  887. * 2) Prompting the user to make the file read-only
  888. */
  889. function SetupFinished(SetupWebPage $oP, $aParamValues, $iCurrentStep, Config $oConfig)
  890. {
  891. $sAuthUser = $aParamValues['auth_user'];
  892. $sAuthPwd = $aParamValues['auth_pwd'];
  893. try
  894. {
  895. session_start();
  896. // Write the final configuration file
  897. $oConfig->WriteToFile(FINAL_CONFIG_FILE);
  898. // Start the application
  899. InitDataModel($oP, FINAL_CONFIG_FILE, false); // Load model and startup DB
  900. if (UserRights::Login($sAuthUser, $sAuthPwd))
  901. {
  902. $_SESSION['auth_user'] = $sAuthUser;
  903. $_SESSION['auth_pwd'] = $sAuthPwd;
  904. // remove the tmp config file
  905. @unlink(TMP_CONFIG_FILE);
  906. // try to make the final config file read-only
  907. @chmod(FINAL_CONFIG_FILE, 0440); // Read-only for owner and group, nothing for others
  908. $oP->set_title("Configuration completed");
  909. $oP->add("<form id=\"theForm\" method=\"get\" action=\"../index.php\">\n");
  910. // Check if there are some manual steps required:
  911. $aAvailableModules = GetAvailableModules($oP);
  912. $aManualSteps = array();
  913. foreach($aParamValues['module'] as $sModuleId)
  914. {
  915. if (!empty($aAvailableModules[$sModuleId]['doc.manual_setup']))
  916. {
  917. $aManualSteps[$aAvailableModules[$sModuleId]['label']] = $aAvailableModules[$sModuleId]['doc.manual_setup'];
  918. }
  919. }
  920. if (count($aManualSteps) > 0)
  921. {
  922. $oP->add("<h2>Manual operations required</h2>");
  923. $oP->p("In order to complete the installation, the following manual operations are required:");
  924. foreach($aManualSteps as $sModuleLabel => $sUrl)
  925. {
  926. $oP->p("<a href=\"$sUrl\" target=\"_blank\">Manual instructions for $sModuleLabel</a>");
  927. }
  928. }
  929. else
  930. {
  931. $oP->add("<h2>Congratulations for installing iTop</h2>");
  932. $oP->ok("The initialization completed successfully.");
  933. }
  934. // Form goes here.. No back button since the job is done !
  935. $oP->add("<h1>Let us know what you think about iTop</h1>");
  936. $oP->add('<table style="width:100%;border:0;padding:0;"><tr><td style="width:100px;vertical-align:middle;background:#f6f6f1;text-align:center">');
  937. $oP->add('<a href="http://www.combodo.com" style="padding:0;background:transparent;margin:0;"><img style="border:0" src="../images/logo-combodo.png"></a></td>');
  938. $oP->add('<td style="padding-left: 10px;font-size:10pt">');
  939. $oP->add("Combodo built iTop because Combodo believes that modern ITIL tools should be at the center of any IT department.");
  940. $oP->p("Combodo invested a lot of time and effort in iTop, but you can help us improve it even further by providing your feedbacks</p>");
  941. $oP->p("<a href=\"http://www.combodo.com/register?product=iTop&version=".urlencode(ITOP_VERSION." revision ".ITOP_REVISION)."\" target=\"_blank\">Register online</a> to get informed about all iTop related events (new versions, webinars, etc...)");
  942. $oP->p("Check out the <a href=\"http://www.combodo.com/itopsupport\">support options</a> available for iTop.");
  943. $oP->add('</td></tr></table>');
  944. $oP->add("<p style=\"text-align:center;width:100%\"><button type=\"submit\">Enter iTop</button></p>\n");
  945. $oP->add("</form>\n");
  946. }
  947. else
  948. {
  949. $oP->add("<h1>iTop configuration wizard</h1>\n");
  950. $oP->add("<h2>Step 5: Configuration completed</h2>\n");
  951. @unlink(FINAL_CONFIG_FILE); // remove the aborted config
  952. $oP->error("Error: Failed to login for user: '$sAuthUser'\n");
  953. $oP->add("<form id=\"theForm\" method=\"post\">\n");
  954. $oP->add("<button type=\"button\" onClick=\"return DoGoBack($iCurrentStep);\"><< Back</button>\n");
  955. AddParamsToForm($oP, $aParamValues);
  956. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"step0\">\n");
  957. $oP->add("</form>\n");
  958. }
  959. }
  960. catch(Exception $e)
  961. {
  962. $oP->error("Error: unable to create the configuration file.");
  963. $oP->p($e->getHtmlDesc());
  964. $oP->p("Did you forget to remove the previous (read-only) configuration file ?");
  965. $oP->add("<form id=\"theForm\" method=\"post\">\n");
  966. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"step0\">\n");
  967. AddParamsToForm($oP, $aParamValues);
  968. $oP->add("<button type=\"button\" onClick=\"return DoGoBack($iCurrentStep);\"><< Back</button>\n");
  969. $oP->add("</form>\n");
  970. }
  971. }
  972. ///////////////////////////////////////////////////////////////////////////////////////////////////
  973. // Main program
  974. ///////////////////////////////////////////////////////////////////////////////////////////////////
  975. clearstatcache(); // Make sure we know what we are doing !
  976. if (file_exists(FINAL_CONFIG_FILE))
  977. {
  978. // The configuration file already exists
  979. if (is_writable(FINAL_CONFIG_FILE))
  980. {
  981. $oP->warning("<b>Warning:</b> a configuration file '".FINAL_CONFIG_FILE."' already exists, and will be overwritten.");
  982. }
  983. else
  984. {
  985. $oP->add("<h1>iTop configuration wizard</h1>\n");
  986. $oP->add("<h2>Fatal error</h2>\n");
  987. $oP->error("<b>Error:</b> the configuration file '".FINAL_CONFIG_FILE."' already exists and cannot be overwritten.");
  988. $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.");
  989. $oP->output();
  990. exit;
  991. }
  992. }
  993. else
  994. {
  995. // No configuration file yet
  996. // Check that the wizard can write into the root dir to create the configuration file
  997. if (!is_writable(dirname(FINAL_CONFIG_FILE)))
  998. {
  999. $oP->add("<h1>iTop configuration wizard</h1>\n");
  1000. $oP->add("<h2>Fatal error</h2>\n");
  1001. $oP->error("<b>Error:</b> the directory where to store the configuration file is not writable.");
  1002. $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.");
  1003. $oP->output();
  1004. exit;
  1005. }
  1006. }
  1007. try
  1008. {
  1009. $oConfig = new Config(TMP_CONFIG_FILE);
  1010. }
  1011. catch(Exception $e)
  1012. {
  1013. // We'll end here when the tmp config file does not exist. It's normal
  1014. $oConfig = new Config(TMP_CONFIG_FILE, false /* Don't try to load it */);
  1015. }
  1016. try
  1017. {
  1018. $aParams = array('licence_ok', 'db_server', 'db_user', 'db_pwd','db_name', 'new_db_name', 'db_prefix', 'module', 'sample_data', 'auth_user', 'auth_pwd', 'language');
  1019. foreach($aParams as $sName)
  1020. {
  1021. $aParamValues[$sName] = utils::ReadParam($sName, '');
  1022. }
  1023. switch($sOperation)
  1024. {
  1025. case 'step0':
  1026. $oP->no_cache();
  1027. $oP->log("Info - ========= Wizard step 0 ========");
  1028. WelcomeAndCheckPrerequisites($oP, $aParamValues, 0);
  1029. break;
  1030. case 'step1':
  1031. $oP->no_cache();
  1032. $oP->log("Info - ========= Wizard step 1 ========");
  1033. LicenceAcknowledgement($oP, $aParamValues, 1);
  1034. break;
  1035. case 'step2':
  1036. $oP->log("Info - ========= Wizard step 2 ========");
  1037. DatabaseServerSelection($oP, $aParamValues, 2);
  1038. break;
  1039. case 'step3':
  1040. $oP->no_cache();
  1041. $oP->log("Info - ========= Wizard step 3 ========");
  1042. DatabaseInstanceSelection($oP, $aParamValues, 3, $oConfig);
  1043. break;
  1044. case 'step4':
  1045. $oP->no_cache();
  1046. $oP->log("Info - ========= Wizard step 4 ========");
  1047. ModulesSelection($oP, $aParamValues, 4, $oConfig);
  1048. break;
  1049. case 'step5':
  1050. $oP->no_cache();
  1051. $oP->log("Info - ========= Wizard step 5 ========");
  1052. AdminAccountDefinition($oP, $aParamValues, 5, $oConfig);
  1053. break;
  1054. case 'step6':
  1055. $oP->no_cache();
  1056. $oP->log("Info - ========= Wizard step 6 ========");
  1057. SampleDataSelection($oP, $aParamValues, 6, $oConfig);
  1058. break;
  1059. case 'step7':
  1060. $oP->no_cache();
  1061. $oP->log("Info - ========= Wizard step 7 ========");
  1062. SetupFinished($oP, $aParamValues, 7, $oConfig);
  1063. break;
  1064. default:
  1065. $oP->error("Error: unsupported operation '$sOperation'");
  1066. }
  1067. }
  1068. catch(Exception $e)
  1069. {
  1070. $oP->error("Error: '".$e->getMessage()."'");
  1071. $oP->add("<button type=\"button\" onClick=\"window.history.back();\"><< Back</button>\n");
  1072. }
  1073. catch(CoreException $e)
  1074. {
  1075. $oP->error("Error: '".$e->getHtmlDesc()."'");
  1076. $oP->add("<button type=\"button\" onClick=\"window.history.back();\"><< Back</button>\n");
  1077. }
  1078. $oP->output();
  1079. ?>