37018-vm/submit.php
2025-12-18 01:59:13 +00:00

187 lines
8.3 KiB
PHP

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
require_once __DIR__ . '/db/config.php';
require_once __DIR__ . '/includes/ImageProcessor.php';
error_log("submit.php: Script started.");
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit;
}
$pdo = db();
$errors = [];
$success = false;
// Fetch categories and subcategories for the form
$categories = $pdo->query("SELECT * FROM categories WHERE visibility = 1 ORDER BY display_order ASC, name ASC")->fetchAll();
$subcategories = [];
if (!empty($categories)) {
$stmt = $pdo->query("SELECT * FROM subcategories ORDER BY name ASC");
while ($row = $stmt->fetch()) {
$subcategories[$row['category_id']][] = $row;
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
error_log("submit.php: POST request received.");
$title = trim($_POST['title'] ?? '');
$url = trim($_POST['url'] ?? '');
$description = trim($_POST['description'] ?? '');
$subcategory_id = $_POST['subcategory_id'] ?? null;
if (empty($title)) $errors[] = 'Title is required.';
if (empty($url)) $errors[] = 'URL is required.';
if (!filter_var($url, FILTER_VALIDATE_URL)) $errors[] = 'Invalid URL.';
if (empty($subcategory_id)) $errors[] = 'Subcategory is required.';
if (empty($errors)) {
// Determine status based on user role
$status = ($_SESSION['user_role'] === 'admin' || $_SESSION['user_role'] === 'power_user') ? 'approved' : 'pending';
// For now, thumbnail is not implemented
$thumbnail_url = null;
if (isset($_FILES['thumbnail']) && $_FILES['thumbnail']['error'] === UPLOAD_ERR_OK) {
error_log("submit.php: Image upload detected. Processing image...");
$uploadedImagePath = ImageProcessor::processAndSaveImage($_FILES['thumbnail']);
if ($uploadedImagePath) {
$thumbnail_url = $uploadedImagePath;
error_log("submit.php: Image processed successfully. Path: " . $uploadedImagePath);
} else {
$errors[] = 'Failed to process uploaded image. Please ensure it is a valid image file (JPEG, PNG, GIF).';
error_log("submit.php: Failed to process uploaded image.");
}
}
try {
error_log("submit.php: Attempting database insertion...");
$stmt = $pdo->prepare("INSERT INTO links (user_id, subcategory_id, title, url, description, thumbnail_url, status) VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt->execute([$_SESSION['user_id'], $subcategory_id, $title, $url, $description, $thumbnail_url, $status]);
$success = true;
error_log("submit.php: Database insertion successful.");
} catch (PDOException $e) {
$errors[] = "Database error: " . $e->getMessage();
error_log("submit.php: Database error: " . $e->getMessage());
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Submit a Link - <?php echo htmlspecialchars($_SERVER['PROJECT_NAME'] ?? 'Web Directory'); ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<header class="header">
<h1><a href="/" style="text-decoration: none; color: inherit;"><?php echo htmlspecialchars($_SERVER['PROJECT_NAME'] ?? 'Web Directory'); ?></a></h1>
<div class="auth-links">
<?php if (isset($_SESSION['user_id'])): ?>
<a href="submit.php">Submit Link</a>
<a href="logout.php">Logout</a>
<?php else: ?>
<a href="register.php">Register</a>
<a href="login.php">Login</a>
<?php endif; ?>
</div>
</header>
<div class="container my-4">
<div class="row justify-content-center">
<div class="col-md-8">
<main class="content p-4">
<h2>Submit a New Link</h2>
<?php if (!empty($errors)): ?>
<div class="alert alert-danger">
<?php foreach ($errors as $error): ?><p class="mb-0"><?php echo $error; ?></p><?php endforeach; ?>
</div>
<?php endif; ?>
<?php if ($success): ?>
<div class="alert alert-success">
<p class="mb-0">Thank you for your submission! It will be reviewed shortly.</p>
</div>
<?php else: ?>
<form action="submit.php" method="POST" enctype="multipart/form-data">
<div class="mb-3">
<label for="title" class="form-label">Title</label>
<input type="text" class="form-control" id="title" name="title" required>
</div>
<div class="mb-3">
<label for="url" class="form-label">URL</label>
<input type="url" class="form-control" id="url" name="url" required>
</div>
<div class="mb-3">
<label for="category" class="form-label">Category</label>
<select class="form-select" id="category" name="category">
<option selected disabled>-- Select a Category --</option>
<?php foreach($categories as $cat): ?>
<option value="<?php echo $cat['id']; ?>"><?php echo htmlspecialchars($cat['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label for="subcategory_id" class="form-label">Subcategory</label>
<select class="form-select" id="subcategory_id" name="subcategory_id" required>
<option selected disabled>-- Select a Subcategory --</option>
<?php foreach($subcategories as $cat_id => $subs): ?>
<?php foreach($subs as $sub): ?>
<option class="d-none" data-category="<?php echo $cat_id; ?>" value="<?php echo $sub['id']; ?>"><?php echo htmlspecialchars($sub['name']); ?></option>
<?php endforeach; ?>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label for="description" class="form-label">Description</label>
<textarea class="form-control" id="description" name="description" rows="3"></textarea>
</div>
<div class="mb-3">
<label for="thumbnail" class="form-label">Image (optional)</label>
<input type="file" class="form-control" id="thumbnail" name="thumbnail" accept="image/*">
<button type="submit" class="btn btn-primary">Submit Link</button>
</form>
<?php endif; ?>
</main>
</div>
</div>
</div>
<footer class="footer">
<p>&copy; <?php echo date("Y"); ?> <?php echo htmlspecialchars($_SERVER['PROJECT_NAME'] ?? 'Web Directory'); ?>. All Rights Reserved.</p>
</footer>
<script>
document.getElementById('category').addEventListener('change', function() {
const categoryId = this.value;
const subcategorySelect = document.getElementById('subcategory_id');
// Reset and show the default option
subcategorySelect.value = '-- Select a Subcategory --';
// Hide all subcategory options
Array.from(subcategorySelect.options).forEach(opt => {
if (opt.dataset.category) { // Skip the default disabled option
opt.classList.add('d-none');
}
});
// Show subcategories for the selected category
const relevantOptions = subcategorySelect.querySelectorAll(`[data-category="${categoryId}"]`);
relevantOptions.forEach(opt => opt.classList.remove('d-none'));
});
</script>
</body>
</html>