jablonka.czprosek.czf

weathermap

Subversion Repositories:
[/] [Weathermap.class.php] - Diff between revs 30 and 85

Show entire file Ignore whitespace

Rev 30 Rev 85
Line 1... Line 1...
<?php <?php
// PHP Weathermap 0.92 // PHP Weathermap 0.97a
// Copyright Howard Jones, 2005-2007 howie@thingy.com // Copyright Howard Jones, 2005-2010 howie@thingy.com
// http://www.network-weathermap.com/ // http://www.network-weathermap.com/
// Released under the GNU Public License // Released under the GNU Public License
   
require_once "HTML_ImageMap.class.php"; require_once "HTML_ImageMap.class.php";
   
$WEATHERMAP_VERSION="0.92"; require_once "WeatherMap.functions.php";
  require_once "WeatherMapNode.class.php";
  require_once "WeatherMapLink.class.php";
   
  $WEATHERMAP_VERSION="0.97a";
$weathermap_debugging=FALSE; $weathermap_debugging=FALSE;
  $weathermap_map="";
  $weathermap_warncount=0;
  $weathermap_debug_suppress = array("processstring","mysprintf");
  $weathemap_lazycounter=0;
   
// Turn on ALL error reporting for now. // Turn on ALL error reporting for now.
error_reporting (E_ALL); // error_reporting (E_ALL|E_STRICT);
   
// parameterise the in/out stuff a bit // parameterise the in/out stuff a bit
define("IN",0); define("IN",0);
define("OUT",1); define("OUT",1);
define("WMCHANNELS",2); define("WMCHANNELS",2);
   
// Utility functions define('CONFIG_TYPE_LITERAL',0);
// Check for GD & PNG support This is just in here so that both the editor and CLI can use it without the need for another file define('CONFIG_TYPE_COLOR',1);
function module_checks()  
{  
if (!extension_loaded('gd'))  
{  
warn ("\n\nNo image (gd) extension is loaded. This is required by weathermap.\n\n");  
warn ("\nrun check.php to check PHP requirements.\n\n");  
   
return (FALSE); // some strings that are used in more than one place
} define('FMT_BITS_IN',"{link:this:bandwidth_in:%2k}");
  define('FMT_BITS_OUT',"{link:this:bandwidth_out:%2k}");
  define('FMT_UNFORM_IN',"{link:this:bandwidth_in}");
  define('FMT_UNFORM_OUT',"{link:this:bandwidth_out}");
  define('FMT_PERC_IN',"{link:this:inpercent:%.2f}%");
  define('FMT_PERC_OUT',"{link:this:outpercent:%.2f}%");
   
if (!function_exists('imagecreatefrompng')) // the fields within a spine triple
{ define("X",0);
warn ("Your GD php module doesn't support PNG format.\n"); define("Y",1);
warn ("\nrun check.php to check PHP requirements.\n\n"); define("DISTANCE",2);
return (FALSE);  
}  
   
if (!function_exists('imagecreatetruecolor'))  
{  
warn ("Your GD php module doesn't support truecolor.\n");  
warn ("\nrun check.php to check PHP requirements.\n\n");  
return (FALSE);  
}  
   
if (!function_exists('imagecopyresampled'))  
{  
warn ("Your GD php module doesn't support thumbnail creation (imagecopyresampled).\n");  
}  
return (TRUE);  
}  
   
function debug($string)  
{  
global $weathermap_debugging;  
   
if ($weathermap_debugging)  
{  
// use Cacti's debug log, if we are running from the poller  
if (function_exists('debug_log_insert') && (!function_exists('show_editor_startpage')))  
{ cacti_log(rtrim($string), true, "WEATHERMAP"); }  
else  
{  
$stderr=fopen('php://stderr', 'w');  
fwrite($stderr, $string);  
fclose ($stderr);  
}  
}  
}  
   
function warn($string)  
{  
// use Cacti's debug log, if we are running from the poller  
if (function_exists('cacti_log') && (!function_exists('show_editor_startpage')))  
{ cacti_log(rtrim($string), true, "WEATHERMAP"); }  
else  
{  
$stderr=fopen('php://stderr', 'w');  
fwrite($stderr, $string);  
fclose ($stderr);  
}  
}  
   
function js_escape($str, $wrap=TRUE)  
{  
$str=str_replace('\\', '\\\\', $str);  
$str=str_replace("'", "\\'", $str);  
   
if($wrap) $str="'" . $str . "'";  
   
return ($str);  
}  
   
function mysprintf($format,$value,$kilo=1000)  
{  
$output = "";  
   
debug("mysprintf: $format $value\n");  
if(preg_match("/%(\d*\.?\d*)k/",$format,$matches))  
{  
$spec = $matches[1];  
$places = 2;  
if($spec !='')  
{  
preg_match("/(\d*)\.?(\d*)/",$spec,$matches);  
if($matches[2] != '') $places=$matches[2];  
// we don't really need the justification (pre-.) part...  
}  
debug("KMGT formatting $value with $spec.\n");  
$result = nice_scalar($value, $kilo, $places);  
$output = preg_replace("/%".$spec."k/",$format,$result);  
}  
else  
{  
debug("Falling through to standard sprintf\n");  
$output = sprintf($format,$value);  
}  
return $output;  
}  
   
// wrapper around imagecolorallocate to try and re-use palette slots where possible  
function myimagecolorallocate($image, $red, $green, $blue)  
{  
// it's possible that we're being called early - just return straight away, in that case  
if(!isset($image)) return(-1);  
   
$existing=imagecolorexact($image, $red, $green, $blue);  
   
if ($existing > -1)  
return $existing;  
   
return (imagecolorallocate($image, $red, $green, $blue));  
}  
   
function render_colour($col)  
{  
if (($col[0] < 0) && ($col[1] < 0) && ($col[1] < 0)) { return 'none'; }  
else { return sprintf("%d %d %d", $col[0], $col[1], $col[2]); }  
}  
   
// take the same set of points that imagepolygon does, but don't close the shape  
function imagepolyline($image, $points, $npoints, $color)  
{  
for ($i=0; $i < ($npoints - 1);  
$i++) { imageline($image, $points[$i * 2], $points[$i * 2 + 1], $points[$i * 2 + 2], $points[$i * 2 + 3],  
$color); }  
}  
   
function imagecreatefromfile($filename)  
{  
$bgimage=NULL;  
$formats = imagetypes();  
if (is_readable($filename))  
{  
list($width, $height, $type, $attr) = getimagesize($filename);  
switch($type)  
{  
case IMAGETYPE_GIF:  
if(imagetypes() & IMG_GIF)  
{  
$bgimage=imagecreatefromgif($filename);  
}  
else  
{  
warn("Image file $filename is GIF, but GIF is not supported by your GD library.\n");  
}  
break;  
   
case IMAGETYPE_JPEG:  
if(imagetypes() & IMG_JPEG)  
{  
$bgimage=imagecreatefromjpeg($filename);  
}  
else  
{  
warn("Image file $filename is JPEG, but JPEG is not supported by your GD library.\n");  
}  
break;  
   
case IMAGETYPE_PNG:  
if(imagetypes() & IMG_PNG)  
{  
$bgimage=imagecreatefrompng($filename);  
}  
else  
{  
warn("Image file $filename is PNG, but PNG is not supported by your GD library.\n");  
}  
break;  
   
default:  
warn("Image file $filename wasn't recognised (type=$type). Check format is supported by your GD library.\n");  
break;  
}  
}  
else  
{  
warn("Image file $filename is unreadable. Check permissions.\n");  
}  
return $bgimage;  
}  
   
// rotate a list of points around cx,cy by an angle in radians, IN PLACE  
function RotateAboutPoint(&$points, $cx,$cy, $angle=0)  
{  
$npoints = count($points)/2;  
   
for($i=0;$i<$npoints;$i++)  
{  
$ox = $points[$i*2] - $cx;  
$oy = $points[$i*2+1] - $cy;  
$rx = $ox * cos($angle) - $oy*sin($angle);  
$ry = $oy * cos($angle) + $ox*sin($angle);  
   
$points[$i*2] = $rx + $cx;  
$points[$i*2+1] = $ry + $cy;  
}  
}  
   
// calculate the points for a span of the curve. We pass in the distance so far, and the array index, so that  
// the chunk of array generated by this function can be array_merged with existing points from before.  
// Considering how many array functions there are, PHP has horrible list support  
// Each point is a 3-tuple - x,y,distance - which is used later to figure out where the 25%, 50% marks are on the curve  
function calculate_catmull_rom_span($startn, $startdistance, $numsteps, $x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3)  
{  
$Ap_x=-$x0 + 3 * $x1 - 3 * $x2 + $x3;  
$Bp_x=2 * $x0 - 5 * $x1 + 4 * $x2 - $x3;  
$Cp_x=-$x0 + $x2;  
$Dp_x=2 * $x1;  
   
$Ap_y=-$y0 + 3 * $y1 - 3 * $y2 + $y3;  
$Bp_y=2 * $y0 - 5 * $y1 + 4 * $y2 - $y3;  
$Cp_y=-$y0 + $y2;  
$Dp_y=2 * $y1;  
   
$d=2;  
$n=$startn;  
$distance=$startdistance;  
   
$lx=$x0;  
$ly=$y0;  
   
$allpoints[]=array  
(  
$x0,  
$y0,  
$distance  
);  
   
for ($i=0; $i <= $numsteps; $i++)  
{  
$t=$i / $numsteps;  
$t2=$t * $t;  
$t3=$t2 * $t;  
$x=(($Ap_x * $t3) + ($Bp_x * $t2) + ($Cp_x * $t) + $Dp_x) / $d;  
$y=(($Ap_y * $t3) + ($Bp_y * $t2) + ($Cp_y * $t) + $Dp_y) / $d;  
   
if ($i > 0)  
{  
$step=sqrt((($x - $lx) * ($x - $lx)) + (($y - $ly) * ($y - $ly)));  
$distance=$distance + $step;  
$allpoints[$n]=array  
(  
$x,  
$y,  
$distance  
);  
   
$n++;  
}  
   
$lx=$x;  
$ly=$y;  
}  
   
return array($allpoints, $distance, $n);  
}  
   
function find_distance_coords(&$pointarray,$distance)  
{  
// We find the nearest lower point for each distance,  
// then linearly interpolate to get a more accurate point  
// this saves having quite so many points-per-curve  
   
$index=find_distance($pointarray, $distance);  
   
$ratio=($distance - $pointarray[$index][2]) / ($pointarray[$index + 1][2] - $pointarray[$index][2]);  
$x = $pointarray[$index][0] + $ratio * ($pointarray[$index + 1][0] - $pointarray[$index][0]);  
$y = $pointarray[$index][1] + $ratio * ($pointarray[$index + 1][1] - $pointarray[$index][1]);  
   
return(array($x,$y,$index));  
}  
   
function find_distance_coords_angle(&$pointarray,$distance)  
{  
// This is the point we need  
list($x,$y,$index) = find_distance_coords($pointarray,$distance);  
   
// now to find one either side of it, to get a line to find the angle of  
$left = $index;  
$right = $left+1;  
$max = count($pointarray)-1;  
// if we're right up against the last point, then step backwards one  
if($right>=$max)  
{  
$left--;  
$right--;  
}  
# if($left<=0) { $left = 0; }  
   
$x1 = $pointarray[$left][0];  
$y1 = $pointarray[$left][1];  
   
$x2 = $pointarray[$right][0];  
$y2 = $pointarray[$right][1];  
   
$dx = $x2 - $x1;  
$dy = $y2 - $y1;  
   
$angle = rad2deg(atan2(-$dy,$dx));  
   
return(array($x,$y,$index,$angle));  
}  
   
// return the index of the point either at (unlikely) or just before the target distance  
// we will linearly interpolate afterwards to get a true point - pointarray is an array of 3-tuples produced by the function above  
function find_distance(&$pointarray, $distance)  
{  
$left=0;  
$right=count($pointarray) - 1;  
   
if ($left == $right)  
return ($left);  
   
// if it's a point past the end of the line, then just return the end of the line  
// Weathermap should *never* ask for this, anyway  
if ($pointarray[$right][2] < $distance) { return ($right); }  
   
// if somehow we have a 0-length curve, then don't try and search, just give up  
// in a somewhat predictable manner  
if ($pointarray[$left][2] == $pointarray[$right][2]) { return ($left); }  
   
while ($left <= $right)  
{  
$mid=floor(($left + $right) / 2);  
   
if (($pointarray[$mid][2] < $distance) && ($pointarray[$mid + 1][2] >= $distance)) { return $mid; }  
   
if ($distance <= $pointarray[$mid][2]) { $right=$mid - 1; }  
else { $left=$mid + 1; }  
}  
   
print "FELL THROUGH\n";  
die ("Howie's crappy binary search is wrong after all.\n");  
}  
   
// Give a list of key points, calculate a curve through them  
// return value is an array of triples (x,y,distance)  
function calc_curve(&$in_xarray, &$in_yarray,$pointsperspan = 12)  
{  
// search through the point list, for consecutive duplicate points  
// (most common case will be a straight link with both NODEs at the same place, I think)  
// strip those out, because they'll break the binary search/centre-point stuff  
   
$last_x=NULL;  
$last_y=NULL;  
   
for ($i=0; $i < count($in_xarray); $i++)  
{  
if (($in_xarray[$i] == $last_x) && ($in_yarray[$i] == $last_y)) { debug  
("Dumping useless duplicate point on curve\n"); }  
else  
{  
$xarray[]=$in_xarray[$i];  
$yarray[]=$in_yarray[$i];  
}  
   
$last_x=$in_xarray[$i];  
$last_y=$in_yarray[$i];  
}  
   
// only proceed if we still have at least two points!  
if(count($xarray) <= 1)  
{  
warn ("Arrow not drawn, as it's 1-dimensional.\n");  
return (array(NULL, NULL, NULL, NULL));  
}  
   
// duplicate the first and last points, so that all points are drawn  
// (C-R normally would draw from x[1] to x[n-1]  
array_unshift($xarray, $xarray[0]);  
array_unshift($yarray, $yarray[0]);  
   
$x=array_pop($xarray);  
$y=array_pop($yarray);  
array_push($xarray, $x);  
array_push($xarray, $x);  
array_push($yarray, $y);  
array_push($yarray, $y);  
   
$npoints=count($xarray);  
   
$curvepoints=array  
(  
);  
   
// add in the very first point manually (the calc function skips this one to avoid duplicates, which mess up the distance stuff)  
$curvepoints[]=array  
(  
$xarray[0],  
$yarray[0],  
0  
);  
   
$np=0;  
$distance=0;  
   
for ($i=0; $i < ($npoints - 3); $i++)  
{  
list($newpoints,  
$distance,  
$np)=calculate_catmull_rom_span($np, $distance, $pointsperspan, $xarray[$i],  
$yarray[$i], $xarray[$i + 1], $yarray[$i + 1], $xarray[$i + 2],  
$yarray[$i + 2], $xarray[$i + 3], $yarray[$i + 3]);  
$curvepoints=$curvepoints + $newpoints;  
}  
   
return ($curvepoints);  
}  
   
function calc_arrowsize($width,&$map,$linkname)  
{  
$arrowlengthfactor=4;  
$arrowwidthfactor=2;  
   
if ($map->links[$linkname]->arrowstyle == 'compact')  
{  
$arrowlengthfactor=1;  
$arrowwidthfactor=1;  
}  
   
if (preg_match('/(\d+) (\d+)/', $map->links[$linkname]->arrowstyle, $matches))  
{  
$arrowlengthfactor=$matches[1];  
$arrowwidthfactor=$matches[2];  
}  
   
$arrowsize = $width * $arrowlengthfactor;  
$arrowwidth = $width * $arrowwidthfactor;  
   
return( array($arrowsize,$arrowwidth) );  
}  
   
// top-level function that takes a two lists to define some points, and draws a weathermap link  
// - this takes care of all the extras, like arrowheads, and where to put the bandwidth labels  
// curvepoints is an array of the points the curve passes through  
// width is the link width (the actual width is twice this)  
// outlinecolour is a GD colour reference  
// fillcolours is an array of two more colour references, one for the out, and one for the in spans  
function draw_curve($image, &$curvepoints, $width, $outlinecolour, $comment_colour, $fillcolours, $linkname, &$map,  
$q2_percent=50)  
{  
// now we have a 'spine' - all the central points for this curve.  
// time to flesh it out to the right width, and figure out where to draw arrows and bandwidth boxes...  
$unidirectional = FALSE;  
   
// get the full length of the curve from the last point  
$totaldistance = $curvepoints[count($curvepoints)-1][2];  
// find where the in and out arrows will join (normally halfway point)  
$halfway = $totaldistance * ($q2_percent/100);  
   
$dirs = array(OUT,IN);  
   
// for a unidirectional map, we just ignore the second half (direction = -1)  
if($unidirectional)  
{  
$halfway = $totaldistance;  
$dirs = array(OUT);  
}  
   
// loop increment, start point, width, labelpos, fillcolour, outlinecolour, commentpos  
$arrowsettings[OUT] = array(+1, 0, $width, 0, $fillcolours[OUT], $outlinecolour, 5);  
$arrowsettings[IN] = array(-1, count($curvepoints) - 1, $width, 0, $fillcolours[IN], $outlinecolour, 95);  
   
// we calculate the arrow size up here, so that we can decide on the  
// minimum length for a link. The arrowheads are the limiting factor.  
list($arrowsize,$arrowwidth) = calc_arrowsize($width,$map,$linkname);  
   
// the 2.7 here is empirical. It ought to be 2 in theory.  
// in practice, a link this short is useless anyway, especially with bwlabels.  
$minimumlength = 2.7*$arrowsize;  
   
# warn("$linkname: Total: $totaldistance $arrowsize $arrowwidth $minimumlength\n");  
if($totaldistance <= $minimumlength)  
{  
warn("Skipping drawing very short link ($linkname). Impossible to draw! Try changing WIDTH or ARROWSTYLE?\n");  
return;  
}  
   
   
list($halfway_x,$halfway_y,$halfwayindex) = find_distance_coords($curvepoints,$halfway);  
   
// loop over direction here  
// direction is 1.0 for the first half (forwards through the pointlist), and -1.0 for the second half (backwards from the end)  
// - used as a multiplier on anything that looks forwards or backwards through the list  
   
foreach ($dirs as $dir)  
{  
$direction = $arrowsettings[$dir][0];  
// this is the last index before the arrowhead starts  
list($pre_mid_x,$pre_mid_y,$pre_midindex) = find_distance_coords($curvepoints,$halfway - $direction * $arrowsize);  
   
$there_points=array();  
$back_points=array();  
$arrowpoints=array();  
   
# if ($direction < 0) { $start=count($curvepoints) - 1; }  
# else { $start=0; }  
$start = $arrowsettings[$dir][1];  
   
for ($i=$start; $i != $pre_midindex; $i+=$direction)  
{  
// for each point on the spine, produce two points normal to it's direction,  
// each is $width away from the spine, but we build up the two lists in the opposite order,  
// so that when they are joined together, we get one continuous line  
   
$dx=$curvepoints[$i + $direction][0] - $curvepoints[$i][0];  
$dy=$curvepoints[$i + $direction][1] - $curvepoints[$i][1];  
$l=sqrt(($dx * $dx) + ($dy * $dy));  
$nx=$dy / $l;  
$ny=-$dx / $l;  
   
$there_points[]=$curvepoints[$i][0] + $direction * $width * $nx;  
$there_points[]=$curvepoints[$i][1] + $direction * $width * $ny;  
   
$back_points[]=$curvepoints[$i][0] - $direction * $width * $nx;  
$back_points[]=$curvepoints[$i][1] - $direction * $width * $ny;  
}  
   
// all the normal line is done, now lets add an arrowhead on  
   
$adx=($halfway_x - $pre_mid_x);  
$ady=($halfway_y - $pre_mid_y);  
$l=sqrt(($adx * $adx) + ($ady * $ady));  
   
$anx=$ady / $l;  
$any=-$adx / $l;  
   
$there_points[]=$pre_mid_x + $direction * $width * $anx;  
$there_points[]=$pre_mid_y + $direction * $width * $any;  
   
$there_points[]=$pre_mid_x + $direction * $arrowwidth * $anx;  
$there_points[]=$pre_mid_y + $direction * $arrowwidth * $any;  
   
$there_points[]=$halfway_x;  
$there_points[]=$halfway_y;  
   
$there_points[]=$pre_mid_x - $direction * $arrowwidth * $anx;  
$there_points[]=$pre_mid_y - $direction * $arrowwidth * $any;  
   
$there_points[]=$pre_mid_x - $direction * $width * $anx;  
$there_points[]=$pre_mid_y - $direction * $width * $any;  
   
// all points done, now combine the lists, and produce the final result.  
   
$y=array_pop($back_points);  
$x=array_pop($back_points);  
do  
{  
$there_points[]=$x;  
$there_points[]=$y;  
$y=array_pop($back_points);  
$x=array_pop($back_points);  
} while (!is_null($y));  
   
$arrayindex=0;  
   
if ($direction < 0) $arrayindex=1;  
   
if (!is_null($fillcolours[$arrayindex]))  
{ imagefilledpolygon($image, $there_points, count($there_points) / 2, $arrowsettings[$dir][4]); }  
   
$map->imap->addArea("Polygon", "LINK:" . $linkname, '', $there_points);  
debug ("Adding Poly imagemap for $linkname\n");  
   
if (!is_null($outlinecolour))  
imagepolygon($image, $there_points, count($there_points) / 2, $arrowsettings[$dir][5]);  
}  
}  
   
function unformat_number($instring, $kilo = 1000)  
{  
$matches=0;  
$number=0;  
   
if (preg_match("/([0-9\.]+)(M|G|K|T|m|u)/", $instring, $matches))  
{  
$number=floatval($matches[1]);  
   
if ($matches[2] == 'K') { $number=$number * $kilo; }  
if ($matches[2] == 'M') { $number=$number * $kilo * $kilo; }  
if ($matches[2] == 'G') { $number=$number * $kilo * $kilo * $kilo; }  
if ($matches[2] == 'T') { $number=$number * $kilo * $kilo * $kilo * $kilo; }  
// new, for absolute datastyle. Think seconds.  
if ($matches[2] == 'm') { $number=$number / $kilo; }  
if ($matches[2] == 'u') { $number=$number / ($kilo * $kilo); }  
}  
else { $number=floatval($instring); }  
   
return ($number);  
}  
   
// given a compass-point, and a width & height, return a tuple of the x,y offsets  
function calc_offset($offsetstring, $width, $height)  
{  
if(preg_match("/^([-+]?\d+):([-+]?\d+)$/",$offsetstring,$matches))  
{  
debug("Numeric Offset found\n");  
return(array($matches[1],$matches[2]));  
}  
else  
{  
switch (strtoupper($offsetstring))  
{  
case 'N':  
return (array(0, -$height / 2));  
   
break;  
   
case 'S':  
return (array(0, $height / 2));  
   
break;  
   
case 'E':  
return (array(+$width / 2, 0));  
   
break;  
   
case 'W':  
return (array(-$width / 2, 0));  
   
break;  
   
case 'NW':  
return (array(-$width / 2, -$height / 2));  
   
break;  
   
case 'NE':  
return (array($width / 2, -$height / 2));  
   
break;  
   
case 'SW':  
return (array(-$width / 2, $height / 2));  
   
break;  
   
case 'SE':  
return (array($width / 2, $height / 2));  
   
break;  
   
default:  
return (array(0, 0));  
   
break;  
}  
}  
}  
   
// These next two are based on perl's Number::Format module  
// by William R. Ward, chopped down to just what I needed  
   
function format_number($number, $precision = 2, $trailing_zeroes = 0)  
{  
$sign=1;  
   
if ($number < 0)  
{  
$number=abs($number);  
$sign=-1;  
}  
   
$number=round($number, $precision);  
$integer=intval($number);  
   
if (strlen($integer) < strlen($number)) { $decimal=substr($number, strlen($integer) + 1); }  
   
if (!isset($decimal)) { $decimal=''; }  
   
$integer=$sign * $integer;  
   
if ($decimal == '') { return ($integer); }  
else { return ($integer . "." . $decimal); }  
}  
   
function nice_bandwidth($number, $kilo = 1000,$decimals=1)  
{  
$suffix='';  
   
if ($number == 0)  
return '0';  
   
$mega=$kilo * $kilo;  
$giga=$mega * $kilo;  
$tera=$giga * $kilo;  
   
if ($number > $tera)  
{  
$number/=$tera;  
$suffix="T";  
}  
elseif ($number > $giga)  
{  
$number/=$giga;  
$suffix="G";  
}  
elseif ($number > $mega)  
{  
$number/=$mega;  
$suffix="M";  
}  
elseif ($number > $kilo)  
{  
$number/=$kilo;  
$suffix="K";  
}  
   
$result=format_number($number, $decimals) . $suffix;  
return ($result);  
}  
   
function nice_scalar($number, $kilo = 1000, $decimals=1)  
{  
$suffix='';  
   
if ($number == 0)  
return '0';  
   
$mega=$kilo * $kilo;  
$giga=$mega * $kilo;  
$tera=$giga * $kilo;  
   
if ($number > $tera)  
{  
$number/=$tera;  
$suffix="T";  
}  
elseif ($number > $giga)  
{  
$number/=$giga;  
$suffix="G";  
}  
elseif ($number > $mega)  
{  
$number/=$mega;  
$suffix="M";  
}  
elseif ($number > $kilo)  
{  
$number/=$kilo;  
$suffix="K";  
}  
elseif ($number < (1 / ($kilo)))  
{  
$number=$number * $mega;  
$suffix="u";  
}  
elseif ($number < 1)  
{  
$number=$number * $kilo;  
$suffix="m";  
}  
   
$result=format_number($number, $decimals) . $suffix;  
return ($result);  
}  
   
   
// ***********************************************  
   
// we use enough points in various places to make it worth a small class to save some variable-pairs.  
class Point  
{  
var $x, $y;  
function Point($x=0,$y=0)  
{  
$this->x = $x;  
$this->y = $y;  
}  
}  
   
// similarly for 2D vectors  
class Vector  
{  
var $dx, $dy;  
   
function Vector($dx=0,$dy=0)  
{  
$this->dx = $dx;  
$this->dy = $dy;  
}  
   
function normalise()  
{  
$len = $this->length();  
$this->dx = $this->dx/$len;  
$this->dy = $this->dy/$len;  
}  
   
function length()  
{  
return( sqrt(($this->dx)*($this->dx) + ($this->dy)*($this->dy)) );  
}  
}  
   
// *********************************************** // ***********************************************
   
// template class for data sources. All data sources extend this class. // template class for data sources. All data sources extend this class.
// I really wish PHP4 would just die overnight // I really wish PHP4 would just die overnight
Line 834... Line 59...
   
// the actual ReadData // the actual ReadData
// returns an array of two values (in,out). -1,-1 if it couldn't get valid data // returns an array of two values (in,out). -1,-1 if it couldn't get valid data
// configline is passed in, to allow for better error messages // configline is passed in, to allow for better error messages
// itemtype and itemname may be used as part of the target (e.g. for TSV source line) // itemtype and itemname may be used as part of the target (e.g. for TSV source line)
function ReadData($targetstring, $configline, $itemtype, $itemname, $map) { return (array(-1,-1)); } // function ReadData($targetstring, $configline, $itemtype, $itemname, $map) { return (array(-1,-1)); }
  function ReadData($targetstring, &$map, &$item)
  {
  return(array(-1,-1));
  }
   
  // pre-register a target + context, to allow a plugin to batch up queries to a slow database, or snmp for example
  function Register($targetstring, &$map, &$item)
  {
   
  }
   
  // called before ReadData, to allow plugins to DO the prefetch of targets known from Register
  function Prefetch()
  {
   
  }
} }
   
// template classes for the pre- and post-processor plugins // template classes for the pre- and post-processor plugins
class WeatherMapPreProcessor class WeatherMapPreProcessor
{ {
Line 860... Line 101...
var $hints = array(); var $hints = array();
var $inherit_fieldlist; var $inherit_fieldlist;
   
function add_note($name,$value) function add_note($name,$value)
{ {
  debug("Adding note $name='$value' to ".$this->name."\n");
$this->notes[$name] = $value; $this->notes[$name] = $value;
} }
   
   
function get_note($name) function get_note($name)
{ {
if(isset($this->notes[$name])) if(isset($this->notes[$name]))
{ {
  // debug("Found note $name in ".$this->name." with value of ".$this->notes[$name].".\n");
return($this->notes[$name]); return($this->notes[$name]);
} }
else else
{ {
  // debug("Looked for note $name in ".$this->name." which doesn't exist.\n");
return(NULL); return(NULL);
} }
} }
   
function add_hint($name,$value) function add_hint($name,$value)
{ {
  debug("Adding hint $name='$value' to ".$this->name."\n");
$this->hints[$name] = $value; $this->hints[$name] = $value;
# warn("Adding hint $name to ".$this->my_type()."/".$this->name."\n"); # warn("Adding hint $name to ".$this->my_type()."/".$this->name."\n");
} }
   
   
function get_hint($name) function get_hint($name)
{ {
if(isset($this->hints[$name])) if(isset($this->hints[$name]))
{ {
  // debug("Found hint $name in ".$this->name." with value of ".$this->hints[$name].".\n");
return($this->hints[$name]); return($this->hints[$name]);
} }
else else
{ {
  // debug("Looked for hint $name in ".$this->name." which doesn't exist.\n");
return(NULL); return(NULL);
} }
} }
  }
   
  class WeatherMapConfigItem
  {
  var $defined_in;
  var $name;
  var $value;
  var $type;
} }
   
// The 'things on the map' class. More common code (mainly variables, actually) // The 'things on the map' class. More common code (mainly variables, actually)
class WeatherMapItem extends WeatherMapBase class WeatherMapItem extends WeatherMapBase
{ {
Line 907... Line 161...
var $infourl; var $infourl;
var $overliburl; var $overliburl;
var $overlibwidth, $overlibheight; var $overlibwidth, $overlibheight;
var $overlibcaption; var $overlibcaption;
var $my_default; var $my_default;
  var $defined_in;
  var $config_override; # used by the editor to allow text-editing
   
function my_type() { return "ITEM"; } function my_type() { return "ITEM"; }
   
} }
   
   
class WeatherMapNode extends WeatherMapItem  
{  
var $owner;  
var $x,  
$y;  
var $original_x, $original_y,$relative_resolved;  
var $width,  
$height;  
var $label, $proclabel,  
$labelfont;  
var $name;  
var $infourl;  
var $notes;  
var $overliburl;  
var $overlibwidth,  
$overlibheight;  
var $overlibcaption;  
var $maphtml;  
var $selected = 0;  
var $iconfile, $iconscalew, $iconscaleh;  
var $targets = array();  
var $bandwidth_in, $bandwidth_out;  
var $inpercent, $outpercent;  
var $max_bandwidth_in, $max_bandwidth_out;  
var $max_bandwidth_in_cfg, $max_bandwidth_out_cfg;  
var $labeloffset, $labeloffsetx, $labeloffsety;  
   
var $inherit_fieldlist;  
   
var $labelbgcolour;  
var $labeloutlinecolour;  
var $labelfontcolour;  
var $labelfontshadowcolour;  
var $cachefile;  
var $usescale;  
var $inscalekey,$outscalekey;  
# var $incolour,$outcolour;  
var $scalevar;  
var $notestext;  
var $image;  
var $centre_x, $centre_y;  
var $relative_to;  
   
function WeatherMapNode()  
{  
$this->inherit_fieldlist=array  
(  
'my_default' => NULL,  
'label' => '',  
'proclabel' => '',  
'usescale' => 'DEFAULT',  
'scalevar' => 'in',  
'labelfont' => 3,  
'relative_to' => '',  
'relative_resolved' => FALSE,  
'x' => 0,  
'y' => 0,  
'inscalekey'=>'', 'outscalekey'=>'',  
#'incolour'=>-1,'outcolour'=>-1,  
'original_x' => 0,  
'original_y' => 0,  
'inpercent'=>0,  
'outpercent'=>0,  
'iconfile' => '',  
'iconscalew' => 0,  
'iconscaleh' => 0,  
'targets' => array(),  
'infourl' => '',  
'notestext' => '',  
'notes' => array(),  
'hints' => array(),  
'overliburl' => '',  
'overlibwidth' => 0,  
'overlibheight' => 0,  
'overlibcaption' => '',  
'labeloutlinecolour' => array(0, 0, 0),  
'labelbgcolour' => array(255, 255, 255),  
'labelfontcolour' => array(0, 0, 0),  
'labelfontshadowcolour' => array(-1, -1, -1),  
   
'labeloffset' => '',  
'labeloffsetx' => 0,  
'labeloffsety' => 0,  
'max_bandwidth_in' => 100,  
'max_bandwidth_out' => 100,  
'max_bandwidth_in_cfg' => '100',  
'max_bandwidth_out_cfg' => '100'  
);  
   
$this->width = 0;  
$this->height = 0;  
$this->centre_x = 0;  
$this->centre_y = 0;  
$this->image = NULL;  
}  
   
function my_type() { return "NODE"; }  
   
// make a mini-image, containing this node and nothing else  
// figure out where the real NODE centre is, relative to the top-left corner.  
function pre_render($im, &$map)  
{  
// apparently, some versions of the gd extension will crash  
// if we continue...  
if($this->label == '' && $this->iconfile=='') return;  
   
// start these off with sensible values, so that bbox  
// calculations are easier.  
$icon_x1 = $this->x; $icon_x2 = $this->x;  
$icon_y1 = $this->y; $icon_y2 = $this->y;  
$label_x1 = $this->x; $label_x2 = $this->x;  
$label_y1 = $this->y; $label_y2 = $this->y;  
$boxwidth = 0; $boxheight = 0;  
$icon_w = 0;  
$icon_h = 0;  
   
// figure out a bounding rectangle for the label  
if ($this->label != '')  
{  
$padding = 4.0;  
$padfactor = 1.0;  
   
$this->proclabel = $map->ProcessString($this->label,$this);  
   
list($strwidth, $strheight) = $map->myimagestringsize($this->labelfont, $this->proclabel);  
   
$boxwidth = ($strwidth * $padfactor) + $padding;  
$boxheight = ($strheight * $padfactor) + $padding;  
   
debug ("Node->pre_render: Label Metrics are: $strwidth x $strheight -> $boxwidth x $boxheight\n");  
   
$label_x1 = $this->x - ($boxwidth / 2);  
$label_y1 = $this->y - ($boxheight / 2);  
   
$label_x2 = $this->x + ($boxwidth / 2);  
$label_y2 = $this->y + ($boxheight / 2);  
   
$txt_x = $this->x - ($strwidth / 2);  
$txt_y = $this->y + ($strheight / 2);  
   
# $this->width = $boxwidth;  
# $this->height = $boxheight;  
$map->nodes[$this->name]->width = $boxwidth;  
$map->nodes[$this->name]->height = $boxheight;  
   
# print "TEXT at $txt_x , $txt_y\n";  
   
}  
   
// figure out a bounding rectangle for the icon  
if ($this->iconfile != '')  
{  
$this->iconfile = $map->ProcessString($this->iconfile ,$this);  
if (is_readable($this->iconfile))  
{  
imagealphablending($im, true);  
// draw the supplied icon, instead of the labelled box  
   
$icon_im = imagecreatefromfile($this->iconfile);  
# $icon_im = imagecreatefrompng($this->iconfile);  
   
if ($icon_im)  
{  
$icon_w = imagesx($icon_im);  
$icon_h = imagesy($icon_im);  
   
if(($this->iconscalew * $this->iconscaleh) > 0)  
{  
imagealphablending($icon_im, true);  
   
debug("SCALING ICON here\n");  
if($icon_w > $icon_h)  
{  
$scalefactor = $icon_w/$this->iconscalew;  
}  
else  
{  
$scalefactor = $icon_h/$this->iconscaleh;  
}  
$new_width = $icon_w / $scalefactor;  
$new_height = $icon_h / $scalefactor;  
$scaled = imagecreatetruecolor($new_width, $new_height);  
imagealphablending($scaled,false);  
imagecopyresampled($scaled, $icon_im, 0, 0, 0, 0, $new_width, $new_height, $icon_w, $icon_h);  
imagedestroy($icon_im);  
$icon_im = $scaled;  
$icon_w = imagesx($icon_im);  
$icon_h = imagesy($icon_im);  
}  
   
$icon_x1 = $this->x - $icon_w / 2;  
$icon_y1 = $this->y - $icon_h / 2;  
$icon_x2 = $this->x + $icon_w / 2;  
$icon_y2 = $this->y + $icon_h / 2;  
   
# $this->width = imagesx($icon_im);  
# $this->height = imagesy($icon_im);  
$map->nodes[$this->name]->width = imagesx($icon_im);  
$map->nodes[$this->name]->height = imagesy($icon_im);  
   
$map->imap->addArea("Rectangle", "NODE:" . $this->name, '', array($icon_x1, $icon_y1, $icon_x2, $icon_y2));  
   
}  
else { warn ("Couldn't open PNG ICON: " . $this->iconfile . " - is it a PNG?\n"); }  
}  
else  
{  
warn ("ICON " . $this->iconfile . " does not exist, or is not readable. Check path and permissions.\n");  
}  
}  
   
// do any offset calculations  
   
if ( ($this->labeloffset != '') && (($this->iconfile != '')) )  
{  
$this->labeloffsetx = 0;  
$this->labeloffsety = 0;  
   
list($dx, $dy) = calc_offset($this->labeloffset,  
($icon_w + $boxwidth -1),  
($icon_h + $boxheight)  
);  
   
$this->labeloffsetx = $dx;  
$this->labeloffsety = $dy;  
   
}  
   
$label_x1 += $this->labeloffsetx;  
$label_x2 += $this->labeloffsetx;  
$label_y1 += $this->labeloffsety;  
$label_y2 += $this->labeloffsety;  
   
if($this->label != '')  
{  
$map->imap->addArea("Rectangle", "NODE:" . $this->name, '', array($label_x1, $label_y1, $label_x2, $label_y2));  
}  
   
// work out the bounding box of the whole thing  
   
$bbox_x1 = min($label_x1,$icon_x1);  
$bbox_x2 = max($label_x2,$icon_x2)+1;  
$bbox_y1 = min($label_y1,$icon_y1);  
$bbox_y2 = max($label_y2,$icon_y2)+1;  
   
# imagerectangle($im,$bbox_x1,$bbox_y1,$bbox_x2,$bbox_y2,$map->selected);  
# imagerectangle($im,$label_x1,$label_y1,$label_x2,$label_y2,$map->black);  
# imagerectangle($im,$icon_x1,$icon_y1,$icon_x2,$icon_y2,$map->black);  
   
// create TWO imagemap entries - one for the label and one for the icon  
// (so we can have close-spaced icons better)  
   
   
$temp_width = $bbox_x2-$bbox_x1;  
$temp_height = $bbox_y2-$bbox_y1;  
// create an image of that size and draw into it  
$node_im=imagecreatetruecolor($temp_width,$temp_height );  
// ImageAlphaBlending($node_im, FALSE);  
imageSaveAlpha($node_im, TRUE);  
   
$nothing=imagecolorallocatealpha($node_im,128,0,0,127);  
imagefill($node_im, 0, 0, $nothing);  
   
// imagefilledrectangle($node_im,0,0,$temp_width,$temp_height, $nothing);  
   
$label_x1 -= $bbox_x1;  
$label_x2 -= $bbox_x1;  
$label_y1 -= $bbox_y1;  
$label_y2 -= $bbox_y1;  
   
$icon_x1 -= $bbox_x1;  
$icon_x2 -= $bbox_x1;  
$icon_y1 -= $bbox_y1;  
$icon_y2 -= $bbox_y1;  
   
   
// Draw the icon, if any  
if(isset($icon_im))  
{  
imagecopy($node_im, $icon_im, $icon_x1, $icon_y1, 0, 0, imagesx($icon_im), imagesy($icon_im));  
imagedestroy($icon_im);  
}  
   
// Draw the label, if any  
if ($this->label != '')  
{  
$txt_x -= $bbox_x1;  
$txt_x += $this->labeloffsetx;  
$txt_y -= $bbox_y1;  
$txt_y += $this->labeloffsety;  
   
# print "FINAL TEXT at $txt_x , $txt_y\n";  
   
// if there's an icon, then you can choose to have no background  
   
$col = -1;  
   
// if a target is specified, and you haven't forced no background, then the background will  
// come from the SCALE in USESCALE  
if( !empty($this->targets) && $this->usescale != 'none' )  
{  
$pc = 0;  
   
if($this->scalevar == 'in')  
{  
$pc = $this->inpercent;  
   
}  
if($this->scalevar == 'out')  
{  
$pc = $this->outpercent;  
   
}  
   
// debug("Choosing NODE BGCOLOR for ".$this->name." based on $pc %\n");  
   
list($col,$node_scalekey) = $map->ColourFromPercent($pc, $this->usescale,$this->name);  
// $map->nodes[$this->name]->scalekey = $node_scalekey;  
}  
elseif($this->labelbgcolour != array(-1,-1,-1))  
{  
$col=myimagecolorallocate($node_im, $this->labelbgcolour[0], $this->labelbgcolour[1], $this->labelbgcolour[2]);  
}  
   
if($col != -1)  
{  
imagefilledrectangle($node_im, $label_x1, $label_y1, $label_x2, $label_y2, $col);  
}  
   
if ($this->selected)  
{  
imagerectangle($node_im, $label_x1, $label_y1, $label_x2, $label_y2, $map->selected);  
// would be nice if it was thicker, too...  
imagerectangle($node_im, $label_x1 + 1, $label_y1 + 1, $label_x2 - 1, $label_y2 - 1, $map->selected);  
}  
else  
{  
if ($this->labeloutlinecolour != array(-1,-1,-1))  
{  
$col=myimagecolorallocate($node_im,$this->labeloutlinecolour[0],  
$this->labeloutlinecolour[1], $this->labeloutlinecolour[2]);  
imagerectangle($node_im, $label_x1, $label_y1, $label_x2, $label_y2, $col);  
}  
}  
#}  
   
if ($this->labelfontshadowcolour != array(-1,-1,-1))  
{  
$col=myimagecolorallocate($im, $this->labelfontshadowcolour[0], $this->labelfontshadowcolour[1],  
$this->labelfontshadowcolour[2]);  
$map->myimagestring($node_im, $this->labelfont, $txt_x + 1, $txt_y + 1, $this->proclabel, $col);  
}  
   
$col=myimagecolorallocate($node_im, $this->labelfontcolour[0], $this->labelfontcolour[1],  
$this->labelfontcolour[2]);  
$map->myimagestring($node_im, $this->labelfont, $txt_x, $txt_y, $this->proclabel, $col);  
}  
   
# imagerectangle($node_im,$label_x1,$label_y1,$label_x2,$label_y2,$map->black);  
# imagerectangle($node_im,$icon_x1,$icon_y1,$icon_x2,$icon_y2,$map->black);  
   
$map->nodes[$this->name]->centre_x = $this->x - $bbox_x1;  
$map->nodes[$this->name]->centre_y = $this->y - $bbox_y1;  
   
if(1==0)  
{  
   
imageellipse($node_im, $this->centre_x, $this->centre_y, 8, 8, $map->selected);  
   
foreach (array("N","S","E","W","NE","NW","SE","SW") as $corner)  
{  
list($dx, $dy)=calc_offset($corner, $this->width, $this->height);  
imageellipse($node_im, $this->centre_x + $dx, $this->centre_y + $dy, 5, 5, $map->selected);  
}  
}  
   
# $this->image = $node_im;  
$map->nodes[$this->name]->image = $node_im;  
}  
   
function update_cache($cachedir,$mapname)  
{  
$cachename = $cachedir."/node_".md5($mapname."/".$this->name).".png";  
// save this image to a cache, for the editor  
imagepng($this->image,$cachename);  
}  
   
// draw the node, using the pre_render() output  
function NewDraw($im, &$map)  
{  
// take the offset we figured out earlier, and just blit  
// the image on. Who says "blit" anymore?  
   
// it's possible that there is no image, so better check.  
if(isset($this->image))  
{  
imagealphablending($im, true);  
imagecopy ( $im, $this->image, $this->x - $this->centre_x, $this->y - $this->centre_y, 0, 0, imagesx($this->image), imagesy($this->image) );  
}  
   
}  
   
// take the pre-rendered node and write it to a file so that  
// the editor can get at it.  
function WriteToCache()  
{  
}  
   
function DrawNINK($im, &$map, $size=32)  
{  
$quarter = $size/4;  
   
$x = $this->x;  
$y = $this->y;  
   
$col1 = imagecolorallocate($im,255,0,0);  
$col2 = imagecolorallocate($im,0,255,0);  
$outline = imagecolorallocate($im,255,255,255);  
$font = 2;  
   
imagefilledarc($im,$x,$y,$size,$size,270,90,$col1,IMG_ARC_PIE);  
imagefilledarc($im,$x,$y,$size,$size,90,270,$col2,IMG_ARC_PIE);  
   
imagefilledarc($im,$x,$y+$quarter,$quarter*2,$quarter*2,0,360,$col1,IMG_ARC_PIE);  
imagefilledarc($im,$x,$y-$quarter,$quarter*2,$quarter*2,0,360,$col2,IMG_ARC_PIE);  
   
// draw in the text shadows first, if needed  
if ($this->labelfontshadowcolour != array(-1,-1,-1))  
{  
$col=myimagecolorallocate($im, $this->labelfontshadowcolour[0], $this->labelfontshadowcolour[1],  
$this->labelfontshadowcolour[2]);  
   
imagestring($im, $font, 1 + $x - imagefontwidth($font)*2, 1 + $y-$quarter-imagefontheight($font)/2,"2.5M",$col);  
imagestring($im, $font, 1 + $x - imagefontwidth($font)*2, 1 + $y+$quarter-imagefontheight($font)/2,"786M",$col);  
}  
   
imagestring($im, $font, $x - imagefontwidth($font)*2, $y-$quarter-imagefontheight($font)/2,"2.5M",$outline);  
imagestring($im, $font, $x - imagefontwidth($font)*2, $y+$quarter-imagefontheight($font)/2,"786M",$outline);  
   
}  
   
function calc_size()  
{  
$this->width=0;  
$this->height=0;  
   
// calculate the size of the NODE box, so we can make links end at corners.  
if ($this->label != '')  
{  
$padding=0;  
$font=$this->labelfont;  
   
list($strwidth, $strheight)=$this->owner->myimagestringsize($font, $this->label);  
   
$boxwidth=$strwidth * 1.1;  
$boxheight=$strheight * 1.1;  
   
$this->width=$boxwidth;  
$this->height=$boxheight;  
}  
   
// if there's an icon, then that's what the corners relate to  
if ($this->iconfile != '')  
{  
# $temp_im=imagecreatefrompng($this->iconfile);  
$temp_im = imagecreatefromfile($this->iconfile);  
   
if ($temp_im)  
{  
$this->width=imagesx($temp_im);  
$this->height=imagesy($temp_im);  
}  
   
imagedestroy ($temp_im);  
}  
   
debug ("PRECALC $this->name: $this->width x $this->height\n");  
}  
   
function Reset(&$newowner)  
{  
$this->owner=$newowner;  
   
if (isset($this->owner->defaultnode) && $this->name != 'DEFAULT') {  
// use the defaults from DEFAULT  
$this->CopyFrom($this->owner->defaultnode);  
$this->my_default = $this->owner->defaultnode;  
}  
else  
{  
// use the default defaults  
foreach (array_keys($this->inherit_fieldlist)as $fld)  
{ $this->$fld = $this->inherit_fieldlist[$fld]; }  
}  
# warn($this->name.": ".var_dump($this->hints)."\n");  
# warn("DEF: ".var_dump($this->owner->defaultnode->hints)."\n");  
#if($this->name == 'North')  
#{  
# warn("In Reset, North says: ".$this->nodes['North']->hints['sigdigits']."\n");  
# }  
}  
   
function CopyFrom(&$source)  
{  
foreach (array_keys($this->inherit_fieldlist)as $fld) { $this->$fld=$source->$fld; }  
}  
   
function WriteConfig($fd)  
{  
$output='';  
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['label'] : $this->owner->defaultnode->label);  
   
if ($this->label != $comparison) { $output.="\tLABEL " . $this->label . "\n"; }  
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['infourl'] : $this->owner->defaultnode->infourl);  
   
if ($this->infourl != $comparison) { $output.="\tINFOURL " . $this->infourl . "\n"; }  
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['notestext'] : $this->owner->defaultnode->notestext);  
   
if ($this->notestext != $comparison) { $output.="\tNOTES " . $this->notestext . "\n"; }  
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['overliburl'] : $this->owner->defaultnode->overliburl);  
   
if ($this->overliburl != $comparison) { $output.="\tOVERLIBGRAPH " . $this->overliburl . "\n"; }  
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['iconfile'] : $this->owner->defaultnode->iconfile);  
if ($this->iconfile != $comparison) {  
$output.="\tICON ";  
if($this->iconscalew > 0) {  
$output .= $this->iconscalew." ".$this->iconscaleh." ";  
}  
$output .= $this->iconfile . "\n";  
}  
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['labelfont'] : $this->owner->defaultnode->labelfont);  
   
if ($this->labelfont != $comparison) { $output.="\tLABELFONT " . $this->labelfont . "\n"; }  
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['labeloffset'] : $this->owner->defaultnode->labeloffset);  
   
if ($this->labeloffset != $comparison) { $output.="\tLABELOFFSET " . $this->labeloffset . "\n"; }  
   
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['targets'] : $this->owner->defaultnode->targets);  
   
if ($this->targets != $comparison)  
{  
$output.="\tTARGET";  
   
foreach ($this->targets as $target) { $output.=" " . $target[4]; }  
   
$output.="\n";  
}  
   
$comparison = ($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['usescale'] : $this->owner->defaultnode->usescale);  
$comparison2 = ($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['scalevar'] : $this->owner->defaultnode->scalevar);  
   
if ( ($this->usescale != $comparison) || ($this->scalevar != $comparison2) )  
{ $output.="\tUSESCALE " . $this->usescale . " " . $this->scalevar . "\n"; }  
   
   
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['overlibcaption'] : $this->owner->defaultnode->overlibcaption);  
   
if ($this->overlibcaption != $comparison) { $output.="\tOVERLIBCAPTION " . $this->overlibcaption . "\n"; }  
   
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['overlibwidth'] : $this->owner->defaultnode->overlibwidth);  
   
if ($this->overlibwidth != $comparison) { $output.="\tOVERLIBWIDTH " . $this->overlibwidth . "\n"; }  
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['overlibheight'] : $this->owner->defaultnode->overlibheight);  
   
if ($this->overlibheight != $comparison) { $output.="\tOVERLIBHEIGHT " . $this->overlibheight . "\n"; }  
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['labelbgcolour'] : $this->owner->defaultnode->labelbgcolour);  
   
if ($this->labelbgcolour != $comparison) { $output.="\tLABELBGCOLOR " . render_colour(  
$this->labelbgcolour)  
. "\n"; }  
   
$comparison=($this->name == 'DEFAULT' ? $this->inherit_fieldlist['labelfontcolour']  
: $this->owner->defaultnode->labelfontcolour);  
   
if ($this->labelfontcolour != $comparison) { $output.="\tLABELFONTCOLOR " . render_colour(  
$this->labelfontcolour)  
. "\n"; }  
   
$comparison=($this->name == 'DEFAULT' ? $this->inherit_fieldlist['labeloutlinecolour']  
: $this->owner->defaultnode->labeloutlinecolour);  
   
if ($this->labeloutlinecolour != $comparison) { $output.="\tLABELOUTLINECOLOR " . render_colour(  
$this->labeloutlinecolour) . "\n";  
}  
   
$comparison=($this->name == 'DEFAULT' ? $this->inherit_fieldlist['labelfontshadowcolour']  
: $this->owner->defaultnode->labelfontshadowcolour);  
   
if ($this->labelfontshadowcolour != $comparison)  
{ $output.="\tLABELFONTSHADOWCOLOR " . render_colour($this->labelfontshadowcolour) . "\n"; }  
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['labeloffsetx'] : $this->owner->defaultnode->labeloffsetx);  
$comparison2=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['labeloffsety'] : $this->owner->defaultnode->labeloffsety);  
   
if (($this->labeloffsetx != $comparison) || ($this->labeloffsety != $comparison2))  
{ $output.="\tLABELOFFSET " . $this->labeloffsetx . " " . $this->labeloffsety . "\n"; }  
   
$comparison=($this->name == 'DEFAULT' ? $this->inherit_fieldlist['x'] : $this->owner->defaultnode->x);  
$comparison2=($this->name == 'DEFAULT' ? $this->inherit_fieldlist['y'] : $this->owner->defaultnode->y);  
   
if (($this->x != $comparison) || ($this->y != $comparison2))  
{  
if($this->relative_to == '')  
{ $output.="\tPOSITION " . $this->x . " " . $this->y . "\n"; }  
else  
{ $output.="\tPOSITION " . $this->relative_to . " " . $this->original_x . " " . $this->original_y . "\n"; }  
}  
   
if (($this->max_bandwidth_in != $this->owner->defaultnode->max_bandwidth_in)  
|| ($this->max_bandwidth_out != $this->owner->defaultnode->max_bandwidth_out)  
|| ($this->name == 'DEFAULT'))  
{  
if ($this->max_bandwidth_in == $this->max_bandwidth_out)  
{ $output.="\tMAXVALUE " . $this->max_bandwidth_in_cfg . "\n"; }  
else { $output  
.="\tMAXVALUE " . $this->max_bandwidth_in_cfg . " " . $this->max_bandwidth_out_cfg . "\n"; }  
}  
   
foreach ($this->hints as $hintname=>$hint)  
{  
// all hints for DEFAULT node are for writing  
// only changed ones, or unique ones, otherwise  
if(  
($this->name == 'DEFAULT')  
||  
(isset($this->owner->defaultnode->hints[$hintname])  
&&  
$this->owner->defaultnode->hints[$hintname] != $hint)  
||  
(!isset($this->owner->defaultnode->hints[$hintname]))  
)  
{  
$output .= "\tSET $hintname $hint\n";  
}  
}  
   
if ($output != '')  
{  
fwrite($fd, "NODE " . $this->name . "\n");  
fwrite($fd, "$output\n");  
}  
}  
   
function asJS()  
{  
$js='';  
$js.="Nodes[" . js_escape($this->name) . "] = {";  
$js.="x:" . $this->x . ", ";  
$js.="y:" . $this->y . ", ";  
$js.="label:" . js_escape($this->label) . ", ";  
$js.="name:" . js_escape($this->name) . ", ";  
$js.="infourl:" . js_escape($this->infourl) . ", ";  
$js.="overlibcaption:" . js_escape($this->overlibcaption) . ", ";  
$js.="overliburl:" . js_escape($this->overliburl) . ", ";  
$js.="overlibwidth:" . $this->overlibheight . ", ";  
$js.="overlibheight:" . $this->overlibwidth . ", ";  
$js.="iconfile:" . js_escape($this->iconfile);  
$js.="};\n";  
return $js;  
}  
   
function asJSON()  
{  
$js = '';  
$js .= "" . js_escape($this->name) . ": {";  
$js .= "x:" . ($this->x - $this->centre_x). ", ";  
$js .= "y:" . ($this->y - $this->centre_y) . ", ";  
$js .= "label:" . js_escape($this->label) . ", ";  
$js .= "name:" . js_escape($this->name) . ", ";  
$js .= "infourl:" . js_escape($this->infourl) . ", ";  
$js .= "overliburl:" . js_escape($this->overliburl) . ", ";  
$js.="overlibcaption:" . js_escape($this->overlibcaption) . ", ";  
   
$js .= "overlibwidth:" . $this->overlibheight . ", ";  
$js .= "overlibheight:" . $this->overlibwidth . ", ";  
$js .= "iconfile:" . js_escape($this->iconfile). ", ";  
$js .= "iconcachefile:" . js_escape($this->cachefile);  
$js .= "},\n";  
return $js;  
}  
   
// Set the bandwidth for this link. Convert from KMGT as necessary  
function SetBandwidth($inbw, $outbw)  
{  
   
$kilo = $this->owner->kilo;  
$this->max_bandwidth_in=unformat_number($inbw, $kilo);  
$this->max_bandwidth_out=unformat_number($outbw, $kilo);  
$this->max_bandwidth_in_cfg=$inbw;  
$this->max_bandwidth_out_cfg=$outbw;  
debug (sprintf("Setting bandwidth (%s -> %d bps, %s -> %d bps, KILO = %d)\n", $inbw, $this->max_bandwidth_in, $outbw, $this->max_bandwidth_out, $kilo));  
}  
   
function Draw($im, &$map)  
{  
$strwidth=0;  
$strheight=0;  
   
// we do this little bit first, so that the label-offset stuff can know it  
if ($this->label != '')  
{  
$padding=0;  
$font=$this->labelfont;  
   
list($strwidth, $strheight)=$map->myimagestringsize($font, $this->label);  
   
$boxwidth=$strwidth * 1.0;  
$boxheight=$strheight * 1.0;  
   
debug ("Node->Draw: Metrics are: $font $strwidth x $strheight -> $boxwidth x $boxheight\n");  
}  
   
if ($this->iconfile != '')  
{  
if (is_readable($this->iconfile))  
{  
imagealphablending($im, true);  
// draw the supplied icon, instead of the labelled box  
$temp_im=imagecreatefrompng($this->iconfile);  
   
if ($temp_im)  
{  
$w=imagesx($temp_im);  
$h=imagesy($temp_im);  
$x1=$this->x - $w / 2;  
$y1=$this->y - $h / 2;  
$x2=$this->x + $w / 2;  
$y2=$this->y + $h / 2;  
   
imagecopy($im, $temp_im, $x1, $y1, 0, 0, $w, $h);  
$map->imap->addArea("Rectangle", "NODE:" . $this->name, '', array($x1, $y1, $x2, $y2));  
imagedestroy ($temp_im);  
   
if ($this->labeloffset != '')  
{  
$this->labeloffsetx=0;  
$this->labeloffsety=0;  
   
list($dx, $dy)=calc_offset($this->labeloffset, ($w + $strwidth), ($h + $strheight));  
   
$this->labeloffsetx=$dx;  
$this->labeloffsety=$dy;  
}  
}  
else { warn ("Couldn't open PNG ICON: " . $this->iconfile . " - is it a PNG?\n"); }  
}  
else { warn ("ICON " . $this->iconfile  
. " does not exist, or is not readble. Check path and permissions.\n"); }  
}  
   
if ($this->label != '')  
{  
$x=$this->x + $this->labeloffsetx;  
$y=$this->y + $this->labeloffsety;  
   
$x1 = $x - ($boxwidth / 2) - 2;  
$x2 = $x + ($boxwidth / 2) + 2;  
$y1 = $y - ($boxheight / 2) - 2;  
$y2 = $y + ($boxheight / 2) + 2;  
   
$txt_x = $x - $strwidth / 2;  
$txt_y = $y + $strheight / 2;  
   
if ($this->iconfile == '')  
{  
if ($this->labelbgcolour != array  
(  
-1,  
-1,  
-1  
))  
{  
$col=myimagecolorallocate($im, $this->labelbgcolour[0], $this->labelbgcolour[1],  
$this->labelbgcolour[2]);  
imagefilledrectangle($im, $x1, $y1, $x2, $y2, $col);  
}  
   
if ($this->selected)  
{  
imagerectangle($im, $x1, $y1, $x2, $y2, $map->selected);  
// would be nice if it was thicker, too...  
imagerectangle($im, $x1 - 1, $y1 - 1, $x2 + 1, $y2 + 1, $map->selected);  
}  
else  
{  
if ($this->labeloutlinecolour != array  
(  
-1,  
-1,  
-1  
))  
{  
$col=myimagecolorallocate($im, $this->labeloutlinecolour[0],  
$this->labeloutlinecolour[1], $this->labeloutlinecolour[2]);  
imagerectangle($im, $x1, $y1, $x2, $y2, $col);  
}  
}  
}  
   
if ($this->labelfontshadowcolour != array  
(  
-1,  
-1,  
-1  
))  
{  
$col=myimagecolorallocate($im, $this->labelfontshadowcolour[0], $this->labelfontshadowcolour[1],  
$this->labelfontshadowcolour[2]);  
$map->myimagestring($im, $font, $txt_x + 1, $txt_y + 1, $this->label, $col);  
}  
   
$col=myimagecolorallocate($im, $this->labelfontcolour[0], $this->labelfontcolour[1],  
$this->labelfontcolour[2]);  
$map->myimagestring($im, $font, $txt_x, $txt_y, $this->label, $col);  
   
$map->imap->addArea("Rectangle", "NODE:" . $this->name, '', array($x1, $y1, $x2, $y2));  
}  
}  
}  
   
;  
   
class WeatherMapLink extends WeatherMapItem  
{  
var $owner, $name;  
var $maphtml;  
var $a, $b; // the ends - references to nodes  
var $width, $arrowstyle;  
var $bwfont, $labelstyle, $labelboxstyle;  
var $overliburl, $infourl;  
var $notes;  
var $overlibcaption;  
var $overlibwidth, $overlibheight;  
var $bandwidth_in, $bandwidth_out;  
var $max_bandwidth_in, $max_bandwidth_out;  
var $max_bandwidth_in_cfg, $max_bandwidth_out_cfg;  
var $targets = array();  
var $a_offset, $b_offset;  
var $in_ds, $out_ds;  
var $selected;  
var $inpercent, $outpercent;  
var $inherit_fieldlist;  
var $vialist = array();  
var $usescale;  
var $outlinecolour;  
var $bwoutlinecolour;  
var $bwboxcolour;  
var $commentfont,$notestext;  
var $inscalekey,$outscalekey;  
# var $incolour,$outcolour;  
var $commentfontcolour;  
var $bwfontcolour;  
# var $incomment, $outcomment;  
var $comments = array();  
var $curvepoints;  
var $labeloffset_in, $labeloffset_out;  
var $commentoffset_in, $commentoffset_out;  
   
function WeatherMapLink() { $this->inherit_fieldlist=array  
(  
'my_default' => NULL,  
'width' => 7,  
'commentfont' => 1,  
'bwfont' => 2,  
'labeloffset_out' => 25,  
'labeloffset_in' => 75,  
'commentoffset_out' => 5,  
'commentoffset_in' => 95,  
'arrowstyle' => 'classic',  
'usescale' => 'DEFAULT',  
'targets' => array(),  
'infourl' => '',  
'notestext' => '',  
'notes' => array(),  
'hints' => array(),  
'comments' => array('',''),  
'overliburl' => '',  
'labelstyle' => 'percent',  
'labelboxstyle' => 'classic',  
'overlibwidth' => 0,  
'overlibheight' => 0,  
'outlinecolour' => array(0, 0, 0),  
'bwoutlinecolour' => array(0, 0, 0),  
'bwfontcolour' => array(0, 0, 0),  
'bwboxcolour' => array(255, 255, 255),  
'commentfontcolour' => array(192,192,192),  
'inpercent'=>0, 'outpercent'=>0,  
'inscalekey'=>'', 'outscalekey'=>'',  
# 'incolour'=>-1,'outcolour'=>-1,  
'a_offset' => 'C',  
'b_offset' => 'C',  
#'incomment' => '',  
#'outcomment' => '',  
'overlibcaption' => '',  
'max_bandwidth_in' => 100000000,  
'max_bandwidth_out' => 100000000,  
'max_bandwidth_in_cfg' => '100M',  
'max_bandwidth_out_cfg' => '100M'  
);  
// $this->a_offset = 'C';  
// $this->b_offset = 'C';  
// $this->targets = array();  
}  
   
function Reset(&$newowner)  
{  
$this->owner=$newowner;  
   
if (isset($this->owner->defaultlink) && $this->name != 'DEFAULT') {  
// use the defaults from DEFAULT  
$this->CopyFrom($this->owner->defaultlink);  
$this->my_default = $this->owner->defaultlink;  
}  
else  
{  
foreach (array_keys($this->inherit_fieldlist)as  
$fld) { $this->$fld=$this->inherit_fieldlist[$fld]; }  
}  
}  
   
function my_type() { return "LINK"; }  
   
function CopyFrom($source)  
{  
foreach (array_keys($this->inherit_fieldlist)as $fld) { $this->$fld = $source->$fld; }  
}  
   
// Set the bandwidth for this link. Convert from KMGT as necessary  
function SetBandwidth($inbw, $outbw)  
{  
$kilo = $this->owner->kilo;  
$this->max_bandwidth_in=unformat_number($inbw, $kilo);  
$this->max_bandwidth_out=unformat_number($outbw, $kilo);  
$this->max_bandwidth_in_cfg=$inbw;  
$this->max_bandwidth_out_cfg=$outbw;  
debug (sprintf("Setting bandwidth (%s -> %d bps, %s -> %d bps, KILO = %d)\n", $inbw, $this->max_bandwidth_in, $outbw,  
$this->max_bandwidth_out, $kilo));  
}  
   
function DrawComments($image,$col,$width)  
{  
$curvepoints =& $this->curvepoints;  
$last = count($curvepoints)-1;  
$totaldistance = $curvepoints[$last][2];  
   
$start[OUT] = 0;  
$commentpos[OUT] = $this->commentoffset_out;  
$commentpos[IN] = $this->commentoffset_in;  
$start[IN] = $last;  
   
foreach (array(OUT,IN) as $dir)  
{  
// Time to deal with Link Comments, if any  
$comment = $this->owner->ProcessString($this->comments[$dir], $this);  
   
if($comment != '')  
{  
// XXX - redundant extra variable  
$startindex = $start[$dir];  
$extra_percent = $commentpos[$dir];  
   
$font = $this->commentfont;  
// nudge pushes the comment out along the link arrow a little bit  
// (otherwise there are more problems with text disappearing underneath links)  
# $nudgealong = 0; $nudgeout=0;  
$nudgealong = intval($this->get_hint("comment_nudgealong"));  
$nudgeout = intval($this->get_hint("comment_nudgeout"));  
   
$extra = ($totaldistance * ($extra_percent/100));  
# $comment_index = find_distance($curvepoints,$extra);  
   
list($x,$y,$comment_index,$angle) = find_distance_coords_angle($curvepoints,$extra);  
$dx = $x - $curvepoints[$comment_index][0];  
$dy = $y - $curvepoints[$comment_index][1];  
   
#$ratio = ($extra - $curvepoints[$comment_index][2]) / ($curvepoints[$comment_index + 1][2] - $curvepoints[$comment_index][2]);  
#$dx = -$curvepoints[$startindex][0] + ($curvepoints[$comment_index][0] + $ratio * ($curvepoints[$comment_index + 1][0] - $curvepoints[$comment_index][0]));  
#$dy = -$curvepoints[$startindex][1] + ($curvepoints[$comment_index][1] + $ratio * ($curvepoints[$comment_index + 1][1] - $curvepoints[$comment_index][1]));  
// we need the angle to draw the text  
#$angle = rad2deg(atan2(-$dy,$dx));  
// find the normal to our link, so we can get outside the arrow  
   
$l=sqrt(($dx * $dx) + ($dy * $dy));  
$dx = $dx/$l; $dy = $dy/$l;  
$nx = $dy; $ny = -$dx;  
   
# warn($commentpos[$dir]." $extra/$totaldistance ".$comment_index."\n");  
# warn("$dx/$dy $nx/$ny\n");  
#$edge_x = $x + $nudge*$dx + $nx * ($width + 4);  
#$edge_y = $y + $nudge*$dy + $ny * ($width + 4);  
$flipped = FALSE;  
// if the text will be upside-down, rotate it, flip it, and right-justify it  
// not quite as catchy as Missy's version  
if(abs($angle)>90)  
{  
# $col = $map->selected;  
$angle -= 180;  
if($angle < -180) $angle +=360;  
$edge_x = $x + $nudgealong*$dx - $nx * ($width + 4 + $nudgeout);  
$edge_y = $y + $nudgealong*$dy - $ny * ($width + 4 + $nudgeout);  
# $comment .= "@";  
$flipped = TRUE;  
}  
else  
{  
$edge_x = $x + $nudgealong*$dx + $nx * ($width + 4 + $nudgeout);  
$edge_y = $y + $nudgealong*$dy + $ny * ($width + 4 + $nudgeout);  
}  
   
list($textlength, $textheight) = $this->owner->myimagestringsize($font, $comment);  
   
if( !$flipped && ($extra + $textlength) > $totaldistance)  
{  
$edge_x -= $dx * $textlength;  
$edge_y -= $dy * $textlength;  
# $comment .= "#";  
}  
   
if( $flipped && ($extra - $textlength) < 0)  
{  
$edge_x += $dx * $textlength;  
$edge_y += $dy * $textlength;  
# $comment .= "%";  
}  
   
// FINALLY, draw the text!  
# imagefttext($image, $fontsize, $angle, $edge_x, $edge_y, $col, $font,$comment);  
$this->owner->myimagestring($image, $font, $edge_x, $edge_y, $comment, $col, $angle);  
#imagearc($image,$x,$y,10,10,0, 360,$this->owner->selected);  
#imagearc($image,$edge_x,$edge_y,10,10,0, 360,$this->owner->selected);  
}  
}  
}  
   
function Draw($im, &$map)  
{  
// Get the positions of the end-points  
$x1=$map->nodes[$this->a->name]->x;  
$y1=$map->nodes[$this->a->name]->y;  
   
$x2=$map->nodes[$this->b->name]->x;  
$y2=$map->nodes[$this->b->name]->y;  
   
// Adjust them if there's an offset requested  
#$a_height=$map->nodes[$this->a->name]->height;  
#$a_width=$map->nodes[$this->a->name]->width;  
   
# $b_height=$map->nodes[$this->b->name]->height;  
# $b_width=$map->nodes[$this->b->name]->width;  
   
list($dx, $dy)=calc_offset($this->a_offset, $map->nodes[$this->a->name]->width, $map->nodes[$this->a->name]->height);  
$x1+=$dx;  
$y1+=$dy;  
   
list($dx, $dy)=calc_offset($this->b_offset, $map->nodes[$this->b->name]->width, $map->nodes[$this->b->name]->height);  
$x2+=$dx;  
$y2+=$dy;  
   
$outline_colour=NULL;  
$comment_colour=NULL;  
   
if ($this->outlinecolour != array(-1,-1,-1))  
{  
$outline_colour=myimagecolorallocate(  
$im, $this->outlinecolour[0], $this->outlinecolour[1],  
$this->outlinecolour[2]);  
}  
   
if ($this->commentfontcolour != array(-1,-1,-1))  
{  
$comment_colour=myimagecolorallocate(  
$im, $this->commentfontcolour[0], $this->commentfontcolour[1],  
$this->commentfontcolour[2]);  
}  
   
$xpoints = array ( );  
$ypoints = array ( );  
   
$xpoints[]=$x1;  
$ypoints[]=$y1;  
   
# warn("There are VIAs.\n");  
foreach ($this->vialist as $via)  
{  
# imagearc($im, $via[0],$via[1],20,20,0,360,$map->selected);  
$xpoints[]=$via[0];  
$ypoints[]=$via[1];  
}  
   
$xpoints[]=$x2;  
$ypoints[]=$y2;  
   
list($link_in_colour,$link_in_scalekey) = $map->ColourFromPercent($this->inpercent,$this->usescale,$this->name);  
list($link_out_colour,$link_out_scalekey) = $map->ColourFromPercent($this->outpercent,$this->usescale,$this->name);  
   
// $map->links[$this->name]->inscalekey = $link_in_scalekey;  
// $map->links[$this->name]->outscalekey = $link_out_scalekey;  
   
$link_width=$this->width;  
// these will replace the one above, ultimately.  
$link_in_width=$this->width;  
$link_out_width=$this->width;  
   
// for bulging animations  
if ( ($map->widthmod) || ($map->get_hint('link_bulge') == 1))  
{  
// a few 0.1s and +1s to fix div-by-zero, and invisible links  
$link_width = (($link_width * $this->inpercent * 1.5 + 0.1) / 100) + 1;  
// these too  
$link_in_width = (($link_in_width * $this->inpercent * 1.5 + 0.1) / 100) + 1;  
$link_out_width = (($link_out_width * $this->outpercent * 1.5 + 0.1) / 100) + 1;  
}  
   
   
// Calculate the spine points - the actual curve  
$this->curvepoints = calc_curve($xpoints, $ypoints);  
   
draw_curve($im, $this->curvepoints,  
$link_width, $outline_colour, $comment_colour, array($link_in_colour, $link_out_colour),  
$this->name, $map);  
   
$this->DrawComments($im,$comment_colour,$link_width*1.1);  
   
$curvelength = $this->curvepoints[count($this->curvepoints)-1][2];  
// figure out where the labels should be, and what the angle of the curve is at that point  
list($q1_x,$q1_y,$junk,$q1_angle) = find_distance_coords_angle($this->curvepoints,($this->labeloffset_out/100)*$curvelength);  
list($q3_x,$q3_y,$junk,$q3_angle) = find_distance_coords_angle($this->curvepoints,($this->labeloffset_in/100)*$curvelength);  
   
# imageline($im, $q1_x+20*cos(deg2rad($q1_angle)),$q1_y-20*sin(deg2rad($q1_angle)), $q1_x-20*cos(deg2rad($q1_angle)), $q1_y+20*sin(deg2rad($q1_angle)), $this->owner->selected );  
# imageline($im, $q3_x+20*cos(deg2rad($q3_angle)),$q3_y-20*sin(deg2rad($q3_angle)), $q3_x-20*cos(deg2rad($q3_angle)), $q3_y+20*sin(deg2rad($q3_angle)), $this->owner->selected );  
   
# warn("$q1_angle $q3_angle\n");  
   
if (!is_null($q1_x))  
{  
$outbound=array  
(  
$q1_x,  
$q1_y,  
0,  
0,  
$this->outpercent,  
$this->bandwidth_out,  
$q1_angle  
);  
   
$inbound=array  
(  
$q3_x,  
$q3_y,  
0,  
0,  
$this->inpercent,  
$this->bandwidth_in,  
$q3_angle  
);  
   
if ($map->sizedebug)  
{  
$outbound[5]=$this->max_bandwidth_out;  
$inbound[5]=$this->max_bandwidth_in;  
}  
   
foreach (array($inbound, $outbound)as $task)  
{  
$thelabel="";  
   
if ($this->labelstyle != 'none')  
{  
debug("Bandwidth is ".$task[5]."\n");  
if ($this->labelstyle == 'bits') { $thelabel=nice_bandwidth($task[5], $this->owner->kilo); }  
elseif ($this->labelstyle == 'unformatted') { $thelabel=$task[5]; }  
elseif ($this->labelstyle == 'percent') { $thelabel=format_number($task[4]) . "%"; }  
   
$padding = intval($this->get_hint('bwlabel_padding'));  
   
if($this->labelboxstyle == 'angled')  
{  
$map->DrawLabelRotated($im, $task[0], $task[1],$task[6], $thelabel, $this->bwfont, $padding,  
$this->name, $this->bwfontcolour, $this->bwboxcolour, $this->bwoutlinecolour,$map);  
}  
else  
{  
$map->DrawLabel($im, $task[0], $task[1], $thelabel, $this->bwfont, $padding,  
$this->name, $this->bwfontcolour, $this->bwboxcolour, $this->bwoutlinecolour,$map);  
}  
   
}  
}  
}  
}  
   
function WriteConfig($fd)  
{  
$output='';  
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['infourl'] : $this->owner->defaultlink->infourl);  
   
if ($this->infourl != $comparison) { $output.="\tINFOURL " . $this->infourl . "\n"; }  
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['notestext'] : $this->owner->defaultlink->notestext);  
   
if ($this->notestext != $comparison) { $output.="\tNOTES " . $this->notestext . "\n"; }  
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['overliburl'] : $this->owner->defaultlink->overliburl);  
   
if ($this->overliburl != $comparison) { $output.="\tOVERLIBGRAPH " . $this->overliburl . "\n"; }  
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['overlibcaption'] : $this->owner->defaultlink->overlibcaption);  
   
if ($this->overlibcaption != $comparison) { $output.="\tOVERLIBCAPTION " . $this->overlibcaption . "\n"; }  
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['overlibwidth'] : $this->owner->defaultlink->overlibwidth);  
   
if ($this->overlibwidth != $comparison) { $output.="\tOVERLIBWIDTH " . $this->overlibwidth . "\n"; }  
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['overlibheight'] : $this->owner->defaultlink->overlibheight);  
   
if ($this->overlibheight != $comparison) { $output.="\tOVERLIBHEIGHT " . $this->overlibheight . "\n"; }  
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['arrowstyle'] : $this->owner->defaultlink->arrowstyle);  
   
if ($this->arrowstyle != $comparison) { $output.="\tARROWSTYLE " . $this->arrowstyle . "\n"; }  
   
$comparison=($this->name == 'DEFAULT'  
? ($this->inherit_fieldlist['labelstyle']) : ($this->owner->defaultlink->labelstyle));  
if ($this->labelstyle != $comparison) { $output.="\tBWLABEL " . $this->labelstyle . "\n"; }  
   
$comparison=($this->name == 'DEFAULT'  
? ($this->inherit_fieldlist['labelboxstyle']) : ($this->owner->defaultlink->labelboxstyle));  
if ($this->labelboxstyle != $comparison) { $output.="\tBWSTYLE " . $this->labelboxstyle . "\n"; }  
   
$comparison = ($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['labeloffset_in'] : $this->owner->defaultlink->labeloffset_in);  
$comparison2 = ($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['labeloffset_out'] : $this->owner->defaultlink->labeloffset_out);  
   
if ( ($this->labeloffset_in != $comparison) || ($this->labeloffset_out != $comparison2) )  
{ $output.="\tBWLABELPOS " . $this->labeloffset_in . " " . $this->labeloffset_out . "\n"; }  
   
$comparison=($this->name == 'DEFAULT'  
? ($this->inherit_fieldlist['commentoffset_in'].":".$this->inherit_fieldlist['commentoffset_out']) : ($this->owner->defaultlink->commentoffset_in.":".$this->owner->defaultlink->commentoffset_out));  
$mine = $this->commentoffset_in.":".$this->commentoffset_out;  
if ($mine != $comparison) { $output.="\tCOMMENTPOS " . $this->commentoffset_in." ".$this->commentoffset_out. "\n"; }  
   
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['targets'] : $this->owner->defaultlink->targets);  
   
if ($this->targets != $comparison)  
{  
$output.="\tTARGET";  
   
foreach ($this->targets as $target) { $output.=" " . $target[4]; }  
   
$output.="\n";  
}  
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['usescale'] : $this->owner->defaultlink->usescale);  
if ($this->usescale != $comparison) { $output.="\tUSESCALE " . $this->usescale . "\n"; }  
   
   
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['comments'][IN] : $this->owner->defaultlink->comments[IN]);  
if ($this->comments[IN] != $comparison) { $output.="\tINCOMMENT " . $this->comments[IN] . "\n"; }  
   
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['comments'][OUT] : $this->owner->defaultlink->comments[OUT]);  
if ($this->comments[OUT] != $comparison) { $output.="\tOUTCOMMENT " . $this->comments[OUT] . "\n"; }  
   
   
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['usescale'] : $this->owner->defaultlink->usescale);  
if ($this->usescale != $comparison) { $output.="\tUSESCALE " . $this->usescale . "\n"; }  
   
   
   
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['bwfont'] : $this->owner->defaultlink->bwfont);  
   
if ($this->bwfont != $comparison) { $output.="\tBWFONT " . $this->bwfont . "\n"; }  
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['commentfont'] : $this->owner->defaultlink->commentfont);  
   
if ($this->commentfont != $comparison) { $output.="\tCOMMENTFONT " . $this->commentfont . "\n"; }  
   
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['width'] : $this->owner->defaultlink->width);  
   
if ($this->width != $comparison) { $output.="\tWIDTH " . $this->width . "\n"; }  
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['outlinecolour'] : $this->owner->defaultlink->outlinecolour);  
   
if ($this->outlinecolour != $comparison) { $output.="\tOUTLINECOLOR " . render_colour(  
$this->outlinecolour)  
. "\n"; }  
   
$comparison=($this->name == 'DEFAULT' ? $this->inherit_fieldlist['bwoutlinecolour']  
: $this->owner->defaultlink->bwoutlinecolour);  
   
if ($this->bwoutlinecolour != $comparison) { $output.="\tBWOUTLINECOLOR " . render_colour(  
$this->bwoutlinecolour)  
. "\n"; }  
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['bwfontcolour'] : $this->owner->defaultlink->bwfontcolour);  
   
if ($this->bwfontcolour != $comparison) { $output.="\tBWFONTCOLOR " . render_colour(  
$this->bwfontcolour) . "\n"; }  
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['commentfontcolour'] : $this->owner->defaultlink->commentfontcolour);  
   
if ($this->commentfontcolour != $comparison) { $output.="\tCOMMENTFONTCOLOR " . render_colour(  
$this->commentfontcolour) . "\n"; }  
   
   
$comparison=($this->name == 'DEFAULT'  
? $this->inherit_fieldlist['bwboxcolour'] : $this->owner->defaultlink->bwboxcolour);  
   
if ($this->bwboxcolour != $comparison) { $output.="\tBWBOXCOLOR " . render_colour(  
$this->bwboxcolour) . "\n"; }  
   
if (isset($this->a) && isset($this->b))  
{  
$output.="\tNODES " . $this->a->name;  
   
if ($this->a_offset != 'C')  
$output.=":" . $this->a_offset;  
   
$output.=" " . $this->b->name;  
   
if ($this->b_offset != 'C')  
$output.=":" . $this->b_offset;  
   
$output.="\n";  
}  
   
if (count($this->vialist) > 0)  
{  
foreach ($this->vialist as $via)  
$output.=sprintf("\tVIA %d %d\n", $via[0], $via[1]);  
}  
   
if (($this->max_bandwidth_in != $this->owner->defaultlink->max_bandwidth_in)  
|| ($this->max_bandwidth_out != $this->owner->defaultlink->max_bandwidth_out)  
|| ($this->name == 'DEFAULT'))  
{  
if ($this->max_bandwidth_in == $this->max_bandwidth_out)  
{ $output.="\tBANDWIDTH " . $this->max_bandwidth_in_cfg . "\n"; }  
else { $output  
.="\tBANDWIDTH " . $this->max_bandwidth_in_cfg . " " . $this->max_bandwidth_out_cfg . "\n"; }  
}  
   
   
foreach ($this->hints as $hintname=>$hint)  
{  
// all hints for DEFAULT node are for writing  
// only changed ones, or unique ones, otherwise  
if(  
($this->name == 'DEFAULT')  
||  
(isset($this->owner->defaultlink->hints[$hintname])  
&&  
$this->owner->defaultlink->hints[$hintname] != $hint)  
||  
(!isset($this->owner->defaultlink->hints[$hintname]))  
)  
{  
$output .= "\tSET $hintname $hint\n";  
}  
}  
   
if ($output != '')  
{  
fwrite($fd, "LINK " . $this->name . "\n");  
fwrite($fd, "$output\n");  
}  
}  
   
function asJS()  
{  
$js='';  
$js.="Links[" . js_escape($this->name) . "] = {";  
   
if ($this->name != 'DEFAULT')  
{  
$js.="a:'" . $this->a->name . "', ";  
$js.="b:'" . $this->b->name . "', ";  
}  
   
$js.="width:'" . $this->width . "', ";  
$js.="target:";  
   
$tgt='';  
   
foreach ($this->targets as $target) { $tgt.=$target[4] . ' '; }  
   
$js.=js_escape(trim($tgt));  
$js.=",";  
   
$js.="bw_in:" . js_escape($this->max_bandwidth_in_cfg) . ", ";  
$js.="bw_out:" . js_escape($this->max_bandwidth_out_cfg) . ", ";  
   
$js.="name:" . js_escape($this->name) . ", ";  
$js.="overlibwidth:'" . $this->overlibheight . "', ";  
$js.="overlibheight:'" . $this->overlibwidth . "', ";  
$js.="overlibcaption:" . js_escape($this->overlibcaption) . ", ";  
   
$js.="infourl:" . js_escape($this->infourl) . ", ";  
$js.="overliburl:" . js_escape($this->overliburl);  
$js.="};\n";  
return $js;  
}  
   
function asJSON()  
{  
$js='';  
$js.="" . js_escape($this->name) . ": {";  
   
if ($this->name != 'DEFAULT')  
{  
$js.="a:'" . $this->a->name . "', ";  
$js.="b:'" . $this->b->name . "', ";  
}  
   
$js.="width:'" . $this->width . "', ";  
$js.="target:";  
   
$tgt='';  
   
foreach ($this->targets as $target) { $tgt.=$target[4] . ' '; }  
   
$js.=js_escape(trim($tgt));  
$js.=",";  
   
$js.="bw_in:" . js_escape($this->max_bandwidth_in_cfg) . ", ";  
$js.="bw_out:" . js_escape($this->max_bandwidth_out_cfg) . ", ";  
   
$js.="name:" . js_escape($this->name) . ", ";  
$js.="overlibwidth:'" . $this->overlibheight . "', ";  
$js.="overlibheight:'" . $this->overlibwidth . "', ";  
$js.="overlibcaption:" . js_escape($this->overlibcaption) . ", ";  
   
$js.="infourl:" . js_escape($this->infourl) . ", ";  
$js.="overliburl:" . js_escape($this->overliburl);  
$js.="},\n";  
return $js;  
}  
}  
   
;  
   
class WeatherMap extends WeatherMapBase class WeatherMap extends WeatherMapBase
{ {
var $nodes = array(); // an array of WeatherMapNodes var $nodes = array(); // an array of WeatherMapNodes
var $links = array(); // an array of WeatherMapLinks var $links = array(); // an array of WeatherMapLinks
var $texts = array(); // an array containing all the extraneous text bits var $texts = array(); // an array containing all the extraneous text bits
var $used_images = array(); // an array of image filenames referred to (used by editor) var $used_images = array(); // an array of image filenames referred to (used by editor)
  var $seen_zlayers = array(0=>array(),1000=>array()); // 0 is the background, 1000 is the legends, title, etc
   
  var $config;
  var $next_id;
  var $min_ds_time;
  var $max_ds_time;
var $background; var $background;
var $htmlstyle; var $htmlstyle;
var $imap; var $imap;
var $colours; var $colours;
var $configfile; var $configfile;
Line 2439... Line 203...
var $timex, var $timex,
$timey; $timey;
var $width, var $width,
$height; $height;
var $keyx, var $keyx,
$keyy; $keyy, $keyimage;
var $titlex, var $titlex,
$titley; $titley;
var $keytext, var $keytext,
$stamptext, $datestamp; $stamptext, $datestamp;
  var $min_data_time, $max_data_time;
var $htmloutputfile, var $htmloutputfile,
$imageoutputfile; $imageoutputfile;
  var $htmlstylesheet;
var $defaultlink, var $defaultlink,
$defaultnode; $defaultnode;
var $need_size_precalc; var $need_size_precalc;
var $keystyle,$keysize; var $keystyle,$keysize;
var $rrdtool_check; var $rrdtool_check;
var $inherit_fieldlist; var $inherit_fieldlist;
  var $mintimex, $maxtimex;
  var $mintimey, $maxtimey;
  var $minstamptext, $maxstamptext;
var $context; var $context;
var $cachefolder,$mapcache; var $cachefolder,$mapcache,$cachefile_version;
var $name; var $name;
var $black, var $black,
$white, $white,
$grey, $grey,
$selected; $selected;
   
var $datasourceclasses; var $datasourceclasses;
var $preprocessclasses; var $preprocessclasses;
var $postprocessclasses; var $postprocessclasses;
var $activedatasourceclasses; var $activedatasourceclasses;
  var $thumb_width, $thumb_height;
  var $has_includes;
  var $has_overlibs;
  var $node_template_tree;
  var $link_template_tree;
   
  var $plugins = array();
  var $included_files = array();
  var $usage_stats = array();
   
function WeatherMap() function WeatherMap()
{ {
$this->inherit_fieldlist=array $this->inherit_fieldlist=array
( (
Line 2476... Line 254...
'kilo' => 1000, 'kilo' => 1000,
'numscales' => array('DEFAULT' => 0), 'numscales' => array('DEFAULT' => 0),
'datasourceclasses' => array(), 'datasourceclasses' => array(),
'preprocessclasses' => array(), 'preprocessclasses' => array(),
'postprocessclasses' => array(), 'postprocessclasses' => array(),
  'included_files' => array(),
'context' => '', 'context' => '',
'dumpconfig' => FALSE, 'dumpconfig' => FALSE,
'rrdtool_check' => '', 'rrdtool_check' => '',
'background' => '', 'background' => '',
'imageoutputfile' => '', 'imageoutputfile' => '',
  'imageuri' => '',
'htmloutputfile' => '', 'htmloutputfile' => '',
  'htmlstylesheet' => '',
'labelstyle' => 'percent', // redundant? 'labelstyle' => 'percent', // redundant?
'htmlstyle' => 'static', 'htmlstyle' => 'static',
'keystyle' => array('DEFAULT' => 'classic'), 'keystyle' => array('DEFAULT' => 'classic'),
'title' => 'Network Weathermap', 'title' => 'Network Weathermap',
'keytext' => array('DEFAULT' => 'Traffic Load'), 'keytext' => array('DEFAULT' => 'Traffic Load'),
'keyx' => array('DEFAULT' => -1), 'keyx' => array('DEFAULT' => -1),
'keyy' => array('DEFAULT' => -1), 'keyy' => array('DEFAULT' => -1),
  'keyimage' => array(),
'keysize' => array('DEFAULT' => 400), 'keysize' => array('DEFAULT' => 400),
'stamptext' => 'Created: %b %d %Y %H:%M:%S', 'stamptext' => 'Created: %b %d %Y %H:%M:%S',
'keyfont' => 4, 'keyfont' => 4,
'titlefont' => 2, 'titlefont' => 2,
'timefont' => 2, 'timefont' => 2,
'timex' => 0, 'timex' => 0,
'timey' => 0, 'timey' => 0,
   
  'mintimex' => -10000,
  'mintimey' => -10000,
  'maxtimex' => -10000,
  'maxtimey' => -10000,
  'minstamptext' => 'Oldest Data: %b %d %Y %H:%M:%S',
  'maxstamptext' => 'Newest Data: %b %d %Y %H:%M:%S',
   
  'thumb_width' => 0,
  'thumb_height' => 0,
'titlex' => -1, 'titlex' => -1,
'titley' => -1, 'titley' => -1,
'cachefolder' => 'cached', 'cachefolder' => 'cached',
'mapcache' => '', 'mapcache' => '',
'sizedebug' => FALSE, 'sizedebug' => FALSE,
'debugging' => FALSE, 'debugging' => FALSE,
'widthmod' => FALSE, 'widthmod' => FALSE,
'name' => 'MAP' 'has_includes' => FALSE,
  'has_overlibs' => FALSE,
  'name' => 'MAP'
); );
   
$this->Reset(); $this->Reset();
} }
   
  function my_type() { return "MAP"; }
   
function Reset() function Reset()
{ {
  $this->next_id = 100;
foreach (array_keys($this->inherit_fieldlist)as $fld) { $this->$fld=$this->inherit_fieldlist[$fld]; } foreach (array_keys($this->inherit_fieldlist)as $fld) { $this->$fld=$this->inherit_fieldlist[$fld]; }
   
  $this->min_ds_time = NULL;
  $this->max_ds_time = NULL;
   
// these two are used for default settings $this->need_size_precalc=FALSE;
$this->defaultlink=new WeatherMapLink;  
$this->defaultlink->name="DEFAULT";  
$this->defaultlink->Reset($this);  
   
$this->defaultnode=new WeatherMapNode; $this->nodes=array(); // an array of WeatherMapNodes
$this->defaultnode->name="DEFAULT"; $this->links=array(); // an array of WeatherMapLinks
$this->defaultnode->Reset($this);  
  // these are the default defaults
  // by putting them into a normal object, we can use the
  // same code for writing out LINK DEFAULT as any other link.
  debug("Creating ':: DEFAULT ::' DEFAULT LINK\n");
  // these two are used for default settings
  $deflink = new WeatherMapLink;
  $deflink->name=":: DEFAULT ::";
  $deflink->template=":: DEFAULT ::";
  $deflink->Reset($this);
   
  $this->links[':: DEFAULT ::'] = &$deflink;
   
$this->need_size_precalc=FALSE; debug("Creating ':: DEFAULT ::' DEFAULT NODE\n");
  $defnode = new WeatherMapNode;
  $defnode->name=":: DEFAULT ::";
  $defnode->template=":: DEFAULT ::";
  $defnode->Reset($this);
   
  $this->nodes[':: DEFAULT ::'] = &$defnode;
   
  $this->node_template_tree = array();
  $this->link_template_tree = array();
   
  $this->node_template_tree['DEFAULT'] = array();
  $this->link_template_tree['DEFAULT'] = array();
   
$this->nodes=array  
(  
); // an array of WeatherMapNodes  
   
$this->links=array // ************************************
( // now create the DEFAULT link and node, based on those.
); // an array of WeatherMapLinks // these can be modified by the user, but their template (and therefore comparison in WriteConfig) is ':: DEFAULT ::'
  debug("Creating actual DEFAULT NODE from :: DEFAULT ::\n");
  $defnode2 = new WeatherMapNode;
  $defnode2->name = "DEFAULT";
  $defnode2->template = ":: DEFAULT ::";
  $defnode2->Reset($this);
   
  $this->nodes['DEFAULT'] = &$defnode2;
   
  debug("Creating actual DEFAULT LINK from :: DEFAULT ::\n");
  $deflink2 = new WeatherMapLink;
  $deflink2->name = "DEFAULT";
  $deflink2->template = ":: DEFAULT ::";
  $deflink2->Reset($this);
   
  $this->links['DEFAULT'] = &$deflink2;
   
  // for now, make the old defaultlink and defaultnode work too.
  // $this->defaultlink = $this->links['DEFAULT'];
  // $this->defaultnode = $this->nodes['DEFAULT'];
   
  assert('is_object($this->nodes[":: DEFAULT ::"])');
  assert('is_object($this->links[":: DEFAULT ::"])');
  assert('is_object($this->nodes["DEFAULT"])');
  assert('is_object($this->links["DEFAULT"])');
   
  // ************************************
   
   
$this->imap=new HTML_ImageMap('weathermap'); $this->imap=new HTML_ImageMap('weathermap');
$this->colours=array $this->colours=array
( (
); );
   
debug ("Adding default map colour set.\n"); debug ("Adding default map colour set.\n");
$defaults=array $defaults=array
( (
'KEYTEXT' => array('bottom' => -2, 'top' => -1, 'red1' => 0, 'green1' => 0, 'blue1' => 0), 'KEYTEXT' => array('bottom' => -2, 'top' => -1, 'red1' => 0, 'green1' => 0, 'blue1' => 0, 'special' => 1),
'KEYBG' => array('bottom' => -2, 'top' => -1, 'red1' => 255, 'green1' => 255, 'blue1' => 255), 'KEYOUTLINE' => array('bottom' => -2, 'top' => -1, 'red1' => 0, 'green1' => 0, 'blue1' => 0, 'special' => 1),
'BG' => array('bottom' => -2, 'top' => -1, 'red1' => 255, 'green1' => 255, 'blue1' => 255), 'KEYBG' => array('bottom' => -2, 'top' => -1, 'red1' => 255, 'green1' => 255, 'blue1' => 255, 'special' => 1),
'TITLE' => array('bottom' => -2, 'top' => -1, 'red1' => 0, 'green1' => 0, 'blue1' => 0), 'BG' => array('bottom' => -2, 'top' => -1, 'red1' => 255, 'green1' => 255, 'blue1' => 255, 'special' => 1),
'TIME' => array('bottom' => -2, 'top' => -1, 'red1' => 0, 'green1' => 0, 'blue1' => 0) 'TITLE' => array('bottom' => -2, 'top' => -1, 'red1' => 0, 'green1' => 0, 'blue1' => 0, 'special' => 1),
  'TIME' => array('bottom' => -2, 'top' => -1, 'red1' => 0, 'green1' => 0, 'blue1' => 0, 'special' => 1)
); );
   
foreach ($defaults as $key => $def) { $this->colours['DEFAULT'][$key]=$def; } foreach ($defaults as $key => $def) { $this->colours['DEFAULT'][$key]=$def; }
   
$this->configfile=''; $this->configfile='';
$this->imagefile=''; $this->imagefile='';
$this->imageuri=''; $this->imageuri='';
   
// $this->bg_r = 255;  
// $this->bg_g = 255;  
// $this->bg_b = 255;  
   
$this->fonts=array(); $this->fonts=array();
   
// Adding these makes the editor's job a little easier, mainly // Adding these makes the editor's job a little easier, mainly
for($i=1; $i<=5; $i++) for($i=1; $i<=5; $i++)
Line 2579... Line 420...
function myimagestring($image, $fontnumber, $x, $y, $string, $colour, $angle=0) function myimagestring($image, $fontnumber, $x, $y, $string, $colour, $angle=0)
{ {
// if it's supposed to be a special font, and it hasn't been defined, then fall through // if it's supposed to be a special font, and it hasn't been defined, then fall through
if ($fontnumber > 5 && !isset($this->fonts[$fontnumber])) if ($fontnumber > 5 && !isset($this->fonts[$fontnumber]))
{ {
warn ("Using a non-existent special font ($fontnumber) - falling back to internal GD fonts\n"); warn ("Using a non-existent special font ($fontnumber) - falling back to internal GD fonts [WMWARN03]\n");
if($angle != 0) warn("Angled text doesn't work with non-FreeType fonts\n"); if($angle != 0) warn("Angled text doesn't work with non-FreeType fonts [WMWARN02]\n");
$fontnumber=5; $fontnumber=5;
} }
   
if (($fontnumber > 0) && ($fontnumber < 6)) if (($fontnumber > 0) && ($fontnumber < 6))
{ {
imagestring($image, $fontnumber, $x, $y - imagefontheight($fontnumber), $string, $colour); imagestring($image, $fontnumber, $x, $y - imagefontheight($fontnumber), $string, $colour);
if($angle != 0) warn("Angled text doesn't work with non-FreeType fonts\n"); if($angle != 0) warn("Angled text doesn't work with non-FreeType fonts [WMWARN02]\n");
} }
else else
{ {
// look up what font is defined for this slot number // look up what font is defined for this slot number
if ($this->fonts[$fontnumber]->type == 'truetype') if ($this->fonts[$fontnumber]->type == 'truetype')
{ {
imagettftext($image, $this->fonts[$fontnumber]->size, $angle, $x, $y, wimagettftext($image, $this->fonts[$fontnumber]->size, $angle, $x, $y,
$colour, $this->fonts[$fontnumber]->file, $string); $colour, $this->fonts[$fontnumber]->file, $string);
} }
   
if ($this->fonts[$fontnumber]->type == 'gd') if ($this->fonts[$fontnumber]->type == 'gd')
{ {
imagestring($image, $this->fonts[$fontnumber]->gdnumber, imagestring($image, $this->fonts[$fontnumber]->gdnumber,
$x, $y - imagefontheight($this->fonts[$fontnumber]->gdnumber), $x, $y - imagefontheight($this->fonts[$fontnumber]->gdnumber),
$string, $colour); $string, $colour);
if($angle != 0) warn("Angled text doesn't work with non-FreeType fonts\n"); if($angle != 0) warn("Angled text doesn't work with non-FreeType fonts [WMWARN04]\n");
} }
} }
} }
   
function myimagestringsize($fontnumber, $string) function myimagestringsize($fontnumber, $string)
{ {
  $linecount = 1;
   
  $lines = split("\n",$string);
  $linecount = sizeof($lines);
  $maxlinelength=0;
  foreach($lines as $line)
  {
  $l = strlen($line);
  if($l > $maxlinelength) $maxlinelength = $l;
  }
   
if (($fontnumber > 0) && ($fontnumber < 6)) if (($fontnumber > 0) && ($fontnumber < 6))
{ return array(imagefontwidth($fontnumber) * strlen($string), imagefontheight($fontnumber)); } { return array(imagefontwidth($fontnumber) * $maxlinelength, $linecount * imagefontheight($fontnumber)); }
else else
{ {
// look up what font is defined for this slot number // look up what font is defined for this slot number
if (!isset($this->fonts[$fontnumber])) if (!isset($this->fonts[$fontnumber]))
{ {
warn ("Using a non-existent special font ($fontnumber) - falling back to internal GD fonts\n"); warn ("Using a non-existent special font ($fontnumber) - falling back to internal GD fonts [WMWARN36]\n");
$fontnumber=5; $fontnumber=5;
return array(imagefontwidth($fontnumber) * strlen($string), imagefontheight($fontnumber)); return array(imagefontwidth($fontnumber) * $maxlinelength, $linecount * imagefontheight($fontnumber));
} }
else else
{ {
if ($this->fonts[$fontnumber]->type == 'truetype') if ($this->fonts[$fontnumber]->type == 'truetype')
{ {
$bounds=imagettfbbox($this->fonts[$fontnumber]->size, 0, $this->fonts[$fontnumber]->file, $ysize = 0;
$string); $xsize = 0;
return (array($bounds[4] - $bounds[0], $bounds[1] - $bounds[5])); foreach($lines as $line)
  {
  $bounds=imagettfbbox($this->fonts[$fontnumber]->size, 0, $this->fonts[$fontnumber]->file, $line);
  $cx = $bounds[4] - $bounds[0];
  $cy = $bounds[1] - $bounds[5];
  if($cx > $xsize) $xsize = $cx;
  $ysize += ($cy*1.2);
  # warn("Adding $cy (x was $cx)\n");
  }
  #$bounds=imagettfbbox($this->fonts[$fontnumber]->size, 0, $this->fonts[$fontnumber]->file,
  # $string);
  # return (array($bounds[4] - $bounds[0], $bounds[1] - $bounds[5]));
  # warn("Size of $string is $xsize x $ysize over $linecount lines\n");
   
  return(array($xsize,$ysize));
} }
   
if ($this->fonts[$fontnumber]->type == 'gd') if ($this->fonts[$fontnumber]->type == 'gd')
{ return array(imagefontwidth($this->fonts[$fontnumber]->gdnumber) * strlen($string), { return array(imagefontwidth($this->fonts[$fontnumber]->gdnumber) * $maxlinelength,
imagefontheight($this->fonts[$fontnumber]->gdnumber)); } $linecount * imagefontheight($this->fonts[$fontnumber]->gdnumber)); }
} }
} }
} }
   
function ProcessString($input,&$context, $include_notes=TRUE) function ProcessString($input,&$context, $include_notes=TRUE,$multiline=FALSE)
{ {
$output = $input;  
   
# debug("ProcessString: input is $input\n"); # debug("ProcessString: input is $input\n");
   
  assert('is_scalar($input)');
   
  $context_description = strtolower( $context->my_type() );
  if($context_description != "map") $context_description .= ":" . $context->name;
   
  debug("Trace: ProcessString($input, $context_description)\n");
   
  if($multiline==TRUE)
  {
  $i = $input;
  $input = str_replace("\\n","\n",$i);
  # if($i != $input) warn("$i into $input\n");
  }
   
  $output = $input;
   
# while( preg_match("/(\{[^}]+\})/",$input,$matches) ) # while( preg_match("/(\{[^}]+\})/",$input,$matches) )
while( preg_match("/(\{(?:node|map|link)[^}]+\})/",$input,$matches) ) while( preg_match("/(\{(?:node|map|link)[^}]+\})/",$input,$matches) )
{ {
$value = "[UNKNOWN]"; $value = "[UNKNOWN]";
$format = ""; $format = "";
$key = $matches[1]; $key = $matches[1];
# debug("ProcessString: working on ".$key."\n"); debug("ProcessString: working on ".$key."\n");
   
if ( preg_match("/\{(node|map|link):([^}]+)\}/",$key,$matches) ) if ( preg_match("/\{(node|map|link):([^}]+)\}/",$key,$matches) )
{ {
$type = $matches[1]; $type = $matches[1];
$args = $matches[2]; $args = $matches[2];
# debug("ProcessString: type is ".$type.", arguments are ".$args."\n"); # debug("ProcessString: type is ".$type.", arguments are ".$args."\n");
   
if($type == 'map') if($type == 'map')
{ {
$the_item = $this; $the_item = $this;
if(preg_match("/map:([^:]+):*([^:]*)/",$args,$matches)) if(preg_match("/map:([^:]+):*([^:]*)/",$args,$matches))
Line 2681... Line 561...
   
$the_item = NULL; $the_item = NULL;
if( ($itemname == "this") && ($type == strtolower($context->my_type())) ) if( ($itemname == "this") && ($type == strtolower($context->my_type())) )
{ {
$the_item = $context; $the_item = $context;
  }
  elseif( strtolower($context->my_type()) == "link" && $type == 'node' && ($itemname == '_linkstart_' || $itemname == '_linkend_') )
  {
  // this refers to the two nodes at either end of this link
  if($itemname == '_linkstart_')
  {
  $the_item = $context->a;
  }
   
  if($itemname == '_linkend_')
  {
  $the_item = $context->b;
  }
} }
elseif( ($itemname == "parent") && ($type == strtolower($context->my_type())) && ($type=='node') && ($context->relative_to != '') ) elseif( ($itemname == "parent") && ($type == strtolower($context->my_type())) && ($type=='node') && ($context->relative_to != '') )
{ {
$the_item = $this->nodes[$context->relative_to]; $the_item = $this->nodes[$context->relative_to];
} }
else else
{ {
if( ($type == 'link') && isset($this->links[$itemname]) ) if( ($type == 'link') && isset($this->links[$itemname]) )
{ {
Line 2702... Line 595...
} }
} }
   
if(is_null($the_item)) if(is_null($the_item))
{ {
warn("ProcessString: $key refers to unknown item\n"); warn("ProcessString: $key refers to unknown item (context is $context_description) [WMWARN05]\n");
} }
else else
{ {
# warn($the_item->name.": ".var_dump($the_item->hints)."\n"); # warn($the_item->name.": ".var_dump($the_item->hints)."\n");
debug("ProcessString: Found appropriate item: ".get_class($the_item)." ".$the_item->name."\n"); debug("ProcessString: Found appropriate item: ".get_class($the_item)." ".$the_item->name."\n");
   
# warn($the_item->name."/hints: ".var_dump($the_item->hints)."\n"); # warn($the_item->name."/hints: ".var_dump($the_item->hints)."\n");
# warn($the_item->name."/notes: ".var_dump($the_item->notes)."\n"); # warn($the_item->name."/notes: ".var_dump($the_item->notes)."\n");
   
// SET and notes have precedent over internal properties // SET and notes have precedent over internal properties
// this is my laziness - it saves me having a list of reserved words // this is my laziness - it saves me having a list of reserved words
// which are currently used for internal props. You can just 'overwrite' any of them. // which are currently used for internal props. You can just 'overwrite' any of them.
if(isset($the_item->hints[$args])) if(isset($the_item->hints[$args]))
{ {
Line 2727... Line 620...
// able to get data from uncontrolled sources (i.e. data sources rather than SET in config files). // able to get data from uncontrolled sources (i.e. data sources rather than SET in config files).
elseif($include_notes && isset($the_item->notes[$args])) elseif($include_notes && isset($the_item->notes[$args]))
{ {
$value = $the_item->notes[$args]; $value = $the_item->notes[$args];
debug("ProcessString: used note\n"); debug("ProcessString: used note\n");
   
} }
elseif(isset($the_item->$args)) elseif(isset($the_item->$args))
{ {
$value = $the_item->$args; $value = $the_item->$args;
debug("ProcessString: used internal property\n"); debug("ProcessString: used internal property\n");
} }
} }
} }
   
// format, and sanitise the value string here, before returning it // format, and sanitise the value string here, before returning it
   
  if($value===NULL) $value='NULL';
debug("ProcessString: replacing ".$key." with $value\n"); debug("ProcessString: replacing ".$key." with $value\n");
   
# if($format != '') $value = sprintf($format,$value); # if($format != '') $value = sprintf($format,$value);
if($format != '') if($format != '')
{ {
   
# debug("Formatting with mysprintf($format,$value)\n"); # debug("Formatting with mysprintf($format,$value)\n");
$value = mysprintf($format,$value); $value = mysprintf($format,$value);
} }
Line 2769... Line 663...
} }
   
function LoadPlugins( $type="data", $dir="lib/datasources" ) function LoadPlugins( $type="data", $dir="lib/datasources" )
{ {
debug("Beginning to load $type plugins from $dir\n"); debug("Beginning to load $type plugins from $dir\n");
   
  if ( ! file_exists($dir)) {
  $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . $dir;
  debug("Relative path didn't exist. Trying $dir\n");
  }
# $this->datasourceclasses = array(); # $this->datasourceclasses = array();
$dh=@opendir($dir); $dh=@opendir($dir);
   
if(!$dh) { // try to find it with the script, if the relative path fails if(!$dh) { // try to find it with the script, if the relative path fails
$srcdir = substr($_SERVER['argv'][0], 0, strrpos($_SERVER['argv'][0], DIRECTORY_SEPARATOR)); $srcdir = substr($_SERVER['argv'][0], 0, strrpos($_SERVER['argv'][0], DIRECTORY_SEPARATOR));
Line 2790... Line 689...
{ {
debug("Loading $type Plugin class from $file\n"); debug("Loading $type Plugin class from $file\n");
   
include_once( $realfile ); include_once( $realfile );
$class = preg_replace( "/\.php$/", "", $file ); $class = preg_replace( "/\.php$/", "", $file );
if($type == 'data') if($type == 'data')
{ {
$this->datasourceclasses [$class]= $class; $this->datasourceclasses [$class]= $class;
$this->activedatasourceclasses[$class]=1; $this->activedatasourceclasses[$class]=1;
} }
if($type == 'pre') $this->preprocessclasses [$class]= $class; if($type == 'pre') $this->preprocessclasses [$class]= $class;
if($type == 'post') $this->postprocessclasses [$class]= $class; if($type == 'post') $this->postprocessclasses [$class]= $class;
   
debug("Loaded $type Plugin class $class from $file\n"); debug("Loaded $type Plugin class $class from $file\n");
  $this->plugins[$type][$class] = new $class;
  if(! isset($this->plugins[$type][$class]))
  {
  debug("** Failed to create an object for plugin $type/$class\n");
  }
  else
  {
  debug("Instantiated $class.\n");
  }
} }
else else
{ {
debug("Skipping $file\n"); debug("Skipping $file\n");
} }
} }
} }
else else
{ {
warn("Couldn't open $type Plugin directory ($dir). Things will probably go wrong.\n"); warn("Couldn't open $type Plugin directory ($dir). Things will probably go wrong. [WMWARN06]\n");
} }
} }
   
function ReadData() function DatasourceInit()
{ {
   
debug("Running Init() for Data Source Plugins...\n"); debug("Running Init() for Data Source Plugins...\n");
foreach ($this->datasourceclasses as $ds_class) foreach ($this->datasourceclasses as $ds_class)
{ {
  // make an instance of the class
  $dsplugins[$ds_class] = new $ds_class;
debug("Running $ds_class"."->Init()\n"); debug("Running $ds_class"."->Init()\n");
$ret = call_user_func(array($ds_class, 'Init'), $this); # $ret = call_user_func(array($ds_class, 'Init'), $this);
  assert('isset($this->plugins["data"][$ds_class])');
   
  $ret = $this->plugins['data'][$ds_class]->Init($this);
   
if(! $ret) if(! $ret)
{ {
debug("Removing $ds_class from Data Source list, since Init() failed\n"); debug("Removing $ds_class from Data Source list, since Init() failed\n");
$this->activedatasourceclasses[$ds_class]=0; $this->activedatasourceclasses[$ds_class]=0;
# unset($this->datasourceclasses[$ds_class]); # unset($this->datasourceclasses[$ds_class]);
} }
} }
debug("Finished Initialising Plugins...\n"); debug("Finished Initialising Plugins...\n");
  }
   
debug ("================== ReadData: Updating link data for all links and nodes\n"); function ProcessTargets()
  {
  debug("Preprocessing targets\n");
   
  $allitems = array(&$this->links, &$this->nodes);
  reset($allitems);
   
  debug("Preprocessing targets\n");
   
  while( list($kk,) = each($allitems))
  {
  unset($objects);
  $objects = &$allitems[$kk];
   
  reset($objects);
  while (list($k,) = each($objects))
  {
  unset($myobj);
  $myobj = &$objects[$k];
   
  $type = $myobj->my_type();
  $name=$myobj->name;
   
   
  if( ($type=='LINK' && isset($myobj->a)) || ($type=='NODE' && !is_null($myobj->x) ) )
  {
  if (count($myobj->targets)>0)
  {
  $tindex = 0;
  foreach ($myobj->targets as $target)
  {
  debug ("ProcessTargets: New Target: $target[4]\n");
  // processstring won't use notes (only hints) for this string
   
  $targetstring = $this->ProcessString($target[4], $myobj, FALSE, FALSE);
  if($target[4] != $targetstring) debug("Targetstring is now $targetstring\n");
   
  // if the targetstring starts with a -, then we're taking this value OFF the aggregate
  $multiply = 1;
  if(preg_match("/^-(.*)/",$targetstring,$matches))
  {
  $targetstring = $matches[1];
  $multiply = -1 * $multiply;
  }
   
  // if the remaining targetstring starts with a number and a *-, then this is a scale factor
  if(preg_match("/^(\d+\.?\d*)\*(.*)/",$targetstring,$matches))
  {
  $targetstring = $matches[2];
  $multiply = $multiply * floatval($matches[1]);
  }
   
  $matched = FALSE;
  $matched_by = '';
  foreach ($this->datasourceclasses as $ds_class)
  {
  if(!$matched)
  {
  // $recognised = call_user_func(array($ds_class, 'Recognise'), $targetstring);
  $recognised = $this->plugins['data'][$ds_class]->Recognise($targetstring);
   
  if( $recognised )
  {
  $matched = TRUE;
  $matched_by = $ds_class;
   
  if($this->activedatasourceclasses[$ds_class])
  {
  $this->plugins['data'][$ds_class]->Register($targetstring, $this, $myobj);
  if($type == 'NODE')
  {
  $this->nodes[$name]->targets[$tindex][1] = $multiply;
  $this->nodes[$name]->targets[$tindex][0] = $targetstring;
  $this->nodes[$name]->targets[$tindex][5] = $matched_by;
  }
  if($type == 'LINK')
  {
  $this->links[$name]->targets[$tindex][1] = $multiply;
  $this->links[$name]->targets[$tindex][0] = $targetstring;
  $this->links[$name]->targets[$tindex][5] = $matched_by;
  }
  }
  else
  {
  warn("ProcessTargets: $type $name, target: $targetstring on config line $target[3] of $target[2] was recognised as a valid TARGET by a plugin that is unable to run ($ds_class) [WMWARN07]\n");
  }
  }
  }
  }
  if(! $matched)
  {
  warn("ProcessTargets: $type $name, target: $target[4] on config line $target[3] of $target[2] was not recognised as a valid TARGET [WMWARN08]\n");
  }
   
  $tindex++;
  }
  }
  }
  }
  }
  }
   
  function ReadData()
  {
  $this->DatasourceInit();
   
  debug ("======================================\n");
  debug("ReadData: Updating link data for all links and nodes\n");
   
  // we skip readdata completely in sizedebug mode
if ($this->sizedebug == 0) if ($this->sizedebug == 0)
{ {
  $this->ProcessTargets();
   
  debug ("======================================\n");
  debug("Starting prefetch\n");
  foreach ($this->datasourceclasses as $ds_class)
  {
  $this->plugins['data'][$ds_class]->Prefetch();
  }
   
  debug ("======================================\n");
  debug("Starting main collection loop\n");
   
$allitems = array(&$this->links, &$this->nodes); $allitems = array(&$this->links, &$this->nodes);
reset($allitems); reset($allitems);
   
while( list($kk,) = each($allitems)) while( list($kk,) = each($allitems))
{ {
unset($objects); unset($objects);
# $objects = &$this->links;  
$objects = &$allitems[$kk]; $objects = &$allitems[$kk];
   
reset($objects); reset($objects);
while (list($k,) = each($objects)) while (list($k,) = each($objects))
{ {
unset($myobj); unset($myobj);
$myobj = &$objects[$k]; $myobj = &$objects[$k];
   
$type = $myobj->my_type(); $type = $myobj->my_type();
   
$total_in=0; $total_in=0;
$total_out=0; $total_out=0;
$name=$myobj->name; $name=$myobj->name;
debug ("\n\nReadData for $type $name: \n"); debug ("\n");
  debug ("ReadData for $type $name: \n");
   
if (count($myobj->targets)>0) if( ($type=='LINK' && isset($myobj->a)) || ($type=='NODE' && !is_null($myobj->x) ) )
{ {
foreach ($myobj->targets as $target) if (count($myobj->targets)>0)
{ {
debug ("ReadData: New Target: $target[0]\n"); $tindex = 0;
  foreach ($myobj->targets as $target)
   
   
$in = 0;  
$out = 0;  
if ($target[4] != '')  
{ {
// processstring won't use notes (only hints) for this string debug ("ReadData: New Target: $target[4]\n");
$targetstring = $this->ProcessString($target[4], $myobj, FALSE); # debug ( var_dump($target));
if($target[4] != $targetstring) debug("Targetstring is now $targetstring\n");  
  $targetstring = $target[0];
// if the targetstring starts with a -, then we're taking this value OFF the aggregate $multiply = $target[1];
$multiply = 1;  
if(preg_match("/^-(.*)/",$target[4],$matches)) # exit();
   
  $in = 0;
  $out = 0;
  $datatime = 0;
  if ($target[4] != '')
{ {
$targetstring = $matches[1]; // processstring won't use notes (only hints) for this string
$multiply = -1;  
} $targetstring = $this->ProcessString($target[0], $myobj, FALSE, FALSE);
  if($target[0] != $targetstring) debug("Targetstring is now $targetstring\n");
$matched = FALSE; if($multiply != 1) debug("Will multiply result by $multiply\n");
$matched_by = '';  
foreach ($this->datasourceclasses as $ds_class) if($target[0] != "")
{  
if(!$matched)  
{ {
$recognised = call_user_func(array($ds_class, 'Recognise'), $targetstring); $matched_by = $target[5];
  list($in,$out,$datatime) = $this->plugins['data'][ $target[5] ]->ReadData($targetstring, $this, $myobj);
  }
   
  if (($in === NULL) && ($out === NULL))
  {
  $in=0;
  $out=0;
  warn
  ("ReadData: $type $name, target: $targetstring on config line $target[3] of $target[2] had no valid data, according to $matched_by\n");
  }
  else
  {
  if($in === NULL) $in = 0;
  if($out === NULL) $out = 0;
  }
   
  if($multiply != 1) {
  debug("Pre-multiply: $in $out\n");
   
  $in = $multiply*$in;
  $out = $multiply*$out;
   
  debug("Post-multiply: $in $out\n");
  }
   
  $total_in=$total_in + $in;
  $total_out=$total_out + $out;
  debug("Aggregate so far: $total_in $total_out\n");
  # keep a track of the range of dates for data sources (mainly for MRTG/textfile based DS)
  if($datatime > 0)
  {
  if($this->max_data_time==NULL || $datatime > $this->max_data_time) $this->max_data_time = $datatime;
  if($this->min_data_time==NULL || $datatime < $this->min_data_time) $this->min_data_time = $datatime;
   
if( $recognised ) debug("DataTime MINMAX: ".$this->min_data_time." -> ".$this->max_data_time."\n");
{  
if($this->activedatasourceclasses[$ds_class])  
{  
debug("ReadData: Matched for $ds_class. Calling ${ds_class}->ReadData()\n");  
   
// line number is in $target[3]  
# list($in,$out,$datatime) = call_user_func( array($ds_class, 'ReadData'), $targetstring, $this, $myobj );  
list($in,$out,$datatime) = call_user_func_array( array($ds_class, 'ReadData'), array($targetstring, &$this, &$myobj));  
}  
else  
{  
warn("ReadData: $type $name, target: $targetstring on config line $target[3] was recognised as a valid TARGET by a plugin that is unable to run ($ds_class)\n");  
}  
$matched = TRUE;  
$matched_by = $ds_class;  
}  
} }
   
} }
  $tindex++;
if(! $matched)  
{  
// **  
warn("ReadData: $type $name, target: $target[4] on config line $target[3] was not recognised as a valid TARGET\n");  
}  
   
if (($in < 0) || ($out < 0))  
{  
$in=0;  
$out=0;  
// **  
warn  
("ReadData: $type $name, target: $targetstring on config line $target[3] had no valid data, according to $matched_by\n");  
}  
   
$total_in=$total_in + $multiply*$in;  
$total_out=$total_out + $multiply*$out;  
} }
   
  debug ("ReadData complete for $type $name: $total_in $total_out\n");
  }
  else
  {
  debug("ReadData: No targets for $type $name\n");
} }
} }
else else
{ {
debug("ReadData: No targets for $type $name\n"); debug("ReadData: Skipping $type $name that looks like a template\n.");
} }
   
# $this->links[$name]->bandwidth_in=$total_in; # $this->links[$name]->bandwidth_in=$total_in;
# $this->links[$name]->bandwidth_out=$total_out; # $this->links[$name]->bandwidth_out=$total_out;
$myobj->bandwidth_in = $total_in; $myobj->bandwidth_in = $total_in;
$myobj->bandwidth_out = $total_out; $myobj->bandwidth_out = $total_out;
   
  if($type == 'LINK' && $myobj->duplex=='half')
  {
  // in a half duplex link, in and out share a common bandwidth pool, so percentages need to include both
  debug("Calculating percentage using half-duplex\n");
  $myobj->outpercent = (($total_in + $total_out) / ($myobj->max_bandwidth_out)) * 100;
  $myobj->inpercent = (($total_out + $total_in) / ($myobj->max_bandwidth_in)) * 100;
  if($myobj->max_bandwidth_out != $myobj->max_bandwidth_in)
  {
  warn("ReadData: $type $name: You're using asymmetric bandwidth AND half-duplex in the same link. That makes no sense. [WMWARN44]\n");
  }
  }
  else
  {
  $myobj->outpercent = (($total_out) / ($myobj->max_bandwidth_out)) * 100;
  $myobj->inpercent = (($total_in) / ($myobj->max_bandwidth_in)) * 100;
  }
   
  # print $myobj->name."=>".$myobj->inpercent."%/".$myobj->outpercent."\n";
   
  $warn_in = true;
  $warn_out = true;
  if($type=='NODE' && $myobj->scalevar =='in') $warn_out = false;
  if($type=='NODE' && $myobj->scalevar =='out') $warn_in = false;
   
  if($myobj->scaletype == 'percent')
  {
  list($incol,$inscalekey,$inscaletag) = $this->NewColourFromPercent($myobj->inpercent,$myobj->usescale,$myobj->name, TRUE, $warn_in);
  list($outcol,$outscalekey, $outscaletag) = $this->NewColourFromPercent($myobj->outpercent,$myobj->usescale,$myobj->name, TRUE, $warn_out);
  }
  else
  {
  // use absolute values, if that's what is requested
  list($incol,$inscalekey,$inscaletag) = $this->NewColourFromPercent($myobj->bandwidth_in,$myobj->usescale,$myobj->name, FALSE, $warn_in);
  list($outcol,$outscalekey, $outscaletag) = $this->NewColourFromPercent($myobj->bandwidth_out,$myobj->usescale,$myobj->name, FALSE, $warn_out);
  }
   
$myobj->outpercent = (($total_out) / ($myobj->max_bandwidth_out)) * 100; $myobj->add_note("inscalekey",$inscalekey);
$myobj->inpercent = (($total_in) / ($myobj->max_bandwidth_in)) * 100; $myobj->add_note("outscalekey",$outscalekey);
   
list($incol,$inscalekey) = $this->ColourFromPercent($myobj->inpercent,$myobj->usescale,$myobj->name);  
list($outcol,$outscalekey) = $this->ColourFromPercent($myobj->outpercent,$myobj->usescale,$myobj->name);  
   
// $myobj->incolour = $incol; $myobj->add_note("inscaletag",$inscaletag);
$myobj->inscalekey = $inscalekey; $myobj->add_note("outscaletag",$outscaletag);
// $myobj->outcolour = $outcol;  
$myobj->outscalekey = $outscalekey;  
   
  $myobj->add_note("inscalecolor",$incol->as_html());
  $myobj->add_note("outscalecolor",$outcol->as_html());
   
  $myobj->colours[IN] = $incol;
  $myobj->colours[OUT] = $outcol;
   
  ### warn("TAGS (setting) |$inscaletag| |$outscaletag| \n");
   
debug ("ReadData: Setting $total_in,$total_out\n"); debug ("ReadData: Setting $total_in,$total_out\n");
unset($myobj); unset($myobj);
} }
} }
debug ("\nReadData Completed.\n--------------\n"); debug ("ReadData Completed.\n");
  debug("------------------------------\n");
} }
} }
   
// nodename is a vestigal parameter, from the days when nodes where just big labels // nodename is a vestigal parameter, from the days when nodes were just big labels
function DrawLabel($im, $x, $y, $text, $font, $padding, $linkname, $textcolour, $bgcolour, $outlinecolour, &$map) function DrawLabelRotated($im, $x, $y, $angle, $text, $font, $padding, $linkname, $textcolour, $bgcolour, $outlinecolour, &$map, $direction)
{ {
list($strwidth, $strheight)=$this->myimagestringsize($font, $text); list($strwidth, $strheight)=$this->myimagestringsize($font, $text);
   
$extra=3; if(abs($angle)>90) $angle -= 180;
  if($angle < -180) $angle +=360;
$x1=$x - ($strwidth / 2) - $padding - $extra;  
$x2=$x + ($strwidth / 2) + $padding + $extra;  
$y1=$y - ($strheight / 2) - $padding - $extra;  
$y2=$y + ($strheight / 2) + $padding + $extra;  
   
if ($bgcolour != array  
(  
-1,  
-1,  
-1  
))  
{  
$bgcol=myimagecolorallocate($im, $bgcolour[0], $bgcolour[1], $bgcolour[2]);  
imagefilledrectangle($im, $x1, $y1, $x2, $y2, $bgcol);  
}  
   
if ($outlinecolour != array  
(  
-1,  
-1,  
-1  
))  
{  
$outlinecol=myimagecolorallocate($im, $outlinecolour[0], $outlinecolour[1], $outlinecolour[2]);  
imagerectangle($im, $x1, $y1, $x2, $y2, $outlinecol);  
}  
   
$textcol=myimagecolorallocate($im, $textcolour[0], $textcolour[1], $textcolour[2]);  
$this->myimagestring($im, $font, $x - $strwidth / 2, $y + $strheight / 2 + 1, $text, $textcol);  
   
$this->imap->addArea("Rectangle", "LINK:".$linkname, '', array($x1, $y1, $x2, $y2));  
   
}  
   
// nodename is a vestigal parameter, from the days when nodes where just big labels  
function DrawLabelRotated($im, $x, $y, $angle, $text, $font, $padding, $linkname, $textcolour, $bgcolour, $outlinecolour, &$map)  
{  
list($strwidth, $strheight)=$this->myimagestringsize($font, $text);  
   
if(abs($angle)>90)  
{  
$angle -= 180;  
if($angle < -180) $angle +=360;  
}  
   
$rangle = -deg2rad($angle); $rangle = -deg2rad($angle);
   
$extra=3; $extra=3;
   
$x1= $x - ($strwidth / 2) - $padding - $extra; $x1= $x - ($strwidth / 2) - $padding - $extra;
$x2= $x + ($strwidth / 2) + $padding + $extra; $x2= $x + ($strwidth / 2) + $padding + $extra;
$y1= $y - ($strheight / 2) - $padding - $extra; $y1= $y - ($strheight / 2) - $padding - $extra;
$y2= $y + ($strheight / 2) + $padding + $extra; $y2= $y + ($strheight / 2) + $padding + $extra;
   
// a box. the last point is the start point for the text. // a box. the last point is the start point for the text.
$points = array($x1,$y1, $x1,$y2, $x2,$y2, $x2,$y1, $x-$strwidth/2, $y+$strheight/2 + 1); $points = array($x1,$y1, $x1,$y2, $x2,$y2, $x2,$y1, $x-$strwidth/2, $y+$strheight/2 + 1);
$npoints = count($points)/2; $npoints = count($points)/2;
   
RotateAboutPoint($points, $x,$y, $rangle); RotateAboutPoint($points, $x,$y, $rangle);
   
if ($bgcolour != array if ($bgcolour != array
( (
-1, -1,
-1, -1,
-1 -1
)) ))
{ {
$bgcol=myimagecolorallocate($im, $bgcolour[0], $bgcolour[1], $bgcolour[2]); $bgcol=myimagecolorallocate($im, $bgcolour[0], $bgcolour[1], $bgcolour[2]);
# imagefilledrectangle($im, $x1, $y1, $x2, $y2, $bgcol); # imagefilledrectangle($im, $x1, $y1, $x2, $y2, $bgcol);
imagefilledpolygon($im,$points,4,$bgcol); wimagefilledpolygon($im,$points,4,$bgcol);
} }
   
if ($outlinecolour != array if ($outlinecolour != array
( (
-1, -1,
Line 3045... Line 1079...
-1 -1
)) ))
{ {
$outlinecol=myimagecolorallocate($im, $outlinecolour[0], $outlinecolour[1], $outlinecolour[2]); $outlinecol=myimagecolorallocate($im, $outlinecolour[0], $outlinecolour[1], $outlinecolour[2]);
# imagerectangle($im, $x1, $y1, $x2, $y2, $outlinecol); # imagerectangle($im, $x1, $y1, $x2, $y2, $outlinecol);
imagepolygon($im,$points,4,$outlinecol); wimagepolygon($im,$points,4,$outlinecol);
} }
   
$textcol=myimagecolorallocate($im, $textcolour[0], $textcolour[1], $textcolour[2]); $textcol=myimagecolorallocate($im, $textcolour[0], $textcolour[1], $textcolour[2]);
$this->myimagestring($im, $font, $points[8], $points[9], $text, $textcol,$angle); $this->myimagestring($im, $font, $points[8], $points[9], $text, $textcol,$angle);
   
$this->imap->addArea("Rectangle", "LINK:".$linkname, '', array($x1, $y1, $x2, $y2)); $areaname = "LINK:L".$map->links[$linkname]->id.':'.($direction+2);
   
  // the rectangle is about half the size in the HTML, and easier to optimise/detect in the browser
  if($angle==0)
  {
  $map->imap->addArea("Rectangle", $areaname, '', array($x1, $y1, $x2, $y2));
  debug ("Adding Rectangle imagemap for $areaname\n");
  }
  else
  {
  $map->imap->addArea("Polygon", $areaname, '', $points);
  debug ("Adding Poly imagemap for $areaname\n");
  }
   
} }
   
function ColourFromPercent($percent,$scalename="DEFAULT",$name="") function ColourFromPercent($image, $percent,$scalename="DEFAULT",$name="")
{ {
$col = NULL; $col = NULL;
  $tag = '';
   
  $nowarn_clipping = intval($this->get_hint("nowarn_clipping"));
  $nowarn_scalemisses = intval($this->get_hint("nowarn_scalemisses"));
   
  $bt = debug_backtrace();
  $function = (isset($bt[1]['function']) ? $bt[1]['function'] : '');
  print "$function calls ColourFromPercent\n";
   
  exit();
   
if(isset($this->colours[$scalename])) if(isset($this->colours[$scalename]))
{ {
$colours=$this->colours[$scalename]; $colours=$this->colours[$scalename];
   
if ($percent > 100) if ($percent > 100)
{ {
warn ("ColourFromPercent: Clipped $name $percent% to 100%\n"); if($nowarn_clipping==0) warn ("ColourFromPercent: Clipped $name $percent% to 100% [WMWARN33]\n");
$percent=100; $percent=100;
} }
   
foreach ($colours as $key => $colour) foreach ($colours as $key => $colour)
{ {
if (($percent >= $colour['bottom']) and ($percent <= $colour['top'])) if (($percent >= $colour['bottom']) and ($percent <= $colour['top']))
{ {
  if(isset($colour['tag'])) $tag = $colour['tag'];
   
// we get called early now, so might not need to actually allocate a colour // we get called early now, so might not need to actually allocate a colour
if(isset($this->image)) if(isset($image))
{ {
if (isset($colour['red2'])) if (isset($colour['red2']))
{ {
if($colour["bottom"] == $colour["top"]) if($colour["bottom"] == $colour["top"])
{ {
Line 3086... Line 1144...
} }
else else
{ {
$ratio=($percent - $colour["bottom"]) / ($colour["top"] - $colour["bottom"]); $ratio=($percent - $colour["bottom"]) / ($colour["top"] - $colour["bottom"]);
} }
   
$r=$colour["red1"] + ($colour["red2"] - $colour["red1"]) * $ratio; $r=$colour["red1"] + ($colour["red2"] - $colour["red1"]) * $ratio;
$g=$colour["green1"] + ($colour["green2"] - $colour["green1"]) * $ratio; $g=$colour["green1"] + ($colour["green2"] - $colour["green1"]) * $ratio;
$b=$colour["blue1"] + ($colour["blue2"] - $colour["blue1"]) * $ratio; $b=$colour["blue1"] + ($colour["blue2"] - $colour["blue1"]) * $ratio;
   
$col = myimagecolorallocate($this->image, $r, $g, $b); $col = myimagecolorallocate($image, $r, $g, $b);
} }
else { else {
$col = $colour['gdref1']; $r=$colour["red1"];
  $g=$colour["green1"];
  $b=$colour["blue1"];
   
  $col = myimagecolorallocate($image, $r, $g, $b);
  # $col = $colour['gdref1'];
} }
  debug("CFPC $name $tag $key $r $g $b\n");
  }
   
  ### warn(">>TAGS CFPC $tag\n");
   
  return(array($col,$key,$tag));
  }
  }
  }
  else
  {
  if($scalename != 'none')
  {
  warn("ColourFromPercent: Attempted to use non-existent scale: $scalename for $name [WMWARN09]\n");
  }
  else
  {
  return array($this->white,'','');
  }
  }
   
  // you'll only get grey for a COMPLETELY quiet link if there's no 0 in the SCALE lines
  if ($percent == 0) { return array($this->grey,'',''); }
   
  // and you'll only get white for a link with no colour assigned
  if($nowarn_scalemisses==0) warn("ColourFromPercent: Scale $scalename doesn't cover $percent% for $name [WMWARN29]\n");
  return array($this->white,'','');
  }
   
  function NewColourFromPercent($value,$scalename="DEFAULT",$name="",$is_percent=TRUE, $scale_warning=TRUE)
  {
  $col = new Colour(0,0,0);
  $tag = '';
  $matchsize = NULL;
   
  $nowarn_clipping = intval($this->get_hint("nowarn_clipping"));
  $nowarn_scalemisses = (!$scale_warning) || intval($this->get_hint("nowarn_scalemisses"));
   
  if(isset($this->colours[$scalename]))
  {
  $colours=$this->colours[$scalename];
   
  if ($is_percent && $value > 100)
  {
  if($nowarn_clipping==0) warn ("NewColourFromPercent: Clipped $value% to 100% for item $name [WMWARN33]\n");
  $value = 100;
  }
   
  if ($is_percent && $value < 0)
  {
  if($nowarn_clipping==0) warn ("NewColourFromPercent: Clipped $value% to 0% for item $name [WMWARN34]\n");
  $value = 0;
  }
   
  foreach ($colours as $key => $colour)
  {
  if ( (!isset($colour['special']) || $colour['special'] == 0) and ($value >= $colour['bottom']) and ($value <= $colour['top']))
  {
  $range = $colour['top'] - $colour['bottom'];
  if (isset($colour['red2']))
  {
  if($colour["bottom"] == $colour["top"])
  {
  $ratio = 0;
  }
  else
  {
  $ratio=($value - $colour["bottom"]) / ($colour["top"] - $colour["bottom"]);
  }
   
  $r=$colour["red1"] + ($colour["red2"] - $colour["red1"]) * $ratio;
  $g=$colour["green1"] + ($colour["green2"] - $colour["green1"]) * $ratio;
  $b=$colour["blue1"] + ($colour["blue2"] - $colour["blue1"]) * $ratio;
  }
  else {
  $r=$colour["red1"];
  $g=$colour["green1"];
  $b=$colour["blue1"];
   
  # $col = new Colour($r, $g, $b);
  # $col = $colour['gdref1'];
} }
   
return(array($col,$key)); // change in behaviour - with multiple matching ranges for a value, the smallest range wins
  if( is_null($matchsize) || ($range < $matchsize) )
  {
  $col = new Colour($r, $g, $b);
  $matchsize = $range;
  }
   
  if(isset($colour['tag'])) $tag = $colour['tag'];
  #### warn(">>NCFPC TAGS $tag\n");
  debug("NCFPC $name $scalename $value '$tag' $key $r $g $b\n");
   
  return(array($col,$key,$tag));
} }
} }
} }
else else
{ {
warn("ColourFromPercent: Attempted to use non-existent scale: $scalename for $name\n"); if($scalename != 'none')
  {
  warn("ColourFromPercent: Attempted to use non-existent scale: $scalename for item $name [WMWARN09]\n");
  }
  else
  {
  return array(new Colour(255,255,255),'','');
  }
} }
   
  // shouldn't really get down to here if there's a complete SCALE
   
// you'll only get grey for a COMPLETELY quiet link if there's no 0 in the SCALE lines // you'll only get grey for a COMPLETELY quiet link if there's no 0 in the SCALE lines
if ($percent == 0) { return array($this->grey,''); } if ($value == 0) { return array(new Colour(192,192,192),'',''); }
   
  if($nowarn_scalemisses==0) warn("NewColourFromPercent: Scale $scalename doesn't include a line for $value".($is_percent ? "%" : "")." while drawing item $name [WMWARN29]\n");
   
// and you'll only get white for a link with no colour assigned // and you'll only get white for a link with no colour assigned
return array($this->white,''); return array(new Colour(255,255,255),'','');
} }
   
   
function coloursort($a, $b) function coloursort($a, $b)
{ {
if ($a['bottom'] == $b['bottom']) if ($a['bottom'] == $b['bottom'])
{ {
Line 3126... Line 1293...
} }
   
if ($a['bottom'] < $b['bottom']) { return -1; } if ($a['bottom'] < $b['bottom']) { return -1; }
   
return 1; return 1;
} }
   
  function FindScaleExtent($scalename="DEFAULT")
  {
  $max = -999999999999999999999;
  $min = - $max;
   
  if(isset($this->colours[$scalename]))
  {
  $colours=$this->colours[$scalename];
   
  foreach ($colours as $key => $colour)
  {
  if(! $colour['special'])
  {
  $min = min($colour['bottom'], $min);
  $max = max($colour['top'], $max);
  }
  }
  }
  else
  {
  warn("FindScaleExtent: non-existent SCALE $scalename [WMWARN43]\n");
  }
  return array($min, $max);
  }
   
function DrawLegend_Horizontal($im,$scalename="DEFAULT",$width=400) function DrawLegend_Horizontal($im,$scalename="DEFAULT",$width=400)
{ {
$title=$this->keytext[$scalename]; $title=$this->keytext[$scalename];
   
Line 3139... Line 1331...
   
debug("Drawing $nscales colours into SCALE\n"); debug("Drawing $nscales colours into SCALE\n");
   
$font=$this->keyfont; $font=$this->keyfont;
   
$x=$this->keyx[$scalename]; # $x=$this->keyx[$scalename];
$y=$this->keyy[$scalename]; # $y=$this->keyy[$scalename];
  $x = 0;
  $y = 0;
   
# $width = 400; # $width = 400;
$scalefactor = $width/100; $scalefactor = $width/100;
   
list($tilewidth, $tileheight)=$this->myimagestringsize($font, "100%"); list($tilewidth, $tileheight)=$this->myimagestringsize($font, "100%");
$box_left = $x; $box_left = $x;
  # $box_left = 0;
$scale_left = $box_left + 4 + $scalefactor/2; $scale_left = $box_left + 4 + $scalefactor/2;
$box_right = $scale_left + $width + $tilewidth + 4 + $scalefactor/2; $box_right = $scale_left + $width + $tilewidth + 4 + $scalefactor/2;
$scale_right = $scale_left + $width; $scale_right = $scale_left + $width;
   
$box_top = $y; $box_top = $y;
  # $box_top = 0;
$scale_top = $box_top + $tileheight + 6; $scale_top = $box_top + $tileheight + 6;
$scale_bottom = $scale_top + $tileheight * 1.5; $scale_bottom = $scale_top + $tileheight * 1.5;
$box_bottom = $scale_bottom + $tileheight * 2 + 6; $box_bottom = $scale_bottom + $tileheight * 2 + 6;
   
imagefilledrectangle($im, $box_left, $box_top, $box_right, $box_bottom, $scale_im = imagecreatetruecolor($box_right+1, $box_bottom+1);
$this->colours['DEFAULT']['KEYBG']['gdref1']); $scale_ref = 'gdref_legend_'.$scalename;
imagerectangle($im, $box_left, $box_top, $box_right, $box_bottom, $this->AllocateScaleColours($scale_im,$scale_ref);
$this->colours['DEFAULT']['KEYTEXT']['gdref1']);  
   
$this->myimagestring($im, $font, $scale_left, $scale_bottom + $tileheight * 2 + 2 , $title, wimagefilledrectangle($scale_im, $box_left, $box_top, $box_right, $box_bottom,
$this->colours['DEFAULT']['KEYTEXT']['gdref1']); $this->colours['DEFAULT']['KEYBG'][$scale_ref]);
  wimagerectangle($scale_im, $box_left, $box_top, $box_right, $box_bottom,
  $this->colours['DEFAULT']['KEYOUTLINE'][$scale_ref]);
   
  $this->myimagestring($scale_im, $font, $scale_left, $scale_bottom + $tileheight * 2 + 2 , $title,
  $this->colours['DEFAULT']['KEYTEXT'][$scale_ref]);
   
for($p=0;$p<=100;$p++) for($p=0;$p<=100;$p++)
{ {
$dx = $p*$scalefactor; $dx = $p*$scalefactor;
   
if( ($p % 25) == 0) if( ($p % 25) == 0)
{ {
imageline($im, $scale_left + $dx, $scale_top - $tileheight, imageline($scale_im, $scale_left + $dx, $scale_top - $tileheight,
$scale_left + $dx, $scale_bottom + $tileheight, $scale_left + $dx, $scale_bottom + $tileheight,
$this->colours['DEFAULT']['KEYTEXT']['gdref1']); $this->colours['DEFAULT']['KEYTEXT'][$scale_ref]);
$labelstring=sprintf("%d%%", $p); $labelstring=sprintf("%d%%", $p);
$this->myimagestring($im, $font, $scale_left + $dx + 2, $scale_top - 2, $labelstring, $this->myimagestring($scale_im, $font, $scale_left + $dx + 2, $scale_top - 2, $labelstring,
$this->colours['DEFAULT']['KEYTEXT']['gdref1']); $this->colours['DEFAULT']['KEYTEXT'][$scale_ref]);
} }
   
list($col,$junk) = $this->ColourFromPercent($p,$scalename); list($col,$junk) = $this->NewColourFromPercent($p,$scalename);
imagefilledrectangle($im, $scale_left + $dx - $scalefactor/2, $scale_top, if($col->is_real())
$scale_left + $dx + $scalefactor/2, $scale_bottom, {
$col); $cc = $col->gdallocate($scale_im);
  wimagefilledrectangle($scale_im, $scale_left + $dx - $scalefactor/2, $scale_top,
  $scale_left + $dx + $scalefactor/2, $scale_bottom,
  $cc);
  }
} }
   
$this->imap->addArea("Rectangle", "LEGEND:$scalename", '', imagecopy($im,$scale_im,$this->keyx[$scalename],$this->keyy[$scalename],0,0,imagesx($scale_im),imagesy($scale_im));
array($box_left, $box_top, $box_right, $box_bottom)); $this->keyimage[$scalename] = $scale_im;
   
  $rx = $this->keyx[$scalename];
  $ry = $this->keyy[$scalename];
   
  $this->imap->addArea("Rectangle", "LEGEND:$scalename", '',
  array($rx+$box_left, $ry+$box_top, $rx+$box_right, $ry+$box_bottom));
} }
   
function DrawLegend_Vertical($im,$scalename="DEFAULT",$height=400) function DrawLegend_Vertical($im,$scalename="DEFAULT",$height=400,$inverted=false)
{ {
$title=$this->keytext[$scalename]; $title=$this->keytext[$scalename];
   
$colours=$this->colours[$scalename]; $colours=$this->colours[$scalename];
$nscales=$this->numscales[$scalename]; $nscales=$this->numscales[$scalename];
Line 3208... Line 1417...
# $height = 400; # $height = 400;
$scalefactor = $height/100; $scalefactor = $height/100;
   
list($tilewidth, $tileheight)=$this->myimagestringsize($font, "100%"); list($tilewidth, $tileheight)=$this->myimagestringsize($font, "100%");
   
$box_left = $x; # $box_left = $x;
$box_top = $y; # $box_top = $y;
  $box_left = 0;
  $box_top = 0;
   
$scale_left = $box_left+$scalefactor*2 +4 ; $scale_left = $box_left+$scalefactor*2 +4 ;
$scale_right = $scale_left + $tileheight*2; $scale_right = $scale_left + $tileheight*2;
$box_right = $scale_right + $tilewidth + $scalefactor*2 + 4; $box_right = $scale_right + $tilewidth + $scalefactor*2 + 4;
   
Line 3225... Line 1436...
   
$scale_top = $box_top + 4 + $scalefactor + $tileheight*2; $scale_top = $box_top + 4 + $scalefactor + $tileheight*2;
$scale_bottom = $scale_top + $height; $scale_bottom = $scale_top + $height;
$box_bottom = $scale_bottom + $scalefactor + $tileheight/2 + 4; $box_bottom = $scale_bottom + $scalefactor + $tileheight/2 + 4;
   
imagefilledrectangle($im, $box_left, $box_top, $box_right, $box_bottom, $scale_im = imagecreatetruecolor($box_right+1, $box_bottom+1);
  $scale_ref = 'gdref_legend_'.$scalename;
  $this->AllocateScaleColours($scale_im,$scale_ref);
   
  wimagefilledrectangle($scale_im, $box_left, $box_top, $box_right, $box_bottom,
$this->colours['DEFAULT']['KEYBG']['gdref1']); $this->colours['DEFAULT']['KEYBG']['gdref1']);
imagerectangle($im, $box_left, $box_top, $box_right, $box_bottom, wimagerectangle($scale_im, $box_left, $box_top, $box_right, $box_bottom,
$this->colours['DEFAULT']['KEYTEXT']['gdref1']); $this->colours['DEFAULT']['KEYOUTLINE']['gdref1']);
   
$this->myimagestring($im, $font, $scale_left-$scalefactor, $scale_top - $tileheight , $title, $this->myimagestring($scale_im, $font, $scale_left-$scalefactor, $scale_top - $tileheight , $title,
$this->colours['DEFAULT']['KEYTEXT']['gdref1']); $this->colours['DEFAULT']['KEYTEXT']['gdref1']);
   
  $updown = 1;
  if($inverted) $updown = -1;
   
   
for($p=0;$p<=100;$p++) for($p=0;$p<=100;$p++)
{ {
$dy = $p*$scalefactor; if($inverted)
$dx = $dy; {
  $dy = (100-$p) * $scalefactor;
  }
  else
  {
  $dy = $p*$scalefactor;
  }
   
if( ($p % 25) == 0) if( ($p % 25) == 0)
{ {
imageline($im, $scale_left - $scalefactor, $scale_top + $dy, imageline($scale_im, $scale_left - $scalefactor, $scale_top + $dy,
$scale_right + $scalefactor, $scale_top + $dy, $scale_right + $scalefactor, $scale_top + $dy,
$this->colours['DEFAULT']['KEYTEXT']['gdref1']); $this->colours['DEFAULT']['KEYTEXT'][$scale_ref]);
$labelstring=sprintf("%d%%", $p); $labelstring=sprintf("%d%%", $p);
$this->myimagestring($im, $font, $scale_right + $scalefactor*2 , $scale_top + $dy + $tileheight/2, $this->myimagestring($scale_im, $font, $scale_right + $scalefactor*2 , $scale_top + $dy + $tileheight/2,
$labelstring, $this->colours['DEFAULT']['KEYTEXT']['gdref1']); $labelstring, $this->colours['DEFAULT']['KEYTEXT'][$scale_ref]);
} }
   
list($col,$junk) = $this->ColourFromPercent($p,$scalename); list($col,$junk) = $this->NewColourFromPercent($p,$scalename);
imagefilledrectangle($im, $scale_left, $scale_top + $dy - $scalefactor/2, if( $col->is_real())
$scale_right, $scale_top + $dy + $scalefactor/2, {
$col); $cc = $col->gdallocate($scale_im);
  wimagefilledrectangle($scale_im, $scale_left, $scale_top + $dy - $scalefactor/2,
  $scale_right, $scale_top + $dy + $scalefactor/2,
  $cc);
  }
} }
   
  imagecopy($im,$scale_im,$this->keyx[$scalename],$this->keyy[$scalename],0,0,imagesx($scale_im),imagesy($scale_im));
  $this->keyimage[$scalename] = $scale_im;
   
  $rx = $this->keyx[$scalename];
  $ry = $this->keyy[$scalename];
$this->imap->addArea("Rectangle", "LEGEND:$scalename", '', $this->imap->addArea("Rectangle", "LEGEND:$scalename", '',
array($box_left, $box_top, $box_right, $box_bottom)); array($rx+$box_left, $ry+$box_top, $rx+$box_right, $ry+$box_bottom));
} }
   
function DrawLegend_Classic($im,$scalename="DEFAULT") function DrawLegend_Classic($im,$scalename="DEFAULT",$use_tags=FALSE)
{ {
$title=$this->keytext[$scalename]; $title=$this->keytext[$scalename];
   
$colours=$this->colours[$scalename]; $colours=$this->colours[$scalename];
  usort($colours, array("Weathermap", "coloursort"));
   
$nscales=$this->numscales[$scalename]; $nscales=$this->numscales[$scalename];
   
debug("Drawing $nscales colours into SCALE\n"); debug("Drawing $nscales colours into SCALE\n");
   
$hide_zero = intval($this->get_hint("key_hidezero_".$scalename)); $hide_zero = intval($this->get_hint("key_hidezero_".$scalename));
$hide_percent = intval($this->get_hint("key_hidepercent_".$scalename)); $hide_percent = intval($this->get_hint("key_hidepercent_".$scalename));
   
  // did we actually hide anything?
  $hid_zero = FALSE;
if( ($hide_zero == 1) && isset($colours['0_0']) ) if( ($hide_zero == 1) && isset($colours['0_0']) )
{ {
$nscales--; $nscales--;
  $hid_zero = TRUE;
} }
   
$font=$this->keyfont; $font=$this->keyfont;
   
$x=$this->keyx[$scalename]; $x=$this->keyx[$scalename];
Line 3284... Line 1523...
   
list($tilewidth, $tileheight)=$this->myimagestringsize($font, "MMMM"); list($tilewidth, $tileheight)=$this->myimagestringsize($font, "MMMM");
$tileheight=$tileheight * 1.1; $tileheight=$tileheight * 1.1;
$tilespacing=$tileheight + 2; $tilespacing=$tileheight + 2;
   
if (($x >= 0) && ($y >= 0)) if (($this->keyx[$scalename] >= 0) && ($this->keyy[$scalename] >= 0))
{ {
   
# $minwidth = imagefontwidth($font) * strlen('XX 100%-100%')+10; # $minwidth = imagefontwidth($font) * strlen('XX 100%-100%')+10;
# $boxwidth = imagefontwidth($font) * strlen($title) + 10; # $boxwidth = imagefontwidth($font) * strlen($title) + 10;
list($minwidth, $junk)=$this->myimagestringsize($font, 'MMMM 100%-100%'); list($minwidth, $junk)=$this->myimagestringsize($font, 'MMMM 100%-100%');
  list($minminwidth, $junk)=$this->myimagestringsize($font, 'MMMM ');
list($boxwidth, $junk)=$this->myimagestringsize($font, $title); list($boxwidth, $junk)=$this->myimagestringsize($font, $title);
   
  if($use_tags)
  {
  $max_tag = 0;
  foreach ($colours as $colour)
  {
  if ( isset($colour['tag']) )
  {
  list($w, $junk)=$this->myimagestringsize($font, $colour['tag']);
  # print $colour['tag']." $w \n";
  if($w > $max_tag) $max_tag = $w;
  }
  }
   
  // now we can tweak the widths, appropriately to allow for the tag strings
  # print "$max_tag > $minwidth?\n";
  if( ($max_tag + $minminwidth) > $minwidth) $minwidth = $minminwidth + $max_tag;
  # print "minwidth is now $minwidth\n";
  }
   
$minwidth+=10; $minwidth+=10;
$boxwidth+=10; $boxwidth+=10;
   
if ($boxwidth < $minwidth) { $boxwidth=$minwidth; } if ($boxwidth < $minwidth) { $boxwidth=$minwidth; }
   
$boxheight=$tilespacing * ($nscales + 1) + 10; $boxheight=$tilespacing * ($nscales + 1) + 10;
   
$boxx=$x; $boxx=$x; $boxy=$y;
$boxy=$y; $boxx=0;
  $boxy=0;
   
// allow for X11-style negative positioning // allow for X11-style negative positioning
if ($boxx < 0) { $boxx+=$this->width; } if ($boxx < 0) { $boxx+=$this->width; }
   
if ($boxy < 0) { $boxy+=$this->height; } if ($boxy < 0) { $boxy+=$this->height; }
   
imagefilledrectangle($im, $boxx, $boxy, $boxx + $boxwidth, $boxy + $boxheight, $scale_im = imagecreatetruecolor($boxwidth+1, $boxheight+1);
$this->colours['DEFAULT']['KEYBG']['gdref1']); $scale_ref = 'gdref_legend_'.$scalename;
imagerectangle($im, $boxx, $boxy, $boxx + $boxwidth, $boxy + $boxheight, $this->AllocateScaleColours($scale_im,$scale_ref);
$this->colours['DEFAULT']['KEYTEXT']['gdref1']);  
$this->myimagestring($im, $font, $boxx + 4, $boxy + 4 + $tileheight, $title,  
$this->colours['DEFAULT']['KEYTEXT']['gdref1']);  
   
usort($colours, array("Weathermap", "coloursort")); wimagefilledrectangle($scale_im, $boxx, $boxy, $boxx + $boxwidth, $boxy + $boxheight,
  $this->colours['DEFAULT']['KEYBG'][$scale_ref]);
  wimagerectangle($scale_im, $boxx, $boxy, $boxx + $boxwidth, $boxy + $boxheight,
  $this->colours['DEFAULT']['KEYOUTLINE'][$scale_ref]);
  $this->myimagestring($scale_im, $font, $boxx + 4, $boxy + 4 + $tileheight, $title,
  $this->colours['DEFAULT']['KEYTEXT'][$scale_ref]);
   
$i=1; $i=1;
   
foreach ($colours as $colour) foreach ($colours as $colour)
{ {
if ($colour['bottom'] >= 0) if (!isset($colour['special']) || $colour['special'] == 0)
{ // if ( 1==1 || $colour['bottom'] >= 0)
  {
  // pick a value in the middle...
  $value = ($colour['bottom'] + $colour['top']) / 2;
  debug(sprintf("%f-%f (%f) %d %d %d\n", $colour['bottom'], $colour['top'], $value, $colour['red1'], $colour['green1'], $colour['blue1']));
   
# debug("$i: drawing\n"); # debug("$i: drawing\n");
if( ($hide_zero == 0) || $colour['key'] != '0_0') if( ($hide_zero == 0) || $colour['key'] != '0_0')
{ {
$y=$boxy + $tilespacing * $i + 8; $y=$boxy + $tilespacing * $i + 8;
$x=$boxx + 6; $x=$boxx + 6;
   
  $fudgefactor = 0;
  if( $hid_zero && $colour['bottom']==0 )
  {
  // calculate a small offset that can be added, which will hide the zero-value in a
  // gradient, but not make the scale incorrect. A quarter of a pixel should do it.
  $fudgefactor = ($colour['top'] - $colour['bottom'])/($tilewidth*4);
  # warn("FUDGING $fudgefactor\n");
  }
   
  // if it's a gradient, red2 is defined, and we need to sweep the values
if (isset($colour['red2'])) if (isset($colour['red2']))
{ {
for ($n=0; $n <= $tilewidth; $n++) for ($n=0; $n <= $tilewidth; $n++)
{ {
$percent $value
=$colour['bottom'] + ($n / $tilewidth) * ($colour['top'] - $colour['bottom']); = $fudgefactor + $colour['bottom'] + ($n / $tilewidth) * ($colour['top'] - $colour['bottom']);
list($col,$junk) = $this->ColourFromPercent($percent,$scalename); list($ccol,$junk) = $this->NewColourFromPercent($value, $scalename, "", FALSE);
imagefilledrectangle($im, $x + $n, $y, $x + $n, $y + $tileheight, $col = $ccol->gdallocate($scale_im);
  wimagefilledrectangle($scale_im, $x + $n, $y, $x + $n, $y + $tileheight,
$col); $col);
} }
} }
else else
{ {
// pick a percentage in the middle... // pick a value in the middle...
$percent=($colour['bottom'] + $colour['top']) / 2; //$value = ($colour['bottom'] + $colour['top']) / 2;
list($col,$junk) = $this->ColourFromPercent($percent,$scalename); list($ccol,$junk) = $this->NewColourFromPercent($value, $scalename, "", FALSE);
imagefilledrectangle($im, $x, $y, $x + $tilewidth, $y + $tileheight, $col = $ccol->gdallocate($scale_im);
  wimagefilledrectangle($scale_im, $x, $y, $x + $tilewidth, $y + $tileheight,
$col); $col);
} }
   
$labelstring=sprintf("%s-%s", $colour['bottom'], $colour['top']); if($use_tags)
if($hide_percent==0) { $labelstring.="%"; } {
$this->myimagestring($im, $font, $x + 4 + $tilewidth, $y + $tileheight, $labelstring, $labelstring = "";
$this->colours['DEFAULT']['KEYTEXT']['gdref1']); if(isset($colour['tag'])) $labelstring = $colour['tag'];
  }
  else
  {
  $labelstring=sprintf("%s-%s", $colour['bottom'], $colour['top']);
  if($hide_percent==0) { $labelstring.="%"; }
  }
   
  $this->myimagestring($scale_im, $font, $x + 4 + $tilewidth, $y + $tileheight, $labelstring,
  $this->colours['DEFAULT']['KEYTEXT'][$scale_ref]);
$i++; $i++;
} }
  imagecopy($im,$scale_im,$this->keyx[$scalename],$this->keyy[$scalename],0,0,imagesx($scale_im),imagesy($scale_im));
  $this->keyimage[$scalename] = $scale_im;
   
} }
} }
   
$this->imap->addArea("Rectangle", "LEGEND:$scalename", '', $this->imap->addArea("Rectangle", "LEGEND:$scalename", '',
array($boxx, $boxy, $boxx + $boxwidth, $boxy + $boxheight)); array($this->keyx[$scalename], $this->keyy[$scalename], $this->keyx[$scalename] + $boxwidth, $this->keyy[$scalename] + $boxheight));
# $this->imap->setProp("href","#","LEGEND"); # $this->imap->setProp("href","#","LEGEND");
# $this->imap->setProp("extrahtml","onclick=\"position_legend();\"","LEGEND"); # $this->imap->setProp("extrahtml","onclick=\"position_legend();\"","LEGEND");
   
} }
} }
   
function DrawTimestamp($im, $font, $colour) function DrawTimestamp($im, $font, $colour, $which="")
{ {
// add a timestamp to the corner, so we can tell if it's all being updated // add a timestamp to the corner, so we can tell if it's all being updated
# $datestring = "Created: ".date("M d Y H:i:s",time()); # $datestring = "Created: ".date("M d Y H:i:s",time());
# $this->datestamp=strftime($this->stamptext, time()); # $this->datestamp=strftime($this->stamptext, time());
   
list($boxwidth, $boxheight)=$this->myimagestringsize($font, $this->datestamp); switch($which)
  {
  case "MIN":
  $stamp = strftime($this->minstamptext, $this->min_data_time);
  $pos_x = $this->mintimex;
  $pos_y = $this->mintimey;
  break;
  case "MAX":
  $stamp = strftime($this->maxstamptext, $this->max_data_time);
  $pos_x = $this->maxtimex;
  $pos_y = $this->maxtimey;
  break;
  default:
  $stamp = $this->datestamp;
  $pos_x = $this->timex;
  $pos_y = $this->timey;
  break;
  }
   
  list($boxwidth, $boxheight)=$this->myimagestringsize($font, $stamp);
   
$x=$this->width - $boxwidth; $x=$this->width - $boxwidth;
$y=$boxheight; $y=$boxheight;
   
if (($this->timex > 0) && ($this->timey > 0)) if (($pos_x != 0) && ($pos_y != 0))
{ {
$x=$this->timex; $x = $pos_x;
$y=$this->timey; $y = $pos_y;
} }
   
$this->myimagestring($im, $font, $x, $y, $this->datestamp, $colour); $this->myimagestring($im, $font, $x, $y, $stamp, $colour);
  $this->imap->addArea("Rectangle", $which."TIMESTAMP", '', array($x, $y, $x + $boxwidth, $y - $boxheight));
$this->imap->addArea("Rectangle", "TIMESTAMP", '', array($x, $y, $x + $boxwidth, $y - $boxheight));  
} }
   
function DrawTitle($im, $font, $colour) function DrawTitle($im, $font, $colour)
{ {
$string = $this->ProcessString($this->title,$this); $string = $this->ProcessString($this->title,$this);
   
  if($this->get_hint('screenshot_mode')==1) $string= screenshotify($string);
   
list($boxwidth, $boxheight)=$this->myimagestringsize($font, $string); list($boxwidth, $boxheight)=$this->myimagestringsize($font, $string);
   
$x=10; $x=10;
$y=$this->titley - $boxheight; $y=$this->titley - $boxheight;
Line 3407... Line 1718...
$this->myimagestring($im, $font, $x, $y, $string, $colour); $this->myimagestring($im, $font, $x, $y, $string, $colour);
   
$this->imap->addArea("Rectangle", "TITLE", '', array($x, $y, $x + $boxwidth, $y - $boxheight)); $this->imap->addArea("Rectangle", "TITLE", '', array($x, $y, $x + $boxwidth, $y - $boxheight));
} }
   
function ReadConfig($filename) function ReadConfigNG($input, $is_include=FALSE, $initial_context="GLOBAL")
  {
  $valid_commands = array(
  "GLOBAL.set", "LINK.set", "NODE.set",
  "GLOBAL.#","LINK.#","NODE.#",
  "GLOBAL.include", "NODE.include", "LINK.include",
   
  "GLOBAL.width", "GLOBAL.height", "GLOBAL.background",
  "GLOBAL.scale", "GLOBAL.title", "GLOBAL.titlepos",
  "GLOBAL.fontdefine",
  "GLOBAL.keystyle", "GLOBAL.titlecolor", "GLOBAL.timecolor",
  "GLOBAL.titlefont", "GLOBAL.timefont", "GLOBAL.htmloutputfile",
  "GLOBAL.htmlstyle", "GLOBAL.imageoutputfile", "GLOBAL.keyfont",
  "GLOBAL.keytextcolor", "GLOBAL.keyoutlinecolor", "GLOBAL.keybgcolor",
  "GLOBAL.bgcolor",
   
  "SCALE.keypos", "SCALE.keystyle", "SCALE.scale",
   
  "LINK.width", "LINK.link", "LINK.nodes",
  "LINK.target", "LINK.usescale", "LINK.infourl",
  "LINK.linkstyle", "LINK.overlibcaption", "LINK.inoverlibcaption", "LINK.outoverlibcaption",
  "LINK.inoverlibgraph", "LINK.outoverlibgraph",
  "LINK.overlibgraph", "LINK.overlibwidth", "LINK.overlibheight",
  "LINK.bwlabel", "LINK.via", "LINK.zorder", "LINK.outlinecolor",
  "LINK.notes", "LINK.innotes", "LINK.outnotes","LINK.ininfourl", "LINK.outinfourl",
  "LINK.bwstyle", "LINK.template", "LINK.splitpos", "LINK.bwlabelpos", "LINK.incomment", "LINK.outcomment",
  "LINK.viastyle", "LINK.bandwidth", "LINK.inbwformat", "LINK.outbwformat",
  "LINK.commentstyle", "LINK.commentfont", "LINK.commentfontcolor", "LINK.bwfont",
   
  "NODE.icon", "NODE.target", "NODE.position", "NODE.infourl", "NODE.overlibgraph",
  "NODE.zorder", "NODE.label", "NODE.template", "NODE.labelbgcolor",
  "NODE.maxvalue",
  "NODE.labeloutlinecolor", "NODE.aiconoutlinecolor", "NODE.aiconfillcolor", "NODE.usescale",
  "NODE.labelfontcolor", "NODE.labelfont", "NODE.labelangle", "NODE.labelfontshadowcolor",
  "NODE.node", "NODE.overlibwidth", "NODE.overlibheight", "NODE.labeloffset"
  );
   
   
  if( (strchr($input,"\n")!=FALSE) || (strchr($input,"\r")!=FALSE ) )
  {
  debug("ReadConfig Detected that this is a config fragment.\n");
  // strip out any Windows line-endings that have gotten in here
  $input=str_replace("\r", "", $input);
  $lines = split("/n",$input);
  $filename = "{text insert}";
  }
  else
  {
  debug("ReadConfig Detected that this is a config filename.\n");
  $filename = $input;
   
  $fd=fopen($filename, "r");
   
  if ($fd)
  {
  while (!feof($fd))
  {
  $buffer=fgets($fd, 4096);
  // strip out any Windows line-endings that have gotten in here
  $buffer=str_replace("\r", "", $buffer);
  $lines[] = $buffer;
  }
  fclose($fd);
  }
  }
   
  $linecount = 0;
  $context = $initial_context;
   
  foreach($lines as $buffer)
  {
  $linematched=0;
  $linecount++;
  $nextcontext = "";
  $key = "";
   
  $buffer = trim($buffer);
  // alternative for use later where quoted strings are more useful
  $args = ParseString($buffer);
   
  if(sizeof($args) > 0)
  {
  $linematched++;
  $cmd = strtolower(array_shift($args));
   
  if($cmd == 'include')
  {
  $this->ReadConfigNG($args[0],TRUE, $context);
  }
  elseif($cmd == 'node')
  {
  $context = "NODE.".$args[0];
  }
  elseif($cmd == 'link')
  {
  $context = "LINK.".$args[0];
  $vcount = 0; # reset the via-number counter, it's a new link
  }
  elseif($cmd == 'scale' || $cmd == 'keystyle' || $cmd == 'keypos')
  {
  if( preg_match("/^[0-9\-]+/i",$args[0]) )
  {
  $scalename = "DEFAULT";
  }
  else
  {
  $scalename = array_shift($args);
  }
  if($cmd=="scale") $key = $args[0]."_".$args[1];
  $nextcontext = $context;
  $context = "SCALE.".$scalename;
  }
   
  array_unshift($args,$cmd);
   
  if($context == 'GLOBAL')
  {
  $ctype='GLOBAL';
  }
  else
  {
  list($ctype,$junk) = split("\\.", $context, 2);
  }
   
  $lookup = $ctype.".".$cmd;
   
  // Some things (scales, mainly) might define special keys
  // the key should be unique for that object
  // most (all?) things for a link or node are one-offs.
  if($key == "") $key = $cmd;
  if($cmd == 'set' || $cmd == 'fontdefine') $key .= "_".$args[1];
  if($cmd == 'via')
  {
  $key .= "_".$vcount;
  $vcount++;
  }
   
  # everything else
  if( substr($cmd, 0, 1) != '#')
  {
  if(! in_array($lookup, $valid_commands))
  {
  print "INVALID COMMAND: $lookup\n";
  }
   
  if(isset($config[$context][$key]))
  {
  print "REDEFINED $key in $context\n";
  }
  else
  {
  array_unshift($args,$linecount);
  array_unshift($args,$filename);
  $this->config[$context][$key] = $args;
  }
  }
  print "$context\\$key $filename:$linecount ".join("|",$args)."\n";
   
  if($nextcontext != "") $context = $nextcontext;
  }
   
  if ($linematched == 0 && trim($buffer) != '') { warn ("Unrecognised config on line $linecount: $buffer\n"); }
   
  }
   
  if(! $is_include)
  {
  print_r($this->config);
   
  foreach ($this->config as $context=>$values)
  {
  print "> $context\n";
  }
  }
   
   
  }
   
  function ReadConfigNNG($input, $is_include=FALSE, $initial_context="GLOBAL")
  {
  global $valid_commands;
   
  if( (strchr($input,"\n")!=FALSE) || (strchr($input,"\r")!=FALSE ) )
  {
  debug("ReadConfig Detected that this is a config fragment.\n");
  // strip out any Windows line-endings that have gotten in here
  $input=str_replace("\r", "", $input);
  $lines = split("/n",$input);
  $filename = "{text insert}";
  }
  else
  {
  debug("ReadConfig Detected that this is a config filename.\n");
  $filename = $input;
   
  $fd=fopen($filename, "r");
   
  if ($fd)
  {
  while (!feof($fd))
  {
  $buffer=fgets($fd, 4096);
  // strip out any Windows line-endings that have gotten in here
  $buffer=str_replace("\r", "", $buffer);
  $lines[] = $buffer;
  }
  fclose($fd);
  }
  }
   
  $linecount = 0;
  $context = $initial_context;
   
  foreach($lines as $buffer)
  {
  $linematched=0;
  $linecount++;
  $nextcontext = "";
  $key = "";
   
  $buffer = trim($buffer);
  // alternative for use later where quoted strings are more useful
  $args = ParseString($buffer);
   
  if(sizeof($args) > 0)
  {
  $linematched++;
  $cmd = strtolower(array_shift($args));
   
  if($cmd == 'include')
  {
  $context = $this->ReadConfigNNG($args[0],TRUE, $context);
  }
  elseif($cmd == 'node')
  {
  $context = "NODE.".$args[0];
  }
  elseif($cmd == 'link')
  {
  $context = "LINK.".$args[0];
  $vcount = 0; # reset the via-number counter, it's a new link
  }
  elseif($cmd == 'scale' || $cmd == 'keystyle' || $cmd == 'keypos')
  {
  if( preg_match("/^[0-9\-]+/i",$args[0]) )
  {
  $scalename = "DEFAULT";
  }
  else
  {
  $scalename = array_shift($args);
  }
  if($cmd=="scale") $key = $args[0]."_".$args[1];
  $nextcontext = $context;
  $context = "SCALE.".$scalename;
  }
   
  array_unshift($args,$cmd);
   
  if($context == 'GLOBAL')
  {
  $ctype='GLOBAL';
  }
  else
  {
  list($ctype,$junk) = split("\\.", $context, 2);
  }
   
  $lookup = $ctype.".".$cmd;
   
  // Some things (scales, mainly) might define special keys
  // the key should be unique for that object
  // most (all?) things for a link or node are one-offs.
  if($key == "") $key = $cmd;
  if($cmd == 'set' || $cmd == 'fontdefine') $key .= "_".$args[1];
  if($cmd == 'via')
  {
  $key .= "_".$vcount;
  $vcount++;
  }
   
  # everything else
  if( substr($cmd, 0, 1) != '#')
  {
  if(! array_key_exists($lookup, $valid_commands))
  {
  print "INVALID COMMAND: $lookup\n";
  }
   
  if(isset($config[$context][$key]))
  {
  print "REDEFINED $key in $context\n";
  }
  else
  {
  array_unshift($args,$linecount);
  array_unshift($args,$filename);
  $this->config[$context][$key] = $args;
  }
  }
  print "$context\\$key $filename:$linecount ".join("|",$args)."\n";
   
  if($nextcontext != "") $context = $nextcontext;
  }
   
  if ($linematched == 0 && trim($buffer) != '') { warn ("Unrecognised config on line $linecount: $buffer\n"); }
   
  }
   
  if(! $is_include)
  {
   
  # print_r($this->config);
   
  foreach ($this->config as $context=>$values)
  {
  # print "> $context\n";
  }
  }
   
  return($context);
  }
   
   
  function WriteConfigNG($filename)
  {
  global $WEATHERMAP_VERSION;
   
  $fd = fopen($filename);
   
   
   
  fclose($fd);
  }
   
  function ReadConfig($input, $is_include=FALSE)
{ {
$curnode=null; $curnode=null;
$curlink=null; $curlink=null;
$matches=0; $matches=0;
$nodesseen=0; $nodesseen=0;
$linksseen=0; $linksseen=0;
$scalesseen=0; $scalesseen=0;
  $last_seen="GLOBAL";
  $filename = "";
  $objectlinecount=0;
   
$last_seen="---"; // check if $input is more than one line. if it is, it's a text of a config file
$fd=fopen($filename, "r"); // if it isn't, it's the filename
   
if ($fd) $lines = array();
   
  if( (strchr($input,"\n")!=FALSE) || (strchr($input,"\r")!=FALSE ) )
{ {
$linecount = 0; debug("ReadConfig Detected that this is a config fragment.\n");
  // strip out any Windows line-endings that have gotten in here
while (!feof($fd)) $input=str_replace("\r", "", $input);
{ $lines = split("/n",$input);
$buffer=fgets($fd, 4096); $filename = "{text insert}";
// strip out any Windows line-endings that have gotten in here }
$buffer=str_replace("\r", "", $buffer); else
$linematched=0; {
$linecount++; debug("ReadConfig Detected that this is a config filename.\n");
  $filename = $input;
if (preg_match("/^\s*#/", $buffer)) {  
// this is a comment line if($is_include){
  debug("ReadConfig Detected that this is an INCLUDED config filename.\n");
  if($is_include && in_array($filename, $this->included_files))
  {
  warn("Attempt to include '$filename' twice! Skipping it.\n");
  return(FALSE);
} }
else else
{ {
// for any other config elements that are shared between nodes and links, they can use this $this->included_files[] = $filename;
unset($curobj); $this->has_includes = TRUE;
$curobj = NULL; }
if($last_seen == "LINK") $curobj = &$curlink; }
if($last_seen == "NODE") $curobj = &$curnode;  
  $fd=fopen($filename, "r");
   
  if ($fd)
  {
  while (!feof($fd))
  {
  $buffer=fgets($fd, 4096);
  // strip out any Windows line-endings that have gotten in here
  $buffer=str_replace("\r", "", $buffer);
  $lines[] = $buffer;
  }
  fclose($fd);
  }
  }
   
  $linecount = 0;
  $objectlinecount = 0;
   
if (preg_match("/^\s*(LINK|NODE)\s+(\S+)\s*$/i", $buffer, $matches)) foreach($lines as $buffer)
  {
  $linematched=0;
  $linecount++;
   
  if (preg_match("/^\s*#/", $buffer)) {
  // this is a comment line
  }
  else
  {
  $buffer = trim($buffer);
   
  // for any other config elements that are shared between nodes and links, they can use this
  unset($curobj);
  $curobj = NULL;
  if($last_seen == "LINK") $curobj = &$curlink;
  if($last_seen == "NODE") $curobj = &$curnode;
  if($last_seen == "GLOBAL") $curobj = &$this;
   
  $objectlinecount++;
   
  #if (preg_match("/^\s*(LINK|NODE)\s+([A-Za-z][A-Za-z0-9_\.\-\:]*)\s*$/i", $buffer, $matches))
  if (preg_match("/^\s*(LINK|NODE)\s+(\S+)\s*$/i", $buffer, $matches))
  {
  $objectlinecount = 0;
  if(1==1)
  {
  $this->ReadConfig_Commit($curobj);
  }
  else
{ {
// first, save the previous item, before starting work on the new one // first, save the previous item, before starting work on the new one
if ($last_seen == "NODE") if ($last_seen == "NODE")
{ {
if ($curnode->name == 'DEFAULT') $this->nodes[$curnode->name]=$curnode;
{ if($curnode->template == 'DEFAULT') $this->node_template_tree[ "DEFAULT" ][]= $curnode->name;
$this->defaultnode = $curnode;  
debug ("Saving Default Node: " . $curnode->name . "\n"); debug ("Saving Node: " . $curnode->name . "\n");
}  
else  
{  
$this->nodes[$curnode->name]=$curnode;  
debug ("Saving Node: " . $curnode->name . "\n");  
}  
} }
   
if ($last_seen == "LINK") if ($last_seen == "LINK")
{ {
if ($curlink->name == 'DEFAULT') if (isset($curlink->a) && isset($curlink->b))
{ {
$this->defaultlink=$curlink; $this->links[$curlink->name]=$curlink;
debug ("Saving Default Link: " . $curlink->name . "\n"); debug ("Saving Link: " . $curlink->name . "\n");
} }
else else
{ {
if (isset($curlink->a) && isset($curlink->b)) $this->links[$curlink->name]=$curlink;
{ debug ("Saving Template-Only Link: " . $curlink->name . "\n");
$this->links[$curlink->name]=$curlink;  
debug ("Saving Link: " . $curlink->name . "\n");  
}  
else { warn  
("Dropping LINK " . $curlink->name . " - it hasn't got 2 NODES!\n"); }  
} }
  if($curlink->template == 'DEFAULT') $this->link_template_tree[ "DEFAULT" ][]= $curlink->name;
} }
  }
   
if ($matches[1] == 'LINK') if ($matches[1] == 'LINK')
  {
  if ($matches[2] == 'DEFAULT')
{ {
if ($matches[2] == 'DEFAULT') if ($linksseen > 0) { warn
{ ("LINK DEFAULT is not the first LINK. Defaults will not apply to earlier LINKs. [WMWARN26]\n");
if ($linksseen > 0) { warn  
("LINK DEFAULT is not the first LINK. Defaults will not apply to earlier LINKs.\n");  
}  
unset($curlink);  
$curlink = $this->defaultlink;  
} }
else unset($curlink);
  debug("Loaded LINK DEFAULT\n");
  $curlink = $this->links['DEFAULT'];
  }
  else
  {
  unset($curlink);
   
  if(isset($this->links[$matches[2]]))
{ {
unset($curlink); warn("Duplicate link name ".$matches[2]." at line $linecount - only the last one defined is used. [WMWARN25]\n");
$curlink=new WeatherMapLink;  
$curlink->name=$matches[2];  
$curlink->Reset($this);  
$linksseen++;  
} }
   
$last_seen="LINK"; debug("New LINK ".$matches[2]."\n");
$curlink->configline = $linecount; $curlink=new WeatherMapLink;
$linematched++; $curlink->name=$matches[2];
  $curlink->Reset($this);
   
  $linksseen++;
} }
   
if ($matches[1] == 'NODE') $last_seen="LINK";
{ $curlink->configline = $linecount;
if ($matches[2] == 'DEFAULT') $linematched++;
{ $curobj = &$curlink;
if ($nodesseen > 0) { warn }
("NODE DEFAULT is not the first NODE. Defaults will not apply to earlier NODEs.\n");  
}  
   
unset($curnode); if ($matches[1] == 'NODE')
$curnode = $this->defaultnode; {
  if ($matches[2] == 'DEFAULT')
  {
  if ($nodesseen > 0) { warn
  ("NODE DEFAULT is not the first NODE. Defaults will not apply to earlier NODEs. [WMWARN27]\n");
} }
else  
  unset($curnode);
  debug("Loaded NODE DEFAULT\n");
  $curnode = $this->nodes['DEFAULT'];
  }
  else
  {
  unset($curnode);
   
  if(isset($this->nodes[$matches[2]]))
{ {
unset($curnode); warn("Duplicate node name ".$matches[2]." at line $linecount - only the last one defined is used. [WMWARN24]\n");
$curnode=new WeatherMapNode;  
$curnode->name=$matches[2];  
$curnode->Reset($this);  
$nodesseen++;  
} }
   
$curnode->configline = $linecount; $curnode=new WeatherMapNode;
$last_seen="NODE"; $curnode->name=$matches[2];
$linematched++; $curnode->Reset($this);
   
  $nodesseen++;
} }
   
  $curnode->configline = $linecount;
  $last_seen="NODE";
  $linematched++;
  $curobj = &$curnode;
} }
   
  # record where we first heard about this object
  $curobj->defined_in = $filename;
  }
   
  // most of the config keywords just copy stuff into object properties.
  // these are all dealt with from this one array. The special-cases
  // follow on from that
  $config_keywords = array(
  array('LINK','/^\s*(MAXVALUE|BANDWIDTH)\s+(\d+\.?\d*[KMGT]?)\s+(\d+\.?\d*[KMGT]?)\s*$/i',array('max_bandwidth_in_cfg'=>2,'max_bandwidth_out_cfg'=>3)),
  array('LINK','/^\s*(MAXVALUE|BANDWIDTH)\s+(\d+\.?\d*[KMGT]?)\s*$/i',array('max_bandwidth_in_cfg'=>2,'max_bandwidth_out_cfg'=>2)),
  array('NODE','/^\s*(MAXVALUE)\s+(\d+\.?\d*[KMGT]?)\s+(\d+\.?\d*[KMGT]?)\s*$/i',array('max_bandwidth_in_cfg'=>2,'max_bandwidth_out_cfg'=>3)),
  array('NODE','/^\s*(MAXVALUE)\s+(\d+\.?\d*[KMGT]?)\s*$/i',array('max_bandwidth_in_cfg'=>2,'max_bandwidth_out_cfg'=>2)),
  array('GLOBAL','/^\s*BACKGROUND\s+(.*)\s*$/i',array('background'=>1)),
  array('GLOBAL','/^\s*HTMLOUTPUTFILE\s+(.*)\s*$/i',array('htmloutputfile'=>1)),
  array('GLOBAL','/^\s*HTMLSTYLESHEET\s+(.*)\s*$/i',array('htmlstylesheet'=>1)),
  array('GLOBAL','/^\s*IMAGEOUTPUTFILE\s+(.*)\s*$/i',array('imageoutputfile'=>1)),
  array('GLOBAL','/^\s*IMAGEURI\s+(.*)\s*$/i',array('imageuri'=>1)),
  array('GLOBAL','/^\s*TITLE\s+(.*)\s*$/i',array('title'=>1)),
  array('GLOBAL','/^\s*HTMLSTYLE\s+(static|overlib)\s*$/i',array('htmlstyle'=>1)),
  array('GLOBAL','/^\s*KEYFONT\s+(\d+)\s*$/i',array('keyfont'=>1)),
  array('GLOBAL','/^\s*TITLEFONT\s+(\d+)\s*$/i',array('titlefont'=>1)),
  array('GLOBAL','/^\s*TIMEFONT\s+(\d+)\s*$/i',array('timefont'=>1)),
  array('GLOBAL','/^\s*TITLEPOS\s+(-?\d+)\s+(-?\d+)\s*$/i',array('titlex'=>1, 'titley'=>2)),
  array('GLOBAL','/^\s*TITLEPOS\s+(-?\d+)\s+(-?\d+)\s+(.*)\s*$/i',array('titlex'=>1, 'titley'=>2, 'title'=>3)),
  array('GLOBAL','/^\s*TIMEPOS\s+(-?\d+)\s+(-?\d+)\s*$/i',array('timex'=>1, 'timey'=>2)),
  array('GLOBAL','/^\s*TIMEPOS\s+(-?\d+)\s+(-?\d+)\s+(.*)\s*$/i',array('timex'=>1, 'timey'=>2, 'stamptext'=>3)),
  array('GLOBAL','/^\s*MINTIMEPOS\s+(-?\d+)\s+(-?\d+)\s*$/i',array('mintimex'=>1, 'mintimey'=>2)),
  array('GLOBAL','/^\s*MINTIMEPOS\s+(-?\d+)\s+(-?\d+)\s+(.*)\s*$/i',array('mintimex'=>1, 'mintimey'=>2, 'minstamptext'=>3)),
  array('GLOBAL','/^\s*MAXTIMEPOS\s+(-?\d+)\s+(-?\d+)\s*$/i',array('maxtimex'=>1, 'maxtimey'=>2)),
  array('GLOBAL','/^\s*MAXTIMEPOS\s+(-?\d+)\s+(-?\d+)\s+(.*)\s*$/i',array('maxtimex'=>1, 'maxtimey'=>2, 'maxstamptext'=>3)),
  array('NODE', "/^\s*LABEL\s*$/i", array('label'=>'')), # special case for blank labels
  array('NODE', "/^\s*LABEL\s+(.*)\s*$/i", array('label'=>1)),
  array('(LINK|GLOBAL)', "/^\s*WIDTH\s+(\d+)\s*$/i", array('width'=>1)),
  array('(LINK|GLOBAL)', "/^\s*HEIGHT\s+(\d+)\s*$/i", array('height'=>1)),
  array('LINK', "/^\s*WIDTH\s+(\d+\.\d+)\s*$/i", array('width'=>1)),
  array('LINK', '/^\s*ARROWSTYLE\s+(classic|compact)\s*$/i', array('arrowstyle'=>1)),
  array('LINK', '/^\s*VIASTYLE\s+(curved|angled)\s*$/i', array('viastyle'=>1)),
  array('LINK', '/^\s*INCOMMENT\s+(.*)\s*$/i', array('comments[IN]'=>1)),
  array('LINK', '/^\s*OUTCOMMENT\s+(.*)\s*$/i', array('comments[OUT]'=>1)),
  array('LINK', '/^\s*BWFONT\s+(\d+)\s*$/i', array('bwfont'=>1)),
  array('LINK', '/^\s*COMMENTFONT\s+(\d+)\s*$/i', array('commentfont'=>1)),
  array('LINK', '/^\s*COMMENTSTYLE\s+(edge|center)\s*$/i', array('commentstyle'=>1)),
  array('LINK', '/^\s*DUPLEX\s+(full|half)\s*$/i', array('duplex'=>1)),
  array('LINK', '/^\s*BWSTYLE\s+(classic|angled)\s*$/i', array('labelboxstyle'=>1)),
  array('LINK', '/^\s*LINKSTYLE\s+(twoway|oneway)\s*$/i', array('linkstyle'=>1)),
  array('LINK', '/^\s*BWLABELPOS\s+(\d+)\s(\d+)\s*$/i', array('labeloffset_in'=>1,'labeloffset_out'=>2)),
  array('LINK', '/^\s*COMMENTPOS\s+(\d+)\s(\d+)\s*$/i', array('commentoffset_in'=>1, 'commentoffset_out'=>2)),
  array('LINK', '/^\s*USESCALE\s+([A-Za-z][A-Za-z0-9_]*)\s*$/i', array('usescale'=>1)),
  array('LINK', '/^\s*USESCALE\s+([A-Za-z][A-Za-z0-9_]*)\s+(absolute|percent)\s*$/i', array('usescale'=>1,'scaletype'=>2)),
   
  array('LINK', '/^\s*SPLITPOS\s+(\d+)\s*$/i', array('splitpos'=>1)),
   
  array('NODE', '/^\s*LABELOFFSET\s+([-+]?\d+)\s+([-+]?\d+)\s*$/i', array('labeloffsetx'=>1,'labeloffsety'=>2)),
  array('NODE', '/^\s*LABELOFFSET\s+(C|NE|SE|NW|SW|N|S|E|W)\s*$/i', array('labeloffset'=>1)),
  array('NODE', '/^\s*LABELOFFSET\s+((C|NE|SE|NW|SW|N|S|E|W)\d+)\s*$/i', array('labeloffset'=>1)),
  array('NODE', '/^\s*LABELOFFSET\s+(-?\d+r\d+)\s*$/i', array('labeloffset'=>1)),
   
  array('NODE', '/^\s*LABELFONT\s+(\d+)\s*$/i', array('labelfont'=>1)),
  array('NODE', '/^\s*LABELANGLE\s+(0|90|180|270)\s*$/i', array('labelangle'=>1)),
  # array('(NODE|LINK)', '/^\s*TEMPLATE\s+(\S+)\s*$/i', array('template'=>1)),
   
  array('LINK', '/^\s*OUTBWFORMAT\s+(.*)\s*$/i', array('bwlabelformats[OUT]'=>1,'labelstyle'=>'--')),
  array('LINK', '/^\s*INBWFORMAT\s+(.*)\s*$/i', array('bwlabelformats[IN]'=>1,'labelstyle'=>'--')),
  # array('NODE','/^\s*ICON\s+none\s*$/i',array('iconfile'=>'')),
  array('NODE','/^\s*ICON\s+(\S+)\s*$/i',array('iconfile'=>1, 'iconscalew'=>'#0', 'iconscaleh'=>'#0')),
  array('NODE','/^\s*ICON\s+(\S+)\s*$/i',array('iconfile'=>1)),
  array('NODE','/^\s*ICON\s+(\d+)\s+(\d+)\s+(inpie|outpie|box|rbox|round|gauge|nink)\s*$/i',array('iconfile'=>3, 'iconscalew'=>1, 'iconscaleh'=>2)),
  array('NODE','/^\s*ICON\s+(\d+)\s+(\d+)\s+(\S+)\s*$/i',array('iconfile'=>3, 'iconscalew'=>1, 'iconscaleh'=>2)),
   
  array('NODE','/^\s*NOTES\s+(.*)\s*$/i',array('notestext[IN]'=>1,'notestext[OUT]'=>1)),
  array('LINK','/^\s*NOTES\s+(.*)\s*$/i',array('notestext[IN]'=>1,'notestext[OUT]'=>1)),
  array('LINK','/^\s*INNOTES\s+(.*)\s*$/i',array('notestext[IN]'=>1)),
  array('LINK','/^\s*OUTNOTES\s+(.*)\s*$/i',array('notestext[OUT]'=>1)),
   
  array('NODE','/^\s*INFOURL\s+(.*)\s*$/i',array('infourl[IN]'=>1,'infourl[OUT]'=>1)),
  array('LINK','/^\s*INFOURL\s+(.*)\s*$/i',array('infourl[IN]'=>1,'infourl[OUT]'=>1)),
  array('LINK','/^\s*ININFOURL\s+(.*)\s*$/i',array('infourl[IN]'=>1)),
  array('LINK','/^\s*OUTINFOURL\s+(.*)\s*$/i',array('infourl[OUT]'=>1)),
   
  array('NODE','/^\s*OVERLIBCAPTION\s+(.*)\s*$/i',array('overlibcaption[IN]'=>1,'overlibcaption[OUT]'=>1)),
  array('LINK','/^\s*OVERLIBCAPTION\s+(.*)\s*$/i',array('overlibcaption[IN]'=>1,'overlibcaption[OUT]'=>1)),
  array('LINK','/^\s*INOVERLIBCAPTION\s+(.*)\s*$/i',array('overlibcaption[IN]'=>1)),
  array('LINK','/^\s*OUTOVERLIBCAPTION\s+(.*)\s*$/i',array('overlibcaption[OUT]'=>1)),
   
  array('(NODE|LINK)', "/^\s*ZORDER\s+([-+]?\d+)\s*$/i", array('zorder'=>1)),
  array('(NODE|LINK)', "/^\s*OVERLIBWIDTH\s+(\d+)\s*$/i", array('overlibwidth'=>1)),
  array('(NODE|LINK)', "/^\s*OVERLIBHEIGHT\s+(\d+)\s*$/i", array('overlibheight'=>1)),
  array('NODE', "/^\s*POSITION\s+([-+]?\d+)\s+([-+]?\d+)\s*$/i", array('x'=>1,'y'=>2)),
  array('NODE', "/^\s*POSITION\s+(\S+)\s+([-+]?\d+)\s+([-+]?\d+)\s*$/i", array('x'=>2,'y'=>3,'original_x'=>2,'original_y'=>3,'relative_to'=>1,'relative_resolved'=>FALSE)),
  array('NODE', "/^\s*POSITION\s+(\S+)\s+([-+]?\d+)r(\d+)\s*$/i", array('x'=>2,'y'=>3,'original_x'=>2,'original_y'=>3,'relative_to'=>1,'polar'=>TRUE,'relative_resolved'=>FALSE))
  );
   
if (preg_match("/^\s*POSITION\s+([-+]?\d+)\s+([-+]?\d+)\s*$/i", $buffer, $matches)) // alternative for use later where quoted strings are more useful
{ $args = ParseString($buffer);
if ($last_seen == 'NODE')  
{  
$curnode->x=$matches[1];  
$curnode->y=$matches[2];  
$linematched++;  
}  
}  
   
if (preg_match("/^\s*POSITION\s+(\S+)\s+([-+]?\d+)\s+([-+]?\d+)\s*$/i", $buffer, $matches)) // this loop replaces a whole pile of duplicated ifs with something with consistent handling
  foreach ($config_keywords as $keyword)
  {
  if(preg_match("/".$keyword[0]."/",$last_seen))
{ {
if ($last_seen == 'NODE') $statskey = $last_seen."-".$keyword[1];
  $statskey = str_replace( array('/^\s*','\s*$/i'),array('',''), $statskey);
  if(!isset($this->usage_stats[$statskey])) $this->usage_stats[$statskey] = 0;
   
  if(preg_match($keyword[1],$buffer,$matches))
{ {
$curnode->relative_to = $matches[1]; # print "CONFIG MATCHED: ".$keyword[1]."\n";
$curnode->relative_resolved = FALSE;  
$curnode->x = $matches[2]; $this->usage_stats[$statskey]++;
$curnode->y = $matches[3];  
$curnode->original_x = $matches[2]; foreach ($keyword[2] as $key=>$val)
$curnode->original_y = $matches[3]; {
  // so we can poke in numbers too, if the value starts with #
  // then take the # off, and treat the rest as a number literal
  if(preg_match("/^#(.*)/",$val,$m))
  {
  $val = $m[1];
  }
  elseif(is_numeric($val))
  {
  // if it's a number, then it;s a match number,
  // otherwise it's a literal to be put into a variable
  $val = $matches[$val];
  }
   
  assert('is_object($curobj)');
   
  if(preg_match('/^(.*)\[([^\]]+)\]$/',$key,$m))
  {
  $index = constant($m[2]);
  $key = $m[1];
  $curobj->{$key}[$index] = $val;
  }
  else
  {
  $curobj->$key = $val;
  }
  }
$linematched++; $linematched++;
  # print "\n\n";
  break;
} }
} }
  }
   
if (preg_match("/^\s*LABEL\s+(.*)\s*$/i", $buffer, $matches)) if (preg_match("/^\s*NODES\s+(\S+)\s+(\S+)\s*$/i", $buffer, $matches))
  {
  if ($last_seen == 'LINK')
{ {
if ($last_seen == 'NODE') $valid_nodes=2;
{  
$curnode->label=$matches[1];  
$linematched++;  
}  
}  
   
if (preg_match("/^\s*NODES\s+(\S+)\s+(\S+)\s*$/i", $buffer, $matches)) foreach (array(1, 2)as $i)
{  
if ($last_seen == 'LINK')  
{ {
$valid_nodes=2; $endoffset[$i]='C';
  $nodenames[$i]=$matches[$i];
   
foreach (array(1, 2)as $i) // percentage of compass - must be first
  if (preg_match("/:(NE|SE|NW|SW|N|S|E|W|C)(\d+)$/i", $matches[$i], $submatches))
{ {
$endoffset[$i]='C'; $endoffset[$i]=$submatches[1].$submatches[2];
$nodenames[$i]=$matches[$i]; $nodenames[$i]=preg_replace("/:(NE|SE|NW|SW|N|S|E|W|C)\d+$/i", '', $matches[$i]);
  $this->need_size_precalc=TRUE;
if (preg_match("/:(NE|SE|NW|SW|N|S|E|W)$/i", $matches[$i], $submatches)) }
{  
$endoffset[$i]=$submatches[1]; if (preg_match("/:(NE|SE|NW|SW|N|S|E|W|C)$/i", $matches[$i], $submatches))
$nodenames[$i]=preg_replace("/:(NE|SE|NW|SW|N|S|E|W)$/i", '', $matches[$i]); {
$this->need_size_precalc=TRUE; $endoffset[$i]=$submatches[1];
} $nodenames[$i]=preg_replace("/:(NE|SE|NW|SW|N|S|E|W|C)$/i", '', $matches[$i]);
  $this->need_size_precalc=TRUE;
  }
   
if (preg_match("/:([-+]?\d+):([-+]?\d+)$/i", $matches[$i], $submatches)) if( preg_match("/:(-?\d+r\d+)$/i", $matches[$i], $submatches) )
{ {
$xoff = $submatches[1]; $endoffset[$i]=$submatches[1];
$yoff = $submatches[2]; $nodenames[$i]=preg_replace("/:(-?\d+r\d+)$/i", '', $matches[$i]);
$endoffset[$i]=$xoff.":".$yoff; $this->need_size_precalc=TRUE;
$nodenames[$i]=preg_replace("/:$xoff:$yoff$/i", '', $matches[$i]);  
$this->need_size_precalc=TRUE;  
}  
   
if (!array_key_exists($nodenames[$i], $this->nodes))  
{  
warn ("Unknown node '" . $nodenames[$i]  
. "' on line $linecount of config\n");  
$valid_nodes--;  
}  
} }
   
// TODO - really, this should kill the whole link, and reset for the next one if (preg_match("/:([-+]?\d+):([-+]?\d+)$/i", $matches[$i], $submatches))
if ($valid_nodes == 2)  
{ {
$curlink->a=$this->nodes[$nodenames[1]]; $xoff = $submatches[1];
$curlink->b=$this->nodes[$nodenames[2]]; $yoff = $submatches[2];
$curlink->a_offset=$endoffset[1]; $endoffset[$i]=$xoff.":".$yoff;
$curlink->b_offset=$endoffset[2]; $nodenames[$i]=preg_replace("/:$xoff:$yoff$/i", '', $matches[$i]);
  $this->need_size_precalc=TRUE;
} }
else {  
// this'll stop the current link being added  
$last_seen="broken"; }  
   
$linematched++; if (!array_key_exists($nodenames[$i], $this->nodes))
  {
  warn ("Unknown node '" . $nodenames[$i] . "' on line $linecount of config\n");
  $valid_nodes--;
  }
} }
   
  // TODO - really, this should kill the whole link, and reset for the next one
  if ($valid_nodes == 2)
  {
  $curlink->a=$this->nodes[$nodenames[1]];
  $curlink->b=$this->nodes[$nodenames[2]];
  $curlink->a_offset=$endoffset[1];
  $curlink->b_offset=$endoffset[2];
  }
  else {
  // this'll stop the current link being added
  $last_seen="broken"; }
   
  $linematched++;
} }
  }
   
if (preg_match("/^\s*TARGET\s+(.*)\s*$/i", $buffer, $matches)) if ( $last_seen=='GLOBAL' && preg_match("/^\s*INCLUDE\s+(.*)\s*$/i", $buffer, $matches))
  {
  if(file_exists($matches[1])){
  debug("Including '{$matches[1]}'\n");
  $this->ReadConfig($matches[1], TRUE);
  $last_seen = "GLOBAL";
  }else{
  warn("INCLUDE File '{$matches[1]}' not found!\n");
  }
  $linematched++;
  }
   
  if ( ( $last_seen=='NODE' || $last_seen=='LINK' ) && preg_match("/^\s*TARGET\s+(.*)\s*$/i", $buffer, $matches))
  {
  $linematched++;
  # $targets=preg_split('/\s+/', $matches[1], -1, PREG_SPLIT_NO_EMPTY);
  $rawtargetlist = $matches[1]." ";
   
  if($args[0]=='TARGET')
{ {
$linematched++;  
   
$targets=preg_split('/\s+/', $matches[1], -1, PREG_SPLIT_NO_EMPTY);  
// wipe any existing targets, otherwise things in the DEFAULT accumulate with the new ones // wipe any existing targets, otherwise things in the DEFAULT accumulate with the new ones
$curobj->targets = array(); $curobj->targets = array();
foreach ($targets as $target) array_shift($args); // take off the actual TARGET keyword
   
  foreach($args as $arg)
{ {
// we store the original TARGET string, and line number, along with the breakdown, to make nicer error messages later // we store the original TARGET string, and line number, along with the breakdown, to make nicer error messages later
$newtarget=array($target,'','',$linecount,$target); // array of 7 things:
  // - only 0,1,2,3,4 are used at the moment (more used to be before DS plugins)
  // 0 => final target string (filled in by ReadData)
  // 1 => multiplier (filled in by ReadData)
  // 2 => config filename where this line appears
  // 3 => linenumber in that file
  // 4 => the original target string
  // 5 => the plugin to use to pull data
  $newtarget=array('','',$filename,$linecount,$arg,"","");
if ($curobj) if ($curobj)
{ {
  debug(" TARGET: $arg\n");
$curobj->targets[]=$newtarget; $curobj->targets[]=$newtarget;
} }
} }
} }
  }
if (preg_match("/^\s*WIDTH\s+(\d+)\s*$/i", $buffer, $matches))  
  if ($last_seen == 'LINK' && preg_match(
  "/^\s*BWLABEL\s+(bits|percent|unformatted|none)\s*$/i", $buffer,
  $matches))
  {
  $format_in = '';
  $format_out = '';
  $style = strtolower($matches[1]);
  if($style=='percent')
{ {
if ($last_seen == 'LINK') $format_in = FMT_PERC_IN;
{ $format_out = FMT_PERC_OUT;
$curlink->width=$matches[1];  
$linematched++;  
}  
else // we're talking about the global WIDTH  
{  
$this->width=$matches[1];  
$linematched++;  
}  
} }
  if($style=='bits')
if (preg_match("/^\s*HEIGHT\s+(\d+)\s*$/i", $buffer, $matches))  
{ {
$this->height=$matches[1]; $format_in = FMT_BITS_IN;
$linematched++; $format_out = FMT_BITS_OUT;
} }
  if($style=='unformatted')
if ( ($last_seen == 'LINK') && (preg_match("/^\s*INCOMMENT\s+(.*)\s*$/i", $buffer, $matches)))  
{ {
# $curlink->incomment = $matches[1]; $format_in = FMT_UNFORM_IN;
$curlink->comments[IN] = $matches[1]; $format_out = FMT_UNFORM_OUT;
$linematched++;  
} }
   
if ( ($last_seen == 'LINK') && (preg_match("/^\s*OUTCOMMENT\s+(.*)\s*$/i", $buffer, $matches))) $curobj->labelstyle=$style;
{ $curobj->bwlabelformats[IN] = $format_in;
# $curlink->outcomment = $matches[1]; $curobj->bwlabelformats[OUT] = $format_out;
$curlink->comments[OUT] = $matches[1]; $linematched++;
  }
   
  if (preg_match("/^\s*SET\s+(\S+)\s+(.*)\s*$/i", $buffer, $matches))
  {
  $curobj->add_hint($matches[1],trim($matches[2]));
$linematched++; $linematched++;
} }
   
  // allow setting a variable to ""
if ( ($last_seen == 'LINK') && (preg_match("/^\s*(BANDWIDTH|MAXVALUE)\s+(\d+\.?\d*[KMGT]?)\s*$/i", $buffer, $matches))) if (preg_match("/^\s*SET\s+(\S+)\s*$/i", $buffer, $matches))
{ {
$curlink->SetBandwidth($matches[2], $matches[2]); $curobj->add_hint($matches[1],'');
$linematched++; $linematched++;
} }
   
  if (preg_match("/^\s*(IN|OUT)?OVERLIBGRAPH\s+(.+)$/i", $buffer, $matches))
  {
  $this->has_overlibs = TRUE;
  if($last_seen == 'NODE' && $matches[1] != '') {
  warn("IN/OUTOVERLIBGRAPH make no sense for a NODE! [WMWARN42]\n");
  } else if($last_seen == 'LINK' || $last_seen=='NODE' ) {
   
if ( ($last_seen == 'LINK') && (preg_match("/^\s*(MAXVALUE|BANDWIDTH)\s+(\d+\.?\d*[KMGT]?)\s+(\d+\.?\d*[KMGT]?)\s*$/i", $buffer, $urls = preg_split('/\s+/', $matches[2], -1, PREG_SPLIT_NO_EMPTY);
$matches)))  
{  
$curlink->SetBandwidth($matches[2], $matches[3]);  
$linematched++;  
}  
   
if ( ($last_seen == 'NODE') && (preg_match("/^\s*MAXVALUE\s+(\d+\.?\d*[KMGT]?)\s+(\d+\.?\d*[KMGT]?)\s*$/i", $buffer, if($matches[1] == 'IN') $index = IN;
$matches))) if($matches[1] == 'OUT') $index = OUT;
{ if($matches[1] == '') {
$curnode->SetBandwidth($matches[1], $matches[2]); $curobj->overliburl[IN]=$urls;
$linematched++; $curobj->overliburl[OUT]=$urls;
} } else {
  $curobj->overliburl[$index]=$urls;
if ( ($last_seen == 'NODE') && (preg_match("/^\s*MAXVALUE\s+(\d+\.?\d*[KMGT]?)\s*$/i", $buffer,  
$matches)))  
{  
$curnode->SetBandwidth($matches[1], $matches[1]);  
$linematched++;  
}  
   
if (preg_match("/^\s*ICON\s+(\S+)\s*$/i", $buffer, $matches))  
{  
if ($last_seen == 'NODE')  
{  
if($matches[1]=='none')  
{  
$curnode->iconfile='';  
}  
else  
{  
$curnode->iconfile=$matches[1];  
$this->used_images[] = $matches[1];  
} }
$linematched++; $linematched++;
} }
} }
   
if (preg_match("/^\s*ICON\s+(\d+)\s+(\d+)\s+(\S+)\s*$/i", $buffer, $matches)) // array('(NODE|LINK)', '/^\s*TEMPLATE\s+(\S+)\s*$/i', array('template'=>1)),
   
  if ( ( $last_seen=='NODE' || $last_seen=='LINK' ) && preg_match("/^\s*TEMPLATE\s+(\S+)\s*$/i", $buffer, $matches))
  {
  $tname = $matches[1];
  if( ($last_seen=='NODE' && isset($this->nodes[$tname])) || ($last_seen=='LINK' && isset($this->links[$tname])) )
{ {
if ($last_seen == 'NODE') $curobj->template = $matches[1];
{ debug("Resetting to template $last_seen ".$curobj->template."\n");
$curnode->iconfile=$matches[3]; $curobj->Reset($this);
$this->used_images[] = $matches[3]; if( $objectlinecount > 1 ) warn("line $linecount: TEMPLATE is not first line of object. Some data may be lost. [WMWARN39]\n");
$curnode->iconscalew = $matches[1]; // build up a list of templates - this will be useful later for the tree view
$curnode->iconscaleh = $matches[2];  
$linematched++; if($last_seen == 'NODE') $this->node_template_tree[ $tname ][]= $curobj->name;
} if($last_seen == 'LINK') $this->link_template_tree[ $tname ][]= $curobj->name;
} }
  else
if (preg_match("/^\s*SET\s+(\S+)\s+(.*)\s*$/i", $buffer, $matches))  
{ {
if($curobj) warn("line $linecount: $last_seen TEMPLATE '$tname' doesn't exist! (if it does exist, check it's defined first) [WMWARN40]\n");
{  
// THIS IS NOT UPDATING THE 'REAL' DEFAULT NODE  
$curobj->add_hint($matches[1],$matches[2]);  
$linematched++;  
# warn("POST-SET ".$curobj->name."::".var_dump($curobj->hints)."::\n");  
# warn("DEFAULT FIRST SAYS ".$this->defaultnode->hints['sigdigits']."\n");  
}  
else  
{  
// it's a global thing, for the map  
$this->add_hint($matches[1],$matches[2]);  
$linematched++;  
}  
} }
  $linematched++;
   
  }
   
if (preg_match("/^\s*NOTES\s+(.*)\s*$/i", $buffer, $matches)) if ($last_seen == 'LINK' && preg_match("/^\s*VIA\s+([-+]?\d+)\s+([-+]?\d+)\s*$/i", $buffer, $matches))
{ {
if($curobj) $curlink->vialist[]=array
{ (
$curobj->notestext=$matches[1]; $matches[1],
$linematched++; $matches[2]
} );
}  
   
if (preg_match("/^\s*INFOURL\s+(\S+)\s*$/i", $buffer, $matches)) $linematched++;
{ }
if($curobj)  
{  
$curobj->infourl=$matches[1];  
$linematched++;  
}  
}  
   
if (preg_match("/^\s*OVERLIBGRAPH\s+(\S+)\s*$/i", $buffer, $matches)) if ($last_seen == 'LINK' && preg_match("/^\s*VIA\s+(\S+)\s+([-+]?\d+)\s+([-+]?\d+)\s*$/i", $buffer, $matches))
{ {
if($curobj) $curlink->vialist[]=array
{ (
$curobj->overliburl=$matches[1]; $matches[2],
$linematched++; $matches[3],
} $matches[1]
} );
   
if (preg_match("/^\s*OVERLIBCAPTION\s+(.+)\s*$/i", $buffer, $matches)) $linematched++;
{ }
if($curobj)  
{  
$curobj->overlibcaption=$matches[1];  
$linematched++;  
}  
}  
   
if (preg_match("/^\s*OVERLIBHEIGHT\s+(\d+)\s*$/i", $buffer, $matches)) if( ($last_seen == 'NODE') && preg_match("/^\s*USE(ICON)?SCALE\s+([A-Za-z][A-Za-z0-9_]*)(\s+(in|out))?(\s+(absolute|percent))?\s*$/i",$buffer,$matches))
  {
  $svar = '';
  $stype = 'percent';
  if(isset($matches[3]))
{ {
if($curobj) $svar = trim($matches[3]);
{  
$curobj->overlibheight=$matches[1];  
$linematched++;  
}  
} }
  if(isset($matches[6]))
if (preg_match("/^\s*OVERLIBWIDTH\s+(\d+)\s*$/i", $buffer, $matches))  
{ {
if($curobj) $stype = strtolower(trim($matches[6]));
{  
$curobj->overlibwidth=$matches[1];  
$linematched++;  
}  
} }
  // opens the door for other scaley things...
if ($last_seen == 'NODE' && preg_match("/^\s*LABELFONT\s+(\d+)\s*$/i", $buffer, $matches)) switch($matches[1])
{ {
$curnode->labelfont=$matches[1]; case 'ICON':
$linematched++; $varname = 'iconscalevar';
  $uvarname = 'useiconscale';
  $tvarname = 'iconscaletype';
   
  // if(!function_exists("imagefilter"))
  // {
  // warn("ICON SCALEs require imagefilter, which is not present in your PHP [WMWARN040]\n");
  // }
  break;
  default:
  $varname = 'scalevar';
  $uvarname = 'usescale';
  $tvarname = 'scaletype';
  break;
} }
   
if ($last_seen == 'NODE' && preg_match( if($svar != '')
"/^\s*LABELOFFSET\s+([-+]?\d+)\s+([-+]?\d+)\s*$/i", $buffer,  
$matches))  
{ {
$curnode->labeloffsetx=$matches[1]; $curnode->$varname = $svar;
$curnode->labeloffsety=$matches[2];  
   
$linematched++;  
} }
  $curnode->$tvarname = $stype;
  $curnode->$uvarname = $matches[2];
   
if ($last_seen == 'NODE' && preg_match( // warn("Set $varname and $uvarname\n");
"/^\s*LABELOFFSET\s+(C|NE|SE|NW|SW|N|S|E|W)\s*$/i", $buffer,  
$matches))  
{  
$curnode->labeloffset=$matches[1];  
$linematched++;  
}  
   
if ($last_seen == 'LINK' && preg_match("/^\s*VIA\s+(\d+)\s+(\d+)\s*$/i", $buffer, $matches)) // print ">> $stype $svar ".$matches[2]." ".$curnode->name." \n";
{  
$curlink->vialist[]=array  
(  
$matches[1],  
$matches[2]  
);  
   
$linematched++; $linematched++;
} }
   
if ($last_seen == 'LINK' && preg_match("/^\s*BWFONT\s+(\d+)\s*$/i", $buffer, $matches)) // one REGEXP to rule them all:
{ // if(preg_match("/^\s*SCALE\s+([A-Za-z][A-Za-z0-9_]*\s+)?(\d+\.?\d*)\s+(\d+\.?\d*)\s+(\d+)\s+(\d+)\s+(\d+)(?:\s+(\d+)\s+(\d+)\s+(\d+))?\s*$/i",
$curlink->bwfont=$matches[1]; // 0.95b if(preg_match("/^\s*SCALE\s+([A-Za-z][A-Za-z0-9_]*\s+)?(\d+\.?\d*)\s+(\d+\.?\d*)\s+(\d+)\s+(\d+)\s+(\d+)(?:\s+(\d+)\s+(\d+)\s+(\d+))?\s*(.*)$/i",
$linematched++; if(preg_match("/^\s*SCALE\s+([A-Za-z][A-Za-z0-9_]*\s+)?(\-?\d+\.?\d*[munMGT]?)\s+(\-?\d+\.?\d*[munMGT]?)\s+(?:(\d+)\s+(\d+)\s+(\d+)(?:\s+(\d+)\s+(\d+)\s+(\d+))?|(none))\s*(.*)$/i",
} $buffer, $matches))
  {
  // The default scale name is DEFAULT
  if($matches[1]=='') $matches[1] = 'DEFAULT';
  else $matches[1] = trim($matches[1]);
   
if ($last_seen == 'LINK' && preg_match("/^\s*COMMENTFONT\s+(\d+)\s*$/i", $buffer, $matches)) $key=$matches[2] . '_' . $matches[3];
{  
$curlink->commentfont=$matches[1];  
$linematched++;  
}  
   
if ($last_seen == 'LINK' && preg_match( $this->colours[$matches[1]][$key]['key']=$key;
"/^\s*BWLABEL\s+(bits|percent|unformatted|none)\s*$/i", $buffer,  
$matches))  
{  
$curlink->labelstyle=strtolower($matches[1]);  
$linematched++;  
}  
   
if ($last_seen == 'LINK' && preg_match( $tag = $matches[11];
"/^\s*BWSTYLE\s+(classic|angled)\s*$/i", $buffer,  
$matches))  
{  
$curlink->labelboxstyle=$matches[1];  
$linematched++;  
}  
   
if ($last_seen == 'LINK' && preg_match( $this->colours[$matches[1]][$key]['tag']=$tag;
"/^\s*BWLABELPOS\s+(\d+)\s(\d+)\s*$/i", $buffer,  
$matches))  
{  
$curlink->labeloffset_in = $matches[1];  
$curlink->labeloffset_out = $matches[2];  
$linematched++;  
}  
   
if ($last_seen == 'LINK' && preg_match(  
"/^\s*COMMENTPOS\s+(\d+)\s(\d+)\s*$/i", $buffer,  
$matches))  
{  
$curlink->commentoffset_in = $matches[1];  
$curlink->commentoffset_out = $matches[2];  
$linematched++;  
}  
   
if( ($last_seen == 'NODE') && preg_match("/^\s*USESCALE\s+([A-Za-z][A-Za-z0-9_]*)(\s+(in|out))?\s*$/i",$buffer,$matches)) $this->colours[$matches[1]][$key]['bottom'] = unformat_number($matches[2], $this->kilo);
{ $this->colours[$matches[1]][$key]['top'] = unformat_number($matches[3], $this->kilo);
$svar = ''; $this->colours[$matches[1]][$key]['special'] = 0;
if(isset($matches[2]))  
{  
$svar = trim($matches[2]);  
}  
   
if($matches[1] == 'none')  
{  
$curnode->usescale = $matches[1];  
if($svar != '')  
{  
$curnode->scalevar = $svar;  
}  
}  
else  
{  
$curnode->usescale = $matches[1];  
if($svar != '')  
{  
$curnode->scalevar = $svar;  
}  
}  
$linematched++;  
}  
   
if( ($last_seen == 'LINK') && preg_match("/^\s*USESCALE\s+([A-Za-z][A-Za-z0-9_]*)\s*$/i",$buffer,$matches)) if(isset($matches[10]) && $matches[10] == 'none')
{ {
$curlink->usescale = $matches[1]; $this->colours[$matches[1]][$key]['red1'] = -1;
  $this->colours[$matches[1]][$key]['green1'] = -1;
$linematched++; $this->colours[$matches[1]][$key]['blue1'] = -1;
} }
  else
// one REGEXP to rule them all:  
if(preg_match("/^\s*SCALE\s+([A-Za-z][A-Za-z0-9_]*\s+)?(\d+\.?\d*)\s+(\d+\.?\d*)\s+(\d+)\s+(\d+)\s+(\d+)(?:\s+(\d+)\s+(\d+)\s+(\d+))?\s*$/i",  
$buffer, $matches))  
{ {
// The default scale name is DEFAULT  
if($matches[1]=='') $matches[1] = 'DEFAULT';  
else $matches[1] = trim($matches[1]);  
   
$key=$matches[2] . '_' . $matches[3];  
   
$this->colours[$matches[1]][$key]['key']=$key;  
$this->colours[$matches[1]][$key]['bottom'] = (float)($matches[2]);  
$this->colours[$matches[1]][$key]['top'] = (float)($matches[3]);  
   
$this->colours[$matches[1]][$key]['red1'] = (int)($matches[4]); $this->colours[$matches[1]][$key]['red1'] = (int)($matches[4]);
$this->colours[$matches[1]][$key]['green1'] = (int)($matches[5]); $this->colours[$matches[1]][$key]['green1'] = (int)($matches[5]);
$this->colours[$matches[1]][$key]['blue1'] = (int)($matches[6]); $this->colours[$matches[1]][$key]['blue1'] = (int)($matches[6]);
   
// this is the second colour, if there is one  
if(isset($matches[7]))  
{  
$this->colours[$matches[1]][$key]['red2'] = (int) ($matches[7]);  
$this->colours[$matches[1]][$key]['green2'] = (int) ($matches[8]);  
$this->colours[$matches[1]][$key]['blue2'] = (int) ($matches[9]);  
}  
   
   
if(! isset($this->numscales[$matches[1]]))  
{  
$this->numscales[$matches[1]]=1;  
}  
else  
{  
$this->numscales[$matches[1]]++;  
}  
// we count if we've seen any default scale, otherwise, we have to add  
// one at the end.  
if($matches[1]=='DEFAULT')  
{  
$scalesseen++;  
}  
   
$linematched++;  
} }
   
if (preg_match("/^\s*KEYPOS\s+([A-Za-z][A-Za-z0-9_]*\s+)?(-?\d+)\s+(-?\d+)(.*)/i", $buffer, $matches)) // this is the second colour, if there is one
  if(isset($matches[7]) && $matches[7] != '')
{ {
$whichkey = trim($matches[1]); $this->colours[$matches[1]][$key]['red2'] = (int) ($matches[7]);
if($whichkey == '') $whichkey = 'DEFAULT'; $this->colours[$matches[1]][$key]['green2'] = (int) ($matches[8]);
  $this->colours[$matches[1]][$key]['blue2'] = (int) ($matches[9]);
$this->keyx[$whichkey]=$matches[2];  
$this->keyy[$whichkey]=$matches[3];  
$extra=trim($matches[4]);  
   
if ($extra != '')  
$this->keytext[$whichkey] = $extra;  
if(!isset($this->keytext[$whichkey]))  
$this->keytext[$whichkey] = "DEFAULT TITLE";  
if(!isset($this->keystyle[$whichkey]))  
$this->keystyle[$whichkey] = "classic";  
   
$linematched++;  
} }
   
if (preg_match("/^\s*TITLEPOS\s+(-?\d+)\s+(-?\d+)\s+(.*)\s*$/i", $buffer, $matches))  
{  
$this->titlex=$matches[1];  
$this->titley=$matches[2];  
$extra=trim($matches[3]);  
   
if ($extra != '') if(! isset($this->numscales[$matches[1]]))
$this->title=$extra;  
   
$linematched++;  
}  
   
// truetype font definition (actually, we don't really check if it's truetype) - filename + size  
if (preg_match("/^\s*FONTDEFINE\s+(\d+)\s+(\S+)\s+(\d+)\s*$/i", $buffer, $matches))  
{ {
if (function_exists("imagettfbbox")) $this->numscales[$matches[1]]=1;
{  
// test if this font is valid, before adding it to the font table...  
$bounds=@imagettfbbox($matches[3], 0, $matches[2], "Ignore me");  
   
if (isset($bounds[0]))  
{  
$this->fonts[$matches[1]]->type="truetype";  
$this->fonts[$matches[1]]->file=$matches[2];  
$this->fonts[$matches[1]]->size=$matches[3];  
}  
else { warn  
("Failed to load ttf font " . $matches[2] . " - at config line $linecount\n"); }  
}  
else { warn  
("imagettfbbox() is not a defined function. You don't seem to have FreeType compiled into your gd module.\n");  
}  
   
$linematched++;  
} }
  else
// GD font definition (no size here)  
if (preg_match("/^\s*FONTDEFINE\s+(\d+)\s+(\S+)\s*$/i", $buffer, $matches))  
{ {
$newfont=imageloadfont($matches[2]); $this->numscales[$matches[1]]++;
   
if ($newfont)  
{  
$this->fonts[$matches[1]]->type="gd";  
$this->fonts[$matches[1]]->file=$matches[2];  
$this->fonts[$matches[1]]->gdnumber=$newfont;  
}  
else { warn ("Failed to load GD font: " . $matches[2]  
. " ($newfont) at config line $linecount\n"); }  
   
$linematched++;  
} }
  // we count if we've seen any default scale, otherwise, we have to add
if (preg_match("/^\s*KEYFONT\s+(\d+)\s*$/i", $buffer, $matches)) // one at the end.
  if($matches[1]=='DEFAULT')
{ {
$this->keyfont=$matches[1]; $scalesseen++;
$linematched++;  
} }
   
if (preg_match("/^\s*TIMEFONT\s+(\d+)\s*$/i", $buffer, $matches)) $linematched++;
{ }
$this->timefont=$matches[1];  
$linematched++;  
}  
   
if (preg_match("/^\s*TITLEFONT\s+(\d+)\s*$/i", $buffer, $matches)) if (preg_match("/^\s*KEYPOS\s+([A-Za-z][A-Za-z0-9_]*\s+)?(-?\d+)\s+(-?\d+)(.*)/i", $buffer, $matches))
{ {
$this->titlefont=$matches[1]; $whichkey = trim($matches[1]);
$linematched++; if($whichkey == '') $whichkey = 'DEFAULT';
}  
   
if (preg_match("/^\s*NODEFONT\s+(\d+)\s*$/i", $buffer, $matches)) $this->keyx[$whichkey]=$matches[2];
{ $this->keyy[$whichkey]=$matches[3];
$this->nodefont=$matches[1]; $extra=trim($matches[4]);
$this->defaultnode->labelfont=$matches[1];  
warn  
("NODEFONT is deprecated. Use NODE DEFAULT and LABELFONT instead. config line $linecount\n");  
$linematched++;  
}  
   
if (preg_match("/^\s*LINKFONT\s+(\d+)\s*$/i", $buffer, $matches)) if ($extra != '')
{ $this->keytext[$whichkey] = $extra;
$this->linkfont=$matches[1]; if(!isset($this->keytext[$whichkey]))
$this->defaultlink->bwfont=$matches[1]; $this->keytext[$whichkey] = "DEFAULT TITLE";
warn if(!isset($this->keystyle[$whichkey]))
("LINKFONT is deprecated. Use LINK DEFAULT and BWFONT instead. config line $linecount\n"); $this->keystyle[$whichkey] = "classic";
$linematched++;  
}  
   
if (preg_match("/^\s*TIMEPOS\s+(\d+)\s+(\d+)(.*)\s*$/i", $buffer, $matches)) $linematched++;
  }
   
   
  // truetype font definition (actually, we don't really check if it's truetype) - filename + size
  if (preg_match("/^\s*FONTDEFINE\s+(\d+)\s+(\S+)\s+(\d+)\s*$/i", $buffer, $matches))
  {
  if (function_exists("imagettfbbox"))
{ {
$this->timex=$matches[1]; // test if this font is valid, before adding it to the font table...
$this->timey=$matches[2]; $bounds=@imagettfbbox($matches[3], 0, $matches[2], "Ignore me");
$extra=trim($matches[3]);  
   
if ($extra != '') if (isset($bounds[0]))
$this->stamptext=$extra;  
   
$linematched++;  
}  
   
if(preg_match("/^\s*KEYSTYLE\s+([A-Za-z][A-Za-z0-9_]+\s+)?(classic|horizontal|vertical)\s+?(\d+)?\s*$/i",$buffer, $matches))  
{  
$whichkey = trim($matches[1]);  
if($whichkey == '') $whichkey = 'DEFAULT';  
$this->keystyle[$whichkey] = strtolower($matches[2]);  
   
if(isset($matches[3]) && $matches[3] != '')  
{ {
$this->keysize[$whichkey] = $matches[3]; $this->fonts[$matches[1]]->type="truetype";
  $this->fonts[$matches[1]]->file=$matches[2];
  $this->fonts[$matches[1]]->size=$matches[3];
} }
else else { warn
{ ("Failed to load ttf font " . $matches[2] . " - at config line $linecount\n [WMWARN30]"); }
$this->keysize[$whichkey] = $this->keysize['DEFAULT']; }
} else { warn
  ("imagettfbbox() is not a defined function. You don't seem to have FreeType compiled into your gd module. [WMWARN31]\n");
$linematched++;  
} }
   
if (preg_match("/^\s*BWLABELS\s+(bits|percent|none)\s*$/i", $buffer, $matches)) $linematched++;
{ }
# $this->labelstyle = strtolower($matches[1]);  
$this->defaultlink->labelstyle=strtolower($matches[1]);  
warn  
("BWLABELS is deprecated. Use LINK DEFAULT and BWLABEL instead. config line $linecount\n");  
   
$linematched++; // GD font definition (no size here)
} if (preg_match("/^\s*FONTDEFINE\s+(\d+)\s+(\S+)\s*$/i", $buffer, $matches))
  {
  $newfont=imageloadfont($matches[2]);
   
if (preg_match("/^\s*KILO\s+(\d+)\s*$/i", $buffer, $matches)) if ($newfont)
{ {
$this->kilo=$matches[1]; $this->fonts[$matches[1]]->type="gd";
$this->defaultlink->owner->kilo=$matches[1]; $this->fonts[$matches[1]]->file=$matches[2];
$linematched++; $this->fonts[$matches[1]]->gdnumber=$newfont;
} }
  else { warn ("Failed to load GD font: " . $matches[2]
  . " ($newfont) at config line $linecount [WMWARN32]\n"); }
   
if (preg_match("/^\s*BACKGROUND\s+(.+)\s*$/i", $buffer, $matches)) $linematched++;
{ }
$this->background=$matches[1];  
$this->used_images[] = $matches[1];  
$linematched++;  
}  
   
if (preg_match( if(preg_match("/^\s*KEYSTYLE\s+([A-Za-z][A-Za-z0-9_]+\s+)?(classic|horizontal|vertical|inverted|tags)\s?(\d+)?\s*$/i",$buffer, $matches))
"/^\s*(TIME|TITLE|KEYBG|KEYTEXT|KEYOUTLINE|BG)COLOR\s+(\d+)\s+(\d+)\s+(\d+)\s*$/i", {
$buffer, $whichkey = trim($matches[1]);
$matches)) if($whichkey == '') $whichkey = 'DEFAULT';
{ $this->keystyle[$whichkey] = strtolower($matches[2]);
$key=$matches[1];  
# "Found colour line for $key\n";  
$this->colours['DEFAULT'][$key]['red1']=$matches[2];  
$this->colours['DEFAULT'][$key]['green1']=$matches[3];  
$this->colours['DEFAULT'][$key]['blue1']=$matches[4];  
$this->colours['DEFAULT'][$key]['bottom']=-2;  
$this->colours['DEFAULT'][$key]['top']=-1;  
   
$linematched++; if(isset($matches[3]) && $matches[3] != '')
}  
   
if (($last_seen == 'NODE') && (preg_match(  
"/^\s*(LABELFONT|LABELFONTSHADOW|LABELBG|LABELOUTLINE)COLOR\s+(\d+)\s+(\d+)\s+(\d+)\s*$/i",  
$buffer,  
$matches)))  
{ {
$key=$matches[1]; $this->keysize[$whichkey] = $matches[3];
# print "Found NODE colour line for $key\n";  
$field=strtolower($matches[1]) . 'colour';  
$curnode->$field=array  
(  
$matches[2],  
$matches[3],  
$matches[4]  
);  
   
$linematched++;  
} }
  else
if (($last_seen == 'LINK') && (preg_match(  
"/^\s*(COMMENTFONT|BWBOX|BWFONT|BWOUTLINE|OUTLINE)COLOR\s+(\d+)\s+(\d+)\s+(\d+)\s*$/i",  
$buffer,  
$matches)))  
{ {
$key=$matches[1]; $this->keysize[$whichkey] = $this->keysize['DEFAULT'];
# print "Found LINK colour line for $key\n";  
$field=strtolower($matches[1]) . 'colour';  
$curlink->$field=array  
(  
$matches[2],  
$matches[3],  
$matches[4]  
);  
   
$linematched++;  
} }
   
if (($last_seen == 'NODE') && (preg_match( $linematched++;
"/^\s*(LABELFONTSHADOW|LABELBG|LABELOUTLINE)COLOR\s+none\s*$/i", }
$buffer,  
$matches)))  
{  
$key=$matches[1];  
# print "Found NODE non-colour line for $key\n";  
$field=strtolower($matches[1]) . 'colour';  
$curnode->$field=array  
(  
-1,  
-1,  
-1  
);  
   
$linematched++;  
} if (preg_match("/^\s*KILO\s+(\d+)\s*$/i", $buffer, $matches))
  {
  $this->kilo=$matches[1];
  # $this->defaultlink->owner->kilo=$matches[1];
  # $this->links['DEFAULT']=$matches[1];
  $linematched++;
  }
   
  if (preg_match(
  "/^\s*(TIME|TITLE|KEYBG|KEYTEXT|KEYOUTLINE|BG)COLOR\s+(\d+)\s+(\d+)\s+(\d+)\s*$/i",
  $buffer,
  $matches))
  {
  $key=$matches[1];
  # "Found colour line for $key\n";
  $this->colours['DEFAULT'][$key]['red1']=$matches[2];
  $this->colours['DEFAULT'][$key]['green1']=$matches[3];
  $this->colours['DEFAULT'][$key]['blue1']=$matches[4];
  $this->colours['DEFAULT'][$key]['bottom']=-2;
  $this->colours['DEFAULT'][$key]['top']=-1;
  $this->colours['DEFAULT'][$key]['special']=1;
   
if (($last_seen == 'LINK') && (preg_match( $linematched++;
"/^\s*(BWBOX|BWOUTLINE|OUTLINE)COLOR\s+none\s*$/i", $buffer, }
$matches)))  
{  
$key=$matches[1];  
# print "Found LINK non-colour line for $key\n";  
$field=strtolower($matches[1]) . 'colour';  
$curlink->$field=array  
(  
-1,  
-1,  
-1  
);  
   
$linematched++; if (($last_seen == 'NODE') && (preg_match(
} "/^\s*(AICONOUTLINE|AICONFILL|LABELFONT|LABELFONTSHADOW|LABELBG|LABELOUTLINE)COLOR\s+((\d+)\s+(\d+)\s+(\d+)|none|contrast|copy)\s*$/i",
  $buffer,
  $matches)))
  {
  $key=$matches[1];
  $field=strtolower($matches[1]) . 'colour';
  $val = strtolower($matches[2]);
   
if (preg_match("/^\s*HTMLSTYLE\s+(static|overlib)\s*$/i", $buffer, $matches)) if(isset($matches[3])) // this is a regular colour setting thing
{ {
$this->htmlstyle=$matches[1]; $curnode->$field=array( $matches[3],$matches[4],$matches[5]);
$linematched++; $linematched++;
} }
   
if ($last_seen == 'LINK' && preg_match( if($val == 'none' && ($matches[1]=='LABELFONTSHADOW' || $matches[1]=='LABELBG' || $matches[1]=='LABELOUTLINE' || $matches[1]=='AICONOUTLINE'))
"/^\s*ARROWSTYLE\s+(classic|compact)\s*$/i", $buffer, $matches))  
{ {
$curlink->arrowstyle=$matches[1]; $curnode->$field=array(-1,-1,-1);
$linematched++; $linematched++;
} }
   
if ($last_seen == 'LINK' && preg_match( if($val == 'contrast' && $matches[1]=='LABELFONT')
"/^\s*ARROWSTYLE\s+(\d+)\s+(\d+)\s*$/i", $buffer, $matches))  
{ {
$curlink->arrowstyle=$matches[1] . ' ' . $matches[2]; $curnode->$field=array(-3,-3,-3);
$linematched++; $linematched++;
} }
   
if ($last_seen == '---' && preg_match( if($matches[2] == 'copy' && $matches[1]=='AICONFILL')
"/^\s*ARROWSTYLE\s+(classic|compact)\s*$/i", $buffer, $matches))  
{ {
warn ("Global ARROWSTYLE is deprecated. Use LINK DEFAULT and ARROWSTYLE instead.\n"); $curnode->$field=array(-2,-2,-2);
$this->defaultlink->arrowstyle=$matches[1];  
$linematched++; $linematched++;
} }
  }
   
  if (($last_seen == 'LINK') && (preg_match(
  "/^\s*(COMMENTFONT|BWBOX|BWFONT|BWOUTLINE|OUTLINE)COLOR\s+((\d+)\s+(\d+)\s+(\d+)|none|contrast|copy)\s*$/i",
  $buffer,
  $matches)))
  {
  $key=$matches[1];
  $field=strtolower($matches[1]) . 'colour';
  $val = strtolower($matches[2]);
   
if (preg_match("/^\s*TITLE\s+(.*)\s*$/i", $buffer, $matches)) if(isset($matches[3])) // this is a regular colour setting thing
{ {
$this->title=$matches[1]; $curlink->$field=array( $matches[3],$matches[4],$matches[5]);
$linematched++; $linematched++;
} }
   
if (preg_match("/^\s*HTMLOUTPUTFILE\s+(.*)\s*$/i", $buffer, $matches)) if($val == 'none' && ($key=='BWBOX' || $key=='BWOUTLINE' || $key=='OUTLINE'))
{ {
$this->htmloutputfile=trim($matches[1]); // print "***********************************\n";
  $curlink->$field=array(-1,-1,-1);
$linematched++; $linematched++;
} }
   
if (preg_match("/^\s*IMAGEOUTPUTFILE\s+(.*)\s*$/i", $buffer, $matches)) if($val == 'contrast' && $key=='COMMENTFONT')
{ {
$this->imageoutputfile=trim($matches[1]); // print "***********************************\n";
  $curlink->$field=array(-3,-3,-3);
$linematched++; $linematched++;
} }
  }
   
if ($linematched == 0 && trim($buffer) != '') { warn if ($last_seen == 'LINK' && preg_match(
("Unrecognised config on line $linecount: $buffer"); } "/^\s*ARROWSTYLE\s+(\d+)\s+(\d+)\s*$/i", $buffer, $matches))
  {
  $curlink->arrowstyle=$matches[1] . ' ' . $matches[2];
  $linematched++;
  }
   
   
if ($linematched > 1) { warn if ($linematched == 0 && trim($buffer) != '') { warn
("Same line ($linecount) interpreted twice. This is a program error. Please report to Howie with your config!\nThe line was: $buffer"); ("Unrecognised config on line $linecount: $buffer\n"); }
}  
} // if blankline  
} // while  
   
if ($last_seen == "NODE") if ($linematched > 1) { warn
{ ("Same line ($linecount) interpreted twice. This is a program error. Please report to Howie with your config!\nThe line was: $buffer");
# $this->nodes[$curnode->name] = $curnode;  
if ($curnode->name == 'DEFAULT')  
{  
$this->defaultnode=$curnode;  
debug ("Saving Default Node: " . $curnode->name . "\n");  
} }
else } // if blankline
{ } // while
$this->nodes[$curnode->name]=$curnode;  
debug ("Saving Node: " . $curnode->name . "\n");  
}  
}  
   
if ($last_seen == "LINK") if(1==1)
{ {
# $this->links[$curlink->name] = $curlink; $this->ReadConfig_Commit($curobj);
if ($curlink->name == 'DEFAULT') }
{  
$this->defaultlink=$curlink;  
debug ("Saving Default Link: " . $curlink->name . "\n");  
}  
else  
{  
if (isset($curlink->a) && isset($curlink->b))  
{  
$this->links[$curlink->name]=$curlink;  
debug ("Saving Link: " . $curlink->name . "\n");  
}  
else { warn ("Dropping LINK " . $curlink->name . " - it hasn't got 2 NODES!"); }  
}  
}  
} // if $fd  
else else
{ {
warn ("Couldn't open config file $filename for reading\n"); if ($last_seen == "NODE")
return (FALSE); {
  $this->nodes[$curnode->name]=$curnode;
  debug ("Saving Node: " . $curnode->name . "\n");
  if($curnode->template == 'DEFAULT') $this->node_template_tree[ "DEFAULT" ][]= $curnode->name;
} }
   
fclose ($fd); if ($last_seen == "LINK")
  {
  if (isset($curlink->a) && isset($curlink->b))
  {
  $this->links[$curlink->name]=$curlink;
  debug ("Saving Link: " . $curlink->name . "\n");
  if($curlink->template == 'DEFAULT') $this->link_template_tree[ "DEFAULT" ][]= $curlink->name;
  }
  else { warn ("Dropping LINK " . $curlink->name . " - it hasn't got 2 NODES!"); }
  }
  }
   
   
  debug("ReadConfig has finished reading the config ($linecount lines)\n");
  debug("------------------------------------------\n");
   
// load some default colouring, otherwise it all goes wrong // load some default colouring, otherwise it all goes wrong
if ($scalesseen == 0) if ($scalesseen == 0)
{ {
debug ("Adding default SCALE set.\n"); debug ("Adding default SCALE colour set (no SCALE lines seen).\n");
$defaults=array $defaults=array
( (
'1_10' => array('bottom' => 1, 'top' => 10, 'red1' => 140, 'green1' => 0, 'blue1' => 255), '0_0' => array('bottom' => 0, 'top' => 0, 'red1' => 192, 'green1' => 192, 'blue1' => 192, 'special'=>0),
'10_25' => array('bottom' => 10, 'top' => 25, 'red1' => 32, 'green1' => 32, 'blue1' => 255), '0_1' => array('bottom' => 0, 'top' => 1, 'red1' => 255, 'green1' => 255, 'blue1' => 255, 'special'=>0),
'25_40' => array('bottom' => 25, 'top' => 40, 'red1' => 0, 'green1' => 192, 'blue1' => 255), '1_10' => array('bottom' => 1, 'top' => 10, 'red1' => 140, 'green1' => 0, 'blue1' => 255, 'special'=>0),
'40_55' => array('bottom' => 40, 'top' => 55, 'red1' => 0, 'green1' => 240, 'blue1' => 0), '10_25' => array('bottom' => 10, 'top' => 25, 'red1' => 32, 'green1' => 32, 'blue1' => 255, 'special'=>0),
'55_70' => array('bottom' => 55, 'top' => 70, 'red1' => 240, 'green1' => 240, 'blue1' => 0), '25_40' => array('bottom' => 25, 'top' => 40, 'red1' => 0, 'green1' => 192, 'blue1' => 255, 'special'=>0),
'70_85' => array('bottom' => 70, 'top' => 85, 'red1' => 255, 'green1' => 192, 'blue1' => 0), '40_55' => array('bottom' => 40, 'top' => 55, 'red1' => 0, 'green1' => 240, 'blue1' => 0, 'special'=>0),
'85_100' => array('bottom' => 85, 'top' => 100, 'red1' => 255, 'green1' => 0, 'blue1' => 0) '55_70' => array('bottom' => 55, 'top' => 70, 'red1' => 240, 'green1' => 240, 'blue1' => 0, 'special'=>0),
  '70_85' => array('bottom' => 70, 'top' => 85, 'red1' => 255, 'green1' => 192, 'blue1' => 0, 'special'=>0),
  '85_100' => array('bottom' => 85, 'top' => 100, 'red1' => 255, 'green1' => 0, 'blue1' => 0, 'special'=>0)
); );
   
foreach ($defaults as $key => $def) foreach ($defaults as $key => $def)
{ {
$this->colours['DEFAULT'][$key]=$def; $this->colours['DEFAULT'][$key]=$def;
  $this->colours['DEFAULT'][$key]['key']=$key;
$scalesseen++; $scalesseen++;
} }
  // we have a 0-0 line now, so we need to hide that.
  $this->add_hint("key_hidezero_DEFAULT",1);
} }
else { debug ("Already have $scalesseen scales, no defaults added.\n"); } else { debug ("Already have $scalesseen scales, no defaults added.\n"); }
   
$this->numscales['DEFAULT']=$scalesseen; $this->numscales['DEFAULT']=$scalesseen;
$this->configfile="$filename"; $this->configfile="$filename";
   
  if($this->has_overlibs && $this->htmlstyle == 'static')
  {
  warn("OVERLIBGRAPH is used, but HTMLSTYLE is static. This is probably wrong. [WMWARN41]\n");
  }
   
  debug("Building cache of z-layers and finalising bandwidth.\n");
   
  // $allitems = array_merge($this->links, $this->nodes);
   
  $allitems = array();
  foreach ($this->nodes as $node)
  {
  $allitems[] = $node;
  }
  foreach ($this->links as $link)
  {
  $allitems[] = $link;
  }
   
  # foreach ($allitems as &$item)
  foreach ($allitems as $ky=>$vl)
  {
  $item =& $allitems[$ky];
  $z = $item->zorder;
  if(!isset($this->seen_zlayers[$z]) || !is_array($this->seen_zlayers[$z]))
  {
  $this->seen_zlayers[$z]=array();
  }
  array_push($this->seen_zlayers[$z], $item);
   
  // while we're looping through, let's set the real bandwidths
  if($item->my_type() == "LINK")
  {
  $this->links[$item->name]->max_bandwidth_in = unformat_number($item->max_bandwidth_in_cfg, $this->kilo);
  $this->links[$item->name]->max_bandwidth_out = unformat_number($item->max_bandwidth_out_cfg, $this->kilo);
  }
  elseif($item->my_type() == "NODE")
  {
  $this->nodes[$item->name]->max_bandwidth_in = unformat_number($item->max_bandwidth_in_cfg, $this->kilo);
  $this->nodes[$item->name]->max_bandwidth_out = unformat_number($item->max_bandwidth_out_cfg, $this->kilo);
  }
  else
  {
  warn("Internal bug - found an item of type: ".$item->my_type()."\n");
  }
  // $item->max_bandwidth_in=unformat_number($item->max_bandwidth_in_cfg, $this->kilo);
  // $item->max_bandwidth_out=unformat_number($item->max_bandwidth_out_cfg, $this->kilo);
   
  debug (sprintf(" Setting bandwidth on ".$item->my_type()." $item->name (%s -> %d bps, %s -> %d bps, KILO = %d)\n", $item->max_bandwidth_in_cfg, $item->max_bandwidth_in, $item->max_bandwidth_out_cfg, $item->max_bandwidth_out, $this->kilo));
  }
   
  debug("Found ".sizeof($this->seen_zlayers)." z-layers including builtins (0,100).\n");
   
// calculate any relative positions here - that way, nothing else // calculate any relative positions here - that way, nothing else
// really needs to know about them // really needs to know about them
   
  debug("Resolving relative positions for NODEs...\n");
// safety net for cyclic dependencies // safety net for cyclic dependencies
$i=100; $i=100;
do do
{ {
$skipped = 0; $set=0; $skipped = 0; $set=0;
foreach ($this->nodes as $node) foreach ($this->nodes as $node)
{ {
if( ($node->relative_to != '') && (!$node->relative_resolved)) if( ($node->relative_to != '') && (!$node->relative_resolved))
{ {
debug("Resolving relative position for NODE ".$node->name." to ".$node->relative_to."\n"); debug("Resolving relative position for NODE ".$node->name." to ".$node->relative_to."\n");
if(array_key_exists($node->relative_to,$this->nodes)) if(array_key_exists($node->relative_to,$this->nodes))
{ {
   
// check if we are relative to another node which is in turn relative to something // check if we are relative to another node which is in turn relative to something
// we need to resolve that one before we can resolve this one! // we need to resolve that one before we can resolve this one!
if( ($this->nodes[$node->relative_to]->relative_to != '') && (!$this->nodes[$node->relative_to]->relative_resolved) ) if( ($this->nodes[$node->relative_to]->relative_to != '') && (!$this->nodes[$node->relative_to]->relative_resolved) )
{ {
debug("Skipping unresolved relative_to. Let's hope it's not a circular one\n"); debug("Skipping unresolved relative_to. Let's hope it's not a circular one\n");
$skipped++; $skipped++;
} }
else else
{ {
// save the relative coords, so that WriteConfig can work $rx = $this->nodes[$node->relative_to]->x;
// resolve the relative stuff $ry = $this->nodes[$node->relative_to]->y;
   
$newpos_x = $this->nodes[$node->relative_to]->x + $this->nodes[$node->name]->x; if($node->polar)
$newpos_y = $this->nodes[$node->relative_to]->y + $this->nodes[$node->name]->y; {
debug("->$newpos_x,$newpos_y\n"); // treat this one as a POLAR relative coordinate.
$this->nodes[$node->name]->x = $newpos_x; // - draw rings around a node!
$this->nodes[$node->name]->y = $newpos_y; $angle = $node->x;
$this->nodes[$node->name]->relative_resolved=TRUE; $distance = $node->y;
$set++; $newpos_x = $rx + $distance * sin(deg2rad($angle));
  $newpos_y = $ry - $distance * cos(deg2rad($angle));
  debug("->$newpos_x,$newpos_y\n");
  $this->nodes[$node->name]->x = $newpos_x;
  $this->nodes[$node->name]->y = $newpos_y;
  $this->nodes[$node->name]->relative_resolved=TRUE;
  $set++;
  }
  else
  {
   
  // save the relative coords, so that WriteConfig can work
  // resolve the relative stuff
   
  $newpos_x = $rx + $this->nodes[$node->name]->x;
  $newpos_y = $ry + $this->nodes[$node->name]->y;
  debug("->$newpos_x,$newpos_y\n");
  $this->nodes[$node->name]->x = $newpos_x;
  $this->nodes[$node->name]->y = $newpos_y;
  $this->nodes[$node->name]->relative_resolved=TRUE;
  $set++;
  }
} }
} }
else else
{ {
warn("NODE ".$node->name." has a relative position to an unknown node!\n"); warn("NODE ".$node->name." has a relative position to an unknown node! [WMWARN10]\n");
} }
} }
} }
debug("Cycle $i - set $set and Skipped $skipped for unresolved dependencies\n"); debug("Relative Positions Cycle $i - set $set and Skipped $skipped for unresolved dependencies\n");
$i--; $i--;
} while( ($set>0) && ($i!=0) ); } while( ($set>0) && ($i!=0) );
   
if($skipped>0) if($skipped>0)
{ {
warn("There are Circular dependencies in relative POSITION lines for $skipped nodes.\n"); warn("There are Circular dependencies in relative POSITION lines for $skipped nodes. [WMWARN11]\n");
} }
   
# warn("---\n\nDEFAULT NODE AGAIN::".var_dump($this->defaultnode->hints)."::\n"); debug("-----------------------------------\n");
#warn("DEFAULT NOW SAYS ".$this->defaultnode->hints['sigdigits']."\n");  
#warn("North NOW SAYS ".$this->nodes['North']->hints['sigdigits']."\n");  
   
   
debug("Running Pre-Processing Plugins...\n"); debug("Running Pre-Processing Plugins...\n");
foreach ($this->preprocessclasses as $pre_class) foreach ($this->preprocessclasses as $pre_class)
{ {
debug("Running $pre_class"."->run()\n"); debug("Running $pre_class"."->run()\n");
# call_user_func(array($pre_class, 'run'), $this); $this->plugins['pre'][$pre_class]->run($this);
call_user_func_array(array($pre_class, 'run'), array(&$this));  
   
} }
debug("Finished Pre-Processing Plugins...\n"); debug("Finished Pre-Processing Plugins...\n");
   
return (TRUE); return (TRUE);
} }
   
function WriteConfig($filename) function ReadConfig_Commit(&$curobj)
{ {
global $WEATHERMAP_VERSION; if(is_null($curobj)) return;
   
  $last_seen = $curobj->my_type();
   
$fd=fopen($filename, "w"); // first, save the previous item, before starting work on the new one
$output=""; if ($last_seen == "NODE")
   
if ($fd)  
{ {
$output.="# Automatically generated by php-weathermap v$WEATHERMAP_VERSION\n\n"; $this->nodes[$curobj->name]=$curobj;
  debug ("Saving Node: " . $curobj->name . "\n");
  if($curobj->template == 'DEFAULT') $this->node_template_tree[ "DEFAULT" ][]= $curobj->name;
  }
   
if ($this->background != '') { $output.="BACKGROUND " . $this->background . "\n"; } if ($last_seen == "LINK")
  {
  if (isset($curobj->a) && isset($curobj->b))
  {
  $this->links[$curobj->name]=$curobj;
  debug ("Saving Link: " . $curobj->name . "\n");
  }
else else
{ {
$output.="WIDTH " . $this->width . "\n"; $this->links[$curobj->name]=$curobj;
$output.="HEIGHT " . $this->height . "\n"; debug ("Saving Template-Only Link: " . $curobj->name . "\n");
} }
  if($curobj->template == 'DEFAULT') $this->link_template_tree[ "DEFAULT" ][]= $curobj->name;
  }
  }
   
if ($this->htmlstyle != $this->inherit_fieldlist['htmlstyle']) function WriteConfig($filename)
{ $output.="HTMLSTYLE " . $this->htmlstyle . "\n"; } {
  global $WEATHERMAP_VERSION;
   
# if( $this->keystyle != $this->inherit_fieldlist['keystyle']) { $output .= "KEYSTYLE ".$this->keystyle."\n"; } $fd=fopen($filename, "w");
if ($this->kilo != $this->inherit_fieldlist['kilo']) { $output.="KILO " . $this->kilo . "\n"; } $output="";
   
$output.="\n"; if ($fd)
  {
  $output.="# Automatically generated by php-weathermap v$WEATHERMAP_VERSION\n\n";
   
if (count($this->fonts) > 0) if (count($this->fonts) > 0)
{ {
foreach ($this->fonts as $fontnumber => $font) foreach ($this->fonts as $fontnumber => $font)
{ {
Line 4436... Line 3154...
} }
   
$output.="\n"; $output.="\n";
} }
   
if ($this->keyfont != $this->inherit_fieldlist['keyfont']) $basic_params = array(
{ $output.="KEYFONT " . $this->keyfont . "\n"; } array('background','BACKGROUND',CONFIG_TYPE_LITERAL),
  array('width','WIDTH',CONFIG_TYPE_LITERAL),
  array('height','HEIGHT',CONFIG_TYPE_LITERAL),
  array('htmlstyle','HTMLSTYLE',CONFIG_TYPE_LITERAL),
  array('kilo','KILO',CONFIG_TYPE_LITERAL),
  array('keyfont','KEYFONT',CONFIG_TYPE_LITERAL),
  array('timefont','TIMEFONT',CONFIG_TYPE_LITERAL),
  array('titlefont','TITLEFONT',CONFIG_TYPE_LITERAL),
  array('title','TITLE',CONFIG_TYPE_LITERAL),
  array('htmloutputfile','HTMLOUTPUTFILE',CONFIG_TYPE_LITERAL),
  array('htmlstylesheet','HTMLSTYLESHEET',CONFIG_TYPE_LITERAL),
  array('imageuri','IMAGEURI',CONFIG_TYPE_LITERAL),
  array('imageoutputfile','IMAGEOUTPUTFILE',CONFIG_TYPE_LITERAL)
  );
   
if ($this->timefont != $this->inherit_fieldlist['timefont']) foreach ($basic_params as $param)
{ $output.="TIMEFONT " . $this->timefont . "\n"; } {
  $field = $param[0];
  $keyword = $param[1];
   
if ($this->titlefont != $this->inherit_fieldlist['titlefont']) if ($this->$field != $this->inherit_fieldlist[$field])
{ $output.="TITLEFONT " . $this->titlefont . "\n"; } {
  if($param[2] == CONFIG_TYPE_COLOR) $output.="$keyword " . render_colour($this->$field) . "\n";
if (trim($this->title) != $this->inherit_fieldlist['title']) if($param[2] == CONFIG_TYPE_LITERAL) $output.="$keyword " . $this->$field . "\n";
{ $output.="TITLE " . $this->title . "\n"; } }
  }
if (trim($this->htmloutputfile) != $this->inherit_fieldlist['htmloutputfile'])  
{ $output.="HTMLOUTPUTFILE " . $this->htmloutputfile . "\n"; }  
   
if (trim($this->imageoutputfile) != $this->inherit_fieldlist['imageoutputfile'])  
{ $output.="IMAGEOUTPUTFILE " . $this->imageoutputfile . "\n"; }  
   
if (($this->timex != $this->inherit_fieldlist['timex']) if (($this->timex != $this->inherit_fieldlist['timex'])
|| ($this->timey != $this->inherit_fieldlist['timey']) || ($this->timey != $this->inherit_fieldlist['timey'])
|| ($this->stamptext != $this->inherit_fieldlist['stamptext'])) || ($this->stamptext != $this->inherit_fieldlist['stamptext']))
$output.="TIMEPOS " . $this->timex . " " . $this->timey . " " . $this->stamptext . "\n"; $output.="TIMEPOS " . $this->timex . " " . $this->timey . " " . $this->stamptext . "\n";
   
  if (($this->mintimex != $this->inherit_fieldlist['mintimex'])
  || ($this->mintimey != $this->inherit_fieldlist['mintimey'])
  || ($this->minstamptext != $this->inherit_fieldlist['minstamptext']))
  $output.="MINTIMEPOS " . $this->mintimex . " " . $this->mintimey . " " . $this->minstamptext . "\n";
   
  if (($this->maxtimex != $this->inherit_fieldlist['maxtimex'])
  || ($this->maxtimey != $this->inherit_fieldlist['maxtimey'])
  || ($this->maxstamptext != $this->inherit_fieldlist['maxstamptext']))
  $output.="MAXTIMEPOS " . $this->maxtimex . " " . $this->maxtimey . " " . $this->maxstamptext . "\n";
   
if (($this->titlex != $this->inherit_fieldlist['titlex']) if (($this->titlex != $this->inherit_fieldlist['titlex'])
|| ($this->titley != $this->inherit_fieldlist['titley'])) || ($this->titley != $this->inherit_fieldlist['titley']))
$output.="TITLEPOS " . $this->titlex . " " . $this->titley . "\n"; $output.="TITLEPOS " . $this->titlex . " " . $this->titley . "\n";
   
$output.="\n"; $output.="\n";
   
foreach ($this->colours as $scalename=>$colours) foreach ($this->colours as $scalename=>$colours)
{ {
// not all keys will have keypos but if they do, then all three vars should be defined // not all keys will have keypos but if they do, then all three vars should be defined
if ( (isset($this->keyx[$scalename])) if ( (isset($this->keyx[$scalename])) && (isset($this->keyy[$scalename])) && (isset($this->keytext[$scalename]))
|| ($this->keytext[$scalename] != $this->inherit_fieldlist['keytext']) && (($this->keytext[$scalename] != $this->inherit_fieldlist['keytext'])
|| ($this->keyx[$scalename] != $this->inherit_fieldlist['keyx']) || ($this->keyx[$scalename] != $this->inherit_fieldlist['keyx'])
|| ($this->keyy[$scalename] != $this->inherit_fieldlist['keyy'])) || ($this->keyy[$scalename] != $this->inherit_fieldlist['keyy'])))
  {
  // sometimes a scale exists but without defaults. A proper scale object would sort this out...
  if($this->keyx[$scalename] == '') { $this->keyx[$scalename] = -1; }
  if($this->keyy[$scalename] == '') { $this->keyy[$scalename] = -1; }
   
$output.="KEYPOS " . $scalename." ". $this->keyx[$scalename] . " " . $this->keyy[$scalename] . " " . $this->keytext[$scalename] . "\n"; $output.="KEYPOS " . $scalename." ". $this->keyx[$scalename] . " " . $this->keyy[$scalename] . " " . $this->keytext[$scalename] . "\n";
  }
   
if ( (isset($this->keystyle[$scalename])) && ($this->keystyle[$scalename] != $this->inherit_fieldlist['keystyle']['DEFAULT']) ) if ( (isset($this->keystyle[$scalename])) && ($this->keystyle[$scalename] != $this->inherit_fieldlist['keystyle']['DEFAULT']) )
{ {
$extra=''; $extra='';
if ( (isset($this->keysize[$scalename])) && ($this->keysize[$scalename] != $this->inherit_fieldlist['keysize']['DEFAULT']) ) if ( (isset($this->keysize[$scalename])) && ($this->keysize[$scalename] != $this->inherit_fieldlist['keysize']['DEFAULT']) )
{ {
$extra = " ".$this->keysize[$scalename]; $extra = " ".$this->keysize[$scalename];
} }
$output.="KEYSTYLE " . $scalename." ". $this->keystyle[$scalename] . $extra . "\n"; $output.="KEYSTYLE " . $scalename." ". $this->keystyle[$scalename] . $extra . "\n";
} }
  $locale = localeconv();
  $decimal_point = $locale['decimal_point'];
   
foreach ($colours as $k => $colour) foreach ($colours as $k => $colour)
{ {
if ($colour['top'] >= 0) if (!isset($colour['special']) || ! $colour['special'] )
{ {
$top = rtrim(rtrim(sprintf("%f",$colour['top']),"0"),"."); $top = rtrim(rtrim(sprintf("%f",$colour['top']),"0"),$decimal_point);
$bottom= rtrim(rtrim(sprintf("%f",$colour['bottom']),"0"),"."); $bottom= rtrim(rtrim(sprintf("%f",$colour['bottom']),"0"),$decimal_point);
   
if (!isset($colour['red2'])) if ($bottom > 1000) {
$output.=sprintf("SCALE %s %s %s %d %d %d\n", $scalename, $bottom = nice_bandwidth($colour['bottom'], $this->kilo);
  }
   
  if ($top > 1000) {
  $top = nice_bandwidth($colour['top'], $this->kilo);
  }
   
  $tag = (isset($colour['tag'])? $colour['tag']:'');
   
  if( ($colour['red1'] == -1) && ($colour['green1'] == -1) && ($colour['blue1'] == -1))
  {
  $output.=sprintf("SCALE %s %-4s %-4s none %s\n", $scalename,
  $bottom, $top, $tag);
  }
  elseif (!isset($colour['red2']))
  {
  $output.=sprintf("SCALE %s %-4s %-4s %3d %3d %3d %s\n", $scalename,
$bottom, $top, $bottom, $top,
$colour['red1'], $colour['green1'], $colour['blue1']); $colour['red1'], $colour['green1'], $colour['blue1'],$tag);
  }
else else
$output.=sprintf("SCALE %s %s %s %d %d %d %d %d %d\n", $scalename, {
  $output.=sprintf("SCALE %s %-4s %-4s %3d %3d %3d %3d %3d %3d %s\n", $scalename,
$bottom, $top, $bottom, $top,
$colour['red1'], $colour['red1'],
$colour['green1'], $colour['blue1'], $colour['green1'], $colour['blue1'],
$colour['red2'], $colour['green2'], $colour['red2'], $colour['green2'],
$colour['blue2']); $colour['blue2'], $tag);
  }
} }
else { $output.=sprintf("%sCOLOR %d %d %d\n", $k, $colour['red1'], $colour['green1'], else { $output.=sprintf("%sCOLOR %d %d %d\n", $k, $colour['red1'], $colour['green1'],
$colour['blue1']); } $colour['blue1']); }
} }
$output .= "\n"; $output .= "\n";
} }
   
foreach ($this->hints as $hintname=>$hint) foreach ($this->hints as $hintname=>$hint)
{ {
$output .= "SET $hintname $hint\n"; $output .= "SET $hintname $hint\n";
  }
   
  // this doesn't really work right, but let's try anyway
  if($this->has_includes)
  {
  $output .= "\n# Included files\n";
  foreach ($this->included_files as $ifile)
  {
  $output .= "INCLUDE $ifile\n";
  }
} }
   
$output.="\n# End of global section\n\n# DEFAULT definitions:\n"; $output.="\n# End of global section\n\n";
   
fwrite($fd, $output); fwrite($fd, $output);
   
$this->defaultnode->WriteConfig($fd); ## fwrite($fd,$this->nodes['DEFAULT']->WriteConfig());
$this->defaultlink->WriteConfig($fd); ## fwrite($fd,$this->links['DEFAULT']->WriteConfig());
   
fwrite($fd, "\n# End of DEFAULTS section\n\n# Node definitions:\n"); # fwrite($fd, "\n\n# Node definitions:\n");
   
foreach ($this->nodes as $node) { $node->WriteConfig($fd); } foreach (array("template","normal") as $which)
  {
  if($which == "template") fwrite($fd,"\n# TEMPLATE-only NODEs:\n");
  if($which == "normal") fwrite($fd,"\n# regular NODEs:\n");
   
  foreach ($this->nodes as $node)
  {
  if(!preg_match("/^::\s/",$node->name))
  {
  if($node->defined_in == $this->configfile)
  {
   
fwrite($fd, "\n# End of NODE section\n\n# Link definitions:\n"); if($which=="template" && $node->x === NULL) { debug("TEMPLATE\n"); fwrite($fd,$node->WriteConfig()); }
  if($which=="normal" && $node->x !== NULL) { fwrite($fd,$node->WriteConfig()); }
  }
  }
  }
   
  if($which == "template") fwrite($fd,"\n# TEMPLATE-only LINKs:\n");
  if($which == "normal") fwrite($fd,"\n# regular LINKs:\n");
   
  foreach ($this->links as $link)
  {
  if(!preg_match("/^::\s/",$link->name))
  {
  if($link->defined_in == $this->configfile)
  {
  if($which=="template" && $link->a === NULL) fwrite($fd,$link->WriteConfig());
  if($which=="normal" && $link->a !== NULL) fwrite($fd,$link->WriteConfig());
  }
  }
  }
  }
   
foreach ($this->links as $link) { $link->WriteConfig($fd); } fwrite($fd, "\n\n# That's All Folks!\n");
   
fwrite($fd, "\n# End of LINK section\n\n# That's All Folks!\n"); fclose($fd);
} }
else else
{ {
warn ("Couldn't open config file $filename for writing"); warn ("Couldn't open config file $filename for writing");
return (FALSE); return (FALSE);
Line 4544... Line 3349...
   
// pre-allocate colour slots for the colours used by the arrows // pre-allocate colour slots for the colours used by the arrows
// this way, it's the pretty icons that suffer if there aren't enough colours, and // this way, it's the pretty icons that suffer if there aren't enough colours, and
// not the actual useful data // not the actual useful data
// we skip any gradient scales // we skip any gradient scales
function AllocateScaleColours($im) function AllocateScaleColours($im,$refname='gdref1')
{ {
# $colours=$this->colours['DEFAULT']; # $colours=$this->colours['DEFAULT'];
foreach ($this->colours as $scalename=>$colours) foreach ($this->colours as $scalename=>$colours)
{ {
foreach ($colours as $key => $colour) foreach ($colours as $key => $colour)
{ {
if (!isset($this->colours[$scalename][$key]['red2'])) if ( (!isset($this->colours[$scalename][$key]['red2']) ) && (!isset( $this->colours[$scalename][$key][$refname] )) )
{ {
$r=$colour['red1']; $r=$colour['red1'];
$g=$colour['green1']; $g=$colour['green1'];
$b=$colour['blue1']; $b=$colour['blue1'];
debug ("AllocateScaleColours: $scalename $key ($r,$g,$b)\n"); debug ("AllocateScaleColours: $scalename/$refname $key ($r,$g,$b)\n");
$this->colours[$scalename][$key]['gdref1']=myimagecolorallocate($im, $r, $g, $b); $this->colours[$scalename][$key][$refname]=myimagecolorallocate($im, $r, $g, $b);
} }
} }
} }
} }
   
function DrawMap($filename = '', $thumbnailfile = '', $thumbnailmax = 250, $withnodes = TRUE) function DrawMap($filename = '', $thumbnailfile = '', $thumbnailmax = 250, $withnodes = TRUE, $use_via_overlay = FALSE, $use_rel_overlay=FALSE)
{ {
  debug("Trace: DrawMap()\n");
  metadump("# start",true);
$bgimage=NULL; $bgimage=NULL;
  if($this->configfile != "")
  {
  $this->cachefile_version = crc32(file_get_contents($this->configfile));
  }
  else
  {
  $this->cachefile_version = crc32("........");
  }
   
debug("Running Post-Processing Plugins...\n"); debug("Running Post-Processing Plugins...\n");
foreach ($this->postprocessclasses as $post_class) foreach ($this->postprocessclasses as $post_class)
{ {
debug("Running $post_class"."->run()\n"); debug("Running $post_class"."->run()\n");
call_user_func_array(array($post_class, 'run'), array(&$this)); //call_user_func_array(array($post_class, 'run'), array(&$this));
  $this->plugins['post'][$post_class]->run($this);
   
} }
debug("Finished Post-Processing Plugins...\n"); debug("Finished Post-Processing Plugins...\n");
   
  debug("=====================================\n");
  debug("Start of Map Drawing\n");
   
$this->datestamp = strftime($this->stamptext, time()); $this->datestamp = strftime($this->stamptext, time());
   
// do the basic prep work // do the basic prep work
if ($this->background != '') if ($this->background != '')
Line 4599... Line 3418...
else { warn else { warn
("Your background image file could not be read. Check the filename, and permissions, for " ("Your background image file could not be read. Check the filename, and permissions, for "
. $this->background . "\n"); } . $this->background . "\n"); }
} }
   
$image=imagecreatetruecolor($this->width, $this->height); $image=wimagecreatetruecolor($this->width, $this->height);
   
# $image = imagecreate($this->width, $this->height); # $image = imagecreate($this->width, $this->height);
if (!$image) { warn if (!$image) { warn
("Couldn't create output image in memory (" . $this->width . "x" . $this->height . ")."); } ("Couldn't create output image in memory (" . $this->width . "x" . $this->height . ")."); }
else else
Line 4622... Line 3441...
$this->selected=myimagecolorallocate($image, 255, 0, 0); // for selections in the editor $this->selected=myimagecolorallocate($image, 255, 0, 0); // for selections in the editor
   
$this->AllocateScaleColours($image); $this->AllocateScaleColours($image);
   
// fill with background colour anyway, in case the background image failed to load // fill with background colour anyway, in case the background image failed to load
imagefilledrectangle($image, 0, 0, $this->width, $this->height, $this->colours['DEFAULT']['BG']['gdref1']); wimagefilledrectangle($image, 0, 0, $this->width, $this->height, $this->colours['DEFAULT']['BG']['gdref1']);
   
if ($bgimage) if ($bgimage)
{ {
imagecopy($image, $bgimage, 0, 0, 0, 0, $this->width, $this->height); imagecopy($image, $bgimage, 0, 0, 0, 0, $this->width, $this->height);
imagedestroy ($bgimage); imagedestroy ($bgimage);
} }
   
// Now it's time to draw a map // Now it's time to draw a map
   
# foreach ($this->nodes as $node) { $this->nodes[$node->name]->calc_size(); } // do the node rendering stuff first, regardless of where they are actually drawn.
  // this is so we can get the size of the nodes, which links will need if they use offsets
  foreach ($this->nodes as $node)
  {
  // don't try and draw template nodes
  debug("Pre-rendering ".$node->name." to get bounding boxes.\n");
  if(!is_null($node->x)) $this->nodes[$node->name]->pre_render($image, $this);
  }
   
  $all_layers = array_keys($this->seen_zlayers);
  sort($all_layers);
   
foreach ($this->nodes as $node) { $node->pre_render($image, $this); } foreach ($all_layers as $z)
foreach ($this->links as $link) { $link->Draw($image, $this); } {
  $z_items = $this->seen_zlayers[$z];
  debug("Drawing layer $z\n");
  // all the map 'furniture' is fixed at z=1000
  if($z==1000)
  {
  foreach ($this->colours as $scalename=>$colours)
  {
  debug("Drawing KEY for $scalename if necessary.\n");
   
if($withnodes) if( (isset($this->numscales[$scalename])) && (isset($this->keyx[$scalename])) && ($this->keyx[$scalename] >= 0) && ($this->keyy[$scalename] >= 0) )
  {
  if($this->keystyle[$scalename]=='classic') $this->DrawLegend_Classic($image,$scalename,FALSE);
  if($this->keystyle[$scalename]=='horizontal') $this->DrawLegend_Horizontal($image,$scalename,$this->keysize[$scalename]);
  if($this->keystyle[$scalename]=='vertical') $this->DrawLegend_Vertical($image,$scalename,$this->keysize[$scalename]);
  if($this->keystyle[$scalename]=='inverted') $this->DrawLegend_Vertical($image,$scalename,$this->keysize[$scalename],true);
  if($this->keystyle[$scalename]=='tags') $this->DrawLegend_Classic($image,$scalename,TRUE);
  }
  }
   
  $this->DrawTimestamp($image, $this->timefont, $this->colours['DEFAULT']['TIME']['gdref1']);
  if(! is_null($this->min_data_time))
  {
  $this->DrawTimestamp($image, $this->timefont, $this->colours['DEFAULT']['TIME']['gdref1'],"MIN");
  $this->DrawTimestamp($image, $this->timefont, $this->colours['DEFAULT']['TIME']['gdref1'],"MAX");
  }
  $this->DrawTitle($image, $this->titlefont, $this->colours['DEFAULT']['TITLE']['gdref1']);
  }
   
  if(is_array($z_items))
  {
  foreach($z_items as $it)
  {
  if(strtolower(get_class($it))=='weathermaplink')
  {
  // only draw LINKs if they have NODES defined (not templates)
  // (also, check if the link still exists - if this is in the editor, it may have been deleted by now)
  if ( isset($this->links[$it->name]) && isset($it->a) && isset($it->b))
  {
  debug("Drawing LINK ".$it->name."\n");
  $this->links[$it->name]->Draw($image, $this);
  }
  }
  if(strtolower(get_class($it))=='weathermapnode')
  {
  // if(!is_null($it->x)) $it->pre_render($image, $this);
  if($withnodes)
  {
  // don't try and draw template nodes
  if( isset($this->nodes[$it->name]) && !is_null($it->x))
  {
  # print "::".get_class($it)."\n";
  debug("Drawing NODE ".$it->name."\n");
  $this->nodes[$it->name]->NewDraw($image, $this);
  $ii=0;
  foreach($this->nodes[$it->name]->boundingboxes as $bbox)
  {
  # $areaname = "NODE:" . $it->name . ':'.$ii;
  $areaname = "NODE:N". $it->id . ":" . $ii;
  $this->imap->addArea("Rectangle", $areaname, '', $bbox);
  debug("Adding imagemap area");
  $ii++;
  }
  debug("Added $ii bounding boxes too\n");
  }
  }
  }
  }
  }
  }
   
  $overlay = myimagecolorallocate($image, 200, 0, 0);
   
  // for the editor, we can optionally overlay some other stuff
  if($this->context == 'editor')
  {
  if($use_rel_overlay)
{ {
  # $overlay = myimagecolorallocate($image, 200, 0, 0);
   
  // first, we can show relatively positioned NODEs
foreach ($this->nodes as $node) { foreach ($this->nodes as $node) {
$node->NewDraw($image, $this); if($node->relative_to != '')
# debug($node->name.": ".var_dump($node->notes)."\n"); {
  $rel_x = $this->nodes[$node->relative_to]->x;
  $rel_y = $this->nodes[$node->relative_to]->y;
  imagearc($image,$node->x, $node->y,
  15,15,0,360,$overlay);
  imagearc($image,$node->x, $node->y,
  16,16,0,360,$overlay);
   
  imageline($image,$node->x, $node->y,
  $rel_x, $rel_y, $overlay);
  }
} }
# debug("DEFAULT: ".var_dump($this->defaultnode->notes)."\n");  
} }
   
foreach ($this->colours as $scalename=>$colours) if($use_via_overlay)
{ {
debug("Drawing KEY for $scalename if necessary.\n"); // then overlay VIAs, so they can be seen
  foreach($this->links as $link)
if( (isset($this->numscales[$scalename])) && (isset($this->keyx[$scalename])) && ($this->keyx[$scalename] >= 0) && ($this->keyy[$scalename] >= 0) )  
{ {
if($this->keystyle[$scalename]=='classic') $this->DrawLegend_Classic($image,$scalename); foreach ($link->vialist as $via)
if($this->keystyle[$scalename]=='horizontal') $this->DrawLegend_Horizontal($image,$scalename,$this->keysize[$scalename]); {
if($this->keystyle[$scalename]=='vertical') $this->DrawLegend_Vertical($image,$scalename,$this->keysize[$scalename]); if(isset($via[2]))
  {
  $x = $this->nodes[$via[2]]->x + $via[0];
  $y = $this->nodes[$via[2]]->y + $via[1];
  }
  else
  {
  $x = $via[0];
  $y = $via[1];
  }
  imagearc($image, $x,$y, 10,10,0,360,$overlay);
  imagearc($image, $x,$y, 12,12,0,360,$overlay);
  }
} }
} }
  }
   
$this->DrawTimestamp($image, $this->timefont, $this->colours['DEFAULT']['TIME']['gdref1']); #$this->myimagestring($image, 3, 200, 100, "Test 1\nLine 2", $overlay,0);
$this->DrawTitle($image, $this->titlefont, $this->colours['DEFAULT']['TITLE']['gdref1']);  
  # $this->myimagestring($image, 30, 100, 100, "Test 1\nLine 2", $overlay,0);
# $this->DrawNINK($image,300,300,48); #$this->myimagestring($image, 30, 200, 200, "Test 1\nLine 2", $overlay,45);
   
// Ready to output the results... // Ready to output the results...
   
if($filename == 'null') if($filename == 'null')
{ {
Line 4677... Line 3604...
else { else {
$result = FALSE; $result = FALSE;
$functions = TRUE; $functions = TRUE;
if(function_exists('imagejpeg') && preg_match("/\.jpg/i",$filename)) if(function_exists('imagejpeg') && preg_match("/\.jpg/i",$filename))
{ {
debug("Writing JPEG file\n"); debug("Writing JPEG file to $filename\n");
$result = imagejpeg($image, $filename); $result = imagejpeg($image, $filename);
} }
elseif(function_exists('imagegif') && preg_match("/\.gif/i",$filename)) elseif(function_exists('imagegif') && preg_match("/\.gif/i",$filename))
{ {
debug("Writing GIF file\n"); debug("Writing GIF file to $filename\n");
$result = imagegif($image, $filename); $result = imagegif($image, $filename);
} }
elseif(function_exists('imagepng') && preg_match("/\.png/i",$filename)) elseif(function_exists('imagepng') && preg_match("/\.png/i",$filename))
{ {
debug("Writing PNG file\n"); debug("Writing PNG file to $filename\n");
$result = imagepng($image, $filename); $result = imagepng($image, $filename);
} }
else else
{ {
warn("Failed to write map image. No function existed for the image format you requested.\n"); warn("Failed to write map image. No function existed for the image format you requested. [WMWARN12]\n");
$functions = FALSE; $functions = FALSE;
} }
   
if(($result==FALSE) && ($functions==TRUE)) if(($result==FALSE) && ($functions==TRUE))
{ {
if(file_exists($filename)) if(file_exists($filename))
{ {
warn("Failed to overwrite existing image file $filename - permissions of existing file are wrong?"); warn("Failed to overwrite existing image file $filename - permissions of existing file are wrong? [WMWARN13]");
} }
else else
{ {
warn("Failed to create image file $filename - permissions of output directory are wrong?"); warn("Failed to create image file $filename - permissions of output directory are wrong? [WMWARN14]");
} }
} }
} }
} }
   
if($this->context == 'editor2') if($this->context == 'editor2')
{ {
$cachefile = $this->cachefolder.DIRECTORY_SEPARATOR.dechex(crc32($this->configfile))."_bg.png"; $cachefile = $this->cachefolder.DIRECTORY_SEPARATOR.dechex(crc32($this->configfile))."_bg.".$this->cachefile_version.".png";
imagepng($image, $cachefile); imagepng($image, $cachefile);
$cacheuri = $this->cachefolder.'/'.dechex(crc32($this->configfile))."_bg.png"; $cacheuri = $this->cachefolder.'/'.dechex(crc32($this->configfile))."_bg.".$this->cachefile_version.".png";
$this->mapcache = $cacheuri; $this->mapcache = $cacheuri;
} }
   
if (function_exists('imagecopyresampled')) if (function_exists('imagecopyresampled'))
{ {
Line 4727... Line 3654...
{ {
$result = FALSE; $result = FALSE;
if ($this->width > $this->height) { $factor=($thumbnailmax / $this->width); } if ($this->width > $this->height) { $factor=($thumbnailmax / $this->width); }
else { $factor=($thumbnailmax / $this->height); } else { $factor=($thumbnailmax / $this->height); }
   
$twidth=$this->width * $factor; $this->thumb_width = $this->width * $factor;
$theight=$this->height * $factor; $this->thumb_height = $this->height * $factor;
   
$imagethumb=imagecreatetruecolor($twidth, $theight); $imagethumb=imagecreatetruecolor($this->thumb_width, $this->thumb_height);
imagecopyresampled($imagethumb, $image, 0, 0, 0, 0, $twidth, $theight, imagecopyresampled($imagethumb, $image, 0, 0, 0, 0, $this->thumb_width, $this->thumb_height,
$this->width, $this->height); $this->width, $this->height);
$result = imagepng($imagethumb, $thumbnailfile); $result = imagepng($imagethumb, $thumbnailfile);
imagedestroy($imagethumb); imagedestroy($imagethumb);
   
   
   
if(($result==FALSE)) if(($result==FALSE))
{ {
if(file_exists($filename)) if(file_exists($filename))
{ {
warn("Failed to overwrite existing image file $filename - permissions of existing file are wrong?"); warn("Failed to overwrite existing image file $filename - permissions of existing file are wrong? [WMWARN15]");
} }
else else
{ {
warn("Failed to create image file $filename - permissions of output directory are wrong?"); warn("Failed to create image file $filename - permissions of output directory are wrong? [WMWARN16]");
} }
} }
} }
} }
else else
{ {
warn("Skipping thumbnail creation, since we don't have the necessary function."); warn("Skipping thumbnail creation, since we don't have the necessary function. [WMWARN17]");
} }
imagedestroy ($image); imagedestroy ($image);
} }
} }
   
Line 4768... Line 3697...
   
} }
   
function PreloadMapHTML() function PreloadMapHTML()
{ {
if ($this->htmlstyle == "overlib") debug("Trace: PreloadMapHTML()\n");
{  
// onmouseover="return overlib('<img src=graph.png>',DELAY,250,CAPTION,'$caption');" onmouseout="return nd();" // onmouseover="return overlib('<img src=graph.png>',DELAY,250,CAPTION,'$caption');" onmouseout="return nd();"
   
  // find the middle of the map
$center_x=$this->width / 2; $center_x=$this->width / 2;
$center_y=$this->height / 2; $center_y=$this->height / 2;
   
foreach ($this->links as $link) // loop through everything. Figure out along the way if it's a node or a link
  $allitems = array(&$this->nodes, &$this->links);
  reset($allitems);
   
  while( list($kk,) = each($allitems))
{ {
if ( ($link->overliburl != '') || ($link->notestext != '') ) unset($objects);
  # $objects = &$this->links;
  $objects = &$allitems[$kk];
   
  reset($objects);
  while (list($k,) = each($objects))
{ {
# $overlibhtml = "onmouseover=\"return overlib('&lt;img src=".$link->overliburl."&gt;',DELAY,250,CAPTION,'".$link->name."');\" onmouseout=\"return nd();\""; unset($myobj);
$a_x=$link->a->x; $myobj = &$objects[$k];
$b_x=$link->b->x;  
$mid_x=($a_x + $b_x) / 2;  
$a_y=$link->a->y;  
$b_y=$link->b->y;  
$mid_y=($a_y + $b_y) / 2;  
   
# debug($link->overlibwidth."---".$link->overlibheight."---\n"); $type = $myobj->my_type();
  $prefix = substr($type,0,1);
$left="";  
$above="";  
   
if ($link->overlibwidth != 0) $dirs = array();
  //print "\n\nConsidering a $type - ".$myobj->name.".\n";
  if($type == 'LINK') $dirs = array(IN=>array(0,2), OUT=>array(1,3));
  if($type == 'NODE') $dirs = array(IN=>array(0,1,2,3));
   
  // check to see if any of the relevant things have a value
  $change = "";
  foreach ($dirs as $d=>$parts)
{ {
$left="WIDTH," . $link->overlibwidth . ","; //print "$d - ".join(" ",$parts)."\n";
  $change .= join('',$myobj->overliburl[$d]);
if ($mid_x > $center_x) $change .= $myobj->notestext[$d];
$left.="LEFT,";  
} }
   
if ($link->overlibheight != 0) if ($this->htmlstyle == "overlib")
{ {
$above="HEIGHT," . $link->overlibheight . ","; //print "CHANGE: $change\n";
   
if ($mid_y > $center_y) // skip all this if it's a template node
$above.="ABOVE,"; if($type=='LINK' && ! isset($myobj->a->name)) { $change = ''; }
} if($type=='NODE' && ! isset($myobj->x)) { $change = ''; }
   
$caption = ($link->overlibcaption != '' ? $link->overlibcaption : $link->name); if($change != '')
$caption = $this->ProcessString($caption,$link); {
  //print "Something to be done.\n";
  if($type=='NODE')
  {
  $mid_x = $myobj->x;
  $mid_y = $myobj->y;
  }
  if($type=='LINK')
  {
  $a_x = $this->nodes[$myobj->a->name]->x;
  $a_y = $this->nodes[$myobj->a->name]->y;
   
$overlibhtml = "onmouseover=\"return overlib('"; $b_x = $this->nodes[$myobj->b->name]->x;
if($link->overliburl != '') $b_y = $this->nodes[$myobj->b->name]->y;
{  
$overlibhtml .= "&lt;img src=" . $this->ProcessString($link->overliburl,$link) . "&gt;";  
}  
if($link->notestext != '')  
{  
# put in a linebreak if there was an image AND notes  
if($link->overliburl != '') $overlibhtml .= '&lt;br /&gt;';  
$note = $this->ProcessString($link->notestext,$link);  
$note = htmlspecialchars($note, ENT_NOQUOTES);  
$note=str_replace("'", "\\&apos;", $note);  
$note=str_replace('"', "&quot;", $note);  
$overlibhtml .= $note;  
}  
$overlibhtml .= "',DELAY,250,${left}${above}CAPTION,'" . $caption  
. "');\" onmouseout=\"return nd();\"";  
   
$this->imap->setProp("extrahtml", $overlibhtml, "LINK:" . $link->name); $mid_x=($a_x + $b_x) / 2;
} $mid_y=($a_y + $b_y) / 2;
} }
  $left=""; $above="";
  $img_extra = "";
   
  if ($myobj->overlibwidth != 0)
  {
  $left="WIDTH," . $myobj->overlibwidth . ",";
  $img_extra .= " WIDTH=$myobj->overlibwidth";
   
foreach ($this->nodes as $node) if ($mid_x > $center_x) $left.="LEFT,";
{ }
if ( ($node->overliburl != '') || ($node->notestext != '') )  
{ if ($myobj->overlibheight != 0)
# $overlibhtml = "onmouseover=\"return overlib('&lt;img src=".$node->overliburl."&gt;',DELAY,250,CAPTION,'".$node->name."');\" onmouseout=\"return nd();\""; {
  $above="HEIGHT," . $myobj->overlibheight . ",";
  $img_extra .= " HEIGHT=$myobj->overlibheight";
   
debug ($node->overlibwidth . "---" . $node->overlibheight . "---\n"); if ($mid_y > $center_y) $above.="ABOVE,";
  }
   
  foreach ($dirs as $dir=>$parts)
  {
  $caption = ($myobj->overlibcaption[$dir] != '' ? $myobj->overlibcaption[$dir] : $myobj->name);
  $caption = $this->ProcessString($caption,$myobj);
   
$left=""; $overlibhtml = "onmouseover=\"return overlib('";
$above="";  
   
if ($node->overlibwidth != 0) $n = 0;
  if(sizeof($myobj->overliburl[$dir]) > 0)
  {
  // print "ARRAY:".is_array($link->overliburl[$dir])."\n";
  foreach ($myobj->overliburl[$dir] as $url)
  {
  if($n>0) { $overlibhtml .= '&lt;br /&gt;'; }
  $overlibhtml .= "&lt;img $img_extra src=" . $this->ProcessString($url,$myobj) . "&gt;";
  $n++;
  }
  }
  # print "Added $n for $dir\n";
  if(trim($myobj->notestext[$dir]) != '')
  {
  # put in a linebreak if there was an image AND notes
  if($n>0) $overlibhtml .= '&lt;br /&gt;';
  $note = $this->ProcessString($myobj->notestext[$dir],$myobj);
  $note = htmlspecialchars($note, ENT_NOQUOTES);
  $note=str_replace("'", "\\&apos;", $note);
  $note=str_replace('"', "&quot;", $note);
  $overlibhtml .= $note;
  }
  $overlibhtml .= "',DELAY,250,${left}${above}CAPTION,'" . $caption
  . "');\" onmouseout=\"return nd();\"";
   
  foreach ($parts as $part)
  {
  $areaname = $type.":" . $prefix . $myobj->id. ":" . $part;
  //print "INFOURL for $areaname - ";
   
  $this->imap->setProp("extrahtml", $overlibhtml, $areaname);
  }
  }
  } // if change
  } // overlib?
   
  // now look at inforurls
  foreach ($dirs as $dir=>$parts)
{ {
$left="WIDTH," . $node->overlibwidth . ","; foreach ($parts as $part)
  {
if ($node->x > $center_x) # $areaname = $type.":" . $myobj->name . ":" . $part;
$left.="LEFT,"; $areaname = $type.":" . $prefix . $myobj->id. ":" . $part;
  //print "INFOURL for $areaname - ";
   
  if ( ($this->htmlstyle != 'editor') && ($myobj->infourl[$dir] != '') ) {
  $this->imap->setProp("href", $this->ProcessString($myobj->infourl[$dir],$myobj), $areaname);
  //print "Setting.\n";
  }
  else
  {
  //print "NOT Setting.\n";
  }
  }
} }
   
if ($node->overlibheight != 0)  
{  
$above="HEIGHT," . $node->overlibheight . ",";  
   
if ($node->y > $center_y)  
$above.="ABOVE,";  
}  
   
$caption = ($node->overlibcaption != '' ? $node->overlibcaption : $node->name);  
$caption = $this->ProcessString($caption,$node);  
   
$overlibhtml = "onmouseover=\"return overlib('";  
if($node->overliburl != '')  
{  
$overlibhtml .= "&lt;img src=" . $this->ProcessString($node->overliburl,$node) . "&gt;";  
}  
if($node->notestext != '')  
{  
# put in a linebreak if there was an image AND notes  
if($node->overliburl != '') $overlibhtml .= '&lt;br /&gt;';  
$note = $this->ProcessString($node->notestext,$node);  
$note = htmlspecialchars($note, ENT_NOQUOTES);  
$note=str_replace("'", "\\&apos;", $note);  
$note=str_replace('"', "&quot;", $note);  
$overlibhtml .= $note;  
}  
$overlibhtml .= "',DELAY,250,${left}${above}CAPTION,'" . $caption  
. "');\" onmouseout=\"return nd();\"";  
   
# $overlibhtml .= " onclick=\"return overlib('Some Test or other',CAPTION,'MENU',)\"";  
   
$this->imap->setProp("extrahtml", $overlibhtml, "NODE:" . $node->name);  
} }
} }
}  
   
if ($this->htmlstyle == 'editor')  
{  
foreach ($this->links as $link) {  
# $this->imap->setProp("href","#","LINK:".$link->name);  
# $this->imap->setProp("extrahtml","onclick=\"click_handler('link','".$link->name."');\"","LINK:".$link->name);  
}  
   
foreach ($this->nodes as $node) {  
# $this->imap->setProp("href","#","NODE:".$node->name);  
# $this->imap->setProp("extrahtml","onclick=\"click_handler('node','".$node->name."');\"","NODE:".$node->name);  
# $this->imap->setProp("extrahtml","onclick=\"alert('".$node->name."');\"","NODE:".$node->name);  
}  
}  
else  
{  
foreach ($this->links as $link)  
{  
if ($link->infourl != '') { $this->imap->setProp("href", $this->ProcessString($link->infourl,$link),  
"LINK:" . $link->name); }  
}  
   
foreach ($this->nodes as $node)  
{  
if ($node->infourl != '') { $this->imap->setProp("href", $this->ProcessString($node->infourl,$node),  
"NODE:" . $node->name); }  
}  
}  
} }
   
function asJS() function asJS()
{ {
$js=''; $js='';
   
$js.="var Links = new Array();\n"; $js .= "var Links = new Array();\n";
$js.=$this->defaultlink->asJS(); $js .= "var LinkIDs = new Array();\n";
  # $js.=$this->defaultlink->asJS();
   
foreach ($this->links as $link) { $js.=$link->asJS(); } foreach ($this->links as $link) { $js.=$link->asJS(); }
   
$js.="var Nodes = new Array();\n"; $js .= "var Nodes = new Array();\n";
$js.=$this->defaultnode->asJS(); $js .= "var NodeIDs = new Array();\n";
  # $js.=$this->defaultnode->asJS();
   
foreach ($this->nodes as $node) { $js.=$node->asJS(); } foreach ($this->nodes as $node) { $js.=$node->asJS(); }
   
return $js; return $js;
} }
Line 4940... Line 3876...
{ {
$json = ''; $json = '';
   
$json .= "{ \n"; $json .= "{ \n";
   
$json .= "'map': { \n"; $json .= "\"map\": { \n";
foreach (array_keys($this->inherit_fieldlist)as $fld) foreach (array_keys($this->inherit_fieldlist)as $fld)
{ {
$json .= js_escape($fld).": "; $json .= js_escape($fld).": ";
$json .= js_escape($this->$fld); $json .= js_escape($this->$fld);
$json .= ",\n"; $json .= ",\n";
} }
$json = rtrim($json,", \n"); $json = rtrim($json,", \n");
$json .= "\n},\n"; $json .= "\n},\n";
   
$json .= "'nodes': {\n"; $json .= "\"nodes\": {\n";
$json .= $this->defaultnode->asJSON(); $json .= $this->defaultnode->asJSON();
foreach ($this->nodes as $node) { $json .= $node->asJSON(); } foreach ($this->nodes as $node) { $json .= $node->asJSON(); }
$json = rtrim($json,", \n"); $json = rtrim($json,", \n");
$json .= "\n},\n"; $json .= "\n},\n";
   
   
   
$json .= "'links': {\n"; $json .= "\"links\": {\n";
$json .= $this->defaultlink->asJSON(); $json .= $this->defaultlink->asJSON();
foreach ($this->links as $link) { $json .= $link->asJSON(); } foreach ($this->links as $link) { $json .= $link->asJSON(); }
$json = rtrim($json,", \n"); $json = rtrim($json,", \n");
$json .= "\n},\n"; $json .= "\n},\n";
   
Line 4972... Line 3908...
$json .= ",\n"; $json .= ",\n";
$json .= $this->imap->subJSON("LINK:"); $json .= $this->imap->subJSON("LINK:");
$json .= "\n]\n"; $json .= "\n]\n";
$json .= "\n"; $json .= "\n";
   
$json .= ", valid: 1}\n"; $json .= ", 'valid': 1}\n";
   
return($json); return($json);
} }
   
// imagemapname is a parameter, so we can stack up several maps in the Cacti plugin // This method MUST run *after* DrawMap. It relies on DrawMap to call the map-drawing bits
  // which will populate the ImageMap with regions.
  //
  // imagemapname is a parameter, so we can stack up several maps in the Cacti plugin with their own imagemaps
function MakeHTML($imagemapname = "weathermap_imap") function MakeHTML($imagemapname = "weathermap_imap")
{ {
  debug("Trace: MakeHTML()\n");
  // PreloadMapHTML fills in the ImageMap info, ready for the HTML to be created.
$this->PreloadMapHTML(); $this->PreloadMapHTML();
   
$html=''; $html='';
   
$html .= '<div class="weathermapimage" style="margin-left: 10px; margin-right: auto; width: '.$this->width.'px;" >'; $html .= '<div class="weathermapimage" style="margin-left: auto; margin-right: auto; width: '.$this->width.'px;" >';
if ($this->imageuri != '') { $html.=sprintf( if ( $this->imageuri != '') {
'<img src="%s" width="%s" height="%s" border="0" usemap="#' $html.=sprintf(
. $imagemapname . '" alt="network weathermap" />', '<img id="wmapimage" src="%s" width="%d" height="%d" border="0" usemap="#%s"',
$this->imageuri, $this->imageuri,
$this->width, $this->width,
$this->height); } $this->height,
else { $html.=sprintf( $imagemapname
'<img src="%s" width="%s" height="%s" border="0" usemap="#' . $imagemapname );
. '" alt="network weathermap" />', //$html .= 'alt="network weathermap" ';
$this->imagefile, $html .= '/>';
$this->width, }
$this->height); } else {
  $html.=sprintf(
  '<img id="wmapimage" src="%s" width="%d" height="%d" border="0" usemap="#%s"',
  $this->imagefile,
  $this->width,
  $this->height,
  $imagemapname
  );
  //$html .= 'alt="network weathermap" ';
  $html .= '/>';
  }
$html .= '</div>'; $html .= '</div>';
   
$html.='<map name="' . $imagemapname . '" id="' . $imagemapname . '">' . "\n"; $html .= $this->SortedImagemap($imagemapname);
   
$html.=$this->imap->subHTML("NODE:",true); return ($html);
$html.=$this->imap->subHTML("LINK:",true); }
   
$html.='</map>'; function SortedImagemap($imagemapname)
  {
  $html='<map name="' . $imagemapname . '" id="' . $imagemapname . '">';
   
return ($html); # $html.=$this->imap->subHTML("NODE:",true);
  # $html.=$this->imap->subHTML("LINK:",true);
   
  $all_layers = array_keys($this->seen_zlayers);
  rsort($all_layers);
   
  debug("Starting to dump imagemap in reverse Z-order...\n");
  // this is not precisely efficient, but it'll get us going
  // XXX - get Imagemap to store Z order, or map items to store the imagemap
  foreach ($all_layers as $z)
  {
  debug("Writing HTML for layer $z\n");
  $z_items = $this->seen_zlayers[$z];
  if(is_array($z_items))
  {
  debug(" Found things for layer $z\n");
   
  // at z=1000, the legends and timestamps live
  if($z == 1000) {
  debug(" Builtins fit here.\n");
  $html .= $this->imap->subHTML("LEGEND:",true,($this->context != 'editor'));
  $html .= $this->imap->subHTML("TIMESTAMP",true,($this->context != 'editor'));
  }
   
  foreach($z_items as $it)
  {
  # print " " . $it->name . "\n";
  if($it->name != 'DEFAULT' && $it->name != ":: DEFAULT ::")
  {
  $name = "";
  if(strtolower(get_class($it))=='weathermaplink') $name = "LINK:L";
  if(strtolower(get_class($it))=='weathermapnode') $name = "NODE:N";
  $name .= $it->id . ":";
  debug(" Writing $name from imagemap\n");
  // skip the linkless areas if we are in the editor - they're redundant
  $html .= $this->imap->subHTML($name,true,($this->context != 'editor'));
  }
  }
  }
  }
   
  $html.='</map>';
   
  return($html);
} }
   
// update any editor cache files. // update any editor cache files.
// if the config file is newer than the cache files, or $agelimit seconds have passed, // if the config file is newer than the cache files, or $agelimit seconds have passed,
// then write new stuff, otherwise just return. // then write new stuff, otherwise just return.
// ALWAYS deletes files in the cache folder older than $agelimit, also! // ALWAYS deletes files in the cache folder older than $agelimit, also!
function CacheUpdate($agelimit=600) function CacheUpdate($agelimit=600)
{ {
  global $weathermap_lazycounter;
   
$cachefolder = $this->cachefolder; $cachefolder = $this->cachefolder;
$configchanged = filemtime($this->configfile ); $configchanged = filemtime($this->configfile );
// make a unique, but safe, prefix for all cachefiles related to this map config // make a unique, but safe, prefix for all cachefiles related to this map config
// we use CRC32 because it makes for a shorter filename, and collisions aren't the end of the world. // we use CRC32 because it makes for a shorter filename, and collisions aren't the end of the world.
$cacheprefix = dechex(crc32($this->configfile)); $cacheprefix = dechex(crc32($this->configfile));
Line 5035... Line 4033...
   
if(is_file($realfile) && ( preg_match('/^'.$cacheprefix.'/',$file) )) if(is_file($realfile) && ( preg_match('/^'.$cacheprefix.'/',$file) ))
// if (is_file($realfile) ) // if (is_file($realfile) )
{ {
debug("$realfile\n"); debug("$realfile\n");
if( (filemtime($realfile) < $configchanged) || ((time() - filemtime($realfile)) > $agelimit) ) if( (filemtime($realfile) < $configchanged) || ((time() - filemtime($realfile)) > $agelimit) )
{ {
debug("Cache: deleting $realfile\n"); debug("Cache: deleting $realfile\n");
unlink($realfile); unlink($realfile);
} }
} }
} }
   
closedir ($dh); closedir ($dh);
   
foreach ($this->nodes as $node) foreach ($this->nodes as $node)
{ {
if(isset($node->image)) if(isset($node->image))
Line 5054... Line 4051...
$nodefile = $cacheprefix."_".dechex(crc32($node->name)).".png"; $nodefile = $cacheprefix."_".dechex(crc32($node->name)).".png";
$this->nodes[$node->name]->cachefile = $nodefile; $this->nodes[$node->name]->cachefile = $nodefile;
imagepng($node->image,$cachefolder.DIRECTORY_SEPARATOR.$nodefile); imagepng($node->image,$cachefolder.DIRECTORY_SEPARATOR.$nodefile);
} }
} }
   
