source: trunk/src/inc/KMLFile.class.php@ 7700

Last change on this file since 7700 was 7700, checked in by Pieter Naber, 15 years ago

Code van David om te schrijven naar een bestand

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