index.php 60 KB

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