Solved

Not displaying all data

Posted on 2007-04-07
11
332 Views
Last Modified: 2013-12-12
I have a script (below) that lists all of the shoutcast servers that I am using as well as their stats. There is one portion of the script that isn't working right. The part where it is suppose to display the countries from which users are connected, I can't tell what it is doing but it isn't displaying everything. It is either only displaying the first entry for each server or it is taking the first three entries from the first server and splitting them up between the three servers displayed.

Here is the script in action:  http://www.emphaticradio.com/serverstatus.php

The number of countries for each server should match the number of listeners on that server.

Any help would be much appreciated.

//////SCRIPT//////

<?

/* ---------- General configuration ---------- */

/*
  $RCSfile: status.php,v $
  $Revision: 1.34 $


$module_name = basename(dirname(__FILE__));
require("geostats/shoutcast.class.phps");
require("geostats/geoip.phps");


$index = 1;
is_readable("geostats/GeoIP.dat") or exit("Error: GeoIP.dat not found!");

function elapsedtime($seconds) {
      if ($seconds > 86400) {
            $seconds -= 86400;
            return(gmdate("j\d H:i:s", $seconds));
      } else
            return(gmdate("H:i:s", $seconds));
}

$listenersArray["Total"]["current"] = 0;
$listenersArray["Total"]["max"] = 0;


$station_name = "Emphatic Radio.com!";

$refresh = "60";  // Page refresh time in seconds. Put 0 for no refresh
$timeout = "1"; // Number of seconds before connecton times out - a higher value will slow the page down if any servers are offline

/* ----------- Server configuration ---------- */

// Note: dont include http://
// Main server: The song title will be taken from this server

$ip[1] = "xxxx";
$port[1] = "xxxx";
$location[1] = "U.K.";
$password[1] = "xxxx";

$ip[2] = "xxxx";
$port[2] = "xxxx";
$location[2] = "U.K.";
$password[2] = "xxxx";

$ip[3] = "xxxx";
$port[3] = "xxxx";
$location[3] = "U.S.";
$password[3] = "xxxx";
/* ------------------------------------------- */
$servers = count($ip);
?>

<?

$i = "1";

while($i<=$servers)
      {
      $fp = @fsockopen($ip[$i],$port[$i],$errno,$errstr,$timeout);
      if (!$fp)
            {
            $listeners[$i] = "0";
            $msg[$i] = "<span class=\"red\">ERROR [Connection refused / Server down]</span>";
            $error[$i] = "1";
            }
      else
            {
            fputs($fp, "GET /7.html HTTP/1.0\r\nUser-Agent: Mozilla\r\n\r\n");
            while (!feof($fp))
                  {
                  $info = fgets($fp);
                  }
            $info = str_replace('<HTML><meta http-equiv="Pragma" content="no-cache"></head><body>', "", $info);
            $info = str_replace('</body></html>', "", $info);
            $stats = explode(',', $info);
            if (empty($stats[1]) )
                  {
                  $listeners[$i] = "0";
                  $msg[$i] = "<span class=\"red\">ERROR [There is no source connected]</span>";
                  $error[$i] = "1";
                  }
            else
                  {
                  if ($stats[1] == "1")
                        {
                        $song[$i] = $stats[6];
                        $listeners[$i] = $stats[0];
                        $max[$i] =  $stats[3];
                        $bitrate[$i] = $stats[5];
                        $peak[$i] = $stats[2];
                  
                              $msg[$i] = "<span class=\"red\">Server is up at $bitrate[$i] kbps with $listeners[$i] of $max[$i] listeners</span><p><b>Listener peak:</b> $peak[$i]";
                  }
                        
                  else
                        {
                        $listeners[$i] = "0";
                        $msg[$i] = "    <span class=\"red\">ERROR [Cannot get info from server]</span>";
                        $error[$i] = "1";
                        }
                  }
            }
            
      $i++;
      }
$total_listeners = array_sum($listeners) ;
$total_slots = array_sum($max) ;

$availableslots = ($total_slots-$total_listeners);

print "<div id=\"blu\">\n  <div style=\"text-align: center;\">\n    <font size=\"4\">\nThere are $total_listeners listeners locked <br> $availableslots available slots remaining


