File manager - Edit - /home/linknsbh/rassudr.online/chat/index.php
Back
<?php session_start(); ini_set('display_errors', 1); error_reporting(E_ALL); require 'db.php'; $route = isset($_GET['route']) ? $_GET['route'] : ''; // Widget JS file if ($route == 'widget.js') { header('Content-Type: application/javascript'); readfile('widget.js'); exit; } // API endpoints if ($route == 'api') { header('Content-Type: application/json'); header('Access-Control-Allow-Origin: *'); $action = $_GET['action'] ?? ''; if ($action == 'config') { $prop = $db->query("SELECT * FROM properties LIMIT 1")->fetch(); echo json_encode([ 'success' => true, 'color' => $prop['widget_color'] ?? '#1a73e8', 'position' => $prop['widget_position'] ?? 'bottom_right', 'welcome_message' => $prop['welcome_message'] ?? 'مرحباً! كيف يمكننا مساعدتك؟', 'offline_message' => $prop['offline_message'] ?? 'نحن غير متصلين حالياً.' ]); exit; } if ($action == 'departments') { $depts = $db->query("SELECT id, name FROM departments ORDER BY name")->fetchAll(); echo json_encode(['success' => true, 'departments' => $depts]); exit; } if ($action == 'init') { $data = json_decode(file_get_contents('php://input'), true); $session = $data['session'] ?? ''; $page = $data['page'] ?? ''; $ref = $data['ref'] ?? ''; $stmt = $db->prepare("SELECT id FROM visitors WHERE session_id = ?"); $stmt->execute([$session]); $visitor = $stmt->fetch(); if (!$visitor) { $stmt = $db->prepare("INSERT INTO visitors (session_id, ip, page_url, referrer, user_agent, last_activity) VALUES (?, ?, ?, ?, ?, datetime('now'))"); $stmt->execute([$session, $_SERVER['REMOTE_ADDR'] ?? '', $page, $ref, $_SERVER['HTTP_USER_AGENT'] ?? '']); $visitorId = $db->lastInsertId(); } else { $visitorId = $visitor['id']; $db->prepare("UPDATE visitors SET last_activity = datetime('now') WHERE id = ?")->execute([$visitorId]); } $conv = $db->prepare("SELECT id FROM conversations WHERE visitor_id = ? AND status IN ('pending', 'active')"); $conv->execute([$visitorId]); $conv = $conv->fetch(); echo json_encode(['success' => true, 'visitor_id' => $visitorId, 'conv_id' => $conv ? $conv['id'] : null]); exit; } if ($action == 'start') { $data = json_decode(file_get_contents('php://input'), true); $session = $data['session'] ?? ''; $name = trim($data['name'] ?? ''); $email = trim($data['email'] ?? ''); $dept = $data['dept'] ?? ''; $msg = trim($data['msg'] ?? ''); if (empty($name) || empty($email)) { echo json_encode(['error' => 'Name and email required']); exit; } $stmt = $db->prepare("SELECT id FROM visitors WHERE session_id = ?"); $stmt->execute([$session]); $visitor = $stmt->fetch(); if (!$visitor) { $stmt = $db->prepare("INSERT INTO visitors (session_id, ip, page_url, referrer, user_agent, last_activity) VALUES (?, ?, ?, ?, ?, datetime('now'))"); $stmt->execute([$session, $_SERVER['REMOTE_ADDR'] ?? '', '', '', $_SERVER['HTTP_USER_AGENT'] ?? '']); $visitorId = $db->lastInsertId(); } else { $visitorId = $visitor['id']; } $db->prepare("UPDATE visitors SET name = ?, email = ? WHERE id = ?")->execute([$name, $email, $visitorId]); $deptId = !empty($dept) ? (int)$dept : null; $stmt = $db->prepare("INSERT INTO conversations (visitor_id, department_id, status, email) VALUES (?, ?, 'pending', ?)"); $stmt->execute([$visitorId, $deptId, $email]); $convId = $db->lastInsertId(); $db->prepare("INSERT INTO messages (conversation_id, sender_type, sender_id, content) VALUES (?, 'visitor', ?, ?)") ->execute([$convId, $visitorId, $msg ?: 'مرحباً']); echo json_encode(['success' => true, 'conv_id' => $convId]); exit; } if ($action == 'message') { $data = json_decode(file_get_contents('php://input'), true); $convId = (int)($data['conv'] ?? 0); $msg = trim($data['msg'] ?? ''); $conv = $db->prepare("SELECT visitor_id FROM conversations WHERE id = ?"); $conv->execute([$convId]); $conv = $conv->fetch(); if ($conv) { $db->prepare("INSERT INTO messages (conversation_id, sender_type, sender_id, content) VALUES (?, 'visitor', ?, ?)") ->execute([$convId, $conv['visitor_id'], $msg]); $db->prepare("UPDATE conversations SET updated_at = datetime('now') WHERE id = ?")->execute([$convId]); } echo json_encode(['success' => true]); exit; } if ($action == 'messages') { $convId = (int)($_GET['id'] ?? 0); $since = (int)($_GET['since'] ?? 0); $stmt = $db->prepare("SELECT * FROM messages WHERE conversation_id = ? AND id > ? ORDER BY created_at"); $stmt->execute([$convId, $since]); echo json_encode(['success' => true, 'messages' => $stmt->fetchAll()]); exit; } // Check for new conversations (for agent notification) if ($action == 'check-new') { $lastCheck = (int)($_GET['last'] ?? 0); $stmt = $db->prepare("SELECT COUNT(*) FROM conversations WHERE id > ? AND status = 'pending'"); $stmt->execute([$lastCheck]); $count = $stmt->fetchColumn(); $stmt = $db->prepare("SELECT MAX(id) as max_id FROM conversations"); $stmt->execute(); $maxId = $stmt->fetchColumn() ?: 0; echo json_encode(['success' => true, 'new_count' => $count, 'max_id' => $maxId]); exit; } echo json_encode(['error' => 'Unknown action']); exit; } // Check if installed $installed = $db->query("SELECT COUNT(*) FROM users WHERE role = 'admin'")->fetchColumn() > 0; if (!$installed && $route != 'install' && $route != 'install-save') { $route = 'install'; } if ($installed && ($route == 'install' || $route == 'install-save')) { $route = 'dashboard'; } // Auth check $publicRoutes = ['install', 'install-save', 'login', 'login-check']; if (!in_array($route, $publicRoutes) && !empty($route)) { if (empty($_SESSION['admin_id'])) { header('Location: ?route=login'); exit; } } // Route to page $pageFile = 'pages/' . ($route ?: 'install') . '.php'; if (file_exists($pageFile)) { include $pageFile; } else { include 'pages/dashboard.php'; }
| ver. 1.4 |
Github
|
.
| PHP 8.2.31 | Generation time: 5.93 |
proxy
|
phpinfo
|
Settings