[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 |
|
---|
[7720] | 53 | // First line of the status file must be like this:
|
---|
[7637] | 54 | static $fileFirst = 'type,host_name,has_been_checked,check_execution_time,current_state,last_hard_state,last_check,problem_has_been_acknowledged';
|
---|
[7720] | 55 | // Every following line will be checked using these functions
|
---|
| 56 | private $fileContent = array('string', 'string', 'int', 'double', 'int', 'int', 'int', 'int');
|
---|
[7627] | 57 |
|
---|
[7644] | 58 | private $KMLPlacemarks;
|
---|
[7627] | 59 |
|
---|
| 60 | /*
|
---|
| 61 | * Function: __construct (constructor)
|
---|
[7720] | 62 | * Description: Creating a new KMLFile
|
---|
[7627] | 63 | * Parameters: -
|
---|
[7720] | 64 | * Returns: -
|
---|
[7627] | 65 | */
|
---|
[7631] | 66 | public function __construct() {
|
---|
[7644] | 67 | $this->KMLPlacemarks = array();
|
---|
[7627] | 68 | }
|
---|
| 69 |
|
---|
[7720] | 70 | /*
|
---|
| 71 | * Function: addPlacemark
|
---|
| 72 | * Description: Add a placemark to the local placemark array
|
---|
| 73 | * Parameters: KMLPlacemark $placemark
|
---|
| 74 | * Returns: -
|
---|
| 75 | */
|
---|
[7631] | 76 | public function addPlacemark(KMLPlacemark $placemark) {
|
---|
[7628] | 77 | $this->KMLPlacemarks[] = $placemark;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[7720] | 80 | /*
|
---|
| 81 | * Function: toString
|
---|
| 82 | * Description: Converts the content of this file and the placemarks to a KML valid string
|
---|
| 83 | * Parameters: -
|
---|
| 84 | * Returns: KML valid string
|
---|
| 85 | */
|
---|
[7631] | 86 | public function toString() {
|
---|
[7720] | 87 | global $config;
|
---|
| 88 |
|
---|
[7631] | 89 | $toString = $this->template;
|
---|
[7627] | 90 |
|
---|
| 91 | $placemarkString = '';
|
---|
| 92 | $placemarkCount = count($this->KMLPlacemarks);
|
---|
| 93 | for ($i = 0; $i < $placemarkCount; $i++) {
|
---|
[7631] | 94 | $placemarkString .= $this->KMLPlacemarks[$i]->toString();
|
---|
[7627] | 95 | }
|
---|
| 96 |
|
---|
[7702] | 97 | $toString = str_replace('%PLACEMARK_GREEN%', $config['placemark_green'], $toString);
|
---|
| 98 | $toString = str_replace('%PLACEMARK_ORANGE%', $config['placemark_orange'], $toString);
|
---|
| 99 | $toString = str_replace('%PLACEMARK_RED%', $config['placemark_red'], $toString);
|
---|
[7631] | 100 | $toString = str_replace('%CONTENT%', $placemarkString, $toString);
|
---|
[7627] | 101 |
|
---|
| 102 | return $toString;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[7700] | 105 | /*
|
---|
| 106 | * Function: write
|
---|
[7720] | 107 | * Description: Write KMLFile to a KML file
|
---|
[7700] | 108 | * Parameters: string $filename
|
---|
[7720] | 109 | * Returns: true if we can write to the file, otherwise false
|
---|
[7700] | 110 | */
|
---|
[7631] | 111 | public function write($filename) {
|
---|
[7720] | 112 | // TODO: David: Needs to be placed in FileHandler.class.php. Here we just want to call our file handler.
|
---|
| 113 |
|
---|
[7700] | 114 | trigger_log(SYSLOG_DEBUG, 'Opening the file "' . $filename . '"', __FILE__, __LINE__);
|
---|
| 115 | $file = fopen("$filename","w")
|
---|
| 116 | or exit(trigger_log(SYSLOG_ERR, 'Opening the file "' . $filename . '"', __FILE__, __LINE__));
|
---|
| 117 |
|
---|
| 118 | trigger_log(SYSLOG_DEBUG, 'Writing file "' . $filename . '"', __FILE__, __LINE__);
|
---|
| 119 | fwrite($file,$toString());
|
---|
| 120 |
|
---|
| 121 | trigger_log(SYSLOG_DEBUG, 'Closing file "' . $filename . '"', __FILE__, __LINE__);
|
---|
| 122 | fclose($file);
|
---|
[7627] | 123 | }
|
---|
[7631] | 124 |
|
---|
[7642] | 125 | /*
|
---|
| 126 | * Function: getPlacemarkByName
|
---|
[7720] | 127 | * Description: Find the first KMLPlacemark with the name $name and return its position. If not found, return false
|
---|
[7642] | 128 | * Parameters: string $name
|
---|
[7720] | 129 | * Returns: Position of placemark in our array, if not found false
|
---|
[7642] | 130 | */
|
---|
| 131 | public function getPlacemarkByName($name) {
|
---|
| 132 | $nodesCount = count($this->KMLPlacemarks);
|
---|
| 133 | for ($i = 0; $i < $nodesCount; $i++) {
|
---|
| 134 | if ($this->KMLPlacemarks[$i]->getName() == $name) {
|
---|
| 135 | return $i;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
[7720] | 138 |
|
---|
[7642] | 139 | return false;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | /*
|
---|
| 143 | * Function: parseLocationFile
|
---|
[7720] | 144 | * Description: Parse the node location file updating or adding KMLPlacemark objects to the current KMLFile object
|
---|
[7642] | 145 | * Parameters: string $file
|
---|
[7720] | 146 | * Returns: true is successfull, otherwise false
|
---|
[7642] | 147 | */
|
---|
[7640] | 148 | public function parseLocationFile($file) {
|
---|
| 149 | $nodesCount = preg_match_all('/\[[a-zA-Z0-9]*\]/i', $file, $nodes, PREG_OFFSET_CAPTURE);
|
---|
| 150 | for ($i = 0; $i < $nodesCount; $i++) {
|
---|
[7720] | 151 | // Looking for "location" of the node
|
---|
| 152 | if (!$location = $this->findInLocationFile($file, 'location', $nodes[0][$i][1])) {
|
---|
| 153 | trigger_log(SYSLOG_WARNING, 'Could not find the "location" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
|
---|
| 154 | continue;
|
---|
| 155 | }
|
---|
| 156 | // Looking for "status" of the node
|
---|
| 157 | if (!$status = $this->findInLocationFile($file, 'status', $nodes[0][$i][1])) {
|
---|
| 158 | trigger_log(SYSLOG_WARNING, 'Could not find the "status" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
|
---|
| 159 | continue;
|
---|
| 160 | }
|
---|
| 161 | // Looking for "latitude" of the node
|
---|
| 162 | if (!$latitude = $this->findInLocationFile($file, 'latitude', $nodes[0][$i][1])) {
|
---|
| 163 | trigger_log(SYSLOG_WARNING, 'Could not find the "latitude" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
|
---|
| 164 | continue;
|
---|
| 165 | }
|
---|
| 166 | // Looking for "longitude" of the node
|
---|
| 167 | if (!$longitude = $this->findInLocationFile($file, 'longitude', $nodes[0][$i][1])) {
|
---|
| 168 | trigger_log(SYSLOG_WARNING, 'Could not find the "longitude" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
|
---|
| 169 | continue;
|
---|
| 170 | }
|
---|
| 171 | // Looking for "interfaces" of the node
|
---|
| 172 | if (!$interfaces = $this->findInLocationFile($file, 'interfaces', $nodes[0][$i][1])) {
|
---|
| 173 | trigger_log(SYSLOG_WARNING, 'Could not find the "interfaces" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
|
---|
| 174 | continue;
|
---|
| 175 | }
|
---|
| 176 | // Looking for "masterip" of the node
|
---|
| 177 | if (!$masterip = $this->findInLocationFile($file, 'masterip', $nodes[0][$i][1])) {
|
---|
| 178 | trigger_log(SYSLOG_WARNING, 'Could not find the "masterip" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
|
---|
| 179 | continue;
|
---|
| 180 | }
|
---|
| 181 | // Looking for "nodetype" of the node
|
---|
| 182 | if (!$nodetype = $this->findInLocationFile($file, 'nodetype', $nodes[0][$i][1])) {
|
---|
| 183 | trigger_log(SYSLOG_WARNING, 'Could not find the "nodetype" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
|
---|
| 184 | continue;
|
---|
| 185 | }
|
---|
| 186 | // Looking for "name" of the node
|
---|
| 187 | if (!$name = $this->findInLocationFile($file, 'name', $nodes[0][$i][1])) {
|
---|
| 188 | trigger_log(SYSLOG_WARNING, 'Could not find the "name" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
|
---|
| 189 | continue;
|
---|
| 190 | }
|
---|
[7640] | 191 |
|
---|
[7720] | 192 | // Creating a string with the complete description of the node using all data in the location file
|
---|
[7642] | 193 | $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/>';
|
---|
| 194 |
|
---|
| 195 | if ($placemarkPosition = $this->getPlacemarkByName($name)) {
|
---|
[7720] | 196 | // Updating an excisting placemark
|
---|
[7642] | 197 | $this->KMLPlacemarks[$placemarkPosition]->setDescriptionLocation($descriptionLocation);
|
---|
| 198 | $this->KMLPlacemarks[$placemarkPosition]->setLongitude($longitude);
|
---|
| 199 | $this->KMLPlacemarks[$placemarkPosition]->setLatitude($latitude);
|
---|
| 200 | } else {
|
---|
[7720] | 201 | // Adding a new placemark
|
---|
[7642] | 202 | $placemark = new KMLPlacemark();
|
---|
[7692] | 203 | $placemark->setID($name);
|
---|
[7642] | 204 | $placemark->setName($name);
|
---|
| 205 | $placemark->setDescriptionLocation($descriptionLocation);
|
---|
| 206 | $placemark->setLongitude($longitude);
|
---|
| 207 | $placemark->setLatitude($latitude);
|
---|
| 208 | $this->addPlacemark($placemark);
|
---|
| 209 | }
|
---|
[7640] | 210 | }
|
---|
| 211 | }
|
---|
| 212 |
|
---|
[7642] | 213 | /*
|
---|
| 214 | * Function: findInLocationFile
|
---|
[7720] | 215 | * Description: Find the $keyword in $file and return the value of $keyword, starting at $offset
|
---|
[7642] | 216 | * Parameters: string $file, string $keyword, integer $offset
|
---|
[7720] | 217 | * Returns: The value of the keyword if found, otherwise return false
|
---|
[7642] | 218 | */
|
---|
[7640] | 219 | private function findInLocationFile($file, $keyword, $offset) {
|
---|
[7720] | 220 | $start = strpos($file, $keyword, $offset) + strlen($keyword . ' = ');
|
---|
[7640] | 221 | $end = strpos($file, "\n", $start);
|
---|
[7642] | 222 |
|
---|
| 223 | if ($start && $end && ($start < $end)) {
|
---|
| 224 | return substr($file, $start, $end - $start);
|
---|
| 225 | } else {
|
---|
| 226 | return false;
|
---|
| 227 | }
|
---|
[7640] | 228 | }
|
---|
| 229 |
|
---|
[7642] | 230 | /*
|
---|
| 231 | * Function: parseStatusFile
|
---|
[7720] | 232 | * Description: Parse the node status file updating or adding KMLPlacemark objects to the current KMLFile object
|
---|
[7642] | 233 | * Parameters: string $file
|
---|
[7720] | 234 | * Returns: true is successfull, otherwise false
|
---|
[7642] | 235 | */
|
---|
[7640] | 236 | public function parseStatusFile($file) {
|
---|
[7637] | 237 | $fileContents = explode("\r\n", $file);
|
---|
[7631] | 238 |
|
---|
[7637] | 239 | if ($fileContents[0] != KMLFile::$fileFirst) {
|
---|
[7720] | 240 | trigger_log(SYSLOG_WARNING, 'Contents of file do not match with template of first line', __FILE__, __LINE__);
|
---|
[7631] | 241 | }
|
---|
| 242 |
|
---|
[7720] | 243 | // For loop for all the lines in the file. Skipping first line (headers) and last line (blank)
|
---|
[7642] | 244 | $linesCount = count($fileContents);
|
---|
| 245 | for ($i = 1; $i < $linesCount - 1; $i++) {
|
---|
[7631] | 246 | $lineContent = explode(',', $fileContents[$i]);
|
---|
| 247 |
|
---|
| 248 | if (count($lineContent) != count($this->fileContent)) {
|
---|
[7720] | 249 | trigger_log(LOG_WARNING, 'Contents of the file do not match with template of lines on line "' . $i . '"', __FILE__, __LINE__);
|
---|
| 250 | continue;
|
---|
[7631] | 251 | }
|
---|
| 252 |
|
---|
[7720] | 253 | // Checking for valid entries on this line
|
---|
| 254 | for ($j = 0; $j < 8; $j++) {
|
---|
| 255 | try {
|
---|
| 256 | switch ($this->fileContent[$j]) {
|
---|
| 257 | case 'string':
|
---|
| 258 | $lineContent[$j] = (string) $lineContent[$j];
|
---|
| 259 | break;
|
---|
| 260 | case 'int':
|
---|
| 261 | $lineContent[$j] = (int) $lineContent[$j];
|
---|
| 262 | break;
|
---|
| 263 | case 'double':
|
---|
| 264 | $lineContent[$j] = (double) $lineContent[$j];
|
---|
| 265 | break;
|
---|
| 266 | default:
|
---|
| 267 | break;
|
---|
| 268 | }
|
---|
| 269 | } catch (Exception $err) {
|
---|
| 270 | trigger_log(SYSLOG_WARNING, 'The value "' . $j . '" on line "' . $i . '" is not valid, skipping to next line', __FILE__, __LINE__);
|
---|
| 271 | continue;
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
[7642] | 274 |
|
---|
[7720] | 275 | // Creating a string with the complete description of the node using all data in the status file
|
---|
| 276 | $descriptionStatus = 'Type: ' . $lineContent[0] . '<br/>Host name: ' . $lineContent[1] . '<br/>Has been checked: ' . $lineContent[2] . '<br/>Check execution time: ' . $lineContent[3] . '<br/>Currenr state: ' . $lineContent[4] . '<br/>Last hard state: ' . $lineContent[5] . '<br/>Last check: ' . $lineContent[6] . '<br/>Problem has been acknowledged: ' . $lineContent[7] . '<br/><br/>';
|
---|
[7642] | 277 |
|
---|
[7720] | 278 | if ($placemarkPosition = $this->getPlacemarkByName($lineContent[1])) {
|
---|
| 279 | // Updating an excisting placemark
|
---|
[7642] | 280 | $this->KMLPlacemarks[$placemarkPosition]->setDescriptionStatus($descriptionStatus);
|
---|
| 281 | } else {
|
---|
[7720] | 282 | // Adding a new placemark
|
---|
[7642] | 283 | $placemark = new KMLPlacemark();
|
---|
[7720] | 284 | $placemark->setID($lineContent[1]);
|
---|
| 285 | $placemark->setName($lineContent[1]);
|
---|
[7642] | 286 | $placemark->setDescriptionStatus($descriptionStatus);
|
---|
| 287 | $this->addPlacemark($placemark);
|
---|
| 288 | }
|
---|
[7631] | 289 | }
|
---|
| 290 | }
|
---|
[7627] | 291 | }
|
---|
| 292 | ?>
|
---|