[7661] | 1 | <?php
|
---|
[7627] | 2 | /*
|
---|
| 3 | * Project: NodeMap2.0
|
---|
| 4 | * File: KMLHandler.class.php
|
---|
| 5 | * Purpose: Creating of editing KML files
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | class KMLFile {
|
---|
[7631] | 9 | private $template = '
|
---|
[7692] | 10 | <\?xml version="1.0" encoding="UTF-8"?>
|
---|
[7627] | 11 | <kml xmlns="http://www.opengis.net/kml/2.2">
|
---|
[7632] | 12 | <Document>
|
---|
| 13 | <name>Wireless Leiden Interactive Nodemap 2.0</name>
|
---|
| 14 | <open>1</open>
|
---|
| 15 | <description>Wireless Leiden Interactive Nodemap 2.0</description>
|
---|
| 16 | <Style id="greenArrowIcon">
|
---|
| 17 | <IconStyle>
|
---|
| 18 | <Icon>
|
---|
| 19 | <href>%PLACEMARK_GREEN%</href>
|
---|
| 20 | </Icon>
|
---|
| 21 | </IconStyle>
|
---|
| 22 | </Style>
|
---|
| 23 | <Style id="orangeArrowIcon">
|
---|
| 24 | <IconStyle>
|
---|
| 25 | <Icon>
|
---|
| 26 | <href>%PLACEMARK_ORANGE%</href>
|
---|
| 27 | </Icon>
|
---|
| 28 | </IconStyle>
|
---|
| 29 | </Style>
|
---|
| 30 | <Style id="redArrowIcon">
|
---|
| 31 | <IconStyle>
|
---|
| 32 | <Icon>
|
---|
| 33 | <href>%PLACEMARK_RED%</href>
|
---|
| 34 | </Icon>
|
---|
| 35 | </IconStyle>
|
---|
| 36 | </Style>
|
---|
| 37 | <Folder>
|
---|
| 38 | <name>Nodes</name>
|
---|
| 39 | <description>Nodes from the Wireless Leiden network</description>
|
---|
| 40 | <LookAt>
|
---|
| 41 | <longitude>52.161087</longitude>
|
---|
| 42 | <latitude>4.490153</latitude>
|
---|
| 43 | <altitude>0</altitude>
|
---|
| 44 | <heading>0</heading>
|
---|
| 45 | <tilt>0</tilt>
|
---|
| 46 | <range>500</range>
|
---|
| 47 | </LookAt>
|
---|
| 48 | %CONTENT%
|
---|
| 49 | </Folder>
|
---|
| 50 | </Document>
|
---|
[7627] | 51 | </kml>';
|
---|
[7632] | 52 |
|
---|
[7637] | 53 | static $fileFirst = 'type,host_name,has_been_checked,check_execution_time,current_state,last_hard_state,last_check,problem_has_been_acknowledged';
|
---|
[7631] | 54 | private $fileContent = array('string', 'string', 'integer', 'double', 'integer', 'integer', 'integer', 'integer');
|
---|
[7627] | 55 |
|
---|
[7644] | 56 | private $KMLPlacemarks;
|
---|
[7627] | 57 |
|
---|
| 58 | /*
|
---|
| 59 | * Function: __construct (constructor)
|
---|
| 60 | * Parameters: -
|
---|
| 61 | * Function: Creating a new KMLFile
|
---|
| 62 | */
|
---|
[7631] | 63 | public function __construct() {
|
---|
[7644] | 64 | $this->KMLPlacemarks = array();
|
---|
[7627] | 65 | }
|
---|
| 66 |
|
---|
[7631] | 67 | public function addPlacemark(KMLPlacemark $placemark) {
|
---|
[7628] | 68 | $this->KMLPlacemarks[] = $placemark;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[7631] | 71 | public function toString() {
|
---|
| 72 | $toString = $this->template;
|
---|
[7627] | 73 |
|
---|
| 74 | $placemarkString = '';
|
---|
| 75 | $placemarkCount = count($this->KMLPlacemarks);
|
---|
| 76 | for ($i = 0; $i < $placemarkCount; $i++) {
|
---|
[7631] | 77 | $placemarkString .= $this->KMLPlacemarks[$i]->toString();
|
---|
[7627] | 78 | }
|
---|
| 79 |
|
---|
[7702] | 80 | $toString = str_replace('%PLACEMARK_GREEN%', $config['placemark_green'], $toString);
|
---|
| 81 | $toString = str_replace('%PLACEMARK_ORANGE%', $config['placemark_orange'], $toString);
|
---|
| 82 | $toString = str_replace('%PLACEMARK_RED%', $config['placemark_red'], $toString);
|
---|
[7631] | 83 | $toString = str_replace('%CONTENT%', $placemarkString, $toString);
|
---|
[7627] | 84 |
|
---|
| 85 | return $toString;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[7700] | 88 | /*
|
---|
| 89 | * Function: write
|
---|
| 90 | * Parameters: string $filename
|
---|
| 91 | * Function: Write KMLFile to a KML file
|
---|
| 92 | */
|
---|
[7631] | 93 | public function write($filename) {
|
---|
[7700] | 94 | trigger_log(SYSLOG_DEBUG, 'Opening the file "' . $filename . '"', __FILE__, __LINE__);
|
---|
| 95 | $file = fopen("$filename","w")
|
---|
| 96 | or exit(trigger_log(SYSLOG_ERR, 'Opening the file "' . $filename . '"', __FILE__, __LINE__));
|
---|
| 97 |
|
---|
| 98 | trigger_log(SYSLOG_DEBUG, 'Writing file "' . $filename . '"', __FILE__, __LINE__);
|
---|
| 99 | fwrite($file,$toString());
|
---|
| 100 |
|
---|
| 101 | trigger_log(SYSLOG_DEBUG, 'Closing file "' . $filename . '"', __FILE__, __LINE__);
|
---|
| 102 | fclose($file);
|
---|
[7627] | 103 | }
|
---|
[7631] | 104 |
|
---|
[7642] | 105 | /*
|
---|
| 106 | * Function: getPlacemarkByName
|
---|
| 107 | * Parameters: string $name
|
---|
| 108 | * Function: Find the first KMLPlacemark with the name $name and return its position. If not found, return false
|
---|
| 109 | */
|
---|
| 110 | public function getPlacemarkByName($name) {
|
---|
| 111 | $nodesCount = count($this->KMLPlacemarks);
|
---|
| 112 | for ($i = 0; $i < $nodesCount; $i++) {
|
---|
| 113 | if ($this->KMLPlacemarks[$i]->getName() == $name) {
|
---|
| 114 | return $i;
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | return false;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | /*
|
---|
| 121 | * Function: parseLocationFile
|
---|
| 122 | * Parameters: string $file
|
---|
| 123 | * Function: Parse the node location file updating or adding KMLPlacemark objects to the current KMLFile object
|
---|
| 124 | */
|
---|
[7640] | 125 | public function parseLocationFile($file) {
|
---|
| 126 | $nodesCount = preg_match_all('/\[[a-zA-Z0-9]*\]/i', $file, $nodes, PREG_OFFSET_CAPTURE);
|
---|
| 127 | for ($i = 0; $i < $nodesCount; $i++) {
|
---|
[7642] | 128 | // TODO: Needs checking for parsing errors
|
---|
[7640] | 129 | $location = $this->findInLocationFile($file, 'location', $nodes[0][$i][1]);
|
---|
| 130 | $status = $this->findInLocationFile($file, 'status', $nodes[0][$i][1]);
|
---|
| 131 | $latitude = $this->findInLocationFile($file, 'latitude', $nodes[0][$i][1]);
|
---|
| 132 | $longitude = $this->findInLocationFile($file, 'longitude', $nodes[0][$i][1]);
|
---|
| 133 | $interfaces = $this->findInLocationFile($file, 'interfaces', $nodes[0][$i][1]);
|
---|
| 134 | $masterip = $this->findInLocationFile($file, 'masterip', $nodes[0][$i][1]);
|
---|
| 135 | $nodetype = $this->findInLocationFile($file, 'nodetype', $nodes[0][$i][1]);
|
---|
| 136 | $name = $this->findInLocationFile($file, 'name', $nodes[0][$i][1]);
|
---|
| 137 |
|
---|
[7642] | 138 | $descriptionLocation = 'Naam: ' . $name . '<br/>Locatie: ' . $location . '<br/>Status: ' . $status . '<br/>Latitude: ' . $latitude . '<br/>Longitude: ' . $longitude . '<br/>Interfaces: ' . $interfaces . '<br/>Master IP: ' . $masterip . '<br/>Node type: ' . $nodetype . '<br/><br/>';
|
---|
| 139 |
|
---|
| 140 | if ($placemarkPosition = $this->getPlacemarkByName($name)) {
|
---|
| 141 | $this->KMLPlacemarks[$placemarkPosition]->setDescriptionLocation($descriptionLocation);
|
---|
| 142 | $this->KMLPlacemarks[$placemarkPosition]->setLongitude($longitude);
|
---|
| 143 | $this->KMLPlacemarks[$placemarkPosition]->setLatitude($latitude);
|
---|
| 144 | } else {
|
---|
| 145 | $placemark = new KMLPlacemark();
|
---|
[7692] | 146 | $placemark->setID($name);
|
---|
[7642] | 147 | $placemark->setName($name);
|
---|
| 148 | $placemark->setDescriptionLocation($descriptionLocation);
|
---|
| 149 | $placemark->setLongitude($longitude);
|
---|
| 150 | $placemark->setLatitude($latitude);
|
---|
| 151 | $this->addPlacemark($placemark);
|
---|
| 152 | }
|
---|
[7640] | 153 | }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[7642] | 156 | /*
|
---|
| 157 | * Function: findInLocationFile
|
---|
| 158 | * Parameters: string $file, string $keyword, integer $offset
|
---|
| 159 | * Function: Find the $keyword in $file and return the value of $keyword, starting at $offset, on error return false
|
---|
| 160 | */
|
---|
[7640] | 161 | private function findInLocationFile($file, $keyword, $offset) {
|
---|
[7642] | 162 | $start = strpos($file, $keyword, $offset) + strlen($keyword . ' = ');
|
---|
[7640] | 163 | $end = strpos($file, "\n", $start);
|
---|
[7642] | 164 |
|
---|
| 165 | if ($start && $end && ($start < $end)) {
|
---|
| 166 | return substr($file, $start, $end - $start);
|
---|
| 167 | } else {
|
---|
| 168 | return false;
|
---|
| 169 | }
|
---|
[7640] | 170 | }
|
---|
| 171 |
|
---|
[7642] | 172 | /*
|
---|
| 173 | * Function: parseStatusFile
|
---|
| 174 | * Parameters: string $file
|
---|
| 175 | * Function: Parse the node status file updating or adding KMLPlacemark objects to the current KMLFile object
|
---|
| 176 | */
|
---|
[7640] | 177 | public function parseStatusFile($file) {
|
---|
[7637] | 178 | $fileContents = explode("\r\n", $file);
|
---|
[7631] | 179 |
|
---|
[7637] | 180 | if ($fileContents[0] != KMLFile::$fileFirst) {
|
---|
| 181 | trigger_log(LOG_WARNING, 'Contents of file do not match with template of first line', __FILE__, __LINE__);
|
---|
[7631] | 182 | }
|
---|
| 183 |
|
---|
[7642] | 184 | $linesCount = count($fileContents);
|
---|
| 185 | for ($i = 1; $i < $linesCount - 1; $i++) {
|
---|
[7631] | 186 | $lineContent = explode(',', $fileContents[$i]);
|
---|
| 187 |
|
---|
| 188 | if (count($lineContent) != count($this->fileContent)) {
|
---|
[7637] | 189 | trigger_log(LOG_WARNING, 'Contents of file do not match with template of lines', __FILE__, __LINE__);
|
---|
[7631] | 190 | }
|
---|
| 191 |
|
---|
[7642] | 192 | // TODO: Process all lines and update KMLPlacemark objects we already have in the current object
|
---|
| 193 | $type = $lineContent[0];
|
---|
| 194 | $host_name = str_replace('CNode', '', $lineContent[1]);
|
---|
| 195 | $has_been_checked = $lineContent[2];
|
---|
| 196 | $check_execution_time = $lineContent[3];
|
---|
| 197 | $current_state = $lineContent[4];
|
---|
| 198 | $last_hard_state = $lineContent[5];
|
---|
| 199 | $last_check = $lineContent[6];
|
---|
| 200 | $problem_has_been_acknowledged = $lineContent[7];
|
---|
| 201 |
|
---|
| 202 | $descriptionStatus = 'Type: ' . $type . '<br/>Host name: ' . $host_name . '<br/>Has been checked: ' . $has_been_checked . '<br/>Check execution time: ' . $check_execution_time . '<br/>Currenr state: ' . $current_state . '<br/>Last hard state: ' . $last_hard_state . '<br/>Last check: ' . $last_check . '<br/>Problem has been acknowledged: ' . $problem_has_been_acknowledged . '<br/><br/>';
|
---|
| 203 |
|
---|
| 204 | if ($placemarkPosition = $this->getPlacemarkByName($host_name)) {
|
---|
| 205 | $this->KMLPlacemarks[$placemarkPosition]->setDescriptionStatus($descriptionStatus);
|
---|
| 206 | } else {
|
---|
| 207 | $placemark = new KMLPlacemark();
|
---|
[7692] | 208 | $placemark->setID($host_name);
|
---|
[7642] | 209 | $placemark->setName($host_name);
|
---|
| 210 | $placemark->setDescriptionStatus($descriptionStatus);
|
---|
| 211 | $this->addPlacemark($placemark);
|
---|
| 212 | }
|
---|
[7631] | 213 | }
|
---|
| 214 | }
|
---|
[7627] | 215 | }
|
---|
| 216 | ?>
|
---|