<?php
include("zorbconf.php");
include("zorblib.php");
include("zorblang$lang.php");
$imgpath="images/";
$get=$_GET;
$getkeys=array_keys($get);
$getcount=count($get) -1;
while($getcount>=0)
{
$key=$getkeys[$getcount];
$variable=$get[$key];
$$key=$variable;
$getcount=$getcount-1;
}
$width = 600;
$height = 150;
$lentb = 580;
$image = imagecreatetruecolor($width, $height);
$zorbblue = imagecreatefrompng($imgpath.'zorbblue.png');
$zorbtopblue = imagecreatefrompng($imgpath.'zorbtopblue.png');
$zorbred = imagecreatefrompng($imgpath.'zorbred.png');
$zorbtopred = imagecreatefrompng($imgpath.'zorbtopred.png');
if (!ereg("^(sum|([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3})$",$ip)) $ip="sum";
$year=sprintf("%02d",$year+0);if ($year==0) $year=strftime("%Y",time());
$month=sprintf("%02d",$month+0);if (($month<1) or ($month>12)) $month=strftime("%m",time());
$day=sprintf("%02d",$day+0);if (!checkdate($month,$day,$year)) $day=strftime("%d",time());
while (!checkdate($month,$day,$year)) $day=sprintf("%02d",$day-1);
// colors
$white = imagecolorallocate($image, $bkcolor1, $bkcolor2, $bkcolor3);
$white1 = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
$black = imagecolorallocate($image, 0x00, 0x00, 0x00);
$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
// draw something here
mysql_connect("$dbhost","$dbuname","$dbpass");
$tab=mysql_list_tables("$dbname");
$ret=mysql_query("select hostname from zhost where ip='$ip'");
$row = mysql_fetch_array($ret);
$hostname=$row["hostname"];
$totalin=0;$totalout=0;
if ($ip=="sum")
$ret=mysql_query("select sum(totin) totin,sum(totout) totout,hourid from zhours where hourid like '$year$month$day%' group by hourid order by hourid");
else
$ret=mysql_query("select totin,totout, hourid from zhours where hourid like '$year$month$day%' and ip='$ip'");
$i=0;
while ($i < 24)
{
$datain[$i] = 0;
$dataout[$i] = 0;
$i++;
}
$i=0;
while ($row = mysql_fetch_array($ret))
{
$hourid=substr($row["hourid"],8,2);
if (substr($hourid,0,1)==0) $hourid=substr($hourid,1,1);
$datain[$hourid] = $row["totin"];
$totalin=$totalin+$row["totin"];
$dataout[$hourid] = $row["totout"];
$totalout=$totalout+$row["totout"];
$i++;
}
$totalin=fncScale($totalin,2,0);
$totalout=fncScale($totalout,2,0);
$maxval = max($datain);
$nval = sizeof($datain);
if (max($dataout) > $maxval)
{
$maxval = max($dataout);
$nval = sizeof($dataout);
}
$vmargin = 20; // top (bottom) vertical margin for title (x-labels)
$hmargin = 50; // left horizontal margin for y-labels
$base = floor(($width - $hmargin) / $nval); // distance between columns
$ysize = $height - 2 * $vmargin; // y-size of plot
$xsize = $nval * $base; // x-size of plot
// plot frame
imagefill($image, 1, 1, $white);
imagerectangle($image, $hmargin, $vmargin, $lentb, $vmargin + $ysize, $black);
imagefill($image, $hmargin+2, $vmargin+2, $white1);
imagerectangle($image, $hmargin, $ypos, $lentb, $ypos + 20, $black);
imagefill($image, $hmargin+2, $ypos+2, white);
// title
$titlefont = 3;
if ($ip=="sum") $hostname=zorbtrans("Total");
$title = zorbtrans("Day")." $day ($hostname) ".zorbtrans("Dwld").": ".$totalin." ".zorbtrans("Upld").": ".$totalout;
// pixel-width of title
$txtsz = imagefontwidth($titlefont) * strlen($title);
$xpos = (int)($hmargin + ($xsize - $txtsz)/2); // center the title
$xpos = max(1, $xpos); // force positive coordinates
$ypos = 3; // distance from top
imagestring($image, $titlefont, $xpos, $ypos, $title , $white1);
// y labels and grid lines
$labelfont = 2;
$ngrid = 3; // number of grid lines
$dydat = $maxval / $ngrid; // data units between grid lines
$dypix = $ysize / ($ngrid + 1); // pixels between grid lines
for ($i = 0; $i <= ($ngrid + 1); $i++)
{
// iterate over y ticks
$ydat = fncScale(($i * $dydat),0,0); // height of grid line in units of data
$ypos = $vmargin + $ysize - (int)($i*$dypix); // height of grid line in pixels
$txtsz = imagefontwidth($labelfont) * strlen($ydat); // pixel-width of label
$txtht = imagefontheight($labelfont); // pixel-height of label
$xpos = (int)(($hmargin - $txtsz) / 2);
$xpos = max(1, $xpos);
imagestring($image, $labelfont, $xpos, $ypos - (int)($txtht/2), $ydat, $black);
if (!($i == 0) && !($i > $ngrid))
imageline($image, $hmargin - 3, $ypos, $lentb, $ypos, $gray);
// don't draw at Y=0 and top
}
// columns and x labels
$padding = 3; // half of spacing between columns
if ($dydat==0) $dydat=1;
$yscale = $ysize / (($ngrid+1) * $dydat); // pixels per data unit
for ($i = 0; list($xval, $yval1) = each($datain); $i++)
{
list($xval2, $yval2) = each($dataout);
// vertical columns
$ymax = $vmargin + $ysize;
$ymin1 = $ymax - (int)($yval1*$yscale);
$ymin2 = $ymax - (int)($yval2*$yscale);
$xmax = $hmargin + ($i+1)*$base - $padding;
$xmin = $hmargin + $i*$base + $padding;
$ymin=$ymin1;
if ($ymin>0)
{
if ($ymax-$ymin>0)
{
imagecopyresized($image, $zorbblue,$xmin,$ymin , 0, 0, 16,$ymax-$ymin, 8, 1);
imagecopyresized($image, $zorbtopblue,$xmin,$ymin-4 , 0, 0, 16,4, 8, 4);
}
}
$ymin=$ymin2;
if ($ymin>0)
{
if ($ymax-$ymin>0)
{
imagecopyresized($image, $zorbred,$xmin+4,$ymin , 0, 0, 16,$ymax-$ymin, 8, 1);
imagecopyresized($image, $zorbtopred,$xmin+4,$ymin-4 , 0, 0, 16,4, 8, 4);
}
}
// x labels
$txtsz = imagefontwidth($labelfont) * strlen($xval);
$xpos = $xmin + (int)(($base - $txtsz) / 2);
$xpos = max($xmin, $xpos);
$ypos = $ymax + 3; // distance from x axis
imagestring($image, $labelfont, $xpos, $ypos, $xval, $black);
}
// flush image
header("Content-type: image/png"); // or "Content-type: image/png"
imagepng($image); // or imagepng($image)
imagedestroy($image);
?>