</font>\n  </div>\n</div>\n<div>\n  <div>\n    <p><b>Current song:</b> $song[1]</p>\n  </div>\n</div>\n<div>\n";
$i = "1";
while($i<=$servers)
      {
          print "  <div>\n";
if ($max[$i] > 0)
      {
      $percentage = round(($listeners[$i] / $max[$i] * 100));
      $timesby = (300 / $max[$i]);
      $barlength = round(($listeners[$i] * "$timesby"));
      }
if ($error[$i] = 1)
      {
?>
<br>
    <table width="400"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="25%" align="center"><b>Server <? print $i ?> (<? print $location[$i] ?>)</b>&nbsp;&nbsp;</td>
        <td width="75%" colspan="3" bgcolor="#eeeeee"><img src="images/<? if ($percentage == "100") { print "red-"; } ?>bar.gif" width="<? print $barlength ?>" height="12" alt="The server is at <? print $percentage; ?>% capacity"></td>
      </tr>
      <tr>
        <td width="25%">&nbsp;</td>
        <td width="25%">0%</td>
        <td width="25%" align="center">50%</td>
        <td width="25%" align="right">100%</td>
      </tr>
    </table>
<?
      }
else
      {
?>
<br>
    <table width="400"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="25%" align="center"><b>Server <? print $i ?> (<? print $location[$i] ?>)</b>&nbsp;&nbsp;</td>
        <td width="75%" colspan="3" bgcolor="#eeeeee">&nbsp;</td>
      </tr>
      <tr>
        <td width="25%">&nbsp;</td>
        <td width="25%">0%</td>
        <td width="25%" align="center">50%</td>
        <td width="25%" align="right">100%</td>
      </tr>
    </table>
<?

}

      $fp = @fsockopen($ip[$i],$port[$i],$errno,$errstr,$timeout);
      if (!$fp)
            {
            $listeners[$i] = "0";
            $msg[$i] = "<span class=\"red\">ERROR [Connection refused / Server down]</span>";
            $error[$i] = "1";
            }
      else
            {
            fputs($fp, "GET /7.html HTTP/1.0\r\nUser-Agent: Mozilla\r\n\r\n");
            while (!feof($fp))
                  {
                  $info = fgets($fp);
                  }
            $info = str_replace('<HTML><meta http-equiv="Pragma" content="no-cache"></head><body>', "", $info);
            $info = str_replace('</body></html>', "", $info);
            $stats = explode(',', $info);
            if (empty($stats[1]) )
                  {
                  $listeners[$i] = "0";
                  $msg[$i] = "<span class=\"red\">ERROR [There is no source connected]</span>";
                  $error[$i] = "1";
                  }
            else
                  {
                  if ($stats[1] == "1")
                        {
                        $song[$i] = $stats[6];
                        $listeners[$i] = $stats[0];
                        $max[$i] =  $stats[3];
                        $bitrate[$i] = $stats[5];
                        $peak[$i] = $stats[2];
      



$gi = geoip_open("geostats/GeoIP.dat", GEOIP_STANDARD);
for ($count = 0;$count < count($servers); $count++) {
      $shoutcast = new ShoutCast();
      $shoutcast->name = $location[$i];
      $shoutcast->host = $ip[$i];
      $shoutcast->port = $port[$i];
      $shoutcast->passwd = $password[$i];

      $listenersArray["$shoutcast->name"]["current"] = "NA";
      $listenersArray["$shoutcast->name"]["max"] = "NA";


      if ($shoutcast->openstats()) {
            // We got the XML, gogogo!..
            if ($shoutcast->GetStreamStatus()) {
                  $listenersArray["$shoutcast->name"]["current"] = $shoutcast->GetCurrentListenersCount();
                  $listenersArray["$shoutcast->name"]["max"] = $shoutcast->GetMaxListenersCount();
                  $listenersArray["Total"]["current"] += $shoutcast->GetCurrentListenersCount();
                  $listenersArray["Total"]["max"] += $shoutcast->GetMaxListenersCount();


                  $listeners_geo = $shoutcast->GetListeners();
                        if (is_array($listeners_geo)) {
                        for ($is=0;$is<sizeof($listeners_geo);$is++) {
                              $country_id = geoip_country_id_by_name($gi, $listeners_geo[$is]["hostname"]);
                              $country_name = $GLOBALS['GEOIP_COUNTRY_NAMES'][$country_id];
                              if (!$country_id) {
                                    $country_id = 0;
                                    $country_name = "Earth(unidentified)";
                              }
                              $from["$country_id"]++;
                        }
                  } else {
                        echo "                                          
                                                Noone listens right now..
                                          \n";
                  }
                  echo "                                    \n";
            } else {
                  echo "Server is up, but no stream available..
                                          
                                    \n";
            }
      } else {
            // Ohhh, damnit..
            echo $shoutcast->geterror();
            echo "\n                                          \n                                    \n";
      }
      echo "                                    \n";

$count++;
}
geoip_close($gi);
?>
<br>
<table border=\"1\" id=\"table1\" cellspacing=\"0\" cellpadding=\"0\" align="center">
        <tr>
                <td>Flag</td>
                <td>Country</td>
                <td>Listeners</td>
        </tr>
        <tr>
<?php
arsort($from);
reset($from);
while (list($key, $val) = each($from)) {
      $country_code = $GLOBALS['GEOIP_COUNTRY_CODES']["$key"];
      $country_name = $GLOBALS['GEOIP_COUNTRY_NAMES']["$key"];
      if (!$country_code) {
            if (!$key && $key != 0) {
                  $country_code = $key;
            } else {
                  // I don't want to show unidentified hosts in ranking.
                  // $country_code = "N/A";
                  continue;
            }
      }
      echo"<td><img src=\"geostats/$country_code.GIF\" alt=\"flag of the $country_name\" /></td><td>$country_name</td>
                        <td>      $val</td></tr></table>
                        \n";
      $countrycount++;
      if ($countrycount >= $countrylimit) break;

}



            
                              $msg[$i] = "<span class=\"red\">Server is up at $bitrate[$i] kbps with $listeners[$i] of $max[$i] listeners</span>
<br>
<br>                                          
                                                 <b>Web Hits:</b> ".$shoutcast->GetWebHitsCount()."<br /> <b>Stream Hits:</b> ".$shoutcast->GetStreamHitsCount()."<br /> <b>Average Listen Time:</b> ".elapsedtime($shoutcast->GetAverageListenTime())."

<p><b>Listener Peak:</b> $peak[$i]";


                  }
                        
                  else
                        {
                        $listeners[$i] = "0";
                        $msg[$i] = "    <span class=\"red\">ERROR [Cannot get info from server]</span>";
                        $error[$i] = "1";
                        }
                  }
            }

print "    <p><b>Status:</b> $msg[$i]</p>\n  </div>\n  <div class=\"line\"> </div>\n";
      $i++;
      }
