BxDolStudioUpdater.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. $aCategories = array();
  134. if(!empty($aConfig['language_category']) && is_array($aConfig['language_category'])) {
  135. $aCategories = $aConfig['language_category'];
  136. foreach($aCategories as $iIndex => $aCategory)
  137. $aCategories[$iIndex]['id'] = !empty($aCategory['name']) ? $oLanguages->getLanguageCategory($aCategory['name']) : 0;
  138. }
  139. else
  140. $aCategories[] = array(
  141. 'id' => !empty($aConfig['language_category']) ? $oLanguages->getLanguageCategory($aConfig['language_category']) : 0,
  142. 'name' => $aConfig['language_category'],
  143. 'path' => ''
  144. );
  145. foreach($aLanguages as $sName => $sTitle)
  146. foreach($aCategories as $iIndex => $aCategory)
  147. $this->_updateLanguage($bInstall, $sName, $aCategory['id'], $aCategory['path']);
  148. return $oLanguages->compileLanguage(0, true) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  149. }
  150. /*
  151. * Restore module's language files.
  152. *
  153. * Note. Mainly the action is needed for Updates in 'language' type modules.
  154. * It should be used after 'update_files' action if some changes were done in module's language files.
  155. */
  156. public function actionRestoreLanguages($bInstall = true)
  157. {
  158. $aConfig = self::getModuleConfig($this->_sHomePath . 'install/config.php');
  159. if(empty($aConfig) || !is_array($aConfig) || empty($aConfig['module_uri']))
  160. return BX_DOL_STUDIO_INSTALLER_FAILED;
  161. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  162. $aLanguages = $oLanguages->getLanguages();
  163. $bResult = true;
  164. foreach($aLanguages as $sName => $sTitle)
  165. $bResult &= $oLanguages->restoreLanguage($sName, $aConfig['module_uri']);
  166. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  167. }
  168. protected function _updateLanguage($bInstall, $sLanguage, $iCategory = 0, $sPath = '')
  169. {
  170. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  171. $sPathAbsolute = $this->_sHomePath . 'install/langs/' . $sPath . $sLanguage . '.xml';
  172. $aLanguageInfo = $oLanguages->readLanguage($sPathAbsolute, 'update');
  173. if(empty($aLanguageInfo))
  174. return false;
  175. $iLanguage = $oLanguages->getLangId($sLanguage);
  176. if(!empty($aLanguageInfo['category']))
  177. $iCategory = $oLanguages->getLanguageCategory($aLanguageInfo['category']);
  178. //--- Process delete. Note. Deletion is performed for all languages. ---//
  179. if(isset($aLanguageInfo['strings_del']))
  180. foreach($aLanguageInfo['strings_del'] as $sKey => $sValue)
  181. $oLanguages->deleteLanguageString($sKey, 0, false);
  182. //--- Process add. Note. Key's category will be updated if it doesn't match. ---//
  183. if(isset($aLanguageInfo['strings_add']))
  184. foreach($aLanguageInfo['strings_add'] as $sKey => $sValue)
  185. $oLanguages->addLanguageString($sKey, $sValue, $iLanguage, $iCategory, false);
  186. //--- Process update. Note. Key's category will be updated if it doesn't match. ---//
  187. if(isset($aLanguageInfo['strings_upd']))
  188. foreach($aLanguageInfo['strings_upd'] as $sKey => $sValue)
  189. $oLanguages->updateLanguageString($sKey, $sValue, $iLanguage, $iCategory, false);
  190. return true;
  191. }
  192. }
  193. /** @} */