BxDolUpgrader.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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()
  20. {
  21. $this->setError(false);
  22. $iUmaskSave = umask(0);
  23. while(true) {
  24. $aVersionUpdateInfo = $this->getVersionUpdateInfo();
  25. if (null === $aVersionUpdateInfo) {
  26. $this->setError(_t('_sys_upgrade_get_version_info_failed'));
  27. break;
  28. }
  29. if (!$this->isUpgradeAvailable($aVersionUpdateInfo))
  30. break;
  31. if (BX_DOL_VERSION != bx_get_ver()) {
  32. $this->setError(_t('_sys_upgrade_db_and_files_versions_different'));
  33. break;
  34. }
  35. $fChangedFilesPercent = 1;
  36. $aFailedFiles = $this->checkFilesChecksums ($fChangedFilesPercent);
  37. $bAutoupdateForceModifiedFiles = ('on' == getParam('sys_autoupdate_force_modified_files'));
  38. if (!empty($aFailedFiles) && !$bAutoupdateForceModifiedFiles) {
  39. $this->setError(_t('_sys_upgrade_files_checksum_failed', implode(', ', $aFailedFiles)));
  40. break;
  41. }
  42. elseif ($fChangedFilesPercent > BX_FORCE_AUTOUPDATE_MAX_CHANGED_FILES_PERCENT && $bAutoupdateForceModifiedFiles) {
  43. $this->setError(_t('_sys_upgrade_files_checksum_failed_too_many', round($fChangedFilesPercent * 100)));
  44. break;
  45. }
  46. if (!($sPatchPath = $this->downloadPatch ($aVersionUpdateInfo))) {
  47. $this->setError(_t('_sys_upgrade_patch_download_failed'));
  48. break;
  49. }
  50. if (!$this->isPatchChecksumCorrect ($sPatchPath, $aVersionUpdateInfo)) {
  51. $this->deleteUpgradePatch($sPatchPath);
  52. $this->setError(_t('_sys_upgrade_patch_checksum_failed'));
  53. break;
  54. }
  55. if (!($sUnpackedPath = $this->unpackPatch ($sPatchPath, true))) {
  56. $this->deleteUpgradePatch($sPatchPath);
  57. $this->setError(_t('_sys_upgrade_patch_unpack_failed'));
  58. break;
  59. }
  60. $this->deleteUpgradePatch($sPatchPath);
  61. if (!$this->isValidPatch ($sUnpackedPath, $aVersionUpdateInfo)) {
  62. $this->deleteUpgradeFolder($sPatchPath);
  63. $this->setError(_t('_sys_upgrade_patch_invalid'));
  64. break;
  65. }
  66. if (!$this->setTransientUpgradeCronJob ($sUnpackedPath)) {
  67. $this->deleteUpgradeFolder($sPatchPath);
  68. $this->setError(_t('_sys_upgrade_set_cron_failed'));
  69. break;
  70. }
  71. break;
  72. }
  73. umask($iUmaskSave);
  74. return $this->getError() ? false : true;
  75. }
  76. public function getLatestVersionNumber ()
  77. {
  78. $a = $this->getVersionUpdateInfo ();
  79. if (!$a)
  80. return false;
  81. return $a['latest_version'];
  82. }
  83. public function getVersionUpdateInfo ()
  84. {
  85. $sUrlVersionCheck = $this->_sUrlVersionCheck . ('beta' == getParam('sys_upgrade_channel') ? 'beta/' : '');
  86. $s = bx_file_get_contents($sUrlVersionCheck, array ('v' => bx_get_ver()));
  87. if (!$s)
  88. return null;
  89. $a = json_decode($s, true);
  90. if (!isset($a['latest_version']))
  91. return null;
  92. return $a;
  93. }
  94. public function isNewVersionAvailable ($a)
  95. {
  96. if (1 == version_compare(strtolower($a['latest_version']), strtolower(bx_get_ver())))
  97. return true;
  98. return false;
  99. }
  100. public function isUpgradeAvailable ($a)
  101. {
  102. if ($this->isNewVersionAvailable($a) && isset($a['patch']))
  103. return true;
  104. return false;
  105. }
  106. protected function downloadPatch ($a)
  107. {
  108. if (!isset($a['patch']['url']))
  109. return false;
  110. if (!($f = fopen($a['patch']['url'], "rb")))
  111. return false;
  112. $sTmpFile = BX_DIRECTORY_PATH_TMP . 'patch_' . bx_get_ver() . '_' . $a['patch']['ver'] . '.zip';
  113. if (file_exists($sTmpFile) && !unlink($sTmpFile))
  114. return false;
  115. $sRet = false;
  116. if (false !== $sTmpFile && false !== file_put_contents($sTmpFile, $f))
  117. $sRet = $sTmpFile;
  118. fclose($f);
  119. return $sRet;
  120. }
  121. protected function isPatchChecksumCorrect ($sPatchPath, $aVersionUpdateInfo)
  122. {
  123. return md5_file($sPatchPath) == $aVersionUpdateInfo['patch']['md5'];
  124. }
  125. protected function unpackPatch ($sPatchPath)
  126. {
  127. $sTmpFolder = $this->getTmpFolderFromZip($sPatchPath);
  128. if (file_exists($sTmpFolder) && !bx_rrmdir($sTmpFolder))
  129. return false;
  130. $oZip = new ZipArchive();
  131. if ($oZip->open($sPatchPath) !== true)
  132. return false;
  133. $sRootFolder = $oZip->numFiles > 0 ? $oZip->getNameIndex(0) : false;
  134. if (!$sRootFolder || !mkdir($sTmpFolder) || !$oZip->extractTo($sTmpFolder))
  135. $sRootFolder = false;
  136. $oZip->close();
  137. return $sRootFolder ? $sTmpFolder . '/' . trim($sRootFolder, '/') . '/' : false;
  138. }
  139. protected function checkFilesChecksums (&$fChangedFilesPercent)
  140. {
  141. $oHasher = bx_instance('BxDolInstallerHasher');
  142. return $oHasher->checkSystemFilesHash($fChangedFilesPercent);
  143. }
  144. protected function isValidPatch ($sUnpackedPath, $aVersionUpdateInfo)
  145. {
  146. $sPath = $sUnpackedPath . 'files/' . $this->normalizeVersion(bx_get_ver()) . '-' . $this->normalizeVersion($aVersionUpdateInfo['patch']['ver']) . '/';
  147. $sCheckFilePath = $sPath . 'check.php';
  148. $sVersionFilePath = $sPath . 'files/inc/version.inc.php';
  149. return file_exists($sCheckFilePath) && file_exists($sVersionFilePath) && file_exists($sUnpackedPath . 'BxDolUpgradeCron.php');
  150. }
  151. public function setTransientUpgradeCronJob ($sUnpackedPath)
  152. {
  153. $sUnpackedPath = rtrim($sUnpackedPath, '/') . '/';
  154. return BxDolCronQuery::getInstance()->addTransientJobClass('sys_perform_upgrade', 'BxDolUpgradeCron', bx_ltrim_str($sUnpackedPath, BX_DIRECTORY_PATH_ROOT) . 'BxDolUpgradeCron.php');
  155. }
  156. public function getError()
  157. {
  158. return $this->_sError;
  159. }
  160. protected function setError($s)
  161. {
  162. $this->_sError = $s;
  163. }
  164. protected function normalizeVersion($s)
  165. {
  166. return str_replace(array('-', '_', ' '), '.', $s);
  167. }
  168. protected function getTmpFolderFromZip($sPatchPath)
  169. {
  170. return preg_replace('/\.zip$/', '', $sPatchPath);
  171. }
  172. protected function deleteUpgradeFolder($sPatchPath)
  173. {
  174. @bx_rrmdir($this->getTmpFolderFromZip($sPatchPath));
  175. }
  176. protected function deleteUpgradePatch($sPatchPath)
  177. {
  178. @unlink($sPatchPath);
  179. }
  180. }
  181. /** @} */