UpgradeController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Controllers;
  3. use Slim\Http\Request;
  4. use Slim\Http\Response;
  5. use ZipArchive;
  6. class UpgradeController extends Controller
  7. {
  8. const GITHUB_SOURCE_API = 'https://api.github.com/repos/SergiX44/XBackBone/releases';
  9. /**
  10. * @param Request $request
  11. * @param Response $response
  12. * @return Response
  13. */
  14. public function upgrade(Request $request, Response $response): Response
  15. {
  16. if (!is_writable(BASE_DIR)) {
  17. $this->session->alert(lang('path_not_writable', BASE_DIR), 'warning');
  18. return redirect($response, 'system');
  19. }
  20. try {
  21. $json = $this->getApiJson();
  22. } catch (\RuntimeException $e) {
  23. $this->session->alert($e->getMessage(), 'danger');
  24. return redirect($response, 'system');
  25. }
  26. if (version_compare($json[0]->tag_name, PLATFORM_VERSION, '<=')) {
  27. $this->session->alert(lang('already_latest_version'), 'warning');
  28. return redirect($response, 'system');
  29. }
  30. $tmpFile = sys_get_temp_dir().DIRECTORY_SEPARATOR.'xbackbone_update.zip';
  31. if (file_put_contents($tmpFile, file_get_contents($json[0]->assets[0]->browser_download_url)) === false) {
  32. $this->session->alert(lang('cannot_retrieve_file'), 'danger');
  33. return redirect($response, 'system');
  34. };
  35. if (filesize($tmpFile) !== $json[0]->assets[0]->size) {
  36. $this->session->alert(lang('file_size_no_match'), 'danger');
  37. return redirect($response, 'system');
  38. }
  39. $config = require BASE_DIR.'config.php';
  40. $config['maintenance'] = true;
  41. file_put_contents(BASE_DIR.'config.php', '<?php'.PHP_EOL.'return '.var_export($config, true).';');
  42. $currentFiles = array_merge(
  43. glob_recursive(BASE_DIR.'app/*'),
  44. glob_recursive(BASE_DIR.'bin/*'),
  45. glob_recursive(BASE_DIR.'bootstrap/*'),
  46. glob_recursive(BASE_DIR.'resources/templates/*'),
  47. glob_recursive(BASE_DIR.'resources/lang/*'),
  48. glob_recursive(BASE_DIR.'resources/schemas/*'),
  49. glob_recursive(BASE_DIR.'static/*')
  50. );
  51. removeDirectory(BASE_DIR.'vendor/');
  52. $updateZip = new ZipArchive();
  53. $updateZip->open($tmpFile);
  54. for ($i = 0; $i < $updateZip->numFiles; $i++) {
  55. $nameIndex = $updateZip->getNameIndex($i);
  56. $updateZip->extractTo(BASE_DIR, $nameIndex);
  57. if (($key = array_search(rtrim(BASE_DIR.$nameIndex, '/'), $currentFiles)) !== false) {
  58. unset($currentFiles[$key]);
  59. }
  60. }
  61. foreach ($currentFiles as $extraneous) {
  62. unlink($extraneous);
  63. }
  64. $updateZip->close();
  65. unlink($tmpFile);
  66. return redirect($response, '/install');
  67. }
  68. /**
  69. * @param Request $request
  70. * @param Response $response
  71. * @return Response
  72. */
  73. public function checkForUpdates(Request $request, Response $response): Response
  74. {
  75. $jsonResponse = [
  76. 'status' => null,
  77. 'message' => null,
  78. 'upgrade' => false,
  79. ];
  80. $acceptPrerelease = $request->getParam('prerelease', 'false') === 'true';
  81. try {
  82. $json = $this->getApiJson();
  83. $jsonResponse['status'] = 'OK';
  84. foreach ($json as $release) {
  85. if (version_compare($release->tag_name, PLATFORM_VERSION, '>') && ($release->prerelease === $acceptPrerelease)) {
  86. $jsonResponse['message'] = lang('new_version_available', $release->tag_name);
  87. $jsonResponse['upgrade'] = true;
  88. break;
  89. }
  90. if (version_compare($release->tag_name, PLATFORM_VERSION, '<=')) {
  91. $jsonResponse['message'] = lang('already_latest_version');
  92. $jsonResponse['upgrade'] = false;
  93. break;
  94. }
  95. }
  96. } catch (\RuntimeException $e) {
  97. $jsonResponse['status'] = 'ERROR';
  98. $jsonResponse['message'] = $e->getMessage();
  99. }
  100. return $response->withJson($jsonResponse);
  101. }
  102. protected function getApiJson()
  103. {
  104. $opts = [
  105. 'http' => [
  106. 'method' => 'GET',
  107. 'header' => [
  108. 'User-Agent: XBackBone-App',
  109. 'Accept: application/vnd.github.v3+json',
  110. ],
  111. ],
  112. ];
  113. $data = @file_get_contents(self::GITHUB_SOURCE_API, false, stream_context_create($opts));
  114. if ($data === false) {
  115. throw new \RuntimeException('Cannot contact the Github API. Try again.');
  116. }
  117. return json_decode($data);
  118. }
  119. }