<?php

// URL to your Shoutcast server, including port
// don't put in the http:// part!  do it like you see here
// also, reverse DNS lookup needs to be on, or this probably won't work
$server = "radiohiphop.serverroom.us:5234";

// Admin password for your Shoutcast server
$password = "servermarkroomisht";

$countries = array (
"ac" => "Ascension Island",
"ad" => "Andorra",
"ae" => "United Arab Emirates",
"af" => "Afghanistan",
"ag" => "Antigua and Barbuda",
"ai" => "Anguilla",
"al" => "Albania",
"am" => "Armenia",
"an" => "Netherlands Antilles",
"ao" => "Angola",
"aq" => "Antarctica",
"ar" => "Argentina",
"as" => "American Samoa",
"at" => "Austria",
"au" => "Australia",
"aw" => "Aruba",
"ax" => "Åland",
"az" => "Azerbaijan",
"ba" => "Bosnia and Herzegovina",
"bb" => "Barbados",
"bd" => "Bangladesh",
"be" => "Belgium",
"bf" => "Burkina Faso",
"bg" => "Bulgaria",
"bh" => "Bahrain",
"bi" => "Burundi",
"bj" => "Benin",
"bm" => "Bermuda",
"bn" => "Brunei Darussalam",
"bo" => "Bolivia",
"br" => "Brazil",
"bs" => "Bahamas",
"bt" => "Bhutan",
"bw" => "Botswana",
"by" => "Belarus",
"bz" => "Belize",
"ca" => "Canada",
"cd" => "Democratic Republic of the Congo",
"cf" => "Central African Republic",
"cg" => "Republic of the Congo",
"ch" => "Switzerland",
"ci" => "Côte d'Ivoire",
"ck" => "Cook Islands",
"cl" => "Chile",
"cm" => "Cameroon",
"cn" => "People's Republic of China",
"co" => "Colombia",
"cr" => "Costa Rica",
"cu" => "Cuba",
"cv" => "Cape Verde",
"cx" => "Christmas Island",
"cy" => "Cyprus",
"cz" => "Czech Republic",
"de" => "Germany",
"dj" => "Djibouti",
"dk" => "Denmark",
"dm" => "Dominica",
"do" => "Dominican Republic",
"dz" => "Algeria",
"ec" => "Ecuador",
"ee" => "Estonia",
"eg" => "Egypt",
"er" => "Eritrea",
"es" => "Spain",
"et" => "Ethiopia",
"fi" => "Finland",
"fj" => "Fiji",
"fk" => "Falkland Islands",
"fo" => "Faroe Islands",
"fr" => "France",
"ga" => "Gabon",
"gb" => "United Kingdom",
"gd" => "Grenada",
"ge" => "Georgia",
"gf" => "French Guiana",
"gg" => "Guernsey",
"gh" => "Ghana",
"gi" => "Gibraltar",
"gl" => "Greenland",
"gm" => "The Gambia",
"gn" => "Guinea",
"gp" => "Guadeloupe",
"gq" => "Equatorial Guinea",
"gr" => "Greece",
"gt" => "Guatemala",
"gu" => "Guam",
"gw" => "Guinea-Bissau",
"gy" => "Guyana",
"hk" => "Hong Kon",
"hn" => "Honduras",
"hr" => "Croatia",
"ht" => "Haiti",
"hu" => "Hungary",
"id" => "Indonesia",
"ie" => "Irelan",
"il" => "Israel",
"im" => "Isle of Man",
"in" => "Indi",
"iq" => "Iraq",
"ir" => "Iran",
"is" => "Iceland",
"it" => "Italy",
"je" => "Jersey",
"jm" => "Jamaica",
"jo" => "Jordan",
"jp" => "Japan",
"ke" => "Kenya",
"kg" => "Kyrgyzstan",
"kh" => "Cambodia",
"ki" => "Kiribati",
"km" => "Comoros",
"kr" => "South Korea",
"kw" => "Kuwait",
"ky" => "Cayman Islands",
"kz" => "Kazakhstan",
"la" => "Laos",
"lb" => "Lebanon",
"lc" => "Saint Lucia",
"li" => "Liechtenstein",
"lk" => "Sri Lanka",
"lr" => "Liberia",
"ls" => "Lesotho",
"lt" => "Lithuania",
"lu" => "Luxembourg",
"lv" => "Latvia",
"ly" => "Libya",
"ma" => "Morocco",
"mc" => "Monaco",
"md" => "Moldova",
"mg" => "Madagascar",
"mh" => "Marshall Islands",
"mk" => "Republic of Macedonia",
"ml" => "Mali",
"mm" => "Myanmar",
"mn" => "Mongolia",
"mp" => "Northern Mariana Islands",
"mq" => "Martinique",
"mr" => "Mauritania",
"ms" => "Montserrat",
"mt" => "Malta",
"mu" => "Mauritius",
"mv" => "Maldives",
"mw" => "Malawi",
"mx" => "Mexico",
"my" => "Malaysia",
"mz" => "Mozambique",
"na" => "Namibia",
"nc" => "New Caledonia",
"ne" => "Niger",
"nf" => "Norfolk Island",
"ng" => "Nigeria",
"ni" => "Nicaragua",
"nl" => "Netherlands",
"no" => "Norway",
"np" => "Nepal",
"nr" => "Nauru",
"nz" => "New Zealand",
"om" => "Oman",
"pa" => "Panama",
"pe" => "Peru",
"pf" => "French Polynesia",
"pg" => "Papua New Guinea",
"ph" => "Philippines",
"pk" => "Pakistan",
"pl" => "Poland",
"pn" => "Pitcairn Islands",
"pr" => "Puerto Rico",
"pt" => "Portugal",
"pw" => "Palau",
"py" => "Paraguay",
"qa" => "Qatar",
"re" => "Réunion",
"ro" => "Romania",
"ru" => "Russia",
"rw" => "Rwanda",
"sa" => "Saudi Arabia",
"sb" => "Solomon Islands",
"sc" => "Seychelles",
"sd" => "Sudan",
"se" => "Sweden",
"sg" => "Singapore",
"sh" => "Saint Helena",
"si" => "Slovenia",
"sk" => "Slovakia",
"sl" => "Sierra Leone",
"sm" => "San Marino",
"sn" => "Senegal",
"so" => "Somalia",
"sr" => "Suriname",
"st" => "São Tomé and Príncipe",
"su" => "former Soviet Union",
"sv" => "El Salvador",
"sy" => "Syria",
"sz" => "Swazilan",
"td" => "Chad",
"tg" => "Togo",
"th" => "Thailand",
"tj" => "Tajikistan",
"tl" => "East Timor",
"tm" => "Turkmenistan",
"tn" => "Tunisia",
"to" => "Tonga",
"tp" => "East Timor",
"tr" => "Turkey",
"tt" => "Trinidad and Tobago",
"tw" => "Taiwan",
"tz" => "Tanzania",
"ua" => "Ukraine",
"ug" => "Uganda",
"uk" => "United Kingdom",
"um" => "United States Minor Outlying Island",
"uy" => "Uruguay",
"uz" => "Uzbekistan",
"va" => "Vatican City State",
"vc" => "Saint Vincent and the Grenadines",
"ve" => "Venezuela",
"vg" => "British Virgin Islands",
"vi" => "U.S. Virgin Islands",
"vn" => "Vietnam",
"vu" => "Vanuatu",
"wf" => "Wallis and Futuna",
"ws" => "Samoa",
"ye" => "Yemen",
"yt" => "Mayotte",
"yu" => "Yugoslavia",
"za" => "South Africa",
"zm" => "Zambia",
"zw" => "Zimbabwe"
);
header("Content-Type: text/html");
$mysession = curl_init();
curl_setopt($mysession, CURLOPT_URL, "http://$server/admin.cgi?mode=viewxml");
curl_setopt($mysession, CURLOPT_HEADER, false);
curl_setopt($mysession, CURLOPT_RETURNTRANSFER, true);
curl_setopt($mysession, CURLOPT_POST, false);
curl_setopt($mysession, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($mysession, CURLOPT_USERPWD, "admin:$password");
curl_setopt($mysession, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($mysession, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$xml = curl_exec($mysession);
curl_close($mysession);
$xml = str_replace("&#x27;", "'", $xml);
$result = "";
$listeners = "0";
$maxlisteners = "0";
$songs = "";
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
xml_parse($xml_parser, $xml);
xml_parser_free($xml_parser);
$usa = $listeners;
$temp = "";
foreach ($country as $key=>$val) {
   $country = $countries[$key];
   if (strlen($country) > 0) {
      $temp .= ", $country";
      if ($val > 1)
	 $temp .= " ($val)";
      $usa -= $val;
   }
}
$result = "There are currently $listeners out of $maxlisteners listeners from:\n";
$result .= "USA ($usa)$temp";
$result .= $songs;
echo trim($result);
function startElement($parser, $name, $attrs) {
   global $curTag;
   $curTag .= "^$name";
}
function endElement($parser, $name) {
   global $curTag;
   $caret_pos = strrpos($curTag, '^');
   $curTag = substr($curTag, 0, $caret_pos);
}
function characterData($parser, $data) {
   global $curTag;
   global $country;
   global $listeners;
   global $maxlisteners;
   global $songs;
   if ($curTag == "^SHOUTCASTSERVER^LISTENERS^LISTENER^HOSTNAME") {
      $parts = explode(".", $data);
      $i = end($parts);
      $country[$i]++;
   }
   if ($curTag == "^SHOUTCASTSERVER^CURRENTLISTENERS") {
      $listeners = $data;
   }
   if ($curTag == "^SHOUTCASTSERVER^MAXLISTENERS") {
      $maxlisteners = $data;
   }
   if ($curTag == "^SHOUTCASTSERVER^SONGHISTORY^SONG^TITLE") {
      if (strpos($data, " 0:") === false)
         $songs .= "\n$data";
   }
}
?>