print "</div>\n";
$time_difference = "0"; // BST: 1 GMT: 0
$time_difference = ($time_difference * 60 * 60);
$time = date("h:ia", time() + $time_difference);
$date = date("jS F, Y", time() + 0);
print "<div>\n  <div>\n    <p><b>Live SHOUTcast statistics:</b> $date, $time</p>\n  </div>\n</div>\n";
?>
<div>
  <div>
0
Comment
Question by:MDauphinais1
  • 6
  • 5
11 Comments
 
LVL 20

Expert Comment

by:steelseth12
ID: 18870128
How many servers should there be ???? From what i can see in the code you posted you define 3 servers

$ip[1] = "xxxx";
$port[1] = "xxxx";
$location[1] = "U.K.";
$password[1] = "xxxx";

$ip[2] = "xxxx";
$port[2] = "xxxx";
$location[2] = "U.K.";
$password[2] = "xxxx";

$ip[3] = "xxxx";
$port[3] = "xxxx";
$location[3] = "U.S.";
$password[3] = "xxxx";

and in the url you gave it deisplays 3 servers.
0
 

Author Comment

by:MDauphinais1
ID: 18870133
There should be 3.

The server stats (the bar that displays the percentage of users), number of users is correct.

The part that is problematic is the table under the percentage bar where it shows the country flag, country name and number of listeners from that country for each server. It doesn't add up. It should show countries for every user connected to that server but it doesn't. It is either only displaying the first entry for each server or it is taking the first three entries from the first server and splitting them up between the three servers displayed. I'm not sure which but it doesn't add up.
0
 
LVL 20

Accepted Solution

by:
steelseth12 earned 500 total points
ID: 18870187
i think your problem might be here .....

if ($countrycount >= $countrylimit) break;

i cant find anywhre that $countrylimit is define so it must beak after the first relut is printed ...

either remove that line or set $countrylimit to the number of records you want to show
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:MDauphinais1
ID: 18870342
OK, changing that it is definately starting to look better. But if you check out that page again the listener count under the countries doesn't equal where is says Status: there are x number of listeners...
0
 

Author Comment

by:MDauphinais1
ID: 18870395
It almost looks like it is taking the countries from Server 1 and then adding them to Server 2 so Server 2 is actually displaying the countries from Server 1 and 2. And then Server 3 is showing the countries from all 3 instead of just the ones from Server 3.
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 18870410
try like this ....

