v1.0.0.1
StocketBase
- <?php
-
- declare(strict_types=1);
-
- require_once __DIR__ . '/includes/config.php';
- require_once __DIR__ . '/includes/database.php';
- require_once __DIR__ . '/includes/auth.php';
-
- Auth::boot();
- Auth::requireLogin();
-
- header('Content-Type: application/json');
- header('Cache-Control: no-store');
-
- if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
- http_response_code(405);
- echo json_encode(['success' => false, 'error' => 'Something went wrong. Please refresh the page and try again.']);
- exit;
- }
-
- $type = (string) ($_GET['type'] ?? 'image') === 'video' ? 'video' : 'image';
- $folder = $type === 'video' ? 'videos' : 'img';
- $extensions = $type === 'video' ? ['mp4', 'webm'] : ['jpg', 'jpeg', 'png', 'gif', 'webp'];
- $limit = 300;
-
- $directory = __DIR__ . '/media/' . $folder;
- $items = [];
-
- if (is_dir($directory)) {
- foreach (scandir($directory) ?: [] as $name) {
- if (preg_match('/^[A-Za-z0-9._-]+$/', $name) !== 1) {
- continue;
- }
-
- $extension = strtolower(pathinfo($name, PATHINFO_EXTENSION));
- $path = $directory . '/' . $name;
- if (!in_array($extension, $extensions, true) || !is_file($path)) {
- continue;
- }
-
- $items[] = ['name' => $name, 'url' => 'media/' . $folder . '/' . $name, 'time' => (int) filemtime($path), 'size' => (int) filesize($path)];
- }
- }
-
- usort($items, static fn (array $a, array $b): int => $b['time'] <=> $a['time']);
- $total = count($items);
-
- echo json_encode(['success' => true, 'items' => array_slice($items, 0, $limit), 'total' => $total]);
-