BxDolStudioUpdater.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. ));
  30. }
  31. public function update($aParams)
  32. {
  33. $oDb = bx_instance('BxDolStudioInstallerQuery');
  34. $aResult = array(
  35. 'operation_title' => _t('_adm_txt_modules_operation_update', $this->_aConfig['title'], $this->_aConfig['version_from'], $this->_aConfig['version_to'])
  36. );
  37. //--- Check for module to update ---//
  38. $sQuery = $oDb->prepare("SELECT `id`, `version` FROM `sys_modules` WHERE `path`=? AND `uri`=? LIMIT 1", $this->_aConfig['module_dir'], $this->_aConfig['module_uri']);
  39. $aModuleInfo = $oDb->getRow($sQuery);
  40. if(!$aModuleInfo)
  41. return array_merge($aResult, array(
  42. 'message' => $this->_displayResult('check_module_exists', false, '_adm_err_modules_module_not_found'),
  43. 'result' => false
  44. ));
  45. //--- Check version ---//
  46. if($aModuleInfo['version'] != $this->_aConfig['version_from'])
  47. return array_merge($aResult, array(
  48. 'message' => $this->_displayResult('check_module_version', false, '_adm_err_modules_wrong_version'),
  49. 'result' => false
  50. ));
  51. //--- Check hash ---//
  52. $aFiles = array();
  53. $this->hashFiles(BX_DIRECTORY_PATH_ROOT . 'modules/' . $this->_aConfig['module_dir'], $aFiles);
  54. list($aFilesChanged, $fChangedPercent) = $this->hashCheck($aFiles, $aModuleInfo['id']);
  55. $bAutoupdateForceModifiedFiles = getParam('sys_autoupdate_force_modified_files') == 'on';
  56. if(!empty($aFilesChanged) && !$bAutoupdateForceModifiedFiles)
  57. return array_merge($aResult, array(
  58. 'message' => $this->_displayResult('check_module_hash', false, '_adm_err_modules_module_was_modified'),
  59. 'result' => false
  60. ));
  61. else if($fChangedPercent > BX_FORCE_AUTOUPDATE_MAX_CHANGED_FILES_PERCENT && $bAutoupdateForceModifiedFiles)
  62. return array_merge($aResult, array(
  63. 'message' => $this->_displayResult('check_module_hash', false, _t('_sys_upgrade_files_checksum_failed_too_many', round($fChangedPercent * 100))),
  64. 'result' => false
  65. ));
  66. //--- Perform action and check results ---//
  67. $aResult = array_merge($aResult, $this->_perform('install', 'Update'));
  68. if($aResult['result']) {
  69. $sQuery = $oDb->prepare("UPDATE `sys_modules` SET `version`=? WHERE `id`=?", $this->_aConfig['version_to'], $aModuleInfo['id']);
  70. $oDb->query($sQuery);
  71. $oDb->deleteModuleTrackFiles($aModuleInfo['id']);
  72. $aFiles = array();
  73. $this->hashFiles(BX_DIRECTORY_PATH_ROOT . 'modules/' . $this->_aConfig['module_dir'], $aFiles);
  74. foreach($aFiles as $aFile)
  75. $oDb->insertModuleTrack($aModuleInfo['id'], $aFile);
  76. //--- Remove update pckage ---//
  77. $this->delete();
  78. }
  79. return $aResult;
  80. }
  81. //--- Action Methods ---//
  82. public function actionUpdateFiles($bInstall = true)
  83. {
  84. $sPath = $this->_sHomePath . 'source/';
  85. if(!file_exists($sPath))
  86. return BX_DOL_STUDIO_INSTALLER_FAILED;
  87. bx_import('BxDolFtp');
  88. $oFtp = new BxDolFtp($_SERVER['HTTP_HOST'], getParam('sys_ftp_login'), getParam('sys_ftp_password'), getParam('sys_ftp_dir'));
  89. if($oFtp->connect() == false)
  90. return BX_DOL_STUDIO_INSTALLER_FAILED;
  91. return $oFtp->copy($sPath . '*', 'modules/' . $this->_aConfig['module_dir']) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  92. }
  93. public function actionUpdateLanguages($bInstall = true)
  94. {
  95. $oDb = BxDolDb::getInstance();
  96. bx_import('BxDolStudioLanguagesUtils');
  97. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  98. $aLanguages = $oLanguages->getLanguages();
  99. //--- Process languages' key=>value pears ---//
  100. $aConfig = self::getModuleConfig($this->_sHomePath . 'install/config.php');
  101. if(empty($aConfig) || !is_array($aConfig))
  102. return BX_DOL_STUDIO_INSTALLER_FAILED;
  103. $iCategoryId = $oLanguages->getLanguageCategory($aConfig['language_category']);
  104. foreach($aLanguages as $sName => $sTitle)
  105. $this->_updateLanguage($bInstall, $sName, $iCategoryId);
  106. return $oLanguages->compileLanguage(0, true) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  107. }
  108. protected function _updateLanguage($bInstall, $sLanguage, $iCategoryId = 0)
  109. {
  110. $oDb = BxDolDb::getInstance();
  111. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  112. $sPath = $this->_sHomePath . 'install/langs/' . $sLanguage . '.xml';
  113. $aLanguageInfo = $oLanguages->readLanguage($sPath, 'update');
  114. if(empty($aLanguageInfo))
  115. return false;
  116. $iLanguage = $oLanguages->getLangId($sLanguage);
  117. //--- Process delete ---//
  118. if(isset($aLanguageInfo['strings_del']) && is_array($aLanguageInfo['strings_del']))
  119. $oLanguages->deleteLanguageKeys($aLanguageInfo['strings_del']);
  120. //--- Process add ---//
  121. if(isset($aLanguageInfo['strings_add']) && is_array($aLanguageInfo['strings_add']))
  122. $oLanguages->addLanguageKeys($iLanguage, $iCategoryId, $aLanguageInfo['strings_add']);
  123. //--- Process update ---//
  124. if(isset($aLanguageInfo['strings_upd']) && is_array($aLanguageInfo['strings_upd'])) {
  125. $oLanguages->deleteLanguageKeys($aLanguageInfo['strings_upd']);
  126. $oLanguages->addLanguageKeys($iLanguage, $iCategoryId, $aLanguageInfo['strings_upd']);
  127. }
  128. return true;
  129. }
  130. }
  131. /** @} */