<?

/* ---------- General configuration ---------- */

/*
  $RCSfile: status.php,v $
  $Revision: 1.34 $


$module_name = basename(dirname(__FILE__));
require("geostats/shoutcast.class.phps");
require("geostats/geoip.phps");


$index = 1;
is_readable("geostats/GeoIP.dat") or exit("Error: GeoIP.dat not found!");

function elapsedtime($seconds) {
      if ($seconds > 86400) {
            $seconds -= 86400;
            return(gmdate("j\d H:i:s", $seconds));
      } else
            return(gmdate("H:i:s", $seconds));
}

$listenersArray["Total"]["current"] = 0;
$listenersArray["Total"]["max"] = 0;


$station_name = "Emphatic Radio.com!";

$refresh = "60";  // Page refresh time in seconds. Put 0 for no refresh
$timeout = "1"; // Number of seconds before connecton times out - a higher value will slow the page down if any servers are offline

/* ----------- Server configuration ---------- */

// Note: dont include http://
// Main server: The song title will be taken from this server

$ip[1] = "xxxx";
$port[1] = "xxxx";
$location[1] = "U.K.";
$password[1] = "xxxx";

$ip[2] = "xxxx";
$port[2] = "xxxx";
$location[2] = "U.K.";
$password[2] = "xxxx";

$ip[3] = "xxxx";
$port[3] = "xxxx";
$location[3] = "U.S.";
$password[3] = "xxxx";
/* ------------------------------------------- */
$servers = count($ip);
?>

<?

$i = "1";

while($i<=$servers)
      {
      $fp = @fsockopen($ip[$i],$port[$i],$errno,$errstr,$timeout);
      if (!$fp)
            {
            $listeners[$i] = "0";
            $msg[$i] = "<span class=\"red\">ERROR [Connection refused / Server down]</span>";
            $error[$i] = "1";
            }
      else
            {
            fputs($fp, "GET /7.html HTTP/1.0\r\nUser-Agent: Mozilla\r\n\r\n");
            while (!feof($fp))
                  {
                  $info = fgets($fp);
                  }
            $info = str_replace('<HTML><meta http-equiv="Pragma" content="no-cache"></head><body>', "", $info);
            $info = str_replace('</body></html>', "", $info);
            $stats = explode(',', $info);
            if (empty($stats[1]) )
                  {
                  $listeners[$i] = "0";
                  $msg[$i] = "<span class=\"red\">ERROR [There is no source connected]</span>";
                  $error[$i] = "1";
                  }
            else
                  {
                  if ($stats[1] == "1")
                        {
                        $song[$i] = $stats[6];
                        $listeners[$i] = $stats[0];
                        $max[$i] =  $stats[3];
                        $bitrate[$i] = $stats[5];
                        $peak[$i] = $stats[2];
                 
                              $msg[$i] = "<span class=\"red\">Server is up at $bitrate[$i] kbps with $listeners[$i] of $max[$i] listeners</span><p><b>Listener peak:</b> $peak[$i]";
                  }
                       
                  else
                        {
                        $listeners[$i] = "0";
                        $msg[$i] = "    <span class=\"red\">ERROR [Cannot get info from server]</span>";
                        $error[$i] = "1";
                        }
                  }
            }
           
      $i++;
      }
$total_listeners = array_sum($listeners) ;
$total_slots = array_sum($max) ;

$availableslots = ($total_slots-$total_listeners);

print "<div id=\"blu\">\n  <div style=\"text-align: center;\">\n    <font size=\"4\">\nThere are $total_listeners listeners locked <br> $availableslots available slots remaining


</font>\n  </div>\n</div>\n<div>\n  <div>\n    <p><b>Current song:</b> $song[1]</p>\n  </div>\n</div>\n<div>\n";
$i = "1";
while($i<=$servers)
      {
          print "  <div>\n";
if ($max[$i] > 0)
      {
      $percentage = round(($listeners[$i] / $max[$i] * 100));
      $timesby = (300 / $max[$i]);
      $barlength = round(($listeners[$i] * "$timesby"));
      }
if ($error[$i] = 1)
      {
?>
<br>
    <table width="400"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="25%" align="center"><b>Server <? print $i ?> (<? print $location[$i] ?>)</b>&nbsp;&nbsp;</td>
        <td width="75%" colspan="3" bgcolor="#eeeeee"><img src="images/<? if ($percentage == "100") { print "red-"; } ?>bar.gif" width="<? print $barlength ?>" height="12" alt="The server is at <? print $percentage; ?>% capacity"></td>
      </tr>
      <tr>
        <td width="25%">&nbsp;</td>
        <td width="25%">0%</td>
        <td width="25%" align="center">50%</td>
        <td width="25%" align="right">100%</td>
      </tr>
    </table>
<?
      }
else
      {
?>
<br>
    <table width="400"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="25%" align="center"><b>Server <? print $i ?> (<? print $location[$i] ?>)</b>&nbsp;&nbsp;</td>
        <td width="75%" colspan="3" bgcolor="#eeeeee">&nbsp;</td>
      </tr>
      <tr>
        <td width="25%">&nbsp;</td>
        <td width="25%">0%</td>
        <td width="25%" align="center">50%</td>
        <td width="25%" align="right">100%</td>
      </tr>
    </table>
<?

}

      $fp = @fsockopen($ip[$i],$port[$i],$errno,$errstr,$timeout);
      if (!$fp)
            {
            $listeners[$i] = "0";
            $msg[$i] = "<span class=\"red\">ERROR [Connection refused / Server down]</span>";
            $error[$i] = "1";
            }
      else
            {
            fputs($fp, "GET /7.html HTTP/1.0\r\nUser-Agent: Mozilla\r\n\r\n");
            while (!feof($fp))
                  {
                  $info = fgets($fp);
                  }
            $info = str_replace('<HTML><meta http-equiv="Pragma" content="no-cache"></head><body>', "", $info);
            $info = str_replace('</body></html>', "", $info);
            $stats = explode(',', $info);
            if (empty($stats[1]) )
                  {
                  $listeners[$i] = "0";
                  $msg[$i] = "<span class=\"red\">ERROR [There is no source connected]</span>";
                  $error[$i] = "1";
                  }
            else
                  {
                  if ($stats[1] == "1")
                        {
                        $song[$i] = $stats[6];
                        $listeners[$i] = $stats[0];
                        $max[$i] =  $stats[3];
                        $bitrate[$i] = $stats[5];
                        $peak[$i] = $stats[2];
     



$gi = geoip_open("geostats/GeoIP.dat", GEOIP_STANDARD);
for ($count = 0;$count < count($servers); $count++) {
      $shoutcast = new ShoutCast();
      $shoutcast->name = $location[$i];
      $shoutcast->host = $ip[$i];
      $shoutcast->port = $port[$i];
      $shoutcast->passwd = $password[$i];

      $listenersArray["$shoutcast->name"]["current"] = "NA";
      $listenersArray["$shoutcast->name"]["max"] = "NA";


      if ($shoutcast->openstats()) {
            // We got the XML, gogogo!..
            if ($shoutcast->GetStreamStatus()) {
                  $listenersArray["$shoutcast->name"]["current"] = $shoutcast->GetCurrentListenersCount();
                  $listenersArray["$shoutcast->name"]["max"] = $shoutcast->GetMaxListenersCount();
                  $listenersArray["Total"]["current"] += $shoutcast->GetCurrentListenersCount();
                  $listenersArray["Total"]["max"] += $shoutcast->GetMaxListenersCount();


                  $listeners_geo = $shoutcast->GetListeners();
                        if (is_array($listeners_geo)) {
                        for ($is=0;$is<sizeof($listeners_geo);$is++) {
                              $country_id = geoip_country_id_by_name($gi, $listeners_geo[$is]["hostname"]);
                              $country_name = $GLOBALS['GEOIP_COUNTRY_NAMES'][$country_id];
                              if (!$country_id) {
                                    $country_id = 0;
                                    $country_name = "Earth(unidentified)";
                              }
                              $from[$i]["$country_id"]++;
                        }
                  } else {
                        echo "                                         
                                                Noone listens right now..
                                          \n";
                  }
                  echo "                                    \n";
            } else {
                  echo "Server is up, but no stream available..
                                         
                                    \n";
            }
      } else {
            // Ohhh, damnit..
            echo $shoutcast->geterror();
            echo "\n                                          \n                                    \n";
      }
      echo "                                    \n";

$count++;
}
geoip_close($gi);
?>
<br>
<table border=\"1\" id=\"table1\" cellspacing=\"0\" cellpadding=\"0\" align="center">
        <tr>
                <td>Flag</td>
                <td>Country</td>
                <td>Listeners</td>
        </tr>
        <tr>
<?php
arsort($from[$i]);
reset($from[$i]);
while (list($key, $val) = each($from[$i])) {
      $country_code = $GLOBALS['GEOIP_COUNTRY_CODES']["$key"];
      $country_name = $GLOBALS['GEOIP_COUNTRY_NAMES']["$key"];
      if (!$country_code) {
            if (!$key && $key != 0) {
                  $country_code = $key;
            } else {
                  // I don't want to show unidentified hosts in ranking.
                  // $country_code = "N/A";
                  continue;
            }
      }
      echo"<td><img src=\"geostats/$country_code.GIF\" alt=\"flag of the $country_name\" /></td><td>$country_name</td>
                        <td>      $val</td></tr></table>
                        \n";
      $countrycount++;
      if ($countrycount >= $countrylimit) break;

}



           
                              $msg[$i] = "<span class=\"red\">Server is up at $bitrate[$i] kbps with $listeners[$i] of $max[$i] listeners</span>
<br>
<br>                                          
                                                 <b>Web Hits:</b> ".$shoutcast->GetWebHitsCount()."<br /> <b>Stream Hits:</b> ".$shoutcast->GetStreamHitsCount()."<br /> <b>Average Listen Time:</b> ".elapsedtime($shoutcast->GetAverageListenTime())."

<p><b>Listener Peak:</b> $peak[$i]";


                  }
                       
                  else
                        {
                        $listeners[$i] = "0";
                        $msg[$i] = "    <span class=\"red\">ERROR [Cannot get info from server]</span>";
                        $error[$i] = "1";
                        }
                  }
            }

print "    <p><b>Status:</b> $msg[$i]</p>\n  </div>\n  <div class=\"line\"> </div>\n";
      $i++;
      }
