[ Root System Explorer ]
Location:
Root
/
home
/
u456045770
/
domains
/
srmeshop.in
/
public_html
/
admin
/
chat
+ Folder
+ File
Upload
Editing: chat_actions.php
<?php require_once('../config.php'); class Chat extends DBConnection { private $settings; private $current_user_id; public function __construct(){ global $_settings; $this->settings = $_settings; parent::__construct(); // Get current user ID from session $this->current_user_id = $this->get_user_id(); } public function __destruct(){ parent::__destruct(); } /** * Get user ID - all admins use 'admin', regular users use numeric ID */ private function get_user_id() { $user_id = $_SESSION['userdata']['id'] ?? 0; $login_type = $_SESSION['userdata']['login_type'] ?? 0; // If admin (login_type 54-58), use 'admin' as ID $admin_types = [54, 55, 56, 57, 58]; if (in_array($login_type, $admin_types)) { return 'admin'; } else { return (int)$user_id; } } /** * Check if ID is admin */ private function is_admin_id($id) { return $id === 'admin'; } /** * Get user info from ID */ private function get_user_info_by_id($id) { if ($this->is_admin_id($id)) { // For admin, we need to get admin details // Get the specific admin who's logged in OR a generic admin $admin_id = $_SESSION['userdata']['id'] ?? 0; if ($admin_id > 0) { $stmt = $this->conn->prepare(" SELECT id, firstname, lastname, role as company FROM users WHERE id = ? "); $stmt->bind_param("i", $admin_id); $stmt->execute(); $result = $stmt->get_result(); $user = $result->fetch_assoc(); $stmt->close(); if ($user) { $user['id'] = 'admin'; // Always use 'admin' as ID $user['email'] = $user['email'] ?? 'youtrips@gmail.com'; $user['contact'] = $user['contact'] ?? '9958182242'; return $user; } } // Fallback if admin not found return [ 'id' => 'admin', 'firstname' => 'Admin', 'lastname' => 'User', 'email' => 'youtrips@gmail.com', 'contact' => '9958182242', 'company' => 'Youtrips' ]; } else { $numeric_id = (int)$id; $stmt = $this->conn->prepare(" SELECT id, firstname, lastname, email, contact, company, affiliate_code FROM clients WHERE id = ? "); $stmt->bind_param("i", $numeric_id); $stmt->execute(); $result = $stmt->get_result(); $user = $result->fetch_assoc(); $stmt->close(); return $user; } } /** * Get all conversations for the current user */ public function get_conversations() { $user_id = $this->current_user_id; if (!$user_id) { return json_encode(['error' => 'Not logged in']); } try { // For ADMIN: Get all unique users that admin has chatted with if ($this->is_admin_id($user_id)) { $stmt = $this->conn->prepare(" SELECT DISTINCT CASE WHEN sender_id = 'admin' THEN receiver_id ELSE sender_id END as other_user_id FROM messages WHERE (sender_id = 'admin' OR receiver_id = 'admin') AND is_deleted = 0 "); $stmt->execute(); } // For REGULAR USER: Get all unique users that user has chatted with else { $numeric_id = (int)$user_id; $stmt = $this->conn->prepare(" SELECT DISTINCT CASE WHEN sender_id = ? THEN receiver_id ELSE sender_id END as other_user_id FROM messages WHERE (sender_id = ? OR receiver_id = ?) AND is_deleted = 0 "); $stmt->bind_param("iii", $numeric_id, $numeric_id, $numeric_id); $stmt->execute(); } $result = $stmt->get_result(); $conversations = []; while ($row = $result->fetch_assoc()) { $other_user_id = $row['other_user_id']; // Skip if other_user_id is same as current user if ($other_user_id == $user_id) { continue; } // Get user info $user = $this->get_user_info_by_id($other_user_id); if ($user) { // Get last message $last_msg = $this->get_last_message($other_user_id); // Get unread count $unread_count = $this->get_unread_count($other_user_id); $conversations[] = [ 'id' => $user['id'], // This will be either number or 'admin' 'name' => trim($user['firstname'] . ' ' . $user['lastname']), 'company' => $user['company'] ?? '', 'contact' => $user['contact'] ?? '', 'email' => $user['email'] ?? '', 'is_elite' => isset($user['affiliate_code']) && !empty($user['affiliate_code']), 'last_message' => $last_msg['message_text'] ?? '', 'last_message_time' => $last_msg['created_at'] ?? '', 'unread_count' => $unread_count ]; } } // Sort by last message time (newest first) usort($conversations, function($a, $b) { $timeA = strtotime($a['last_message_time'] ?? '1970-01-01'); $timeB = strtotime($b['last_message_time'] ?? '1970-01-01'); return $timeB - $timeA; }); $stmt->close(); return json_encode($conversations); } catch (Exception $e) { error_log("get_conversations error: " . $e->getMessage()); return json_encode(['error' => 'Failed to load conversations']); } } public function get_messages() { $client_id = $this->current_user_id; $other_user_id = ($_GET['chat_id'] ?? 0); if (!$client_id) { return json_encode(['error' => 'Not logged in']); } if (!$other_user_id) { return json_encode(['error' => 'Invalid user ID']); } try { // For admin, we need to handle differently if ($this->is_admin_id($client_id)) { // Admin getting messages with a client $stmt = $this->conn->prepare(" SELECT m.*, c.firstname, c.lastname FROM messages m LEFT JOIN clients c ON m.sender_id = c.id WHERE ((m.sender_id = 'admin' AND m.receiver_id = ?) OR (m.sender_id = ? AND m.receiver_id = 'admin')) AND m.is_deleted = 0 ORDER BY m.created_at ASC "); $stmt->bind_param("ii", $other_user_id, $other_user_id); } else { // Client getting messages with admin or another client $numeric_id = (int)$client_id; if ($this->is_admin_id($other_user_id)) { // Client getting messages with admin $stmt = $this->conn->prepare(" SELECT m.*, 'Admin' as firstname, 'User' as lastname FROM messages m WHERE ((m.sender_id = ? AND m.receiver_id = 'admin') OR (m.sender_id = 'admin' AND m.receiver_id = ?)) AND m.is_deleted = 0 ORDER BY m.created_at ASC "); $stmt->bind_param("ii", $numeric_id, $numeric_id); } else { // Client getting messages with another client $other_numeric_id = (int)$other_user_id; $stmt = $this->conn->prepare(" SELECT m.*, c.firstname, c.lastname FROM messages m LEFT JOIN clients c ON m.sender_id = c.id WHERE ((m.sender_id = ? AND m.receiver_id = ?) OR (m.sender_id = ? AND m.receiver_id = ?)) AND m.is_deleted = 0 ORDER BY m.created_at ASC "); $stmt->bind_param("iiii", $numeric_id, $other_numeric_id, $other_numeric_id, $numeric_id); } } $stmt->execute(); $result = $stmt->get_result(); $messages = []; while ($m = $result->fetch_assoc()) { // Determine sender name if ($m['sender_id'] == 'admin') { $sender_name = 'Admin User'; } else if (isset($m['firstname']) && isset($m['lastname'])) { $sender_name = trim($m['firstname'] . ' ' . $m['lastname']); } else { $sender_name = 'Unknown User'; } $messages[] = [ 'id' => $m['id'], 'message_text' => $m['message_text'] ?? '', 'created_at' => $m['created_at'] ?? date('Y-m-d H:i:s'), 'sender_id' => $m['sender_id'], 'receiver_id' => $m['receiver_id'], 'sender_name' => $sender_name, 'is_read' => (bool)($m['is_read'] ?? false) ]; } $stmt->close(); return json_encode($messages); } catch (Exception $e) { error_log("get_messages error: " . $e->getMessage()); return json_encode(['error' => 'Failed to load messages']); } } /** * Send a new message */ public function send_message() { $client_id = $this->current_user_id; $receiver_id = ($_POST['receiver_id'] ?? 0); $msg = trim($_POST['message'] ?? ''); if (!$client_id) { return json_encode(['error' => 'Not logged in']); } if (!$receiver_id) { return json_encode(['error' => 'Invalid receiver']); } if (empty($msg)) { return json_encode(['error' => 'Message cannot be empty']); } try { // Ensure admin ID is stored as 'admin' string $sender_id = $this->is_admin_id($client_id) ? 'admin' : $client_id; $receiver_id = $this->is_admin_id($receiver_id) ? 'admin' : $receiver_id; // Insert message $stmt = $this->conn->prepare(" INSERT INTO messages (sender_id, receiver_id, message_text, is_read, created_at) VALUES (?, ?, ?, 0, NOW()) "); $stmt->bind_param("sss", $sender_id, $receiver_id, $msg); $stmt->execute(); $message_id = $stmt->insert_id; $stmt->close(); // Update user's last active time $this->update_last_active(); return json_encode([ 'success' => true, 'message' => 'Message sent successfully', 'message_id' => $message_id ]); } catch (Exception $e) { error_log("send_message error: " . $e->getMessage()); return json_encode(['error' => 'Failed to send message']); } } /** * Mark messages as read for a specific chat */ public function mark_as_read() { $client_id = $this->current_user_id; $chat_id = $_POST['chat_id'] ?? ''; if (!$client_id) { return json_encode(['error' => 'Not logged in']); } if (!$chat_id) { return json_encode(['error' => 'Invalid chat ID']); } try { // Convert IDs properly $receiver_id = $this->is_admin_id($client_id) ? 'admin' : $client_id; $sender_id = $this->is_admin_id($chat_id) ? 'admin' : $chat_id; $stmt = $this->conn->prepare(" UPDATE messages SET is_read = 1 WHERE sender_id = ? AND receiver_id = ? AND is_read = 0 "); $stmt->bind_param("ss", $sender_id, $receiver_id); $stmt->execute(); $affected = $stmt->affected_rows; $stmt->close(); return json_encode([ 'success' => true, 'message' => 'Messages marked as read', 'affected_rows' => $affected ]); } catch (Exception $e) { error_log("mark_as_read error: " . $e->getMessage()); return json_encode(['error' => 'Failed to mark as read']); } } /** * Clear all messages in a chat (soft delete) */ public function clear_chat() { $client_id = $this->current_user_id; $chat_id = $_POST['chat_id'] ?? ''; if (!$client_id) { return json_encode(['error' => 'Not logged in']); } if (!$chat_id) { return json_encode(['error' => 'Invalid chat ID']); } try { // Convert IDs properly $user_id = $this->is_admin_id($client_id) ? 'admin' : $client_id; $other_id = $this->is_admin_id($chat_id) ? 'admin' : $chat_id; $stmt = $this->conn->prepare(" UPDATE messages SET is_deleted = 1 WHERE (sender_id = ? AND receiver_id = ?) OR (sender_id = ? AND receiver_id = ?) "); $stmt->bind_param("ssss", $user_id, $other_id, $other_id, $user_id); $stmt->execute(); $affected = $stmt->affected_rows; $stmt->close(); return json_encode([ 'success' => true, 'message' => 'Chat cleared successfully', 'affected_rows' => $affected ]); } catch (Exception $e) { error_log("clear_chat error: " . $e->getMessage()); return json_encode(['error' => 'Failed to clear chat']); } } /** * Delete chat (permanently remove) */ public function delete_chat() { return $this->clear_chat(); // Same as clear for now } /** * Block a user */ public function block_chat() { $client_id = $this->current_user_id; $chat_id = $_POST['chat_id'] ?? ''; if (!$client_id) { return json_encode(['error' => 'Not logged in']); } if (!$chat_id) { return json_encode(['error' => 'Invalid chat ID']); } try { // Don't allow blocking admin if ($this->is_admin_id($chat_id)) { return json_encode(['error' => 'Cannot block admin']); } // Convert IDs properly $user_id = $this->is_admin_id($client_id) ? 'admin' : $client_id; $other_id = $this->is_admin_id($chat_id) ? 'admin' : $chat_id; // Check if already blocked $check_stmt = $this->conn->prepare(" SELECT id FROM blocked_users WHERE user_id = ? AND blocked_user_id = ? "); $check_stmt->bind_param("ss", $user_id, $other_id); $check_stmt->execute(); $check_result = $check_stmt->get_result(); if ($check_result->num_rows > 0) { $check_stmt->close(); return json_encode(['error' => 'User is already blocked']); } $check_stmt->close(); // Block the user $stmt = $this->conn->prepare(" INSERT INTO blocked_users (user_id, blocked_user_id, created_at) VALUES (?, ?, NOW()) "); $stmt->bind_param("ss", $user_id, $other_id); $stmt->execute(); $stmt->close(); return json_encode([ 'success' => true, 'message' => 'User blocked successfully' ]); } catch (Exception $e) { error_log("block_chat error: " . $e->getMessage()); return json_encode(['error' => 'Failed to block user']); } } /** * Update user's online status */ public function update_status() { $client_id = $this->current_user_id; $status = $_POST['status'] ?? 'offline'; if (!$client_id) { return json_encode(['error' => 'Not logged in']); } try { $is_online = ($status === 'online') ? 1 : 0; if ($this->is_admin_id($client_id)) { // For admin, we need to update all admins or just mark admin as online // Let's update the specific admin who's logged in $admin_id = $_SESSION['userdata']['id'] ?? 0; $stmt = $this->conn->prepare(" UPDATE admin SET is_online = ?, last_active = NOW() WHERE id = ? "); $stmt->bind_param("ii", $is_online, $admin_id); } else { $numeric_id = (int)$client_id; $stmt = $this->conn->prepare(" UPDATE clients SET is_online = ?, last_active = NOW() WHERE id = ? "); $stmt->bind_param("ii", $is_online, $numeric_id); } $stmt->execute(); $stmt->close(); return json_encode([ 'success' => true, 'message' => 'Status updated' ]); } catch (Exception $e) { error_log("update_status error: " . $e->getMessage()); return json_encode(['error' => 'Failed to update status']); } } /** * Get user information */ public function get_user_info($user_id = null) { if (!$user_id) { $user_id = $_GET['user_id'] ?? ''; } if (!$user_id) { return json_encode(['error' => 'Invalid user ID']); } $user = $this->get_user_info_by_id($user_id); if ($user) { $user['avatar_initials'] = strtoupper(substr($user['firstname'], 0, 1)); $user['is_elite'] = isset($user['affiliate_code']) && !empty($user['affiliate_code']); unset($user['affiliate_code']); return json_encode($user); } return json_encode(['error' => 'User not found']); } /** * Search users */ public function search_users() { $client_id = $this->current_user_id; $query = trim($_GET['query'] ?? $_POST['query'] ?? ''); if (!$client_id) { return json_encode(['error' => 'Not logged in']); } if (empty($query)) { return json_encode([]); } try { $search_query = "%{$query}%"; $users = []; // If searching for admin or admin-related terms, add admin if (strpos(strtolower($query), 'admin') !== false) { $users[] = [ 'id' => 'admin', 'firstname' => 'Admin', 'lastname' => 'User', 'email' => '', 'contact' => '', 'company' => 'Administrator', 'is_elite' => false, 'avatar_initials' => 'A' ]; } // Search in clients table $client_stmt = $this->conn->prepare(" SELECT id, firstname, lastname, email, contact, company, affiliate_code, is_online FROM clients WHERE firstname LIKE ? OR lastname LIKE ? OR company LIKE ? OR contact LIKE ? OR email LIKE ? LIMIT 10 "); $client_stmt->bind_param("sssss", $search_query, $search_query, $search_query, $search_query, $search_query ); $client_stmt->execute(); $client_result = $client_stmt->get_result(); while ($row = $client_result->fetch_assoc()) { $row['avatar_initials'] = strtoupper(substr($row['firstname'], 0, 1)); $row['is_elite'] = !empty($row['affiliate_code']); unset($row['affiliate_code']); $users[] = $row; } $client_stmt->close(); return json_encode($users); } catch (Exception $e) { error_log("search_users error: " . $e->getMessage()); return json_encode(['error' => 'Failed to search users']); } } /** * Get chat statistics (unread count, total chats, etc.) */ public function get_stats() { $client_id = $this->current_user_id; if (!$client_id) { return json_encode(['error' => 'Not logged in']); } try { // Convert ID properly for query $user_id = $this->is_admin_id($client_id) ? 'admin' : $client_id; // Total unread messages $unread_stmt = $this->conn->prepare(" SELECT COUNT(*) as total_unread FROM messages WHERE receiver_id = ? AND is_read = 0 AND is_deleted = 0 "); $unread_stmt->bind_param("s", $user_id); $unread_stmt->execute(); $unread_result = $unread_stmt->get_result(); $unread_data = $unread_result->fetch_assoc(); $unread_stmt->close(); // Total conversations if ($this->is_admin_id($client_id)) { $conv_stmt = $this->conn->prepare(" SELECT COUNT(DISTINCT CASE WHEN sender_id = 'admin' THEN receiver_id ELSE sender_id END ) as total_conversations FROM messages WHERE (sender_id = 'admin' OR receiver_id = 'admin') AND is_deleted = 0 "); $conv_stmt->execute(); } else { $numeric_id = (int)$client_id; $conv_stmt = $this->conn->prepare(" SELECT COUNT(DISTINCT CASE WHEN sender_id = ? THEN receiver_id ELSE sender_id END ) as total_conversations FROM messages WHERE (sender_id = ? OR receiver_id = ?) AND is_deleted = 0 "); $conv_stmt->bind_param("iii", $numeric_id, $numeric_id, $numeric_id); $conv_stmt->execute(); } $conv_result = $conv_stmt->get_result(); $conv_data = $conv_result->fetch_assoc(); $conv_stmt->close(); return json_encode([ 'success' => true, 'total_unread' => (int)($unread_data['total_unread'] ?? 0), 'total_conversations' => (int)($conv_data['total_conversations'] ?? 0), 'online_users' => 0 // Not tracking online status in this simplified version ]); } catch (Exception $e) { error_log("get_stats error: " . $e->getMessage()); return json_encode(['error' => 'Failed to get chat statistics']); } } /** * Default action */ public function index() { return json_encode(['error' => 'Invalid action']); } /** * PRIVATE HELPER FUNCTIONS */ private function get_last_message($other_user_id) { try { $client_id = $this->current_user_id; // Convert IDs properly $user_id = $this->is_admin_id($client_id) ? 'admin' : $client_id; $other_id = $this->is_admin_id($other_user_id) ? 'admin' : $other_user_id; $stmt = $this->conn->prepare(" SELECT message_text, created_at FROM messages WHERE ((sender_id = ? AND receiver_id = ?) OR (sender_id = ? AND receiver_id = ?)) AND is_deleted = 0 ORDER BY created_at DESC LIMIT 1 "); $stmt->bind_param("ssss", $user_id, $other_id, $other_id, $user_id ); $stmt->execute(); $result = $stmt->get_result(); if ($row = $result->fetch_assoc()) { $stmt->close(); return $row; } $stmt->close(); return null; } catch (Exception $e) { error_log("get_last_message error: " . $e->getMessage()); return null; } } private function get_unread_count($other_user_id) { try { $client_id = $this->current_user_id; // Convert IDs properly $receiver_id = $this->is_admin_id($client_id) ? 'admin' : $client_id; $sender_id = $this->is_admin_id($other_user_id) ? 'admin' : $other_user_id; $stmt = $this->conn->prepare(" SELECT COUNT(*) as unread_count FROM messages WHERE sender_id = ? AND receiver_id = ? AND is_read = 0 AND is_deleted = 0 "); $stmt->bind_param("ss", $sender_id, $receiver_id); $stmt->execute(); $result = $stmt->get_result(); if ($row = $result->fetch_assoc()) { $stmt->close(); return (int)$row['unread_count']; } $stmt->close(); return 0; } catch (Exception $e) { error_log("get_unread_count error: " . $e->getMessage()); return 0; } } private function update_last_active() { try { $client_id = $this->current_user_id; if ($client_id) { if ($this->is_admin_id($client_id)) { // Update the specific admin who's logged in $admin_id = $_SESSION['userdata']['id'] ?? 0; $stmt = $this->conn->prepare(" UPDATE admin SET last_active = NOW() WHERE id = ? "); $stmt->bind_param("i", $admin_id); } else { $numeric_id = (int)$client_id; $stmt = $this->conn->prepare(" UPDATE clients SET last_active = NOW() WHERE id = ? "); $stmt->bind_param("i", $numeric_id); } $stmt->execute(); $stmt->close(); } } catch (Exception $e) { error_log("update_last_active error: " . $e->getMessage()); } } } $chat = new Chat(); // Determine action from either GET or POST if ($_SERVER['REQUEST_METHOD'] === 'POST') { $action = $_POST['action'] ?? 'index'; } else { $action = $_GET['action'] ?? 'index'; } // Handle the action switch ($action) { case 'get_conversations': echo $chat->get_conversations(); break; case 'get_messages': echo $chat->get_messages(); break; case 'send_message': echo $chat->send_message(); break; case 'mark_as_read': echo $chat->mark_as_read(); break; case 'clear_chat': echo $chat->clear_chat(); break; case 'delete_chat': echo $chat->delete_chat(); break; case 'block_chat': echo $chat->block_chat(); break; case 'update_status': echo $chat->update_status(); break; case 'get_user_info': echo $chat->get_user_info(); break; case 'search_users': echo $chat->search_users(); break; case 'get_stats': echo $chat->get_stats(); break; default: echo $chat->index(); break; } ?>
SAVE CHANGES
[ CANCEL ]
Name
Type
Actions
.. (Parent Directory)
📄 chat_actions(1).php
FILE
Ren
[EDIT]
DEL
📄 chat_actions.php
FILE
Ren
[EDIT]
DEL
📄 chats.php
FILE
Ren
[EDIT]
DEL
📄 messages(1).php
FILE
Ren
[EDIT]
DEL
📄 messages(2).php
FILE
Ren
[EDIT]
DEL
📄 messages.php
FILE
Ren
[EDIT]
DEL
📄 test.html
FILE
Ren
[EDIT]
DEL