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
|
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/config.php';
$db = new PDO(DB_URL, DB_USER, DB_PASS);
$room = $_GET['r'] ?: null;
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("No room found.");
}
$room['encoded'] = urlencode($room['name']);
$stmt = $db->prepare('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 = ?
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([$room['id']]);
$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;
}
} else {
$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)): ?>
<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($room)): ?>
<h1><a href="/"><?= INSTANCE_NAME ?></a> - Index of <?= $room['name'] ?></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='/?r={$room['encoded']}&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>
</html>
|