WebOrbiton
v1.0.0.1

StocketBase

72 lines · 2.3 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. require_once __DIR__ . '/includes/config.php';
  6. require_once __DIR__ . '/includes/database.php';
  7. require_once __DIR__ . '/includes/site-front.php';
  8. ​
  9. $settings = SiteFront::settings();
  10. if (($settings['pwa_enabled'] ?? '0') !== '1') {
  11. http_response_code(404);
  12. exit;
  13. }
  14. ​
  15. function manifestIconMimeType(string $path): string
  16. {
  17. $extension = strtolower((string) pathinfo($path, PATHINFO_EXTENSION));
  18. ​
  19. return match ($extension) {
  20. 'png' => 'image/png',
  21. 'jpg', 'jpeg' => 'image/jpeg',
  22. 'svg' => 'image/svg+xml',
  23. 'webp' => 'image/webp',
  24. 'ico' => 'image/x-icon',
  25. default => 'image/png',
  26. };
  27. }
  28. ​
  29. $fallbackName = trim((string) ($settings['site_name'] ?? Config::get('APP_NAME', 'StocketBase')));
  30. $fallbackName = $fallbackName !== '' ? $fallbackName : 'Website';
  31. ​
  32. $appName = trim((string) ($settings['pwa_name'] ?? ''));
  33. $appName = $appName !== '' ? $appName : $fallbackName;
  34. ​
  35. $shortName = trim((string) ($settings['pwa_short_name'] ?? ''));
  36. $shortName = $shortName !== '' ? $shortName : $appName;
  37. ​
  38. $description = trim((string) ($settings['pwa_description'] ?? ''));
  39. $description = $description !== '' ? $description : trim((string) ($settings['site_description'] ?? ''));
  40. ​
  41. $icon = trim((string) ($settings['pwa_icon_url'] ?? ''));
  42. $icon = $icon !== '' ? $icon : trim((string) ($settings['site_logo_url'] ?? ''));
  43. ​
  44. $themeColor = trim((string) ($settings['pwa_theme_color'] ?? '')) ?: '#ffffff';
  45. $backgroundColor = trim((string) ($settings['pwa_background_color'] ?? '')) ?: '#ffffff';
  46. ​
  47. $basePath = rtrim(Config::get('APP_BASE_PATH', ''), '/') . '/';
  48. ​
  49. $manifest = [
  50. 'name' => $appName,
  51. 'short_name' => $shortName,
  52. 'start_url' => $basePath,
  53. 'scope' => $basePath,
  54. 'display' => 'standalone',
  55. 'background_color' => $backgroundColor,
  56. 'theme_color' => $themeColor,
  57. 'icons' => [],
  58. ];
  59. ​
  60. if ($description !== '') {
  61. $manifest['description'] = $description;
  62. }
  63. ​
  64. if ($icon !== '') {
  65. $mimeType = manifestIconMimeType($icon);
  66. $manifest['icons'][] = ['src' => $icon, 'sizes' => '192x192', 'type' => $mimeType];
  67. $manifest['icons'][] = ['src' => $icon, 'sizes' => '512x512', 'type' => $mimeType];
  68. }
  69. ​
  70. header('Content-Type: application/manifest+json; charset=utf-8');
  71. echo json_encode($manifest, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  72. ​