package.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. declare(strict_types=1);
  3. require_once __DIR__ . "/includes/update-server.php";
  4. $baseDir = __DIR__;
  5. $manifestFile = $baseDir . "/manifest.json";
  6. try {
  7. $version = trim((string) ($_GET["version"] ?? ""));
  8. if (!updateServerVersionIsValid($version)) {
  9. updateServerTextResponse(400, "Invalid version.");
  10. }
  11. $manifest = updateServerReadManifest($manifestFile);
  12. $release = updateServerReleaseByVersion($manifest, $version);
  13. $packagePath = updateServerPackagePath($baseDir, $release["package"]);
  14. if (strtolower(pathinfo($packagePath, PATHINFO_EXTENSION)) !== "zip") {
  15. throw new RuntimeException("Release package is not a ZIP file.");
  16. }
  17. $fileName = basename($packagePath);
  18. $fileSize = filesize($packagePath);
  19. $handle = fopen($packagePath, "rb");
  20. if ($fileSize === false || $handle === false) {
  21. throw new RuntimeException("Release package cannot be opened.");
  22. }
  23. header("Content-Type: application/zip");
  24. header("Content-Disposition: attachment; filename=\"" . addcslashes($fileName, "\"\\") . "\"");
  25. header("Content-Length: " . (string) $fileSize);
  26. header("Cache-Control: public, max-age=300");
  27. header("X-Content-Type-Options: nosniff");
  28. fpassthru($handle);
  29. fclose($handle);
  30. } catch (Throwable $exception) {
  31. $message = $exception->getMessage();
  32. $status = in_array($message, [
  33. "Release is not configured.",
  34. "Release package is missing.",
  35. ], true) ? 404 : 500;
  36. updateServerTextResponse($status, $message);
  37. }