'jpg', 'image/png' => 'png', 'image/webp' => 'webp', 'image/gif' => 'gif', 'video/mp4' => 'mp4', 'video/webm' => 'webm' ]; $MAX_MB = 50; $UPLOAD_DIR = __DIR__ . '/uploads'; $BASE_PATH = 'uploads'; if (!is_dir($UPLOAD_DIR)) { @mkdir($UPLOAD_DIR, 0775, true); } function is_logged_in() { return !empty($_SESSION['is_admin']); } function e($s) { return htmlspecialchars($s, ENT_QUOTES, 'UTF-8'); } function is_video($path) { $ext = strtolower(pathinfo($path, PATHINFO_EXTENSION)); return in_array($ext, ['mp4','webm']); } if (isset($_POST['action']) && $_POST['action'] === 'login') { $pass = $_POST['password'] ?? ''; if (hash_equals($ADMIN_PASSWORD, $pass)) { $_SESSION['is_admin'] = true; header('Location: ?admin=1'); exit; } else { $error = 'Wrong password'; } } if (isset($_GET['logout'])) { session_destroy(); header('Location: ./'); exit; } $messages = []; if (is_logged_in() && isset($_POST['action']) && $_POST['action'] === 'upload') { if (isset($_FILES['files'])) { $count = count($_FILES['files']['name']); for ($i = 0; $i < $count; $i++) { if ($_FILES['files']['error'][$i] === UPLOAD_ERR_OK) { $tmp = $_FILES['files']['tmp_name'][$i]; $size = $_FILES['files']['size'][$i]; $type = mime_content_type($tmp) ?: $_FILES['files']['type'][$i]; if ($size > $MAX_MB * 1024 * 1024) continue; if (!isset($ALLOWED_MIME[$type])) continue; $ext = $ALLOWED_MIME[$type]; $safeName = bin2hex(random_bytes(8)) . '.' . $ext; $dest = $UPLOAD_DIR . '/' . $safeName; if (move_uploaded_file($tmp, $dest)) { @touch($dest, time()); } } } } } $files = []; $dir = @opendir($UPLOAD_DIR); if ($dir) { while (($f = readdir($dir)) !== false) { if ($f === '.' || $f === '..') continue; $path = $UPLOAD_DIR . '/' . $f; if (is_file($path)) { $files[] = [ 'name' => $f, 'mtime' => @filemtime($path) ?: 0 ]; } } closedir($dir); } usort($files, fn($a,$b) => $b['mtime'] <=> $a['mtime']); $total = count($files); $page = max(1, (int)($_GET['page'] ?? 1)); $pages = max(1, (int)ceil($total / $ITEMS_PER_PAGE)); $page = min($page, $pages); $start = ($page - 1) * $ITEMS_PER_PAGE; $slice = array_slice($files, $start, $ITEMS_PER_PAGE); $INFEED_EVERY = 9; ?>