BxDolStudioUpdater.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  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. defined('BX_DOL') or die('hack attempt');
  10. bx_import('BxDolStudioInstaller');
  11. class BxDolStudioUpdater extends BxDolStudioInstaller {
  12. public function BxDolStudioUpdater($aConfig) {
  13. parent::BxDolStudioInstaller($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_version' => array(
  20. 'title' => _t('_adm_txt_modules_check_module_version'),
  21. ),
  22. 'check_module_hash' => array(
  23. 'title' => _t('_adm_txt_modules_check_module_hash'),
  24. ),
  25. 'update_files' => array(
  26. 'title' => _t('_adm_txt_modules_update_files'),
  27. ),
  28. ));
  29. }
  30. public function update($aParams) {
  31. $oDb = BxDolDb::getInstance();
  32. $aResult = array(
  33. 'operation_title' => _t('_adm_txt_modules_operation_update', $this->_aConfig['title'], $this->_aConfig['version_from'], $this->_aConfig['version_to'])
  34. );
  35. //--- Check for module to update ---//
  36. $sQuery = $oDb->prepare("SELECT `id`, `version` FROM `sys_modules` WHERE `path`=? AND `uri`=? LIMIT 1", $this->_aConfig['module_dir'], $this->_aConfig['module_uri']);
  37. $aModuleInfo = $oDb->getRow($sQuery);
  38. if(!$aModuleInfo)
  39. return array_merge($aResult, array(
  40. 'message' => $this->_displayResult('check_module_exists', false, '_adm_err_modules_module_not_found'),
  41. 'result' => false
  42. ));
  43. //--- Check version ---//
  44. if($aModuleInfo['version'] != $this->_aConfig['version_from'])
  45. return array_merge($aResult, array(
  46. 'message' => $this->_displayResult('check_module_version', false, '_adm_err_modules_wrong_version'),
  47. 'result' => false
  48. ));
  49. //--- Check hash ---//
  50. $sQuery = $oDb->prepare("SELECT `file`, `hash` FROM `sys_modules_file_tracks` WHERE `module_id`=?", $aModuleInfo['id']);
  51. $aFilesOrig = $oDb->getAllWithKey($sQuery, "file");
  52. $aFiles = array();
  53. $this->_hash($this->_sModulePath, $aFiles);
  54. foreach($aFiles as $aFile)
  55. if(!isset($aFilesOrig[$aFile['file']]) || $aFilesOrig[$aFile['file']]['hash'] != $aFile['hash'])
  56. return array_merge($aResult, array(
  57. 'message' => $this->_displayResult('check_module_hash', false, '_adm_err_modules_module_was_modified'),
  58. 'result' => false
  59. ));
  60. //--- Perform action and check results ---//
  61. $aResult = array_merge($aResult, $this->_perform('install', 'Update'));
  62. if($aResult['result']) {
  63. $sQuery = $oDb->prepare("UPDATE `sys_modules` SET `version`=? WHERE `id`=?", $this->_aConfig['version_to'], $aModuleInfo['id']);
  64. $oDb->query($sQuery);
  65. $sQuery = $oDb->prepare("DELETE FROM `sys_modules_file_tracks` WHERE `module_id`=?", $aModuleInfo['id']);
  66. $oDb->query($sQuery);
  67. $aFiles = array();
  68. $this->_hash(BX_DIRECTORY_PATH_ROOT . 'modules/' . $this->_aConfig['module_dir'], $aFiles);
  69. foreach($aFiles as $aFile) {
  70. $sQuery = $oDb->prepare("INSERT IGNORE INTO `sys_modules_file_tracks`(`module_id`, `file`, `hash`) VALUES(?, ?, ?)", $aModuleInfo['id'], $aFile['file'], $aFile['hash']);
  71. $oDb->query($sQuery);
  72. }
  73. //--- Remove update pckage ---//
  74. $this->delete();
  75. }
  76. return $aResult;
  77. }
  78. //--- Action Methods ---//
  79. public function actionUpdateFiles($bInstall = true) {
  80. $sPath = $this->_sHomePath . 'source/';
  81. if(!file_exists($sPath))
  82. return BX_DOL_STUDIO_INSTALLER_FAILED;
  83. bx_import('BxDolFtp');
  84. $oFtp = new BxDolFtp($_SERVER['HTTP_HOST'], getParam('sys_ftp_login'), getParam('sys_ftp_password'), getParam('sys_ftp_dir'));
  85. if($oFtp->connect() == false)
  86. return BX_DOL_STUDIO_INSTALLER_FAILED;
  87. return $oFtp->copy($sPath . '*', 'modules/' . $this->_aConfig['module_dir']) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  88. }
  89. public function actionUpdateLanguages($bInstall = true) {
  90. $oDb = BxDolDb::getInstance();
  91. bx_import('BxDolStudioLanguagesUtils');
  92. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  93. $aLanguages = $oLanguages->getLanguages();
  94. //--- Process languages' key=>value pears ---//
  95. $sModuleConfig = $this->_sHomePath . 'install/config.php';
  96. if(!file_exists($sModuleConfig))
  97. return BX_DOL_STUDIO_INSTALLER_FAILED;
  98. include($sModuleConfig);
  99. $iCategoryId = $oLanguages->getLanguageCategory($aConfig['language_category']);
  100. foreach($aLanguages as $sName => $sTitle)
  101. $this->_updateLanguage($bInstall, $sName, $iCategoryId);
  102. return $oLanguages->compileLanguage(0, true) ? BX_DOL_STUDIO_INSTALLER_SUCCESS : BX_DOL_STUDIO_INSTALLER_FAILED;
  103. }
  104. protected function _updateLanguage($bInstall, $sLanguage, $iCategoryId = 0) {
  105. $oDb = BxDolDb::getInstance();
  106. $oLanguages = BxDolStudioLanguagesUtils::getInstance();
  107. $sPath = $this->_sHomePath . 'install/langs/' . $sLanguage . '.xml';
  108. $aLanguageInfo = $oLanguages->readLanguage($sPath, 'update');
  109. if(empty($aLanguageInfo))
  110. return false;
  111. $iLanguage = $oLanguages->getLangId($sLanguage);
  112. //--- Process delete ---//
  113. if(isset($aLanguageInfo['strings_del']) && is_array($aLanguageInfo['strings_del']))
  114. $oLanguages->deleteLanguageKeys($aLanguageInfo['strings_del']);
  115. //--- Process add ---//
  116. if(isset($aLanguageInfo['strings_add']) && is_array($aLanguageInfo['strings_add']))
  117. $oLanguages->addLanguageKeys($iLanguage, $iCategoryId, $aLanguageInfo['strings_add']);
  118. //--- Process update ---//
  119. if(isset($aLanguageInfo['strings_upd']) && is_array($aLanguageInfo['strings_upd'])) {
  120. $oLanguages->deleteLanguageKeys($aLanguageInfo['strings_upd']);
  121. $oLanguages->addLanguageKeys($iLanguage, $iCategoryId, $aLanguageInfo['strings_upd']);
  122. }
  123. return true;
  124. }
  125. }
  126. /** @} */