runtimeenv.class.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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."setup/modulediscovery.class.inc.php");
  25. define ('MODULE_ACTION_OPTIONAL', 1);
  26. define ('MODULE_ACTION_MANDATORY', 2);
  27. define ('MODULE_ACTION_IMPOSSIBLE', 3);
  28. define ('ROOT_MODULE', '_Root_'); // Convention to store IN MEMORY the name/version of the root module i.e. application
  29. class RunTimeEnvironment
  30. {
  31. protected $sTargetEnv;
  32. public function __construct($sEnvironment = 'production')
  33. {
  34. $this->sTargetEnv = $sEnvironment;
  35. }
  36. /**
  37. * Helper function to initialize the ORM and load the data model
  38. * from the given file
  39. * @param $oConfig object The configuration (volatile, not necessarily already on disk)
  40. * @param $bModelOnly boolean Whether or not to allow loading a data model with no corresponding DB
  41. * @return none
  42. */
  43. public function InitDataModel($oConfig, $bModelOnly = true, $bUseCache = false)
  44. {
  45. require_once(APPROOT.'/core/log.class.inc.php');
  46. require_once(APPROOT.'/core/kpi.class.inc.php');
  47. require_once(APPROOT.'/core/coreexception.class.inc.php');
  48. require_once(APPROOT.'/core/dict.class.inc.php');
  49. require_once(APPROOT.'/core/attributedef.class.inc.php');
  50. require_once(APPROOT.'/core/filterdef.class.inc.php');
  51. require_once(APPROOT.'/core/stimulus.class.inc.php');
  52. require_once(APPROOT.'/core/MyHelpers.class.inc.php');
  53. require_once(APPROOT.'/core/expression.class.inc.php');
  54. require_once(APPROOT.'/core/cmdbsource.class.inc.php');
  55. require_once(APPROOT.'/core/sqlquery.class.inc.php');
  56. require_once(APPROOT.'/core/dbobject.class.php');
  57. require_once(APPROOT.'/core/dbobjectsearch.class.php');
  58. require_once(APPROOT.'/core/dbobjectset.class.php');
  59. require_once(APPROOT.'/application/cmdbabstract.class.inc.php');
  60. require_once(APPROOT.'/core/userrights.class.inc.php');
  61. require_once(APPROOT.'/setup/moduleinstallation.class.inc.php');
  62. $sConfigFile = $oConfig->GetLoadedFile();
  63. if (strlen($sConfigFile) > 0)
  64. {
  65. $this->log_info("MetaModel::Startup from $sConfigFile (ModelOnly = $bModelOnly)");
  66. }
  67. else
  68. {
  69. $this->log_info("MetaModel::Startup (ModelOnly = $bModelOnly)");
  70. }
  71. if (!$bUseCache)
  72. {
  73. // Reset the cache for the first use !
  74. MetaModel::ResetCache($this->sTargetEnv);
  75. }
  76. MetaModel::Startup($oConfig, $bModelOnly, $bUseCache);
  77. }
  78. /**
  79. * Analyzes the current installation and the possibilities
  80. *
  81. * @param $oConfig Config Defines the target environment (DB)
  82. * @return hash Array with the following format:
  83. * array =>
  84. * 'iTop' => array(
  85. * 'version_db' => ... (could be empty in case of a fresh install)
  86. * 'version_code => ...
  87. * )
  88. * <module_name> => array(
  89. * 'version_db' => ...
  90. * 'version_code' => ...
  91. * 'install' => array(
  92. * 'flag' => SETUP_NEVER | SETUP_OPTIONAL | SETUP_MANDATORY
  93. * 'message' => ...
  94. * )
  95. * 'uninstall' => array(
  96. * 'flag' => SETUP_NEVER | SETUP_OPTIONAL | SETUP_MANDATORY
  97. * 'message' => ...
  98. * )
  99. * 'label' => ...
  100. * 'dependencies' => array(<module1>, <module2>, ...)
  101. * 'visible' => true | false
  102. * )
  103. * )
  104. */
  105. public function AnalyzeInstallation($oConfig, $sModulesRelativePath)
  106. {
  107. $aRes = array(
  108. ROOT_MODULE => array(
  109. 'version_db' => '',
  110. 'name_db' => '',
  111. 'version_code' => ITOP_VERSION.'.'.ITOP_REVISION,
  112. 'name_code' => ITOP_APPLICATION,
  113. )
  114. );
  115. $aModules = ModuleDiscovery::GetAvailableModules(APPROOT, $sModulesRelativePath);
  116. foreach($aModules as $sModuleId => $aModuleInfo)
  117. {
  118. list($sModuleName, $sModuleVersion) = ModuleDiscovery::GetModuleName($sModuleId);
  119. $sModuleAppVersion = $aModuleInfo['itop_version'];
  120. $aModuleInfo['version_db'] = '';
  121. $aModuleInfo['version_code'] = $sModuleVersion;
  122. if (!in_array($sModuleAppVersion, array('1.0.0', '1.0.1', '1.0.2')))
  123. {
  124. // This module is NOT compatible with the current version
  125. $aModuleInfo['install'] = array(
  126. 'flag' => MODULE_ACTION_IMPOSSIBLE,
  127. 'message' => 'the module is not compatible with the current version of the application'
  128. );
  129. }
  130. elseif ($aModuleInfo['mandatory'])
  131. {
  132. $aModuleInfo['install'] = array(
  133. 'flag' => MODULE_ACTION_MANDATORY,
  134. 'message' => 'the module is part of the application'
  135. );
  136. }
  137. else
  138. {
  139. $aModuleInfo['install'] = array(
  140. 'flag' => MODULE_ACTION_OPTIONAL,
  141. 'message' => ''
  142. );
  143. }
  144. $aRes[$sModuleName] = $aModuleInfo;
  145. }
  146. try
  147. {
  148. require_once(APPROOT.'/core/cmdbsource.class.inc.php');
  149. CMDBSource::Init($oConfig->GetDBHost(), $oConfig->GetDBUser(), $oConfig->GetDBPwd(), $oConfig->GetDBName());
  150. $aSelectInstall = CMDBSource::QueryToArray("SELECT * FROM ".$oConfig->GetDBSubname()."priv_module_install");
  151. }
  152. catch (MySQLException $e)
  153. {
  154. // No database or eroneous information
  155. $aSelectInstall = array();
  156. }
  157. // Build the list of installed module (get the latest installation)
  158. //
  159. $aInstallByModule = array(); // array of <module> => array ('installed' => timestamp, 'version' => <version>)
  160. foreach ($aSelectInstall as $aInstall)
  161. {
  162. //$aInstall['comment']; // unsused
  163. $iInstalled = strtotime($aInstall['installed']);
  164. $sModuleName = $aInstall['name'];
  165. $sModuleVersion = $aInstall['version'];
  166. if ($aInstall['parent_id'] == 0)
  167. {
  168. $sModuleName = ROOT_MODULE;
  169. }
  170. if (array_key_exists($sModuleName, $aInstallByModule))
  171. {
  172. if ($iInstalled < $aInstallByModule[$sModuleName]['installed'])
  173. {
  174. continue;
  175. }
  176. }
  177. if ($aInstall['parent_id'] == 0)
  178. {
  179. $aRes[$sModuleName]['version_db'] = $sModuleVersion;
  180. $aRes[$sModuleName]['name_db'] = $aInstall['name'];
  181. }
  182. $aInstallByModule[$sModuleName]['installed'] = $iInstalled;
  183. $aInstallByModule[$sModuleName]['version'] = $sModuleVersion;
  184. }
  185. // Adjust the list of proposed modules
  186. //
  187. foreach ($aInstallByModule as $sModuleName => $aModuleDB)
  188. {
  189. if ($sModuleName == ROOT_MODULE) continue; // Skip the main module
  190. if (!array_key_exists($sModuleName, $aRes))
  191. {
  192. // A module was installed, it is not proposed in the new build... skip
  193. continue;
  194. }
  195. $aRes[$sModuleName]['version_db'] = $aModuleDB['version'];
  196. if ($aRes[$sModuleName]['install']['flag'] == MODULE_ACTION_MANDATORY)
  197. {
  198. $aRes[$sModuleName]['uninstall'] = array(
  199. 'flag' => MODULE_ACTION_IMPOSSIBLE,
  200. 'message' => 'the module is part of the application'
  201. );
  202. }
  203. else
  204. {
  205. $aRes[$sModuleName]['uninstall'] = array(
  206. 'flag' => MODULE_ACTION_OPTIONAL,
  207. 'message' => ''
  208. );
  209. }
  210. }
  211. return $aRes;
  212. }
  213. /**
  214. * Helper function to create the database structure
  215. * @return boolean true on success, false otherwise
  216. */
  217. public function CreateDatabaseStructure(Config $oConfig, $sMode)
  218. {
  219. if (strlen($oConfig->GetDBSubname()) > 0)
  220. {
  221. $this->log_info("Creating the structure in '".$oConfig->GetDBName()."' (table names prefixed by '".$oConfig->GetDBSubname()."').");
  222. }
  223. else
  224. {
  225. $this->log_info("Creating the structure in '".$oConfig->GetDBSubname()."'.");
  226. }
  227. //MetaModel::CheckDefinitions();
  228. if ($sMode == 'install')
  229. {
  230. if (!MetaModel::DBExists(/* bMustBeComplete */ false))
  231. {
  232. MetaModel::DBCreate();
  233. $this->log_ok("Database structure successfully created.");
  234. }
  235. else
  236. {
  237. if (strlen($oConfig->GetDBSubname()) > 0)
  238. {
  239. 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.");
  240. }
  241. else
  242. {
  243. 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.");
  244. }
  245. }
  246. }
  247. else
  248. {
  249. if (MetaModel::DBExists(/* bMustBeComplete */ false))
  250. {
  251. MetaModel::DBCreate();
  252. $this->log_ok("Database structure successfully updated.");
  253. // Check (and update only if it seems needed) the hierarchical keys
  254. ob_start();
  255. MetaModel::CheckHKeys(false /* bDiagnosticsOnly */, true /* bVerbose*/, true /* bForceUpdate */); // Since in 1.2-beta the detection was buggy, let's force the rebuilding of HKeys
  256. $sFeedback = ob_get_clean();
  257. $this->log_ok("Hierchical keys rebuilt: $sFeedback");
  258. // Check (and fix) data sync configuration
  259. ob_start();
  260. MetaModel::CheckDataSources(false /*$bDiagnostics*/, true/*$bVerbose*/);
  261. $sFeedback = ob_get_clean();
  262. $this->log_ok("Data sources checked: $sFeedback");
  263. }
  264. else
  265. {
  266. if (strlen($oConfig->GetDBSubname()) > 0)
  267. {
  268. throw new Exception("Error: No previous instance of iTop found into the database '".$oConfig->GetDBName()."' (prefix: '".$oConfig->GetDBSubname()."'). Please, try selecting another database instance.");
  269. }
  270. else
  271. {
  272. throw new Exception("Error: No previous instance of iTop found into the database '".$oConfig->GetDBName()."'. Please, try selecting another database instance.");
  273. }
  274. }
  275. }
  276. return true;
  277. }
  278. public function RecordInstallation(Config $oConfig, $aSelectedModules, $sModulesRelativePath)
  279. {
  280. // Record main installation
  281. $oInstallRec = new ModuleInstallation();
  282. $oInstallRec->Set('name', ITOP_APPLICATION);
  283. $oInstallRec->Set('version', ITOP_VERSION.'.'.ITOP_REVISION);
  284. $oInstallRec->Set('comment', "Done by the setup program\nBuilt on ".ITOP_BUILD_DATE);
  285. $oInstallRec->Set('parent_id', 0); // root module
  286. $iMainItopRecord = $oInstallRec->DBInsertNoReload();
  287. // Record installed modules
  288. //
  289. $aAvailableModules = $this->AnalyzeInstallation($oConfig, $sModulesRelativePath);
  290. foreach($aSelectedModules as $sModuleId)
  291. {
  292. $aModuleData = $aAvailableModules[$sModuleId];
  293. $sName = $sModuleId;
  294. $sVersion = $aModuleData['version_code'];
  295. $aComments = array();
  296. $aComments[] = 'Done by the setup program';
  297. if ($aModuleData['mandatory'])
  298. {
  299. $aComments[] = 'Mandatory';
  300. }
  301. else
  302. {
  303. $aComments[] = 'Optional';
  304. }
  305. if ($aModuleData['visible'])
  306. {
  307. $aComments[] = 'Visible (during the setup)';
  308. }
  309. else
  310. {
  311. $aComments[] = 'Hidden (selected automatically)';
  312. }
  313. foreach ($aModuleData['dependencies'] as $sDependOn)
  314. {
  315. $aComments[] = "Depends on module: $sDependOn";
  316. }
  317. $sComment = implode("\n", $aComments);
  318. $oInstallRec = new ModuleInstallation();
  319. $oInstallRec->Set('name', $sName);
  320. $oInstallRec->Set('version', $sVersion);
  321. $oInstallRec->Set('comment', $sComment);
  322. $oInstallRec->Set('parent_id', $iMainItopRecord);
  323. $oInstallRec->DBInsertNoReload();
  324. }
  325. // Database is created, installation has been tracked into it
  326. return true;
  327. }
  328. /**
  329. * Wrappers for logging into the setup log files
  330. */
  331. protected function log_error($sText)
  332. {
  333. SetupPage::log_error($sText);
  334. }
  335. protected function log_warning($sText)
  336. {
  337. SetupPage::log_warning($sText);
  338. }
  339. protected function log_info($sText)
  340. {
  341. SetupPage::log_info($sText);
  342. }
  343. protected function log_ok($sText)
  344. {
  345. SetupPage::log_ok($sText);
  346. }
  347. } // End of class
  348. ?>