prepare("SELECT * FROM videos WHERE id = ?"); $stmt->execute([$id]); $video = $stmt->fetch(); if (!$video) { die("Video not found."); } $error = ''; $success = ''; function formatVideoUrl($url) { if (strpos($url, 'drive.google.com') !== false) { $url = preg_replace('/\/view(\?.*)?$/', '/preview', $url); if (strpos($url, '/preview') === false && strpos($url, '/file/d/') !== false) { $url = rtrim($url, '/') . '/preview'; } } return $url; } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $title = trim($_POST['title']); $description = trim($_POST['description']); $release_date = $_POST['release_date']; $source_type = $_POST['source_type']; $video_url = $video['video_url']; $thumbnail_url = $video['thumbnail_url']; // Handle Thumbnail Upload if (isset($_FILES['thumbnail_file']) && $_FILES['thumbnail_file']['error'] === 0) { $t_ext = strtolower(pathinfo($_FILES['thumbnail_file']['name'], PATHINFO_EXTENSION)); $allowed_img = ['jpg', 'jpeg', 'png', 'webp']; if (in_array($t_ext, $allowed_img)) { $t_filename = 'thumb_' . uniqid() . '.' . $t_ext; if (move_uploaded_file($_FILES['thumbnail_file']['tmp_name'], '../uploads/' . $t_filename)) { if ($video['thumbnail_url'] && strpos($video['thumbnail_url'], 'uploads/') === 0) { @unlink('../' . $video['thumbnail_url']); } $thumbnail_url = 'uploads/' . $t_filename; } } else { $error = "Invalid thumbnail format."; } } if (!$error && $source_type === 'upload' && isset($_FILES['video_file']) && $_FILES['video_file']['error'] === 0) { $ext = strtolower(pathinfo($_FILES['video_file']['name'], PATHINFO_EXTENSION)); $allowed_vid = ['mp4', 'webm', 'ogg']; if (in_array($ext, $allowed_vid)) { $filename = uniqid() . '.' . $ext; $upload_path = '../uploads/' . $filename; if (move_uploaded_file($_FILES['video_file']['tmp_name'], $upload_path)) { if ($video['source_type'] === 'upload') @unlink('../' . $video['video_url']); $video_url = 'uploads/' . $filename; } } else { $error = "Invalid video format."; } } elseif (!$error && $source_type === 'link') { $video_url = formatVideoUrl(trim($_POST['external_url'])); } $stmt = $pdo->prepare("UPDATE videos SET title = ?, description = ?, release_date = ?, source_type = ?, video_url = ?, thumbnail_url = ? WHERE id = ?"); if ($stmt->execute([$title, $description, $release_date, $source_type, $video_url, $thumbnail_url, $id])) { // Update tags $pdo->prepare("DELETE FROM video_tags WHERE video_id = ?")->execute([$id]); if (!empty($_POST['tags'])) { $tags = explode(',', $_POST['tags']); foreach ($tags as $tag_name) { $tag_name = trim($tag_name); if (!$tag_name) continue; $pdo->prepare("INSERT IGNORE INTO tags (name) VALUES (?)")->execute([$tag_name]); $tag_stmt = $pdo->prepare("SELECT id FROM tags WHERE name = ?"); $tag_stmt->execute([$tag_name]); $tag_id = $tag_stmt->fetchColumn(); $pdo->prepare("INSERT IGNORE INTO video_tags (video_id, tag_id) VALUES (?, ?)")->execute([$id, $tag_id]); } } cleanupTags(); $success = "Video updated successfully!"; // Refresh video data $stmt = $pdo->prepare("SELECT * FROM videos WHERE id = ?"); $stmt->execute([$id]); $video = $stmt->fetch(); } else { $error = "Update failed."; } } // Get current tags $stmt = $pdo->prepare("SELECT t.name FROM tags t JOIN video_tags vt ON t.id = vt.tag_id WHERE vt.video_id = ?"); $stmt->execute([$id]); $current_tags = $stmt->fetchAll(PDO::FETCH_COLUMN); $tags_str = implode(', ', $current_tags); ob_start(); require_once '../includes/header.php'; $header = ob_get_clean(); echo str_replace(['assets/', 'index.php', 'login.php', 'logout.php', 'admin/'], ['../assets/', '../index.php', '../login.php', '../logout.php', './'], $header); ?>