[ Root System Explorer ]
Location:
Root
/
home
/
u456045770
/
domains
/
srmeshop.in
/
public_html
/
admin
/
chat
+ Folder
+ File
Upload
Editing: chat_actions(1).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 = $_SESSION['userdata']['id'] ?? 0; } public function __destruct(){ parent::__destruct(); } function capture_err(){ if(!$this->conn->error) return false; else{ $resp['status'] = 'failed'; $resp['error'] = $this->conn->error; return json_encode($resp); exit; } } /** * Get all conversations for the current user */ public function get_conversations() { $user_id = (int)$this->current_user_id; if ($user_id <= 0) { return json_encode(['error' => 'Not logged in']); } try { // Get all unique users that current user has chatted with $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("sss", $user_id, $user_id, $user_id); $stmt->execute(); $result = $stmt->get_result(); $conversations = []; while ($row = $result->fetch_assoc()) { $other_user_id = $row['other_user_id']; // Get user info $user_stmt = $this->conn->prepare(" SELECT id, firstname, lastname, company, contact, email, affiliate_code FROM clients WHERE id = ? "); $user_stmt->bind_param("i", $other_user_id); $user_stmt->execute(); $user_result = $user_stmt->get_result(); if ($user = $user_result->fetch_assoc()) { // 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'], 'name' => trim($user['firstname'] . ' ' . $user['lastname']), 'company' => $user['company'] ?? '', 'contact' => $user['contact'] ?? '', 'email' => $user['email'] ?? '', 'is_elite' => !empty($user['affiliate_code']), 'last_message' => $last_msg['message_text'] ?? '', 'last_message_time' => $last_msg['created_at'] ?? '', 'unread_count' => $unread_count ]; } $user_stmt->close(); } // 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']); } } /** * Get messages between current user and another user */ public function get_messages() { $client_id = (int)$this->current_user_id; $other_user_id = (int)($_GET['chat_id'] ?? 0); if ($client_id <= 0) { return json_encode(['error' => 'Not logged in']); } if ($other_user_id <= 0) { return json_encode(['error' => 'Invalid user ID']); } try { // Get messages between two users $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("ssss", $client_id, $other_user_id, $other_user_id, $client_id); $stmt->execute(); $result = $stmt->get_result(); $messages = []; while ($m = $result->fetch_assoc()) { $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' => trim(($m['firstname'] ?? '') . ' ' . ($m['lastname'] ?? '')), '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 = (int)$this->current_user_id; $receiver_id = (int)($_POST['receiver_id'] ?? 0); $msg = trim($_POST['message'] ?? ''); if ($client_id <= 0) { return json_encode(['error' => 'Not logged in']); } if ($receiver_id <= 0) { return json_encode(['error' => 'Invalid receiver']); } if (empty($msg)) { return json_encode(['error' => 'Message cannot be empty']); } try { // 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", $client_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 = (int)$this->current_user_id; $chat_id = (int)($_POST['chat_id'] ?? 0); if ($client_id <= 0) { return json_encode(['error' => 'Not logged in']); } if ($chat_id <= 0) { return json_encode(['error' => 'Invalid chat ID']); } try { $stmt = $this->conn->prepare(" UPDATE messages SET is_read = 1 WHERE sender_id = ? AND receiver_id = ? AND is_read = 0 "); $stmt->bind_param("ii", $chat_id, $client_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 = (int)$this->current_user_id; $chat_id = (int)($_POST['chat_id'] ?? 0); if ($client_id <= 0) { return json_encode(['error' => 'Not logged in']); } if ($chat_id <= 0) { return json_encode(['error' => 'Invalid chat ID']); } try { $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("iiii", $client_id, $chat_id, $chat_id, $client_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 = (int)$this->current_user_id; $chat_id = (int)($_POST['chat_id'] ?? 0); if ($client_id <= 0) { return json_encode(['error' => 'Not logged in']); } if ($chat_id <= 0) { return json_encode(['error' => 'Invalid chat ID']); } try { // 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("ii", $client_id, $chat_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("ii", $client_id, $chat_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 = (int)$this->current_user_id; $status = $_POST['status'] ?? 'offline'; if ($client_id <= 0) { return json_encode(['error' => 'Not logged in']); } try { $is_online = ($status === 'online') ? 1 : 0; $stmt = $this->conn->prepare(" UPDATE clients SET is_online = ?, last_active = NOW() WHERE id = ? "); $stmt->bind_param("ii", $is_online, $client_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 = (int)($_GET['user_id'] ?? 0); } if ($user_id <= 0) { return json_encode(['error' => 'Invalid user ID']); } try { $stmt = $this->conn->prepare(" SELECT id, firstname, lastname, CONCAT(firstname, ' ', lastname) as name, company, contact, email, is_online, last_active, affiliate_code FROM clients WHERE id = ? "); $stmt->bind_param("i", $user_id); $stmt->execute(); $result = $stmt->get_result(); if ($row = $result->fetch_assoc()) { $row['avatar_initials'] = strtoupper(substr($row['firstname'], 0, 1)); $row['is_elite'] = !empty($row['affiliate_code']); unset($row['affiliate_code']); $stmt->close(); return json_encode($row); } $stmt->close(); return json_encode(['error' => 'User not found']); } catch (Exception $e) { error_log("get_user_info error: " . $e->getMessage()); return json_encode(['error' => 'Failed to get user info']); } } /** * Search users */ public function search_users() { $client_id = (int)$this->current_user_id; $query = trim($_GET['query'] ?? $_POST['query'] ?? ''); if ($client_id <= 0) { return json_encode(['error' => 'Not logged in']); } if (empty($query)) { return json_encode([]); } try { $search_query = "%{$query}%"; $stmt = $this->conn->prepare(" SELECT id, firstname, lastname, CONCAT(firstname, ' ', lastname) as name, company, contact, email, is_online, affiliate_code FROM clients WHERE id != ? AND ( firstname LIKE ? OR lastname LIKE ? OR company LIKE ? OR contact LIKE ? OR email LIKE ? ) LIMIT 20 "); $stmt->bind_param("isssss", $client_id, $search_query, $search_query, $search_query, $search_query, $search_query); $stmt->execute(); $result = $stmt->get_result(); $users = []; while ($row = $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; } $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 = (int)$this->current_user_id; if ($client_id <= 0) { return json_encode(['error' => 'Not logged in']); } try { // 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("i", $client_id); $unread_stmt->execute(); $unread_result = $unread_stmt->get_result(); $unread_data = $unread_result->fetch_assoc(); $unread_stmt->close(); // Total conversations $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", $client_id, $client_id, $client_id); $conv_stmt->execute(); $conv_result = $conv_stmt->get_result(); $conv_data = $conv_result->fetch_assoc(); $conv_stmt->close(); // Online users count $online_stmt = $this->conn->prepare(" SELECT COUNT(*) as online_users FROM clients WHERE is_online = 1 AND id != ? "); $online_stmt->bind_param("i", $client_id); $online_stmt->execute(); $online_result = $online_stmt->get_result(); $online_data = $online_result->fetch_assoc(); $online_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' => (int)($online_data['online_users'] ?? 0) ]); } 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 = (int)$this->current_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("iiii", $client_id, $other_user_id, $other_user_id, $client_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 = (int)$this->current_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("ii", $other_user_id, $client_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 = (int)$this->current_user_id; if ($client_id > 0) { $stmt = $this->conn->prepare(" UPDATE clients SET last_active = NOW() WHERE id = ? "); $stmt->bind_param("i", $client_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