Files
0xa0.dev/status.php
T
2026-07-22 16:35:17 +02:00

85 lines
3.3 KiB
PHP

<?php
require_once __DIR__ . '/config.php';
$cacheFile = DATA_DIR . '/lanyard.json';
$ttl = 2;
if (!function_exists('lanyard_refresh')) {
function lanyard_refresh(string $cacheFile): void {
$ch = curl_init('https://api.lanyard.rest/v1/users/' . DISCORD_ID);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 2,
CURLOPT_TIMEOUT => 3,
]);
$res = curl_exec($ch);
curl_close($ch);
if ($res) {
@mkdir(DATA_DIR, 0755, true);
file_put_contents($cacheFile, $res, LOCK_EX);
}
}
}
$isPoll = realpath($_SERVER['SCRIPT_FILENAME'] ?? '') === __FILE__;
$stale = !is_file($cacheFile) || (time() - filemtime($cacheFile) > $ttl);
if ($stale && ($isPoll || !is_file($cacheFile))) {
lanyard_refresh($cacheFile);
}
$data = json_decode(@file_get_contents($cacheFile), true)['data'] ?? null;
$status = $data['discord_status'] ?? 'offline';
$spotify = ($data['listening_to_spotify'] ?? false) ? $data['spotify'] : null;
// Discord can lag clearing the Spotify activity after playback stops, leaving a
// stale "ghost" track. If the song's end time has already passed, treat it as stopped.
if ($spotify && !empty($spotify['timestamps']['end']) && $spotify['timestamps']['end'] < time() * 1000) {
$spotify = null;
}
$user = $data['discord_user'] ?? [];
$did = $user['id'] ?? DISCORD_ID;
$name = $user['global_name'] ?? $user['username'] ?? '';
$avatarHash = $user['avatar'] ?? null;
$avatar = $avatarHash
? "https://cdn.discordapp.com/avatars/$did/$avatarHash." . (str_starts_with($avatarHash, 'a_') ? 'gif' : 'png') . '?size=128'
: 'https://cdn.discordapp.com/embed/avatars/0.png';
$bannerHash = $user['banner'] ?? null;
$banner = $bannerHash
? "https://cdn.discordapp.com/banners/$did/$bannerHash." . (str_starts_with($bannerHash, 'a_') ? 'gif' : 'png') . '?size=480'
: null;
?>
<div class="status-row">
<div class="status-card"<?= $banner ? ' style="background-image:url(\'' . htmlspecialchars($banner) . '\')"' : '' ?>>
<img class="avatar" src="<?= htmlspecialchars($avatar) ?>" alt="" width="56" height="56" loading="lazy">
<div class="who">
<span class="np-label dc-label">discord</span>
<span class="name"><?= htmlspecialchars($name) ?></span>
<span class="state"><span class="dot dot-<?= htmlspecialchars($status) ?>"></span> <?= htmlspecialchars($status) ?></span>
</div>
</div>
<?php if ($spotify): ?>
<a class="spotify-card" href="https://open.spotify.com/track/<?= htmlspecialchars($spotify['track_id']) ?>" target="_blank" rel="noopener">
<img class="album-art" src="<?= htmlspecialchars($spotify['album_art_url']) ?>" alt="" width="64" height="64" loading="lazy">
<div class="track">
<span class="np-label">&#9834; listening on spotify</span>
<span class="song"><?= htmlspecialchars($spotify['song']) ?></span>
<span class="artist"><?= htmlspecialchars(str_replace('; ', ', ', $spotify['artist'])) ?></span>
</div>
</a>
<?php else: ?>
<div class="spotify-card spotify-idle">
<div class="album-art album-art--empty">&#9834;</div>
<div class="track">
<span class="np-label">spotify</span>
<span class="song">nothing playing</span>
<span class="artist">not listening right now</span>
</div>
</div>
<?php endif; ?>
</div>