Sindbad~EG File Manager
<?php
require_once '../../config/config.php';
checkLogin();
$pageTitle = "Manage Documents - " . APP_NAME;
// Check access
if (!isSuperuser()) {
redirect('../../dashboard.php');
}
$db = Database::getInstance()->getConnection();
$success = '';
$error = '';
// Handle file upload
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['upload_document'])) {
$title = trim($_POST['title'] ?? '');
$description = trim($_POST['description'] ?? '');
if (empty($title)) {
$error = "Title is required";
} elseif (empty($_FILES['document']['name'])) {
$error = "Please select a file to upload";
} else {
$file = $_FILES['document'];
$uploadDir = '../../docs/chatbot/';
// Create directory if it doesn't exist
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0777, true);
}
$fileExt = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowedExts = ['pdf', 'doc', 'docx', 'txt', 'md'];
if (!in_array($fileExt, $allowedExts)) {
$error = "Only PDF, DOC, DOCX, TXT, and MD files are allowed";
} else {
$newFilename = time() . '_' . preg_replace('/[^a-zA-Z0-9._-]/', '', $file['name']);
$filePath = $uploadDir . $newFilename;
if (move_uploaded_file($file['tmp_name'], $filePath)) {
// Extract text content for indexing
$contentText = '';
if ($fileExt === 'txt' || $fileExt === 'md') {
$contentText = file_get_contents($filePath);
}
// For PDF/DOC, you would need additional libraries like PDFParser
$stmt = $db->prepare("
INSERT INTO chatbot_documents
(filename, original_filename, file_path, file_type, file_size, title, description, content_text, uploaded_by)
VALUES (:filename, :original_filename, :file_path, :file_type, :file_size, :title, :description, :content_text, :user_id)
");
if ($stmt->execute([
'filename' => $newFilename,
'original_filename' => $file['name'],
'file_path' => $filePath,
'file_type' => $fileExt,
'file_size' => $file['size'],
'title' => $title,
'description' => $description,
'content_text' => $contentText,
'user_id' => $_SESSION['user_id']
])) {
$success = "Document uploaded successfully!";
} else {
$error = "Failed to save document information";
}
} else {
$error = "Failed to upload file";
}
}
}
}
// Handle delete
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_document'])) {
$id = (int)$_POST['doc_id'];
// Get file path
$stmt = $db->prepare("SELECT file_path FROM chatbot_documents WHERE id = :id");
$stmt->execute(['id' => $id]);
$doc = $stmt->fetch(PDO::FETCH_ASSOC);
if ($doc && file_exists($doc['file_path'])) {
unlink($doc['file_path']);
}
$stmt = $db->prepare("DELETE FROM chatbot_documents WHERE id = :id");
if ($stmt->execute(['id' => $id])) {
$success = "Document deleted successfully!";
}
}
// Handle toggle
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['toggle_document'])) {
$id = (int)$_POST['doc_id'];
$stmt = $db->prepare("UPDATE chatbot_documents SET is_active = NOT is_active WHERE id = :id");
$stmt->execute(['id' => $id]);
$success = "Document status updated!";
}
// Get all documents
$stmt = $db->query("
SELECT d.*, u.full_name as uploaded_by_name
FROM chatbot_documents d
LEFT JOIN users u ON d.uploaded_by = u.id
ORDER BY d.uploaded_at DESC
");
$documents = $stmt->fetchAll(PDO::FETCH_ASSOC);
include '../../includes/header.php';
include '../../includes/sidebar.php';
?>
<main class="main-content md:ml-64 pt-16">
<div class="container mx-auto px-4 py-8">
<!-- Header -->
<div class="mb-6 flex justify-between items-center">
<div>
<h1 class="text-3xl font-bold text-gray-800">
<i class="fas fa-file-upload mr-2 text-purple-500"></i>Manage Documents
</h1>
<p class="text-gray-600 mt-2">Upload documents for AI learning and knowledge base</p>
</div>
<a href="index.php" class="bg-gray-600 text-white px-4 py-2 rounded-lg hover:bg-gray-700 transition">
<i class="fas fa-arrow-left mr-2"></i>Back
</a>
</div>
<?php if ($success): ?>
<div id="successMessage" class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded-lg mb-6 flex items-center justify-between">
<div><i class="fas fa-check-circle mr-2"></i><?php echo $success; ?></div>
<button onclick="dismissMessage('successMessage')" class="text-green-700 hover:text-green-900">
<i class="fas fa-times"></i>
</button>
</div>
<?php endif; ?>
<?php if ($error): ?>
<div id="errorMessage" class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded-lg mb-6 flex items-center justify-between">
<div><i class="fas fa-exclamation-circle mr-2"></i><?php echo $error; ?></div>
<button onclick="dismissMessage('errorMessage')" class="text-red-700 hover:text-red-900">
<i class="fas fa-times"></i>
</button>
</div>
<?php endif; ?>
<!-- Upload Form -->
<div class="bg-white rounded-xl shadow-lg p-6 mb-8">
<h3 class="text-lg font-bold text-gray-800 mb-4">
<i class="fas fa-cloud-upload-alt mr-2"></i>Upload New Document
</h3>
<form method="POST" enctype="multipart/form-data" class="space-y-4">
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">Title *</label>
<input type="text" name="title" required
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-purple-500"
placeholder="Document title">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">Description</label>
<textarea name="description" rows="3"
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-purple-500"
placeholder="Brief description of the document"></textarea>
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">Document File *</label>
<input type="file" name="document" required accept=".pdf,.doc,.docx,.txt,.md"
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-purple-500">
<p class="text-xs text-gray-500 mt-1">Accepted formats: PDF, DOC, DOCX, TXT, MD (Max 10MB)</p>
</div>
<button type="submit" name="upload_document" class="bg-gradient-to-r from-purple-500 to-purple-600 text-white px-6 py-2 rounded-lg hover:shadow-lg transition">
<i class="fas fa-upload mr-2"></i>Upload Document
</button>
</form>
</div>
<!-- Documents List -->
<div class="bg-white rounded-xl shadow-lg p-6">
<h3 class="text-lg font-bold text-gray-800 mb-4">
<i class="fas fa-folder-open mr-2"></i>Uploaded Documents (<?php echo count($documents); ?>)
</h3>
<?php if (empty($documents)): ?>
<div class="text-center py-12 text-gray-500">
<i class="fas fa-file-alt text-6xl mb-4 text-gray-300"></i>
<p>No documents uploaded yet</p>
</div>
<?php else: ?>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<?php foreach ($documents as $doc): ?>
<div class="border border-gray-200 rounded-lg p-4 <?php echo $doc['is_active'] ? '' : 'bg-gray-50 opacity-60'; ?>">
<div class="flex items-start gap-3">
<div class="p-3 bg-purple-100 text-purple-600 rounded-lg">
<i class="fas fa-file-<?php echo $doc['file_type'] === 'pdf' ? 'pdf' : 'alt'; ?> text-2xl"></i>
</div>
<div class="flex-1">
<h4 class="font-bold text-gray-800 mb-1"><?php echo htmlspecialchars($doc['title']); ?></h4>
<?php if ($doc['description']): ?>
<p class="text-sm text-gray-600 mb-2"><?php echo htmlspecialchars($doc['description']); ?></p>
<?php endif; ?>
<div class="flex items-center gap-2 text-xs text-gray-500 mb-2">
<span class="px-2 py-1 bg-gray-100 rounded"><?php echo strtoupper($doc['file_type']); ?></span>
<span><?php echo number_format($doc['file_size'] / 1024, 1); ?> KB</span>
<span><?php echo date('M d, Y', strtotime($doc['uploaded_at'])); ?></span>
</div>
<?php if ($doc['is_active']): ?>
<span class="px-2 py-1 bg-green-100 text-green-700 text-xs rounded">Active</span>
<?php else: ?>
<span class="px-2 py-1 bg-gray-100 text-gray-700 text-xs rounded">Inactive</span>
<?php endif; ?>
</div>
<div class="flex flex-col gap-2">
<a href="<?php echo str_replace('../../', BASE_URL . '/', $doc['file_path']); ?>" target="_blank"
class="text-blue-600 hover:text-blue-700" title="Download">
<i class="fas fa-download"></i>
</a>
<form method="POST" class="inline">
<input type="hidden" name="doc_id" value="<?php echo $doc['id']; ?>">
<button type="submit" name="toggle_document"
class="text-gray-600 hover:text-blue-600" title="Toggle Status">
<i class="fas fa-power-off"></i>
</button>
</form>
<form method="POST" class="inline" onsubmit="return confirm('Delete this document?')">
<input type="hidden" name="doc_id" value="<?php echo $doc['id']; ?>">
<button type="submit" name="delete_document"
class="text-gray-600 hover:text-red-600" title="Delete">
<i class="fas fa-trash"></i>
</button>
</form>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
</div>
</main>
<?php include '../../includes/footer.php'; ?>
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists