BxDolUpgrader.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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/beta/';
  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. $s = bx_file_get_contents($this->_sUrlVersionCheck, array ('v' => bx_get_ver()));
  86. if (!$s)
  87. return null;
  88. $a = json_decode($s, true);
  89. if (!isset($a['latest_version']))
  90. return null;
  91. return $a;
  92. }
  93. public function isNewVersionAvailable ($a)
  94. {
  95. if (1 == version_compare(strtolower($a['latest_version']), strtolower(bx_get_ver())))
  96. return true;
  97. return false;
  98. }
  99. public function isUpgradeAvailable ($a)
  100. {
  101. if ($this->isNewVersionAvailable($a) && isset($a['patch']))
  102. return true;
  103. return false;
  104. }
  105. protected function downloadPatch ($a)
  106. {
  107. if (!isset($a['patch']['url']))
  108. return false;
  109. if (!($f = fopen($a['patch']['url'], "rb")))
  110. return false;
  111. $sTmpFile = BX_DIRECTORY_PATH_TMP . 'patch_' . bx_get_ver() . '_' . $a['patch']['ver'] . '.zip';
  112. if (file_exists($sTmpFile) && !unlink($sTmpFile))
  113. return false;
  114. $sRet = false;
  115. if (false !== $sTmpFile && false !== file_put_contents($sTmpFile, $f))
  116. $sRet = $sTmpFile;
  117. fclose($f);
  118. return $sRet;
  119. }
  120. protected function isPatchChecksumCorrect ($sPatchPath, $aVersionUpdateInfo)
  121. {
  122. return md5_file($sPatchPath) == $aVersionUpdateInfo['patch']['md5'];
  123. }
  124. protected function unpackPatch ($sPatchPath)
  125. {
  126. $sTmpFolder = $this->getTmpFolderFromZip($sPatchPath);
  127. if (file_exists($sTmpFolder) && !bx_rrmdir($sTmpFolder))
  128. return false;
  129. $oZip = new ZipArchive();
  130. if ($oZip->open($sPatchPath) !== true)
  131. return false;
  132. $sRootFolder = $oZip->numFiles > 0 ? $oZip->getNameIndex(0) : false;
  133. if (!$sRootFolder || !mkdir($sTmpFolder) || !$oZip->extractTo($sTmpFolder))
  134. $sRootFolder = false;
  135. $oZip->close();
  136. return $sRootFolder ? $sTmpFolder . '/' . trim($sRootFolder, '/') . '/' : false;
  137. }
  138. protected function checkFilesChecksums (&$fChangedFilesPercent)
  139. {
  140. $oHasher = bx_instance('BxDolInstallerHasher');
  141. return $oHasher->checkSystemFilesHash($fChangedFilesPercent);
  142. }
  143. protected function isValidPatch ($sUnpackedPath, $aVersionUpdateInfo)
  144. {
  145. $sPath = $sUnpackedPath . 'files/' . $this->normalizeVersion(bx_get_ver()) . '-' . $this->normalizeVersion($aVersionUpdateInfo['patch']['ver']) . '/';
  146. $sCheckFilePath = $sPath . 'check.php';
  147. $sVersionFilePath = $sPath . 'files/inc/version.inc.php';
  148. return file_exists($sCheckFilePath) && file_exists($sVersionFilePath) && file_exists($sUnpackedPath . 'BxDolUpgradeCron.php');
  149. }
  150. public function setTransientUpgradeCronJob ($sUnpackedPath)
  151. {
  152. $sUnpackedPath = rtrim($sUnpackedPath, '/') . '/';
  153. return BxDolCronQuery::getInstance()->addTransientJobClass('sys_perform_upgrade', 'BxDolUpgradeCron', bx_ltrim_str($sUnpackedPath, BX_DIRECTORY_PATH_ROOT) . 'BxDolUpgradeCron.php');
  154. }
  155. public function getError()
  156. {
  157. return $this->_sError;
  158. }
  159. protected function setError($s)
  160. {
  161. $this->_sError = $s;
  162. }
  163. protected function normalizeVersion($s)
  164. {
  165. return str_replace(array('-', '_', ' '), '.', $s);
  166. }
  167. protected function getTmpFolderFromZip($sPatchPath)
  168. {
  169. return preg_replace('/\.zip$/', '', $sPatchPath);
  170. }
  171. protected function deleteUpgradeFolder($sPatchPath)
  172. {
  173. @bx_rrmdir($this->getTmpFolderFromZip($sPatchPath));
  174. }
  175. protected function deleteUpgradePatch($sPatchPath)
  176. {
  177. @unlink($sPatchPath);
  178. }
  179. }
  180. /** @} */