/*
* Project: NodeMap2.0
* File: KMLHandler.class.php
* Purpose: Creating of editing KML files
*/
define('PLACEMARK_GREEN', 'http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png');
define('PLACEMARK_ORANGE', 'http://www.google.com/intl/en_us/mapfiles/ms/micons/orange-dot.png');
define('PLACEMARK_RED', 'http://www.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png');
class KMLFile {
private $template = '
Wireless Leiden Interactive Nodemap 2.0
1
Wireless Leiden Interactive Nodemap 2.0
Nodes
Nodes from the Wireless Leiden network
52.161087
4.490153
0
0
0
500
%CONTENT%
';
static $fileFirst = 'type,host_name,has_been_checked,check_execution_time,current_state,last_hard_state,last_check,problem_has_been_acknowledged';
private $fileContent = array('string', 'string', 'integer', 'double', 'integer', 'integer', 'integer', 'integer');
private $KMLPlacemarks = array();
/*
* Function: __construct (constructor)
* Parameters: -
* Function: Creating a new KMLFile
*/
public function __construct() {
}
public function addPlacemark(KMLPlacemark $placemark) {
$this->KMLPlacemarks[] = $placemark;
}
public function toString() {
$toString = $this->template;
$placemarkString = '';
$placemarkCount = count($this->KMLPlacemarks);
for ($i = 0; $i < $placemarkCount; $i++) {
$placemarkString .= $this->KMLPlacemarks[$i]->toString();
}
$toString = str_replace('%PLACEMARK_GREEN%', PLACEMARK_GREEN, $toString);
$toString = str_replace('%PLACEMARK_ORANGE%', PLACEMARK_ORANGE, $toString);
$toString = str_replace('%PLACEMARK_RED%', PLACEMARK_RED, $toString);
$toString = str_replace('%CONTENT%', $placemarkString, $toString);
return $toString;
}
public function write($filename) {
// TODO: Write KMLFile to a KML file
}
public function parseLocationFile($file) {
//preg_match_all('#\[(^\/.+?)\]#is', $file, $matches);
$nodesCount = preg_match_all('/\[[a-zA-Z0-9]*\]/i', $file, $nodes, PREG_OFFSET_CAPTURE);
for ($i = 0; $i < $nodesCount; $i++) {
$location = $this->findInLocationFile($file, 'location', $nodes[0][$i][1]);
$status = $this->findInLocationFile($file, 'status', $nodes[0][$i][1]);
$latitude = $this->findInLocationFile($file, 'latitude', $nodes[0][$i][1]);
$longitude = $this->findInLocationFile($file, 'longitude', $nodes[0][$i][1]);
$interfaces = $this->findInLocationFile($file, 'interfaces', $nodes[0][$i][1]);
$masterip = $this->findInLocationFile($file, 'masterip', $nodes[0][$i][1]);
$nodetype = $this->findInLocationFile($file, 'nodetype', $nodes[0][$i][1]);
$name = $this->findInLocationFile($file, 'name', $nodes[0][$i][1]);
$placemark = new KMLPlacemark();
$placemark->setName($name);
$placemark->setDescription('Naam: ' . $name . '
Locatie: ' . $location . '
Status: ' . $status . '
Latitude: ' . $latitude . '
Longitude: ' . $longitude . '
Interfaces: ' . $interfaces . '
Master IP: ' . $masterip . '
Node type: ' . $nodetype);
$placemark->setLongitude($longitude);
$placemark->setLatitude($latitude);
$placemark->setStyle($status == 'up' ? 'greenArrowIcon' : 'redArrowIcon');
$this->addPlacemark($placemark);
}
}
private function findInLocationFile($file, $keyword, $offset) {
$start = strpos($file, $keyword, $offset) + strlen($keyword . ' = ');
$end = strpos($file, "\n", $start);
return substr($file, $start, $end - $start);
}
public function parseStatusFile($file) {
/*
* TODO: Needs to be rebuild so it doesn't create a new KMLFile object,
* but add the KMLPlacemarks to the current object.
*/
$fileContents = explode("\r\n", $file);
if ($fileContents[0] != KMLFile::$fileFirst) {
trigger_log(LOG_WARNING, 'Contents of file do not match with template of first line', __FILE__, __LINE__);
}
$lines = count($fileContents);
for ($i = 1; $i < count($lines); $i++) {
$lineContent = explode(',', $fileContents[$i]);
if (count($lineContent) != count($this->fileContent)) {
trigger_log(LOG_WARNING, 'Contents of file do not match with template of lines', __FILE__, __LINE__);
}
// TODO: Process all lines and create KMLPlacemark objects we can add to a new KMLFile object
}
}
}
?>