BxDolUpgrader.php 7.8 KB

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