BxDolUpgrader.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolUpgrader extends BxDol
  10. {
  11. protected $_sUrlVersionCheck = 'https://v.una.io/';
  12. protected $_sError = false;
  13. /**
  14. * Prepare for system upgrade.
  15. * It prepares upgrade by downloading upgrade pack and performing different checks.
  16. * After all check are successfully completed, transient cron job is set to run upgrade upon next cron run.
  17. * @return true if upgrade was successfully scheduled to run, or false on error (you can call @see getError to determine particular error message)
  18. */
  19. public function prepare($bAuto = true)
  20. {
  21. $this->setError(false);
  22. $iUmaskSave = umask(0);
  23. while(true) {
  24. if(!$bAuto) {
  25. $aUpdates = BxDolStudioInstallerUtils::getInstance()->checkUpdates();
  26. if(!empty($aUpdates) && is_array($aUpdates)) {
  27. $this->setError(_t('_sys_upgrade_modules_upgrade_first'));
  28. break;
  29. }
  30. }
  31. $aVersionUpdateInfo = $this->getVersionUpdateInfo();
  32. if (null === $aVersionUpdateInfo) {
  33. $this->setError(_t('_sys_upgrade_get_version_info_failed'));
  34. break;
  35. }
  36. if (!$this->isUpgradeAvailable($aVersionUpdateInfo))
  37. break;
  38. if (BX_DOL_VERSION != bx_get_ver()) {
  39. $this->setError(_t('_sys_upgrade_db_and_files_versions_different'));
  40. break;
  41. }
  42. $fChangedFilesPercent = 1;
  43. $aFailedFiles = $this->checkFilesChecksums ($fChangedFilesPercent);
  44. $bAutoupdateForceModifiedFiles = ('on' == getParam('sys_autoupdate_force_modified_files'));
  45. if (!empty($aFailedFiles) && !$bAutoupdateForceModifiedFiles) {
  46. $this->setError(_t('_sys_upgrade_files_checksum_failed', implode(', ', $aFailedFiles)));
  47. break;
  48. }
  49. elseif ($fChangedFilesPercent > BX_FORCE_AUTOUPDATE_MAX_CHANGED_FILES_PERCENT && $bAutoupdateForceModifiedFiles) {
  50. $this->setError(_t('_sys_upgrade_files_checksum_failed_too_many', round($fChangedFilesPercent * 100)));
  51. break;
  52. }
  53. if (!($sPatchPath = $this->downloadPatch ($aVersionUpdateInfo))) {
  54. $this->setError(_t('_sys_upgrade_patch_download_failed'));
  55. break;
  56. }
  57. if (!$this->isPatchChecksumCorrect ($sPatchPath, $aVersionUpdateInfo)) {
  58. $this->deleteUpgradePatch($sPatchPath);
  59. $this->setError(_t('_sys_upgrade_patch_checksum_failed'));
  60. break;
  61. }
  62. if (!($sUnpackedPath = $this->unpackPatch ($sPatchPath, true))) {
  63. $this->deleteUpgradePatch($sPatchPath);
  64. $this->setError(_t('_sys_upgrade_patch_unpack_failed'));
  65. break;
  66. }
  67. $this->deleteUpgradePatch($sPatchPath);
  68. if (!$this->isValidPatch ($sUnpackedPath, $aVersionUpdateInfo)) {
  69. $this->deleteUpgradeFolder($sPatchPath);
  70. $this->setError(_t('_sys_upgrade_patch_invalid'));
  71. break;
  72. }
  73. if (!$this->setTransientUpgradeCronJob ($sUnpackedPath)) {
  74. $this->deleteUpgradeFolder($sPatchPath);
  75. $this->setError(_t('_sys_upgrade_set_cron_failed'));
  76. break;
  77. }
  78. break;
  79. }
  80. umask($iUmaskSave);
  81. return $this->getError() ? false : true;
  82. }
  83. public function getLatestVersionNumber ()
  84. {
  85. $a = $this->getVersionUpdateInfo ();
  86. if (!$a)
  87. return false;
  88. return $a['latest_version'];
  89. }
  90. public function getVersionUpdateInfo ()
  91. {
  92. $sUrlVersionCheck = $this->_sUrlVersionCheck . ('beta' == getParam('sys_upgrade_channel') ? 'beta/' : '');
  93. $s = bx_file_get_contents($sUrlVersionCheck, array ('v' => bx_get_ver()));
  94. if (!$s)
  95. return null;
  96. $a = json_decode($s, true);
  97. if (!isset($a['latest_version']))
  98. return null;
  99. return $a;
  100. }
  101. public function isNewVersionAvailable ($a)
  102. {
  103. if (1 == version_compare(strtolower($a['latest_version']), strtolower(bx_get_ver())))
  104. return true;
  105. return false;
  106. }
  107. public function isUpgradeAvailable ($a)
  108. {
  109. if ($this->isNewVersionAvailable($a) && isset($a['patch']))
  110. return true;
  111. return false;
  112. }
  113. protected function downloadPatch ($a)
  114. {
  115. if (!isset($a['patch']['url']))
  116. return false;
  117. if (!($f = fopen($a['patch']['url'], "rb")))
  118. return false;
  119. $sTmpFile = BX_DIRECTORY_PATH_TMP . 'patch_' . bx_get_ver() . '_' . $a['patch']['ver'] . '.zip';
  120. if (file_exists($sTmpFile) && !unlink($sTmpFile))
  121. return false;
  122. $sRet = false;
  123. if (false !== $sTmpFile && false !== file_put_contents($sTmpFile, $f))
  124. $sRet = $sTmpFile;
  125. fclose($f);
  126. return $sRet;
  127. }
  128. protected function isPatchChecksumCorrect ($sPatchPath, $aVersionUpdateInfo)
  129. {
  130. return md5_file($sPatchPath) == $aVersionUpdateInfo['patch']['md5'];
  131. }
  132. protected function unpackPatch ($sPatchPath)
  133. {
  134. $sTmpFolder = $this->getTmpFolderFromZip($sPatchPath);
  135. if (file_exists($sTmpFolder) && !bx_rrmdir($sTmpFolder))
  136. return false;
  137. $oZip = new ZipArchive();
  138. if ($oZip->open($sPatchPath) !== true)
  139. return false;
  140. $sRootFolder = $oZip->numFiles > 0 ? $oZip->getNameIndex(0) : false;
  141. if (!$sRootFolder || !mkdir($sTmpFolder) || !$oZip->extractTo($sTmpFolder))
  142. $sRootFolder = false;
  143. $oZip->close();
  144. return $sRootFolder ? $sTmpFolder . '/' . trim($sRootFolder, '/') . '/' : false;
  145. }
  146. protected function checkFilesChecksums (&$fChangedFilesPercent)
  147. {
  148. $oHasher = bx_instance('BxDolInstallerHasher');
  149. return $oHasher->checkSystemFilesHash($fChangedFilesPercent);
  150. }
  151. protected function isValidPatch ($sUnpackedPath, $aVersionUpdateInfo)
  152. {
  153. $sPath = $sUnpackedPath . 'files/' . $this->normalizeVersion(bx_get_ver()) . '-' . $this->normalizeVersion($aVersionUpdateInfo['patch']['ver']) . '/';
  154. $sCheckFilePath = $sPath . 'check.php';
  155. $sVersionFilePath = $sPath . 'files/inc/version.inc.php';
  156. return file_exists($sCheckFilePath) && file_exists($sVersionFilePath) && file_exists($sUnpackedPath . 'BxDolUpgradeCron.php');
  157. }
  158. public function setTransientUpgradeCronJob ($sUnpackedPath)
  159. {
  160. $sUnpackedPath = rtrim($sUnpackedPath, '/') . '/';
  161. return BxDolCronQuery::getInstance()->addTransientJobClass('sys_perform_upgrade', 'BxDolUpgradeCron', bx_ltrim_str($sUnpackedPath, BX_DIRECTORY_PATH_ROOT) . 'BxDolUpgradeCron.php');
  162. }
  163. public function getError()
  164. {
  165. return $this->_sError;
  166. }
  167. protected function setError($s)
  168. {
  169. $this->_sError = $s;
  170. }
  171. protected function normalizeVersion($s)
  172. {
  173. return str_replace(array('-', '_', ' '), '.', $s);
  174. }
  175. protected function getTmpFolderFromZip($sPatchPath)
  176. {
  177. return preg_replace('/\.zip$/', '', $sPatchPath);
  178. }
  179. protected function deleteUpgradeFolder($sPatchPath)
  180. {
  181. @bx_rrmdir($this->getTmpFolderFromZip($sPatchPath));
  182. }
  183. protected function deleteUpgradePatch($sPatchPath)
  184. {
  185. @unlink($sPatchPath);
  186. }
  187. }
  188. /** @} */