36439-vm/video.php
2025-11-28 19:14:48 +00:00

112 lines
5.0 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
// Get video ID from URL, default to 1 if not set
$video_id = isset($_GET['id']) ? (int)$_GET['id'] : 1;
// Helper function to process video URLs
function getEmbedHtml($url) {
if (empty($url)) {
return '<div class="video-player-placeholder bg-dark text-white d-flex justify-content-center align-items-center"><i class="material-icons" style="font-size: 64px;">videocam_off</i><p>No video URL provided.</p></div>';
}
$youtube_id = '';
if (preg_match('/youtube\.com\/watch\?v=([a-zA-Z0-9_\-]+)/', $url, $matches)) {
$youtube_id = $matches[1];
} elseif (preg_match('/youtu\.be\/([a-zA-Z0-9_\-]+)/', $url, $matches)) {
$youtube_id = $matches[1];
} elseif (preg_match('/youtube\.com\/embed\/([a-zA-Z0-9_\-]+)/', $url, $matches)) {
$youtube_id = $matches[1];
}
if ($youtube_id) {
$embed_url = 'https://www.youtube.com/embed/' . $youtube_id;
return '<div class="ratio ratio-16x9"><iframe src="' . $embed_url . '" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>';
}
// Fallback for other video URLs
return '<div class="ratio ratio-16x9"><video controls autoplay muted src="' . htmlspecialchars($url) . '" style="width:100%; height:100%;">Your browser does not support the video tag.</video></div>';
}
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT * FROM videos WHERE id = ? AND status = 'approved'");
$stmt->execute([$video_id]);
$video = $stmt->fetch();
} catch (PDOException $e) {
die("Database error: " . $e->getMessage());
}
// If no video is found, display a simple message
if (!$video) {
http_response_code(404);
// You can create a nicer 404 page later
$page_title = "Video Not Found";
include 'includes/header.php'; // Assuming you create a header file
echo "<div class='container text-center'><p>Sorry, the requested video was not found or is not currently available.</p></div>";
include 'includes/footer.php'; // Assuming you create a footer file
exit;
}
$embed_html = getEmbedHtml($video['video_url'] ?? '');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo htmlspecialchars($video['title']); ?> | MyTube</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<div class="container mt-4">
<div class="row">
<div class="col-lg-8">
<!-- Video Player -->
<div class="video-player mb-3">
<?php echo $embed_html; ?>
</div>
<!-- Video Info -->
<h1 class="video-title h3"><?php echo htmlspecialchars($video['title']); ?></h1>
<div class="d-flex justify-content-between align-items-center text-muted small mb-2">
<div class="video-stats">
<span><?php echo htmlspecialchars($video['views']); ?> views</span>
<span>&bull;</span>
<span><?php echo date("M j, Y", strtotime($video['upload_date'])); ?></span>
</div>
</div>
<!-- Creator Info -->
<div class="d-flex align-items-center border-top pt-3 mt-3">
<img src="<?php echo htmlspecialchars($video['creator_avatar'] ?? 'https://placehold.co/48x48/ced4da/212529?text='.strtoupper(substr($video['creator'], 0, 1))); ?>" alt="Creator Avatar" class="rounded-circle me-3" width="48" height="48">
<div class="flex-grow-1">
<div class="fw-bold"><?php echo htmlspecialchars($video['creator']); ?></div>
</div>
<button class="btn btn-danger">Subscribe</button>
</div>
<!-- Description -->
<div class="description-box mt-3 p-3 bg-light rounded">
<p class="fw-bold">Description</p>
<p><?php echo nl2br(htmlspecialchars($video['description'])); ?></p>
</div>
</div>
<div class="col-lg-4">
<h4 class="h5 mb-3">Up Next</h4>
<!-- Placeholder for related videos -->
<p class="text-muted">Related videos will be shown here.</p>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
</body>
</html>