BxDolStudioUpdater.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. $aModuleInfo = $oDb->getModulesBy(array('type' => 'path_and_uri', 'path' => $this->_aConfig['module_dir'], 'uri' => $this->_aConfig['module_uri']));
  42. if(!$aModuleInfo)
  43. return array_merge($aResult, array(
  44. 'message' => $this->_displayResult('check_module_exists', false, '_adm_err_modules_module_not_found'),
  45. 'result' => false
  46. ));
  47. //--- Check Dolphin version compatibility ---//
  48. if(!$this->_isCompatibleWith())
  49. return array(
  50. 'message' => $this->_displayResult('check_script_version', false, _t('_adm_err_modules_wrong_version_script_update', $aModuleInfo['title'])),
  51. 'result' => false
  52. );
  53. //--- Check version ---//
  54. if(version_compare($aModuleInfo['version'], $this->_aConfig['version_from']) != 0)
  55. return array_merge($aResult, array(
  56. 'message' => $this->_displayResult('check_module_version', false, '_adm_err_modules_wrong_version'),
  57. 'result' => false
  58. ));
  59. //--- Check hash ---//
  60. $aFiles = array();
  61. $this->hashFiles(BX_DIRECTORY_PATH_ROOT . 'modules/' . $this->_aConfig['module_dir'], $aFiles);
  62. list($aFilesChanged, $fChangedPercent) = $this->hashCheck($aFiles, $aModuleInfo['id']);
  63. $bAutoupdateForceModifiedFiles = getParam('sys_autoupdate_force_modified_files') == 'on';
  64. if(!empty($aFilesChanged) && !$bAutoupdateForceModifiedFiles)
  65. return array_merge($aResult, array(
  66. 'message' => $this->_displayResult('check_module_hash', false, '_adm_err_modules_module_was_modified'),
  67. 'result' => false
  68. ));
  69. else if($fChangedPercent > BX_FORCE_AUTOUPDATE_MAX_CHANGED_FILES_PERCENT && $bAutoupdateForceModifiedFiles)
  70. return array_merge($aResult, array(
  71. 'message' => $this->_displayResult('check_module_hash', false, _t('_sys_upgrade_files_checksum_failed_too_many', round($fChangedPercent * 100))),
  72. 'result' => false
  73. ));
  74. //--- Perform action and check results ---//
  75. $aResult = array_merge($aResult, $this->_perform('install', 'Update'));
  76. if($aResult['result']) {
  77. $oDb->updateModule(array('version' => $this->_aConfig['version_to']), array('id' => $aModuleInfo['id']));
  78. $oDb->deleteModuleTrackFiles($aModuleInfo['id']);
  79. $aFiles = array();
  80. $this->hashFiles(BX_DIRECTORY_PATH_ROOT . 'modules/' . $this->_aConfig['module_dir'], $aFiles);
  81. foreach($aFiles as $aFile)
  82. $oDb->insertModuleTrack($aModuleInfo['id'], $aFile);
  83. //--- Remove update pckage ---//
  84. $this->delete();
  85. }
  86. return $aResult;
  87. }
  88. //--- Action Methods ---//
  89. public function actionUpdateFiles($bInstall = true)
  90. {
  91. $sPath = $this->_sHomePath . 'source/';
  92. if(!file_exists($sPath))
  93. return BX_DOL_STUDIO_INSTALLER_FAILED;
  94. $oFile = $this->_getFileManager();
  95. if(empty($oFile))
  96. return BX_DOL_STUDIO_INSTALLER_FAILED;
  97. if(!$oFile->copy($sPath . '*', 'modules/' . $this->_aConfig['module_dir']))
  98. return BX_DOL_STUDIO_INSTALLER_FAILED;
  99. if(!empty($this->_aConfig['delete_files']) && is_array($this->_aConfig['delete_files']))
  100. foreach($this->_aConfig['delete_files'] as $sFile)
  101. if(!$oFile->delete('modules/' . $this->_aConfig['module_dir'] . $sFile))
  102. return BX_DOL_STUDIO_INSTALLER_FAILED;
  103. return BX_DOL_STUDIO_INSTALLER_SUCCESS;
  104. }
  105. public function actionUpdateLanguages($bInstall = true)
  106. {
  107. $aConfig = self::getModuleConfig($this->_sHomePath . 'install/config.php');
  108. if(empty($aConfig) || !is_array($aConfig))
  109. return BX_DOL_STUDIO_INSTALLER_FAILED;
  110. bx_import('BxDolStudioLanguagesUtils');
  111. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  112. $aLanguages = $oLanguages->getLanguages();
  113. $iCategoryId = !empty($aConfig['language_category']) ? $oLanguages->getLanguageCategory($aConfig['language_category']) : 0;
  114. foreach($aLanguages as $sName => $sTitle)
  115. $this->_updateLanguage($bInstall, $sName, $iCategoryId);
  116. return $oLanguages->compileLanguage(0, true) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  117. }
  118. /*
  119. * Restore module's language files.
  120. *
  121. * Note. Mainly the action is needed for Updates in 'language' type modules.
  122. * It should be used after 'update_files' action if some changes were done in module's language files.
  123. */
  124. public function actionRestoreLanguages($bInstall = true)
  125. {
  126. $aConfig = self::getModuleConfig($this->_sHomePath . 'install/config.php');
  127. if(empty($aConfig) || !is_array($aConfig) || empty($aConfig['module_uri']))
  128. return BX_DOL_STUDIO_INSTALLER_FAILED;
  129. bx_import('BxDolStudioLanguagesUtils');
  130. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  131. $aLanguages = $oLanguages->getLanguages();
  132. $bResult = true;
  133. foreach($aLanguages as $sName => $sTitle)
  134. $bResult &= $oLanguages->restoreLanguage($sName, $aConfig['module_uri']);
  135. return $bResult ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  136. }
  137. protected function _updateLanguage($bInstall, $sLanguage, $iCategory = 0)
  138. {
  139. $oDb = BxDolDb::getInstance();
  140. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  141. $sPath = $this->_sHomePath . 'install/langs/' . $sLanguage . '.xml';
  142. $aLanguageInfo = $oLanguages->readLanguage($sPath, 'update');
  143. if(empty($aLanguageInfo))
  144. return false;
  145. $iLanguage = $oLanguages->getLangId($sLanguage);
  146. if(!empty($aLanguageInfo['category']))
  147. $iCategory = $oLanguages->getLanguageCategory($aLanguageInfo['category']);
  148. //--- Process delete. Note. Deletion is performed for all languages. ---//
  149. if(isset($aLanguageInfo['strings_del']))
  150. foreach($aLanguageInfo['strings_del'] as $sKey => $sValue)
  151. $oLanguages->deleteLanguageString($sKey, 0, false);
  152. //--- Process add. Note. Key's category will be updated if it doesn't match. ---//
  153. if(isset($aLanguageInfo['strings_add']))
  154. foreach($aLanguageInfo['strings_add'] as $sKey => $sValue)
  155. $oLanguages->addLanguageString($sKey, $sValue, $iLanguage, $iCategory, false);
  156. //--- Process update. Note. Key's category will be updated if it doesn't match. ---//
  157. if(isset($aLanguageInfo['strings_upd']))
  158. foreach($aLanguageInfo['strings_upd'] as $sKey => $sValue)
  159. $oLanguages->updateLanguageString($sKey, $sValue, $iLanguage, $iCategory, false);
  160. return true;
  161. }
  162. }
  163. /** @} */