📁 File Manager
Editing:
/home/wvl7mof7402e/public_html/int/admin/gallery.php
<?php if (session_status() === PHP_SESSION_NONE) { session_start(); } /* ========================================= ADMIN AUTHENTICATION ========================================= */ if (empty($_SESSION['ADMIN_ID'])) { header("Location: login.php"); exit; } /* ========================================= DATABASE CONNECTION Path: int/includes/db.php ========================================= */ $dbFile = __DIR__ . '/includes/db.php'; if (!file_exists($dbFile)) { die("Database file not found: includes/db.php"); } require_once $dbFile; if (!isset($con) || !($con instanceof mysqli)) { die("Database connection variable \$con not found in includes/db.php"); } mysqli_set_charset($con, "utf8mb4"); /* ========================================= CREATE GALLERY TABLE AUTOMATICALLY ========================================= */ $createTable = " CREATE TABLE IF NOT EXISTS gallery ( id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) NOT NULL, description TEXT DEFAULT NULL, image VARCHAR(255) NOT NULL, status ENUM('active','inactive') DEFAULT 'active', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 "; if (!mysqli_query($con, $createTable)) { die("Gallery table error: " . e(mysqli_error($con))); } /* ========================================= HELPERS ========================================= */ function e($value) { return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8'); } function redirectGallery() { header("Location: gallery.php"); exit; } if (empty($_SESSION['gallery_csrf'])) { $_SESSION['gallery_csrf'] = bin2hex(random_bytes(32)); } $csrf = $_SESSION['gallery_csrf']; $uploadDir = __DIR__ . '/../uploads/gallery/'; $uploadUrl = '../uploads/gallery/'; if (!is_dir($uploadDir)) { if (!mkdir($uploadDir, 0755, true) && !is_dir($uploadDir)) { die("Unable to create gallery upload directory."); } } $success = ''; $error = ''; /* ========================================= IMAGE UPLOAD FUNCTION ========================================= */ function uploadGalleryImage($file, $uploadDir) { if (!isset($file) || $file['error'] === UPLOAD_ERR_NO_FILE) { return [ 'success' => false, 'message' => 'Please select an image.' ]; } if ($file['error'] !== UPLOAD_ERR_OK) { return [ 'success' => false, 'message' => 'Image upload failed.' ]; } if ($file['size'] > 5 * 1024 * 1024) { return [ 'success' => false, 'message' => 'Maximum image size is 5MB.' ]; } $imageInfo = @getimagesize($file['tmp_name']); if ($imageInfo === false) { return [ 'success' => false, 'message' => 'Invalid image file.' ]; } $mime = $imageInfo['mime']; $allowed = [ 'image/jpeg' => 'jpg', 'image/png' => 'png', 'image/webp' => 'webp' ]; if (!isset($allowed[$mime])) { return [ 'success' => false, 'message' => 'Only JPG, PNG and WEBP images are allowed.' ]; } $extension = $allowed[$mime]; $fileName = 'gallery_' . date('YmdHis') . '_' . bin2hex(random_bytes(5)) . '.' . $extension; $destination = $uploadDir . $fileName; if (!move_uploaded_file($file['tmp_name'], $destination)) { return [ 'success' => false, 'message' => 'Unable to save uploaded image.' ]; } return [ 'success' => true, 'filename' => $fileName ]; } /* ========================================= DELETE OLD IMAGE FILE ========================================= */ function deleteGalleryImageFile($filename, $uploadDir) { $filename = basename((string)$filename); if ($filename === '') { return; } $path = $uploadDir . $filename; if (is_file($path)) { unlink($path); } } /* ========================================= POST ACTIONS ========================================= */ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $postedToken = $_POST['csrf_token'] ?? ''; if (!hash_equals($csrf, $postedToken)) { $error = "Invalid security token. Please refresh the page."; } else { $action = $_POST['action'] ?? ''; /* ================================= ADD GALLERY ================================= */ if ($action === 'add') { $title = trim($_POST['title'] ?? ''); $description = trim($_POST['description'] ?? ''); $status = $_POST['status'] ?? 'active'; if ($title === '') { $error = "Please enter gallery title."; } elseif (!in_array($status, ['active', 'inactive'], true)) { $error = "Invalid status selected."; } else { $upload = uploadGalleryImage( $_FILES['image'] ?? null, $uploadDir ); if (!$upload['success']) { $error = $upload['message']; } else { $image = $upload['filename']; $stmt = mysqli_prepare( $con, "INSERT INTO gallery (title, description, image, status) VALUES (?, ?, ?, ?)" ); if ($stmt) { mysqli_stmt_bind_param( $stmt, "ssss", $title, $description, $image, $status ); if (mysqli_stmt_execute($stmt)) { mysqli_stmt_close($stmt); $_SESSION['gallery_message'] = "Gallery image added successfully!"; redirectGallery(); } else { $error = "Unable to save gallery record."; deleteGalleryImageFile($image, $uploadDir); } mysqli_stmt_close($stmt); } else { $error = "Database query error."; deleteGalleryImageFile($image, $uploadDir); } } } } /* ================================= EDIT GALLERY ================================= */ elseif ($action === 'edit') { $id = (int)($_POST['id'] ?? 0); $title = trim($_POST['title'] ?? ''); $description = trim($_POST['description'] ?? ''); $status = $_POST['status'] ?? 'active'; if ($id <= 0 || $title === '') { $error = "Please enter valid gallery details."; } elseif (!in_array($status, ['active', 'inactive'], true)) { $error = "Invalid status selected."; } else { $stmt = mysqli_prepare( $con, "SELECT image FROM gallery WHERE id = ? LIMIT 1" ); mysqli_stmt_bind_param($stmt, "i", $id); mysqli_stmt_execute($stmt); mysqli_stmt_bind_result($stmt, $oldImage); $found = mysqli_stmt_fetch($stmt); mysqli_stmt_close($stmt); if (!$found) { $error = "Gallery record not found."; } else { $newImage = $oldImage; $uploadedNewImage = false; if ( isset($_FILES['image']) && $_FILES['image']['error'] !== UPLOAD_ERR_NO_FILE ) { $upload = uploadGalleryImage( $_FILES['image'], $uploadDir ); if (!$upload['success']) { $error = $upload['message']; } else { $newImage = $upload['filename']; $uploadedNewImage = true; } } if ($error === '') { $stmt = mysqli_prepare( $con, "UPDATE gallery SET title = ?, description = ?, image = ?, status = ? WHERE id = ?" ); mysqli_stmt_bind_param( $stmt, "ssssi", $title, $description, $newImage, $status, $id ); if (mysqli_stmt_execute($stmt)) { mysqli_stmt_close($stmt); if ($uploadedNewImage) { deleteGalleryImageFile( $oldImage, $uploadDir ); } $_SESSION['gallery_message'] = "Gallery updated successfully!"; redirectGallery(); } else { $error = "Unable to update gallery."; mysqli_stmt_close($stmt); if ($uploadedNewImage) { deleteGalleryImageFile( $newImage, $uploadDir ); } } } } } } /* ================================= DELETE GALLERY ================================= */ elseif ($action === 'delete') { $id = (int)($_POST['id'] ?? 0); if ($id <= 0) { $error = "Invalid gallery ID."; } else { $stmt = mysqli_prepare( $con, "SELECT image FROM gallery WHERE id = ? LIMIT 1" ); mysqli_stmt_bind_param($stmt, "i", $id); mysqli_stmt_execute($stmt); mysqli_stmt_bind_result($stmt, $image); $found = mysqli_stmt_fetch($stmt); mysqli_stmt_close($stmt); if (!$found) { $error = "Gallery record not found."; } else { $stmt = mysqli_prepare( $con, "DELETE FROM gallery WHERE id = ?" ); mysqli_stmt_bind_param($stmt, "i", $id); if (mysqli_stmt_execute($stmt)) { mysqli_stmt_close($stmt); deleteGalleryImageFile($image, $uploadDir); $_SESSION['gallery_message'] = "Gallery image deleted successfully!"; redirectGallery(); } else { $error = "Unable to delete gallery."; mysqli_stmt_close($stmt); } } } } } } /* ========================================= EDIT RECORD FETCH ========================================= */ $editData = null; if (isset($_GET['edit'])) { $editId = (int)$_GET['edit']; if ($editId > 0) { $stmt = mysqli_prepare( $con, "SELECT id, title, description, image, status FROM gallery WHERE id = ? LIMIT 1" ); mysqli_stmt_bind_param($stmt, "i", $editId); mysqli_stmt_execute($stmt); mysqli_stmt_bind_result( $stmt, $rowId, $rowTitle, $rowDescription, $rowImage, $rowStatus ); if (mysqli_stmt_fetch($stmt)) { $editData = [ 'id' => $rowId, 'title' => $rowTitle, 'description' => $rowDescription, 'image' => $rowImage, 'status' => $rowStatus ]; } mysqli_stmt_close($stmt); } } /* ========================================= GALLERY LIST ========================================= */ $galleryResult = mysqli_query( $con, "SELECT * FROM gallery ORDER BY id DESC" ); $totalImages = mysqli_fetch_assoc( mysqli_query($con, "SELECT COUNT(*) AS total FROM gallery") )['total'] ?? 0; $activeImages = mysqli_fetch_assoc( mysqli_query( $con, "SELECT COUNT(*) AS total FROM gallery WHERE status='active'" ) )['total'] ?? 0; $inactiveImages = mysqli_fetch_assoc( mysqli_query( $con, "SELECT COUNT(*) AS total FROM gallery WHERE status='inactive'" ) )['total'] ?? 0; $flashMessage = $_SESSION['gallery_message'] ?? ''; unset($_SESSION['gallery_message']); $adminName = $_SESSION['ADMIN_NAME'] ?? 'Administrator'; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Gallery Management | International Adventures</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"> <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet"> <style> :root { --navy: #101d3a; --blue: #315bea; --bg: #f4f7fc; --text: #26344d; } * { box-sizing: border-box; } body { background: var(--bg); font-family: 'Inter', sans-serif; color: var(--text); margin: 0; } .sidebar { position: fixed; top: 0; left: 0; width: 250px; height: 100vh; background: var(--navy); padding: 25px 17px; z-index: 1000; transition: .3s; } .brand { color: white; font-size: 15px; font-weight: 800; line-height: 1.5; padding: 5px 10px 25px; border-bottom: 1px solid rgba(255,255,255,.12); } .brand small { display: block; font-size: 10px; color: #9eacc9; letter-spacing: 2px; } .menu-title { color: #8b9bb9; font-size: 10px; letter-spacing: 1.5px; font-weight: 700; margin: 28px 12px 12px; } .sidebar a { display: block; color: #bdc9df; padding: 13px 14px; border-radius: 9px; margin-bottom: 6px; text-decoration: none; font-size: 13px; font-weight: 600; } .sidebar a i { width: 23px; } .sidebar a:hover, .sidebar a.active { background: var(--blue); color: white; } .main { margin-left: 250px; padding: 30px; } .topbar { display: flex; justify-content: space-between; align-items: center; gap: 15px; margin-bottom: 28px; } .topbar h2 { font-size: 24px; font-weight: 800; color: var(--navy); margin: 0 0 6px; } .topbar p { color: #8994a8; font-size: 12px; margin: 0; } .logout { color: #e34c4c; background: #fff0f0; padding: 11px 15px; border-radius: 9px; text-decoration: none; font-size: 12px; font-weight: 700; } .panel { background: white; border: 1px solid #e9edf5; border-radius: 15px; padding: 23px; margin-bottom: 25px; box-shadow: 0 5px 20px rgba(20,40,80,.025); } .panel-heading { font-size: 16px; font-weight: 800; color: var(--navy); margin-bottom: 20px; } .stat-card { background: white; border: 1px solid #e9edf5; border-radius: 14px; padding: 20px; height: 100%; } .stat-card small { color: #8994a8; font-size: 11px; font-weight: 600; } .stat-card h3 { color: var(--navy); font-size: 26px; font-weight: 800; margin: 9px 0 0; } .stat-icon { width: 43px; height: 43px; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-size: 18px; } .form-label { font-size: 12px; font-weight: 700; color: #42506a; margin-bottom: 8px; } .form-control, .form-select { border: 1px solid #e0e6ef; padding: 11px 13px; border-radius: 9px; font-size: 13px; box-shadow: none !important; } .form-control:focus, .form-select:focus { border-color: var(--blue); } .btn-primary { background: var(--blue); border-color: var(--blue); border-radius: 9px; padding: 11px 20px; font-size: 12px; font-weight: 700; } .btn-secondary { border-radius: 9px; padding: 11px 18px; font-size: 12px; } .preview-box { width: 150px; height: 105px; border: 1px dashed #cbd5e5; border-radius: 10px; display: flex; justify-content: center; align-items: center; overflow: hidden; background: #f8faff; margin-top: 12px; } .preview-box img { width: 100%; height: 100%; object-fit: cover; } .gallery-img { width: 90px; height: 65px; object-fit: cover; border-radius: 8px; border: 1px solid #e9edf5; } .table { vertical-align: middle; } .table thead th { background: #f7f9fd; color: #77839a; font-size: 10px; text-transform: uppercase; letter-spacing: .6px; padding: 15px 12px; white-space: nowrap; } .table tbody td { font-size: 12px; padding: 13px 12px; color: #4d5a70; } .badge-active { background: #e5f8ef; color: #15945b; padding: 7px 10px; border-radius: 20px; } .badge-inactive { background: #fff0e5; color: #d17b27; padding: 7px 10px; border-radius: 20px; } .action-btn { width: 32px; height: 32px; display: inline-flex; align-items: center; justify-content: center; border: 0; border-radius: 7px; text-decoration: none; font-size: 12px; } .edit-btn { color: #315bea; background: #edf1ff; } .delete-btn { color: #e34c4c; background: #fff0f0; } .mobile-toggle { display: none; border: 0; background: var(--navy); color: white; width: 40px; height: 40px; border-radius: 9px; } .overlay { display: none; } @media(max-width:768px) { .sidebar { left: -260px; } .sidebar.show { left: 0; } .overlay.show { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 999; } .main { margin-left: 0; padding: 18px 13px; } .mobile-toggle { display: inline-flex; align-items: center; justify-content: center; } .topbar h2 { font-size: 18px; } .topbar { align-items: flex-start; } .panel { padding: 16px; } } </style> </head> <body> <div class="overlay" id="overlay" onclick="closeSidebar()"></div> <!-- SIDEBAR --> <aside class="sidebar" id="sidebar"> <div class="brand"> <i class="fa-solid fa-earth-asia me-2"></i> INTERNATIONAL<br>ADVENTURES <small>ADMIN PANEL</small> </div> <div class="menu-title">MAIN MENU</div> <a href="index.php"> <i class="fa-solid fa-house"></i> Dashboard </a> <a href="gallery.php" class="active"> <i class="fa-regular fa-images"></i> Gallery </a> <a href="jobs.php"> <i class="fa-solid fa-briefcase"></i> Jobs </a> <a href="enquiry.php"> <i class="fa-solid fa-envelope-open-text"></i> Enquiry Form </a> <div style="position:absolute;bottom:25px;left:20px;color:#aab8d1;font-size:11px;"> <i class="fa-solid fa-user-shield me-2"></i> <?php echo e($adminName); ?> </div> </aside> <!-- MAIN --> <main class="main"> <div class="topbar"> <div class="d-flex align-items-center gap-3"> <button class="mobile-toggle" onclick="openSidebar()"> <i class="fa-solid fa-bars"></i> </button> <div> <h2>Gallery Management</h2> <p>Add, edit and manage your website gallery images.</p> </div> </div> <a href="logout.php" class="logout" onclick="return confirm('Are you sure you want to logout?')"> <i class="fa-solid fa-right-from-bracket me-1"></i> Logout </a> </div> <!-- NOTIFICATIONS --> <?php if ($flashMessage): ?> <div class="alert alert-success"> <i class="fa-solid fa-circle-check me-2"></i> <?php echo e($flashMessage); ?> </div> <?php endif; ?> <?php if ($error): ?> <div class="alert alert-danger"> <i class="fa-solid fa-circle-exclamation me-2"></i> <?php echo e($error); ?> </div> <?php endif; ?> <!-- STATISTICS --> <div class="row g-3 mb-4"> <div class="col-md-4"> <div class="stat-card d-flex justify-content-between align-items-center"> <div> <small>TOTAL IMAGES</small> <h3><?php echo (int)$totalImages; ?></h3> </div> <div class="stat-icon" style="background:#edf1ff;color:#315bea;"> <i class="fa-regular fa-images"></i> </div> </div> </div> <div class="col-md-4"> <div class="stat-card d-flex justify-content-between align-items-center"> <div> <small>ACTIVE IMAGES</small> <h3><?php echo (int)$activeImages; ?></h3> </div> <div class="stat-icon" style="background:#e5f8ef;color:#15945b;"> <i class="fa-solid fa-circle-check"></i> </div> </div> </div> <div class="col-md-4"> <div class="stat-card d-flex justify-content-between align-items-center"> <div> <small>INACTIVE IMAGES</small> <h3><?php echo (int)$inactiveImages; ?></h3> </div> <div class="stat-icon" style="background:#fff4e5;color:#dc922d;"> <i class="fa-solid fa-eye-slash"></i> </div> </div> </div> </div> <!-- ADD / EDIT FORM --> <div class="panel"> <div class="panel-heading"> <i class="fa-solid fa-<?php echo $editData ? 'pen-to-square' : 'cloud-arrow-up'; ?> me-2" style="color:#315bea;"></i> <?php echo $editData ? 'Edit Gallery Image' : 'Add New Gallery Image'; ?> </div> <form method="POST" enctype="multipart/form-data"> <input type="hidden" name="csrf_token" value="<?php echo e($csrf); ?>"> <input type="hidden" name="action" value="<?php echo $editData ? 'edit' : 'add'; ?>"> <?php if ($editData): ?> <input type="hidden" name="id" value="<?php echo (int)$editData['id']; ?>"> <?php endif; ?> <div class="row g-3"> <div class="col-md-6"> <label class="form-label">Gallery Title *</label> <input type="text" name="title" class="form-control" placeholder="Enter gallery title" value="<?php echo e($editData['title'] ?? ''); ?>" maxlength="255" required> </div> <div class="col-md-6"> <label class="form-label">Image Status *</label> <select name="status" class="form-select" required> <option value="active" <?php echo (($editData['status'] ?? 'active') === 'active') ? 'selected' : ''; ?>> Active </option> <option value="inactive" <?php echo (($editData['status'] ?? '') === 'inactive') ? 'selected' : ''; ?>> Inactive </option> </select> </div> <div class="col-12"> <label class="form-label">Description</label> <textarea name="description" class="form-control" rows="3" placeholder="Enter image description"><?php echo e($editData['description'] ?? ''); ?></textarea> </div> <div class="col-md-7"> <label class="form-label"> <?php echo $editData ? 'Change Image (Optional)' : 'Upload Image *'; ?> </label> <input type="file" name="image" id="imageInput" class="form-control" accept=".jpg,.jpeg,.png,.webp,image/jpeg,image/png,image/webp" <?php echo $editData ? '' : 'required'; ?>> <small class="text-muted d-block mt-2"> Allowed: JPG, PNG, WEBP | Maximum size: 5MB </small> <div class="preview-box" id="previewBox"> <?php if ($editData): ?> <img src="<?php echo e($uploadUrl . $editData['image']); ?>" alt="Current gallery image"> <?php else: ?> <span class="text-muted small">Image Preview</span> <?php endif; ?> </div> </div> <div class="col-12 d-flex gap-2 flex-wrap mt-3"> <button type="submit" class="btn btn-primary"> <i class="fa-solid fa-<?php echo $editData ? 'floppy-disk' : 'plus'; ?> me-2"></i> <?php echo $editData ? 'Update Gallery' : 'Add Gallery Image'; ?> </button> <?php if ($editData): ?> <a href="gallery.php" class="btn btn-secondary"> Cancel Edit </a> <?php endif; ?> </div> </div> </form> </div> <!-- GALLERY TABLE --> <div class="panel"> <div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-3"> <div class="panel-heading mb-0"> <i class="fa-solid fa-images me-2" style="color:#315bea;"></i> All Gallery Images </div> <span class="badge text-bg-primary"> <?php echo (int)$totalImages; ?> Images </span> </div> <div class="table-responsive"> <table class="table table-hover"> <thead> <tr> <th>#</th> <th>Image</th> <th>Title</th> <th>Description</th> <th>Status</th> <th>Date</th> <th>Actions</th> </tr> </thead> <tbody> <?php if ($galleryResult && mysqli_num_rows($galleryResult) > 0): ?> <?php $i = 1; ?> <?php while ($row = mysqli_fetch_assoc($galleryResult)): ?> <tr> <td><?php echo $i++; ?></td> <td> <img src="<?php echo e($uploadUrl . $row['image']); ?>" class="gallery-img" alt="<?php echo e($row['title']); ?>" loading="lazy" > </td> <td> <strong><?php echo e($row['title']); ?></strong> </td> <td style="max-width:200px;"> <?php $desc = $row['description'] ?? ''; echo e(mb_strimwidth($desc, 0, 65, '...')); ?> </td> <td> <?php if ($row['status'] === 'active'): ?> <span class="badge-active">Active</span> <?php else: ?> <span class="badge-inactive">Inactive</span> <?php endif; ?> </td> <td> <?php echo e(date( 'd M Y', strtotime($row['created_at']) )); ?> </td> <td> <div class="d-flex gap-2"> <a href="gallery.php?edit=<?php echo (int)$row['id']; ?>" class="action-btn edit-btn" title="Edit"> <i class="fa-solid fa-pen"></i> </a> <form method="POST" onsubmit="return confirm('Are you sure you want to delete this gallery image?');"> <input type="hidden" name="csrf_token" value="<?php echo e($csrf); ?>"> <input type="hidden" name="action" value="delete"> <input type="hidden" name="id" value="<?php echo (int)$row['id']; ?>"> <button type="submit" class="action-btn delete-btn" title="Delete"> <i class="fa-solid fa-trash"></i> </button> </form> </div> </td> </tr> <?php endwhile; ?> <?php else: ?> <tr> <td colspan="7" class="text-center py-5"> <i class="fa-regular fa-images fs-1 text-muted"></i> <h6 class="mt-3 fw-bold">No Gallery Images Found</h6> <p class="text-muted small mb-0"> Upload your first gallery image using the form above. </p> </td> </tr> <?php endif; ?> </tbody> </table> </div> </div> <div class="text-center text-muted small mt-4"> © <?php echo date('Y'); ?> International Adventures Overseas Services. All Rights Reserved. </div> </main> <script> function openSidebar() { document.getElementById('sidebar').classList.add('show'); document.getElementById('overlay').classList.add('show'); } function closeSidebar() { document.getElementById('sidebar').classList.remove('show'); document.getElementById('overlay').classList.remove('show'); } // Image Preview document.getElementById('imageInput').addEventListener('change', function() { const file = this.files[0]; const box = document.getElementById('previewBox'); if (!file) return; const allowedTypes = [ 'image/jpeg', 'image/png', 'image/webp' ]; if (!allowedTypes.includes(file.type)) { alert('Only JPG, PNG and WEBP images are allowed.'); this.value = ''; return; } if (file.size > 5 * 1024 * 1024) { alert('Image size must be maximum 5MB.'); this.value = ''; return; } const reader = new FileReader(); reader.onload = function(event) { box.innerHTML = ''; const img = document.createElement('img'); img.src = event.target.result; img.alt = 'Image preview'; box.appendChild(img); }; reader.readAsDataURL(file); }); </script> </body> </html>
Save
Cancel