[7725] | 1 | <?php
|
---|
| 2 | /*
|
---|
| 3 | * Project: NodeMap2.0
|
---|
| 4 | * File: KMLHandler.class.php
|
---|
| 5 | * Purpose: Creating of editing KML files
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | class KMLFile {
|
---|
| 9 | private $template = '
|
---|
| 10 | <kml xmlns="http://www.opengis.net/kml/2.2">
|
---|
| 11 | <Document>
|
---|
| 12 | <name>Wireless Leiden Interactive Nodemap 2.0</name>
|
---|
| 13 | <open>1</open>
|
---|
| 14 | <description>Wireless Leiden Interactive Nodemap 2.0</description>
|
---|
| 15 | <Style id="greenArrowIcon">
|
---|
| 16 | <IconStyle>
|
---|
| 17 | <Icon>
|
---|
[7765] | 18 | <href>%NODE_GREEN%</href>
|
---|
[7725] | 19 | </Icon>
|
---|
| 20 | </IconStyle>
|
---|
| 21 | </Style>
|
---|
| 22 | <Style id="orangeArrowIcon">
|
---|
| 23 | <IconStyle>
|
---|
| 24 | <Icon>
|
---|
[7765] | 25 | <href>%NODE_ORANGE%</href>
|
---|
[7725] | 26 | </Icon>
|
---|
| 27 | </IconStyle>
|
---|
| 28 | </Style>
|
---|
| 29 | <Style id="redArrowIcon">
|
---|
| 30 | <IconStyle>
|
---|
| 31 | <Icon>
|
---|
[7765] | 32 | <href>%NODE_RED%</href>
|
---|
[7725] | 33 | </Icon>
|
---|
| 34 | </IconStyle>
|
---|
| 35 | </Style>
|
---|
[7765] | 36 | <Style id="blackLine">
|
---|
| 37 | <LineStyle>
|
---|
| 38 | <color>%LINE_BLACK%</color>
|
---|
| 39 | <width>3</width>
|
---|
| 40 | </LineStyle>
|
---|
| 41 | </Style>
|
---|
| 42 | <Folder id="nodes">
|
---|
[7725] | 43 | <name>Nodes</name>
|
---|
| 44 | <description>Nodes from the Wireless Leiden network</description>
|
---|
| 45 | <LookAt>
|
---|
[7758] | 46 | <longitude>4.490153</longitude>
|
---|
| 47 | <latitude>52.161087</latitude>
|
---|
[7765] | 48 | <altitude>5</altitude>
|
---|
[7725] | 49 | <heading>0</heading>
|
---|
| 50 | <tilt>0</tilt>
|
---|
| 51 | <range>500</range>
|
---|
| 52 | </LookAt>
|
---|
[7765] | 53 | %NODESCONTENT%
|
---|
[7725] | 54 | </Folder>
|
---|
[7765] | 55 | <Folder id="lines">
|
---|
| 56 | <name>Lines</name>
|
---|
| 57 | <description>Lines from nodes to nodes from the Wireless Leiden network</description>
|
---|
| 58 | <LookAt>
|
---|
| 59 | <longitude>4.490153</longitude>
|
---|
| 60 | <latitude>52.161087</latitude>
|
---|
| 61 | <altitude>5</altitude>
|
---|
| 62 | <heading>0</heading>
|
---|
| 63 | <tilt>0</tilt>
|
---|
| 64 | <range>500</range>
|
---|
| 65 | </LookAt>
|
---|
| 66 | %LINESCONTENT%
|
---|
| 67 | </Folder>
|
---|
| 68 | </Document>
|
---|
[7725] | 69 | </kml>';
|
---|
| 70 |
|
---|
| 71 | // First line of the status file must be like this:
|
---|
| 72 | static $fileFirst = 'type,host_name,has_been_checked,check_execution_time,current_state,last_hard_state,last_check,problem_has_been_acknowledged';
|
---|
| 73 | // Every following line will be checked using these functions
|
---|
| 74 | private $fileContent = array('string', 'string', 'int', 'double', 'int', 'int', 'int', 'int');
|
---|
| 75 |
|
---|
[7765] | 76 | private $KMLNodes;
|
---|
| 77 | private $KMLLines;
|
---|
[7773] | 78 | private $NetworkList;
|
---|
| 79 |
|
---|
[7725] | 80 | /*
|
---|
| 81 | * Function: __construct (constructor)
|
---|
| 82 | * Description: Creating a new KMLFile
|
---|
| 83 | * Parameters: -
|
---|
| 84 | * Returns: -
|
---|
| 85 | */
|
---|
| 86 | public function __construct() {
|
---|
[7765] | 87 | $this->KMLNodes = array();
|
---|
| 88 | $this->KMLLines = array();
|
---|
[7773] | 89 | $this->NetworkList = new NetworkList();
|
---|
[7725] | 90 | }
|
---|
| 91 |
|
---|
| 92 | /*
|
---|
[7765] | 93 | * Function: addNode
|
---|
| 94 | * Description: Add a node to the local node array
|
---|
| 95 | * Parameters: KMLNode $node
|
---|
[7725] | 96 | * Returns: -
|
---|
| 97 | */
|
---|
[7765] | 98 | public function addNode(KMLNode $node) {
|
---|
| 99 | $this->KMLNodes[] = $node;
|
---|
[7725] | 100 | }
|
---|
| 101 |
|
---|
| 102 | /*
|
---|
[7765] | 103 | * Function: addLIne
|
---|
| 104 | * Description: Add a line to the local line array
|
---|
| 105 | * Parameters: KMLLine $line
|
---|
| 106 | * Returns: -
|
---|
| 107 | */
|
---|
| 108 | public function addLine(KMLLine $line) {
|
---|
| 109 | $this->KMLLines[] = $line;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | /*
|
---|
[7725] | 113 | * Function: toString
|
---|
| 114 | * Description: Converts the content of this file and the placemarks to a KML valid string
|
---|
| 115 | * Parameters: -
|
---|
| 116 | * Returns: KML valid string
|
---|
| 117 | */
|
---|
| 118 | public function toString() {
|
---|
| 119 | global $config;
|
---|
| 120 |
|
---|
| 121 | $toString = $this->template;
|
---|
| 122 |
|
---|
[7773] | 123 | // Add all nodes to the string
|
---|
[7765] | 124 | $nodeString = '';
|
---|
| 125 | $nodeCount = count($this->KMLNodes);
|
---|
| 126 | for ($i = 0; $i < $nodeCount; $i++) {
|
---|
| 127 | $nodeString .= $this->KMLNodes[$i]->toString();
|
---|
[7725] | 128 | }
|
---|
| 129 |
|
---|
[7773] | 130 | // Add all connected lines to the string
|
---|
[7765] | 131 | $lineString = '';
|
---|
| 132 | $lineCount = count($this->KMLLines);
|
---|
| 133 | for ($i = 0; $i < $lineCount; $i++) {
|
---|
[7773] | 134 | // If longitude2 and latitude2 aren't set, ignore the lines
|
---|
| 135 | // This happens with all the connections that don't connect 2 nodes
|
---|
| 136 | if ($this->KMLLines[$i]->isConnected()) {
|
---|
| 137 | $lineString .= $this->KMLLines[$i]->toString();
|
---|
| 138 | }
|
---|
[7765] | 139 | }
|
---|
[7725] | 140 |
|
---|
[7765] | 141 | $toString = str_replace('%NODE_GREEN%', $config['node_green'], $toString);
|
---|
| 142 | $toString = str_replace('%NODE_ORANGE%', $config['node_orange'], $toString);
|
---|
| 143 | $toString = str_replace('%NODE_RED%', $config['node_red'], $toString);
|
---|
| 144 | $toString = str_replace('%LINE_BLACK%', $config['line_black'], $toString);
|
---|
| 145 | $toString = str_replace('%NODESCONTENT%', $nodeString, $toString);
|
---|
| 146 | $toString = str_replace('%LINESCONTENT%', $lineString, $toString);
|
---|
[7773] | 147 |
|
---|
[7725] | 148 | return $toString;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | /*
|
---|
[7765] | 152 | * Function: getNodeByName
|
---|
| 153 | * Description: Find the first KMLNode with the name $name and return its position. If not found, return false
|
---|
[7725] | 154 | * Parameters: string $name
|
---|
[7765] | 155 | * Returns: Position of node in our array, if not found false
|
---|
[7725] | 156 | */
|
---|
[7765] | 157 | public function getNodeByName($name) {
|
---|
| 158 | $nodesCount = count($this->KMLNodes);
|
---|
[7725] | 159 | for ($i = 0; $i < $nodesCount; $i++) {
|
---|
[7765] | 160 | if ($this->KMLNodes[$i]->getName() == $name) {
|
---|
[7725] | 161 | return $i;
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | return false;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | /*
|
---|
| 169 | * Function: parseLocationFile
|
---|
[7765] | 170 | * Description: Parse the node location file updating or adding KMLNode objects to the current KMLFile object
|
---|
[7725] | 171 | * Parameters: string $file
|
---|
| 172 | * Returns: true is successfull, otherwise false
|
---|
| 173 | */
|
---|
| 174 | public function parseLocationFile($file) {
|
---|
[7773] | 175 | // We want to find all the nodes in the location file and store information of the nodes...
|
---|
[7725] | 176 | $nodesCount = preg_match_all('/\[[a-zA-Z0-9]*\]/i', $file, $nodes, PREG_OFFSET_CAPTURE);
|
---|
| 177 | for ($i = 0; $i < $nodesCount; $i++) {
|
---|
| 178 | // Looking for "location" of the node
|
---|
[7773] | 179 | if (!$location = $this->findInLocationFile($file, 'location = ', $nodes[0][$i][1])) {
|
---|
[7725] | 180 | trigger_log(SYSLOG_WARNING, 'Could not find the "location" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
|
---|
| 181 | continue;
|
---|
| 182 | }
|
---|
| 183 | // Looking for "status" of the node
|
---|
[7773] | 184 | if (!$status = $this->findInLocationFile($file, 'status = ', $nodes[0][$i][1])) {
|
---|
[7725] | 185 | trigger_log(SYSLOG_WARNING, 'Could not find the "status" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
|
---|
| 186 | continue;
|
---|
| 187 | }
|
---|
| 188 | // Looking for "latitude" of the node
|
---|
[7773] | 189 | if (!$latitude = $this->findInLocationFile($file, 'latitude = ', $nodes[0][$i][1])) {
|
---|
[7725] | 190 | trigger_log(SYSLOG_WARNING, 'Could not find the "latitude" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
|
---|
| 191 | continue;
|
---|
| 192 | }
|
---|
| 193 | // Looking for "longitude" of the node
|
---|
[7773] | 194 | if (!$longitude = $this->findInLocationFile($file, 'longitude = ', $nodes[0][$i][1])) {
|
---|
[7725] | 195 | trigger_log(SYSLOG_WARNING, 'Could not find the "longitude" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
|
---|
| 196 | continue;
|
---|
| 197 | }
|
---|
| 198 | // Looking for "interfaces" of the node
|
---|
[7773] | 199 | if (!$interfaces = $this->findInLocationFile($file, 'interfaces = ', $nodes[0][$i][1])) {
|
---|
[7725] | 200 | trigger_log(SYSLOG_WARNING, 'Could not find the "interfaces" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
|
---|
| 201 | continue;
|
---|
| 202 | }
|
---|
| 203 | // Looking for "masterip" of the node
|
---|
[7773] | 204 | if (!$masterip = $this->findInLocationFile($file, 'masterip = ', $nodes[0][$i][1])) {
|
---|
[7725] | 205 | trigger_log(SYSLOG_WARNING, 'Could not find the "masterip" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
|
---|
| 206 | continue;
|
---|
| 207 | }
|
---|
| 208 | // Looking for "nodetype" of the node
|
---|
[7773] | 209 | if (!$nodetype = $this->findInLocationFile($file, 'nodetype = ', $nodes[0][$i][1])) {
|
---|
[7725] | 210 | trigger_log(SYSLOG_WARNING, 'Could not find the "nodetype" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
|
---|
| 211 | continue;
|
---|
| 212 | }
|
---|
| 213 | // Looking for "name" of the node
|
---|
[7773] | 214 | if (!$name = $this->findInLocationFile($file, 'name = ', $nodes[0][$i][1])) {
|
---|
[7725] | 215 | trigger_log(SYSLOG_WARNING, 'Could not find the "name" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
|
---|
| 216 | continue;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | // Creating a string with the complete description of the node using all data in the location file
|
---|
| 220 | $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/>';
|
---|
| 221 |
|
---|
[7765] | 222 | if ($placemarkPosition = $this->getNodeByName($name)) {
|
---|
[7725] | 223 | // Updating an excisting placemark
|
---|
[7765] | 224 | $this->KMLNodes[$placemarkPosition]->setDescriptionLocation($descriptionLocation);
|
---|
| 225 | $this->KMLNodes[$placemarkPosition]->setLongitude($longitude);
|
---|
| 226 | $this->KMLNodes[$placemarkPosition]->setLatitude($latitude);
|
---|
[7725] | 227 | } else {
|
---|
| 228 | // Adding a new placemark
|
---|
[7765] | 229 | $placemark = new KMLNode();
|
---|
[7725] | 230 | $placemark->setID($name);
|
---|
| 231 | $placemark->setName($name);
|
---|
| 232 | $placemark->setDescriptionLocation($descriptionLocation);
|
---|
| 233 | $placemark->setLongitude($longitude);
|
---|
| 234 | $placemark->setLatitude($latitude);
|
---|
[7765] | 235 | $this->addNode($placemark);
|
---|
[7725] | 236 | }
|
---|
[7773] | 237 |
|
---|
| 238 | // Now let's find all interlinks with this node
|
---|
| 239 | $position = $nodes[0][$i][1];
|
---|
| 240 | while (($position = strpos($file, 'ip=', $position + 1)) && (isset($nodes[0][$i + 1][1]) && $position < $nodes[0][$i + 1][1])) {
|
---|
| 241 | $ipAddress = $this->findInLocationFile($file, 'ip=', $position);
|
---|
| 242 | $posSlash = strpos($ipAddress, '/');
|
---|
| 243 | $ip = substr($ipAddress, 0, $posSlash);
|
---|
| 244 | $netmask = substr($ipAddress, $posSlash + 1);
|
---|
| 245 |
|
---|
| 246 | $posNetwork = $this->NetworkList->find($ip);
|
---|
| 247 | if ($posNetwork) {
|
---|
| 248 | // Network already excists in list, just need to add longitude2 en latitude2 to KMLLine
|
---|
| 249 | // and add the node name to the name of the line.
|
---|
| 250 | // Position in network equals position in KMLLine array :-)
|
---|
| 251 |
|
---|
| 252 | $this->KMLLines[$posNetwork]->setName($this->KMLLines[$posNetwork]->getName() . $name);
|
---|
| 253 | $this->KMLLines[$posNetwork]->setLongitude2($longitude);
|
---|
| 254 | $this->KMLLines[$posNetwork]->setLatitude2($latitude);
|
---|
| 255 | } else {
|
---|
| 256 | // Network doesn't excist. We create a new network and a new line.
|
---|
| 257 |
|
---|
| 258 | $network = new Network($ip, $netmask);
|
---|
| 259 | $this->NetworkList->add($network);
|
---|
| 260 |
|
---|
| 261 | $line = new KMLLine();
|
---|
| 262 | $line->setID($network->networkDecimal);
|
---|
| 263 | $line->setName('Link: Van ' . $name . ' naar ');
|
---|
| 264 | $line->setDescription($network->toString());
|
---|
| 265 | $line->setLongitude1($longitude);
|
---|
| 266 | $line->setLatitude1($latitude);
|
---|
| 267 | $this->KMLLines[] = $line;
|
---|
| 268 | }
|
---|
| 269 | }
|
---|
[7725] | 270 | }
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | /*
|
---|
| 274 | * Function: findInLocationFile
|
---|
| 275 | * Description: Find the $keyword in $file and return the value of $keyword, starting at $offset
|
---|
| 276 | * Parameters: string $file, string $keyword, integer $offset
|
---|
| 277 | * Returns: The value of the keyword if found, otherwise return false
|
---|
| 278 | */
|
---|
| 279 | private function findInLocationFile($file, $keyword, $offset) {
|
---|
[7773] | 280 | $start = strpos($file, $keyword, $offset) + strlen($keyword);
|
---|
[7725] | 281 | $end = strpos($file, "\n", $start);
|
---|
| 282 |
|
---|
| 283 | if ($start && $end && ($start < $end)) {
|
---|
| 284 | return substr($file, $start, $end - $start);
|
---|
| 285 | } else {
|
---|
| 286 | return false;
|
---|
| 287 | }
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | /*
|
---|
| 291 | * Function: parseStatusFile
|
---|
[7765] | 292 | * Description: Parse the node status file updating or adding KMLNode objects to the current KMLFile object
|
---|
[7725] | 293 | * Parameters: string $file
|
---|
| 294 | * Returns: true is successfull, otherwise false
|
---|
| 295 | */
|
---|
| 296 | public function parseStatusFile($file) {
|
---|
| 297 | $fileContents = explode("\r\n", $file);
|
---|
| 298 |
|
---|
| 299 | if ($fileContents[0] != KMLFile::$fileFirst) {
|
---|
| 300 | trigger_log(SYSLOG_WARNING, 'Contents of file do not match with template of first line', __FILE__, __LINE__);
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | // For loop for all the lines in the file. Skipping first line (headers) and last line (blank)
|
---|
| 304 | $linesCount = count($fileContents);
|
---|
| 305 | for ($i = 1; $i < $linesCount - 1; $i++) {
|
---|
| 306 | $lineContent = explode(',', $fileContents[$i]);
|
---|
| 307 |
|
---|
| 308 | if (count($lineContent) != count($this->fileContent)) {
|
---|
| 309 | trigger_log(LOG_WARNING, 'Contents of the file do not match with template of lines on line "' . $i . '"', __FILE__, __LINE__);
|
---|
| 310 | continue;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | // Checking for valid entries on this line
|
---|
| 314 | for ($j = 0; $j < 8; $j++) {
|
---|
| 315 | try {
|
---|
| 316 | switch ($this->fileContent[$j]) {
|
---|
| 317 | case 'string':
|
---|
| 318 | $lineContent[$j] = (string) $lineContent[$j];
|
---|
| 319 | break;
|
---|
| 320 | case 'int':
|
---|
| 321 | $lineContent[$j] = (int) $lineContent[$j];
|
---|
| 322 | break;
|
---|
| 323 | case 'double':
|
---|
| 324 | $lineContent[$j] = (double) $lineContent[$j];
|
---|
| 325 | break;
|
---|
| 326 | default:
|
---|
| 327 | break;
|
---|
| 328 | }
|
---|
| 329 | } catch (Exception $err) {
|
---|
| 330 | trigger_log(SYSLOG_WARNING, 'The value "' . $j . '" on line "' . $i . '" is not valid, skipping to next line', __FILE__, __LINE__);
|
---|
| 331 | continue;
|
---|
| 332 | }
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | // Creating a string with the complete description of the node using all data in the status file
|
---|
| 336 | $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/>';
|
---|
| 337 |
|
---|
[7765] | 338 | if ($placemarkPosition = $this->getNodeByName($lineContent[1])) {
|
---|
[7725] | 339 | // Updating an excisting placemark
|
---|
[7765] | 340 | $this->KMLNodes[$placemarkPosition]->setDescriptionStatus($descriptionStatus);
|
---|
[7725] | 341 | } else {
|
---|
| 342 | // Adding a new placemark
|
---|
[7765] | 343 | $placemark = new KMLNode();
|
---|
[7725] | 344 | $placemark->setID($lineContent[1]);
|
---|
| 345 | $placemark->setName($lineContent[1]);
|
---|
| 346 | $placemark->setDescriptionStatus($descriptionStatus);
|
---|
[7765] | 347 | $this->addNode($placemark);
|
---|
[7725] | 348 | }
|
---|
| 349 | }
|
---|
| 350 | }
|
---|
| 351 | }
|
---|
[7627] | 352 | ?>
|
---|