  foreach ($this->keyimage as $key=>$image)
  {
  $scalefile = $cacheprefix."_scale_".dechex(crc32($key)).".png";
  $this->keycache[$key] = $scalefile;
  imagepng($image,$cachefolder.DIRECTORY_SEPARATOR.$scalefile);
  }
   
   
$json = ""; $json = "";
$fd = fopen($cachefolder.DIRECTORY_SEPARATOR.$cacheprefix."_map.json","w"); $fd = fopen($cachefolder.DIRECTORY_SEPARATOR.$cacheprefix."_map.json","w");
foreach (array_keys($this->inherit_fieldlist)as $fld) foreach (array_keys($this->inherit_fieldlist)as $fld)
{ {
Line 5066... Line 4071...
$json .= ",\n"; $json .= ",\n";
} }
$json = rtrim($json,", \n"); $json = rtrim($json,", \n");
fputs($fd,$json); fputs($fd,$json);
fclose($fd); fclose($fd);
   
  $json = "";
  $fd = fopen($cachefolder.DIRECTORY_SEPARATOR.$cacheprefix."_tree.json","w");
  $id = 10; // first ID for user-supplied thing
   
  $json .= "{ id: 1, text: 'SCALEs'\n, children: [\n";
  foreach ($this->colours as $scalename=>$colours)
  {
  $json .= "{ id: " . $id++ . ", text:" . js_escape($scalename) . ", leaf: true }, \n";
  }
  $json = rtrim($json,", \n");
  $json .= "]},\n";
   
