Sindbad~EG File Manager
<?php
// Database connection settings
$host = 'localhost';
$dbname = 'basic';
$username = 'root';
$password = '';
// Create a new mysqli instance
$mysqli = new mysqli($host, $username, $password, $dbname);
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Function to fetch all blogs from the database
function getBlogs($mysqli) {
$result = $mysqli->query("SELECT * FROM blogs");
return $result->fetch_all(MYSQLI_ASSOC);
}
// Function to fetch a blog by ID
function getBlogById($mysqli, $id) {
$stmt = $mysqli->prepare("SELECT * FROM blogs WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
return $result->fetch_assoc();
}
session_start();
// Function to handle file upload
function handleFileUpload($file) {
if ($file['error'] === UPLOAD_ERR_OK) {
$tmpName = $file['tmp_name'];
$name = basename($file['name']);
$uploadDir = 'images/';
$uploadPath = $uploadDir . $name;
if (move_uploaded_file($tmpName, $uploadPath)) {
return $name;
}
}
return null;
}
// Handle blog actions (Add, Edit, Delete)
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['login'])) {
$username = $_POST['username'];
$password = $_POST['password'];
if ($username === 'admin' && $password === 'Admin123') {
$_SESSION['user'] = ['username' => 'admin']; // Mock user session
header('Location: blog.php');
exit;
} else {
$error = "Invalid credentials";
}
} elseif (isset($_POST['add_blog'])) {
$title = $_POST['title'];
$intro = $_POST['intro'];
$description = $_POST['description'];
$image = handleFileUpload($_FILES['image']);
$stmt = $mysqli->prepare("INSERT INTO blogs (title, intro, description, image) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $title, $intro, $description, $image);
$stmt->execute();
header('Location: blog.php');
exit;
} elseif (isset($_POST['edit_blog'])) {
$id = $_POST['id'];
$title = $_POST['title'];
$intro = $_POST['intro'];
$description = $_POST['description'];
$image = $_POST['current_image']; // Keep existing image by default
if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
$image = handleFileUpload($_FILES['image']);
}
$stmt = $mysqli->prepare("UPDATE blogs SET title = ?, intro = ?, description = ?, image = ? WHERE id = ?");
$stmt->bind_param("ssssi", $title, $intro, $description, $image, $id);
$stmt->execute();
header('Location: blog.php');
exit;
} elseif (isset($_POST['delete_blog'])) {
$id = $_POST['id'];
$stmt = $mysqli->prepare("DELETE FROM blogs WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
header('Location: blog.php');
exit;
}
}
// Handle AJAX request for blog data
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['action']) && $_GET['action'] === 'get_blog' && isset($_GET['id'])) {
$id = $_GET['id'];
$blog = getBlogById($mysqli, $id);
if ($blog) {
echo json_encode($blog);
} else {
echo json_encode(['error' => 'Blog not found']);
}
exit;
}
// Fetch blogs
$blogs = getBlogs($mysqli);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blog Management</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="css/styles.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/tinymce@6.8.1/tinymce.min.js"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Blog Management</a>
<?php if (!isset($_SESSION['user'])): ?>
<button class="btn btn-primary ms-auto" data-bs-toggle="modal" data-bs-target="#loginModal">Login</button>
<?php else: ?>
<button class="btn btn-secondary ms-auto" onclick="window.location.href='logout.php'">Logout</button>
<?php endif; ?>
</nav>
<div class="container mt-4">
<div class="row">
<?php foreach ($blogs as $blog): ?>
<div class="col-md-4 mb-4">
<div class="card">
<img src="images/<?php echo htmlspecialchars($blog['image']); ?>" class="card-img-top" alt="<?php echo htmlspecialchars($blog['title']); ?>">
<div class="card-body">
<h5 class="card-title"><?php echo htmlspecialchars($blog['title']); ?></h5>
<p class="card-text"><?php echo htmlspecialchars($blog['intro']); ?></p>
<a href="#" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#viewBlogModal" data-id="<?php echo $blog['id']; ?>">View Blog</a>
<?php if (isset($_SESSION['user']) && $_SESSION['user']['username'] === 'admin'): ?>
<a href="#" class="btn btn-warning" data-bs-toggle="modal" data-bs-target="#editBlogModal" data-id="<?php echo $blog['id']; ?>">Edit</a>
<a href="#" class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#deleteBlogModal" data-id="<?php echo $blog['id']; ?>">Delete</a>
<?php endif; ?>
</div>
</div>
</div>
<?php endforeach; ?>
<?php if (isset($_SESSION['user']) && $_SESSION['user']['username'] === 'admin'): ?>
<div class="col-md-4 mb-4">
<button class="btn btn-success" data-bs-toggle="modal" data-bs-target="#addBlogModal">Add New Blog</button>
</div>
<?php endif; ?>
</div>
</div>
<!-- Add Blog Modal -->
<div class="modal fade" id="addBlogModal" tabindex="-1" aria-labelledby="addBlogModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addBlogModalLabel">Add New Blog</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form action="blog.php" method="post" enctype="multipart/form-data">
<div class="modal-body">
<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="intro" class="form-label">Intro</label>
<textarea id="intro" name="intro" class="form-control" required></textarea>
</div>
<div class="mb-3">
<label for="description" class="form-label">Description</label>
<textarea id="description" name="description" class="form-control" required></textarea>
</div>
<div class="mb-3">
<label for="image" class="form-label">Image</label>
<input type="file" class="form-control" id="image" name="image">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="add_blog">Add Blog</button>
</div>
</form>
</div>
</div>
</div>
<!-- Edit Blog Modal -->
<div class="modal fade" id="editBlogModal" tabindex="-1" aria-labelledby="editBlogModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="editBlogModalLabel">Edit Blog</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form action="blog.php" method="post" enctype="multipart/form-data">
<input type="hidden" id="edit_blog_id" name="id">
<input type="hidden" id="current_image" name="current_image">
<div class="modal-body">
<div class="mb-3">
<label for="edit_title" class="form-label">Title</label>
<input type="text" class="form-control" id="edit_title" name="title" required>
</div>
<div class="mb-3">
<label for="edit_intro" class="form-label">Intro</label>
<textarea id="edit_intro" name="intro" class="form-control" required></textarea>
</div>
<div class="mb-3">
<label for="edit_description" class="form-label">Description</label>
<textarea id="edit_description" name="description" class="form-control" required></textarea>
</div>
<div class="mb-3">
<label for="edit_image" class="form-label">Image</label>
<input type="file" class="form-control" id="edit_image" name="image">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="edit_blog">Save changes</button>
</div>
</form>
</div>
</div>
</div>
<!-- View Blog Modal -->
<div class="modal fade" id="viewBlogModal" tabindex="-1" aria-labelledby="viewBlogModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="viewBlogModalLabel">View Blog</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body" id="viewBlogContent">
<!-- Content will be injected here by JavaScript -->
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Delete Blog Modal -->
<div class="modal fade" id="deleteBlogModal" tabindex="-1" aria-labelledby="deleteBlogModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteBlogModalLabel">Delete Blog</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form action="blog.php" method="post">
<input type="hidden" id="delete_blog_id" name="id">
<div class="modal-body">
<p>Are you sure you want to delete this blog?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-danger" name="delete_blog">Delete</button>
</div>
</form>
</div>
</div>
</div>
<!-- Modal HTML remains the same -->
<!-- Include your modals here -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
tinymce.init({
selector: '#edit_intro, #edit_description, #intro, #description',
menubar: false,
plugins: 'link image code',
toolbar: 'undo redo | bold italic | link image code'
});
document.addEventListener('DOMContentLoaded', function () {
// Handle the Edit Blog Modal
const editModal = document.getElementById('editBlogModal');
editModal.addEventListener('show.bs.modal', function (event) {
const button = event.relatedTarget;
const id = button.getAttribute('data-id');
fetch(`blog.php?action=get_blog&id=${id}`)
.then(response => response.json())
.then(data => {
if (data.error) {
alert(data.error);
} else {
document.getElementById('edit_blog_id').value = data.id;
document.getElementById('edit_title').value = data.title;
tinymce.get('edit_intro').setContent(data.intro);
tinymce.get('edit_description').setContent(data.description);
document.getElementById('current_image').value = data.image;
}
})
.catch(error => console.error('Error fetching blog data:', error));
});
// Handle the View Blog Modal
const viewModal = document.getElementById('viewBlogModal');
viewModal.addEventListener('show.bs.modal', function (event) {
const button = event.relatedTarget;
const id = button.getAttribute('data-id');
fetch(`blog.php?action=get_blog&id=${id}`)
.then(response => response.json())
.then(data => {
if (data.error) {
alert(data.error);
} else {
document.getElementById('viewBlogContent').innerHTML = `
<h2>${data.title}</h2>
<img src="images/${data.image}" class="img-fluid mb-3" alt="${data.title}">
<p>${data.intro}</p>
<div>${data.description}</div>
`;
}
})
.catch(error => console.error('Error fetching blog data:', error));
});
// Handle the Delete Blog Modal
const deleteModal = document.getElementById('deleteBlogModal');
deleteModal.addEventListener('show.bs.modal', function (event) {
const button = event.relatedTarget;
const id = button.getAttribute('data-id');
document.getElementById('delete_blog_id').value = id;
});
});
</script>
</body>
</html>
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists