BxDolStudioInstallerUtils.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinStudio Dolphin Studio
  7. * @{
  8. */
  9. bx_import('BxDolInstallerUtils');
  10. class BxDolStudioInstallerUtils extends BxDolInstallerUtils implements iBxDolSingleton
  11. {
  12. public function __construct()
  13. {
  14. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  15. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  16. parent::__construct();
  17. }
  18. /**
  19. * Prevent cloning the instance
  20. */
  21. public function __clone()
  22. {
  23. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  24. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  25. }
  26. /**
  27. * Get singleton instance of the class
  28. */
  29. public static function getInstance()
  30. {
  31. if (!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  32. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolStudioInstallerUtils();
  33. return $GLOBALS['bxDolClasses'][__CLASS__];
  34. }
  35. public function perform($sDirectory, $sOperation, $aParams = array())
  36. {
  37. $sConfigFile = 'install/config.php';
  38. $sInstallerFile = 'install/installer.php';
  39. $sInstallerClass = $sOperation == 'update' ? 'Updater' : 'Installer';
  40. $aConfig = self::getModuleConfig(BX_DIRECTORY_PATH_MODULES . $sDirectory . $sConfigFile);
  41. $sPathInstaller = BX_DIRECTORY_PATH_MODULES . $sDirectory . $sInstallerFile;
  42. if(!empty($aConfig) && file_exists($sPathInstaller)) {
  43. require_once($sPathInstaller);
  44. $sClassName = $aConfig['class_prefix'] . $sInstallerClass;
  45. $oInstaller = new $sClassName($aConfig);
  46. $aResult = $oInstaller->$sOperation($aParams);
  47. $aResult = array(
  48. 'code' => $aResult['result'] ? 0 : 1,
  49. 'message' => $aResult['message'],
  50. );
  51. } else
  52. $aResult = array(
  53. 'code' => 2,
  54. 'message' => _t('_adm_mod_err_process_operation_failed', $sOperation, $sDirectory),
  55. );
  56. return $aResult;
  57. }
  58. public function loadModules()
  59. {
  60. $aModules = array();
  61. $oTemplate = BxDolStudioTemplate::getInstance();
  62. $aInstalledPathes = $aInstalledInfo = array();
  63. $this->getInstalledInfo($aInstalledPathes, $aInstalledInfo);
  64. $sPath = BX_DIRECTORY_PATH_MODULES;
  65. if(($rHandleVendor = opendir($sPath)) !== false) {
  66. while(($sVendor = readdir($rHandleVendor)) !== false) {
  67. if(substr($sVendor, 0, 1) == '.' || !is_dir($sPath . $sVendor))
  68. continue;
  69. if(($rHandleModule = opendir($sPath . $sVendor)) !== false) {
  70. while(($sModule = readdir($rHandleModule)) !== false) {
  71. if(!is_dir($sPath . $sVendor . '/' . $sModule) || substr($sModule, 0, 1) == '.')
  72. continue;
  73. $sConfigPath = $sPath . $sVendor . '/' . $sModule . '/install/config.php';
  74. $aModule = $this->getConfigModule($sConfigPath, $aInstalledPathes, $aInstalledInfo);
  75. if(empty($aModule))
  76. continue;
  77. $aModules[$aModule['title']] = $aModule;
  78. }
  79. closedir($rHandleModule);
  80. }
  81. }
  82. closedir($rHandleVendor);
  83. }
  84. ksort($aModules);
  85. return $aModules;
  86. }
  87. public function loadUpdates()
  88. {
  89. $aUpdates = array();
  90. $oTemplate = BxDolStudioTemplate::getInstance();
  91. $aInstalledPathes = $aInstalledInfo = array();
  92. $this->getInstalledInfo($aInstalledPathes, $aInstalledInfo);
  93. $sPath = BX_DIRECTORY_PATH_MODULES;
  94. if($rHandleVendor = opendir($sPath)) {
  95. while(($sVendor = readdir($rHandleVendor)) !== false) {
  96. if(substr($sVendor, 0, 1) == '.' || !is_dir($sPath . $sVendor))
  97. continue;
  98. if($rHandleModule = opendir($sPath . $sVendor . '/')) {
  99. while(($sModule = readdir($rHandleModule)) !== false) {
  100. if(!is_dir($sPath . $sVendor . '/' . $sModule) || substr($sModule, 0, 1) == '.')
  101. continue;
  102. if($rHandleUpdate = @opendir($sPath . $sVendor . '/' . $sModule . '/updates/')) {
  103. while(($sUpdate = readdir($rHandleUpdate)) !== false) {
  104. if(!is_dir($sPath . $sVendor . '/' . $sModule . '/updates/' . $sUpdate) || substr($sUpdate, 0, 1) == '.')
  105. continue;
  106. $sConfigPathModule = $sPath . $sVendor . '/' . $sModule . '/install/config.php';
  107. $sConfigPathUpdate = $sPath . $sVendor . '/' . $sModule . '/updates/' . $sUpdate . '/install/config.php';
  108. $aUpdate = $this->getConfigUpdate($sConfigPathModule, $sConfigPathUpdate, $aInstalledPathes, $aInstalledInfo);
  109. if(empty($aUpdate))
  110. continue;
  111. $aUpdates[$aUpdate['title']] = $aUpdate;
  112. }
  113. closedir($rHandleUpdate);
  114. }
  115. }
  116. closedir($rHandleModule);
  117. }
  118. }
  119. closedir($rHandleVendor);
  120. }
  121. ksort($aUpdates);
  122. return $aUpdates;
  123. }
  124. protected function getConfigModule($sConfigPath, $aInstalledPathes = array(), $aInstalledInfo = array())
  125. {
  126. $aConfig = self::getModuleConfig($sConfigPath);
  127. if(empty($aConfig) || !is_array($aConfig))
  128. return array();
  129. $sModulePath = $aConfig['home_dir'];
  130. $bInstalled = !empty($aInstalledPathes) && in_array($sModulePath, $aInstalledPathes);
  131. $bEnabled = $bInstalled && !empty($aInstalledInfo) && (int)$aInstalledInfo[$sModulePath]['enabled'] == 1;
  132. $sLinkMarket = '';
  133. if(isset($aConfig['product_url'])) {
  134. $aTmplVars = array(
  135. 'vendor' => $aConfig['vendor'],
  136. 'version' => $aConfig['version'],
  137. 'uri' => $aConfig['home_uri'],
  138. 'title' => $aConfig['title']
  139. );
  140. $sLinkMarket = BxDolStudioTemplate::getInstance()->parseHtmlByContent(bx_html_attribute($aConfig['product_url']), $aTmplVars, array('{', '}'));
  141. }
  142. return array(
  143. 'type' => $aConfig['type'],
  144. 'name' => isset($aConfig['name']) ? $aConfig['name'] : $aConfig['home_uri'],
  145. 'title' => bx_process_output($aConfig['title']),
  146. 'vendor' => $aConfig['vendor'],
  147. 'version' => $aConfig['version'],
  148. 'uri' => $aConfig['home_uri'],
  149. 'dir' => $aConfig['home_dir'],
  150. 'note' => isset($aConfig['note']) ? bx_process_output($aConfig['note']) : '',
  151. 'link_market' => $sLinkMarket,
  152. 'installed' => $bInstalled,
  153. 'enabled' => $bInstalled && $bEnabled
  154. );
  155. }
  156. protected function getConfigUpdate($sConfigPathModule, $sConfigPathUpdate, $aInstalledPathes = array(), $aInstalledInfo = array())
  157. {
  158. $aModule = $this->getConfigModule($sConfigPathModule, $aInstalledPathes, $aInstalledInfo);
  159. if(empty($aModule) || !$aModule['installed'])
  160. return array();
  161. $aConfig = self::getModuleConfig($sConfigPathUpdate);
  162. if(empty($aConfig) || !is_array($aConfig))
  163. return array();
  164. return array(
  165. 'title' => bx_process_output($aConfig['title']),
  166. 'vendor' => $aConfig['vendor'],
  167. 'version_from' => $aConfig['version_from'],
  168. 'version_to' => $aConfig['version_to'],
  169. 'dir' => $aConfig['home_dir'],
  170. 'module_type' => $aModule['type'],
  171. 'module_name' => $aModule['name'],
  172. 'module_dir' => $aModule['dir'],
  173. 'module_link_market' => $aModule['link_market'],
  174. );
  175. }
  176. private function getInstalledInfo(&$aInstalledPathes, &$aInstalledInfo)
  177. {
  178. bx_import('BxDolModuleDb');
  179. $aModules = BxDolModuleDb::getInstance()->getModules();
  180. $aInstalledInfo = array();
  181. foreach($aModules as $aModule)
  182. $aInstalledInfo[$aModule['path']] = $aModule;
  183. $aInstalledPathes = array_keys($aInstalledInfo);
  184. }
  185. }
  186. /** @} */