-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessages.php
More file actions
106 lines (98 loc) · 4.02 KB
/
messages.php
File metadata and controls
106 lines (98 loc) · 4.02 KB
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
<?php
require 'config.php';
$user = current_user();
if (!$user) header('Location: login.php');
// Получаем список диалогов
$stmt = $pdo->prepare("
SELECT u.id, u.username, u.display_name,
MAX(m.created_at) as last_msg_time,
(SELECT content FROM messages WHERE (from_user_id = ? AND to_user_id = u.id) OR (from_user_id = u.id AND to_user_id = ?) ORDER BY created_at DESC LIMIT 1) as last_msg
FROM messages m
JOIN users u ON (u.id = m.from_user_id OR u.id = m.to_user_id)
WHERE (m.from_user_id = ? OR m.to_user_id = ?) AND u.id != ?
GROUP BY u.id
ORDER BY last_msg_time DESC
");
$stmt->execute([$user['id'], $user['id'], $user['id'], $user['id'], $user['id']]);
$chats = $stmt->fetchAll();
// Если выбран чат
$chat_with = null;
$messages = [];
if (isset($_GET['with'])) {
$chat_with_id = (int)$_GET['with'];
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ? AND is_banned = 0");
$stmt->execute([$chat_with_id]);
$chat_with = $stmt->fetch();
if ($chat_with) {
// Отмечаем сообщения как прочитанные
$pdo->prepare("UPDATE messages SET is_read = 1 WHERE to_user_id = ? AND from_user_id = ?")->execute([$user['id'], $chat_with_id]);
// Получаем историю
$stmt = $pdo->prepare("
SELECT m.*, u.username
FROM messages m
JOIN users u ON u.id = m.from_user_id
WHERE (m.from_user_id = ? AND m.to_user_id = ?) OR (m.from_user_id = ? AND m.to_user_id = ?)
ORDER BY m.created_at ASC
");
$stmt->execute([$user['id'], $chat_with_id, $chat_with_id, $user['id']]);
$messages = $stmt->fetchAll();
}
}
// Отправка сообщения
if ($_POST['send_message'] && $chat_with) {
$content = trim($_POST['content']);
if ($content) {
$stmt = $pdo->prepare("INSERT INTO messages (from_user_id, to_user_id, content) VALUES (?, ?, ?)");
$stmt->execute([$user['id'], $chat_with['id'], $content]);
header("Location: messages.php?with={$chat_with['id']}");
exit;
}
}
?>
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title>Сообщения — TweeX</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<div class="card">
<h2>Сообщения</h2>
<div class="nav">
<a href="index.php">Лента</a>
<a href="profile.php">Профиль</a>
<?php if (is_admin($user)): ?><a href="admin.php">Админка</a><?php endif; ?>
<a href="logout.php">Выйти</a>
</div>
</div>
<div class="card chat-list">
<h3>Диалоги</h3>
<?php foreach ($chats as $chat): ?>
<a href="messages.php?with=<?= $chat['id'] ?>" style="color:#fff; display:block; padding:10px; border-bottom:1px solid #333;">
<strong><?= htmlspecialchars($chat['display_name'] ?: $chat['username']) ?></strong><br>
<small><?= htmlspecialchars($chat['last_msg'] ?? 'Нет сообщений') ?></small>
</a>
<?php endforeach; ?>
</div>
<?php if ($chat_with): ?>
<div class="card">
<h3>Чат с @<?= htmlspecialchars($chat_with['username']) ?></h3>
<div>
<?php foreach ($messages as $msg): ?>
<div class="chat-message <?= $msg['from_user_id'] == $user['id'] ? 'me' : '' ?>">
<?= nl2br(htmlspecialchars($msg['content'])) ?>
<br><time><?= date('H:i d.m', strtotime($msg['created_at'])) ?></time>
</div>
<?php endforeach; ?>
</div>
<form method="POST">
<textarea name="content" placeholder="Напишите сообщение..." required></textarea>
<button type="submit" name="send_message">Отправить</button>
</form>
</div>
<?php endif; ?>
</div>
</body>
</html>