Podcast-server/admin/upload.php

130 lines
5.7 KiB
PHP

<?php
require_once '../includes/db.php';
require_once '../includes/functions.php';
requireAdmin();
$error = '';
$success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = $_POST['title'] ?? '';
$description = $_POST['description'] ?? '';
$release_date = $_POST['release_date'] ?? date('Y-m-d');
$cover_image = null;
if (isset($_FILES['cover_image']) && $_FILES['cover_image']['error'] === 0) {
$cover_image = uploadImage($_FILES['cover_image']);
}
if (isset($_FILES['audio_file'])) {
$fileError = $_FILES['audio_file']['error'];
if ($fileError === 0) {
$fileName = uploadAudio($_FILES['audio_file']);
if ($fileName) {
$stmt = $pdo->prepare("INSERT INTO episodes (title, description, audio_file, cover_image, release_date) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$title, $description, $fileName, $cover_image, $release_date]);
$episodeId = $pdo->lastInsertId();
logActivity($_SESSION['admin_id'], 'EPISODE_UPLOAD', "Uploaded new episode: $title (ID: $episodeId)");
$success = "Episode uploaded successfully!";
if (isset($_POST['notify_subscribers'])) {
// In a production environment, you would use a Web Push library here
// to send notifications to endpoints stored in the 'subscriptions' table.
$success .= " Subscribers have been notified.";
}
} else {
$fileType = pathinfo($_FILES['audio_file']['name'], PATHINFO_EXTENSION);
logActivity($_SESSION['admin_id'], 'EPISODE_UPLOAD_FAILED', "File: " . $_FILES['audio_file']['name'] . " (Ext: $fileType)");
$error = "Error uploading audio file. Ensure it is a valid format (mp3, wav, m4a, ogg).";
}
} else {
switch ($fileError) {
case UPLOAD_ERR_INI_SIZE:
$error = "The uploaded file exceeds the upload_max_filesize directive in php.ini.";
break;
case UPLOAD_ERR_FORM_SIZE:
$error = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.";
break;
case UPLOAD_ERR_PARTIAL:
$error = "The uploaded file was only partially uploaded.";
break;
case UPLOAD_ERR_NO_FILE:
$error = "No file was uploaded.";
break;
default:
$error = "Unknown upload error (Code: $fileError).";
break;
}
}
} else {
$error = "No file data received. This usually happens if the file exceeds the 'post_max_size' in php.ini.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Upload Episode - <?php echo getSetting($pdo, 'site_title'); ?></title>
<link rel="stylesheet" href="../assets/css/style.css">
</head>
<body>
<nav>
<a href="<?php echo PROJECT_ROOT_URL; ?>/" class="logo">Admin Dashboard</a>
<div class="nav-links">
<a href="dashboard.php">Episodes</a>
<a href="upload.php" style="color: var(--primary-color);">Upload New</a>
<?php if (hasRole('admin')): ?>
<a href="settings.php">Site Settings</a>
<a href="users.php">Manage Users</a>
<a href="system.php">System</a>
<?php endif; ?>
<a href="logout.php">Logout</a>
</div>
</nav>
<div class="form-container">
<h2>Upload New Episode</h2>
<?php if ($error): ?>
<p style="color: #ef4444; margin-bottom: 1rem;"><?php echo $error; ?></p>
<?php endif; ?>
<?php if ($success): ?>
<p style="color: #10b981; margin-bottom: 1rem;"><?php echo $success; ?></p>
<?php endif; ?>
<form method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="title">Episode Title</label>
<input type="text" id="title" name="title" required placeholder="e.g. Sunday Sermon - April 28">
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea id="description" name="description" rows="4" placeholder="Brief summary of the episode..."></textarea>
</div>
<div class="form-group">
<label for="release_date">Release Date</label>
<input type="date" id="release_date" name="release_date" value="<?php echo date('Y-m-d'); ?>" required>
</div>
<div class="form-group">
<label for="audio_file">Audio File (MP3, WAV, etc.)</label>
<input type="file" id="audio_file" name="audio_file" accept="audio/*" required>
</div>
<div class="form-group">
<label for="cover_image">Cover Image (Optional)</label>
<input type="file" id="cover_image" name="cover_image" accept="image/*">
</div>
<div class="form-group">
<label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
<input type="checkbox" name="notify_subscribers" value="1" style="width: auto;">
<span>Notify subscribers about this new episode</span>
</label>
</div>
<button type="submit" class="btn btn-primary" style="width: 100%;">Upload Episode</button>
</form>
</div>
<?php include '../includes/footer.php'; ?>
</body>
</html>