-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_send.php
More file actions
95 lines (77 loc) · 1.95 KB
/
Copy pathchat_send.php
File metadata and controls
95 lines (77 loc) · 1.95 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
<?php
require_once 'includes/bootstrap.php';
require_login();
header('Content-Type: application/json');
if (!isset($_POST['csrf_token']) || !hash_equals($_SESSION['csrf'] ?? '', $_POST['csrf_token'])) {
http_response_code(403);
echo json_encode(['error' => 'Invalid CSRF']);
exit;
}
$pdo = db();
$userId = user_id();
$ip = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
// Get character for current city
$stmt = $pdo->prepare("SELECT * FROM characters WHERE user_id = ? AND city_id = ? LIMIT 1");
$stmt->execute([$userId, current_city_id()]);
$char = $stmt->fetch();
if (!$char) {
exit;
}
// Update latest known IP
$stmt = $pdo->prepare("
UPDATE users
SET last_ip = ?
WHERE id = ?
");
$stmt->execute([$ip, $userId]);
// Check if user is muted
$stmt = $pdo->prepare("
SELECT *
FROM chat_mutes
WHERE user_id = ?
AND (
expires_at IS NULL
OR expires_at > NOW()
)
ORDER BY created_at DESC
LIMIT 1
");
$stmt->execute([$userId]);
$mute = $stmt->fetch();
if ($mute) {
http_response_code(403);
$reason = trim((string)($mute['reason'] ?? ''));
$msg = 'You are muted from chat.';
if ($reason !== '') {
$msg .= ' Reason: ' . $reason;
}
exit($msg);
}
$message = trim($_POST['message'] ?? '');
$channel = $_POST['channel'] ?? 'global';
if (!in_array($channel, ['global', 'faction', 'zone'], true)) {
$channel = 'global';
}
if ($message === '' || strlen($message) > 200) exit;
$turf_id = null;
$faction_id = null;
if ($channel === 'zone') {
$turf_id = $char['turf_id'];
} elseif ($channel === 'faction') {
$faction_id = $char['faction_id'];
}
$stmt = $pdo->prepare("
INSERT INTO chat_messages
(character_id, character_name, message, channel, turf_id, faction_id, city_id, ip_address)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
");
$stmt->execute([
$char['id'],
$char['name'],
$message,
$channel,
$turf_id,
$faction_id,
current_city_id(),
$ip
]);