Sindbad~EG File Manager

Current Path : /home/u625735752/domains/snackoncrumbs.com/public_html/demo/1.1/
Upload File :
Current File : /home/u625735752/domains/snackoncrumbs.com/public_html/demo/1.1/manage-slider.php

<?php
session_start();
include 'conn.php';
include 'manage-common1.php';

// Define pagination variables
$items_per_page = 10; // Number of sliders to display per page
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $items_per_page;

function handle_file_upload($file) {
    $target_dir = "assets/img/sliders/";
    $date_prefix = date("d-m-y-H-i-s");    // Add unique name to avoid overwriting
    $original_name = basename($file["name"]);
    $file_extension = strtolower(pathinfo($original_name, PATHINFO_EXTENSION));
    $new_file_name = $date_prefix . '-' . $original_name;
    $target_file = $target_dir . $new_file_name;

    // Check if file is an image
    $check = getimagesize($file["tmp_name"]);
    if ($check === false) {
        return "File is not an image.";
    }

    // Check file size (limit to 5MB)
    if ($file["size"] > 5000000) {
        return "Sorry, your file is too large.";
    }

    // Allow certain file formats (jpg, jpeg, png, gif)
    if (!in_array($file_extension, ["jpg", "jpeg", "png", "gif"])) {
        return "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    }

    // Attempt to move the uploaded file
    if (move_uploaded_file($file["tmp_name"], $target_file)) {
        return $target_file;
    } else {
        return "Sorry, there was an error uploading your file.";
    }
}

// Add Slider
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_slider'])) {
    function sanitize($data) {
        return htmlspecialchars(strip_tags(trim($data)));
    }

    $title = sanitize($_POST['title']);
    $url = sanitize($_POST['url']);
    $image = '';

    if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
        $uploaded_file = handle_file_upload($_FILES['image']);
        if (strpos($uploaded_file, "Sorry") === 0) {  // Error occurred
            echo "<p class='text-danger'>$uploaded_file</p>";
        } else {
            $image = $uploaded_file;
        }
    }

    $sql = "INSERT INTO slider (title, url, image) VALUES (?, ?, ?)";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("sss", $title, $url, $image);

    if ($stmt->execute()) {
        header("Location: manage-slider.php");
        exit();
    } else {
        echo "Error: Unable to add slider.";
    }
    $stmt->close();
}

// Edit Slider
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['edit_slider'])) {
    function sanitize($data) {
        return htmlspecialchars(strip_tags(trim($data)));
    }

    $slider_id = $_POST['edit_id'];
    $title = sanitize($_POST['edit_title']);
    $url = sanitize($_POST['edit_url']);

    // Retain existing image URL
    $image = htmlspecialchars($_POST['edit_image']);

    if (isset($_FILES['edit_image']) && $_FILES['edit_image']['error'] === UPLOAD_ERR_OK) {
        $uploaded_file = handle_file_upload($_FILES['edit_image']);
        if (strpos($uploaded_file, "Sorry") === 0) {  // Error occurred
            echo "<p class='text-danger'>$uploaded_file</p>";
        } else {
            $image = $uploaded_file;
        }
    }

    $sql = "UPDATE slider SET title=?, url=?, image=? WHERE id=?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("sssi", $title, $url, $image, $slider_id);

    if ($stmt->execute()) {
        header("Location: manage-slider.php");
        exit();
    } else {
        echo "Error: Unable to edit slider.";
    }
    $stmt->close();
}

// Delete Slider
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['delete_slider_id'])) {
    $slider_id = $_GET['delete_slider_id'];

    $sql = "DELETE FROM slider WHERE id=?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $slider_id);
    if ($stmt->execute()) {
        header("Location: manage-slider.php");
        exit();
    } else {
        echo "Error: Unable to delete slider.";
    }
    $stmt->close();
}

// Fetch Sliders for Display
$total_sliders_result = $conn->query("SELECT COUNT(*) as total FROM slider");
$total_sliders = $total_sliders_result->fetch_assoc()['total'];
$total_pages = ceil($total_sliders / $items_per_page);

$sql = "SELECT * FROM slider LIMIT ? OFFSET ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ii", $items_per_page, $offset);
$stmt->execute();
$sliders_result = $stmt->get_result();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <title>Slider Management</title>
</head>
<body>



<?php include 'manage-nav.php';?>