  $json .= "{ id: 2, text: 'FONTs',\n children: [\n";
  foreach ($this->fonts as $fontnumber => $font)
  {
  if ($font->type == 'truetype')
  $json .= sprintf("{ id: %d, text: %s, leaf: true}, \n", $id++, js_escape("Font $fontnumber (TT)"));
   
  if ($font->type == 'gd')
  $json .= sprintf("{ id: %d, text: %s, leaf: true}, \n", $id++, js_escape("Font $fontnumber (GD)"));
  }
  $json = rtrim($json,", \n");
  $json .= "]},\n";
   
  $json .= "{ id: 3, text: 'NODEs',\n children: [\n";
  $json .= "{ id: ". $id++ . ", text: 'DEFAULT', children: [\n";
   
  $weathemap_lazycounter = $id;
  // pass the list of subordinate nodes to the recursive tree function
  $json .= $this->MakeTemplateTree( $this->node_template_tree );
  $id = $weathermap_lazycounter;
   
  $json = rtrim($json,", \n");
  $json .= "]} ]},\n";
   
  $json .= "{ id: 4, text: 'LINKs',\n children: [\n";
  $json .= "{ id: ". $id++ . ", text: 'DEFAULT', children: [\n";
  $weathemap_lazycounter = $id;
  $json .= $this->MakeTemplateTree( $this->link_template_tree );
  $id = $weathermap_lazycounter;
  $json = rtrim($json,", \n");
  $json .= "]} ]}\n";
   