print "</div>\n";
$time_difference = "0"; // BST: 1 GMT: 0
$time_difference = ($time_difference * 60 * 60);
$time = date("h:ia", time() + $time_difference);
$date = date("jS F, Y", time() + 0);
print "<div>\n  <div>\n    <p><b>Live SHOUTcast statistics:</b> $date, $time</p>\n  </div>\n</div>\n";
?>
<div>
  <div>
0
 

Author Comment

by:MDauphinais1
ID: 18870440
The page doesn't even load now. It just goes completely blank.
0
 
LVL 20

Assisted Solution

by:steelseth12
steelseth12 earned 500 total points
ID: 18870444
on second thought ... just add       unset($from); after  // We got the XML, gogogo!..

but i cant test either of these as i dont have the data.
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 18870447
<?

/* ---------- General configuration ---------- */

/*
  $RCSfile: status.php,v $
  $Revision: 1.34 $


$module_name = basename(dirname(__FILE__));
require("geostats/shoutcast.class.phps");
require("geostats/geoip.phps");


$index = 1;
is_readable("geostats/GeoIP.dat") or exit("Error: GeoIP.dat not found!");

function elapsedtime($seconds) {
      if ($seconds > 86400) {
            $seconds -= 86400;
            return(gmdate("j\d H:i:s", $seconds));
      } else
            return(gmdate("H:i:s", $seconds));
}

$listenersArray["Total"]["current"] = 0;
$listenersArray["Total"]["max"] = 0;


$station_name = "Emphatic Radio.com!";

$refresh = "60";  // Page refresh time in seconds. Put 0 for no refresh
$timeout = "1"; // Number of seconds before connecton times out - a higher value will slow the page down if any servers are offline

/* ----------- Server configuration ---------- */

// Note: dont include http://
// Main server: The song title will be taken from this server

$ip[1] = "xxxx";
$port[1] = "xxxx";
$location[1] = "U.K.";
$password[1] = "xxxx";

$ip[2] = "xxxx";
$port[2] = "xxxx";
$location[2] = "U.K.";
$password[2] = "xxxx";

$ip[3] = "xxxx";
$port[3] = "xxxx";
$location[3] = "U.S.";
$password[3] = "xxxx";
/* ------------------------------------------- */
$servers = count($ip);
?>

<?

$i = "1";

while($i<=$servers)
      {
      $fp = @fsockopen($ip[$i],$port[$i],$errno,$errstr,$timeout);
      if (!$fp)
            {
            $listeners[$i] = "0";
            $msg[$i] = "<span class=\"red\">ERROR [Connection refused / Server down]</span>";
            $error[$i] = "1";
            }
      else
            {
            fputs($fp, "GET /7.html HTTP/1.0\r\nUser-Agent: Mozilla\r\n\r\n");
            while (!feof($fp))
                  {
                  $info = fgets($fp);
                  }
            $info = str_replace('<HTML><meta http-equiv="Pragma" content="no-cache"></head><body>', "", $info);
            $info = str_replace('</body></html>', "", $info);
            $stats = explode(',', $info);
            if (empty($stats[1]) )
                  {
                  $listeners[$i] = "0";
                  $msg[$i] = "<span class=\"red\">ERROR [There is no source connected]</span>";
                  $error[$i] = "1";
                  }
            else
                  {
                  if ($stats[1] == "1")
                        {
                        $song[$i] = $stats[6];
                        $listeners[$i] = $stats[0];
                        $max[$i] =  $stats[3];
                        $bitrate[$i] = $stats[5];
                        $peak[$i] = $stats[2];
                 
                              $msg[$i] = "<span class=\"red\">Server is up at $bitrate[$i] kbps with $listeners[$i] of $max[$i] listeners</span><p><b>Listener peak:</b> $peak[$i]";
                  }
                       
                  else
                        {
                        $listeners[$i] = "0";
                        $msg[$i] = "    <span class=\"red\">ERROR [Cannot get info from server]</span>";
                        $error[$i] = "1";
                        }
                  }
            }
           
      $i++;
      }
$total_listeners = array_sum($listeners) ;
$total_slots = array_sum($max) ;

$availableslots = ($total_slots-$total_listeners);

print "<div id=\"blu\">\n  <div style=\"text-align: center;\">\n    <font size=\"4\">\nThere are $total_listeners listeners locked <br> $availableslots available slots remaining


