<?php
header("Refresh: 5"); // AUTO REFRESH EVERY 5 SECONDS

$hostname = 'cr3.broadcasturself.com';
$port     = 23990;
$user     = 'YourOldiesRadio';
$pass     = 'Q5DbUrjK7z9REsqXa';
$mount    = '/stream';

$statsUrl = "http://$hostname:$port/admin/stats";
$track    = 'Loading...';

$context = stream_context_create([
  'http' => [
    'timeout' => 3,
    'header'  => "Authorization: Basic " . base64_encode("$user:$pass")
  ]
]);

$response = @file_get_contents($statsUrl, false, $context);

if ($response) {
    $xml = simplexml_load_string($response);

    if ($xml && isset($xml->source)) {
        $streamTitle = null;
        $autodjTitle = null;

        foreach ($xml->source as $source) {
            $mountName = (string)$source['mount'];
            $title     = (string)$source->title;

            if ($mountName === $mount && !empty($title)) {
                $streamTitle = htmlspecialchars($title);
            }

            if ($mountName === '/autodj' && !empty($title)) {
                $autodjTitle = htmlspecialchars($title);
            }
        }

        if ($streamTitle) {
            $track = $streamTitle;
        } elseif ($autodjTitle) {
            $track = $autodjTitle;
        } else {
            $track = 'Auto DJ';
        }
    }
}

echo "<span id='track-text'>$track</span>";
