aboutsummaryrefslogtreecommitdiffstats
path: root/motd/index.php
blob: 23e0a8d161df02c1ff5bd673604fa874237ef691 (plain) (blame)
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
<?php
require_once __DIR__ . '/../partials/asset_version.php';

$motd = [
    'title' => '',
    'body' => '',
    'updated' => '',
    'rev' => 0,
    'history' => [],
];
$page_file = __DIR__ . '/motd-data.php';
$json_file = __DIR__ . '/motd.json';
if (is_file($page_file)) {
    $data = require $page_file;
    if (is_array($data)) $motd = array_merge($motd, $data);
} elseif (is_file($json_file)) {
    $raw = file_get_contents($json_file);
    $data = is_string($raw) ? json_decode($raw, true) : null;
    if (is_array($data)) $motd = array_merge($motd, $data);
}

$title = trim((string)($motd['title'] ?? ''));
$heading = trim((string)($motd['heading'] ?? ''));
if ($title === '') $title = $heading;
$body = trim((string)($motd['body'] ?? ''));
$live = $body !== '';
$updated = trim((string)($motd['updated'] ?? ''));
$rev = (int)($motd['rev'] ?? 0);
$history = is_array($motd['history'] ?? null) ? $motd['history'] : [];

$page_title = $live && $title !== ''
    ? $title . ' — SillyLaird'
    : 'MOTD — SillyLaird';
$page_desc = $live
    ? ($title !== '' ? $title . '. ' : '') . preg_replace('/\s+/', ' ', $body)
    : 'Pinned message of the day for the SillyLaird XMPP room, posted by waifu.';
if (strlen($page_desc) > 200) {
    $page_desc = substr($page_desc, 0, 197) . '...';
}

$h = static fn(string $s): string => htmlspecialchars($s, ENT_QUOTES, 'UTF-8');

$link_body = static function (string $text) use ($h): string {
    $esc = $h($text);
    return preg_replace(
        '~(https?://[^\s<]+)~',
        '<a href="$1" rel="noopener noreferrer">$1</a>',
        $esc
    ) ?? $esc;
};

$fmt_when = static function (string $iso): string {
    if ($iso === '') return '';
    try {
        $dt = new DateTimeImmutable($iso);
        return $dt->format('Y-m-d H:i T');
    } catch (Exception $e) {
        return $iso;
    }
};

$past = [];
foreach ($history as $row) {
    if (!is_array($row)) continue;
    $pbody = trim((string)($row['body'] ?? ''));
    if ($pbody === '') continue;
    $past[] = $row;
}
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width,initial-scale=1" />
  <title><?= $h($page_title) ?></title>
<?php
  $meta = [
    'lang'      => 'en',
    'title'     => $page_title,
    'desc'      => $page_desc,
    'base'      => 'motd/index',
    'self_url'  => 'https://www.sillylaird.ca/motd/',
    'en_url'    => 'https://www.sillylaird.ca/motd/',
    'ja_url'    => 'https://www.sillylaird.ca/motd/',
    'zh_url'    => 'https://www.sillylaird.ca/motd/',
    'x_default' => 'https://www.sillylaird.ca/motd/',
    'updated'   => $updated !== '' ? substr($updated, 0, 10) : '',
  ];
  include __DIR__ . '/../partials/head_meta.php';
?>
  <link rel="stylesheet" href="/assets/css/fonts.css" />
  <link rel="stylesheet" href="/assets/css/site.css" />
  <link rel="stylesheet" href="/assets/css/skeleton.css" />
  <link rel="stylesheet" href="/<?= $h(asset_v('assets/css/pages/motd.css')) ?>" />
</head>
<body>
  <a class="skip-link" href="#main">Skip to content</a>
  <?php include __DIR__ . '/../partials/header.php'; ?>

  <main id="main" class="wrap">
    <section aria-labelledby="motd-page-title">
      <h1 id="motd-page-title">MOTD</h1>
      <p class="muted">Pinned notice for the XMPP room. Type <code>!motd</code> to read it. waifu posts it to the room only when it is announced from admin.</p>

      <h2 id="motd-current-title">Current notice</h2>
      <?php if ($live): ?>
      <div class="motd-current" aria-live="polite">
        <?php if ($title !== ''): ?>
        <div class="motd-heading"><?= $h($title) ?></div>
        <?php endif; ?>
        <p class="motd-body"><?= $link_body($body) ?></p>
        <?php
          $bits = [];
          if ($updated !== '') $bits[] = 'posted ' . $fmt_when($updated);
          if ($rev > 0) $bits[] = 'rev ' . $rev;
          if ($bits):
        ?>
        <p class="muted"><?= $h(implode(' · ', $bits)) ?></p>
        <?php endif; ?>
      </div>
      <?php else: ?>
      <p class="muted">Nothing pinned right now.</p>
      <?php endif; ?>

      <h2>Room</h2>
      <ul class="contact-list">
        <li>XMPP: <a href="xmpp:based@conference.sillylaird.ca?join">based@conference.sillylaird.ca</a></li>
        <li>Bot: waifu</li>
        <li>Command: <code>!motd</code></li>
      </ul>

      <?php if ($past): ?>
      <h2 id="motd-archive-title">Previous notices</h2>
      <?php foreach ($past as $row):
          $ptitle = trim((string)($row['title'] ?? ''));
          $pbody = trim((string)($row['body'] ?? ''));
          $pwhen = trim((string)($row['updated'] ?? ''));
      ?>
      <div class="entry">
        <?php if ($pwhen !== ''): ?><time datetime="<?= $h($pwhen) ?>"><?= $h($fmt_when($pwhen)) ?></time><?php endif; ?>
        <p><?php if ($ptitle !== ''): ?><strong><?= $h($ptitle) ?></strong> <?php endif; ?><?= $link_body($pbody) ?></p>
      </div>
      <?php endforeach; ?>
      <?php endif; ?>
    </section>
  </main>

  <?php include __DIR__ . '/../partials/footer.php'; ?>
</body>
</html>