UpgradeController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. try {
  17. $json = $this->getApiJson();
  18. } catch (\RuntimeException $e) {
  19. $jsonResponse['message'] = $e->getMessage();
  20. return $response->withJson($jsonResponse, 503);
  21. }
  22. if (version_compare($json[0]->tag_name, PLATFORM_VERSION, '<=')) {
  23. $this->session->alert(lang('already_latest_version'), 'warning');
  24. return redirect($response, 'system');
  25. }
  26. $tmpFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'xbackbone_update.zip';
  27. if (file_put_contents($tmpFile, file_get_contents($json[0]->assets[0]->browser_download_url)) === false) {
  28. $this->session->alert(lang('cannot_retrieve_file'), 'danger');
  29. return redirect($response, 'system');
  30. };
  31. if (filesize($tmpFile) !== $json[0]->assets[0]->size) {
  32. $this->session->alert(lang('file_size_no_match'), 'danger');
  33. return redirect($response, 'system');
  34. }
  35. $updateZip = new ZipArchive();
  36. $updateZip->open($tmpFile);
  37. for ($i = 0; $i < $updateZip->numFiles; $i++) {
  38. $nameIndex = $updateZip->getNameIndex($i);
  39. if (is_dir(BASE_DIR . $nameIndex)) {
  40. continue;
  41. }
  42. if (file_exists(BASE_DIR . $nameIndex) && md5($updateZip->getFromIndex($i)) !== md5_file(BASE_DIR . $nameIndex)) {
  43. $updateZip->extractTo(BASE_DIR, $nameIndex);
  44. } elseif (!file_exists(BASE_DIR . $nameIndex)) {
  45. $updateZip->extractTo(BASE_DIR, $nameIndex);
  46. }
  47. print_r($updateZip->getNameIndex($i) . '<br>');
  48. }
  49. $updateZip->close();
  50. unlink($tmpFile);
  51. return redirect($response, '/install');
  52. }
  53. /**
  54. * @param Request $request
  55. * @param Response $response
  56. * @return Response
  57. */
  58. public function checkForUpdates(Request $request, Response $response): Response
  59. {
  60. $jsonResponse = [
  61. 'status' => null,
  62. 'message' => null,
  63. 'upgrade' => false,
  64. ];
  65. try {
  66. $json = $this->getApiJson();
  67. $jsonResponse['status'] = 'OK';
  68. if (version_compare($json[0]->tag_name, PLATFORM_VERSION, '>')) {
  69. $jsonResponse['message'] = lang('new_version_available', $json[0]->tag_name); //"New version {$json[0]->tag_name} available!";
  70. $jsonResponse['upgrade'] = true;
  71. } else {
  72. $jsonResponse['message'] = lang('already_latest_version');//'You have already the latest version.';
  73. $jsonResponse['upgrade'] = false;
  74. }
  75. return $response->withJson($jsonResponse, 200);
  76. } catch (\RuntimeException $e) {
  77. $jsonResponse['status'] = 'ERROR';
  78. $jsonResponse['message'] = $e->getMessage();
  79. return $response->withJson($jsonResponse, 503);
  80. }
  81. }
  82. protected function getApiJson()
  83. {
  84. $opts = [
  85. 'http' => [
  86. 'method' => 'GET',
  87. 'header' => [
  88. 'User-Agent: XBackBone-App',
  89. 'Accept: application/vnd.github.v3+json',
  90. ],
  91. ],
  92. ];
  93. $data = file_get_contents(self::GITHUB_SOURCE_API, false, stream_context_create($opts));
  94. if ($data === false) {
  95. throw new \RuntimeException('Cannot contact the Github API. Try again.');
  96. }
  97. return json_decode($data);
  98. }
  99. }