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. $sPathConfig = BX_DIRECTORY_PATH_MODULES . $sDirectory . $sConfigFile;
  41. $sPathInstaller = BX_DIRECTORY_PATH_MODULES . $sDirectory . $sInstallerFile;
  42. if(file_exists($sPathConfig) && file_exists($sPathInstaller)) {
  43. include($sPathConfig);
  44. require_once($sPathInstaller);
  45. $sClassName = $aConfig['class_prefix'] . $sInstallerClass;
  46. $oInstaller = new $sClassName($aConfig);
  47. $aResult = $oInstaller->$sOperation($aParams);
  48. $aResult = array(
  49. 'code' => $aResult['result'] ? 0 : 1,
  50. 'message' => $aResult['message'],
  51. );
  52. } else
  53. $aResult = array(
  54. 'code' => 2,
  55. 'message' => _t('_adm_mod_err_process_operation_failed', $sOperation, $sDirectory),
  56. );
  57. return $aResult;
  58. }
  59. public function loadModules()
  60. {
  61. $aModules = array();
  62. $oTemplate = BxDolStudioTemplate::getInstance();
  63. $aInstalledPathes = $aInstalledInfo = array();
  64. $this->getInstalledInfo($aInstalledPathes, $aInstalledInfo);
  65. $sPath = BX_DIRECTORY_PATH_MODULES;
  66. if(($rHandleVendor = opendir($sPath)) !== false) {
  67. while(($sVendor = readdir($rHandleVendor)) !== false) {
  68. if(substr($sVendor, 0, 1) == '.' || !is_dir($sPath . $sVendor))
  69. continue;
  70. if(($rHandleModule = opendir($sPath . $sVendor)) !== false) {
  71. while(($sModule = readdir($rHandleModule)) !== false) {
  72. if(!is_dir($sPath . $sVendor . '/' . $sModule) || substr($sModule, 0, 1) == '.')
  73. continue;
  74. $sConfigPath = $sPath . $sVendor . '/' . $sModule . '/install/config.php';
  75. $aModule = $this->getConfigModule($sConfigPath, $aInstalledPathes, $aInstalledInfo);
  76. if(empty($aModule))
  77. continue;
  78. $aModules[$aModule['title']] = $aModule;
  79. }
  80. closedir($rHandleModule);
  81. }
  82. }
  83. closedir($rHandleVendor);
  84. }
  85. ksort($aModules);
  86. return $aModules;
  87. }
  88. public function loadUpdates()
  89. {
  90. $aUpdates = array();
  91. $oTemplate = BxDolStudioTemplate::getInstance();
  92. $aInstalledPathes = $aInstalledInfo = array();
  93. $this->getInstalledInfo($aInstalledPathes, $aInstalledInfo);
  94. $sPath = BX_DIRECTORY_PATH_MODULES;
  95. if($rHandleVendor = opendir($sPath)) {
  96. while(($sVendor = readdir($rHandleVendor)) !== false) {
  97. if(substr($sVendor, 0, 1) == '.' || !is_dir($sPath . $sVendor))
  98. continue;
  99. if($rHandleModule = opendir($sPath . $sVendor . '/')) {
  100. while(($sModule = readdir($rHandleModule)) !== false) {
  101. if(!is_dir($sPath . $sVendor . '/' . $sModule) || substr($sModule, 0, 1) == '.')
  102. continue;
  103. if($rHandleUpdate = @opendir($sPath . $sVendor . '/' . $sModule . '/updates/')) {
  104. while(($sUpdate = readdir($rHandleUpdate)) !== false) {
  105. if(!is_dir($sPath . $sVendor . '/' . $sModule . '/updates/' . $sUpdate) || substr($sUpdate, 0, 1) == '.')
  106. continue;
  107. $sConfigPathModule = $sPath . $sVendor . '/' . $sModule . '/install/config.php';
  108. $sConfigPathUpdate = $sPath . $sVendor . '/' . $sModule . '/updates/' . $sUpdate . '/install/config.php';
  109. $aUpdate = $this->getConfigUpdate($sConfigPathModule, $sConfigPathUpdate, $aInstalledPathes, $aInstalledInfo);
  110. if(empty($aUpdate))
  111. continue;
  112. $aUpdates[$aUpdate['title']] = $aUpdate;
  113. }
  114. closedir($rHandleUpdate);
  115. }
  116. }
  117. closedir($rHandleModule);
  118. }
  119. }
  120. closedir($rHandleVendor);
  121. }
  122. ksort($aUpdates);
  123. return $aUpdates;
  124. }
  125. protected function getConfigModule($sConfigPath, $aInstalledPathes = array(), $aInstalledInfo = array())
  126. {
  127. $aConfig = self::getModuleConfig($sConfigPath);
  128. if(empty($aConfig) || !is_array($aConfig))
  129. return array();
  130. $sModulePath = $aConfig['home_dir'];
  131. $bInstalled = !empty($aInstalledPathes) && in_array($sModulePath, $aInstalledPathes);
  132. $bEnabled = $bInstalled && !empty($aInstalledInfo) && (int)$aInstalledInfo[$sModulePath]['enabled'] == 1;
  133. $sLinkMarket = '';
  134. if(isset($aConfig['product_url'])) {
  135. $aTmplVars = array(
  136. 'vendor' => $aConfig['vendor'],
  137. 'version' => $aConfig['version'],
  138. 'uri' => $aConfig['home_uri'],
  139. 'title' => $aConfig['title']
  140. );
  141. $sLinkMarket = BxDolStudioTemplate::getInstance()->parseHtmlByContent(bx_html_attribute($aConfig['product_url']), $aTmplVars, array('{', '}'));
  142. }
  143. return array(
  144. 'type' => $aConfig['type'],
  145. 'name' => isset($aConfig['name']) ? $aConfig['name'] : $aConfig['home_uri'],
  146. 'title' => bx_process_output($aConfig['title']),
  147. 'vendor' => $aConfig['vendor'],
  148. 'version' => $aConfig['version'],
  149. 'uri' => $aConfig['home_uri'],
  150. 'dir' => $aConfig['home_dir'],
  151. 'note' => isset($aConfig['note']) ? bx_process_output($aConfig['note']) : '',
  152. 'link_market' => $sLinkMarket,
  153. 'installed' => $bInstalled,
  154. 'enabled' => $bInstalled && $bEnabled
  155. );
  156. }
  157. protected function getConfigUpdate($sConfigPathModule, $sConfigPathUpdate, $aInstalledPathes = array(), $aInstalledInfo = array())
  158. {
  159. $aModule = $this->getConfigModule($sConfigPathModule, $aInstalledPathes, $aInstalledInfo);
  160. if(empty($aModule) || !$aModule['installed'])
  161. return array();
  162. $aConfig = self::getModuleConfig($sConfigPathUpdate);
  163. if(empty($aConfig) || !is_array($aConfig))
  164. return array();
  165. return array(
  166. 'title' => bx_process_output($aConfig['title']),
  167. 'vendor' => $aConfig['vendor'],
  168. 'version_from' => $aConfig['version_from'],
  169. 'version_to' => $aConfig['version_to'],
  170. 'dir' => $aConfig['home_dir'],
  171. 'module_type' => $aModule['type'],
  172. 'module_name' => $aModule['name'],
  173. 'module_dir' => $aModule['dir'],
  174. 'module_link_market' => $aModule['link_market'],
  175. );
  176. }
  177. private function getInstalledInfo(&$aInstalledPathes, &$aInstalledInfo)
  178. {
  179. bx_import('BxDolModuleDb');
  180. $aModules = BxDolModuleDb::getInstance()->getModules();
  181. $aInstalledInfo = array();
  182. foreach($aModules as $aModule)
  183. $aInstalledInfo[$aModule['path']] = $aModule;
  184. $aInstalledPathes = array_keys($aInstalledInfo);
  185. }
  186. }
  187. /** @} */