setuppage.class.inc.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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. * Web page used for displaying the login form
  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."/application/nicewebpage.class.inc.php");
  25. require_once(APPROOT."designer/modulediscovery.class.inc.php");
  26. define('INSTALL_LOG_FILE', APPROOT.'/setup.log');
  27. define ('MODULE_ACTION_OPTIONAL', 1);
  28. define ('MODULE_ACTION_MANDATORY', 2);
  29. define ('MODULE_ACTION_IMPOSSIBLE', 3);
  30. define ('ROOT_MODULE', '_Root_'); // Convention to store IN MEMORY the name/version of the root module i.e. application
  31. date_default_timezone_set('Europe/Paris');
  32. class SetupPage extends NiceWebPage
  33. {
  34. public function __construct($sTitle)
  35. {
  36. parent::__construct($sTitle);
  37. $this->add_linked_script("../js/jquery.blockUI.js");
  38. $this->add_linked_script("./setup.js");
  39. $this->add_linked_script("./setup_environment.js");
  40. $this->add_style("
  41. body {
  42. background-color: #eee;
  43. margin: 0;
  44. padding: 0;
  45. font-size: 10pt;
  46. overflow-y: auto;
  47. }
  48. #header {
  49. width: 600px;
  50. margin-left: auto;
  51. margin-right: auto;
  52. margin-top: 50px;
  53. padding: 20px;
  54. background: #f6f6f1;
  55. height: 54px;
  56. border-top: 1px solid #000;
  57. border-left: 1px solid #000;
  58. border-right: 1px solid #000;
  59. }
  60. #header img {
  61. border: 0;
  62. vertical-align: middle;
  63. margin-right: 20px;
  64. }
  65. #header h1 {
  66. vertical-align: middle;
  67. height: 54px;
  68. noline-height: 54px;
  69. margin: 0;
  70. }
  71. #setup {
  72. width: 600px;
  73. margin-left: auto;
  74. margin-right: auto;
  75. padding: 20px;
  76. background-color: #fff;
  77. border-left: 1px solid #000;
  78. border-right: 1px solid #000;
  79. border-bottom: 1px solid #000;
  80. }
  81. .center {
  82. text-align: center;
  83. }
  84. h1 {
  85. color: #1C94C4;
  86. font-size: 16pt;
  87. }
  88. h2 {
  89. color: #000;
  90. font-size: 14pt;
  91. }
  92. h3 {
  93. color: #1C94C4;
  94. font-size: 12pt;
  95. font-weight: bold;
  96. }
  97. .next {
  98. width: 100%;
  99. text-align: right;
  100. }
  101. .v-spacer {
  102. padding-top: 1em;
  103. }
  104. button {
  105. margin-top: 1em;
  106. padding-left: 1em;
  107. padding-right: 1em;
  108. }
  109. p.info {
  110. padding-left: 50px;
  111. background: url(../images/info-mid.png) no-repeat left -5px;
  112. min-height: 48px;
  113. }
  114. p.ok {
  115. padding-left: 50px;
  116. background: url(../images/clean-mid.png) no-repeat left -8px;
  117. min-height: 48px;
  118. }
  119. p.warning {
  120. padding-left: 50px;
  121. background: url(../images/messagebox_warning-mid.png) no-repeat left -5px;
  122. min-height: 48px;
  123. }
  124. p.error {
  125. padding-left: 50px;
  126. background: url(../images/stop-mid.png) no-repeat left -5px;
  127. min-height: 48px;
  128. }
  129. td.label {
  130. text-align: left;
  131. }
  132. label.read-only {
  133. color: #666;
  134. cursor: text;
  135. }
  136. td.input {
  137. text-align: left;
  138. }
  139. table.formTable {
  140. border: 0;
  141. cellpadding: 2px;
  142. cellspacing: 0;
  143. }
  144. .wizlabel, .wizinput {
  145. color: #000;
  146. font-size: 10pt;
  147. }
  148. .wizhelp {
  149. color: #333;
  150. font-size: 8pt;
  151. }
  152. #progress {
  153. border:1px solid #000000;
  154. width: 180px;
  155. height: 20px;
  156. line-height: 20px;
  157. text-align: center;
  158. margin: 5px;
  159. }
  160. h3.clickable {
  161. background: url(../images/plus.gif) no-repeat left;
  162. padding-left:16px;
  163. cursor: hand;
  164. }
  165. h3.clickable.open {
  166. background: url(../images/minus.gif) no-repeat left;
  167. padding-left:16px;
  168. cursor: hand;
  169. }
  170. ");
  171. }
  172. public function info($sText)
  173. {
  174. $this->add("<p class=\"info\">$sText</p>\n");
  175. $this->log_info($sText);
  176. }
  177. public function ok($sText)
  178. {
  179. $this->add("<p class=\"ok\">$sText</p>\n");
  180. $this->log_ok($sText);
  181. }
  182. public function warning($sText)
  183. {
  184. $this->add("<p class=\"warning\">$sText</p>\n");
  185. $this->log_warning($sText);
  186. }
  187. public function error($sText)
  188. {
  189. $this->add("<p class=\"error\">$sText</p>\n");
  190. $this->log_error($sText);
  191. }
  192. public function form($aData)
  193. {
  194. $this->add("<table class=\"formTable\">\n");
  195. foreach($aData as $aRow)
  196. {
  197. $this->add("<tr>\n");
  198. if (isset($aRow['label']) && isset($aRow['input']) && isset($aRow['help']))
  199. {
  200. $this->add("<td class=\"wizlabel\">{$aRow['label']}</td>\n");
  201. $this->add("<td class=\"wizinput\">{$aRow['input']}</td>\n");
  202. $this->add("<td class=\"wizhelp\">{$aRow['help']}</td>\n");
  203. }
  204. else if (isset($aRow['label']) && isset($aRow['help']))
  205. {
  206. $this->add("<td colspan=\"2\" class=\"wizlabel\">{$aRow['label']}</td>\n");
  207. $this->add("<td class=\"wizhelp\">{$aRow['help']}</td>\n");
  208. }
  209. else if (isset($aRow['label']) && isset($aRow['input']))
  210. {
  211. $this->add("<td class=\"wizlabel\">{$aRow['label']}</td>\n");
  212. $this->add("<td colspan=\"2\" class=\"wizinput\">{$aRow['input']}</td>\n");
  213. }
  214. else if (isset($aRow['label']))
  215. {
  216. $this->add("<td colspan=\"3\" class=\"wizlabel\">{$aRow['label']}</td>\n");
  217. }
  218. $this->add("</tr>\n");
  219. }
  220. $this->add("</table>\n");
  221. }
  222. public function collapsible($sId, $sTitle, $aItems, $bOpen = true)
  223. {
  224. $this->add("<h3 class=\"clickable open\" id=\"{$sId}\">$sTitle</h3>");
  225. $this->p('<ul id="'.$sId.'_list">');
  226. foreach($aItems as $sItem)
  227. {
  228. $this->p("<li>$sItem</li>\n");
  229. }
  230. $this->p('</ul>');
  231. $this->add_ready_script("$('#{$sId}').click( function() { $(this).toggleClass('open'); $('#{$sId}_list').toggle();} );\n");
  232. if (!$bOpen)
  233. {
  234. $this->add_ready_script("$('#{$sId}').toggleClass('open'); $('#{$sId}_list').toggle();\n");
  235. }
  236. }
  237. public function output()
  238. {
  239. $this->s_content = "<div id=\"header\"><h1><a href=\"http://www.combodo.com/itop\" target=\"_blank\"><img title=\"iTop by Combodo\" src=\"../images/itop-logo.png\"></a>&nbsp;".htmlentities($this->s_title, ENT_QUOTES, 'UTF-8')."</h1>\n</div><div id=\"setup\">{$this->s_content}\n</div>\n";
  240. return parent::output();
  241. }
  242. public static function log_error($sText)
  243. {
  244. self::log("Error - ".$sText);
  245. }
  246. public static function log_warning($sText)
  247. {
  248. self::log("Warning - ".$sText);
  249. }
  250. public static function log_info($sText)
  251. {
  252. self::log("Info - ".$sText);
  253. }
  254. public static function log_ok($sText)
  255. {
  256. self::log("Ok - ".$sText);
  257. }
  258. public static function log($sText)
  259. {
  260. $hLogFile = @fopen(INSTALL_LOG_FILE, 'a');
  261. if ($hLogFile !== false)
  262. {
  263. $sDate = date('Y-m-d H:i:s');
  264. fwrite($hLogFile, "$sDate - $sText\n");
  265. fclose($hLogFile);
  266. }
  267. }
  268. } // End of class
  269. /**
  270. * Helper function to initialize a configuration from the page arguments
  271. */
  272. function UpdateConfigSettings(&$oConfig, $aParamValues, $sModulesDir = null)
  273. {
  274. if (isset($aParamValues['application_path']))
  275. {
  276. $oConfig->Set('app_root_url', $aParamValues['application_path']);
  277. }
  278. if (isset($aParamValues['mode']) && isset($aParamValues['language']))
  279. {
  280. if (($aParamValues['mode'] == 'install') || $oConfig->GetDefaultLanguage() == '')
  281. {
  282. $oConfig->SetDefaultLanguage($aParamValues['language']);
  283. }
  284. }
  285. if (isset($aParamValues['db_server']))
  286. {
  287. $oConfig->SetDBHost($aParamValues['db_server']);
  288. $oConfig->SetDBUser($aParamValues['db_user']);
  289. $oConfig->SetDBPwd($aParamValues['db_pwd']);
  290. $sDBName = $aParamValues['db_name'];
  291. if ($sDBName == '')
  292. {
  293. $sDBName = $aParamValues['new_db_name'];
  294. }
  295. $oConfig->SetDBName($sDBName);
  296. $oConfig->SetDBSubname($aParamValues['db_prefix']);
  297. }
  298. if (!is_null($sModulesDir))
  299. {
  300. if (isset($aParamValues['selected_modules']))
  301. {
  302. $aSelectedModules = explode(',', $aParamValues['selected_modules']);
  303. }
  304. else
  305. {
  306. $aSelectedModules = null;
  307. }
  308. // Initialize the arrays below with default values for the application...
  309. $oEmptyConfig = new Config('dummy_file', false); // Do NOT load any config file, just set the default values
  310. $aAddOns = $oEmptyConfig->GetAddOns();
  311. $aAppModules = $oEmptyConfig->GetAppModules();
  312. $aDataModels = $oEmptyConfig->GetDataModels();
  313. $aWebServiceCategories = $oEmptyConfig->GetWebServiceCategories();
  314. $aDictionaries = $oEmptyConfig->GetDictionaries();
  315. // Merge the values with the ones provided by the modules
  316. // Make sure when don't load the same file twice...
  317. $aModules = ModuleDiscovery::GetAvailableModules(APPROOT, $sModulesDir);
  318. foreach($aModules as $sModuleId => $aModuleInfo)
  319. {
  320. list($sModuleName, $sModuleVersion) = ModuleDiscovery::GetModuleName($sModuleId);
  321. if (is_null($aSelectedModules) || in_array($sModuleName, $aSelectedModules))
  322. {
  323. if (isset($aModuleInfo['datamodel']))
  324. {
  325. $aDataModels = array_unique(array_merge($aDataModels, $aModuleInfo['datamodel']));
  326. }
  327. if (isset($aModuleInfo['webservice']))
  328. {
  329. $aWebServiceCategories = array_unique(array_merge($aWebServiceCategories, $aModuleInfo['webservice']));
  330. }
  331. if (isset($aModuleInfo['dictionary']))
  332. {
  333. $aDictionaries = array_unique(array_merge($aDictionaries, $aModuleInfo['dictionary']));
  334. }
  335. if (isset($aModuleInfo['settings']))
  336. {
  337. foreach($aModuleInfo['settings'] as $sProperty => $value)
  338. {
  339. list($sName, $sVersion) = ModuleDiscovery::GetModuleName($sModuleId);
  340. $oConfig->SetModuleSetting($sName, $sProperty, $value);
  341. }
  342. }
  343. if (isset($aModuleInfo['installer']))
  344. {
  345. $sModuleInstallerClass = $aModuleInfo['installer'];
  346. if (!class_exists($sModuleInstallerClass))
  347. {
  348. throw new Exception("Wrong installer class: '$sModuleInstallerClass' is not a PHP class - Module: ".$aModuleInfo['label']);
  349. }
  350. if (!is_subclass_of($sModuleInstallerClass, 'ModuleInstallerAPI'))
  351. {
  352. throw new Exception("Wrong installer class: '$sModuleInstallerClass' is not derived from 'ModuleInstallerAPI' - Module: ".$aModuleInfo['label']);
  353. }
  354. $aCallSpec = array($sModuleInstallerClass, 'BeforeWritingConfig');
  355. $oConfig = call_user_func_array($aCallSpec, array($oConfig));
  356. }
  357. }
  358. }
  359. $oConfig->SetAddOns($aAddOns);
  360. $oConfig->SetAppModules($aAppModules);
  361. $oConfig->SetDataModels($aDataModels);
  362. $oConfig->SetWebServiceCategories($aWebServiceCategories);
  363. $oConfig->SetDictionaries($aDictionaries);
  364. }
  365. }
  366. /**
  367. * Helper function to initialize the ORM and load the data model
  368. * from the given file
  369. * @param $oConfig object The configuration (volatile, not necessarily already on disk)
  370. * @param $bModelOnly boolean Whether or not to allow loading a data model with no corresponding DB
  371. * @return none
  372. */
  373. function InitDataModel($oConfig, $bModelOnly = true, $bUseCache = false)
  374. {
  375. require_once(APPROOT.'/core/log.class.inc.php');
  376. require_once(APPROOT.'/core/kpi.class.inc.php');
  377. require_once(APPROOT.'/core/coreexception.class.inc.php');
  378. require_once(APPROOT.'/core/dict.class.inc.php');
  379. require_once(APPROOT.'/core/attributedef.class.inc.php');
  380. require_once(APPROOT.'/core/filterdef.class.inc.php');
  381. require_once(APPROOT.'/core/stimulus.class.inc.php');
  382. require_once(APPROOT.'/core/MyHelpers.class.inc.php');
  383. require_once(APPROOT.'/core/expression.class.inc.php');
  384. require_once(APPROOT.'/core/cmdbsource.class.inc.php');
  385. require_once(APPROOT.'/core/sqlquery.class.inc.php');
  386. require_once(APPROOT.'/core/dbobject.class.php');
  387. require_once(APPROOT.'/core/dbobjectsearch.class.php');
  388. require_once(APPROOT.'/core/dbobjectset.class.php');
  389. require_once(APPROOT.'/application/cmdbabstract.class.inc.php');
  390. require_once(APPROOT.'/core/userrights.class.inc.php');
  391. require_once(APPROOT.'/setup/moduleinstallation.class.inc.php');
  392. $sConfigFile = $oConfig->GetLoadedFile();
  393. if (strlen($sConfigFile) > 0)
  394. {
  395. SetupPage::log_info("MetaModel::Startup from $sConfigFile (ModelOnly = $bModelOnly)");
  396. }
  397. else
  398. {
  399. SetupPage::log_info("MetaModel::Startup (ModelOnly = $bModelOnly)");
  400. }
  401. if ($bUseCache)
  402. {
  403. // Reset the cache for the first use !
  404. MetaModel::ResetCache($oConfig);
  405. }
  406. MetaModel::Startup($oConfig, $bModelOnly, $bUseCache);
  407. }
  408. /**
  409. * Analyzes the current installation and the possibilities
  410. *
  411. * @param $oConfig Config Defines the target environment (DB)
  412. * @return hash Array with the following format:
  413. * array =>
  414. * 'iTop' => array(
  415. * 'version_db' => ... (could be empty in case of a fresh install)
  416. * 'version_code => ...
  417. * )
  418. * <module_name> => array(
  419. * 'version_db' => ...
  420. * 'version_code' => ...
  421. * 'install' => array(
  422. * 'flag' => SETUP_NEVER | SETUP_OPTIONAL | SETUP_MANDATORY
  423. * 'message' => ...
  424. * )
  425. * 'uninstall' => array(
  426. * 'flag' => SETUP_NEVER | SETUP_OPTIONAL | SETUP_MANDATORY
  427. * 'message' => ...
  428. * )
  429. * 'label' => ...
  430. * 'dependencies' => array(<module1>, <module2>, ...)
  431. * 'visible' => true | false
  432. * )
  433. * )
  434. */
  435. function AnalyzeInstallation($oConfig, $sModulesRelativePath)
  436. {
  437. $aRes = array(
  438. ROOT_MODULE => array(
  439. 'version_db' => '',
  440. 'name_db' => '',
  441. 'version_code' => ITOP_VERSION.'.'.ITOP_REVISION,
  442. 'name_code' => ITOP_APPLICATION,
  443. )
  444. );
  445. $aModules = ModuleDiscovery::GetAvailableModules(APPROOT, $sModulesRelativePath);
  446. foreach($aModules as $sModuleId => $aModuleInfo)
  447. {
  448. list($sModuleName, $sModuleVersion) = ModuleDiscovery::GetModuleName($sModuleId);
  449. $sModuleAppVersion = $aModuleInfo['itop_version'];
  450. $aModuleInfo['version_db'] = '';
  451. $aModuleInfo['version_code'] = $sModuleVersion;
  452. if (!in_array($sModuleAppVersion, array('1.0.0', '1.0.1', '1.0.2')))
  453. {
  454. // This module is NOT compatible with the current version
  455. $aModuleInfo['install'] = array(
  456. 'flag' => MODULE_ACTION_IMPOSSIBLE,
  457. 'message' => 'the module is not compatible with the current version of the application'
  458. );
  459. }
  460. elseif ($aModuleInfo['mandatory'])
  461. {
  462. $aModuleInfo['install'] = array(
  463. 'flag' => MODULE_ACTION_MANDATORY,
  464. 'message' => 'the module is part of the application'
  465. );
  466. }
  467. else
  468. {
  469. $aModuleInfo['install'] = array(
  470. 'flag' => MODULE_ACTION_OPTIONAL,
  471. 'message' => ''
  472. );
  473. }
  474. $aRes[$sModuleName] = $aModuleInfo;
  475. }
  476. try
  477. {
  478. CMDBSource::Init($oConfig->GetDBHost(), $oConfig->GetDBUser(), $oConfig->GetDBPwd(), $oConfig->GetDBName());
  479. $aSelectInstall = CMDBSource::QueryToArray("SELECT * FROM ".$oConfig->GetDBSubname()."priv_module_install");
  480. }
  481. catch (MySQLException $e)
  482. {
  483. // No database or eroneous information
  484. $aSelectInstall = array();
  485. }
  486. // Build the list of installed module (get the latest installation)
  487. //
  488. $aInstallByModule = array(); // array of <module> => array ('installed' => timestamp, 'version' => <version>)
  489. foreach ($aSelectInstall as $aInstall)
  490. {
  491. //$aInstall['comment']; // unsused
  492. $iInstalled = strtotime($aInstall['installed']);
  493. $sModuleName = $aInstall['name'];
  494. $sModuleVersion = $aInstall['version'];
  495. if ($aInstall['parent_id'] == 0)
  496. {
  497. $sModuleName = ROOT_MODULE;
  498. }
  499. if (array_key_exists($sModuleName, $aInstallByModule))
  500. {
  501. if ($iInstalled < $aInstallByModule[$sModuleName]['installed'])
  502. {
  503. continue;
  504. }
  505. }
  506. if ($aInstall['parent_id'] == 0)
  507. {
  508. $aRes[$sModuleName]['version_db'] = $sModuleVersion;
  509. $aRes[$sModuleName]['name_db'] = $aInstall['name'];
  510. }
  511. $aInstallByModule[$sModuleName]['installed'] = $iInstalled;
  512. $aInstallByModule[$sModuleName]['version'] = $sModuleVersion;
  513. }
  514. // Adjust the list of proposed modules
  515. //
  516. foreach ($aInstallByModule as $sModuleName => $aModuleDB)
  517. {
  518. if ($sModuleName == ROOT_MODULE) continue; // Skip the main module
  519. if (!array_key_exists($sModuleName, $aRes))
  520. {
  521. // A module was installed, it is not proposed in the new build... skip
  522. continue;
  523. }
  524. $aRes[$sModuleName]['version_db'] = $aModuleDB['version'];
  525. if ($aRes[$sModuleName]['install']['flag'] == MODULE_ACTION_MANDATORY)
  526. {
  527. $aRes[$sModuleName]['uninstall'] = array(
  528. 'flag' => MODULE_ACTION_IMPOSSIBLE,
  529. 'message' => 'the module is part of the application'
  530. );
  531. }
  532. else
  533. {
  534. $aRes[$sModuleName]['uninstall'] = array(
  535. 'flag' => MODULE_ACTION_OPTIONAL,
  536. 'message' => ''
  537. );
  538. }
  539. }
  540. return $aRes;
  541. }
  542. /**
  543. * Helper function to create the database structure
  544. * @return boolean true on success, false otherwise
  545. */
  546. function CreateDatabaseStructure(Config $oConfig, $sMode)
  547. {
  548. if (strlen($oConfig->GetDBSubname()) > 0)
  549. {
  550. SetupPage::log_info("Creating the structure in '".$oConfig->GetDBName()."' (table names prefixed by '".$oConfig->GetDBSubname()."').");
  551. }
  552. else
  553. {
  554. SetupPage::log_info("Creating the structure in '".$oConfig->GetDBSubname()."'.");
  555. }
  556. //MetaModel::CheckDefinitions();
  557. if ($sMode == 'install')
  558. {
  559. if (!MetaModel::DBExists(/* bMustBeComplete */ false))
  560. {
  561. MetaModel::DBCreate();
  562. SetupPage::log_ok("Database structure successfully created.");
  563. }
  564. else
  565. {
  566. if (strlen($oConfig->GetDBSubname()) > 0)
  567. {
  568. throw new Exception("Error: found iTop tables into the database '".$oConfig->GetDBName()."' (prefix: '".$oConfig->GetDBSubname()."'). Please, try selecting another database instance or specify another prefix to prevent conflicting table names.");
  569. }
  570. else
  571. {
  572. throw new Exception("Error: found iTop tables into the database '".$oConfig->GetDBName()."'. Please, try selecting another database instance or specify a prefix to prevent conflicting table names.");
  573. }
  574. }
  575. }
  576. else
  577. {
  578. if (MetaModel::DBExists(/* bMustBeComplete */ false))
  579. {
  580. MetaModel::DBCreate();
  581. SetupPage::log_ok("Database structure successfully updated.");
  582. // Check (and update only if it seems needed) the hierarchical keys
  583. ob_start();
  584. MetaModel::CheckHKeys(false /* bDiagnosticsOnly */, true /* bVerbose*/, true /* bForceUpdate */); // Since in 1.2-beta the detection was buggy, let's force the rebuilding of HKeys
  585. $sFeedback = ob_get_clean();
  586. SetupPage::log_ok("Hierchical keys rebuilt: $sFeedback");
  587. // Check (and fix) data sync configuration
  588. ob_start();
  589. MetaModel::CheckDataSources(false /*$bDiagnostics*/, true/*$bVerbose*/);
  590. $sFeedback = ob_get_clean();
  591. SetupPage::log_ok("Data sources checked: $sFeedback");
  592. }
  593. else
  594. {
  595. if (strlen($oConfig->GetDBSubname()) > 0)
  596. {
  597. throw new Exception("Error: No previous instance of iTop found into the database '".$oConfig->GetDBName()."' (prefix: '".$oConfig->GetDBSubname()."'). Please, try selecting another database instance.");
  598. }
  599. else
  600. {
  601. throw new Exception("Error: No previous instance of iTop found into the database '".$oConfig->GetDBName()."'. Please, try selecting another database instance.");
  602. }
  603. }
  604. }
  605. return true;
  606. }
  607. function RecordInstallation(Config $oConfig, $aSelectedModules, $sModulesRelativePath)
  608. {
  609. // Record main installation
  610. $oInstallRec = new ModuleInstallation();
  611. $oInstallRec->Set('name', ITOP_APPLICATION);
  612. $oInstallRec->Set('version', ITOP_VERSION.'.'.ITOP_REVISION);
  613. $oInstallRec->Set('comment', "Done by the setup program\nBuilt on ".ITOP_BUILD_DATE);
  614. $oInstallRec->Set('parent_id', 0); // root module
  615. $iMainItopRecord = $oInstallRec->DBInsertNoReload();
  616. // Record installed modules
  617. //
  618. $aAvailableModules = AnalyzeInstallation($oConfig, $sModulesRelativePath);
  619. foreach($aSelectedModules as $sModuleId)
  620. {
  621. $aModuleData = $aAvailableModules[$sModuleId];
  622. $sName = $sModuleId;
  623. $sVersion = $aModuleData['version_code'];
  624. $aComments = array();
  625. $aComments[] = 'Done by the setup program';
  626. if ($aModuleData['mandatory'])
  627. {
  628. $aComments[] = 'Mandatory';
  629. }
  630. else
  631. {
  632. $aComments[] = 'Optional';
  633. }
  634. if ($aModuleData['visible'])
  635. {
  636. $aComments[] = 'Visible (during the setup)';
  637. }
  638. else
  639. {
  640. $aComments[] = 'Hidden (selected automatically)';
  641. }
  642. foreach ($aModuleData['dependencies'] as $sDependOn)
  643. {
  644. $aComments[] = "Depends on module: $sDependOn";
  645. }
  646. $sComment = implode("\n", $aComments);
  647. $oInstallRec = new ModuleInstallation();
  648. $oInstallRec->Set('name', $sName);
  649. $oInstallRec->Set('version', $sVersion);
  650. $oInstallRec->Set('comment', $sComment);
  651. $oInstallRec->Set('parent_id', $iMainItopRecord);
  652. $oInstallRec->DBInsertNoReload();
  653. }
  654. // Database is created, installation has been tracked into it
  655. return true;
  656. }
  657. ?>