BxDolStudioUpdater.php 7.9 KB

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