Sindbad~EG File Manager
<?php
/**
* Install Complete Chat System
* Creates all necessary tables for member-admin chat
*/
require_once 'config/config.php';
if (!isLoggedIn() || !isSuperuser()) {
die('Access denied. Superuser access required.');
}
$db = Database::getInstance()->getConnection();
$created = [];
$updated = [];
$errors = [];
try {
// Create conversations table
$db->exec("
CREATE TABLE IF NOT EXISTS conversations (
id INT AUTO_INCREMENT PRIMARY KEY,
visitor_name VARCHAR(255) NULL,
visitor_email VARCHAR(255) NULL,
member_id INT NULL,
status ENUM('active', 'closed') DEFAULT 'active',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (member_id) REFERENCES member_accounts(id) ON DELETE SET NULL,
INDEX idx_status (status),
INDEX idx_updated (updated_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
");
$created[] = "✅ Created 'conversations' table";
// Create chat_messages table
$db->exec("
CREATE TABLE IF NOT EXISTS chat_messages (
id INT AUTO_INCREMENT PRIMARY KEY,
conversation_id INT NOT NULL,
sender_type ENUM('visitor', 'member', 'admin') NOT NULL,
sender_id INT NULL,
message_text TEXT NOT NULL,
is_read TINYINT(1) DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (conversation_id) REFERENCES conversations(id) ON DELETE CASCADE,
FOREIGN KEY (sender_id) REFERENCES users(id) ON DELETE SET NULL,
INDEX idx_conversation (conversation_id),
INDEX idx_conversation_read (conversation_id, is_read, sender_type),
INDEX idx_created (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
");
$created[] = "✅ Created 'chat_messages' table";
// Check and update existing tables if needed
$stmt = $db->query("SHOW TABLES LIKE 'conversations'");
if ($stmt->rowCount() > 0) {
// Check if visitor_name column exists
$stmt = $db->query("SHOW COLUMNS FROM conversations LIKE 'visitor_name'");
if ($stmt->rowCount() == 0) {
$db->exec("
ALTER TABLE conversations
ADD COLUMN visitor_name VARCHAR(255) NULL AFTER id,
ADD COLUMN visitor_email VARCHAR(255) NULL AFTER visitor_name
");
$updated[] = "✅ Added visitor columns to conversations table";
}
// Check if member_id exists
$stmt = $db->query("SHOW COLUMNS FROM conversations LIKE 'member_id'");
if ($stmt->rowCount() == 0) {
$db->exec("
ALTER TABLE conversations
ADD COLUMN member_id INT NULL AFTER visitor_email,
ADD FOREIGN KEY (member_id) REFERENCES member_accounts(id) ON DELETE SET NULL
");
$updated[] = "✅ Added member_id to conversations table";
}
}
$stmt = $db->query("SHOW TABLES LIKE 'chat_messages'");
if ($stmt->rowCount() > 0) {
// Check if is_read column exists
$stmt = $db->query("SHOW COLUMNS FROM chat_messages LIKE 'is_read'");
if ($stmt->rowCount() == 0) {
$db->exec("ALTER TABLE chat_messages ADD COLUMN is_read TINYINT(1) DEFAULT 0 AFTER message_text");
$updated[] = "✅ Added is_read column to chat_messages table";
}
// Check if sender_id exists
$stmt = $db->query("SHOW COLUMNS FROM chat_messages LIKE 'sender_id'");
if ($stmt->rowCount() == 0) {
$db->exec("
ALTER TABLE chat_messages
ADD COLUMN sender_id INT NULL AFTER sender_type,
ADD FOREIGN KEY (sender_id) REFERENCES users(id) ON DELETE SET NULL
");
$updated[] = "✅ Added sender_id to chat_messages table";
}
}
echo "<h2 style='color: green;'>✅ Chat System Installed Successfully!</h2>";
} catch (Exception $e) {
$errors[] = $e->getMessage();
echo "<h2 style='color: red;'>❌ Installation Error</h2>";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Install Chat System</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 900px;
margin: 50px auto;
padding: 20px;
background: #f5f5f5;
}
h2, h3 {
color: #333;
}
.card {
background: white;
padding: 30px;
border-radius: 16px;
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
margin-bottom: 20px;
}
ul {
line-height: 2;
}
.success {
color: #10B981;
}
.error {
color: #EF4444;
background: #FEE2E2;
padding: 15px;
border-radius: 8px;
margin: 10px 0;
}
table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
}
table th {
background: #f9fafb;
padding: 12px;
text-align: left;
border: 1px solid #e5e7eb;
}
table td {
padding: 10px;
border: 1px solid #e5e7eb;
}
</style>
</head>
<body>
<div class="card">
<?php if (!empty($created)): ?>
<h3>✅ Tables Created:</h3>
<ul>
<?php foreach ($created as $item): ?>
<li class="success"><?php echo $item; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php if (!empty($updated)): ?>
<h3>🔄 Tables Updated:</h3>
<ul>
<?php foreach ($updated as $item): ?>
<li><?php echo $item; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php if (!empty($errors)): ?>
<h3 style="color: #EF4444;">❌ Errors:</h3>
<?php foreach ($errors as $error): ?>
<div class="error"><?php echo htmlspecialchars($error); ?></div>
<?php endforeach; ?>
<?php endif; ?>
<?php if (empty($errors)): ?>
<p style="color: #10B981; font-weight: bold; font-size: 18px; margin-top: 20px;">
✨ All done! Your chat system is ready to use.
</p>
<?php endif; ?>
</div>
<?php if (empty($errors)): ?>
<div class="card">
<h3>📋 Database Schema Created:</h3>
<h4>1. conversations Table</h4>
<table>
<tr>
<th>Column</th>
<th>Type</th>
<th>Description</th>
</tr>
<tr>
<td>id</td>
<td>INT</td>
<td>Primary key</td>
</tr>
<tr>
<td>visitor_name</td>
<td>VARCHAR(255)</td>
<td>Name of the person chatting</td>
</tr>
<tr>
<td>visitor_email</td>
<td>VARCHAR(255)</td>
<td>Email if provided</td>
</tr>
<tr>
<td>member_id</td>
<td>INT</td>
<td>Link to member_accounts if logged in</td>
</tr>
<tr>
<td>status</td>
<td>ENUM</td>
<td>active or closed</td>
</tr>
<tr>
<td>created_at</td>
<td>TIMESTAMP</td>
<td>When conversation started</td>
</tr>
<tr>
<td>updated_at</td>
<td>TIMESTAMP</td>
<td>Last message time</td>
</tr>
</table>
<h4>2. chat_messages Table</h4>
<table>
<tr>
<th>Column</th>
<th>Type</th>
<th>Description</th>
</tr>
<tr>
<td>id</td>
<td>INT</td>
<td>Primary key</td>
</tr>
<tr>
<td>conversation_id</td>
<td>INT</td>
<td>Which conversation this belongs to</td>
</tr>
<tr>
<td>sender_type</td>
<td>ENUM</td>
<td>visitor, member, or admin</td>
</tr>
<tr>
<td>sender_id</td>
<td>INT</td>
<td>Admin user ID (if admin sent)</td>
</tr>
<tr>
<td>message_text</td>
<td>TEXT</td>
<td>The actual message</td>
</tr>
<tr>
<td>is_read</td>
<td>TINYINT(1)</td>
<td>Whether admin has read it</td>
</tr>
<tr>
<td>created_at</td>
<td>TIMESTAMP</td>
<td>When message was sent</td>
</tr>
</table>
</div>
<div class="card">
<h3>🎯 How The System Works:</h3>
<h4>For Public Visitors & Members:</h4>
<ul>
<li>💬 Click <strong>blue chat button</strong> on any page</li>
<li>📋 Choose: <strong>Chat with Admin</strong> or <strong>AI Assistant</strong></li>
<li>✍️ Type message and send</li>
<li>✅ Message saved to database</li>
<li>📬 Wait for admin response</li>
</ul>
<h4>For Admins:</h4>
<ul>
<li>📥 See <strong>green inbox button</strong> with unread count</li>
<li>🔔 Button pulses when new messages arrive</li>
<li>📋 Click to see all conversations</li>
<li>🟢 Unread conversations highlighted in green</li>
<li>💬 Click conversation to view and reply</li>
<li>✅ Messages auto-marked as read</li>
</ul>
</div>
<div class="card">
<h3>🧪 Test Your Chat System:</h3>
<h4>Step 1: Test as Visitor</h4>
<ol>
<li>Open an <strong>incognito/private window</strong></li>
<li>Go to: <code>http://localhost/copmadinaarea/</code></li>
<li>Click the <strong>blue chat button</strong> (bottom right)</li>
<li>Click <strong>"Chat with Admin"</strong></li>
<li>Type: "Hello, I need help!"</li>
<li>Click send</li>
</ol>
<h4>Step 2: Respond as Admin</h4>
<ol>
<li>Come back to this admin window</li>
<li>Go to dashboard or any admin page</li>
<li>Look for <strong>green inbox button</strong> (bottom right)</li>
<li>See the <strong>unread badge</strong> showing "1"</li>
<li>Click the green button</li>
<li>See your test message</li>
<li>Click the conversation</li>
<li>Type a reply and send!</li>
</ol>
<h4>Step 3: Check Response</h4>
<ol>
<li>Go back to the incognito window</li>
<li>See the admin's reply appear!</li>
</ol>
</div>
<div class="card">
<h3>🎨 Visual Guide:</h3>
<table>
<tr>
<th>User Type</th>
<th>Button Color</th>
<th>Icon</th>
<th>What They See</th>
</tr>
<tr>
<td><strong>Visitor</strong></td>
<td>🔵 Blue/Purple</td>
<td>💬 Chat bubbles</td>
<td>Choose: Admin or AI</td>
</tr>
<tr>
<td><strong>Member</strong></td>
<td>🔵 Blue/Purple</td>
<td>💬 Chat bubbles</td>
<td>Choose: Admin or AI</td>
</tr>
<tr>
<td><strong>Admin</strong></td>
<td>🟢 Green</td>
<td>📥 Inbox</td>
<td>All member chats + unread count</td>
</tr>
</table>
</div>
<div class="card">
<h3>✨ Features Included:</h3>
<ul>
<li>✅ <strong>Real-time messaging</strong> between members and admins</li>
<li>✅ <strong>Unread counter</strong> on admin inbox button</li>
<li>✅ <strong>Pulse animation</strong> when new messages arrive</li>
<li>✅ <strong>Conversation filtering</strong> (All / Unread)</li>
<li>✅ <strong>Message history</strong> preserved per conversation</li>
<li>✅ <strong>Read receipts</strong> (auto-mark as read)</li>
<li>✅ <strong>Auto-refresh</strong> every 10-30 seconds</li>
<li>✅ <strong>Mobile responsive</strong> design</li>
<li>✅ <strong>Name tracking</strong> (visitor names shown)</li>
<li>✅ <strong>Time stamps</strong> (2m ago, 1h ago, etc.)</li>
</ul>
</div>
<hr>
<p>
<a href="dashboard.php" style="display: inline-block; background: linear-gradient(135deg, #10B981 0%, #059669 100%); color: white; padding: 12px 24px; border-radius: 8px; text-decoration: none; font-weight: bold; margin-right: 10px;">
🏠 Go to Dashboard
</a>
<a href="index.php" target="_blank" style="display: inline-block; background: linear-gradient(135deg, #1E40AF 0%, #9333EA 100%); color: white; padding: 12px 24px; border-radius: 8px; text-decoration: none; font-weight: bold;">
🧪 Test as Visitor
</a>
</p>
<p style="color: #666; margin-top: 30px; font-size: 14px;">
<strong>Note:</strong> You can delete this file (install_chat_system.php) after successful installation.
</p>
<?php endif; ?>
</body>
</html>
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists