aboutsummaryrefslogtreecommitdiffstats
path: root/partials/vibe.php
blob: 84b148a6a74892ed590ed3f4d0263b4b238eefda (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
<?php
$vibe = file_exists(__DIR__ . '/../vibe.php')
    ? require __DIR__ . '/../vibe.php'
    : ['description' => '', 'url' => '', 'updated' => ''];

$vibe_desc = htmlspecialchars($vibe['description'] ?? '', ENT_QUOTES, 'UTF-8');
$vibe_url  = $vibe['url'] ?? '';
$safe_url  = htmlspecialchars($vibe_url, ENT_QUOTES, 'UTF-8');

function vibe_detect_type(string $url): string {
    if (empty($url)) return 'none';
    $path = strtolower(parse_url($url, PHP_URL_PATH) ?? '');
    $ext  = pathinfo($path, PATHINFO_EXTENSION);
    if (in_array($ext, ['jpg','jpeg','png','gif','webp','bmp','svg','avif'])) return 'image';
    if (in_array($ext, ['mp4','webm','ogv','mov'])) return 'video';
    if (in_array($ext, ['mp3','flac','wav','ogg','aac','opus','m4a'])) return 'audio';
    if (preg_match('/(?:youtube\.com\/watch\?.*v=|youtu\.be\/)([A-Za-z0-9_-]{11})/', $url, $m)) return 'youtube:' . $m[1];
    if (preg_match('/vimeo\.com\/(\d+)/', $url, $m)) return 'vimeo:' . $m[1];
    return 'link';
}

$type = vibe_detect_type($vibe_url);
?>

<section aria-labelledby="vibe-title">
  <h2 id="vibe-title">My Current Vibe</h2>

  <?php if ($vibe_desc): ?>
    <p><?= $vibe_desc ?></p>
  <?php endif; ?>

  <?php if ($vibe_url): ?>

    <?php if ($type === 'image'): ?>
      <p><a href="<?= $safe_url ?>" target="_blank" rel="noopener noreferrer"><?= $safe_url ?></a></p>
      <img src="<?= $safe_url ?>" alt="Current vibe" style="max-width:100%;height:auto;" loading="lazy">

    <?php elseif ($type === 'video'): ?>
      <p><a href="<?= $safe_url ?>" target="_blank" rel="noopener noreferrer"><?= $safe_url ?></a></p>
      <video controls style="max-width:100%;" preload="none">
        <source src="<?= $safe_url ?>">
      </video>

    <?php elseif ($type === 'audio'): ?>
      <p><a href="<?= $safe_url ?>" target="_blank" rel="noopener noreferrer"><?= $safe_url ?></a></p>
      <audio controls preload="none" style="width:100%;">
        <source src="<?= $safe_url ?>">
      </audio>

    <?php elseif (str_starts_with($type, 'youtube:')): ?>
      <?php $yt_id = substr($type, 8); ?>
      <p><a href="<?= $safe_url ?>" target="_blank" rel="noopener noreferrer"><?= $safe_url ?></a></p>
      <div style="position:relative;padding-top:56.25%;overflow:hidden;">
        <iframe src="https://www.youtube.com/embed/<?= htmlspecialchars($yt_id) ?>"
          style="position:absolute;top:0;left:0;width:100%;height:100%;border:0;"
          allowfullscreen loading="lazy"></iframe>
      </div>

    <?php elseif (str_starts_with($type, 'vimeo:')): ?>
      <?php $vimeo_id = substr($type, 6); ?>
      <p><a href="<?= $safe_url ?>" target="_blank" rel="noopener noreferrer"><?= $safe_url ?></a></p>
      <div style="position:relative;padding-top:56.25%;overflow:hidden;">
        <iframe src="https://player.vimeo.com/video/<?= htmlspecialchars($vimeo_id) ?>"
          style="position:absolute;top:0;left:0;width:100%;height:100%;border:0;"
          allowfullscreen loading="lazy"></iframe>
      </div>

    <?php else: ?>
      <p><a href="<?= $safe_url ?>" target="_blank" rel="noopener noreferrer"><?= $safe_url ?></a></p>
    <?php endif; ?>

  <?php endif; ?>
</section>