<?php
/*
* weathermap5rrd - configfile.php
*
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Copyright (C)
* 2007 Zdenek Styblik <hide@address.com>
*
* The Initial Developer of the Original Code is
* 2007 Zdenek Styblik <hide@address.com>
*
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* Authors:
* Zdenek Styblik ~ 2007-2008
*
*/
class config_file
{
# xml-2-obj
public $config; /* stdclass */
# default config file
public $configfile = 'weathermap.xml';
# xpath thru xml version of config file
private $xpath; /* DOMXPath */
# init
function __construct($file = null)
{
if (!is_null($file) && !empty($file))
{
$this->configfile = $file;
} /* !is_null($file) */
$this->auto_index = -1;
$this->config = new StdClass;
$this->config->debug = new StdClass;
/* arrow width */
if (!defined('ARROW_WIDTH'))
define('ARROW_WIDTH', 4);
/* draw grid(yes) */
if (!defined('DRAWGRID'))
define('DRAWGRID', 1);
/* font size */
if (!defined('FONT'))
define('FONT', 3);
/* default image height */
if (!defined('HEIGHT'))
define('HEIGHT', 480);
/* inodes to edit */
if (!defined('INODES'))
define('INODES', 5);
/*refresh map every 60s */
if (!defined('REFRESH'))
define('REFRESH', 60);
/* type of javascript library used for
* rollover above map elements
*/
if (!defined('ROLLOVERLIB'))
define('ROLLOVERLIB','overlib');
/* == 5 minutes */
if (!defined('RRDSTEP'))
define('RRDSTEP', 300);
/* default image width */
if (!defined('WIDTH'))
define('WIDTH', 640);
}
# clean-up
function __destruct()
{
unset($this->xpath);
unset($this->config);
}
# desc: reads xml file defined by $this->configfile;
# @return: bool;
public function readconfig()
{
if (file_exists("{$this->configfile}") && is_readable("{$this->configfile}"))
{
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
if ($doc->load($this->configfile))
{
$this->xpath = new DOMXPath($doc);
return true;
}
else
{
return false;
} /* not $result */
}
else
{
return false;
} /* file_exists */
}
# desc: save xml config
# @return: bool;
public function writeconfig($xml)
{
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
$doc->loadXML($xml);
$result = $doc->save($this->configfile);
if ($result == false)
{
return false;
}
else
{
return true;
} /* not $result */
}
# @return: const(RRDSTEP);
public function getRRDStep()
{
return RRDSTEP;
}
# @return: const(ARROW_WIDTH);
public function getArrowWidth()
{
return ARROW_WIDTH;
}
# @return: const(INODES);
public function getINODESCount()
{
return INODES;
}
# @return: const(FONT);
public function getFontSize()
{
return FONT;
}
# @return: const(ROLLOVERLIB)
public function getRollOverLib()
{
return ROLLOVERLIB;
}
# @return: obj;
public function getDefaultConfig()
{
$config = new StdClass;
$config->arrowwidth = ARROW_WIDTH;
# image size
$config->width = WIDTH;
$config->height = HEIGHT;
# font
$config->font = FONT;
# legend
$config->legend = new StdClass;
$config->legend->x = 50;
$config->legend->y = 80;
$config->legend->legendstyle = "original";
# title
$config->title = new StdClass;
$config->title->x = 50;
$config->title->y = 40;
$config->title->text = "WeatherMap5RRD-default";
## background
$config->title->bg = new StdClass;
$config->title->bg->r = 255;
$config->title->bg->g = 255;
$config->title->bg->b = 255;
## foreground
$config->title->fg = new StdClass;
$config->title->fg->r = 0;
$config->title->fg->g = 0;
$config->title->fg->b = 0;
# scale
$config->autoscale = new StdClass;
$config->autoscale->nodivisions = 10;
$config->autoscale->rstart = 24;
$config->autoscale->rend = 0;
$config->autoscale->gstart = 232;
$config->autoscale->gend = 0;
$config->autoscale->bstart = 2;
$config->autoscale->bend = 0;
return $config;
}
# desc: fetch doggie; returns xml-in->obj;
# @return: $obj;
public function getConfig()
{
$config = $this->config;
$this->getGlobal('arrowwidth', $config);
$this->getGlobal('background', $config);
$this->getGlobal('drawgrid', $config);
$this->getGlobal('font', $config);
$this->getGlobal('height', $config);
$this->getGlobal('htmlfile', $config);
$this->getGlobal('ipcheck', $config);
$this->getGlobal('outputfile', $config);
$this->getGlobal('refresh', $config);
$this->getGlobal('width', $config);
$config->legend = $this->getLegend();
$config->title = $this->getTitle();
$config->autoscale = $this->getAutoScale();
$config->scale = $this->getScale();
$config->nodes = $this->getNodes();
$config->links = $this->getLinks();
// final check
if ($config->scale == NULL && $config->autoscale == NULL)
{
$config->autoscale = $this->defaultAutoscale();
unset($config->scale);
} /* $config->scale */
if (isset($config->scale) && $config->scale == NULL)
unset($config->scale);
if (isset($config->autoscale) && $config->autoscale == NULL)
unset($config->autoscale);
if (!isset($config->arrowwidth) || !is_numeric($config->arrowwidth))
$config->arrowwidth = ARROW_WIDTH;
if (!isset($config->drawgrid) || !is_numeric($config->drawgrid))
$config->drawgrid = DRAWGRID;
if (!isset($config->font) || !is_numeric($config->font))
$config->font = FONT;
if (!isset($config->height) || !is_numeric($config->height))
$config->height = HEIGHT;
if (!isset($config->refresh) || !is_numeric($config->refresh))
$config->refresh = REFRESH;
if (!isset($config->width) || !is_numeric($config->width))
$config->width = WIDTH;
return $config;
}
# desc: returns node defined right in xml tree;
# @return: $obj;
public function getGlobal($node = null, $obj = null)
{
$node = preg_replace('/^\s+/', '', $node);
$node = preg_replace('/\s+$/', '', $node);
$node = preg_replace('/\s+/', '', $node);
if (!is_null($node) && !empty($node) && is_object($obj))
{
$result1 = $this->xpath->query("/root/{$node}");
foreach ($result1 as $element)
{
$obj->{$node} = $element->nodeValue;
}
} /* !is_null($node) */
return $obj;
}
# desc: returns links with its properties;
# @return: $obj;
public function getLinks()
{
// '/root/links/child::*[group="foo"]' - for the future
$result1 = $this->xpath->query('/root/links/*');
if ($result1->length > 0)
{
$links = new StdClass;
foreach($result1 as $xmlnode)
{
$linkname = preg_replace('/^\s+/', '', $xmlnode->tagName);
$linkname = preg_replace('/\s+$/', '', $linkname);
$linkname = preg_replace('/\s+/', '', $linkname);
if (isset($links->{$linkname}))
{
/* duplicate nodename */
}
else
{
$links->{$linkname} = new StdClass;
$link = $links->{$linkname};
$link->name = $linkname;
foreach ($this->xpath->query('*', $xmlnode) as $element)
{
switch ($element->nodeName)
{
case 'arrow':
$link->arrow = $element->nodeValue;
break;
case 'bandwidth':
if (is_numeric($link->bandwidth))
{
$link->bandwidth = $element->nodeValue;
}
else
{
$link->bandwidth = 0;
} /* not is_numeric($link->bandwidth) */
break;
case 'displayvalue':
if ($element->nodeValue == 1)
$link->displayvalue = 1;
break;
case 'group':
$link->group = $element->nodeValue;
break;
case 'from':
$link->from = $element->nodeValue;
break;
case 'inodegroup':
$link->inodegroup = $element->nodeValue;
break;
case 'inpos':
if (is_numeric($element->nodeValue))
$link->inpos = $element->nodeValue;
break;
case 'internodedisplay':
if ($element->nodeValue == 1)
$link->internodedisplay = 1;
break;
case 'internodedisplaymid':
if ($element->nodeValue == 1)
$link->internodedisplaymid = 1;
break;
case 'internodedisplaynum':
if ($element->nodeValue == 1)
$link->internodedisplaynum = 1;
break;
case 'linkoverlibgraph':
$link->linkoverlibgraph = $element->nodeValue;
break;
case 'linkinfourl':
$link->linkinfourl = $element->nodeValue;
break;
case 'nodes':
$link->nodes = $element->nodeValue;
break;
case 'outpos':
if (is_numeric($element->nodeValue))
$link->outpos = $element->nodeValue;
break;
case 'ratein':
if (is_numeric($element->nodeValue))
$link->ratein = $element->nodeValue;
break;
case 'rateout':
if (is_numeric($element->nodeValue))
$link->rateout = $element->nodeValue;
break;
case 'target':
$link->target = $element->nodeValue;
break;
case 'targetin':
$link->targetin = $element->nodeValue;
break;
case 'targetout':
$link->targetout = $element->nodeValue;
break;
case 'to':
$link->to = $element->nodeValue;
break;
case 'unit':
$link->unit = $element->nodeValue;
break;
} /* switch $element->nodeName */
} /* foreach $this->xpath->query */
// now query for internodes, if any
$result2 = $this->xpath->query('internode', $xmlnode);
$j = $result2->length;
for ($i = 1; $i <= $j; $i++)
{
$x = null;
$y = null;
foreach ($this->xpath->query("internode[{$i}]/*", $xmlnode) as $inode)
{
switch ($inode->nodeName)
{
case 'x':
if (is_numeric($inode->nodeValue))
$x = $inode->nodeValue;
break;
case 'y':
if (is_numeric($inode->nodeValue))
$y = $inode->nodeValue;
break;
} /* switch */
} /* foreach */
if (is_numeric($x) && is_numeric($y))
{
$link->internode[$i] = new StdClass;
$link->internode[$i]->x = $x;
$link->internode[$i]->y = $y;
} /* is_numeric($x) */
} /* for i = 1 */
// unset
unset($result2);
} /* not isset($links->{$linkname}) */
} /* foreach $result1 as $xmlnode */
unset($result1);
return $links;
}
else
{
unset($result1);
return NULL;
} /* $result1->length > 0 */
}
# desc: returns nodes with its properties;
# @return: $obj;
public function getNodes()
{
$result1 = $this->xpath->query('/root/nodes/*');
if ($result1->length > 0)
{
$nodes = new StdClass;
foreach($result1 as $xmlnode)
{
$nodename = preg_replace('/^\s+/', '', $xmlnode->tagName);
$nodename = preg_replace('/\s+$/', '', $nodename);
$nodename = preg_replace('/\s+/', '_', $nodename);
if (isset($nodes->{$nodename}))
{
/* duplicate nodename */
}
else
{
$nodes->{$nodename} = new StdClass;
$node = $nodes->{$nodename};
$node->name = $nodename;
foreach($this->xpath->query('*', $xmlnode) as $element)
{
switch ($element->nodeName)
{
case 'label':
$node->label = $element->nodeValue;
break;
case 'labeltype':
$node->labeltype = $element->nodeValue;
break;
case 'x':
if (is_numeric($element->nodeValue))
{
$node->x = $element->nodeValue;
}
else
{
$node->x = 1;
} /* not is_numeric($element->nodeValue) */
break;
case 'y':
if (is_numeric($element->nodeValue))
{
$node->y = $element->nodeValue;
}
else
{
$node->y = 1;
} /* not is_numeric($element->nodeValue) */
break;
case 'icon':
$node->icon = $element->nodeValue;
break;
case 'iconx':
if (is_numeric($element->nodeValue))
{
$node->iconx = $element->nodeValue;
}
else
{
$node->iconx = 1;
} /* not is_numeric($element->nodeValue) */
break;
case 'icony':
if (is_numeric($element->nodeValue))
{
$node->icony = $element->nodeValue;
}
else
{
$node->icony = 1;
} /* not is_numeric($element->nodeValue) */
break;
case 'icontpt':
$var = $element->nodeValue;
if (is_numeric($var) && $var >= 0 && $var <= 100)
$node->icontpt = $var;
break;
case 'iconresize':
if (is_numeric($element->nodeValue))
$node->iconresize = $element->nodeValue;
break;
case 'ip':
$node->ip = $element->nodeValue;
break;
case 'check':
$node->check = $element->nodeValue;
break;
case 'nodeurl':
$node->nodeurl = $element->nodeValue;
break;
} /* switch */
} /* foreach($this->xpath->query */
} /* not isset($nodes->{$nodename}) */
}
unset($result1);
return $nodes;
}
else
{
unset($result1);
return NULL;
} /* not $result->length */
}
# desc: returns autoscale settings;
# @return: $obj;
public function getAutoScale()
{
$result0 = $this->xpath->query('/root/autoscale');
if ($result0->length > 0)
{
// set up default structure
$autoscale = $this->defaultAutoscale();
foreach ($result0 as $e)
{
$result1 = $this->xpath->query('*', $e);
foreach ($result1 as $element)
{
switch ($element->tagName)
{
case 'nodivisions':
if (is_numeric($element->nodeValue))
$autoscale->nodivisions = $element->nodeValue;
break;
case 'rstart':
if (is_numeric($element->nodeValue))
$autoscale->rstart = $element->nodeValue;
break;
case 'rend':
if (is_numeric($element->nodeValue))
$autoscale->rend = $element->nodeValue;
break;
case 'gstart':
if (is_numeric($element->nodeValue))
$autoscale->gstart = $element->nodeValue;
break;
case 'gend':
if (is_numeric($element->nodeValue))
$autoscale->gend = $element->nodeValue;
break;
case 'bstart':
if (is_numeric($element->nodeValue))
$autoscale->bstart = $element->nodeValue;
break;
case 'bend':
if (is_numeric($element->nodeValue))
$autoscale->bend = $element->nodeValue;
break;
} /* switch $element->tagName */
} /* foreach $result1 */
unset($result1);
} /* foreach $result0 */
return $autoscale;
}
else
{
return NULL;
} /* not $result->length */
}
# desc: default autoscale values
# @return: $obj;
public function defaultAutoscale()
{
$autoscale = new StdClass;
$autoscale->nodivisions = 10;
$autoscale->rstart = 24;
$autoscale->rend = 0;
$autoscale->gstart = 232;
$autoscale->gend = 0;
$autoscale->bstart = 2;
$autoscale->bend = 0;
return $autoscale;
}
# desc: returns scale object;
# @return: $obj;
public function getScale()
{
$result1 = $this->xpath->query('/root/scale/*');
if ($result1->length > 0)
{
$scale = new StdClass;
for ($i = 1; $i <= $result1->length; $i++)
{
$result2 = $this->xpath->query("/root/scale/entry[{$i}]/*");
$low = null;
$high = null;
$r = null;
$g = null;
$b = null;
foreach ($result2 as $element)
{
switch ($element->tagName)
{
case 'low':
if (is_numeric($element->nodeValue))
$low = $element->nodeValue;
break;
case 'high':
if (is_numeric($element->nodeValue))
$high = $element->nodeValue;
break;
case 'r':
if (is_numeric($element->nodeValue))
$r = $element->nodeValue;
break;
case 'g':
if (is_numeric($element->nodeValue))
$g = $element->nodeValue;
break;
case 'b':
if (is_numeric($element->nodeValue))
$b = $element->nodeValue;
break;
} /* switch $element->tagName */
}
if (!is_null($low) & !is_null($high) & !is_null($r) & !is_null($g) & !is_null($b))
{
$scale->entry[$i] = new StdClass;
$scale->entry[$i]->low = $low;
$scale->entry[$i]->high = $high;
$scale->entry[$i]->r = $r;
$scale->entry[$i]->g = $g;
$scale->entry[$i]->b = $b;
} /* if !is_null */
} /* for i=1 */
unset($result2);
}
else
{
/* however we won't return any default static scale,
* because there is no reason to do so.
*/
// set up defaults
/*
* $scale->entry[0] = new StdClass;
* $scale->entry[0]->low = 0;
* $scale->entry[0]->high = 100;
* $scale->entry[0]->r = 0;
* $scale->entry[0]->g = 128;
* $scale->entry[0]->b = 255;
*/
$scale = NULL;
} /* not $result->length */
unset($result1);
return $scale;
}
# desc: returns title object;
# @return: $obj;
public function getTitle()
{
// set up default structure
$title = new StdClass;
$title->x = 100;
$title->y = 200;
$title->text = "WeatherMap5RRD-default";
// title background
$title->bg = new StdClass;
$title->bg->r = 255;
$title->bg->g = 255;
$title->bg->b = 255;
// title foreground
$title->fg = new StdClass;
$title->fg->r = 0;
$title->fg->g = 0;
$title->fg->b = 0;
$result1 = $this->xpath->query('/root/title/*');
foreach ($result1 as $element)
{
switch ($element->tagName)
{
case 'x':
if (is_numeric($element->nodeValue))
$title->x = $element->nodeValue;
break;
case 'y':
if (is_numeric($element->nodeValue))
$title->y = $element->nodeValue;
break;
case 'text':
$title->text = $element->nodeValue;
break;
case 'bg':
$result2 = $this->xpath->query('*', $element);
$this->colorParse($result2, $title->bg);
unset($result2);
break;
case 'fg':
$result2 = $this->xpath->query('*', $element);
$this->colorParse($result2, $title->fg);
unset($result2);
break;
default:
break;
} /* switch $element->tagName */
} /* foreach $result1 */
unset($result1);
return $title;
}
# desc: tied with getTitle; support function;
# $result: DOMNodeList;
# $obj: stdClass;
# @return: $obj;
private function colorParse($result, $obj)
{
foreach ($result as $element)
{
switch ($element->tagName)
{
case 'r':
if (is_numeric($element->nodeValue))
$obj->r = $element->nodeValue;
break;
case 'g':
if (is_numeric($element->nodeValue))
$obj->g = $element->nodeValue;
break;
case 'b':
if (is_numeric($element->nodeValue))
$obj->b = $element->nodeValue;
break;
} /* switch $element->tagName */
} /* foreach $result1 */
$retval = & $obj;
return $retval;
}
# desc: returns legend object;
# @return: $obj;
public function getLegend()
{
$legend = new StdClass;
// set up defaults
$legend->x = 100;
$legend->y = 100;
$legend->legendstyle = "original";
$result = $this->xpath->query('/root/legend/*');
foreach ($result as $element)
{
switch ($element->tagName)
{
case 'x':
if (is_numeric($element->nodeValue))
$legend->x = $element->nodeValue;
break;
case 'y':
if (is_numeric($element->nodeValue))
$legend->y = $element->nodeValue;
break;
case 'legendstyle':
if ($element->nodeValue == "line")
$legend->legendstyle = $element->nodeValue;
break;
default:
break;
} /* switch $element->tagName */
} /* foreach $element->tagName */
// unset things
unset($result);
return $legend;
}
# desc: returns autoscale object;
# @return: $obj;
public function autoscale($ascale = null)
{
if (is_object($ascale))
{
if (!isset($ascale->nodivisions) && !is_numeric($ascale->nodivisions))
{
$ascale->nodivisions = 7;
} /* !isset */
$dif_red=-($ascale->rstart - $ascale->rend);
$dif_green=-($ascale->gstart - $ascale->gend);
$dif_blue=-($ascale->bstart - $ascale->bend);
$step_red = $dif_red/$ascale->nodivisions;
$step_green = $dif_green/$ascale->nodivisions;
$step_blue = $dif_blue/$ascale->nodivisions;
$bounder_inf = 0;
$bounder_sup = round(100/$ascale->nodivisions);
$scale = new StdClass;
for ($i=1; $i < $ascale->nodivisions; $i++)
{
$scale->entry[$i] = new StdClass;
$scale->entry[$i]->low = $bounder_inf;
$scale->entry[$i]->high = $bounder_sup;
$scale->entry[$i]->r = $ascale->rstart + $i * $step_red;
$scale->entry[$i]->r = round($scale->entry[$i]->r, 0);
$scale->entry[$i]->g = $ascale->gstart + $i * $step_green;
$scale->entry[$i]->g = round($scale->entry[$i]->g, 0);
$scale->entry[$i]->b = $ascale->bstart + $i * $step_blue;
$scale->entry[$i]->b = round($scale->entry[$i]->b, 0);
$bounder_inf = $bounder_sup;
if ($i == ($ascale->nodivisions-2))
{
$bounder_sup = 100;
}
else
{
$bounder_sup = ($i+2)*round(100/$ascale->nodivisions);
} /* not $i == $ascale */
} /* for i=1 */
return $scale;
}
else
{
return NULL;
} /* not is_object */
}
}
?>