-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetconversations.php
111 lines (89 loc) · 3.08 KB
/
getconversations.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
include 'header.php';
if (!isset($_SESSION["loggedinuser"])) {
header("location: user.php");
exit; // Stop further execution of the code
}
// Establish the database connection
$conn = new mysqli("$servernamesql", "$usernamesql", "$passwordsql", "$databasesql");
// Check the database connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Retrieve list of conversations for the current user from the database
$currentUserId = $_SESSION['userid'] ?? null; // Replace with your actual logic to get the current user ID
// Validate and sanitize the current user ID
$currentUserId = filter_var($currentUserId, FILTER_VALIDATE_INT);
if ($currentUserId === false) {
// Invalid user ID
$response = array(
'success' => false,
'error' => 'Invalid user ID'
);
// Set the response header to JSON
header('Content-Type: application/json');
// Send the JSON response
echo json_encode($response);
exit();
}
$query = "SELECT DISTINCT
c.conversation_id,
CASE
WHEN c.recipient_id = ? THEN u_sender.username
WHEN c.sender_id = ? THEN u_recipient.username
END AS other_username,
'accepted' AS status,
u_recipient.username as recipient_username,
c.created_at
FROM
conversations AS c
LEFT JOIN friends AS f ON (
(c.recipient_id = ? AND f.receiver_id = ? AND f.status = ?) OR
(c.sender_id = ? AND f.sender_id = ? AND f.status = ?)
)
INNER JOIN users AS u_recipient ON c.recipient_id = u_recipient.userid
INNER JOIN users AS u_sender ON c.sender_id = u_sender.userid
WHERE BINARY
(c.recipient_id = ? OR c.sender_id = ?)
ORDER BY
recipient_username ASC;
";
$status = "accepted";
$username = $_SESSION['loggedinuser'];
$stmt = $conn->prepare($query);
// Bind the parameters with the correct number of placeholders
$stmt->bind_param('iiiisiisii',
$currentUserId, $currentUserId, $currentUserId,
$currentUserId, $status, $currentUserId, $currentUserId,
$status, $currentUserId, $currentUserId
);
// Print the filled-in query (for debugging purposes)
//echo "Filled-in Query: " . $query . " with parameters: " . $currentUserId . ', ' . $currentUserId . ', ' . $currentUserId . ', ' . $currentUserId . ', ' . $currentUserId . ', ' . $currentUserId . ', ' . $currentUserId . ', ' . $currentUserId . ', ' . $username . ', ' . $currentUserId . ', ' . $username . ', ' . $currentUserId . ', ' . $status;
// Execute the statement
$stmt->execute();
// Get the result
$result = $stmt->get_result();
// Check if any conversations were found
if ($result->num_rows > 0) {
// Fetch conversations as an associative array
$conversations = $result->fetch_all(MYSQLI_ASSOC);
// Return the conversations as a JSON response
$response = array(
'success' => true,
'conversations' => $conversations,
);
} else {
// No conversations found
$response = array(
'success' => false
);
}
// Close the statement
$stmt->close();
// Close the database connection
$conn->close();
// Set the response header to JSON
header('Content-Type: application/json');
// Send the JSON response
echo json_encode($response);
?>