  fputs($fd,"[". $json . "]");
  fclose($fd);
   
$fd = fopen($cachefolder.DIRECTORY_SEPARATOR.$cacheprefix."_nodes.json","w"); $fd = fopen($cachefolder.DIRECTORY_SEPARATOR.$cacheprefix."_nodes.json","w");
$json = $this->defaultnode->asJSON(); $json = "";
foreach ($this->nodes as $node) { $json .= $node->asJSON(); } // $json = $this->defaultnode->asJSON(TRUE);
  foreach ($this->nodes as $node) { $json .= $node->asJSON(TRUE); }
$json = rtrim($json,", \n"); $json = rtrim($json,", \n");
fputs($fd,$json); fputs($fd,$json);
fclose($fd); fclose($fd);
   
  $fd = fopen($cachefolder.DIRECTORY_SEPARATOR.$cacheprefix."_nodes_lite.json","w");
  $json = "";
  // $json = $this->defaultnode->asJSON(FALSE);
  foreach ($this->nodes as $node) { $json .= $node->asJSON(FALSE); }
  $json = rtrim($json,", \n");
  fputs($fd,$json);
  fclose($fd);
   
   
   
$fd = fopen($cachefolder.DIRECTORY_SEPARATOR.$cacheprefix."_links.json","w"); $fd = fopen($cachefolder.DIRECTORY_SEPARATOR.$cacheprefix."_links.json","w");
$json = $this->defaultlink->asJSON(); $json = "";
foreach ($this->links as $link) { $json .= $link->asJSON(); } // $json = $this->defaultlink->asJSON(TRUE);
  foreach ($this->links as $link) { $json .= $link->asJSON(TRUE); }
$json = rtrim($json,", \n"); $json = rtrim($json,", \n");
fputs($fd,$json); fputs($fd,$json);
fclose($fd); fclose($fd);
   
