WebOrbiton
v1.0.0.1

StocketBase

49 lines · 1.5 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/auth.php';
  8. ​
  9. Auth::boot();
  10. Auth::requireLogin();
  11. ​
  12. header('Content-Type: application/json');
  13. header('Cache-Control: no-store');
  14. ​
  15. if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
  16. http_response_code(405);
  17. echo json_encode(['success' => false, 'error' => 'Something went wrong. Please refresh the page and try again.']);
  18. exit;
  19. }
  20. ​
  21. $type = (string) ($_GET['type'] ?? 'image') === 'video' ? 'video' : 'image';
  22. $folder = $type === 'video' ? 'videos' : 'img';
  23. $extensions = $type === 'video' ? ['mp4', 'webm'] : ['jpg', 'jpeg', 'png', 'gif', 'webp'];
  24. $limit = 300;
  25. ​
  26. $directory = __DIR__ . '/media/' . $folder;
  27. $items = [];
  28. ​
  29. if (is_dir($directory)) {
  30. foreach (scandir($directory) ?: [] as $name) {
  31. if (preg_match('/^[A-Za-z0-9._-]+$/', $name) !== 1) {
  32. continue;
  33. }
  34. ​
  35. $extension = strtolower(pathinfo($name, PATHINFO_EXTENSION));
  36. $path = $directory . '/' . $name;
  37. if (!in_array($extension, $extensions, true) || !is_file($path)) {
  38. continue;
  39. }
  40. ​
  41. $items[] = ['name' => $name, 'url' => 'media/' . $folder . '/' . $name, 'time' => (int) filemtime($path), 'size' => (int) filesize($path)];
  42. }
  43. }
  44. ​
  45. usort($items, static fn (array $a, array $b): int => $b['time'] <=> $a['time']);
  46. $total = count($items);
  47. ​
  48. echo json_encode(['success' => true, 'items' => array_slice($items, 0, $limit), 'total' => $total]);
  49. ​