index.php 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  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('../approot.inc.php');
  25. require_once(APPROOT.'/application/utils.inc.php');
  26. require_once(APPROOT.'/core/config.class.inc.php');
  27. require_once(APPROOT.'/core/log.class.inc.php');
  28. require_once(APPROOT.'/core/kpi.class.inc.php');
  29. require_once(APPROOT.'/core/cmdbsource.class.inc.php');
  30. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  31. require_once(APPROOT.'/setup/moduleinstaller.class.inc.php');
  32. define('TMP_CONFIG_FILE', APPROOT.'/tmp-config-itop.php');
  33. define('FINAL_CONFIG_FILE', APPROOT.'/config-itop.php');
  34. define('PHP_MIN_VERSION', '5.2.0');
  35. define('MYSQL_MIN_VERSION', '5.0.0');
  36. define('MIN_MEMORY_LIMIT', 32*1024*1024);
  37. define('SUHOSIN_GET_MAX_VALUE_LENGTH', 1024);
  38. $sOperation = Utils::ReadParam('operation', 'step0');
  39. $oP = new SetupWebPage('iTop configuration wizard');
  40. ///////////////////////////////////////////////////////////////////////////////////////////////////
  41. // Various helper function
  42. ///////////////////////////////////////////////////////////////////////////////////////////////////
  43. /**
  44. * Get a nicely formatted version string
  45. */
  46. function GetITopVersion($bShort = true)
  47. {
  48. $sVersionString = '';
  49. if ($bShort)
  50. {
  51. $sVersionString = "iTop Version ".ITOP_VERSION;
  52. }
  53. else
  54. {
  55. if (ITOP_REVISION == '$WCREV$')
  56. {
  57. // This is NOT a version built using the buil system, just display the main version
  58. $sVersionString = "iTop Version ".ITOP_VERSION;
  59. }
  60. else
  61. {
  62. // This is a build made from SVN, let display the full information
  63. $sVersionString = "iTop Version ".ITOP_VERSION." revision ".ITOP_REVISION.", built on: ".ITOP_BUILD_DATE;
  64. }
  65. }
  66. return $sVersionString;
  67. }
  68. /**
  69. * Helper function to retrieve the system's temporary directory
  70. * Emulates sys_get_temp_dir if neeed (PHP < 5.2.1)
  71. * @return string Path to the system's temp directory
  72. */
  73. function GetTmpDir()
  74. {
  75. // try to figure out what is the temporary directory
  76. // prior to PHP 5.2.1 the function sys_get_temp_dir
  77. // did not exist
  78. if ( !function_exists('sys_get_temp_dir'))
  79. {
  80. if( $temp=getenv('TMP') ) return realpath($temp);
  81. if( $temp=getenv('TEMP') ) return realpath($temp);
  82. if( $temp=getenv('TMPDIR') ) return realpath($temp);
  83. $temp=tempnam(__FILE__,'');
  84. if (file_exists($temp))
  85. {
  86. unlink($temp);
  87. return realpath(dirname($temp));
  88. }
  89. return null;
  90. }
  91. else
  92. {
  93. return realpath(sys_get_temp_dir());
  94. }
  95. }
  96. /**
  97. * Helper function to retrieve the directory where files are to be uploaded
  98. * @return string Path to the temp directory used for uploading files
  99. */
  100. function GetUploadTmpDir()
  101. {
  102. $sPath = ini_get('upload_tmp_dir');
  103. if (empty($sPath))
  104. {
  105. $sPath = GetTmpDir();
  106. }
  107. return $sPath;
  108. }
  109. /**
  110. * Helper function to check if the current version of PHP
  111. * is compatible with the application
  112. * @return boolean true if this is Ok, false otherwise
  113. */
  114. function CheckPHPVersion(SetupWebPage $oP)
  115. {
  116. $bResult = true;
  117. $aErrors = array();
  118. $aWarnings = array();
  119. $aOk = array();
  120. $oP->log('Info - CheckPHPVersion');
  121. if (version_compare(phpversion(), PHP_MIN_VERSION, '>='))
  122. {
  123. $aOk [] = "The current PHP Version (".phpversion().") is greater than the minimum required version (".PHP_MIN_VERSION.")";
  124. }
  125. else
  126. {
  127. $aErrors[] = "Error: The current PHP Version (".phpversion().") is lower than the minimum required version (".PHP_MIN_VERSION.")";
  128. $bResult = false;
  129. }
  130. $aMandatoryExtensions = array('mysqli', 'iconv', 'simplexml', 'soap', 'hash', 'json', 'session', 'pcre', 'dom');
  131. $aOptionalExtensions = array('mcrypt' => 'Strong encryption will not be used.',
  132. 'ldap' => 'LDAP authentication will be disabled.');
  133. asort($aMandatoryExtensions); // Sort the list to look clean !
  134. ksort($aOptionalExtensions); // Sort the list to look clean !
  135. $aExtensionsOk = array();
  136. $aMissingExtensions = array();
  137. $aMissingExtensionsLinks = array();
  138. // First check the mandatory extensions
  139. foreach($aMandatoryExtensions as $sExtension)
  140. {
  141. if (extension_loaded($sExtension))
  142. {
  143. $aExtensionsOk[] = $sExtension;
  144. }
  145. else
  146. {
  147. $aMissingExtensions[] = $sExtension;
  148. $aMissingExtensionsLinks[] = "<a href=\"http://www.php.net/manual/en/book.$sExtension.php\" target=\"_blank\">$sExtension</a>";
  149. }
  150. }
  151. if (count($aExtensionsOk) > 0)
  152. {
  153. $aOk[] = "Required PHP extension(s): ".implode(', ', $aExtensionsOk).".";
  154. }
  155. if (count($aMissingExtensions) > 0)
  156. {
  157. $aErrors[] = "Missing PHP extension(s): ".implode(', ', $aMissingExtensionsLinks).".";
  158. $bResult = false;
  159. }
  160. // Next check the optional extensions
  161. $aExtensionsOk = array();
  162. $aMissingExtensions = array();
  163. foreach($aOptionalExtensions as $sExtension => $sMessage)
  164. {
  165. if (extension_loaded($sExtension))
  166. {
  167. $aExtensionsOk[] = $sExtension;
  168. }
  169. else
  170. {
  171. $aMissingExtensions[$sExtension] = $sMessage;
  172. }
  173. }
  174. if (count($aExtensionsOk) > 0)
  175. {
  176. $aOk[] = "Optional PHP extension(s): ".implode(', ', $aExtensionsOk).".";
  177. }
  178. if (count($aMissingExtensions) > 0)
  179. {
  180. foreach($aMissingExtensions as $sExtension => $sMessage)
  181. {
  182. $aWarnings[] = "Missing optional PHP extension: $sExtension. ".$sMessage;
  183. }
  184. }
  185. // Check some ini settings here
  186. if (function_exists('php_ini_loaded_file')) // PHP >= 5.2.4
  187. {
  188. $sPhpIniFile = php_ini_loaded_file();
  189. // Other included/scanned files
  190. if ($sFileList = php_ini_scanned_files())
  191. {
  192. if (strlen($sFileList) > 0)
  193. {
  194. $aFiles = explode(',', $sFileList);
  195. foreach ($aFiles as $sFile)
  196. {
  197. $sPhpIniFile .= ', '.trim($sFile);
  198. }
  199. }
  200. }
  201. $oP->log("Info - php.ini file(s): '$sPhpIniFile'");
  202. }
  203. else
  204. {
  205. $sPhpIniFile = 'php.ini';
  206. }
  207. if (!ini_get('file_uploads'))
  208. {
  209. $aErrors[] = "Files upload is not allowed on this server (file_uploads = ".ini_get('file_uploads').").";
  210. $bResult = false;
  211. }
  212. $sUploadTmpDir = GetUploadTmpDir();
  213. if (empty($sUploadTmpDir))
  214. {
  215. $sUploadTmpDir = '/tmp';
  216. $aErrors[] = "Temporary directory for files upload is not defined (upload_tmp_dir), assuming that $sUploadTmpDir is used.";
  217. }
  218. // check that the upload directory is indeed writable from PHP
  219. if (!empty($sUploadTmpDir))
  220. {
  221. if (!file_exists($sUploadTmpDir))
  222. {
  223. $aErrors[] = "Temporary directory for files upload ($sUploadTmpDir) does not exist or cannot be read by PHP.";
  224. $bResult = false;
  225. }
  226. else if (!is_writable($sUploadTmpDir))
  227. {
  228. $aErrors[] = "Temporary directory for files upload ($sUploadTmpDir) is not writable.";
  229. $bResult = false;
  230. }
  231. else
  232. {
  233. $oP->log("Info - Temporary directory for files upload ($sUploadTmpDir) is writable.");
  234. }
  235. }
  236. if (!ini_get('upload_max_filesize'))
  237. {
  238. $aErrors[] = "File upload is not allowed on this server (upload_max_filesize = ".ini_get('upload_max_filesize').").";
  239. }
  240. $iMaxFileUploads = ini_get('max_file_uploads');
  241. if (!empty($iMaxFileUploads) && ($iMaxFileUploads < 1))
  242. {
  243. $aErrors[] = "File upload is not allowed on this server (max_file_uploads = ".ini_get('max_file_uploads').").";
  244. $bResult = false;
  245. }
  246. $iMaxUploadSize = utils::ConvertToBytes(ini_get('upload_max_filesize'));
  247. $iMaxPostSize = utils::ConvertToBytes(ini_get('post_max_size'));
  248. if ($iMaxPostSize <= $iMaxUploadSize)
  249. {
  250. $aWarnings[] = "post_max_size (".ini_get('post_max_size').") must be bigger than upload_max_filesize (".ini_get('upload_max_filesize')."). 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. }
  252. $oP->log("Info - upload_max_filesize: ".ini_get('upload_max_filesize'));
  253. $oP->log("Info - post_max_size: ".ini_get('post_max_size'));
  254. $oP->log("Info - max_file_uploads: ".ini_get('max_file_uploads'));
  255. // Check some more ini settings here, needed for file upload
  256. if (function_exists('get_magic_quotes_gpc'))
  257. {
  258. if (@get_magic_quotes_gpc())
  259. {
  260. $aErrors[] = "'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.";
  261. $bResult = false;
  262. }
  263. }
  264. if (function_exists('magic_quotes_runtime'))
  265. {
  266. if (@magic_quotes_runtime())
  267. {
  268. $aErrors[] = "'magic_quotes_runtime' 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.";
  269. $bResult = false;
  270. }
  271. }
  272. $sMemoryLimit = trim(ini_get('memory_limit'));
  273. if (empty($sMemoryLimit))
  274. {
  275. // On some PHP installations, memory_limit does not exist as a PHP setting!
  276. // (encountered on a 5.2.0 under Windows)
  277. // In that case, ini_set will not work, let's keep track of this and proceed anyway
  278. $aWarnings[] = "No memory limit has been defined in this instance of PHP";
  279. }
  280. else
  281. {
  282. // Check that the limit will allow us to load the data
  283. //
  284. $iMemoryLimit = utils::ConvertToBytes($sMemoryLimit);
  285. if ($iMemoryLimit < MIN_MEMORY_LIMIT)
  286. {
  287. $aErrors[] = "memory_limit ($iMemoryLimit) is too small, the minimum value to run iTop is ".MIN_MEMORY_LIMIT.".";
  288. $bResult = false;
  289. }
  290. else
  291. {
  292. $oP->log_info("memory_limit is $iMemoryLimit, ok.");
  293. }
  294. }
  295. // Special case for APC
  296. if (extension_loaded('apc'))
  297. {
  298. $sAPCVersion = phpversion('apc');
  299. $aOk[] = "APC detected (version $sAPCVersion). The APC cache will be used to speed-up iTop.";
  300. }
  301. // Special case Suhosin extension
  302. if (extension_loaded('suhosin'))
  303. {
  304. $sSuhosinVersion = phpversion('suhosin');
  305. $aOk[] = "Suhosin extension detected (version $sSuhosinVersion).";
  306. $iGetMaxValueLength = ini_get('suhosin.get.max_value_length');
  307. if ($iGetMaxValueLength < SUHOSIN_GET_MAX_VALUE_LENGTH)
  308. {
  309. $aErrors[] = "suhosin.get.max_value_length ($iGetMaxValueLength) is too small, the minimum value to run iTop is ".SUHOSIN_GET_MAX_VALUE_LENGTH.". This value is set by the PHP configuration file(s): '$sPhpIniFile'. Be aware that this setting can also be overridden in the apache configuration.";
  310. $bResult = false;
  311. }
  312. else
  313. {
  314. $oP->log_info("suhosin.get.max_value_length = $iGetMaxValueLength, ok.");
  315. }
  316. }
  317. if (!$bResult)
  318. {
  319. $sTitle = 'Checking prerequisites: Failed !';
  320. }
  321. else
  322. {
  323. if (count($aWarnings) > 0)
  324. {
  325. $sTitle = '<img src="../images/messagebox_warning-mid.png" style="vertical-align:middle"> Checking prerequisites: Warning <a href="#" onClick="$(\'#prereq_details\').toggle();">(show details)</a>';
  326. $oP->add_ready_script("$('#prereq_details').hide();\n");
  327. }
  328. else
  329. {
  330. $sTitle = '<img src="../images/clean-mid.png" style="vertical-align:middle"> Checking prerequisites: Ok <a href="#" onClick="$(\'#prereq_details\').toggle();">(show details)</a>';
  331. $oP->add_ready_script("$('#prereq_details').hide();\n");
  332. }
  333. }
  334. $oP->add("<h2>$sTitle</h2>\n");
  335. $oP->add("<div id=\"prereq_details\">\n");
  336. foreach($aErrors as $sError)
  337. {
  338. $oP->error($sError);
  339. //$oP->add_ready_script("$('#prereq_details').show();");
  340. }
  341. foreach($aWarnings as $sWarning)
  342. {
  343. $oP->warning($sWarning);
  344. }
  345. foreach($aOk as $sOk)
  346. {
  347. $oP->ok($sOk);
  348. }
  349. $oP->add("</div>\n");
  350. return $bResult;
  351. }
  352. /**
  353. * Helper function check the connection to the database and (if connected) to enumerate
  354. * the existing databases
  355. * @return Array The list of databases found in the server
  356. */
  357. function CheckServerConnection(SetupWebPage $oP, $sDBServer, $sDBUser, $sDBPwd)
  358. {
  359. $aResult = array();
  360. $oP->log('Info - CheckServerConnection');
  361. try
  362. {
  363. $oDBSource = new CMDBSource;
  364. $oDBSource->Init($sDBServer, $sDBUser, $sDBPwd);
  365. $oP->ok("Connection to '$sDBServer' as '$sDBUser' successful.");
  366. $oP->log("Info - User privileges: ".($oDBSource->GetRawPrivileges()));
  367. $sDBVersion = $oDBSource->GetDBVersion();
  368. if (version_compare($sDBVersion, MYSQL_MIN_VERSION, '>='))
  369. {
  370. $oP->ok("Current MySQL version ($sDBVersion), greater than minimum required version (".MYSQL_MIN_VERSION.")");
  371. // Check some server variables
  372. $iMaxAllowedPacket = $oDBSource->GetServerVariable('max_allowed_packet');
  373. $iMaxUploadSize = utils::ConvertToBytes(ini_get('upload_max_filesize'));
  374. if ($iMaxAllowedPacket >= (500 + $iMaxUploadSize)) // Allow some space for the query + the file to upload
  375. {
  376. $oP->ok("MySQL server's max_allowed_packet is big enough.");
  377. }
  378. else if($iMaxAllowedPacket < $iMaxUploadSize)
  379. {
  380. $oP->warning("MySQL server's max_allowed_packet ($iMaxAllowedPacket) is not big enough. Please, consider setting it to at least ".(500 + $iMaxUploadSize).".");
  381. }
  382. $oP->log("Info - MySQL max_allowed_packet: $iMaxAllowedPacket");
  383. $iMaxConnections = $oDBSource->GetServerVariable('max_connections');
  384. if ($iMaxConnections < 5)
  385. {
  386. $oP->warning("MySQL server's max_connections ($iMaxConnections) is not enough. Please, consider setting it to at least 5.");
  387. }
  388. $oP->log("Info - MySQL max_connections: ".($oDBSource->GetServerVariable('max_connections')));
  389. }
  390. else
  391. {
  392. $oP->error("Error: Current MySQL version is ($sDBVersion), minimum required version (".MYSQL_MIN_VERSION.")");
  393. return false;
  394. }
  395. try
  396. {
  397. $aResult = $oDBSource->ListDB();
  398. }
  399. catch(Exception $e)
  400. {
  401. $oP->warning("Warning: unable to enumerate the current databases.");
  402. $aResult = true; // Not an array to differentiate with an empty array
  403. }
  404. }
  405. catch(Exception $e)
  406. {
  407. $oP->error("Error: Connection to '$sDBServer' as '$sDBUser' failed.");
  408. $oP->p($e->GetHtmlDesc());
  409. $aResult = false;
  410. }
  411. return $aResult;
  412. }
  413. /**
  414. * Scans the ./data directory for XML files and output them as a Javascript array
  415. */
  416. function PopulateDataFilesList(SetupWebPage $oP, $aParamValues, $oConfig)
  417. {
  418. $sScript = "function PopulateDataFilesList()\n";
  419. $sScript .= "{\n";
  420. $sScript .= "if (aFilesToLoad.length > 0) return;"; // Populate the list only once...
  421. $aAvailableModules = AnalyzeInstallation($oConfig);
  422. $sMode = $aParamValues['mode'];
  423. $aStructureDataFiles = array();
  424. $aSampleDataFiles = array();
  425. foreach($aAvailableModules as $sModuleId => $aModule)
  426. {
  427. if (($sModuleId != ROOT_MODULE))
  428. {
  429. if (in_array($sModuleId, $aParamValues['module']))
  430. {
  431. if (empty($aModule['version_db']))
  432. {
  433. // New installation load the data
  434. $aModuleStruct = $aAvailableModules[$sModuleId]['data.struct'];
  435. $aModuleSamples = $aAvailableModules[$sModuleId]['data.sample'];
  436. $aStructureDataFiles = array_merge($aStructureDataFiles, $aModuleStruct);
  437. $aSampleDataFiles = array_merge($aSampleDataFiles, $aModuleSamples);
  438. }
  439. }
  440. }
  441. }
  442. // Structure data
  443. //
  444. foreach($aStructureDataFiles as $sFile)
  445. {
  446. // Under Windows, it is a must to escape backslashes (not an issue until a folder name starts with t or n, etc...)
  447. $sFile = APPROOT.$sFile;
  448. $sFile = str_replace('\\', '\\\\', $sFile);
  449. $sScript .= "aFilesToLoad[aFilesToLoad.length] = '$sFile';\n";
  450. }
  451. // Sample data - loaded IIF wished by the user
  452. //
  453. if ($aParamValues['sample_data'] != 'no')
  454. {
  455. foreach($aSampleDataFiles as $sFile)
  456. {
  457. // Under Windows, it is a must to escape backslashes (not an issue until a folder name starts with t or n, etc...)
  458. $sFile = APPROOT.$sFile;
  459. $sFile = str_replace('\\', '\\\\', $sFile);
  460. $sScript .= "aFilesToLoad[aFilesToLoad.length] = '$sFile';\n";
  461. }
  462. }
  463. $sScript .= "}\n";
  464. $oP->add_script($sScript);
  465. }
  466. /**
  467. * Add some parameters as hidden inputs into a form
  468. * @param SetupWebpage $oP The page to insert the form elements into
  469. * @param Hash $aParamValues The pairs name/value to be stored in the form
  470. * @param Array $aExcludeParams A list of parameters to exclude from the previous hash
  471. */
  472. function AddParamsToForm(SetupWebpage $oP, $aParamValues, $aExcludeParams = array())
  473. {
  474. foreach($aParamValues as $sName => $value)
  475. {
  476. if(!in_array($sName, $aExcludeParams))
  477. {
  478. AddHiddenParam($oP, $sName, $value);
  479. }
  480. }
  481. }
  482. /**
  483. * Add a hidden <INPUT> field to store the specified parameter
  484. * @param $sName string Name of the parameter
  485. * @param $value mixed Value of the parameter
  486. */
  487. function AddHiddenParam($oP, $sName, $value)
  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. * Build the config file from the parameters (especially the selected modules)
  503. */
  504. function BuildConfig(SetupWebpage $oP, Config &$oConfig, $aParamValues, $aAvailableModules)
  505. {
  506. // Initialize the arrays below with default values for the application...
  507. $oEmptyConfig = new Config('dummy_file', false); // Do NOT load any config file, just set the default values
  508. $aAddOns = $oEmptyConfig->GetAddOns();
  509. $aAppModules = $oEmptyConfig->GetAppModules();
  510. $aDataModels = $oEmptyConfig->GetDataModels();
  511. $aWebServiceCategories = $oEmptyConfig->GetWebServiceCategories();
  512. $aDictionaries = $oEmptyConfig->GetDictionaries();
  513. // Merge the values with the ones provided by the modules
  514. // Make sure when don't load the same file twice...
  515. foreach($aParamValues['module'] as $sModuleId)
  516. {
  517. $oP->log('Installed iTop module: '. $sModuleId);
  518. if (isset($aAvailableModules[$sModuleId]['datamodel']))
  519. {
  520. $aDataModels = array_unique(array_merge($aDataModels, $aAvailableModules[$sModuleId]['datamodel']));
  521. }
  522. if (isset($aAvailableModules[$sModuleId]['webservice']))
  523. {
  524. $aWebServiceCategories = array_unique(array_merge($aWebServiceCategories, $aAvailableModules[$sModuleId]['webservice']));
  525. }
  526. if (isset($aAvailableModules[$sModuleId]['dictionary']))
  527. {
  528. $aDictionaries = array_unique(array_merge($aDictionaries, $aAvailableModules[$sModuleId]['dictionary']));
  529. }
  530. if (isset($aAvailableModules[$sModuleId]['settings']))
  531. {
  532. foreach($aAvailableModules[$sModuleId]['settings'] as $sProperty => $value)
  533. {
  534. list($sName, $sVersion) = GetModuleName($sModuleId);
  535. $oConfig->SetModuleSetting($sName, $sProperty, $value);
  536. }
  537. }
  538. if (isset($aAvailableModules[$sModuleId]['installer']))
  539. {
  540. $sModuleInstallerClass = $aAvailableModules[$sModuleId]['installer'];
  541. if (!class_exists($sModuleInstallerClass))
  542. {
  543. throw new Exception("Wrong installer class: '$sModuleInstallerClass' is not a PHP class - Module: ".$aAvailableModules[$sModuleId]['label']);
  544. }
  545. if (!is_subclass_of($sModuleInstallerClass, 'ModuleInstallerAPI'))
  546. {
  547. throw new Exception("Wrong installer class: '$sModuleInstallerClass' is not derived from 'ModuleInstallerAPI' - Module: ".$aAvailableModules[$sModuleId]['label']);
  548. }
  549. $aCallSpec = array($sModuleInstallerClass, 'BeforeWritingConfig');
  550. $oConfig = call_user_func_array($aCallSpec, array($oConfig));
  551. }
  552. }
  553. $oConfig->SetAddOns($aAddOns);
  554. $oConfig->SetAppModules($aAppModules);
  555. $oConfig->SetDataModels($aDataModels);
  556. $oConfig->SetWebServiceCategories($aWebServiceCategories);
  557. $oConfig->SetDictionaries($aDictionaries);
  558. }
  559. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  560. // Handling of the different steps of the setup wizard
  561. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  562. /**
  563. * Displays the welcome screen and check some basic prerequisites
  564. */
  565. function WelcomeAndCheckPrerequisites(SetupWebPage $oP, $aParamValues, $iCurrentStep)
  566. {
  567. $sNextOperation = 'step'.($iCurrentStep+1);
  568. $aParamValues['previous_step'] = 0;
  569. $oP->add("<h1>iTop configuration wizard</h1>\n");
  570. $sVersionStringShort = GetITopVersion(true);
  571. $sVersionStringLong = GetITopVersion(false);
  572. $oP->set_title('Welcome to '.$sVersionStringShort);
  573. $oP->log($sVersionStringLong);
  574. $aPreviousParams = array();
  575. $oP->add("<form id=\"theForm\" method=\"post\" onSubmit=\"return DoSubmit('', 0)\">\n");
  576. $sMode = 'install'; // Fresh install
  577. // Check for a previous version
  578. if (file_exists(FINAL_CONFIG_FILE))
  579. {
  580. $oConfig = new Config(FINAL_CONFIG_FILE);
  581. $oConfig->WriteToFile(TMP_CONFIG_FILE);
  582. $aVersion = AnalyzeInstallation($oConfig);
  583. if (!empty($aVersion[ROOT_MODULE]['version_db']))
  584. {
  585. $aPreviousParams = array('mode', 'db_server', 'db_user', 'db_pwd','db_name', 'new_db_name', 'db_prefix');
  586. $sMode = 'upgrade';
  587. if ($aVersion[ROOT_MODULE]['version_db'] == $aVersion[ROOT_MODULE]['version_code'])
  588. {
  589. $oP->ok("Version ".$aVersion[ROOT_MODULE]['version_db']." of iTop detected.<br/>The <b>same version</b> of the application will be reinstalled.");
  590. }
  591. else
  592. {
  593. $oP->ok("Version ".$aVersion[ROOT_MODULE]['version_db']." of iTop detected.<br/>The application will be upgraded to version ".$aVersion[ROOT_MODULE]['version_code'].".");
  594. }
  595. AddHiddenParam($oP, 'db_server', $oConfig->GetDBHost());
  596. AddHiddenParam($oP, 'db_user', $oConfig->GetDBUser());
  597. AddHiddenParam($oP, 'db_pwd', $oConfig->GetDBPwd());
  598. AddHiddenParam($oP, 'db_name', $oConfig->GetDBName());
  599. AddHiddenParam($oP, 'db_prefix', $oConfig->GetDBSubname());
  600. AddHiddenParam($oP, 'mode', $sMode);
  601. if (CheckPHPVersion($oP))
  602. {
  603. $oP->add("<h2 class=\"next\">Next: Licence agreement</h2>\n");
  604. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  605. AddParamsToForm($oP, $aParamValues, $aPreviousParams);
  606. $oP->add("<table style=\"width:100%\"><tr>\n");
  607. $oP->add("<td style=\"text-align:right;\"><button type=\"submit\" type=\"submit\">Next >></button></td>\n");
  608. $oP->add("</tr></table>\n");
  609. }
  610. return;
  611. }
  612. // else, normal install ??
  613. }
  614. if (CheckPHPVersion($oP))
  615. {
  616. $oP->add("<h2>What do you want to do?</h2>\n");
  617. $sChecked = ($aParamValues['mode'] == 'install') ? 'checked' : '';
  618. $oP->p("<input id=\"choice_install\" type=\"radio\" value=\"install\" $sChecked name=\"mode\"><label for=\"choice_install\">&nbsp;Install a new iTop</label>");
  619. $sChecked = ($aParamValues['mode'] == 'upgrade') ? 'checked' : '';
  620. $oP->p("<input id=\"choice_upgrade\" type=\"radio\" value=\"upgrade\" $sChecked name=\"mode\"><label for=\"choice_upgrade\">&nbsp;Upgrade an existing iTop instance</label>");
  621. $oP->add("<h2 class=\"next\">Next: Licence agreement</h2>\n");
  622. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  623. $aPreviousParams = array('mode');
  624. AddParamsToForm($oP, $aParamValues, $aPreviousParams);
  625. $oP->add("<table style=\"width:100%\"><tr>\n");
  626. $oP->add("<td style=\"text-align:right;\"><button type=\"submit\">Next >></button></td>\n");
  627. $oP->add("</tr></table>\n");
  628. $oP->add("</form>\n");
  629. }
  630. }
  631. function LicenceAcknowledgement($oP, $aParamValues, $iCurrentStep)
  632. {
  633. $sNextOperation = 'step'.($iCurrentStep+1);
  634. $iPrevStep = 0;
  635. $aParamValues['previous_step'] = $iCurrentStep; // Come back here
  636. $oP->set_title('License agreement');
  637. $oP->add('<h2>iTop is released by <a href="http://www.combodo.com" target="_blank">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>');
  638. $oP->add("<iframe style=\"width: 100%; height: 350px; overflow-y:auto; font-size:0.8em;\" src=\"./licence.html\">Next: Database server selection</iframe>\n");
  639. $oP->add("<form id=\"theForm\" method=\"post\">\n");
  640. AddParamsToForm($oP, $aParamValues, array('licence_ok'));
  641. $sChecked = $aParamValues['licence_ok'] == 1 ? 'checked' : '';
  642. $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");
  643. if (file_exists(FINAL_CONFIG_FILE))
  644. {
  645. $oP->add("<h2 class=\"next\">Next: Modules selection</h2>\n");
  646. $sNextOperation = 'step4';
  647. }
  648. else
  649. {
  650. $oP->add("<h2 class=\"next\">Next: Database server selection</h2>\n");
  651. }
  652. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  653. $oP->add("<table style=\"width:100%\"><tr>\n");
  654. $oP->add("<td style=\"text-align:left;\"><button type=\"button\" onClick=\"return DoGoBack($iPrevStep)\"><< Back</button></td>\n");
  655. $oP->add("<td style=\"text-align:right;\"><input type=\"submit\" onClick=\"return DoSubmit('', $iCurrentStep)\" value=\" Next >> \"/></td>\n");
  656. $oP->add("</tr></table>\n");
  657. $oP->add("</form>\n");
  658. }
  659. /**
  660. * Display the form for the first step of the configuration wizard
  661. * which consists in the database server selection
  662. */
  663. function DatabaseServerSelection(SetupWebPage $oP, $aParamValues, $iCurrentStep)
  664. {
  665. $sNextOperation = 'step'.($iCurrentStep+1);
  666. $iPrevStep = 1;
  667. $aParamValues['previous_step'] = $iCurrentStep; // Come back here
  668. $oP->add("<form id=\"theForm\" method=\"post\">\n");
  669. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  670. AddParamsToForm($oP, $aParamValues, array('db_server', 'db_user', 'db_pwd'));
  671. if ($aParamValues['licence_ok'] == 1)
  672. {
  673. $sRedStar = '<span class="hilite">*</span>';
  674. $oP->set_title("Database server selection\n");
  675. $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");
  676. // Form goes here
  677. $oP->add("<fieldset><legend>Database connection</legend>\n");
  678. $aForm = array();
  679. $aForm[] = array('label' => "Server name$sRedStar:", 'input' => "<input id=\"db_server\" type=\"text\" name=\"db_server\" value=\"{$aParamValues['db_server']}\">",
  680. 'help' => 'E.g. "localhost", "dbserver.mycompany.com" or "192.142.10.23"');
  681. $aForm[] = array('label' => "User name$sRedStar:", 'input' => "<input id=\"db_user\" type=\"text\" name=\"db_user\" autocomplete=\"off\" value=\"{$aParamValues['db_user']}\">",
  682. 'help' => 'The account must have the following privileges on the database: SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, CREATE VIEW, TRIGGER');
  683. $aForm[] = array('label' => 'Password:', 'input' => "<input id=\"db_pwd\" type=\"password\" name=\"db_pwd\" autocomplete=\"off\" value=\"{$aParamValues['db_pwd']}\">");
  684. $oP->form($aForm);
  685. $oP->add("</fieldset>\n");
  686. $oP->add("<h2 class=\"next\">Next: Database instance Selection</h2>\n");
  687. $oP->add("<table style=\"width:100%\"><tr>\n");
  688. $oP->add("<td style=\"text-align:left;\"><button type=\"button\" onClick=\"return DoGoBack($iPrevStep)\"><< Back</button></td>\n");
  689. $oP->add("<td style=\"text-align:right;\"><input type=\"submit\" onClick=\"return DoSubmit('Connecting to the database...', $iCurrentStep);\" value=\" Next >> \"/></td>\n");
  690. $oP->add("</tr></table>\n");
  691. }
  692. else
  693. {
  694. $oP->add("<button type=\"button\" onClick=\"return DoGoBack($iPrevStep);\"><< Back</button>\n");
  695. }
  696. $oP->add("</form>\n");
  697. }
  698. /**
  699. * Display the form for the second step of the configuration wizard
  700. * which consists in
  701. * 1) Validating the parameters by connecting to the database server
  702. * 2) Prompting to select an existing database or to create a new one
  703. */
  704. function DatabaseInstanceSelection(SetupWebPage $oP, $aParamValues, $iCurrentStep, $oConfig)
  705. {
  706. $sNextOperation = 'step'.($iCurrentStep+1);
  707. $iPrevStep = 2;
  708. $aParamValues['previous_step'] = $iCurrentStep; // Come back here
  709. $oP->set_title("Database instance selection\n");
  710. $oP->add("<form id=\"theForm\" method=\"post\">\n");
  711. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  712. AddParamsToForm($oP, $aParamValues, array('db_name', 'db_prefix', 'new_db_name'));
  713. $sDBServer = $aParamValues['db_server'];
  714. $sDBUser = $aParamValues['db_user'];
  715. $sDBPwd = $aParamValues['db_pwd'];
  716. $aDatabases = CheckServerConnection($oP, $sDBServer, $sDBUser, $sDBPwd);
  717. if ($aDatabases === false)
  718. {
  719. // Connection failed, invalid credentials ? Go back
  720. $oP->add("<button type=\"button\" onClick=\"return DoGoBack($iPrevStep);\"><< Back</button>\n");
  721. }
  722. else
  723. {
  724. // Connection is Ok, save it and continue the setup wizard
  725. $oConfig->SetDBHost($sDBServer);
  726. $oConfig->SetDBUser($sDBUser);
  727. $oConfig->SetDBPwd($sDBPwd);
  728. $oConfig->WriteToFile();
  729. $oP->add("<fieldset><legend>Select the database instance to use for iTop<span class=\"hilite\">*</span></legend>\n");
  730. $aForm = array();
  731. $bExistingChecked = false;
  732. $sChecked = '';
  733. $sDBName = '';
  734. // If the 'Create Database' option was checked... and the database still does not exist
  735. if (!$bExistingChecked && !empty($aParamValues['new_db_name']))
  736. {
  737. $sChecked = 'checked';
  738. $sDBName = $aParamValues['new_db_name'];
  739. }
  740. if ($aParamValues['mode'] == 'install')
  741. {
  742. $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\"/>");
  743. }
  744. if (is_array($aDatabases))
  745. {
  746. foreach($aDatabases as $sDBName)
  747. {
  748. $sChecked = '';
  749. if ($aParamValues['db_name'] == $sDBName)
  750. {
  751. $sChecked = 'checked';
  752. $bExistingChecked = true;
  753. }
  754. $aForm[] = array('label' => "<input id=\"db_$sDBName\" type=\"radio\" name=\"db_name\" value=\"$sDBName\" $sChecked/><label for=\"db_$sDBName\"> $sDBName</label>");
  755. }
  756. }
  757. else
  758. {
  759. $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\"/>");
  760. $oP->add_ready_script("$('#current_db_name').click( function() { $('#current_db').attr('checked', true); });");
  761. }
  762. $oP->add('<div style="height:250px;overflow-y:auto;padding-left:1em;">');
  763. $oP->form($aForm);
  764. $oP->add('</div>');
  765. $oP->add_ready_script("$('#new_db_name').click( function() { $('#new_db').attr('checked', true); })");
  766. $oP->add("</fieldset>\n");
  767. $aForm = array();
  768. if ($aParamValues['mode'] == 'install')
  769. {
  770. $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\"/>");
  771. }
  772. else
  773. {
  774. $aForm[] = array('label' => "The following prefix is used for the iTop tables: <input id=\"db_prefix\" type=\"text\" name=\"db_prefix\" value=\"{$aParamValues['db_prefix']}\" maxlength=\"32\"/>");
  775. }
  776. $oP->form($aForm);
  777. $oP->add("<h2 class=\"next\">Next: iTop modules selection</h2>\n");
  778. $oP->add("<table style=\"width:100%\"><tr>\n");
  779. $oP->add("<td style=\"text-align:left;\"><button type=\"button\" onClick=\"return DoGoBack($iPrevStep)\"><< Back</button></td>\n");
  780. $oP->add("<td style=\"text-align:right;\"><input type=\"submit\" onClick=\"return DoSubmit('', $iCurrentStep);\" value=\" Next >> \"/></td>\n");
  781. $oP->add("</tr></table>\n");
  782. }
  783. $oP->add("</form>\n");
  784. }
  785. /**
  786. * Display the form to select the iTop modules to be installed
  787. */
  788. function ModulesSelection(SetupWebPage $oP, $aParamValues, $iCurrentStep, $oConfig)
  789. {
  790. $sNextOperation = 'step'.($iCurrentStep+1);
  791. $aParamValues['previous_step'] = $iCurrentStep; // Come back here
  792. $sDBName = $aParamValues['db_name'];
  793. if ($sDBName == '')
  794. {
  795. $sDBName = $aParamValues['new_db_name'];
  796. }
  797. $sDBPrefix = $aParamValues['db_prefix'];
  798. $oConfig->SetDBName($sDBName);
  799. $oConfig->SetDBSubname($sDBPrefix);
  800. $oConfig->WriteToFile(TMP_CONFIG_FILE);
  801. $oP->add("<form id=\"theForm\" method=\"post\">\n");
  802. AddParamsToForm($oP, $aParamValues, array('module'));
  803. $sRedStar = '<span class="hilite">*</span>';
  804. $oP->set_title("iTop modules selection");
  805. $aAvailableModules = AnalyzeInstallation($oConfig);
  806. // Form goes here
  807. if ($aParamValues['mode'] == 'upgrade')
  808. {
  809. if (file_exists(FINAL_CONFIG_FILE))
  810. {
  811. $iPrevStep = 1; // depends on where we came from
  812. }
  813. else
  814. {
  815. $iPrevStep = 3;
  816. }
  817. if (empty($aAvailableModules[ROOT_MODULE]['version_db']))
  818. {
  819. $oP->error("Unable to detect the previous installation of iTop. The upgrade cannot continue.\n");
  820. $oP->add("<table style=\"width:100%\"><tr>\n");
  821. $oP->add("<td style=\"text-align:left;\"><button type=\"button\" onClick=\"return DoGoBack($iPrevStep)\"><< Back</button></td>\n");
  822. $oP->add("<td style=\"text-align:right;\">&nbsp;</td>\n");
  823. $oP->add("</tr></table>\n");
  824. $oP->add("</form>\n");
  825. return;
  826. }
  827. $oP->ok("iTop version ".$aAvailableModules[ROOT_MODULE]['version_db']." detected.\n");
  828. $oP->add("<h2>Customize your iTop installation to fit your needs</h2>\n");
  829. $oP->add("<fieldset><legend>Select the iTop modules you want to install or upgrade:</legend>\n");
  830. }
  831. else
  832. {
  833. $iPrevStep = 3; // depends on where we came from
  834. if (!empty($aAvailableModules[ROOT_MODULE]['version_db']))
  835. {
  836. $oP->error("A instance of iTop already exists. Please select the \"Upgrade\" mode to upgrade it.\n");
  837. $oP->add("<table style=\"width:100%\"><tr>\n");
  838. $oP->add("<td style=\"text-align:left;\"><button type=\"button\" onClick=\"return DoGoBack($iPrevStep)\"><< Back</button></td>\n");
  839. $oP->add("<td style=\"text-align:right;\">&nbsp;</td>\n");
  840. $oP->add("</tr></table>\n");
  841. $oP->add("</form>\n");
  842. return;
  843. }
  844. $oP->add("<h2>Customize your iTop installation to fit your needs</h2>\n");
  845. $oP->add("<fieldset><legend>Select the iTop modules you want to install:</legend>\n");
  846. }
  847. $oP->add("<div style=\"border: 0;width:100%; height: 250px; overflow-y:auto;\">");
  848. $sRedStar = '<span class="hilite">*</span>';
  849. $index = 0;
  850. $aSelectedModules = $aParamValues['module'];
  851. if ($aSelectedModules == '')
  852. {
  853. // Make sure it gets initialized as an array, default value: all modules selected !
  854. $aSelectedModules = array();
  855. foreach($aAvailableModules as $sModuleId => $aModule)
  856. {
  857. $aSelectedModules[] = $sModuleId;
  858. }
  859. }
  860. foreach($aAvailableModules as $sModuleId => $aModule)
  861. {
  862. if ($sModuleId == ROOT_MODULE) continue; // Convention: the version number of the application is stored as a module named ROOT_MODULE
  863. $sModuleLabel = $aModule['label'];
  864. $sModuleHelp = $aModule['doc.more_information'];
  865. //$sClass = ($aModule['mandatory']) ? 'class="read-only"' : '';
  866. try
  867. {
  868. $sDefaultAppPath = utils::GetDefaultUrlAppRoot();
  869. }
  870. catch(Exception $e)
  871. {
  872. $sDefaultAppPath = '..';
  873. }
  874. $sMoreInfo = (!empty($aModule['doc.more_information'])) ? "<a href=\"$sDefaultAppPath{$aModule['doc.more_information']}\" target=\"_blank\">more info</a>": '';
  875. if ($aModule['category'] == 'authentication')
  876. {
  877. // For now authentication modules are always on and hidden
  878. $oP->add("<input type=\"hidden\" id=\"module[$index]\" name=\"module[$index]\" value=\"$sModuleId\">\n");
  879. $index++;
  880. }
  881. elseif ($aModule['visible'])
  882. {
  883. switch($aModule['install']['flag'])
  884. {
  885. case MODULE_ACTION_OPTIONAL:
  886. $sClass = '';
  887. if ($aParamValues['mode'] == 'upgrade')
  888. {
  889. if (!empty($aParamValues['module']))
  890. {
  891. $sChecked = in_array($sModuleId, $aParamValues['module']) ? 'checked' : '';
  892. }
  893. else
  894. {
  895. $sChecked = '';
  896. // Default value: modules previously installed are checked
  897. if (!empty($aModule['version_db']))
  898. {
  899. $sChecked = 'checked'; // Checked if previously installed
  900. // Previously installed, are we allowed to uninstall this module ?
  901. if ($aModule['install']['flag'] == MODULE_ACTION_IMPOSSIBLE)
  902. {
  903. $sClass = 'class="read-only"';
  904. }
  905. }
  906. }
  907. }
  908. else
  909. {
  910. if (!empty($aParamValues['module']))
  911. {
  912. $sChecked = in_array($sModuleId, $aParamValues['module']) ? 'checked' : '';
  913. }
  914. else
  915. {
  916. $sChecked = 'checked';
  917. }
  918. }
  919. $oP->add("<p><input type=\"checkbox\" $sChecked $sClass id=\"module[$index]\" name=\"module[$index]\" value=\"$sModuleId\"><label $sClass for=\"module[$index]\"> {$aModule['label']}</label> $sMoreInfo</p>\n");
  920. break;
  921. case MODULE_ACTION_MANDATORY:
  922. $oP->add("<p><input type=\"checkbox\" class=\"read-only\" checked id=\"module[$index]\" name=\"module[$index]\" value=\"$sModuleId\"><label class=\"read-only\" for=\"module[$index]\"> {$aModule['label']}</label> $sMoreInfo</p>\n");
  923. break;
  924. case MODULE_ACTION_IMPOSSIBLE:
  925. if ($aParamValues['mode'] == 'upgrade')
  926. {
  927. if (!empty($aModule['version_db']))
  928. {
  929. // Previously installed, are we allowed to uninstall this module ?
  930. if ($aModule['uninstall']['flag'] == MODULE_ACTION_IMPOSSIBLE)
  931. {
  932. $oP->error('Error: impossible to uninstall the module: '.$aModule['label']."({$aModule['uninstall']['message']})");
  933. }
  934. }
  935. }
  936. else
  937. {
  938. $oP->add("<p><input type=\"checkbox\" class=\"read-only\" id=\"module[$index]\" name=\"module[$index]\" value=\"$sModuleId\"><label class=\"read-only\" for=\"module[$index]\"> {$aModule['label']}</label> $sMoreInfo</p>\n");
  939. }
  940. break;
  941. }
  942. $index++;
  943. }
  944. else
  945. {
  946. // For now hidden modules are always on !
  947. $oP->add("<input type=\"hidden\" id=\"module[$index]\" name=\"module[$index]\" value=\"$sModuleId\">\n");
  948. $index++;
  949. }
  950. }
  951. $oP->add("</div>");
  952. $oP->add("</fieldset>\n");
  953. if ($aParamValues['mode'] == 'upgrade')
  954. {
  955. $oP->add("<h2 class=\"next\">Next: Application path</h2>\n");
  956. AddHiddenParam($oP, 'operation', 'step6');
  957. }
  958. else
  959. {
  960. $oP->add("<h2 class=\"next\">Next: Administrator account definition</h2>\n");
  961. AddHiddenParam($oP, 'operation', 'step5');
  962. }
  963. $oP->add("<table style=\"width:100%\"><tr>\n");
  964. $oP->add("<td style=\"text-align:left;\"><button type=\"button\" onClick=\"return DoGoBack($iPrevStep)\"><< Back</button></td>\n");
  965. $oP->add("<td style=\"text-align:right;\"><input type=\"submit\" onClick=\"return DoSubmit('', 4)\" value=\" Next >> \"/></td>\n");
  966. $oP->add("</tr></table>\n");
  967. $oP->add("</form>\n");
  968. $oP->add_ready_script("$('.read-only').click( function() { $(this).attr('checked','checked'); } );");
  969. }
  970. /**
  971. * Display the form for the third step of the configuration wizard
  972. * which consists in
  973. * 1) Validating the parameters by connecting to the database server & selecting the database
  974. * 2) Creating the database structure
  975. * 3) Prompting for the admin account to be created
  976. */
  977. function AdminAccountDefinition(SetupWebPage $oP, $aParamValues, $iCurrentStep, Config $oConfig)
  978. {
  979. $sNextOperation = 'step'.($iCurrentStep+1);
  980. $aParamValues['previous_step'] = $iCurrentStep; // Come back here
  981. $oP->set_title("Administrator account definition");
  982. $oP->add("<form id=\"theForm\" method=\"post\">\n");
  983. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  984. AddParamsToForm($oP, $aParamValues, array('auth_user', 'auth_pwd', 'language'));
  985. $aAvailableModules = AnalyzeInstallation($oConfig);
  986. BuildConfig($oP, $oConfig, $aParamValues, $aAvailableModules); // Load all the includes based on the modules selected
  987. $oConfig->WriteToFile(TMP_CONFIG_FILE);
  988. InitDataModel(TMP_CONFIG_FILE, true); // Needed to know the available languages
  989. $sRedStar = "<span class=\"hilite\">*</span>";
  990. $oP->add("<h2>Default language for the application:</h2>\n");
  991. // Possible languages (depends on the dictionaries loaded in the config)
  992. $aForm = array();
  993. $aAvailableLanguages = Dict::GetLanguages();
  994. $sLanguages = '';
  995. $sDefaultCode = $oConfig->GetDefaultLanguage();
  996. foreach($aAvailableLanguages as $sLangCode => $aInfo)
  997. {
  998. $sSelected = ($sLangCode == $sDefaultCode ) ? 'selected' : '';
  999. $sLanguages.="<option $sSelected value=\"{$sLangCode}\">{$aInfo['description']} ({$aInfo['localized_description']})</option>";
  1000. }
  1001. $aForm[] = array('label' => "Default Language$sRedStar:", 'input' => "<select id=\"language\" name=\"language\">$sLanguages</option>");
  1002. $oP->form($aForm);
  1003. $oP->add("<h2>Definition of the administrator account</h2>\n");
  1004. // Database created, continue with admin creation
  1005. $oP->add("<fieldset><legend>Administrator account</legend>\n");
  1006. $aForm = array();
  1007. $aForm[] = array('label' => "Login$sRedStar:", 'input' => "<input id=\"auth_user\" type=\"text\" name=\"auth_user\" value=\"{$aParamValues['auth_user']}\">");
  1008. $aForm[] = array('label' => "Password$sRedStar:", 'input' => "<input id=\"auth_pwd\" type=\"password\" name=\"auth_pwd\" value=\"{$aParamValues['auth_pwd']}\">");
  1009. $aForm[] = array('label' => "Retype password$sRedStar:", 'input' => "<input id=\"auth_pwd2\" type=\"password\" name=\"auth_pwd2\" value=\"{$aParamValues['auth_pwd']}\">");
  1010. $oP->form($aForm);
  1011. $oP->add("</fieldset>\n");
  1012. $oP->add("<h2 class=\"next\">Next: Application path</h2>\n");
  1013. $oP->add("<table style=\"width:100%\"><tr>\n");
  1014. $oP->add("<td style=\"text-align:left;\"><button type=\"button\" onClick=\"return DoGoBack(4)\"><< Back</button></td>\n");
  1015. $oP->add("<td style=\"text-align:right;\"><input type=\"submit\" onClick=\"return DoSubmit('', 5);\" value=\" Next >> \"/></td>\n");
  1016. $oP->add("</tr></table>\n");
  1017. // Form goes here
  1018. $oP->add("</form>\n");
  1019. }
  1020. /**
  1021. * Display the form for validating/entering the URL (path) to the application
  1022. * which consists in
  1023. */
  1024. function ApplicationPathSelection(SetupWebPage $oP, $aParamValues, $iCurrentStep, Config $oConfig)
  1025. {
  1026. $sNextOperation = 'step7';
  1027. if ($aParamValues['mode'] == 'upgrade')
  1028. {
  1029. $iPrevStep = 4;
  1030. }
  1031. else
  1032. {
  1033. $iPrevStep = 5;
  1034. }
  1035. $oP->set_title("Application Path");
  1036. $oP->add("<form id=\"theForm\" method=\"post\"\">\n");
  1037. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  1038. AddParamsToForm($oP, $aParamValues, array('application_path'));
  1039. try
  1040. {
  1041. $sDefaultAppPath = utils::GetDefaultUrlAppRoot();
  1042. }
  1043. catch(Exception $e)
  1044. {
  1045. $sDefaultAppPath = 'http://<your_host>/itop';
  1046. }
  1047. $sAppPath = empty($aParamValues['application_path']) ? $sDefaultAppPath : $aParamValues['application_path'];
  1048. $oP->add("<h2>Enter the URL that will be used to connect to the application</h2>\n");
  1049. $oP->p("<fieldset><legend> Application URL </legend>\n");
  1050. $oP->p("<input type=\"text\" id=\"application_path\" size=\"60\" name=\"application_path\" value=\"$sAppPath\">\n");
  1051. $oP->p("</fieldset>\n");
  1052. $oP->add("<h2 class=\"next\">Next: Sample data selection</h2>\n");
  1053. $oP->add("<table style=\"width:100%\"><tr>\n");
  1054. $oP->add("<td style=\"text-align:left;\"><button type=\"button\" onClick=\"return DoGoBack($iPrevStep)\"><< Back</button></td>\n");
  1055. $oP->add("<td style=\"text-align:right;\"><input type=\"submit\" onClick=\"return DoSubmit('', 6)\" value=\" Next >> \"/></td>\n");
  1056. $oP->add("</tr></table>\n");
  1057. $oP->add("</form>\n");
  1058. }
  1059. /**
  1060. * Display the form for the fourth step of the configuration wizard
  1061. * which consists in
  1062. * 1) Creating the admin user account
  1063. * 2) Prompting to load some sample data
  1064. */
  1065. function SampleDataSelection(SetupWebPage $oP, $aParamValues, $iCurrentStep, Config $oConfig)
  1066. {
  1067. $sNextOperation = 'step8';
  1068. $iPrevStep = 6;
  1069. $oP->set_title("Application initialization");
  1070. $sAdminUser = $aParamValues['auth_user'];
  1071. $sAdminPwd = $aParamValues['auth_pwd'];
  1072. $sLanguage = $aParamValues['language'];
  1073. if (($aParamValues['mode'] == 'install') || $oConfig->GetDefaultLanguage() == '')
  1074. {
  1075. $oConfig->SetDefaultLanguage($aParamValues['language']);
  1076. }
  1077. $aAvailableModules = AnalyzeInstallation($oConfig);
  1078. BuildConfig($oP, $oConfig, $aParamValues, $aAvailableModules); // Load all the includes based on the modules selected
  1079. $oConfig->Set('app_root_url', $aParamValues['application_path']);
  1080. // in case of upgrade, the value is already present in the config file
  1081. $oConfig->WriteToFile(TMP_CONFIG_FILE);
  1082. $oP->add("<form id=\"theForm\" method=\"post\"\">\n");
  1083. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  1084. AddParamsToForm($oP, $aParamValues, array('sample_data'));
  1085. InitDataModel(TMP_CONFIG_FILE, true); // load data model and connect to the database
  1086. $aAvailableModules = GetAvailableModules($oP);
  1087. foreach($aParamValues['module'] as $sModuleId)
  1088. {
  1089. if (isset($aAvailableModules[$sModuleId]['installer']))
  1090. {
  1091. $sModuleInstallerClass = $aAvailableModules[$sModuleId]['installer'];
  1092. // The validity of the sModuleInstallerClass has been established in BuildConfig()
  1093. $aCallSpec = array($sModuleInstallerClass, 'AfterDatabaseCreation');
  1094. call_user_func_array($aCallSpec, array($oConfig));
  1095. }
  1096. }
  1097. $oP->add("<h2>Loading of sample data</h2>\n");
  1098. $oP->p("<fieldset><legend> Do you want to load sample data into the database ? </legend>\n");
  1099. $sChecked = ($aParamValues['sample_data'] == 'no') ? '' : 'checked';
  1100. $oP->p("<input type=\"radio\" id=\"sample_data\" name=\"sample_data\" id=\"sample_data_no\" $sChecked value=\"yes\"><label for=\"sample_data_yes\"> Yes, for testing purposes, populate the database with sample data.</label>\n");
  1101. $sChecked = ($aParamValues['sample_data'] == 'no') ? 'checked' : '';
  1102. $oP->p("<input type=\"radio\" name=\"sample_data\" unchecked id=\"sample_data_no\" $sChecked value=\"no\"><label for=\"sample_data_no\"> No, this is a production system, load only the data required by the application.</label>\n");
  1103. $oP->p("</fieldset>\n");
  1104. $oP->add("<h2 class=\"next\">Next: Installation summary</h2>\n");
  1105. $oP->add("<table style=\"width:100%\"><tr>\n");
  1106. $oP->add("<td style=\"text-align:left;\"><button type=\"button\" onClick=\"return DoGoBack($iPrevStep)\"><< Back</button></td>\n");
  1107. $oP->add("<td style=\"text-align:right;\"><input type=\"submit\" onClick=\"DoSubmit('', 7)\" value=\" Next >> \"/></td>\n");
  1108. $oP->add("</tr></table>\n");
  1109. $oP->add("</form>\n");
  1110. }
  1111. /**
  1112. * Displays the summary of the actions to be taken
  1113. */
  1114. function DisplaySummary(SetupWebPage $oP, $aParamValues, $iCurrentStep, Config $oConfig)
  1115. {
  1116. $sMode = $aParamValues['mode'];
  1117. $aAvailableModules = AnalyzeInstallation($oConfig);
  1118. BuildConfig($oP, $oConfig, $aParamValues, $aAvailableModules); // Load all the includes based on the modules selected
  1119. $oConfig->WriteToFile(TMP_CONFIG_FILE);
  1120. InitDataModel(TMP_CONFIG_FILE, true); // Needed to know the available languages
  1121. $aInstall = array();
  1122. $aUpgrade = array();
  1123. $aUninstall = array();
  1124. $aUnchanged = array();
  1125. switch($sMode)
  1126. {
  1127. case 'install':
  1128. foreach($aAvailableModules as $sModuleId => $aModule)
  1129. {
  1130. if (($sModuleId != ROOT_MODULE) && $aModule['visible'] && ($aModule['category'] != 'authentication'))
  1131. {
  1132. if (in_array($sModuleId, $aParamValues['module']))
  1133. {
  1134. $aInstall[$sModuleId] = $aModule;
  1135. }
  1136. }
  1137. }
  1138. $oP->set_title('Installation Summary');
  1139. $oP->add("<h3>iTop version ".$aAvailableModules[ROOT_MODULE]['version_code']." will be installed.</h3>");
  1140. $oP->add('<div id="summary_content" style="height:350px;overflow-y:auto;border:1px solid #999;padding-left:1em;">');
  1141. // Database information
  1142. $sPrefix = '';
  1143. if ($oConfig->GetDBSubname() != '')
  1144. {
  1145. $sPrefix = " (prefix: ".$oConfig->GetDBSubname().")";
  1146. }
  1147. $oP->collapsible('db', "Database", array($oConfig->GetDBName()." on server: ".$oConfig->GetDBHost().$sPrefix));
  1148. if (count($aInstall) > 0)
  1149. {
  1150. $iCount = count($aInstall);
  1151. $aItems = array();
  1152. foreach($aInstall as $sModuleId => $aModule)
  1153. {
  1154. $aItems[] = $aModule['label'].' version '.$aModule['version_code'];
  1155. }
  1156. $oP->collapsible('install', "$iCount module(s) will be installed", $aItems, ($iCount < 5));
  1157. }
  1158. // Sample data
  1159. if ($aParamValues['sample_data'] != 'no')
  1160. {
  1161. $sSampleData = 'Sample data will be loaded for the newly installed modules.';
  1162. }
  1163. else
  1164. {
  1165. $sSampleData = 'No sample data will be loaded.';
  1166. }
  1167. $oP->collapsible('sample_data', "Sample Data", array($sSampleData));
  1168. // Application Path
  1169. $oP->collapsible('application_path', "Application path", array('URL:'.htmlentities($aParamValues['application_path'], ENT_QUOTES, 'UTF-8')));
  1170. // Admin account
  1171. $oP->collapsible('admin_account', "Administrator account", array('Login:'.htmlentities($aParamValues['auth_user'], ENT_QUOTES, 'UTF-8')));
  1172. // Default language
  1173. $aAvailableLanguages = Dict::GetLanguages();
  1174. $oP->collapsible('language', "Default application language", array( $aAvailableLanguages[$aParamValues['language']]['description']." (".$aAvailableLanguages[$aParamValues['language']]['localized_description'].")"));
  1175. $oP->add('</div>');
  1176. $oP->add("<form id=\"theForm\" method=\"post\">\n");
  1177. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"step9\">\n");
  1178. AddParamsToForm($oP, $aParamValues);
  1179. $oP->add("<table style=\"width:100%\"><tr>\n");
  1180. $oP->add("<td style=\"text-align:left;\"><button type=\"button\" onClick=\"return DoGoBack(7)\"><< Back</button></td>\n");
  1181. // Note: the Next >> button is NOT a submit, since moving to the next page is triggered asynchronously
  1182. $oP->add("<td style=\"text-align:right;\"><button type=\"button\" onClick=\"DoSubmit('Installing...', 8)\"> Install ! </button></td>\n");
  1183. $oP->add("</tr></table>\n");
  1184. $oP->add("</form>\n");
  1185. break;
  1186. case 'upgrade':
  1187. foreach($aAvailableModules as $sModuleId => $aModule)
  1188. {
  1189. if (($sModuleId != ROOT_MODULE) && $aModule['visible'] && ($aModule['category'] != 'authentication'))
  1190. {
  1191. if (in_array($sModuleId, $aParamValues['module']))
  1192. {
  1193. if (empty($aModule['version_db']))
  1194. {
  1195. $aInstall[$sModuleId] = $aModule;
  1196. }
  1197. else if ($aModule['version_db'] == $aModule['version_code'])
  1198. {
  1199. $aUnchanged[$sModuleId] = $aModule;
  1200. }
  1201. else
  1202. {
  1203. // Consider it's an upgrade... TO DO: handle downgrades ??
  1204. $aUpgrade[$sModuleId] = $aModule;
  1205. }
  1206. }
  1207. else if (!empty($aModule['version_db']))
  1208. {
  1209. $aUninstall[$sModuleId] = $aModule;
  1210. }
  1211. // Else do nothing: the module was not installed and is not selected
  1212. }
  1213. }
  1214. $oP->set_title('Upgrade Summary');
  1215. $oP->add("<h3>The application will be upgraded from version ".$aAvailableModules[ROOT_MODULE]['version_db'].' to version '.$aAvailableModules[ROOT_MODULE]['version_code'].'</h3>');
  1216. $oP->add('<div id="summary_content" style="height:350px;overflow-y:auto;border:1px solid #999;padding-left:1em;">');
  1217. // Database information
  1218. $sPrefix = '';
  1219. if ($oConfig->GetDBSubname() != '')
  1220. {
  1221. $sPrefix = " (prefix: ".$oConfig->GetDBSubname().")";
  1222. }
  1223. $oP->collapsible('db', "Database", array($oConfig->GetDBName()." on server: ".$oConfig->GetDBHost().$sPrefix));
  1224. // Modules summary, per "type" (install, uninstall...)
  1225. if (count($aUpgrade) > 0)
  1226. {
  1227. $iCount = count($aUpgrade);
  1228. $aItems = array();
  1229. foreach($aUpgrade as $sModuleId => $aModule)
  1230. {
  1231. $aItems[] = $aModule['label'].' version '.$aModule['version_db'].' to version '.$aModule['version_code'];
  1232. }
  1233. $oP->collapsible('upgrade', "$iCount module(s) will be upgraded", $aItems, ($iCount < 5));
  1234. }
  1235. if (count($aInstall) > 0)
  1236. {
  1237. $iCount = count($aInstall);
  1238. $aItems = array();
  1239. foreach($aInstall as $sModuleId => $aModule)
  1240. {
  1241. $aItems[] = $aModule['label'].' version '.$aModule['version_code'];
  1242. }
  1243. $oP->collapsible('install', "$iCount module(s) will be installed", $aItems, ($iCount < 5));
  1244. }
  1245. if (count($aUninstall) > 0)
  1246. {
  1247. $iCount = count($aUninstall);
  1248. $aItems = array();
  1249. foreach($aUninstall as $sModuleId => $aModule)
  1250. {
  1251. $aItems[] = $aModule['label'].' version '.$aModule['version_db'];
  1252. }
  1253. $oP->collapsible('uninstall', "$iCount module(s) will be removed", $aItems, true /* always open */);
  1254. }
  1255. if (count($aUnchanged) > 0)
  1256. {
  1257. $iCount = count($aUnchanged);
  1258. $aItems = array();
  1259. foreach($aUnchanged as $sModuleId => $aModule)
  1260. {
  1261. $aItems[] = $aModule['label'].' version '.$aModule['version_db'];
  1262. }
  1263. $oP->collapsible('unchanged', "$iCount module(s) will remain unchanged", $aItems, ($iCount < 5));
  1264. }
  1265. // Sample data
  1266. if ($aParamValues['sample_data'] != 'no')
  1267. {
  1268. $sSampleData = 'Sample data will be loaded for the newly installed modules.';
  1269. }
  1270. else
  1271. {
  1272. $sSampleData = 'No sample data will be loaded.';
  1273. }
  1274. $oP->collapsible('sample_data', "Sample Data", array($sSampleData));
  1275. // Application Path
  1276. $oP->collapsible('application_path', "Application path", array('URL:'.htmlentities($aParamValues['application_path'], ENT_QUOTES, 'UTF-8')));
  1277. $oP->add('</div>');
  1278. $oP->add("<form id=\"theForm\" method=\"post\">\n");
  1279. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"step8\">\n");
  1280. AddParamsToForm($oP, $aParamValues);
  1281. $oP->add("<table style=\"width:100%\"><tr>\n");
  1282. $oP->add("<td style=\"text-align:left;\"><button type=\"button\" onClick=\"DoGoBack(6)\"><< Back</button></td>\n");
  1283. // Note: the Next >> button is NOT a submit, since moving to the next page is triggered asynchronously
  1284. $oP->add("<td style=\"text-align:right;\"><button type=\"button\" onClick=\"DoSubmit('', 8)\"> Upgrade ! </button></td>\n");
  1285. $oP->add("</tr></table>\n");
  1286. $oP->add("</form>\n");
  1287. break;
  1288. default:
  1289. $oP->error("Unsupported mode $sMode");
  1290. }
  1291. // Hidden form submitted when moving on to the next page, once all the data files
  1292. // have been processed
  1293. $oP->add("<form id=\"GoToNextStep\" method=\"post\">\n");
  1294. AddParamsToForm($oP, $aParamValues);
  1295. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"step9\">\n");
  1296. $oP->add("</form>\n");
  1297. $oP->add("<div id=\"log\" style=\"color:#F00;\"></div>\n");
  1298. $oP->add_linked_script('./jquery.progression.js');
  1299. PopulateDataFilesList($oP, $aParamValues, $oConfig);
  1300. $oP->add_ready_script(
  1301. <<<EOF
  1302. $('#log').ajaxError(
  1303. function(e, xhr, settings, exception)
  1304. {
  1305. bStopAysncProcess = true;
  1306. alert('Fatal error detected: '+ xhr.responseText);
  1307. $('#log').append(xhr.responseText);
  1308. $('#setup').unblock();
  1309. } );
  1310. EOF
  1311. );
  1312. }
  1313. /** Display the form for the fifth (and final) step of the configuration wizard
  1314. * which consists in
  1315. * 1) Creating the final configuration file
  1316. * 2) Prompting the user to make the file read-only
  1317. */
  1318. function SetupFinished(SetupWebPage $oP, $aParamValues, $iCurrentStep, Config $oConfig)
  1319. {
  1320. $sAuthUser = $aParamValues['auth_user'];
  1321. $sAuthPwd = $aParamValues['auth_pwd'];
  1322. $iPrevStep = $aParamValues['previous_step'];
  1323. $aParamValues['previous_step'] = $iCurrentStep; // Come back here
  1324. try
  1325. {
  1326. $sSessionName = $oConfig->Get('session_name');
  1327. if ($sSessionName != '')
  1328. {
  1329. $sSessionName = sprintf('iTop-%x', rand());
  1330. $oConfig->Set('session_name', $sSessionName);
  1331. }
  1332. session_name($sSessionName);
  1333. session_start();
  1334. // Migration: force utf8_unicode_ci as the collation to make the global search
  1335. // NON case sensitive
  1336. $oConfig->SetDBCollation('utf8_unicode_ci');
  1337. // Write the final configuration file
  1338. $oConfig->WriteToFile(FINAL_CONFIG_FILE);
  1339. // Start the application
  1340. InitDataModel(FINAL_CONFIG_FILE, false, true); // Load model, startup DB and load the cache
  1341. if ($aParamValues['mode'] == 'install')
  1342. {
  1343. if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd))
  1344. {
  1345. UserRights::Login($sAuthUser);
  1346. $_SESSION['auth_user'] = $sAuthUser;
  1347. $_SESSION['login_mode'] = 'form'; // Will enable the "log-off button"
  1348. }
  1349. else
  1350. {
  1351. $oP->add("<h1>iTop configuration wizard</h1>\n");
  1352. $oP->add("<h2>Step 5: Configuration completed</h2>\n");
  1353. @unlink(FINAL_CONFIG_FILE); // remove the aborted config
  1354. $oP->error("Error: Failed to login for user: '$sAuthUser'\n");
  1355. $oP->add("<form id=\"theForm\" method=\"post\">\n");
  1356. $oP->add("<button type=\"button\" onClick=\"return DoGoBack($iPrevStep);\"><< Back</button>\n");
  1357. AddParamsToForm($oP, $aParamValues);
  1358. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"step0\">\n");
  1359. $oP->add("</form>\n");
  1360. return;
  1361. }
  1362. }
  1363. // remove the tmp config file
  1364. @unlink(TMP_CONFIG_FILE);
  1365. // try to make the final config file read-only
  1366. @chmod(FINAL_CONFIG_FILE, 0440); // Read-only for owner and group, nothing for others
  1367. $oP->set_title("Setup complete");
  1368. $oP->add("<form id=\"theForm\" method=\"get\" action=\"../index.php\">\n");
  1369. // Check if there are some manual steps required:
  1370. $aAvailableModules = AnalyzeInstallation($oConfig);
  1371. $aManualSteps = array();
  1372. $sRootUrl = utils::GetAbsoluteUrlAppRoot();
  1373. foreach($aParamValues['module'] as $sModuleId)
  1374. {
  1375. if (!empty($aAvailableModules[$sModuleId]['doc.manual_setup']))
  1376. {
  1377. $aManualSteps[$aAvailableModules[$sModuleId]['label']] = $sRootUrl.$aAvailableModules[$sModuleId]['doc.manual_setup'];
  1378. }
  1379. }
  1380. if (count($aManualSteps) > 0)
  1381. {
  1382. $oP->add("<h2>Manual operations required</h2>");
  1383. $oP->p("In order to complete the installation, the following manual operations are required:");
  1384. foreach($aManualSteps as $sModuleLabel => $sUrl)
  1385. {
  1386. $oP->p("<a href=\"$sUrl\" target=\"_blank\">Manual instructions for $sModuleLabel</a>");
  1387. }
  1388. $oP->add("<h2>Congratulations for installing iTop</h2>");
  1389. }
  1390. else
  1391. {
  1392. $oP->add("<h2>Congratulations for installing iTop</h2>");
  1393. $oP->ok("The initialization completed successfully.");
  1394. }
  1395. // Form goes here.. No back button since the job is done !
  1396. $oP->add('<table style="width:600px;border:0;padding:0;"><tr>');
  1397. $oP->add("<td><a style=\"background:transparent;padding:0;\" title=\"Free: Register your iTop version.\" href=\"http://www.combodo.com/register?product=iTop&version=".urlencode(ITOP_VERSION." revision ".ITOP_REVISION)."\" target=\"_blank\"><img style=\"border:0\" src=\"../images/setup-register.gif\"/></td></a>");
  1398. $oP->add("<td><a style=\"background:transparent;padding:0;\" title=\"Get Professional Support from Combodo\" href=\"http://www.combodo.com/itopsupport\" target=\"_blank\"><img style=\"border:0\" src=\"../images/setup-support.gif\"/></td></a>");
  1399. $oP->add("<td><a style=\"background:transparent;padding:0;\" title=\"Get Professional Training from Combodo\" href=\"http://www.combodo.com/itoptraining\" target=\"_blank\"><img style=\"border:0\" src=\"../images/setup-training.gif\"/></td></a>");
  1400. $oP->add('</tr></table>');
  1401. $oP->add("<p style=\"text-align:center;width:100%\"><button type=\"submit\">Enter iTop</button></p>\n");
  1402. $oP->add("</form>\n");
  1403. }
  1404. catch(Exception $e)
  1405. {
  1406. $oP->error("Error: unable to create the configuration file.");
  1407. $oP->p($e->getHtmlDesc());
  1408. $oP->p("Did you forget to remove the previous (read-only) configuration file ?");
  1409. $oP->add("<form id=\"theForm\" method=\"post\">\n");
  1410. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"step7\">\n");
  1411. AddParamsToForm($oP, $aParamValues, array('previous_step'));
  1412. $oP->add("<button type=\"button\" onClick=\"return DoGoBack(7);\"><< Back</button>\n");
  1413. $oP->add("</form>\n");
  1414. }
  1415. }
  1416. ///////////////////////////////////////////////////////////////////////////////////////////////////
  1417. // Main program
  1418. ///////////////////////////////////////////////////////////////////////////////////////////////////
  1419. clearstatcache(); // Make sure we know what we are doing !
  1420. // Set a long (at least 4 minutes) execution time for the setup to avoid timeouts during this phase
  1421. ini_set('max_execution_time', max(240, ini_get('max_execution_time')));
  1422. // While running the setup it is desirable to see any error that may happen
  1423. ini_set('display_errors', true);
  1424. ini_set('display_startup_errors', true);
  1425. $aParams = array('mode', 'previous_step', 'licence_ok', 'db_server', 'db_user', 'db_pwd','db_name', 'new_db_name', 'db_prefix', 'module', 'sample_data', 'auth_user', 'auth_pwd', 'language', 'application_path');
  1426. foreach($aParams as $sName)
  1427. {
  1428. $aParamValues[$sName] = utils::ReadParam($sName, '', false, 'raw_data');
  1429. }
  1430. if (file_exists(FINAL_CONFIG_FILE))
  1431. {
  1432. // The configuration file already exists
  1433. if (!is_writable(FINAL_CONFIG_FILE))
  1434. {
  1435. $oP->add("<h1>iTop configuration wizard</h1>\n");
  1436. $oP->add("<h2>Fatal error</h2>\n");
  1437. $oP->error("<b>Error:</b> the configuration file '".FINAL_CONFIG_FILE."' already exists and cannot be overwritten.");
  1438. $oP->p("The wizard cannot modify the configuration file for you. If you want to upgrade iTop, please make sure that the file '<b>".realpath(FINAL_CONFIG_FILE)."</b>' can be modified by the web server.");
  1439. $oP->output();
  1440. exit;
  1441. }
  1442. }
  1443. else
  1444. {
  1445. // No configuration file yet
  1446. // Check that the wizard can write into the root dir to create the configuration file
  1447. if (!is_writable(dirname(TMP_CONFIG_FILE)))
  1448. {
  1449. $oP->add("<h1>iTop configuration wizard</h1>\n");
  1450. $oP->add("<h2>Fatal error</h2>\n");
  1451. $oP->error("<b>Error:</b> the directory where to store the configuration file is not writable.");
  1452. $oP->p("The wizard cannot create the configuration file for you. Please make sure that the directory '<b>".realpath(dirname(TMP_CONFIG_FILE))."</b>' is writable for the web server.");
  1453. $oP->output();
  1454. exit;
  1455. }
  1456. if (!is_writable(dirname(TMP_CONFIG_FILE).'/setup'))
  1457. {
  1458. $oP->add("<h1>iTop configuration wizard</h1>\n");
  1459. $oP->add("<h2>Fatal error</h2>\n");
  1460. $oP->error("<b>Error:</b> the directory where to store temporary setup files is not writable.");
  1461. $oP->p("The wizard cannot create operate. Please make sure that the directory '<b>".realpath(dirname(TMP_CONFIG_FILE))."/setup</b>' is writable for the web server.");
  1462. $oP->output();
  1463. exit;
  1464. }
  1465. }
  1466. try
  1467. {
  1468. $oConfig = new Config(TMP_CONFIG_FILE);
  1469. }
  1470. catch(Exception $e)
  1471. {
  1472. // We'll end here when the tmp config file does not exist. It's normal
  1473. $oConfig = new Config(TMP_CONFIG_FILE, false /* Don't try to load it */);
  1474. }
  1475. try
  1476. {
  1477. switch($sOperation)
  1478. {
  1479. case 'step0':
  1480. $oP->no_cache();
  1481. $oP->log("Info - ========= Wizard step 0 ========");
  1482. WelcomeAndCheckPrerequisites($oP, $aParamValues, 0);
  1483. break;
  1484. case 'step1':
  1485. $oP->no_cache();
  1486. $oP->log("Info - ========= Wizard step 1 ========");
  1487. LicenceAcknowledgement($oP, $aParamValues, 1);
  1488. break;
  1489. case 'step2':
  1490. $oP->log("Info - ========= Wizard step 2 ========");
  1491. DatabaseServerSelection($oP, $aParamValues, 2);
  1492. break;
  1493. case 'step3':
  1494. $oP->no_cache();
  1495. $oP->log("Info - ========= Wizard step 3 ========");
  1496. DatabaseInstanceSelection($oP, $aParamValues, 3, $oConfig);
  1497. break;
  1498. case 'step4':
  1499. $oP->no_cache();
  1500. $oP->log("Info - ========= Wizard step 4 ========");
  1501. ModulesSelection($oP, $aParamValues, 4, $oConfig);
  1502. break;
  1503. case 'step5':
  1504. $oP->no_cache();
  1505. $oP->log("Info - ========= Wizard step 5 ========");
  1506. AdminAccountDefinition($oP, $aParamValues, 5, $oConfig);
  1507. break;
  1508. case 'step6':
  1509. $oP->no_cache();
  1510. $oP->log("Info - ========= Wizard step 6 ========");
  1511. ApplicationPathSelection($oP, $aParamValues, 6, $oConfig);
  1512. break;
  1513. case 'step7':
  1514. $oP->no_cache();
  1515. $oP->log("Info - ========= Wizard step 7 ========");
  1516. SampleDataSelection($oP, $aParamValues, 6, $oConfig);
  1517. break;
  1518. case 'step8':
  1519. $oP->no_cache();
  1520. $oP->log("Info - ========= Wizard step 8 ========");
  1521. DisplaySummary($oP, $aParamValues, 7, $oConfig);
  1522. break;
  1523. case 'step9':
  1524. $oP->no_cache();
  1525. $oP->log("Info - ========= Wizard step 9 ========");
  1526. SetupFinished($oP, $aParamValues, 8, $oConfig);
  1527. break;
  1528. default:
  1529. $oP->error("Error: unsupported operation '$sOperation'");
  1530. }
  1531. }
  1532. catch(Exception $e)
  1533. {
  1534. $oP->error("Error: '".$e->getMessage()."'");
  1535. $oP->add("<button type=\"button\" onClick=\"window.history.back();\"><< Back</button>\n");
  1536. }
  1537. catch(CoreException $e)
  1538. {
  1539. $oP->error("Error: '".$e->getHtmlDesc()."'");
  1540. $oP->add("<button type=\"button\" onClick=\"window.history.back();\"><< Back</button>\n");
  1541. }
  1542. $oP->output();
  1543. ?>