= 1024 && $i < count($units) - 1) { $bytes /= 1024; $i++; } return round($bytes, 2) . ' ' . $units[$i]; } function formatDateTime(string $datetime): string { try { $ts = strtotime($datetime); if ($ts === false) return $datetime; return date('d.m.Y H:i', $ts); } catch (\Exception $e) { return $datetime; } } function encodePath(string $path): string { return rawurlencode($path); } function getRelativePath(string $fullPath): string { $entry = ENTRY_POINT; if ($fullPath === $entry) return '/'; if (strpos($fullPath, $entry . '/') === 0) { $rel = substr($fullPath, strlen($entry) + 1); return $rel === '' ? '/' : '/' . $rel; } return $fullPath; } function getBreadcrumbs(string $path): array { $entry = ENTRY_POINT; $parts = []; if ($path === $entry) { return [['label' => '/', 'path' => $entry]]; } // Root segment $parts[] = ['label' => '/', 'path' => $entry]; $rel = substr($path, strlen($entry) + 1); $segments = explode('/', $rel); $current = $entry; foreach ($segments as $segment) { if ($segment === '') continue; $current .= '/' . $segment; $parts[] = ['label' => $segment, 'path' => $current]; } return $parts; } // ── API: Directory Listing ───────────────────────────────────────────────── function handleList(): void { $rawPath = $_GET['path'] ?? ''; $currentPath = $rawPath === '' ? ENTRY_POINT : ENTRY_POINT . '/' . urldecode($rawPath); // Resolve to absolute path inside ENTRY_POINT $currentPath = realpath($currentPath); if ($currentPath === false || strpos($currentPath, ENTRY_POINT) !== 0) { http_response_code(404); echo json_encode(['error' => 'Directory not found']); return; } $items = []; if ($dh = opendir($currentPath)) { while (($entry = readdir($dh)) !== false) { if ($entry === '.' || $entry === '..') continue; $fullPath = $currentPath . '/' . $entry; $stat = stat($fullPath); $items[] = [ 'name' => $entry, 'type' => is_dir($fullPath) ? 'dir' : 'file', 'size' => is_dir($fullPath) ? null : ($stat !== false ? $stat['size'] : 0), 'modified' => $stat !== false ? date('Y-m-d H:i:s', $stat['mtime']) : '', 'path' => $fullPath, ]; } closedir($dh); } echo json_encode($items); } // ── API: File Download ───────────────────────────────────────────────────── function handleDownload(): void { $rawFile = $_GET['file'] ?? ''; $filePath = ENTRY_POINT . '/' . urldecode($rawFile); if (!is_file($filePath)) { http_response_code(404); echo 'File not found'; return; } header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filePath) . '"'); header('Content-Length: ' . filesize($filePath)); header('Cache-Control: no-cache'); header('Pragma: no-cache'); readfile($filePath); exit; } // ── MIME-Type Mapping ──────────────────────────────────────────── function getMimeType(string $filePath): string { // Try finfo first (most accurate) if (function_exists('finfo_open')) { $finfo = finfo_open(FILEINFO_MIME_TYPE); $mime = finfo_file($finfo, $filePath); finfo_close($finfo); if ($mime && $mime !== 'application/octet-stream') { return $mime; } } // Fallback: extension-based mapping $ext = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); $mimes = [ // Images 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif', 'webp' => 'image/webp', 'svg' => 'image/svg+xml', 'bmp' => 'image/bmp', 'ico' => 'image/x-icon', 'tiff' => 'image/tiff', 'tif' => 'image/tiff', 'avif' => 'image/avif', // Audio 'mp3' => 'audio/mpeg', 'ogg' => 'audio/ogg', 'wav' => 'audio/wav', 'flac' => 'audio/flac', 'aac' => 'audio/aac', 'wma' => 'audio/x-ms-wma', 'opus' => 'audio/opus', 'm4a' => 'audio/mp4', // Video 'mp4' => 'video/mp4', 'webm' => 'video/webm', 'ogg' => 'video/ogg', 'mov' => 'video/quicktime', 'avi' => 'video/x-msvideo', 'mkv' => 'video/x-matroska', 'm4v' => 'video/x-m4v', ]; return $mimes[$ext] ?? 'application/octet-stream'; } // ── API: File Stream (for media playback) ────────────────────────── function handleStream(): void { $rawFile = $_GET['file'] ?? ''; $filePath = ENTRY_POINT . '/' . urldecode($rawFile); if (!is_file($filePath)) { http_response_code(404); echo 'File not found'; return; } $mime = getMimeType($filePath); $size = filesize($filePath); // Handle Range requests (seeking support) $rangeHeader = $_SERVER['HTTP_RANGE'] ?? ''; if (!empty($rangeHeader)) { // Parse Range header (format: bytes=start-end) if (preg_match('/bytes=(\d+)-(\d+)?/', $rangeHeader, $matches)) { $start = (int)$matches[1]; $end = isset($matches[2]) && $matches[2] !== '' ? (int)$matches[2] : $size - 1; $length = $end - $start + 1; header('HTTP/1.1 206 Partial Content'); header('Content-Range: bytes ' . $start . '-' . $end . '/' . $size); header('Accept-Ranges: bytes'); header('Content-Length: ' . $length); header('Content-Type: ' . $mime); header('Cache-Control: public, max-age=3600'); $fp = fopen($filePath, 'rb'); fseek($fp, $start); $remaining = $length; while ($remaining > 0 && !feof($fp)) { $chunk = min(8192, $remaining); echo fread($fp, $chunk); $remaining -= $chunk; } fclose($fp); exit; } } // Full file response header('HTTP/1.1 200 OK'); header('Content-Type: ' . $mime); header('Content-Length: ' . $size); header('Accept-Ranges: bytes'); header('Cache-Control: public, max-age=3600'); readfile($filePath); exit; } // ── API Router ───────────────────────────────────────────────────── if (isset($_GET['action'])) { header('Content-Type: application/json'); if ($_GET['action'] === 'list') { handleList(); exit; } if ($_GET['action'] === 'download') { header('Content-Type: application/octet-stream'); handleDownload(); exit; } if ($_GET['action'] === 'stream') { handleStream(); exit; } } // ── HTML Output ──────────────────────────────────────────────────────────── ?>
Lade Dateien…