setuppage.class.inc.php 21 KB

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