UpgradeController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. $currentFiles = array_merge(
  40. glob_recursive(BASE_DIR . 'app/*'),
  41. glob_recursive(BASE_DIR . 'bin/*'),
  42. glob_recursive(BASE_DIR . 'bootstrap/*'),
  43. glob_recursive(BASE_DIR . 'resources/templates/*'),
  44. glob_recursive(BASE_DIR . 'resources/lang/*'),
  45. glob_recursive(BASE_DIR . 'resources/schemas/*'),
  46. glob_recursive(BASE_DIR . 'static/*')
  47. );
  48. removeDirectory(BASE_DIR . 'vendor/');
  49. $updateZip = new ZipArchive();
  50. $updateZip->open($tmpFile);
  51. for ($i = 0; $i < $updateZip->numFiles; $i++) {
  52. $nameIndex = $updateZip->getNameIndex($i);
  53. $updateZip->extractTo(BASE_DIR, $nameIndex);
  54. if (($key = array_search(rtrim(BASE_DIR . $nameIndex, '/'), $currentFiles)) !== false) {
  55. unset($currentFiles[$key]);
  56. }
  57. }
  58. foreach ($currentFiles as $extraneous) {
  59. unlink($extraneous);
  60. }
  61. $updateZip->close();
  62. unlink($tmpFile);
  63. return redirect($response, '/install');
  64. }
  65. /**
  66. * @param Request $request
  67. * @param Response $response
  68. * @return Response
  69. */
  70. public function checkForUpdates(Request $request, Response $response): Response
  71. {
  72. $jsonResponse = [
  73. 'status' => null,
  74. 'message' => null,
  75. 'upgrade' => false,
  76. ];
  77. try {
  78. $json = $this->getApiJson();
  79. $jsonResponse['status'] = 'OK';
  80. if (version_compare($json[0]->tag_name, PLATFORM_VERSION, '>')) {
  81. $jsonResponse['message'] = lang('new_version_available', $json[0]->tag_name);
  82. $jsonResponse['upgrade'] = true;
  83. } else {
  84. $jsonResponse['message'] = lang('already_latest_version');
  85. $jsonResponse['upgrade'] = false;
  86. }
  87. return $response->withJson($jsonResponse, 200);
  88. } catch (\RuntimeException $e) {
  89. $jsonResponse['status'] = 'ERROR';
  90. $jsonResponse['message'] = $e->getMessage();
  91. return $response->withJson($jsonResponse, 503);
  92. }
  93. }
  94. protected function getApiJson()
  95. {
  96. $opts = [
  97. 'http' => [
  98. 'method' => 'GET',
  99. 'header' => [
  100. 'User-Agent: XBackBone-App',
  101. 'Accept: application/vnd.github.v3+json',
  102. ],
  103. ],
  104. ];
  105. $data = file_get_contents(self::GITHUB_SOURCE_API, false, stream_context_create($opts));
  106. if ($data === false) {
  107. throw new \RuntimeException('Cannot contact the Github API. Try again.');
  108. }
  109. return json_decode($data);
  110. }
  111. }