<!-- Add Slider Modal -->
<div class="modal fade" id="addModal" tabindex="-1" aria-labelledby="addModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="addModalLabel">Add Slider</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form method="post" action="manage-slider.php" enctype="multipart/form-data">
                    <div class="mb-3">
                        <label for="title" class="form-label">Slider Title</label>
                        <input type="text" id="title" name="title" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label for="url" class="form-label">URL</label>
                        <input type="url" id="url" name="url" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label for="image" class="form-label">Image</label>
                        <input type="file" id="image" name="image" class="form-control" accept="image/*">
                    </div>
                    <button type="submit" name="add_slider" class="btn btn-primary">Add Slider</button>
                </form>
            </div>
        </div>
    </div>
</div>

<!-- Edit Slider Modal -->
<div class="modal fade" id="editModal" tabindex="-1" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="editModalLabel">Edit Slider</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form method="post" action="manage-slider.php" enctype="multipart/form-data">
                    <input type="hidden" id="edit_id" name="edit_id">
                    <div class="mb-3">
                        <label for="edit_title" class="form-label">Slider Title</label>
                        <input type="text" id="edit_title" name="edit_title" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label for="edit_url" class="form-label">URL</label>
                        <input type="url" id="edit_url" name="edit_url" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label for="edit_image" class="form-label">Image</label>
                        <input type="file" id="edit_image" name="edit_image" class="form-control" accept="image/*">
                    </div>
                    <button type="submit" name="edit_slider" class="btn btn-primary">Save Changes</button>
                </form>
            </div>
        </div>
    </div>
</div>

<div class="container mt-5">
    <h1 class="mb-4">Slider Management</h1>

    <!-- Add New Slider Button, only visible if logged in -->
    <?php if ($logged_in) { ?>
        <button type="button" class="btn btn-primary mb-4" data-bs-toggle="modal" data-bs-target="#addModal">
            Add New Slider
        </button>
    <?php } ?>

    <table class="table">
        <thead>
            <tr>
                <th>Title</th>
                <th>Image</th>
                <th>URL</th>
                <th>Actions</th>
            </tr>
        </thead>

        <tbody>
            <?php while ($slider = $sliders_result->fetch_assoc()) { ?>
                <tr>
                    <td><?= htmlspecialchars($slider['title']) ?></td>
                    <td>
                        <?php if (!empty($slider['image'])) { ?>
                            <img src="<?= htmlspecialchars($slider['image']) ?>" alt="<?= htmlspecialchars($slider['title']) ?>" width="100">
                        <?php } ?>
                    </td>
                    <td><?= htmlspecialchars($slider['url']) ?></td>
                    <td>
                        <?php if ($logged_in) { ?>
                            <button type="button" class="btn btn-warning btn-sm" data-bs-toggle="modal" data-bs-target="#editModal" onclick="populateEditModal(<?= $slider['id'] ?>, '<?= addslashes($slider['title']) ?>', '<?= addslashes($slider['url']) ?>', '<?= addslashes($slider['image']) ?>')">
                                Edit
                            </button>
                            <a href="?delete_slider_id=<?= $slider['id'] ?>" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure you want to delete this slider?')">Delete</a>
                        <?php } ?>
                    </td>
                </tr>
            <?php } ?>
        </tbody>
    </table>

    <nav>
        <ul class="pagination">
            <li class="page-item <?= ($page <= 1) ? 'disabled' : '' ?>">
                <a class="page-link" href="?page=<?= $page - 1 ?>">Previous</a>
            </li>
            <?php for ($i = 1; $i <= $total_pages; $i++) { ?>
                <li class="page-item <?= ($i == $page) ? 'active' : '' ?>">
                    <a class="page-link" href="?page=<?= $i ?>"><?= $i ?></a>
                </li>
            <?php } ?>
            <li class="page-item <?= ($page >= $total_pages) ? 'disabled' : '' ?>">
                <a class="page-link" href="?page=<?= $page + 1 ?>">Next</a>
            </li>
        </ul>
    </nav>
</div>

<script>
document.addEventListener('DOMContentLoaded', function () {
    window.populateEditModal = function(id, title, url, image) {
        document.getElementById('edit_id').value = id;
        document.getElementById('edit_title').value = title;
        document.getElementById('edit_url').value = url;
        document.getElementById('edit_image').value = image;
    }
});
</script>

<?php include 'footer-manage.php'; ?>

</body>
</html>

<?php $conn->close(); ?>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists