BxDolStudioUpdater.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaStudio UNA Studio
  7. * @{
  8. */
  9. class BxDolStudioUpdater extends BxDolStudioInstaller
  10. {
  11. protected $_aModule;
  12. public function __construct($aConfig)
  13. {
  14. parent::__construct($aConfig);
  15. $this->_aModule = $this->oDb->getModuleByUri($aConfig['module_uri']);
  16. $this->_sModulePath = $this->_sBasePath . $aConfig['module_dir'];
  17. $this->_aActions = array_merge($this->_aActions, array(
  18. 'check_module_exists' => array(
  19. 'title' => _t('_adm_txt_modules_check_module_exists'),
  20. ),
  21. 'check_module_matches' => array(
  22. 'title' => _t('_adm_txt_modules_check_module_matches'),
  23. ),
  24. 'check_module_version' => array(
  25. 'title' => _t('_adm_txt_modules_check_module_version'),
  26. ),
  27. 'check_module_hash' => array(
  28. 'title' => _t('_adm_txt_modules_check_module_hash'),
  29. ),
  30. 'update_files' => array(
  31. 'title' => _t('_adm_txt_modules_update_files'),
  32. ),
  33. 'restore_languages' => array(
  34. 'title' => _t('_adm_txt_modules_restore_languages'),
  35. ),
  36. ));
  37. }
  38. public function update($aParams)
  39. {
  40. $bHtmlResponce = isset($aParams['html_response']) && (bool)$aParams['html_response'];
  41. $aResult = array(
  42. 'operation_title' => _t('_adm_txt_modules_operation_update', $this->_aConfig['title'], $this->_aConfig['version_from'], $this->_aConfig['version_to'])
  43. );
  44. //--- Check for module to update ---//
  45. $aModuleInfo = $this->oDb->getModulesBy(array('type' => 'path_and_uri', 'path' => $this->_aConfig['module_dir'], 'uri' => $this->_aConfig['module_uri']));
  46. if(!$aModuleInfo)
  47. return array_merge($aResult, array(
  48. 'message' => $this->_displayResult('check_module_exists', false, '_adm_err_modules_module_not_found', $bHtmlResponce),
  49. 'result' => false
  50. ));
  51. if(isset($aParams['module_name']) && strcmp($aParams['module_name'], $aModuleInfo['name']) !== 0)
  52. return array_merge($aResult, array(
  53. 'message' => $this->_displayResult('check_module_matches', false, '_adm_err_modules_module_not_match', $bHtmlResponce),
  54. 'result' => false
  55. ));
  56. //--- Check version compatibility ---//
  57. if(!$this->_isCompatibleWith())
  58. return array(
  59. 'message' => $this->_displayResult('check_script_version', false, _t('_adm_err_modules_wrong_version_script_update', $aModuleInfo['title']), $bHtmlResponce),
  60. 'result' => false
  61. );
  62. //--- Check version ---//
  63. if(version_compare($aModuleInfo['version'], $this->_aConfig['version_from']) != 0)
  64. return array_merge($aResult, array(
  65. 'message' => $this->_displayResult('check_module_version', false, '_adm_err_modules_wrong_version', $bHtmlResponce),
  66. 'result' => false
  67. ));
  68. //--- Check hash ---//
  69. $aFiles = array();
  70. $this->hashFiles(BX_DIRECTORY_PATH_ROOT . 'modules/' . $this->_aConfig['module_dir'], $aFiles);
  71. list($aFilesChanged, $fChangedPercent) = $this->hashCheck($aFiles, $aModuleInfo['id']);
  72. $bAutoupdateForceModifiedFiles = getParam('sys_autoupdate_force_modified_files') == 'on';
  73. if(!empty($aFilesChanged) && !$bAutoupdateForceModifiedFiles)
  74. return array_merge($aResult, array(
  75. 'message' => $this->_displayResult('check_module_hash', false, '_adm_err_modules_module_was_modified', $bHtmlResponce),
  76. 'result' => false
  77. ));
  78. else if($fChangedPercent > BX_FORCE_AUTOUPDATE_MAX_CHANGED_FILES_PERCENT && $bAutoupdateForceModifiedFiles)
  79. return array_merge($aResult, array(
  80. 'message' => $this->_displayResult('check_module_hash', false, _t('_sys_upgrade_files_checksum_failed_too_many_module', round($fChangedPercent * 100), $aModuleInfo['title']), $bHtmlResponce),
  81. 'result' => false
  82. ));
  83. //--- Perform action and check results ---//
  84. $aResult = array_merge($aResult, $this->_perform('install', $aParams));
  85. if($aResult['result']) {
  86. $this->oDb->updateModule(array('version' => $this->_aConfig['version_to']), array('id' => $aModuleInfo['id']));
  87. //--- Remove update package ---//
  88. $this->delete($aParams);
  89. }
  90. return $aResult;
  91. }
  92. //--- Action Methods ---//
  93. protected function actionUpdateFiles($bInstall = true)
  94. {
  95. $sPath = $this->_sHomePath . 'source/';
  96. if(!file_exists($sPath))
  97. return BX_DOL_STUDIO_INSTALLER_FAILED;
  98. $oFile = $this->_getFileManager();
  99. if(empty($oFile))
  100. return BX_DOL_STUDIO_INSTALLER_FAILED;
  101. if(!$oFile->copy($sPath . '*', 'modules/' . $this->_aConfig['module_dir']))
  102. return BX_DOL_STUDIO_INSTALLER_FAILED;
  103. if(!empty($this->_aConfig['delete_files']) && is_array($this->_aConfig['delete_files']))
  104. foreach($this->_aConfig['delete_files'] as $sFile)
  105. if(!$oFile->delete('modules/' . $this->_aConfig['module_dir'] . $sFile))
  106. return BX_DOL_STUDIO_INSTALLER_FAILED;
  107. //--- Update files' tracks
  108. $aModuleInfo = $this->oDb->getModulesBy(array('type' => 'path_and_uri', 'path' => $this->_aConfig['module_dir'], 'uri' => $this->_aConfig['module_uri']));
  109. $this->oDb->deleteModuleTrackFiles($aModuleInfo['id']);
  110. $aFiles = array();
  111. $this->hashFiles(BX_DIRECTORY_PATH_ROOT . 'modules/' . $this->_aConfig['module_dir'], $aFiles);
  112. foreach($aFiles as $aFile)
  113. $this->oDb->insertModuleTrack($aModuleInfo['id'], $aFile);
  114. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  115. }
  116. protected function actionExecuteSql($sOperation)
  117. {
  118. $aModule = $this->oDb->getModuleByUri($this->_aConfig['module_uri']);
  119. if(empty($aModule))
  120. return BX_DOL_STUDIO_INSTALLER_FAILED;
  121. $sPathInstall = $this->_sHomePath . 'install/sql/install.sql';
  122. $sPathEnable = $this->_sHomePath . 'install/sql/enable.sql';
  123. $mixedResult = true;
  124. if(file_exists($sPathInstall))
  125. $mixedResult = $this->oDb->executeSQL($sPathInstall, $this->getMarkersForDb());
  126. if($mixedResult === true && (int)$aModule['enabled'] == 1 && file_exists($sPathEnable))
  127. $mixedResult = $this->oDb->executeSQL($sPathEnable, $this->getMarkersForDb());
  128. return $mixedResult === true ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $mixedResult);
  129. }
  130. protected function actionUpdateLanguages($bInstall = true)
  131. {
  132. $aConfig = self::getModuleConfig($this->_sHomePath . 'install/config.php');
  133. if(empty($aConfig) || !is_array($aConfig))
  134. return BX_DOL_STUDIO_INSTALLER_FAILED;
  135. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  136. $aLanguages = $oLanguages->getLanguagesExt();
  137. $aCategories = array();
  138. if(!empty($aConfig['language_category']) && is_array($aConfig['language_category'])) {
  139. $aCategories = $aConfig['language_category'];
  140. foreach($aCategories as $iIndex => $aCategory)
  141. $aCategories[$iIndex]['id'] = !empty($aCategory['name']) ? $oLanguages->getLanguageCategory($aCategory['name']) : 0;
  142. }
  143. else
  144. $aCategories[] = array(
  145. 'id' => !empty($aConfig['language_category']) ? $oLanguages->getLanguageCategory($aConfig['language_category']) : 0,
  146. 'name' => $aConfig['language_category'],
  147. 'path' => ''
  148. );
  149. foreach($aLanguages as $sName => $aLanguage)
  150. foreach($aCategories as $iIndex => $aCategory)
  151. $this->_updateLanguage($bInstall, $aLanguage, $aCategory['id'], $aCategory['path']);
  152. return $oLanguages->compileLanguage(0, true) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  153. }
  154. /*
  155. * Restore module's language files.
  156. *
  157. * Note. Mainly the action is needed for Updates in 'language' type modules.
  158. * It should be used after 'update_files' action if some changes were done in module's language files.
  159. */
  160. protected function actionRestoreLanguages($bInstall = true)
  161. {
  162. $aConfig = self::getModuleConfig($this->_sHomePath . 'install/config.php');
  163. if(empty($aConfig) || !is_array($aConfig) || empty($aConfig['module_uri']))
  164. return BX_DOL_STUDIO_INSTALLER_FAILED;
  165. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  166. $aLanguages = $oLanguages->getLanguages();
  167. $bResult = true;
  168. foreach($aLanguages as $sName => $sTitle)
  169. $bResult &= $oLanguages->restoreLanguage($sName, $aConfig['module_uri']);
  170. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  171. }
  172. /**
  173. * NOTE. The action is ONLY needed for dependent module to let
  174. * Notifications based module(s) know that he(they) should
  175. * update (request and save) handlers from this dependent module.
  176. */
  177. protected function actionUpdateRelations($sOperation)
  178. {
  179. if(!in_array($sOperation, array('install')))
  180. return BX_DOL_STUDIO_INSTALLER_FAILED;
  181. if(empty($this->_aConfig['relations']) || !is_array($this->_aConfig['relations']))
  182. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  183. foreach($this->_aConfig['relations'] as $sModule) {
  184. if(!$this->oDb->isModuleByName($sModule))
  185. continue;
  186. $aRelation = $this->oDb->getRelationsBy(array('type' => 'module', 'value' => $sModule));
  187. if(empty($aRelation) || empty($aRelation['on_enable']) || empty($aRelation['on_disable']) || !BxDolRequest::serviceExists($aRelation['module'], $aRelation['on_enable']) || !BxDolRequest::serviceExists($aRelation['module'], $aRelation['on_disable']))
  188. continue;
  189. BxDolService::call($aRelation['module'], $aRelation['on_disable'], array($this->_aConfig['module_uri']));
  190. BxDolService::call($aRelation['module'], $aRelation['on_enable'], array($this->_aConfig['module_uri']));
  191. }
  192. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  193. }
  194. /**
  195. * NOTE. The action is ONLY needed for Notifications based modules
  196. * to update (request and save) handlers from all dependent modules.
  197. */
  198. protected function actionUpdateRelationsForAll($sOperation)
  199. {
  200. if(!in_array($sOperation, array('install')))
  201. return BX_DOL_STUDIO_INSTALLER_FAILED;
  202. $aRelation = $this->oDb->getRelationsBy(array('type' => 'module', 'value' => $this->_aModule['name']));
  203. if(empty($aRelation) || empty($aRelation['on_enable']) || empty($aRelation['on_disable']) || !BxDolRequest::serviceExists($this->_aModule['name'], $aRelation['on_enable']) || !BxDolRequest::serviceExists($this->_aModule['name'], $aRelation['on_disable']))
  204. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  205. $aModules = $this->oDb->getModulesBy(array('type' => 'all', 'active' => 1));
  206. foreach($aModules as $aModule) {
  207. $aConfig = self::getModuleConfig($aModule);
  208. if(empty($aConfig['relations']) || !is_array($aConfig['relations']) || !in_array($this->_aModule['name'], $aConfig['relations']))
  209. continue;
  210. BxDolService::call($this->_aModule['name'], $aRelation['on_disable'], array($aModule['uri']));
  211. BxDolService::call($this->_aModule['name'], $aRelation['on_enable'], array($aModule['uri']));
  212. }
  213. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  214. }
  215. protected function _updateLanguage($bInstall, $aLanguage, $iCategory = 0, $sPath = '')
  216. {
  217. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  218. $sPathAbsolute = $this->_sHomePath . 'install/langs/' . $sPath . $aLanguage['name'] . '.xml';
  219. $aLanguageInfo = $oLanguages->readLanguage($sPathAbsolute, 'update');
  220. if(empty($aLanguageInfo))
  221. return false;
  222. if(!empty($aLanguageInfo['category']))
  223. $iCategory = $oLanguages->getLanguageCategory($aLanguageInfo['category']);
  224. //--- Process delete. Note. Deletion is performed for all languages. ---//
  225. if(isset($aLanguageInfo['strings_del']))
  226. foreach($aLanguageInfo['strings_del'] as $sKey => $sValue)
  227. $oLanguages->deleteLanguageString($sKey, 0, false);
  228. //--- Process add. Note. Key's category will be updated if it doesn't match. ---//
  229. if(isset($aLanguageInfo['strings_add']))
  230. foreach($aLanguageInfo['strings_add'] as $sKey => $sValue)
  231. $oLanguages->addLanguageString($sKey, $sValue, $aLanguage['id'], $iCategory, false);
  232. //--- Process update. Note. Key's category will be updated if it doesn't match. ---//
  233. if(isset($aLanguageInfo['strings_upd']))
  234. foreach($aLanguageInfo['strings_upd'] as $sKey => $sValue)
  235. $oLanguages->updateLanguageString($sKey, $sValue, $aLanguage['id'], $iCategory, false);
  236. return true;
  237. }
  238. }
  239. /** @} */