index.php 61 KB

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