<?php
date_default_timezone_set('America/New_York');

$server_ip = 'cr3.broadcasturself.com';
$server_port = '6996';
$admin_user = 'AlmostHeavenRadio';
$admin_password = 'f846z35Frqm';

$mounts = ['/stream', '/autodj'];
$stats_url = "http://$admin_user:$admin_password@$server_ip:$server_port/admin/stats.xml";

$xml = @file_get_contents($stats_url);
if (!$xml) {
  echo 'Unable to fetch stream info.';
  return;
}

foreach ($mounts as $mount) {
  $needle = '<source mount="' . $mount . '">';
  if (str_contains($xml, $needle)) {
    $section = strstr($xml, $needle);
    $titleBlock = strstr($section, '<title>');
    if ($titleBlock) {
      $start = strpos($titleBlock, '<title>') + 7;
      $end = strpos($titleBlock, '</title>');
      $track = trim(substr($titleBlock, $start, $end - $start));
      echo $track ?: 'No track info available.';
      return;
    }
  }
}

echo 'No track info available.';
?>
