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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/config.php';
$db = new PDO(DB_URL, DB_USER, DB_PASS);
$room = $_GET['r'] ?: null;
$user = $_GET['u'] ?: null;
$date = $_GET['d'] ?: null;
$limit = min(abs(intval($_GET['l'] ?? '500')), 500);
$page = abs(intval($_GET['p'] ?? '1') - 1);
$offset = $limit * $page;
if (isset($user)) {
$stmt = $db->prepare('SELECT id, `nick`, joined_at, departed_at FROM users WHERE `nick` = ?');
$stmt->execute([$user]);
$user = $stmt->fetch(PDO::FETCH_ASSOC) ?: null;
if (!$user) {
http_response_code(404);
exit("User not found.");
}
}
if (isset($room)) {
$room = urldecode($room);
$stmt = $db->prepare('SELECT id, `name`, joined_at, departed_at FROM rooms WHERE `name` = ?');
$stmt->execute([$room]);
$room = $stmt->fetch(PDO::FETCH_ASSOC) ?: null;
if (!$room) {
http_response_code(404);
exit("Room not found.");
}
$room['encoded'] = urlencode($room['name']);
}
if (isset($date)) {
$user_id = $user ? $user['id'] : "m.user_id";
$room_id = $room ? $room['id'] : "m.room_id";
$room_column = !$room ? ", r.name AS room" : "";
$stmt = $db->prepare("SELECT m.id, u.nick, m.command, m.params, m.tags, m.sent_at $room_column
FROM messages m
JOIN rooms r ON r.id = m.room_id
JOIN users u ON u.id = m.user_id
WHERE m.room_id = $room_id AND m.user_id = $user_id
AND m.sent_at BETWEEN ? AND DATE_ADD(?, INTERVAL 1 DAY)
ORDER BY sent_at DESC
LIMIT $limit OFFSET $offset
");
$stmt->execute([$date, $date]);
$messages = $stmt->fetchAll(PDO::FETCH_ASSOC);
} else if ($room || $user) {
$room_id = isset($room) ? $room['id'] : 'room_id';
$user_id = isset($user) ? $user['id'] : 'user_id';
$stmt = $db->query("SELECT YEAR(sent_at) AS msg_year,
MONTH(sent_at) AS msg_month,
DAY(sent_at) AS msg_day,
COUNT(*) AS msg_count
FROM messages
WHERE room_id = $room_id AND user_id = $user_id
GROUP BY YEAR(sent_at), MONTH(sent_at), DAY(sent_at)
ORDER BY YEAR(sent_at) DESC, MONTH(sent_at) DESC, DAY(sent_at) DESC
");
$stmt->execute();
$raw_dates = $stmt->fetchAll(PDO::FETCH_ASSOC);
$dates = [];
foreach ($raw_dates as $rm) {
$y = $rm['msg_year'];
$m = $rm['msg_month'];
$d = $rm['msg_day'];
$c = $rm['msg_count'];
if (!array_key_exists($y, $dates)) {
$dates[$y] = [];
}
if (!array_key_exists($m, $dates[$y])) {
$dates[$y][$m] = [];
}
if (!array_key_exists($d, $dates[$y][$m])) {
$dates[$y][$m] = [];
}
$dates[$y][$m][$d] = $c;
}
}
$page_title = (isset($user) ? ($user['nick'] . (isset($room) ? ' in ' : '')) : '') . (isset($room) ? "{$room['name']}" : '');
$link = match (true) {
isset($user, $room) => "r={$room['encoded']}&u={$user['nick']}",
isset($user) => "u={$user['nick']}",
isset($room) => "r={$room['encoded']}",
default => ""
};
if (!isset($user) && !isset($room)) {
$stmt = $db->query('SELECT name FROM rooms ORDER BY joined_at, departed_at DESC');
$stmt->execute();
$rooms = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
?>
<!DOCTYPE html>
<html>
<head>
<?php if (isset($room, $date)): ?>
<title><?= sprintf('Log for %s, %s - %s', $room['name'], $date, INSTANCE_NAME) ?></title>
<?php elseif (isset($room)): ?>
<title><?= $room['name'] ?> - <?= INSTANCE_NAME ?></title>
<?php else: ?>
<title>Index - <?= INSTANCE_NAME ?></title>
<?php endif; ?>
<link rel="stylesheet" href="/static/style.css">
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
</head>
<body>
<?php if (isset($messages)): ?>
<h1><?= sprintf('Log for %s, %s', $page_title, $date) ?></h1>
<?php if (empty($messages)): ?>
<p>No messages.</p>
<?php else: ?>
<p>Showing <?= count($messages) ?> messages on Page <?= $page + 1 ?></p>
<table>
<tr>
<th>Time</th>
<?php if (array_key_exists('room', $messages[0])): ?>
<th>Room</th>
<?php endif; ?>
<th>Nick</th>
<th>Message</th>
</tr>
<?php foreach ($messages as $m): ?>
<tr class="message" data-irc-tags="<?= $m['tags'] ?>" id="msgid=<?= $m['id'] ?>">
<td class="datetime"><a
href="/?<?= $link ?>&d=<?= urlencode($date) ?>#msgid=<?= $m['id'] ?>"><?= date('H:i', strtotime($m['sent_at'])) ?></a>
</td>
<?php if (array_key_exists('room', $m)): ?>
<td class="room"><a href="/?r=<?= urlencode($m['room']) ?>"><?= $m['room'] ?></a></td>
<?php endif; ?>
<td class="nick">
<?= (!isset($prev_msg_nick) || $prev_msg_nick != $m['nick']) ? $m['nick'] : '' ?>
</td>
<td class="content"><?= $m['params'] ?></td>
</tr>
<?php $prev_msg_nick = $m['nick']; ?>
<?php endforeach; ?>
</table>
<?php endif; ?>
<?php elseif (isset($dates)): ?>
<h1><a href="/"><?= INSTANCE_NAME ?></a> - Index of <?= $page_title ?></h1>
<div class="calendar-wrapper">
<?php foreach ($dates as $year => $months): ?>
<?php foreach ($months as $month => $days): ?>
<?php
$fd = new DateTime("$year-$month-01");
$dm = (int) $fd->format('t');
$sw = (int) $fd->format('N');
$ms = $fd->format('M');
?>
<table class="calendar">
<caption><?= "{$ms} {$year}" ?></caption>
<tr>
<th>Mo</th>
<th>Tu</th>
<th>We</th>
<th>Th</th>
<th>Fr</th>
<th>Sa</th>
<th>Su</th>
</tr>
<tr>
<?php
for ($i = 1; $i < $sw; $i++)
echo '<td></td>';
for ($day = 1; $day <= $dm; $day++) {
echo '<td>';
if (array_key_exists($day, $days) && $days[$day] > 0) {
echo "<a href='/?$link&d=$year-$month-$day'>$day</a>";
} else {
echo $day;
}
echo '</td>';
if ((($day + $sw - 1) % 7) == 0 && $day != $dm)
echo '</tr><tr>';
}
$remainingdays = (7 - (($dm + $sw - 1) % 7)) % 7;
for ($i = 0; $i < $remainingdays; $i++)
echo '<td></td>';
?>
</tr>
</table>
<?php endforeach; ?>
<?php endforeach; ?>
</div>
<?php elseif (isset($rooms)): ?>
<h1>Index of <?= INSTANCE_NAME ?></h1>
<?php if (empty($rooms)): ?>
<p>There are no rooms. <a href="/mod/">Add one!</a></p>
<?php else: ?>
<table>
<tr>
<th>Room</th>
<th>Last message</th>
</tr>
<?php foreach ($rooms as $r): ?>
<tr>
<td><a href="/?r=<?= urlencode($r['name']) ?>"><?= $r['name'] ?></a></td>
<td><?= '1 minute ago' ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<?php endif; ?>
</body>
<?php if (isset($messages)): ?>
<script>
const messages = document.querySelectorAll(".message");
for (const m of messages) {
let tags = m.getAttribute("data-irc-tags");
const nickElement = m.querySelector(".nick");
const contentElement = m.querySelector(".content");
let color = null;
for (const tag of tags.split(";")) {
const p = tag.split("=");
if (p[0] == "color") {
color = p[1];
break;
}
}
if (color) {
nickElement.style.color = color;
if (contentElement.textContent.startsWith("ACTION")) {
contentElement.textContent = contentElement.textContent.substring(7);
contentElement.style.color = color;
contentElement.style.fontStyle = 'italic';
}
}
}
</script>
<?php endif; ?>
</html>
|