setuppage.class.inc.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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."setup/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/setup.js");
  39. $this->add_linked_script("../setup/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. /**
  173. * Overriden because the application is not fully loaded when the setup is being run
  174. */
  175. public function GetAbsoluteUrlAppRoot()
  176. {
  177. return '../';
  178. }
  179. /**
  180. * Overriden because the application is not fully loaded when the setup is being run
  181. */
  182. public function GetAbsoluteUrlModulesRoot()
  183. {
  184. return $this->GetAbsoluteUrlAppRoot().utils::GetCurrentEnvironment();
  185. }
  186. /**
  187. * Overriden because the application is not fully loaded when the setup is being run
  188. */
  189. function GetApplicationContext()
  190. {
  191. return '';
  192. }
  193. public function info($sText)
  194. {
  195. $this->add("<p class=\"info\">$sText</p>\n");
  196. $this->log_info($sText);
  197. }
  198. public function ok($sText)
  199. {
  200. $this->add("<p class=\"ok\">$sText</p>\n");
  201. $this->log_ok($sText);
  202. }
  203. public function warning($sText)
  204. {
  205. $this->add("<p class=\"warning\">$sText</p>\n");
  206. $this->log_warning($sText);
  207. }
  208. public function error($sText)
  209. {
  210. $this->add("<p class=\"error\">$sText</p>\n");
  211. $this->log_error($sText);
  212. }
  213. public function form($aData)
  214. {
  215. $this->add("<table class=\"formTable\">\n");
  216. foreach($aData as $aRow)
  217. {
  218. $this->add("<tr>\n");
  219. if (isset($aRow['label']) && isset($aRow['input']) && isset($aRow['help']))
  220. {
  221. $this->add("<td class=\"wizlabel\">{$aRow['label']}</td>\n");
  222. $this->add("<td class=\"wizinput\">{$aRow['input']}</td>\n");
  223. $this->add("<td class=\"wizhelp\">{$aRow['help']}</td>\n");
  224. }
  225. else if (isset($aRow['label']) && isset($aRow['help']))
  226. {
  227. $this->add("<td colspan=\"2\" class=\"wizlabel\">{$aRow['label']}</td>\n");
  228. $this->add("<td class=\"wizhelp\">{$aRow['help']}</td>\n");
  229. }
  230. else if (isset($aRow['label']) && isset($aRow['input']))
  231. {
  232. $this->add("<td class=\"wizlabel\">{$aRow['label']}</td>\n");
  233. $this->add("<td colspan=\"2\" class=\"wizinput\">{$aRow['input']}</td>\n");
  234. }
  235. else if (isset($aRow['label']))
  236. {
  237. $this->add("<td colspan=\"3\" class=\"wizlabel\">{$aRow['label']}</td>\n");
  238. }
  239. $this->add("</tr>\n");
  240. }
  241. $this->add("</table>\n");
  242. }
  243. public function collapsible($sId, $sTitle, $aItems, $bOpen = true)
  244. {
  245. $this->add("<h3 class=\"clickable open\" id=\"{$sId}\">$sTitle</h3>");
  246. $this->p('<ul id="'.$sId.'_list">');
  247. foreach($aItems as $sItem)
  248. {
  249. $this->p("<li>$sItem</li>\n");
  250. }
  251. $this->p('</ul>');
  252. $this->add_ready_script("$('#{$sId}').click( function() { $(this).toggleClass('open'); $('#{$sId}_list').toggle();} );\n");
  253. if (!$bOpen)
  254. {
  255. $this->add_ready_script("$('#{$sId}').toggleClass('open'); $('#{$sId}_list').toggle();\n");
  256. }
  257. }
  258. public function output()
  259. {
  260. $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";
  261. return parent::output();
  262. }
  263. public static function log_error($sText)
  264. {
  265. self::log("Error - ".$sText);
  266. }
  267. public static function log_warning($sText)
  268. {
  269. self::log("Warning - ".$sText);
  270. }
  271. public static function log_info($sText)
  272. {
  273. self::log("Info - ".$sText);
  274. }
  275. public static function log_ok($sText)
  276. {
  277. self::log("Ok - ".$sText);
  278. }
  279. public static function log($sText)
  280. {
  281. $hLogFile = @fopen(INSTALL_LOG_FILE, 'a');
  282. if ($hLogFile !== false)
  283. {
  284. $sDate = date('Y-m-d H:i:s');
  285. fwrite($hLogFile, "$sDate - $sText\n");
  286. fclose($hLogFile);
  287. }
  288. }
  289. } // End of class
  290. /**
  291. * Helper function to initialize the ORM and load the data model
  292. * from the given file
  293. * @param $oConfig object The configuration (volatile, not necessarily already on disk)
  294. * @param $bModelOnly boolean Whether or not to allow loading a data model with no corresponding DB
  295. * @return none
  296. */
  297. function InitDataModel($oConfig, $bModelOnly = true, $bUseCache = false)
  298. {
  299. require_once(APPROOT.'/core/log.class.inc.php');
  300. require_once(APPROOT.'/core/kpi.class.inc.php');
  301. require_once(APPROOT.'/core/coreexception.class.inc.php');
  302. require_once(APPROOT.'/core/dict.class.inc.php');
  303. require_once(APPROOT.'/core/attributedef.class.inc.php');
  304. require_once(APPROOT.'/core/filterdef.class.inc.php');
  305. require_once(APPROOT.'/core/stimulus.class.inc.php');
  306. require_once(APPROOT.'/core/MyHelpers.class.inc.php');
  307. require_once(APPROOT.'/core/expression.class.inc.php');
  308. require_once(APPROOT.'/core/cmdbsource.class.inc.php');
  309. require_once(APPROOT.'/core/sqlquery.class.inc.php');
  310. require_once(APPROOT.'/core/dbobject.class.php');
  311. require_once(APPROOT.'/core/dbobjectsearch.class.php');
  312. require_once(APPROOT.'/core/dbobjectset.class.php');
  313. require_once(APPROOT.'/application/cmdbabstract.class.inc.php');
  314. require_once(APPROOT.'/core/userrights.class.inc.php');
  315. require_once(APPROOT.'/setup/moduleinstallation.class.inc.php');
  316. $sConfigFile = $oConfig->GetLoadedFile();
  317. if (strlen($sConfigFile) > 0)
  318. {
  319. SetupPage::log_info("MetaModel::Startup from $sConfigFile (ModelOnly = $bModelOnly)");
  320. }
  321. else
  322. {
  323. SetupPage::log_info("MetaModel::Startup (ModelOnly = $bModelOnly)");
  324. }
  325. if (!$bUseCache)
  326. {
  327. // Reset the cache for the first use !
  328. MetaModel::ResetCache($oConfig);
  329. }
  330. MetaModel::Startup($oConfig, $bModelOnly, $bUseCache);
  331. }
  332. /**
  333. * Analyzes the current installation and the possibilities
  334. *
  335. * @param $oConfig Config Defines the target environment (DB)
  336. * @return hash Array with the following format:
  337. * array =>
  338. * 'iTop' => array(
  339. * 'version_db' => ... (could be empty in case of a fresh install)
  340. * 'version_code => ...
  341. * )
  342. * <module_name> => array(
  343. * 'version_db' => ...
  344. * 'version_code' => ...
  345. * 'install' => array(
  346. * 'flag' => SETUP_NEVER | SETUP_OPTIONAL | SETUP_MANDATORY
  347. * 'message' => ...
  348. * )
  349. * 'uninstall' => array(
  350. * 'flag' => SETUP_NEVER | SETUP_OPTIONAL | SETUP_MANDATORY
  351. * 'message' => ...
  352. * )
  353. * 'label' => ...
  354. * 'dependencies' => array(<module1>, <module2>, ...)
  355. * 'visible' => true | false
  356. * )
  357. * )
  358. */
  359. function AnalyzeInstallation($oConfig, $sModulesRelativePath)
  360. {
  361. $aRes = array(
  362. ROOT_MODULE => array(
  363. 'version_db' => '',
  364. 'name_db' => '',
  365. 'version_code' => ITOP_VERSION.'.'.ITOP_REVISION,
  366. 'name_code' => ITOP_APPLICATION,
  367. )
  368. );
  369. $aModules = ModuleDiscovery::GetAvailableModules(APPROOT, $sModulesRelativePath);
  370. foreach($aModules as $sModuleId => $aModuleInfo)
  371. {
  372. list($sModuleName, $sModuleVersion) = ModuleDiscovery::GetModuleName($sModuleId);
  373. $sModuleAppVersion = $aModuleInfo['itop_version'];
  374. $aModuleInfo['version_db'] = '';
  375. $aModuleInfo['version_code'] = $sModuleVersion;
  376. if (!in_array($sModuleAppVersion, array('1.0.0', '1.0.1', '1.0.2')))
  377. {
  378. // This module is NOT compatible with the current version
  379. $aModuleInfo['install'] = array(
  380. 'flag' => MODULE_ACTION_IMPOSSIBLE,
  381. 'message' => 'the module is not compatible with the current version of the application'
  382. );
  383. }
  384. elseif ($aModuleInfo['mandatory'])
  385. {
  386. $aModuleInfo['install'] = array(
  387. 'flag' => MODULE_ACTION_MANDATORY,
  388. 'message' => 'the module is part of the application'
  389. );
  390. }
  391. else
  392. {
  393. $aModuleInfo['install'] = array(
  394. 'flag' => MODULE_ACTION_OPTIONAL,
  395. 'message' => ''
  396. );
  397. }
  398. $aRes[$sModuleName] = $aModuleInfo;
  399. }
  400. try
  401. {
  402. CMDBSource::Init($oConfig->GetDBHost(), $oConfig->GetDBUser(), $oConfig->GetDBPwd(), $oConfig->GetDBName());
  403. $aSelectInstall = CMDBSource::QueryToArray("SELECT * FROM ".$oConfig->GetDBSubname()."priv_module_install");
  404. }
  405. catch (MySQLException $e)
  406. {
  407. // No database or eroneous information
  408. $aSelectInstall = array();
  409. }
  410. // Build the list of installed module (get the latest installation)
  411. //
  412. $aInstallByModule = array(); // array of <module> => array ('installed' => timestamp, 'version' => <version>)
  413. foreach ($aSelectInstall as $aInstall)
  414. {
  415. //$aInstall['comment']; // unsused
  416. $iInstalled = strtotime($aInstall['installed']);
  417. $sModuleName = $aInstall['name'];
  418. $sModuleVersion = $aInstall['version'];
  419. if ($aInstall['parent_id'] == 0)
  420. {
  421. $sModuleName = ROOT_MODULE;
  422. }
  423. if (array_key_exists($sModuleName, $aInstallByModule))
  424. {
  425. if ($iInstalled < $aInstallByModule[$sModuleName]['installed'])
  426. {
  427. continue;
  428. }
  429. }
  430. if ($aInstall['parent_id'] == 0)
  431. {
  432. $aRes[$sModuleName]['version_db'] = $sModuleVersion;
  433. $aRes[$sModuleName]['name_db'] = $aInstall['name'];
  434. }
  435. $aInstallByModule[$sModuleName]['installed'] = $iInstalled;
  436. $aInstallByModule[$sModuleName]['version'] = $sModuleVersion;
  437. }
  438. // Adjust the list of proposed modules
  439. //
  440. foreach ($aInstallByModule as $sModuleName => $aModuleDB)
  441. {
  442. if ($sModuleName == ROOT_MODULE) continue; // Skip the main module
  443. if (!array_key_exists($sModuleName, $aRes))
  444. {
  445. // A module was installed, it is not proposed in the new build... skip
  446. continue;
  447. }
  448. $aRes[$sModuleName]['version_db'] = $aModuleDB['version'];
  449. if ($aRes[$sModuleName]['install']['flag'] == MODULE_ACTION_MANDATORY)
  450. {
  451. $aRes[$sModuleName]['uninstall'] = array(
  452. 'flag' => MODULE_ACTION_IMPOSSIBLE,
  453. 'message' => 'the module is part of the application'
  454. );
  455. }
  456. else
  457. {
  458. $aRes[$sModuleName]['uninstall'] = array(
  459. 'flag' => MODULE_ACTION_OPTIONAL,
  460. 'message' => ''
  461. );
  462. }
  463. }
  464. return $aRes;
  465. }
  466. /**
  467. * Helper function to create the database structure
  468. * @return boolean true on success, false otherwise
  469. */
  470. function CreateDatabaseStructure(Config $oConfig, $sMode)
  471. {
  472. if (strlen($oConfig->GetDBSubname()) > 0)
  473. {
  474. SetupPage::log_info("Creating the structure in '".$oConfig->GetDBName()."' (table names prefixed by '".$oConfig->GetDBSubname()."').");
  475. }
  476. else
  477. {
  478. SetupPage::log_info("Creating the structure in '".$oConfig->GetDBSubname()."'.");
  479. }
  480. //MetaModel::CheckDefinitions();
  481. if ($sMode == 'install')
  482. {
  483. if (!MetaModel::DBExists(/* bMustBeComplete */ false))
  484. {
  485. MetaModel::DBCreate();
  486. SetupPage::log_ok("Database structure successfully created.");
  487. }
  488. else
  489. {
  490. if (strlen($oConfig->GetDBSubname()) > 0)
  491. {
  492. 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.");
  493. }
  494. else
  495. {
  496. 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.");
  497. }
  498. }
  499. }
  500. else
  501. {
  502. if (MetaModel::DBExists(/* bMustBeComplete */ false))
  503. {
  504. MetaModel::DBCreate();
  505. SetupPage::log_ok("Database structure successfully updated.");
  506. // Check (and update only if it seems needed) the hierarchical keys
  507. ob_start();
  508. MetaModel::CheckHKeys(false /* bDiagnosticsOnly */, true /* bVerbose*/, true /* bForceUpdate */); // Since in 1.2-beta the detection was buggy, let's force the rebuilding of HKeys
  509. $sFeedback = ob_get_clean();
  510. SetupPage::log_ok("Hierchical keys rebuilt: $sFeedback");
  511. // Check (and fix) data sync configuration
  512. ob_start();
  513. MetaModel::CheckDataSources(false /*$bDiagnostics*/, true/*$bVerbose*/);
  514. $sFeedback = ob_get_clean();
  515. SetupPage::log_ok("Data sources checked: $sFeedback");
  516. }
  517. else
  518. {
  519. if (strlen($oConfig->GetDBSubname()) > 0)
  520. {
  521. throw new Exception("Error: No previous instance of iTop found into the database '".$oConfig->GetDBName()."' (prefix: '".$oConfig->GetDBSubname()."'). Please, try selecting another database instance.");
  522. }
  523. else
  524. {
  525. throw new Exception("Error: No previous instance of iTop found into the database '".$oConfig->GetDBName()."'. Please, try selecting another database instance.");
  526. }
  527. }
  528. }
  529. return true;
  530. }
  531. function RecordInstallation(Config $oConfig, $aSelectedModules, $sModulesRelativePath)
  532. {
  533. // Record main installation
  534. $oInstallRec = new ModuleInstallation();
  535. $oInstallRec->Set('name', ITOP_APPLICATION);
  536. $oInstallRec->Set('version', ITOP_VERSION.'.'.ITOP_REVISION);
  537. $oInstallRec->Set('comment', "Done by the setup program\nBuilt on ".ITOP_BUILD_DATE);
  538. $oInstallRec->Set('parent_id', 0); // root module
  539. $iMainItopRecord = $oInstallRec->DBInsertNoReload();
  540. // Record installed modules
  541. //
  542. $aAvailableModules = AnalyzeInstallation($oConfig, $sModulesRelativePath);
  543. foreach($aSelectedModules as $sModuleId)
  544. {
  545. $aModuleData = $aAvailableModules[$sModuleId];
  546. $sName = $sModuleId;
  547. $sVersion = $aModuleData['version_code'];
  548. $aComments = array();
  549. $aComments[] = 'Done by the setup program';
  550. if ($aModuleData['mandatory'])
  551. {
  552. $aComments[] = 'Mandatory';
  553. }
  554. else
  555. {
  556. $aComments[] = 'Optional';
  557. }
  558. if ($aModuleData['visible'])
  559. {
  560. $aComments[] = 'Visible (during the setup)';
  561. }
  562. else
  563. {
  564. $aComments[] = 'Hidden (selected automatically)';
  565. }
  566. foreach ($aModuleData['dependencies'] as $sDependOn)
  567. {
  568. $aComments[] = "Depends on module: $sDependOn";
  569. }
  570. $sComment = implode("\n", $aComments);
  571. $oInstallRec = new ModuleInstallation();
  572. $oInstallRec->Set('name', $sName);
  573. $oInstallRec->Set('version', $sVersion);
  574. $oInstallRec->Set('comment', $sComment);
  575. $oInstallRec->Set('parent_id', $iMainItopRecord);
  576. $oInstallRec->DBInsertNoReload();
  577. }
  578. // Database is created, installation has been tracked into it
  579. return true;
  580. }
  581. ?>