
<?php
$hostname = 'cr3.broadcasturself.com';
$port     = 65001;
$user     = 'HomeTimeRadio';              // your Icecast admin username
$pass     = 'KFnph5qQuBPXaT3VJ4jp';
$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);
            }
        }

        // Prefer live stream if available, otherwise fallback to AutoDJ
        if ($streamTitle) {
            $track = $streamTitle;
        } elseif ($autodjTitle) {
            $track = $autodjTitle;
        }
    }
}

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