extensionsmap.class.inc.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. require_once(APPROOT.'/setup/parameters.class.inc.php');
  3. require_once(APPROOT.'/core/cmdbsource.class.inc.php');
  4. /**
  5. * Basic helper class to describe an extension, with some characteristics and a list of modules
  6. */
  7. class iTopExtension
  8. {
  9. const SOURCE_WIZARD = 'datamodels';
  10. const SOURCE_MANUAL = 'extensions';
  11. const SOURCE_REMOTE = 'data';
  12. /**
  13. * @var string
  14. */
  15. public $sCode;
  16. /**
  17. * @var string
  18. */
  19. public $sVersion;
  20. /**
  21. * @var string
  22. */
  23. public $sInstalledVersion;
  24. /**
  25. * @var string
  26. */
  27. public $sLabel;
  28. /**
  29. * @var string
  30. */
  31. public $sDescription;
  32. /**
  33. * @var string
  34. */
  35. public $sSource;
  36. /**
  37. * @var bool
  38. */
  39. public $bMandatory;
  40. /**
  41. * @var string
  42. */
  43. public $sMoreInfoUrl;
  44. /**
  45. * @var bool
  46. */
  47. public $bMarkedAsChosen;
  48. /**
  49. * @var string[]
  50. */
  51. public $aModules;
  52. public function __construct()
  53. {
  54. $this->sCode = '';
  55. $this->sLabel = '';
  56. $this->sDescription = '';
  57. $this->sSource = self::SOURCE_WIZARD;
  58. $this->bMandatory = false;
  59. $this->sMoreInfoUrl = '';
  60. $this->bMarkedAsChosen = false;
  61. $this->sVersion = ITOP_VERSION;
  62. $this->sInstalledVersion = '';
  63. }
  64. }
  65. /**
  66. * Helper class to discover all available extensions on a given iTop system
  67. */
  68. class iTopExtensionsMap
  69. {
  70. /**
  71. * The list of all discovered extensions
  72. * @var iTopExtension[]
  73. */
  74. protected $aExtensions;
  75. public function __construct($sFromEnvironment = 'production')
  76. {
  77. $this->aExtensions = array();
  78. $this->ScanDisk($sFromEnvironment);
  79. }
  80. /**
  81. * Populate the list of available (pseudo)extensions by scanning the disk
  82. * where the iTop files are located
  83. * @param string $sEnvironment
  84. * @return void
  85. */
  86. protected function ScanDisk($sEnvironment)
  87. {
  88. if (!$this->ReadInstallationWizard(APPROOT.'/datamodels/2.x') && !$this->ReadInstallationWizard(APPROOT.'/datamodels/2.x'))
  89. {
  90. if(!$this->ReadDir(APPROOT.'/datamodels/2.x', iTopExtension::SOURCE_WIZARD)) $this->ReadDir(APPROOT.'/datamodels/1.x', iTopExtension::SOURCE_WIZARD);
  91. }
  92. $this->ReadDir(APPROOT.'/extensions', iTopExtension::SOURCE_MANUAL);
  93. $this->ReadDir(APPROOT.'/data/'.$sEnvironment.'-modules', iTopExtension::SOURCE_REMOTE);
  94. }
  95. /**
  96. * Read the information contained in the "installation.xml" file in the given directory
  97. * and create pseudo extensions from the list of choices described in this file
  98. * @param string $sDir
  99. * @return boolean Return true if the installation.xml file exists and is readable
  100. */
  101. protected function ReadInstallationWizard($sDir)
  102. {
  103. if (!is_readable($sDir.'/installation.xml')) return false;
  104. $oXml = new XMLParameters($sDir.'/installation.xml');
  105. foreach($oXml->Get('steps') as $aStepInfo)
  106. {
  107. if (array_key_exists('options', $aStepInfo))
  108. {
  109. $this->ProcessWizardChoices($aStepInfo['options']);
  110. }
  111. if (array_key_exists('alternatives', $aStepInfo))
  112. {
  113. $this->ProcessWizardChoices($aStepInfo['alternatives']);
  114. }
  115. }
  116. return true;
  117. }
  118. /**
  119. * Helper to process a "choice" array read from the installation.xml file
  120. * @param array $aChoices
  121. * @return void
  122. */
  123. protected function ProcessWizardChoices($aChoices)
  124. {
  125. foreach($aChoices as $aChoiceInfo)
  126. {
  127. if (array_key_exists('extension_code', $aChoiceInfo))
  128. {
  129. $oExtension = new iTopExtension();
  130. $oExtension->sCode = $aChoiceInfo['extension_code'];
  131. $oExtension->sLabel = $aChoiceInfo['title'];
  132. if (array_key_exists('modules', $aChoiceInfo))
  133. {
  134. // Some wizard choices are not associated with any module
  135. $oExtension->aModules = $aChoiceInfo['modules'];
  136. }
  137. if (array_key_exists('sub_options', $aChoiceInfo))
  138. {
  139. if (array_key_exists('options', $aChoiceInfo['sub_options']))
  140. {
  141. $this->ProcessWizardChoices($aChoiceInfo['sub_options']['options']);
  142. }
  143. if (array_key_exists('alternatives', $aChoiceInfo['sub_options']))
  144. {
  145. $this->ProcessWizardChoices($aChoiceInfo['sub_options']['alternatives']);
  146. }
  147. }
  148. $this->AddExtension($oExtension);
  149. }
  150. }
  151. }
  152. /**
  153. * Add an extension to the list of existing extensions, taking care of removing duplicates
  154. * (only the latest/greatest version is kept)
  155. * @param iTopExtension $oNewExtension
  156. * @return void
  157. */
  158. protected function AddExtension(iTopExtension $oNewExtension)
  159. {
  160. foreach($this->aExtensions as $key => $oExtension)
  161. {
  162. if ($oExtension->sCode == $oNewExtension->sCode)
  163. {
  164. if (version_compare($oNewExtension->sVersion, $oExtension->sVersion, '>'))
  165. {
  166. // This "new" extension is "newer" than the previous one, let's replace the previous one
  167. unset($this->aExtensions[$key]);
  168. $this->aExtensions[$oNewExtension->sCode.'/'.$oNewExtension->sVersion] = $oNewExtension;
  169. return;
  170. }
  171. else
  172. {
  173. // This "new" extension is not "newer" than the previous one, let's ignore it
  174. return;
  175. }
  176. }
  177. }
  178. // Finally it's not a duplicate, let's add it to the list
  179. $this->aExtensions[$oNewExtension->sCode.'/'.$oNewExtension->sVersion] = $oNewExtension;
  180. }
  181. /**
  182. * Read (recursively) a directory to find if it contains extensions (or modules)
  183. * @param string $sSearchDir The directory to scan
  184. * @param string $sSource The 'source' value for the extensions found in this directory
  185. * @param string|null $sParentExtensionId Not null if the directory is under a declared extension
  186. * @return boolean
  187. */
  188. protected function ReadDir($sSearchDir, $sSource, $sParentExtensionId = null)
  189. {
  190. if (!is_readable($sSearchDir)) return false;
  191. $hDir = opendir($sSearchDir);
  192. if ($hDir !== false)
  193. {
  194. $sExtensionId = null;
  195. $aSubDirectories = array();
  196. // First check if there is an extension.xml file in this directory
  197. if (is_readable($sSearchDir.'/extension.xml'))
  198. {
  199. $oXml = new XMLParameters($sSearchDir.'/extension.xml');
  200. $oExtension = new iTopExtension();
  201. $oExtension->sCode = $oXml->Get('extension_code');
  202. $oExtension->sLabel = $oXml->Get('label');
  203. $oExtension->sDescription = $oXml->Get('description');
  204. $oExtension->sVersion = $oXml->Get('version');
  205. $oExtension->bMandatory = ($oXml->Get('mandatory') == 'true');
  206. $oExtension->sMoreInfoUrl = $oXml->Get('more_info_url');
  207. $oExtension->sVersion = $oXml->Get('version');
  208. $oExtension->sSource = $sSource;
  209. $sParentExtensionId = $sExtensionId = $oExtension->sCode.'/'.$oExtension->sVersion;
  210. $this->AddExtension($oExtension);
  211. }
  212. // Then scan the other files and subdirectories
  213. while (($sFile = readdir($hDir)) !== false)
  214. {
  215. if (($sFile !== '.') && ($sFile !== '..'))
  216. {
  217. $aMatches = array();
  218. if (is_dir($sSearchDir.'/'.$sFile))
  219. {
  220. // Recurse after parsing all the regular files
  221. $aSubDirectories[] = $sSearchDir.'/'.$sFile;
  222. }
  223. else if (preg_match('/^module\.(.*).php$/i', $sFile, $aMatches))
  224. {
  225. // Found a module
  226. $aModuleInfo = $this->GetModuleInfo($sSearchDir.'/'.$sFile);
  227. // If we are not already inside a formal extension, then the module itself is considered
  228. // as an extension, otherwise, the module is just added to the list of modules belonging
  229. // to this extension
  230. $sModuleId = $aModuleInfo[1];
  231. list($sModuleName, $sModuleVersion) = ModuleDiscovery::GetModuleName($sModuleId);
  232. if ($sModuleVersion == '')
  233. {
  234. // Provide a default module version since version is mandatory when recording ExtensionInstallation
  235. $sModuleVersion = '0.0.1';
  236. }
  237. if ($sParentExtensionId !== null)
  238. {
  239. // Already inside an extension, let's add this module the list of modules belonging to this extension
  240. $this->aExtensions[$sParentExtensionId]->aModules[] = $sModuleName;
  241. }
  242. else
  243. {
  244. // Not already inside an folder containing an 'extension.xml' file
  245. // Ignore non-visible modules and auto-select ones, since these are never prompted
  246. // as a choice to the end-user
  247. if (!$aModuleInfo[2]['visible'] || isset($aModuleInfo[2]['auto_select'])) continue;
  248. // Let's create a "fake" extension from this module (containing just this module) for backwards compatibility
  249. $sExtensionId = $sModuleId;
  250. $oExtension = new iTopExtension();
  251. $oExtension->sCode = $sModuleName;
  252. $oExtension->sLabel = $aModuleInfo[2]['label'];
  253. $oExtension->sDescription = '';
  254. $oExtension->sVersion = $sModuleVersion;
  255. $oExtension->sSource = $sSource;
  256. $oExtension->bMandatory = $aModuleInfo[2]['mandatory'];
  257. $oExtension->sMoreInfoUrl = $aModuleInfo[2]['doc.more_information'];
  258. $oExtension->aModules = array($sModuleName);
  259. $this->AddExtension($oExtension);
  260. }
  261. }
  262. }
  263. }
  264. closedir($hDir);
  265. foreach($aSubDirectories as $sDir)
  266. {
  267. // Recurse inside the subdirectories
  268. $this->ReadDir($sDir, $sSource, $sExtensionId);
  269. }
  270. return true;
  271. }
  272. return false;
  273. }
  274. /**
  275. * Read the information from a module file (module.xxx.php)
  276. * Closely inspired (almost copied/pasted !!) from ModuleDiscovery::ListModuleFiles
  277. * @param string $sModuleFile
  278. * @return array
  279. */
  280. protected function GetModuleInfo($sModuleFile)
  281. {
  282. static $iDummyClassIndex = 0;
  283. $aModuleInfo = array(); // will be filled by the "eval" line below...
  284. try
  285. {
  286. $aMatches = array();
  287. $sModuleFileContents = file_get_contents($sModuleFile);
  288. $sModuleFileContents = str_replace(array('<?php', '?>'), '', $sModuleFileContents);
  289. $sModuleFileContents = str_replace('__FILE__', "'".addslashes($sModuleFile)."'", $sModuleFileContents);
  290. preg_match_all('/class ([A-Za-z0-9_]+) extends ([A-Za-z0-9_]+)/', $sModuleFileContents, $aMatches);
  291. //print_r($aMatches);
  292. $idx = 0;
  293. foreach($aMatches[1] as $sClassName)
  294. {
  295. if (class_exists($sClassName))
  296. {
  297. // rename any class declaration inside the code to prevent a "duplicate class" declaration
  298. // and change its parent class as well so that nobody will find it and try to execute it
  299. // Note: don't use the same naming scheme as ModuleDiscovery otherwise you 'll have the duplicate class error again !!
  300. $sModuleFileContents = str_replace($sClassName.' extends '.$aMatches[2][$idx], $sClassName.'_Ext_'.($iDummyClassIndex++).' extends DummyHandler', $sModuleFileContents);
  301. }
  302. $idx++;
  303. }
  304. // Replace the main function call by an assignment to a variable, as an array...
  305. $sModuleFileContents = str_replace(array('SetupWebPage::AddModule', 'ModuleDiscovery::AddModule'), '$aModuleInfo = array', $sModuleFileContents);
  306. eval($sModuleFileContents); // Assigns $aModuleInfo
  307. if (count($aModuleInfo) === 0)
  308. {
  309. SetupPage::log_warning("Eval of $sModuleFile did not return the expected information...");
  310. }
  311. }
  312. catch(Exception $e)
  313. {
  314. // Continue...
  315. SetupPage::log_warning("Eval of $sModuleFile caused an exception: ".$e->getMessage());
  316. }
  317. return $aModuleInfo;
  318. }
  319. /**
  320. * Get all available extensions
  321. * @return iTopExtension[]
  322. */
  323. public function GetAllExtensions()
  324. {
  325. return $this->aExtensions;
  326. }
  327. /**
  328. * Mark the given extension as chosen
  329. * @param string $sExtensionCode The code of the extension (code without verison number)
  330. * @param bool $bMark The value to set for the bmarkAschosen flag
  331. * @return void
  332. */
  333. public function MarkAsChosen($sExtensionCode, $bMark = true)
  334. {
  335. foreach($this->aExtensions as $oExtension)
  336. {
  337. if ($oExtension->sCode == $sExtensionCode)
  338. {
  339. $oExtension->bMarkedAsChosen = $bMark;
  340. break;
  341. }
  342. }
  343. }
  344. /**
  345. * Tells if a given extension(code) is marked as chosen
  346. * @param string $sExtensionCode
  347. * @return boolean
  348. */
  349. public function IsMarkedAsChosen($sExtensionCode)
  350. {
  351. foreach($this->aExtensions as $oExtension)
  352. {
  353. if ($oExtension->sCode == $sExtensionCode)
  354. {
  355. return $oExtension->bMarkedAsChosen;
  356. }
  357. }
  358. return false;
  359. }
  360. /**
  361. * Set the 'installed_version' of the given extension(code)
  362. * @param string $sExtensionCode
  363. * @param string $sInstalledVersion
  364. * @return void
  365. */
  366. protected function SetInstalledVersion($sExtensionCode, $sInstalledVersion)
  367. {
  368. foreach($this->aExtensions as $oExtension)
  369. {
  370. if ($oExtension->sCode == $sExtensionCode)
  371. {
  372. $oExtension->sInstalledVersion = $sInstalledVersion;
  373. break;
  374. }
  375. }
  376. }
  377. /**
  378. * Get the list of the "chosen" extensions
  379. * @return iTopExtension[]
  380. */
  381. public function GetChoices()
  382. {
  383. $aResult = array();
  384. foreach($this->aExtensions as $oExtension)
  385. {
  386. if ($oExtension->bMarkedAsChosen)
  387. {
  388. $aResult[] = $oExtension;
  389. }
  390. }
  391. return $aResult;
  392. }
  393. /**
  394. * Load the choices (i.e. MarkedAsChosen) from the database defined in the supplied Config
  395. * @param Config $oConfig
  396. * @return bool
  397. */
  398. public function LoadChoicesFromDatabase(Config $oConfig)
  399. {
  400. $aInstalledExtensions = array();
  401. try
  402. {
  403. if (CMDBSource::DBName() === null)
  404. {
  405. CMDBSource::Init($oConfig->GetDBHost(), $oConfig->GetDBUser(), $oConfig->GetDBPwd(), $oConfig->GetDBName());
  406. CMDBSource::SetCharacterSet($oConfig->GetDBCharacterSet(), $oConfig->GetDBCollation());
  407. }
  408. $sLatestInstallationDate = CMDBSource::QueryToScalar("SELECT max(installed) FROM ".$oConfig->GetDBSubname()."priv_extension_install");
  409. $aInstalledExtensions = CMDBSource::QueryToArray("SELECT * FROM ".$oConfig->GetDBSubname()."priv_extension_install WHERE installed = '".$sLatestInstallationDate."'");
  410. }
  411. catch (MySQLException $e)
  412. {
  413. // No database or erroneous information
  414. $aInstalledExtensions = array();
  415. return false;
  416. }
  417. foreach($aInstalledExtensions as $aDBInfo)
  418. {
  419. $this->MarkAsChosen($aDBInfo['code']);
  420. $this->SetInstalledVersion($aDBInfo['code'], $aDBInfo['version']);
  421. }
  422. return true;
  423. }
  424. }