diff --git a/admin/dashboard/main.php b/admin/dashboard/main.php index 13a6664..1e8b00a 100644 --- a/admin/dashboard/main.php +++ b/admin/dashboard/main.php @@ -146,4 +146,63 @@ function time_elapsed_string($datetime, $full = false) { if (!$full) $string = array_slice($string, 0, 1); return $string ? implode(', ', $string) . ' ago' : 'just now'; } + +const GITHUB_SOURCE_API = 'https://api.github.com/repos/Supernova3339/anonfiles/releases'; + +public function checkForUpdates(Request $request, Response $response): Response + { + $jsonResponse = [ + 'status' => 'OK', + 'message' => 'Already Latest Version'), + 'upgrade' => false, + ]; + + $acceptPrerelease = param($request, 'prerelease', 'false') === 'true'; + + try { + $json = $this->getApiJson(); + + foreach ($json as $release) { + if ( + $release->prerelease === $acceptPrerelease && + version_compare($release->tag_name, PLATFORM_VERSION, '>') && + version_compare($release->tag_name, 'prerelease', '<') + ) { + $jsonResponse['message'] = 'New version available!', [$release->tag_name]); + $jsonResponse['upgrade'] = true; + break; + } + + if (version_compare($release->tag_name, PLATFORM_VERSION, '<=')) { + break; + } + } + } catch (RuntimeException $e) { + $jsonResponse['status'] = 'ERROR'; + $jsonResponse['message'] = $e->getMessage(); + } + + return json($response, $jsonResponse); + } + + protected function getApiJson() + { + $opts = [ + 'http' => [ + 'method' => 'GET', + 'header' => [ + 'User-Agent: AnonFiles-App', + 'Accept: application/vnd.github.v3+json', + ], + ], + ]; + + $data = @file_get_contents(self::GITHUB_SOURCE_API, false, stream_context_create($opts)); + + if ($data === false) { + throw new RuntimeException('Cannot contact the Github API. Try again.'); + } + + return json_decode($data); + } ?>