BxDolStudioUpdater.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 TridentStudio Trident Studio
  7. * @{
  8. */
  9. class BxDolStudioUpdater extends BxDolStudioInstaller
  10. {
  11. public function __construct($aConfig)
  12. {
  13. parent::__construct($aConfig);
  14. $this->_sModulePath = $this->_sBasePath . $aConfig['module_dir'];
  15. $this->_aActions = array_merge($this->_aActions, array(
  16. 'check_module_exists' => array(
  17. 'title' => _t('_adm_txt_modules_check_module_exists'),
  18. ),
  19. 'check_module_matches' => array(
  20. 'title' => _t('_adm_txt_modules_check_module_matches'),
  21. ),
  22. 'check_module_version' => array(
  23. 'title' => _t('_adm_txt_modules_check_module_version'),
  24. ),
  25. 'check_module_hash' => array(
  26. 'title' => _t('_adm_txt_modules_check_module_hash'),
  27. ),
  28. 'update_files' => array(
  29. 'title' => _t('_adm_txt_modules_update_files'),
  30. ),
  31. 'restore_languages' => array(
  32. 'title' => _t('_adm_txt_modules_restore_languages'),
  33. ),
  34. ));
  35. }
  36. public function update($aParams)
  37. {
  38. $bHtmlResponce = isset($aParams['html_response']) && (bool)$aParams['html_response'];
  39. $aResult = array(
  40. 'operation_title' => _t('_adm_txt_modules_operation_update', $this->_aConfig['title'], $this->_aConfig['version_from'], $this->_aConfig['version_to'])
  41. );
  42. //--- Check for module to update ---//
  43. $aModuleInfo = $this->oDb->getModulesBy(array('type' => 'path_and_uri', 'path' => $this->_aConfig['module_dir'], 'uri' => $this->_aConfig['module_uri']));
  44. if(!$aModuleInfo)
  45. return array_merge($aResult, array(
  46. 'message' => $this->_displayResult('check_module_exists', false, '_adm_err_modules_module_not_found', $bHtmlResponce),
  47. 'result' => false
  48. ));
  49. if(isset($aParams['module_name']) && strcmp($aParams['module_name'], $aModuleInfo['name']) != 0)
  50. return array_merge($aResult, array(
  51. 'message' => $this->_displayResult('check_module_matches', false, '_adm_err_modules_module_not_match', $bHtmlResponce),
  52. 'result' => false
  53. ));
  54. //--- Check version compatibility ---//
  55. if(!$this->_isCompatibleWith())
  56. return array(
  57. 'message' => $this->_displayResult('check_script_version', false, _t('_adm_err_modules_wrong_version_script_update', $aModuleInfo['title']), $bHtmlResponce),
  58. 'result' => false
  59. );
  60. //--- Check version ---//
  61. if(version_compare($aModuleInfo['version'], $this->_aConfig['version_from']) != 0)
  62. return array_merge($aResult, array(
  63. 'message' => $this->_displayResult('check_module_version', false, '_adm_err_modules_wrong_version', $bHtmlResponce),
  64. 'result' => false
  65. ));
  66. //--- Check hash ---//
  67. $aFiles = array();
  68. $this->hashFiles(BX_DIRECTORY_PATH_ROOT . 'modules/' . $this->_aConfig['module_dir'], $aFiles);
  69. list($aFilesChanged, $fChangedPercent) = $this->hashCheck($aFiles, $aModuleInfo['id']);
  70. $bAutoupdateForceModifiedFiles = getParam('sys_autoupdate_force_modified_files') == 'on';
  71. if(!empty($aFilesChanged) && !$bAutoupdateForceModifiedFiles)
  72. return array_merge($aResult, array(
  73. 'message' => $this->_displayResult('check_module_hash', false, '_adm_err_modules_module_was_modified', $bHtmlResponce),
  74. 'result' => false
  75. ));
  76. else if($fChangedPercent > BX_FORCE_AUTOUPDATE_MAX_CHANGED_FILES_PERCENT && $bAutoupdateForceModifiedFiles)
  77. return array_merge($aResult, array(
  78. 'message' => $this->_displayResult('check_module_hash', false, _t('_sys_upgrade_files_checksum_failed_too_many', round($fChangedPercent * 100)), $bHtmlResponce),
  79. 'result' => false
  80. ));
  81. //--- Perform action and check results ---//
  82. $aResult = array_merge($aResult, $this->_perform('install', $aParams));
  83. if($aResult['result']) {
  84. $this->oDb->updateModule(array('version' => $this->_aConfig['version_to']), array('id' => $aModuleInfo['id']));
  85. $this->oDb->deleteModuleTrackFiles($aModuleInfo['id']);
  86. $aFiles = array();
  87. $this->hashFiles(BX_DIRECTORY_PATH_ROOT . 'modules/' . $this->_aConfig['module_dir'], $aFiles);
  88. foreach($aFiles as $aFile)
  89. $this->oDb->insertModuleTrack($aModuleInfo['id'], $aFile);
  90. //--- Remove update pckage ---//
  91. $this->delete($aParams);
  92. }
  93. return $aResult;
  94. }
  95. //--- Action Methods ---//
  96. public function actionUpdateFiles($bInstall = true)
  97. {
  98. $sPath = $this->_sHomePath . 'source/';
  99. if(!file_exists($sPath))
  100. return BX_DOL_STUDIO_INSTALLER_FAILED;
  101. $oFile = $this->_getFileManager();
  102. if(empty($oFile))
  103. return BX_DOL_STUDIO_INSTALLER_FAILED;
  104. if(!$oFile->copy($sPath . '*', 'modules/' . $this->_aConfig['module_dir']))
  105. return BX_DOL_STUDIO_INSTALLER_FAILED;
  106. if(!empty($this->_aConfig['delete_files']) && is_array($this->_aConfig['delete_files']))
  107. foreach($this->_aConfig['delete_files'] as $sFile)
  108. if(!$oFile->delete('modules/' . $this->_aConfig['module_dir'] . $sFile))
  109. return BX_DOL_STUDIO_INSTALLER_FAILED;
  110. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  111. }
  112. public function actionExecuteSql($sOperation)
  113. {
  114. $aModule = $this->oDb->getModuleByUri($this->_aConfig['module_uri']);
  115. if(empty($aModule))
  116. return BX_DOL_STUDIO_INSTALLER_FAILED;
  117. $sPathInstall = $this->_sHomePath . 'install/sql/install.sql';
  118. $sPathEnable = $this->_sHomePath . 'install/sql/enable.sql';
  119. $mixedResult = true;
  120. if(file_exists($sPathInstall))
  121. $mixedResult = $this->oDb->executeSQL($sPathInstall, $this->getMarkersForDb());
  122. if($mixedResult === true && (int)$aModule['enabled'] == 1 && file_exists($sPathEnable))
  123. $mixedResult = $this->oDb->executeSQL($sPathEnable, $this->getMarkersForDb());
  124. return $mixedResult === true ? BX_DOL_STUDIO_INSTALLER_SUCCESS : array('code' => BX_DOL_STUDIO_INSTALLER_FAILED, 'content' => $mixedResult);
  125. }
  126. public function actionUpdateLanguages($bInstall = true)
  127. {
  128. $aConfig = self::getModuleConfig($this->_sHomePath . 'install/config.php');
  129. if(empty($aConfig) || !is_array($aConfig))
  130. return BX_DOL_STUDIO_INSTALLER_FAILED;
  131. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  132. $aLanguages = $oLanguages->getLanguages();
  133. $iCategoryId = !empty($aConfig['language_category']) ? $oLanguages->getLanguageCategory($aConfig['language_category']) : 0;
  134. foreach($aLanguages as $sName => $sTitle)
  135. $this->_updateLanguage($bInstall, $sName, $iCategoryId);
  136. return $oLanguages->compileLanguage(0, true) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  137. }
  138. /*
  139. * Restore module's language files.
  140. *
  141. * Note. Mainly the action is needed for Updates in 'language' type modules.
  142. * It should be used after 'update_files' action if some changes were done in module's language files.
  143. */
  144. public function actionRestoreLanguages($bInstall = true)
  145. {
  146. $aConfig = self::getModuleConfig($this->_sHomePath . 'install/config.php');
  147. if(empty($aConfig) || !is_array($aConfig) || empty($aConfig['module_uri']))
  148. return BX_DOL_STUDIO_INSTALLER_FAILED;
  149. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  150. $aLanguages = $oLanguages->getLanguages();
  151. $bResult = true;
  152. foreach($aLanguages as $sName => $sTitle)
  153. $bResult &= $oLanguages->restoreLanguage($sName, $aConfig['module_uri']);
  154. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  155. }
  156. protected function _updateLanguage($bInstall, $sLanguage, $iCategory = 0)
  157. {
  158. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  159. $sPath = $this->_sHomePath . 'install/langs/' . $sLanguage . '.xml';
  160. $aLanguageInfo = $oLanguages->readLanguage($sPath, 'update');
  161. if(empty($aLanguageInfo))
  162. return false;
  163. $iLanguage = $oLanguages->getLangId($sLanguage);
  164. if(!empty($aLanguageInfo['category']))
  165. $iCategory = $oLanguages->getLanguageCategory($aLanguageInfo['category']);
  166. //--- Process delete. Note. Deletion is performed for all languages. ---//
  167. if(isset($aLanguageInfo['strings_del']))
  168. foreach($aLanguageInfo['strings_del'] as $sKey => $sValue)
  169. $oLanguages->deleteLanguageString($sKey, 0, false);
  170. //--- Process add. Note. Key's category will be updated if it doesn't match. ---//
  171. if(isset($aLanguageInfo['strings_add']))
  172. foreach($aLanguageInfo['strings_add'] as $sKey => $sValue)
  173. $oLanguages->addLanguageString($sKey, $sValue, $iLanguage, $iCategory, false);
  174. //--- Process update. Note. Key's category will be updated if it doesn't match. ---//
  175. if(isset($aLanguageInfo['strings_upd']))
  176. foreach($aLanguageInfo['strings_upd'] as $sKey => $sValue)
  177. $oLanguages->updateLanguageString($sKey, $sValue, $iLanguage, $iCategory, false);
  178. return true;
  179. }
  180. }
  181. /** @} */