setuppage.class.inc.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. 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 the ORM and load the data model
  271. * from the given file
  272. * @param $oConfig object The configuration (volatile, not necessarily already on disk)
  273. * @param $bModelOnly boolean Whether or not to allow loading a data model with no corresponding DB
  274. * @return none
  275. */
  276. function InitDataModel($oConfig, $bModelOnly = true, $bUseCache = false)
  277. {
  278. require_once(APPROOT.'/core/log.class.inc.php');
  279. require_once(APPROOT.'/core/kpi.class.inc.php');
  280. require_once(APPROOT.'/core/coreexception.class.inc.php');
  281. require_once(APPROOT.'/core/dict.class.inc.php');
  282. require_once(APPROOT.'/core/attributedef.class.inc.php');
  283. require_once(APPROOT.'/core/filterdef.class.inc.php');
  284. require_once(APPROOT.'/core/stimulus.class.inc.php');
  285. require_once(APPROOT.'/core/MyHelpers.class.inc.php');
  286. require_once(APPROOT.'/core/expression.class.inc.php');
  287. require_once(APPROOT.'/core/cmdbsource.class.inc.php');
  288. require_once(APPROOT.'/core/sqlquery.class.inc.php');
  289. require_once(APPROOT.'/core/dbobject.class.php');
  290. require_once(APPROOT.'/core/dbobjectsearch.class.php');
  291. require_once(APPROOT.'/core/dbobjectset.class.php');
  292. require_once(APPROOT.'/application/cmdbabstract.class.inc.php');
  293. require_once(APPROOT.'/core/userrights.class.inc.php');
  294. require_once(APPROOT.'/setup/moduleinstallation.class.inc.php');
  295. $sConfigFile = $oConfig->GetLoadedFile();
  296. if (strlen($sConfigFile) > 0)
  297. {
  298. SetupPage::log_info("MetaModel::Startup from $sConfigFile (ModelOnly = $bModelOnly)");
  299. }
  300. else
  301. {
  302. SetupPage::log_info("MetaModel::Startup (ModelOnly = $bModelOnly)");
  303. }
  304. if (!$bUseCache)
  305. {
  306. // Reset the cache for the first use !
  307. MetaModel::ResetCache($oConfig);
  308. }
  309. MetaModel::Startup($oConfig, $bModelOnly, $bUseCache);
  310. }
  311. /**
  312. * Analyzes the current installation and the possibilities
  313. *
  314. * @param $oConfig Config Defines the target environment (DB)
  315. * @return hash Array with the following format:
  316. * array =>
  317. * 'iTop' => array(
  318. * 'version_db' => ... (could be empty in case of a fresh install)
  319. * 'version_code => ...
  320. * )
  321. * <module_name> => array(
  322. * 'version_db' => ...
  323. * 'version_code' => ...
  324. * 'install' => array(
  325. * 'flag' => SETUP_NEVER | SETUP_OPTIONAL | SETUP_MANDATORY
  326. * 'message' => ...
  327. * )
  328. * 'uninstall' => array(
  329. * 'flag' => SETUP_NEVER | SETUP_OPTIONAL | SETUP_MANDATORY
  330. * 'message' => ...
  331. * )
  332. * 'label' => ...
  333. * 'dependencies' => array(<module1>, <module2>, ...)
  334. * 'visible' => true | false
  335. * )
  336. * )
  337. */
  338. function AnalyzeInstallation($oConfig, $sModulesRelativePath)
  339. {
  340. $aRes = array(
  341. ROOT_MODULE => array(
  342. 'version_db' => '',
  343. 'name_db' => '',
  344. 'version_code' => ITOP_VERSION.'.'.ITOP_REVISION,
  345. 'name_code' => ITOP_APPLICATION,
  346. )
  347. );
  348. $aModules = ModuleDiscovery::GetAvailableModules(APPROOT, $sModulesRelativePath);
  349. foreach($aModules as $sModuleId => $aModuleInfo)
  350. {
  351. list($sModuleName, $sModuleVersion) = ModuleDiscovery::GetModuleName($sModuleId);
  352. $sModuleAppVersion = $aModuleInfo['itop_version'];
  353. $aModuleInfo['version_db'] = '';
  354. $aModuleInfo['version_code'] = $sModuleVersion;
  355. if (!in_array($sModuleAppVersion, array('1.0.0', '1.0.1', '1.0.2')))
  356. {
  357. // This module is NOT compatible with the current version
  358. $aModuleInfo['install'] = array(
  359. 'flag' => MODULE_ACTION_IMPOSSIBLE,
  360. 'message' => 'the module is not compatible with the current version of the application'
  361. );
  362. }
  363. elseif ($aModuleInfo['mandatory'])
  364. {
  365. $aModuleInfo['install'] = array(
  366. 'flag' => MODULE_ACTION_MANDATORY,
  367. 'message' => 'the module is part of the application'
  368. );
  369. }
  370. else
  371. {
  372. $aModuleInfo['install'] = array(
  373. 'flag' => MODULE_ACTION_OPTIONAL,
  374. 'message' => ''
  375. );
  376. }
  377. $aRes[$sModuleName] = $aModuleInfo;
  378. }
  379. try
  380. {
  381. CMDBSource::Init($oConfig->GetDBHost(), $oConfig->GetDBUser(), $oConfig->GetDBPwd(), $oConfig->GetDBName());
  382. $aSelectInstall = CMDBSource::QueryToArray("SELECT * FROM ".$oConfig->GetDBSubname()."priv_module_install");
  383. }
  384. catch (MySQLException $e)
  385. {
  386. // No database or eroneous information
  387. $aSelectInstall = array();
  388. }
  389. // Build the list of installed module (get the latest installation)
  390. //
  391. $aInstallByModule = array(); // array of <module> => array ('installed' => timestamp, 'version' => <version>)
  392. foreach ($aSelectInstall as $aInstall)
  393. {
  394. //$aInstall['comment']; // unsused
  395. $iInstalled = strtotime($aInstall['installed']);
  396. $sModuleName = $aInstall['name'];
  397. $sModuleVersion = $aInstall['version'];
  398. if ($aInstall['parent_id'] == 0)
  399. {
  400. $sModuleName = ROOT_MODULE;
  401. }
  402. if (array_key_exists($sModuleName, $aInstallByModule))
  403. {
  404. if ($iInstalled < $aInstallByModule[$sModuleName]['installed'])
  405. {
  406. continue;
  407. }
  408. }
  409. if ($aInstall['parent_id'] == 0)
  410. {
  411. $aRes[$sModuleName]['version_db'] = $sModuleVersion;
  412. $aRes[$sModuleName]['name_db'] = $aInstall['name'];
  413. }
  414. $aInstallByModule[$sModuleName]['installed'] = $iInstalled;
  415. $aInstallByModule[$sModuleName]['version'] = $sModuleVersion;
  416. }
  417. // Adjust the list of proposed modules
  418. //
  419. foreach ($aInstallByModule as $sModuleName => $aModuleDB)
  420. {
  421. if ($sModuleName == ROOT_MODULE) continue; // Skip the main module
  422. if (!array_key_exists($sModuleName, $aRes))
  423. {
  424. // A module was installed, it is not proposed in the new build... skip
  425. continue;
  426. }
  427. $aRes[$sModuleName]['version_db'] = $aModuleDB['version'];
  428. if ($aRes[$sModuleName]['install']['flag'] == MODULE_ACTION_MANDATORY)
  429. {
  430. $aRes[$sModuleName]['uninstall'] = array(
  431. 'flag' => MODULE_ACTION_IMPOSSIBLE,
  432. 'message' => 'the module is part of the application'
  433. );
  434. }
  435. else
  436. {
  437. $aRes[$sModuleName]['uninstall'] = array(
  438. 'flag' => MODULE_ACTION_OPTIONAL,
  439. 'message' => ''
  440. );
  441. }
  442. }
  443. return $aRes;
  444. }
  445. /**
  446. * Helper function to create the database structure
  447. * @return boolean true on success, false otherwise
  448. */
  449. function CreateDatabaseStructure(Config $oConfig, $sMode)
  450. {
  451. if (strlen($oConfig->GetDBSubname()) > 0)
  452. {
  453. SetupPage::log_info("Creating the structure in '".$oConfig->GetDBName()."' (table names prefixed by '".$oConfig->GetDBSubname()."').");
  454. }
  455. else
  456. {
  457. SetupPage::log_info("Creating the structure in '".$oConfig->GetDBSubname()."'.");
  458. }
  459. //MetaModel::CheckDefinitions();
  460. if ($sMode == 'install')
  461. {
  462. if (!MetaModel::DBExists(/* bMustBeComplete */ false))
  463. {
  464. MetaModel::DBCreate();
  465. SetupPage::log_ok("Database structure successfully created.");
  466. }
  467. else
  468. {
  469. if (strlen($oConfig->GetDBSubname()) > 0)
  470. {
  471. 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.");
  472. }
  473. else
  474. {
  475. 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.");
  476. }
  477. }
  478. }
  479. else
  480. {
  481. if (MetaModel::DBExists(/* bMustBeComplete */ false))
  482. {
  483. MetaModel::DBCreate();
  484. SetupPage::log_ok("Database structure successfully updated.");
  485. // Check (and update only if it seems needed) the hierarchical keys
  486. ob_start();
  487. MetaModel::CheckHKeys(false /* bDiagnosticsOnly */, true /* bVerbose*/, true /* bForceUpdate */); // Since in 1.2-beta the detection was buggy, let's force the rebuilding of HKeys
  488. $sFeedback = ob_get_clean();
  489. SetupPage::log_ok("Hierchical keys rebuilt: $sFeedback");
  490. // Check (and fix) data sync configuration
  491. ob_start();
  492. MetaModel::CheckDataSources(false /*$bDiagnostics*/, true/*$bVerbose*/);
  493. $sFeedback = ob_get_clean();
  494. SetupPage::log_ok("Data sources checked: $sFeedback");
  495. }
  496. else
  497. {
  498. if (strlen($oConfig->GetDBSubname()) > 0)
  499. {
  500. throw new Exception("Error: No previous instance of iTop found into the database '".$oConfig->GetDBName()."' (prefix: '".$oConfig->GetDBSubname()."'). Please, try selecting another database instance.");
  501. }
  502. else
  503. {
  504. throw new Exception("Error: No previous instance of iTop found into the database '".$oConfig->GetDBName()."'. Please, try selecting another database instance.");
  505. }
  506. }
  507. }
  508. return true;
  509. }
  510. function RecordInstallation(Config $oConfig, $aSelectedModules, $sModulesRelativePath)
  511. {
  512. // Record main installation
  513. $oInstallRec = new ModuleInstallation();
  514. $oInstallRec->Set('name', ITOP_APPLICATION);
  515. $oInstallRec->Set('version', ITOP_VERSION.'.'.ITOP_REVISION);
  516. $oInstallRec->Set('comment', "Done by the setup program\nBuilt on ".ITOP_BUILD_DATE);
  517. $oInstallRec->Set('parent_id', 0); // root module
  518. $iMainItopRecord = $oInstallRec->DBInsertNoReload();
  519. // Record installed modules
  520. //
  521. $aAvailableModules = AnalyzeInstallation($oConfig, $sModulesRelativePath);
  522. foreach($aSelectedModules as $sModuleId)
  523. {
  524. $aModuleData = $aAvailableModules[$sModuleId];
  525. $sName = $sModuleId;
  526. $sVersion = $aModuleData['version_code'];
  527. $aComments = array();
  528. $aComments[] = 'Done by the setup program';
  529. if ($aModuleData['mandatory'])
  530. {
  531. $aComments[] = 'Mandatory';
  532. }
  533. else
  534. {
  535. $aComments[] = 'Optional';
  536. }
  537. if ($aModuleData['visible'])
  538. {
  539. $aComments[] = 'Visible (during the setup)';
  540. }
  541. else
  542. {
  543. $aComments[] = 'Hidden (selected automatically)';
  544. }
  545. foreach ($aModuleData['dependencies'] as $sDependOn)
  546. {
  547. $aComments[] = "Depends on module: $sDependOn";
  548. }
  549. $sComment = implode("\n", $aComments);
  550. $oInstallRec = new ModuleInstallation();
  551. $oInstallRec->Set('name', $sName);
  552. $oInstallRec->Set('version', $sVersion);
  553. $oInstallRec->Set('comment', $sComment);
  554. $oInstallRec->Set('parent_id', $iMainItopRecord);
  555. $oInstallRec->DBInsertNoReload();
  556. }
  557. // Database is created, installation has been tracked into it
  558. return true;
  559. }
  560. ?>