BxDolUpgrader.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  7. * @{
  8. */
  9. class BxDolUpgrader extends BxDol
  10. {
  11. protected $_sUrlVersionCheck = 'http://rss.boonex.com/trident/';
  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. }
  45. if (!($sPatchPath = $this->downloadPatch ($aVersionUpdateInfo))) {
  46. $this->setError(_t('_sys_upgrade_patch_download_failed'));
  47. break;
  48. }
  49. if (!$this->isPatchChecksumCorrect ($sPatchPath, $aVersionUpdateInfo)) {
  50. $this->deleteUpgradePatch($sPatchPath);
  51. $this->setError(_t('_sys_upgrade_patch_checksum_failed'));
  52. break;
  53. }
  54. if (!($sUnpackedPath = $this->unpackPatch ($sPatchPath, true))) {
  55. $this->deleteUpgradePatch($sPatchPath);
  56. $this->setError(_t('_sys_upgrade_patch_unpack_failed'));
  57. break;
  58. }
  59. $this->deleteUpgradePatch($sPatchPath);
  60. if (!$this->isValidPatch ($sUnpackedPath, $aVersionUpdateInfo)) {
  61. $this->deleteUpgradeFolder($sPatchPath);
  62. $this->setError(_t('_sys_upgrade_patch_invalid'));
  63. break;
  64. }
  65. if (!$this->setTransientUpgradeCronJob ($sUnpackedPath)) {
  66. $this->deleteUpgradeFolder($sPatchPath);
  67. $this->setError(_t('_sys_upgrade_set_cron_failed'));
  68. break;
  69. }
  70. break;
  71. }
  72. umask($iUmaskSave);
  73. return $this->getError() ? false : true;
  74. }
  75. public function getLatestVersionNumber ()
  76. {
  77. $a = $this->getVersionUpdateInfo ();
  78. if (!$a)
  79. return false;
  80. return $a['latest_version'];
  81. }
  82. public function getVersionUpdateInfo ()
  83. {
  84. $s = bx_file_get_contents($this->_sUrlVersionCheck, array ('v' => bx_get_ver()));
  85. if (!$s)
  86. return null;
  87. $a = json_decode($s, true);
  88. if (!isset($a['latest_version']))
  89. return null;
  90. return $a;
  91. }
  92. public function isNewVersionAvailable ($a)
  93. {
  94. if (1 == version_compare(strtolower($a['latest_version']), strtolower(bx_get_ver())))
  95. return true;
  96. return false;
  97. }
  98. public function isUpgradeAvailable ($a)
  99. {
  100. if ($this->isNewVersionAvailable($a) && isset($a['patch']))
  101. return true;
  102. return false;
  103. }
  104. protected function downloadPatch ($a)
  105. {
  106. if (!isset($a['patch']['url']))
  107. return false;
  108. if (!($f = fopen($a['patch']['url'], "rb")))
  109. return false;
  110. $sTmpFile = BX_DIRECTORY_PATH_TMP . 'patch_' . bx_get_ver() . '_' . $a['patch']['ver'] . '.zip';
  111. if (file_exists($sTmpFile) && !unlink($sTmpFile))
  112. return false;
  113. $sRet = false;
  114. if (false !== $sTmpFile && false !== file_put_contents($sTmpFile, $f))
  115. $sRet = $sTmpFile;
  116. fclose($f);
  117. return $sRet;
  118. }
  119. protected function isPatchChecksumCorrect ($sPatchPath, $aVersionUpdateInfo)
  120. {
  121. return md5_file($sPatchPath) == $aVersionUpdateInfo['patch']['md5'];
  122. }
  123. protected function unpackPatch ($sPatchPath)
  124. {
  125. $sTmpFolder = $this->getTmpFolderFromZip($sPatchPath);
  126. if (file_exists($sTmpFolder) && !bx_rrmdir($sTmpFolder))
  127. return false;
  128. $oZip = new ZipArchive();
  129. if ($oZip->open($sPatchPath) !== true)
  130. return false;
  131. $sRootFolder = $oZip->numFiles > 0 ? $oZip->getNameIndex(0) : false;
  132. if (!$sRootFolder || !mkdir($sTmpFolder) || !$oZip->extractTo($sTmpFolder))
  133. $sRootFolder = false;
  134. $oZip->close();
  135. return $sRootFolder ? $sTmpFolder . '/' . trim($sRootFolder, '/') . '/' : false;
  136. }
  137. protected function checkFilesChecksums (&$fChangedFilesPercent)
  138. {
  139. $oHasher = bx_instance('BxDolInstallerHasher');
  140. return $oHasher->checkSystemFilesHash($fChangedFilesPercent);
  141. }
  142. protected function isValidPatch ($sUnpackedPath, $aVersionUpdateInfo)
  143. {
  144. $sPath = $sUnpackedPath . 'files/' . $this->normalizeVersion(bx_get_ver()) . '-' . $this->normalizeVersion($aVersionUpdateInfo['patch']['ver']) . '/';
  145. $sCheckFilePath = $sPath . 'check.php';
  146. $sVersionFilePath = $sPath . 'files/inc/version.inc.php';
  147. return file_exists($sCheckFilePath) && file_exists($sVersionFilePath) && file_exists($sUnpackedPath . 'BxDolUpgradeCron.php');
  148. }
  149. public function setTransientUpgradeCronJob ($sUnpackedPath)
  150. {
  151. $sUnpackedPath = rtrim($sUnpackedPath, '/') . '/';
  152. return BxDolCronQuery::getInstance()->addTransientJobClass('sys_perform_upgrade', 'BxDolUpgradeCron', bx_ltrim_str($sUnpackedPath, BX_DIRECTORY_PATH_ROOT) . 'BxDolUpgradeCron.php');
  153. }
  154. public function getError()
  155. {
  156. return $this->_sError;
  157. }
  158. protected function setError($s)
  159. {
  160. $this->_sError = $s;
  161. }
  162. protected function normalizeVersion($s)
  163. {
  164. return str_replace(array('-', '_', ' '), '.', $s);
  165. }
  166. protected function getTmpFolderFromZip($sPatchPath)
  167. {
  168. return preg_replace('/\.zip$/', '', $sPatchPath);
  169. }
  170. protected function deleteUpgradeFolder($sPatchPath)
  171. {
  172. @bx_rrmdir($this->getTmpFolderFromZip($sPatchPath));
  173. }
  174. protected function deleteUpgradePatch($sPatchPath)
  175. {
  176. @unlink($sPatchPath);
  177. }
  178. }
  179. /** @} */