  $fd = fopen($cachefolder.DIRECTORY_SEPARATOR.$cacheprefix."_links_lite.json","w");
  $json = "";
  // $json = $this->defaultlink->asJSON(FALSE);
  foreach ($this->links as $link) { $json .= $link->asJSON(FALSE); }
  $json = rtrim($json,", \n");
  fputs($fd,$json);
  fclose($fd);
   
  $fd = fopen($cachefolder.DIRECTORY_SEPARATOR.$cacheprefix."_imaphtml.json","w");
  $json = $this->imap->subHTML("LINK:");
  fputs($fd,$json);
  fclose($fd);
   
   
$fd = fopen($cachefolder.DIRECTORY_SEPARATOR.$cacheprefix."_imap.json","w"); $fd = fopen($cachefolder.DIRECTORY_SEPARATOR.$cacheprefix."_imap.json","w");
$json = ''; $json = '';
$nodejson = trim($this->imap->subJSON("NODE:")); $nodejson = trim($this->imap->subJSON("NODE:"));
if($nodejson != '') if($nodejson != '')
Line 5097... Line 4174...
fclose($fd); fclose($fd);
   
} }
else { debug("Couldn't read cache folder.\n"); } else { debug("Couldn't read cache folder.\n"); }
} }
   
  function MakeTemplateTree( &$tree_list, $startpoint="DEFAULT")
  {
  global $weathermap_lazycounter;
   
  $output = "";
  foreach ($tree_list[$startpoint] as $subnode)
  {
  $output .= "{ id: " . $weathermap_lazycounter++ . ", text: " . js_escape($subnode);
  if( isset($tree_list[$subnode]))
  {
  $output .= ", children: [ \n";
  $output .= $this->MakeTemplateTree($tree_list, $subnode);
  $output = rtrim($output,", \n");
  $output .= "] \n";
  }
  else
  {
  $output .= ", leaf: true ";
  }
  $output .= "}, \n";
  }
   
  return($output);
  }
   
  function DumpStats($filename="")
  {
  $report = "Feature Statistics:\n\n";
  foreach ($this->usage_stats as $key=>$val)
  {
  $report .= sprintf("%70s => %d\n",$key,$val);
  }
   
  if($filename == "") print $report;
  }
   
}; };
// vim:ts=4:sw=4: // vim:ts=4:sw=4:
?> ?>
   

Powered by WebSVN 2.2.1