-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_messages.php
100 lines (79 loc) · 2.81 KB
/
load_messages.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
<?php
include 'db_connect.php';
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$messages_per_page = 20;
$offset = ($page - 1) * $messages_per_page;
// 获取所有被封禁的用户特征码
$banned_result = $conn->query("SELECT user_code FROM banned_users");
$banned_users = array();
while ($row = $banned_result->fetch_assoc()) {
$banned_users[] = $row['user_code'];
}
function getReplies($messageId, $level = 1) {
global $conn, $banned_users;
if ($level > 3) {
return [];
}
$sql = "SELECT * FROM messages WHERE parent_id = ?";
if (count($banned_users) > 0) {
$placeholders = implode(',', array_fill(0, count($banned_users), '?'));
$sql .= " AND user_code NOT IN ($placeholders)";
}
$sql .= " ORDER BY timestamp ASC";
$stmt = $conn->prepare($sql);
if (count($banned_users) > 0) {
$params = array_merge([$messageId], $banned_users);
$types = str_repeat('s', count($banned_users));
$stmt->bind_param("i$types", ...$params);
} else {
$stmt->bind_param('i', $messageId);
}
$stmt->execute();
$result = $stmt->get_result();
$replies = array();
while ($row = $result->fetch_assoc()) {
$replies[] = array_merge($row, ['replies' => getReplies($row['id'], $level + 1)]);
}
return $replies;
}
$banned_users_placeholder = implode(',', array_fill(0, count($banned_users), '?'));
$sql = "SELECT * FROM messages WHERE parent_id IS NULL";
if (count($banned_users) > 0) {
$sql .= " AND user_code NOT IN ($banned_users_placeholder)";
}
$sql .= " ORDER BY timestamp DESC LIMIT ? OFFSET ?";
$stmt = $conn->prepare($sql);
if (count($banned_users) > 0) {
$params = array_merge($banned_users, [$messages_per_page, $offset]);
$stmt->bind_param(str_repeat('s', count($banned_users)) . 'ii', ...$params);
} else {
$stmt->bind_param('ii', $messages_per_page, $offset);
}
$stmt->execute();
$result = $stmt->get_result();
$messages = array();
while ($row = $result->fetch_assoc()) {
$messages[] = array_merge($row, ['replies' => getReplies($row['id'])]);
}
// 获取一下总共的post数量
$count_sql = "SELECT COUNT(*) as total FROM messages WHERE parent_id IS NULL";
if (count($banned_users) > 0) {
$count_sql .= " AND user_code NOT IN ($banned_users_placeholder)";
}
$count_stmt = $conn->prepare($count_sql);
if (count($banned_users) > 0) {
$count_stmt->bind_param(str_repeat('s', count($banned_users)), ...$banned_users);
}
$count_stmt->execute();
$count_result = $count_stmt->get_result();
$total_messages = $count_result->fetch_assoc()['total'];
$response = array(
'messages' => $messages,
'total_messages' => $total_messages,
'messages_per_page' => $messages_per_page,
'current_page' => $page
);
header('Content-Type: application/json');
echo json_encode($response);
$conn->close();
?>