<?php
/*
* weathermap5rrd - index.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 index extends defaultLib
{
public $settings;
public $scriptname;
private $cfgfile;
private $arrow_width;
private $rolloverlib;
// for more info see documentation.txt;
// staticout = 1; ==> image from config file;
// staticout = 0; ==> image from drawmap;
private $staticout = 0;
// ready-up
function __construct()
{
$this->settings = new StdClass;
$settings = $this->settings;
$settings->xmlobject = new StdClass;
$settings->layout = "layout.xsl"; // NULL for XML output
$settings->import = new StdClass;
$this->scriptname = $this->getScriptname();
$settings->xmlobject->scriptname = $this->scriptname;
$this->auto_index = -1;
}
// clean-up
function __destruct()
{
unset($this->settings);
}
// go
public function init()
{
$path = $this->getPath();
$settings = $this->settings;
$auto_index = -1;
$xml = $this->settings->xmlobject;
if (!file_exists("{$path}lib/configfile.php") || !is_readable("{$path}lib/configfile.php"))
{
throw new Exception ("Internal file('{$path}lib/configfile.php') is missing or not readable. Error #99.");
} /* !file_exists */
$this->cfgfile = "weathermap.xml";
if (isset($_GET['config']) && !empty($_GET['config']))
{
$this->cfgfile = $this->stripMe($_GET['config']);
}
require_once "{$path}lib/configfile.php";
$configfile = new config_file($this->cfgfile);
if ($configfile->readconfig() == false)
{
throw new Exception ("Config file not found. Please, make sure weathermap has access to it.");
}
$config = $configfile->getConfig();
$xml->refresh = $config->refresh;
$xml->image = "{$this->scriptname}?module=drawmap&config=" . urlencode($this->cfgfile);
$xml->debuglink = "{$this->scriptname}?module=drawmap&config=" . urlencode($this->cfgfile)
. "&debug=1";
$this->rolloverlib = $configfile->getRollOverLib();
if (isset($config->arrow_width))
{
$this->arrow_width = $config->arrowwidth;
}
else
{
$this->arrow_width = $configfile->getArrowWidth();
} /* not $config->arrow_width */
if (isset($config->links) && is_object($config->links) && isset($config->nodes)
&& is_object($config->nodes))
{
$nodes = $config->nodes;
require_once "{$path}lib/graphic.php";
$graphic = new graphic();
foreach ($config->links as $link) {
if (isset($link->linkinfourl) || isset($link->linkoverlibgraph))
{
if (!isset($xml->map) || !is_object($xml->map))
$xml->map = new StdClass;
if (!isset($link->linkinfourl))
$link->linkinfourl = NULL;
if (!isset($link->linkoverlibgraph))
$link->linkoverlibgraph = NULL;
$auto_index++;
$xml->map->entry[$auto_index] = new StdClass;
$map = & $xml->map->entry[$auto_index];
$map = $this->get_jslib_obj($link->name, $link->linkoverlibgraph, $link->linkinfourl);
$map->shape = "poly";
$coords = array();
if (isset($link->internode))
{
# internode count
$inodecount = count($link->internode);
if (isset($link->internode) && is_array($link->internode) && isset($nodes->{$link->from}->x)
&& isset($nodes->{$link->from}->y) && isset($nodes->{$link->to}->x)
&& isset($nodes->{$link->to}->y))
{
if ($inodecount%2 == 0)
{
switch ($link->arrow)
{
case 'circle':
$coords[0] = $graphic->draw_rectangle_half_map($nodes->{$link->from}->x, $nodes->{$link->from}->y,
$link->internode[1]->x, $link->internode[1]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
$coords[1] = $graphic->draw_rectangle_half_map($nodes->{$link->to}->x, $nodes->{$link->to}->y,
$link->internode[$inodecount]->x, $link->internode[$inodecount]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
break;
case 'halfarrow':
$coords[2] = $graphic->draw_arrow_half_map($nodes->{$link->from}->x, $nodes->{$link->from}->y,
$link->internode[1]->x, $link->internode[1]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
$coords[3] = $graphic->draw_arrow_half_map($nodes->{$link->to}->x, $nodes->{$link->to}->y,
$link->internode[$inodecount]->x, $link->internode[$inodecount]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
break;
default:
$coords[4] = $graphic->draw_arrow_map($nodes->{$link->from}->x, $nodes->{$link->from}->y,
$link->internode[1]->x, $link->internode[1]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
$coords[5] = $graphic->draw_arrow_map($nodes->{$link->to}->x, $nodes->{$link->to}->y,
$link->internode[$inodecount]->x, $link->internode[$inodecount]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
break;
} /* switch $link->arrow */
# map areas between nodes
$nodea = ($inodecount/2) + 1;
$nodeb = $inodecount/2;
for ($i = 1; $i <= $inodecount; $i++)
{
if ($i+1 <= $inodecount)
{
if (($i == $nodea || $i == $nodeb) && ($i+1 == $nodeb || $i+1 == $nodea))
{
# do nothing
}
else
{
switch ($link->arrow)
{
case 'circle':
$coords[6] = $graphic->draw_rectangle_half_map($link->internode[$i]->x, $link->internode[$i]->y,
$link->internode[$i+1]->x, $link->internode[$i+1]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
break;
case 'halfarrow':
$coords[7] = $graphic->draw_arrow_half_map($link->internode[$i]->x, $link->internode[$i]->y,
$link->internode[$i+1]->x, $link->internode[$i+1]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
break;
default:
$coords[8] = $graphic->draw_arrow_map($link->internode[$i]->x, $link->internode[$i]->y,
$link->internode[$i+1]->x, $link->internode[$i+1]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
break;
} /* switch $link->arrow */
} /* not $i == $nodea */
} /* if $i + 1 */
} /* for $i = 1 */
# map areas between $nodea and $nodeb
switch ($link->arrow)
{
case "circle":
$coords[9] = $graphic->draw_rectangle_half_map($link->internode[$nodea]->x, $link->internode[$nodea]->y,
$graphic->middle($link->internode[$nodea]->x, $link->internode[$nodeb]->x),
$graphic->middle($link->internode[$nodea]->y, $link->internode[$nodeb]->y),
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
break;
case 'halfarrow':
$coords[10] = $graphic->draw_arrow_half_map($link->internode[$nodea]->x, $link->internode[$nodea]->y,
$graphic->middle($link->internode[$nodea]->x, $link->internode[$nodeb]->x),
$graphic->middle($link->internode[$nodea]->y, $link->internode[$nodeb]->y),
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
break;
default:
$coords[11] = $graphic->draw_arrow_map($link->internode[$nodea]->x, $link->internode[$nodea]->y,
$graphic->middle($link->internode[$nodea]->x, $link->internode[$nodeb]->x),
$graphic->middle($link->internode[$nodea]->y, $link->internode[$nodeb]->y),
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
break;
} /* switch $link->arrow */
}
else
{
if (count($link->internode) == 1)
{
$j = 1;
$inodelast = 1;
$from = $nodes->{$link->from};
$to = $nodes->{$link->to};
}
else
{
$j = round(count($link->internode)/2, 0);
$inodelast = count($link->internode);
$from = $link->internode[$j-1];
$to = $link->internode[$j+1];
switch ($link->arrow)
{
case 'circle':
$coords[12] = $graphic->draw_rectangle_half_map($nodes->{$link->from}->x, $nodes->{$link->from}->y,
$link->internode[1]->x, $link->internode[1]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
$coords[13] = $graphic->draw_rectangle_half_map($nodes->{$link->to}->x, $nodes->{$link->to}->y,
$link->internode[$inodelast]->x, $link->internode[$inodelast]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
break;
case 'halfarrow':
$coords[14] = $graphic->draw_arrow_half_map($nodes->{$link->from}->x, $nodes->{$link->from}->y,
$link->internode[1]->x, $link->internode[1]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
$coords[15] = $graphic->draw_arrow_half_map($nodes->{$link->to}->x, $nodes->{$link->to}->y,
$link->internode[$inodelast]->x, $link->internode[$inodelast]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
break;
default:
$coords[16] = $graphic->draw_arrow_map($nodes->{$link->from}->x, $nodes->{$link->from}->y,
$link->internode[1]->x, $link->internode[1]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
$coords[17] = $graphic->draw_arrow_map($nodes->{$link->to}->x, $nodes->{$link->to}->y,
$link->internode[$inodelast]->x, $link->internode[$inodelast]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
break;
} /* switch $link->arrow */
} /* not count(link->internode) */
switch ($link->arrow)
{
case "circle":
$coords[18] = $graphic->draw_rectangle_half_map($from->x, $from->y,
$link->internode[$j]->x, $link->internode[$j]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
$coords[19] = $graphic->draw_rectangle_half_map($to->x, $to->y,
$link->internode[$j]->x, $link->internode[$j]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
break;
case 'halfarrow':
$coords[20] = $graphic->draw_arrow_half_map($from->x, $from->y,
$link->internode[$j]->x, $link->internode[$j]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
$coords[21] = $graphic->draw_arrow_half_map($to->x, $to->y,
$link->internode[$j]->x, $link->internode[$j]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
break;
default:
$coords[22] = $graphic->draw_arrow_map($from->x, $from->y,
$link->internode[$j]->x, $link->internode[$j]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
$coords[23] = $graphic->draw_arrow_map($to->x, $to->y,
$link->internode[$j]->x, $link->internode[$j]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
break;
} /* switch $link->arrow */
for ($i = 1; $i <= $inodelast; $i++)
{
if ($i+1 <= $inodelast)
{
if (($i == ($j-1) && ($i+1) == $j) || ($i == $j && ($i+1) == ($j+1)))
{
# do nothing
}
else
{
switch ($link->arrow)
{
case 'circle':
$coords[24] = $graphic->draw_rectangle_half_map($link->internode[$i]->x, $link->internode[$i]->y,
$link->internode[$i+1]->x, $link->internode[$i+1]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
break;
case 'halfarrow':
$coords[25] = $graphic->draw_arrow_half_map($link->internode[$i]->x, $link->internode[$i]->y,
$link->internode[$i+1]->x, $link->internode[$i+1]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
break;
default:
$coords[26] = $graphic->draw_arrow_map($link->internode[$i]->x, $link->internode[$i]->y,
$link->internode[$i+1]->x, $link->internode[$i+1]->y,
$this->arrow_width, $link->name, $link->linkoverlibgraph, $link->linkinfourl);
break;
} /* switch $link->arrow */
} /* not $i == $j-1 */
} /* if $i+1 */
} /* for $i = 1 */
} /* not count % 2 */
}
else
{
setDebugMsg("Unexpected error in index.php. Please report this error;");
} /* not isset $link -> internode && is_array */
}
else
{
if (isset($nodes->{$link->to}->x) && isset($nodes->{$link->from}->y))
{
# else simple arrows;
# one function per arrow
switch ($link->arrow)
{
case "circle":
$coords[1] = $graphic->draw_rectangle_half_map($nodes->{$link->to}->x, $nodes->{$link->to}->y,
$graphic->middle($nodes->{$link->from}->x, $nodes->{$link->to}->x), $graphic->middle($nodes->{$link->from}->y, $nodes->{$link->to}->y),
$this->arrow_width);
$coords[2] = $graphic->draw_rectangle_half_map($nodes->{$link->from}->x, $nodes->{$link->from}->y,
$graphic->middle($nodes->{$link->from}->x, $nodes->{$link->to}->x), $graphic->middle($nodes->{$link->from}->y, $nodes->{$link->to}->y),
$this->arrow_width);
break;
case 'halfarrow':
$coords[3] = $graphic->draw_arrow_half_map($nodes->{$link->to}->x, $nodes->{$link->to}->y,
$nodes->{$link->from}->x, $nodes->{$link->from}->y, $this->arrow_width);
$coords[4] = $graphic->draw_arrow_half_map($nodes->{$link->from}->x, $nodes->{$link->from}->y,
$nodes->{$link->to}->x, $nodes->{$link->to}->y, $this->arrow_width);
break;
default:
$coords[5] = $graphic->draw_arrow_half_map($nodes->{$link->to}->x, $nodes->{$link->to}->y,
$graphic->middle($nodes->{$link->from}->x, $nodes->{$link->to}->x), $graphic->middle($nodes->{$link->from}->y, $nodes->{$link->to}->y),
$this->arrow_width);
$coords[6] = $graphic->draw_arrow_half_map($nodes->{$link->from}->x, $nodes->{$link->from}->y,
$graphic->middle($nodes->{$link->from}->x, $nodes->{$link->to}->x), $graphic->middle($nodes->{$link->from}->y, $nodes->{$link->to}->y),
$this->arrow_width);
break;
} /* switch $link->arrow */
} /* if isset(nodes->{link->to}->x) */
} /* not isset(link->internode) */
if (is_array($coords))
$map->coords = implode(",", $coords);
} /* not isset($link->infourl) */
} /* foreach ($config->links as $link) */
foreach ($nodes as $node)
{
if (isset($node->nodeurl) && !empty($node->nodeurl))
{
$auto_index++;
$xml->map->entry[$auto_index] = new StdClass;
$map = & $xml->map->entry[$auto_index];
$map->shape = "poly";
$map->nodeurl = $node->nodeurl;
switch ($node->labeltype)
{
case 'round':
$map->shape = "circle";
$map->coords = $graphic->draw_label_round_map($node->x, $node->y, $node->label, $config->font);
break;
default:
$map->shape = "rect";
$map->coords = $graphic->draw_label_map($node->x, $node->y, $node->label, $config->font);
break;
} /* switch $node->labeltype */
} /* not isset($node->nodeurl) */
} /* foreach $nodes */
} /* not isset($config->links) */
if ($this->staticout == 1 && isset($config->outputfile))
{
/* image is being generated separately / somewhere outside.
*require_once "{$path}modules/drawmap.php";
*$drawmap = new drawmap(0);
*#$drawmap->setDebug(1);
*$drawmap->init();
*if (isset($drawmap->settings->debug->entry) && is_array($drawmap->settings->debug->entry)) {
* foreach ($drawmap->settings->debug->entry as $entry) {
* setDebugMsg($entry);
* }
*}
*unset($drawmap);
*/
$pos = strrpos($config->outputfile, "/");
if (is_bool($pos) && !$pos)
{
$xml->image = $config->outputfile;
}
else
{
$part = substr($config->outputfile, ($pos+1));
$xml->image = $part;
} /* not is_bool($pos) */
}
else
{
$xml->image = "{$this->scriptname}?module=drawmap&config=" . urlencode($this->cfgfile);
} /* not $this->staticout */
/* hide@address.com 10:33pm
* this is probably waste of resources, since we are
* about to create xhtml here and once again in dlib.php
* by creating output. it /may be/ could be tricked thru
* _destroy(), however i don't have time to test it right
* now.
*/
if (isset($config->htmlfile))
{
$foo = "<root>\n";
$foo.= $this->ObjToXML($xml);
$foo.= "</root>";
$template = $this->xslPrepare($settings->import, "root", "{$path}xsl/layout/{$settings->layout}");
$htmlout = $this->transform($foo, $template);
while(1)
{
if (!$handle = fopen("{$config->htmlfile}", 'w'))
{
setDebugMsg("Unable to open file '{$config->htmlfile}' for writing");
break;
} /* !$handle */
if (fwrite($handle, $htmlout) == false)
{
setDebugMsg("Unable to write into file '{$config->htmlfile}';");
break;
} /* fwrite */
fclose($handle);
break;
} /* while (1) */
} /* not isset($config->htmlfile */
}
# desc:
#
# $linkname: string;
# $overlib: string;
# $infourl: string;
# @return: object;
public function get_jslib_obj($linkname = "foo", $overlib = NULL, $infourl = NULL)
{
$obj = new StdClass;
if ($this->rolloverlib == "overlib" && !is_null($overlib))
{
switch ($overlib)
{
case '1':
case 'linkgraph':
$url = $this->getScriptname() . "?module=linkgraph&link=" . urlencode($linkname)
. "&config=" . urlencode($this->cfgfile) . "&limit=1";
if (($fp = @fopen($url, 'rb')) !== false)
{
$buffer = '';
while (!feof($fp))
{
$buffer.= fread($fp, 8192);
} /* while !feof($fp) */
fclose($fp);
$buffer = str_replace("\"", "", $buffer);
$buffer = str_replace("\n", "", $buffer);
$obj->onmouseover = "<![CDATA[return overlib('{$buffer}', STICKY, DELAY, 250, CAPTION, '{$linkname}', MOUSEOFF);]]>";
$obj->onmouseout = "return nd();";
}
else
{
setDebugMsg("Unable to open linkgrap with params: cfg {$this->cfgfile}, linkname " . urlencode($linkname) . ";");
} /* not $fp == false */
break;
case 'currentvalues':
$url = $this->getHostname() . "/currentvalues.php?link=" . urlencode($linkname)
. "&config=" . urlencode($this->cfgfile);
if (($fp = @fopen($url, 'rb')) !== false)
{
$buffer = '';
while (!feof($fp))
{
$buffer.= fread($fp, 8192);
} /* while !feof */
fclose($fp);
$buffer = str_replace("\"", "", $buffer);
$buffer = str_replace("\n", "", $buffer);
$obj->onmouseover = "<![CDATA[return overlib('{$buffer}', DELAY, 250, CAPTION, 'Link current values');]]>";
$obj->onmouseout = "return nd();";
}
else
{
setDebugMsg("Unable to open currentvalues with params: cfg {$this->cfgfile}, linkname " . urlencode($linkname) . ";");
} /* not $fp == fopen */
break;
default:
# Section ok
/*
* #$html_code=" onmouseover=\"return overlib('<img src=$overlib>',DELAY,250,CAPTION,'$caption');\" ";
* $fp=fopen ("$overlib", "r");
* $buffer.= fread($fp, 1024);
* fclose($fp);
* $buffer=str_replace("\"","",$buffer);
* $buffer=str_replace("\n","<br>",$buffer);
* $html_code=" onmouseover=\"return overlib('$buffer',DELAY,250,CAPTION,'');\" ";
* $html_code.=" onmouseout=\"return nd();\" ";
* #$weathermap4php= $_SERVER["HTTP_HOST"].dirname($_SERVER['REQUEST_URI'])."/overlib";
*/
if (($fp = @fopen($overlib, 'rb')) !== false)
{
$buffer = '';
while (!feof($fp))
{
$buffer.= fread($fp, 8192);
} /* while !feof */
fclose($fp);
$buffer = str_replace("\"", "", $buffer);
$buffer = str_replace("\n", "", $buffer);
$obj->onmouseover ="<![CDATA[return overlib('{$buffer}', STICKY, DELAY, 250, CAPTION, '{$linkname}', MOUSEOFF);]]>";
$obj->onmouseout = "return nd();";
// For Crinago
/*
* $html_code=" onmouseover=\"return overlib('<img src=$overlib>',DELAY,250,CAPTION,'$caption');\" ";
* $html_code.=" onmouseout=\"return nd();\" ";
*/
}
else
{
setDebugMsg("Unable to open custom script;");
} /* not $fp == false */
break;
} /* switch $overlib */
} /* if rolloverlib == overlib */
# linkinfourl
if (!is_null($infourl))
{
if ($infourl == "globalgraph")
{
$obj->linkinfourl = $this->getScriptname() . "?module=linkgraph&link="
. urlencode($linkname) . "&config=" . urlencode($this->cfgfile);
}
else
{
# replace ampersands. however, we can't replace everything,
# so be carefull.
$infourl = str_replace("&", "&", $infourl);
$obj->linkinfourl = $infourl;
} /* not infourl */
} /* if is_null($infourl) */
return $obj;
}
}
?>