</font>\n  </div>\n</div>\n<div>\n  <div>\n    <p><b>Current song:</b> $song[1]</p>\n  </div>\n</div>\n<div>\n";
$i = "1";
while($i<=$servers)
      {
          print "  <div>\n";
if ($max[$i] > 0)
      {
      $percentage = round(($listeners[$i] / $max[$i] * 100));
      $timesby = (300 / $max[$i]);
      $barlength = round(($listeners[$i] * "$timesby"));
      }
if ($error[$i] = 1)
      {
?>
<br>
    <table width="400"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="25%" align="center"><b>Server <? print $i ?> (<? print $location[$i] ?>)</b>&nbsp;&nbsp;</td>
        <td width="75%" colspan="3" bgcolor="#eeeeee"><img src="images/<? if ($percentage == "100") { print "red-"; } ?>bar.gif" width="<? print $barlength ?>" height="12" alt="The server is at <? print $percentage; ?>% capacity"></td>
      </tr>
      <tr>
        <td width="25%">&nbsp;</td>
        <td width="25%">0%</td>
        <td width="25%" align="center">50%</td>
        <td width="25%" align="right">100%</td>
      </tr>
    </table>
<?
      }
else
      {
?>
<br>
    <table width="400"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="25%" align="center"><b>Server <? print $i ?> (<? print $location[$i] ?>)</b>&nbsp;&nbsp;</td>
        <td width="75%" colspan="3" bgcolor="#eeeeee">&nbsp;</td>
      </tr>
      <tr>
        <td width="25%">&nbsp;</td>
        <td width="25%">0%</td>
        <td width="25%" align="center">50%</td>
        <td width="25%" align="right">100%</td>
      </tr>
    </table>
<?

}

      $fp = @fsockopen($ip[$i],$port[$i],$errno,$errstr,$timeout);
      if (!$fp)
            {
            $listeners[$i] = "0";
            $msg[$i] = "<span class=\"red\">ERROR [Connection refused / Server down]</span>";
            $error[$i] = "1";
            }
      else
            {
            fputs($fp, "GET /7.html HTTP/1.0\r\nUser-Agent: Mozilla\r\n\r\n");
            while (!feof($fp))
                  {
                  $info = fgets($fp);
                  }
            $info = str_replace('<HTML><meta http-equiv="Pragma" content="no-cache"></head><body>', "", $info);
            $info = str_replace('</body></html>', "", $info);
            $stats = explode(',', $info);
            if (empty($stats[1]) )
                  {
                  $listeners[$i] = "0";
                  $msg[$i] = "<span class=\"red\">ERROR [There is no source connected]</span>";
                  $error[$i] = "1";
                  }
            else
                  {
                  if ($stats[1] == "1")
                        {
                        $song[$i] = $stats[6];
                        $listeners[$i] = $stats[0];
                        $max[$i] =  $stats[3];
                        $bitrate[$i] = $stats[5];
                        $peak[$i] = $stats[2];
     



$gi = geoip_open("geostats/GeoIP.dat", GEOIP_STANDARD);
for ($count = 0;$count < count($servers); $count++) {
      $shoutcast = new ShoutCast();
      $shoutcast->name = $location[$i];
      $shoutcast->host = $ip[$i];
      $shoutcast->port = $port[$i];
      $shoutcast->passwd = $password[$i];

      $listenersArray["$shoutcast->name"]["current"] = "NA";
      $listenersArray["$shoutcast->name"]["max"] = "NA";


      if ($shoutcast->openstats()) {
            // We got the XML, gogogo!..
                  
                  unset($from);
            if ($shoutcast->GetStreamStatus()) {
                  $listenersArray["$shoutcast->name"]["current"] = $shoutcast->GetCurrentListenersCount();
                  $listenersArray["$shoutcast->name"]["max"] = $shoutcast->GetMaxListenersCount();
                  $listenersArray["Total"]["current"] += $shoutcast->GetCurrentListenersCount();
                  $listenersArray["Total"]["max"] += $shoutcast->GetMaxListenersCount();


                  $listeners_geo = $shoutcast->GetListeners();
                        if (is_array($listeners_geo)) {
                        for ($is=0;$is<sizeof($listeners_geo);$is++) {
                              $country_id = geoip_country_id_by_name($gi, $listeners_geo[$is]["hostname"]);
                              $country_name = $GLOBALS['GEOIP_COUNTRY_NAMES'][$country_id];
                              if (!$country_id) {
                                    $country_id = 0;
                                    $country_name = "Earth(unidentified)";
                              }
                              $from["$country_id"]++;
                        }
                  } else {
                        echo "                                         
                                                Noone listens right now..
                                          \n";
                  }
                  echo "                                    \n";
            } else {
                  echo "Server is up, but no stream available..
                                         
                                    \n";
            }
      } else {
            // Ohhh, damnit..
            echo $shoutcast->geterror();
            echo "\n                                          \n                                    \n";
      }
      echo "                                    \n";

$count++;
}
geoip_close($gi);
?>
<br>
<table border=\"1\" id=\"table1\" cellspacing=\"0\" cellpadding=\"0\" align="center">
        <tr>
                <td>Flag</td>
                <td>Country</td>
                <td>Listeners</td>
        </tr>
        <tr>
<?php
arsort($from);
reset($from);
while (list($key, $val) = each($from)) {
      $country_code = $GLOBALS['GEOIP_COUNTRY_CODES']["$key"];
      $country_name = $GLOBALS['GEOIP_COUNTRY_NAMES']["$key"];
      if (!$country_code) {
            if (!$key && $key != 0) {
                  $country_code = $key;
            } else {
                  // I don't want to show unidentified hosts in ranking.
                  // $country_code = "N/A";
                  continue;
            }
      }
      echo"<td><img src=\"geostats/$country_code.GIF\" alt=\"flag of the $country_name\" /></td><td>$country_name</td>
                        <td>      $val</td></tr></table>
                        \n";
      $countrycount++;
      if ($countrycount >= $countrylimit) break;

}



           
                              $msg[$i] = "<span class=\"red\">Server is up at $bitrate[$i] kbps with $listeners[$i] of $max[$i] listeners</span>
<br>
<br>                                          
                                                 <b>Web Hits:</b> ".$shoutcast->GetWebHitsCount()."<br /> <b>Stream Hits:</b> ".$shoutcast->GetStreamHitsCount()."<br /> <b>Average Listen Time:</b> ".elapsedtime($shoutcast->GetAverageListenTime())."

<p><b>Listener Peak:</b> $peak[$i]";


                  }
                       
                  else
                        {
                        $listeners[$i] = "0";
                        $msg[$i] = "    <span class=\"red\">ERROR [Cannot get info from server]</span>";
                        $error[$i] = "1";
                        }
                  }
            }

print "    <p><b>Status:</b> $msg[$i]</p>\n  </div>\n  <div class=\"line\"> </div>\n";
      $i++;
      }
print "</div>\n";
$time_difference = "0"; // BST: 1 GMT: 0
$time_difference = ($time_difference * 60 * 60);
$time = date("h:ia", time() + $time_difference);
$date = date("jS F, Y", time() + 0);
print "<div>\n  <div>\n    <p><b>Live SHOUTcast statistics:</b> $date, $time</p>\n  </div>\n</div>\n";
?>
<div>
  <div>
0
 

Author Comment

by:MDauphinais1
ID: 18870472
unset($from);  fixed the problem.

Thanks for your help!
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 18870479
Glad i could help.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question