BxDolUpgrader.php 8.6 KB

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