BxDolStudioUpdater.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. $bAutoUpdate = isset($aParams['autoupdate']) && (bool)$aParams['autoupdate'];
  41. $bHtmlResponce = isset($aParams['html_response']) && (bool)$aParams['html_response'];
  42. $aResult = array(
  43. 'operation_title' => _t('_adm_txt_modules_operation_update', $this->_aConfig['title'], $this->_aConfig['version_from'], $this->_aConfig['version_to'])
  44. );
  45. //--- Check for module to update ---//
  46. $aModuleInfo = $this->oDb->getModulesBy(array('type' => 'path_and_uri', 'path' => $this->_aConfig['module_dir'], 'uri' => $this->_aConfig['module_uri']));
  47. if(!$aModuleInfo)
  48. return array_merge($aResult, array(
  49. 'code' => BX_DOL_STUDIO_IU_RCE_NOT_FOUND,
  50. 'message' => $this->_displayResult('check_module_exists', false, '_adm_err_modules_module_not_found', $bHtmlResponce),
  51. 'result' => false
  52. ));
  53. if(isset($aParams['module_name']) && strcmp($aParams['module_name'], $aModuleInfo['name']) !== 0)
  54. return array_merge($aResult, array(
  55. 'code' => BX_DOL_STUDIO_IU_RCE_NOT_AVAILABLE,
  56. 'message' => $this->_displayResult('check_module_matches', false, '_adm_err_modules_module_not_match', $bHtmlResponce),
  57. 'result' => false
  58. ));
  59. //--- Check version compatibility ---//
  60. if(!$this->_isCompatibleWith())
  61. return array(
  62. 'code' => BX_DOL_STUDIO_IU_RCE_WSV_MU,
  63. 'message' => $this->_displayResult('check_script_version', false, _t('_adm_err_modules_wrong_version_script_update', $aModuleInfo['title']), $bHtmlResponce),
  64. 'result' => false
  65. );
  66. //--- Check version ---//
  67. if(version_compare(strtolower($aModuleInfo['version']), strtolower($this->_aConfig['version_from'])) != 0)
  68. return array_merge($aResult, array(
  69. 'code' => BX_DOL_STUDIO_IU_RCE_WMV,
  70. 'message' => $this->_displayResult('check_module_version', false, '_adm_err_modules_wrong_version', $bHtmlResponce),
  71. 'result' => false
  72. ));
  73. //--- Check hash ---//
  74. $aFiles = array();
  75. $this->hashFiles(BX_DIRECTORY_PATH_ROOT . 'modules/' . $this->_aConfig['module_dir'], $aFiles);
  76. list($aFilesChanged, $fChangedPercent) = $this->hashCheck($aFiles, $aModuleInfo['id']);
  77. $bAutoupdateForceModifiedFiles = getParam('sys_autoupdate_force_modified_files') == 'on';
  78. if(!empty($aFilesChanged) && !$bAutoupdateForceModifiedFiles)
  79. return array_merge($aResult, array(
  80. 'code' => BX_DOL_STUDIO_IU_RCE_MODIFIED,
  81. 'message' => $this->_displayResult('check_module_hash', false, '_adm_err_modules_module_was_modified', $bHtmlResponce),
  82. 'result' => false
  83. ));
  84. else if($fChangedPercent > BX_FORCE_AUTOUPDATE_MAX_CHANGED_FILES_PERCENT && $bAutoupdateForceModifiedFiles)
  85. return array_merge($aResult, array(
  86. 'code' => BX_DOL_STUDIO_IU_RCE_CHECKSUM_FAILED,
  87. 'message' => $this->_displayResult('check_module_hash', false, _t('_sys_upgrade_files_checksum_failed_too_many_module', round($fChangedPercent * 100), $aModuleInfo['title']), $bHtmlResponce),
  88. 'result' => false
  89. ));
  90. //--- Perform action and check results ---//
  91. $aResult = array_merge($aResult, $this->_perform('install', $aParams));
  92. if($aResult['result']) {
  93. $aSet = array('version' => $this->_aConfig['version_to'], 'updated' => time());
  94. $aModuleConfig = self::getModuleConfig(BX_DIRECTORY_PATH_MODULES . $this->_aConfig['module_dir'] . 'install/config.php');
  95. if (isset($aModuleConfig['note']))
  96. $aSet['note'] = $aModuleConfig['note'];
  97. $this->oDb->updateModule($aSet, array('id' => $aModuleInfo['id']));
  98. //--- Remove update package ---//
  99. $this->delete($aParams);
  100. //--- Increase revision ---//
  101. setParam('sys_revision', 1 + getParam('sys_revision'));
  102. }
  103. return $aResult;
  104. }
  105. //--- Action Methods ---//
  106. protected function actionUpdateFiles($bInstall = true)
  107. {
  108. $sPath = $this->_sHomePath . 'source/';
  109. if(!file_exists($sPath))
  110. return BX_DOL_STUDIO_INSTALLER_FAILED;
  111. $oFile = $this->_getFileManager();
  112. if(empty($oFile))
  113. return BX_DOL_STUDIO_INSTALLER_FAILED;
  114. if(!$oFile->copy($sPath . '*', 'modules/' . $this->_aConfig['module_dir']))
  115. return BX_DOL_STUDIO_INSTALLER_FAILED;
  116. if(!empty($this->_aConfig['delete_files']) && is_array($this->_aConfig['delete_files']))
  117. foreach($this->_aConfig['delete_files'] as $sFile)
  118. if(!$oFile->delete('modules/' . $this->_aConfig['module_dir'] . $sFile))
  119. return BX_DOL_STUDIO_INSTALLER_FAILED;
  120. //--- Update files' tracks
  121. $aModuleInfo = $this->oDb->getModulesBy(array('type' => 'path_and_uri', 'path' => $this->_aConfig['module_dir'], 'uri' => $this->_aConfig['module_uri']));
  122. $this->oDb->deleteModuleTrackFiles($aModuleInfo['id']);
  123. $aFiles = array();
  124. $this->hashFiles(BX_DIRECTORY_PATH_ROOT . 'modules/' . $this->_aConfig['module_dir'], $aFiles);
  125. foreach($aFiles as $aFile)
  126. $this->oDb->insertModuleTrack($aModuleInfo['id'], $aFile);
  127. if (function_exists('opcache_reset'))
  128. opcache_reset();
  129. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  130. }
  131. protected function actionExecuteSql($sOperation)
  132. {
  133. $aModule = $this->oDb->getModuleByUri($this->_aConfig['module_uri']);
  134. if(empty($aModule))
  135. return BX_DOL_STUDIO_INSTALLER_FAILED;
  136. $sPathInstall = $this->_sHomePath . 'install/sql/install.sql';
  137. $sPathEnable = $this->_sHomePath . 'install/sql/enable.sql';
  138. $mixedResult = true;
  139. if(file_exists($sPathInstall))
  140. $mixedResult = $this->oDb->executeSQL($sPathInstall, $this->getMarkersForDb());
  141. if($mixedResult === true && (int)$aModule['enabled'] == 1 && file_exists($sPathEnable))
  142. $mixedResult = $this->oDb->executeSQL($sPathEnable, $this->getMarkersForDb());
  143. return $mixedResult === true ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $mixedResult);
  144. }
  145. protected function actionUpdateLanguages($bInstall = true)
  146. {
  147. $aConfig = self::getModuleConfig($this->_sHomePath . 'install/config.php');
  148. if(empty($aConfig) || !is_array($aConfig))
  149. return BX_DOL_STUDIO_INSTALLER_FAILED;
  150. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  151. $aLanguages = $oLanguages->getLanguagesExt();
  152. $aCategories = array();
  153. if(!empty($aConfig['language_category']) && is_array($aConfig['language_category'])) {
  154. $aCategories = $aConfig['language_category'];
  155. foreach($aCategories as $iIndex => $aCategory)
  156. $aCategories[$iIndex]['id'] = !empty($aCategory['name']) ? $oLanguages->getLanguageCategory($aCategory['name']) : 0;
  157. }
  158. else
  159. $aCategories[] = array(
  160. 'id' => !empty($aConfig['language_category']) ? $oLanguages->getLanguageCategory($aConfig['language_category']) : 0,
  161. 'name' => $aConfig['language_category'],
  162. 'path' => ''
  163. );
  164. foreach($aLanguages as $sName => $aLanguage)
  165. foreach($aCategories as $iIndex => $aCategory)
  166. $this->_updateLanguage($bInstall, $aLanguage, $aCategory['id'], $aCategory['path']);
  167. return $oLanguages->compileLanguage(0, true) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  168. }
  169. /*
  170. * Restore module's language files.
  171. *
  172. * Note. Mainly the action is needed for Updates in 'language' type modules.
  173. * It should be used after 'update_files' action if some changes were done in module's language files.
  174. */
  175. protected function actionRestoreLanguages($bInstall = true)
  176. {
  177. $aConfig = self::getModuleConfig($this->_sHomePath . 'install/config.php');
  178. if(empty($aConfig) || !is_array($aConfig) || empty($aConfig['module_uri']))
  179. return BX_DOL_STUDIO_INSTALLER_FAILED;
  180. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  181. $aLanguages = $oLanguages->getLanguages();
  182. $bResult = true;
  183. foreach($aLanguages as $sName => $sTitle)
  184. $bResult &= $oLanguages->restoreLanguage($sName, $aConfig['module_uri']);
  185. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  186. }
  187. /**
  188. * NOTE. The action is ONLY needed for dependent module to let
  189. * Notifications based module(s) know that he(they) should
  190. * update (request and save) handlers from this dependent module.
  191. */
  192. protected function actionUpdateRelations($sOperation)
  193. {
  194. if(!in_array($sOperation, array('install')))
  195. return BX_DOL_STUDIO_INSTALLER_FAILED;
  196. if(empty($this->_aConfig['relations']) || !is_array($this->_aConfig['relations']))
  197. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  198. foreach($this->_aConfig['relations'] as $sModule) {
  199. if(!$this->oDb->isModuleByName($sModule))
  200. continue;
  201. $aRelation = $this->oDb->getRelationsBy(array('type' => 'module', 'value' => $sModule));
  202. 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']))
  203. continue;
  204. BxDolService::call($aRelation['module'], $aRelation['on_disable'], array($this->_aConfig['module_uri']));
  205. BxDolService::call($aRelation['module'], $aRelation['on_enable'], array($this->_aConfig['module_uri']));
  206. }
  207. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  208. }
  209. /**
  210. * NOTE. The action is ONLY needed for Notifications based modules
  211. * to update (request and save) handlers from all dependent modules.
  212. */
  213. protected function actionUpdateRelationsForAll($sOperation)
  214. {
  215. if(!in_array($sOperation, array('install')))
  216. return BX_DOL_STUDIO_INSTALLER_FAILED;
  217. $aRelation = $this->oDb->getRelationsBy(array('type' => 'module', 'value' => $this->_aModule['name']));
  218. 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']))
  219. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  220. $aModules = $this->oDb->getModulesBy(array('type' => 'all', 'active' => 1));
  221. foreach($aModules as $aModule) {
  222. $aConfig = self::getModuleConfig($aModule);
  223. if(empty($aConfig['relations']) || !is_array($aConfig['relations']) || !in_array($this->_aModule['name'], $aConfig['relations']))
  224. continue;
  225. BxDolService::call($this->_aModule['name'], $aRelation['on_disable'], array($aModule['uri']));
  226. BxDolService::call($this->_aModule['name'], $aRelation['on_enable'], array($aModule['uri']));
  227. }
  228. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  229. }
  230. protected function _updateLanguage($bInstall, $aLanguage, $iCategory = 0, $sPath = '')
  231. {
  232. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  233. $sPathAbsolute = $this->_sHomePath . 'install/langs/' . $sPath . $aLanguage['name'] . '.xml';
  234. $aLanguageInfo = $oLanguages->readLanguage($sPathAbsolute, 'update');
  235. if(empty($aLanguageInfo))
  236. return false;
  237. if(!empty($aLanguageInfo['category']))
  238. $iCategory = $oLanguages->getLanguageCategory($aLanguageInfo['category']);
  239. //--- Process delete. Note. Deletion is performed for all languages. ---//
  240. if(isset($aLanguageInfo['strings_del']))
  241. foreach($aLanguageInfo['strings_del'] as $sKey => $sValue)
  242. $oLanguages->deleteLanguageString($sKey, 0, false);
  243. //--- Process add. Note. Key's category will be updated if it doesn't match. ---//
  244. if(isset($aLanguageInfo['strings_add']))
  245. foreach($aLanguageInfo['strings_add'] as $sKey => $sValue)
  246. $oLanguages->addLanguageString($sKey, $sValue, $aLanguage['id'], $iCategory, false);
  247. //--- Process update. Note. Key's category will be updated if it doesn't match. ---//
  248. if(isset($aLanguageInfo['strings_upd']))
  249. foreach($aLanguageInfo['strings_upd'] as $sKey => $sValue)
  250. $oLanguages->updateLanguageString($sKey, $sValue, $aLanguage['id'], $iCategory, false);
  251. return true;
  252. }
  253. }
  254. /** @} */