BxDolStudioInstallerUtils.php 8.1 KB

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