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

Last change on this file since 7765 was 7765, checked in by Pieter Naber, 16 years ago

Fixed loghandler so index.php outputs a KML valid file. Also added the possibility to add lines.

  • Property svn:eol-style set to native
File size: 11.3 KB
Line 
1<?php
2/*
3 * Project: NodeMap2.0
4 * File: KMLHandler.class.php
5 * Purpose: Creating of editing KML files
6 */
7
8class 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>
18 <href>%NODE_GREEN%</href>
19 </Icon>
20 </IconStyle>
21 </Style>
22 <Style id="orangeArrowIcon">
23 <IconStyle>
24 <Icon>
25 <href>%NODE_ORANGE%</href>
26 </Icon>
27 </IconStyle>
28 </Style>
29 <Style id="redArrowIcon">
30 <IconStyle>
31 <Icon>
32 <href>%NODE_RED%</href>
33 </Icon>
34 </IconStyle>
35 </Style>
36 <Style id="blackLine">
37 <LineStyle>
38 <color>%LINE_BLACK%</color>
39 <width>3</width>
40 </LineStyle>
41 </Style>
42 <Folder id="nodes">
43 <name>Nodes</name>
44 <description>Nodes from the Wireless Leiden network</description>
45 <LookAt>
46 <longitude>4.490153</longitude>
47 <latitude>52.161087</latitude>
48 <altitude>5</altitude>
49 <heading>0</heading>
50 <tilt>0</tilt>
51 <range>500</range>
52 </LookAt>
53 %NODESCONTENT%
54 </Folder>
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>
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
76 private $KMLNodes;
77 private $KMLLines;
78
79 /*
80 * Function: __construct (constructor)
81 * Description: Creating a new KMLFile
82 * Parameters: -
83 * Returns: -
84 */
85 public function __construct() {
86 $this->KMLNodes = array();
87 $this->KMLLines = array();
88 }
89
90 /*
91 * Function: addNode
92 * Description: Add a node to the local node array
93 * Parameters: KMLNode $node
94 * Returns: -
95 */
96 public function addNode(KMLNode $node) {
97 $this->KMLNodes[] = $node;
98 }
99
100 /*
101 * Function: addLIne
102 * Description: Add a line to the local line array
103 * Parameters: KMLLine $line
104 * Returns: -
105 */
106 public function addLine(KMLLine $line) {
107 $this->KMLLines[] = $line;
108 }
109
110 /*
111 * Function: toString
112 * Description: Converts the content of this file and the placemarks to a KML valid string
113 * Parameters: -
114 * Returns: KML valid string
115 */
116 public function toString() {
117 global $config;
118
119 $toString = $this->template;
120
121 $nodeString = '';
122 $nodeCount = count($this->KMLNodes);
123 for ($i = 0; $i < $nodeCount; $i++) {
124 $nodeString .= $this->KMLNodes[$i]->toString();
125 }
126
127 $lineString = '';
128 $lineCount = count($this->KMLLines);
129 for ($i = 0; $i < $lineCount; $i++) {
130 $lineString .= $this->KMLLines[$i]->toString();
131 }
132
133 $toString = str_replace('%NODE_GREEN%', $config['node_green'], $toString);
134 $toString = str_replace('%NODE_ORANGE%', $config['node_orange'], $toString);
135 $toString = str_replace('%NODE_RED%', $config['node_red'], $toString);
136 $toString = str_replace('%LINE_BLACK%', $config['line_black'], $toString);
137 $toString = str_replace('%NODESCONTENT%', $nodeString, $toString);
138 $toString = str_replace('%LINESCONTENT%', $lineString, $toString);
139
140 return $toString;
141 }
142
143 /*
144 * Function: write
145 * Description: Write KMLFile to a KML file
146 * Parameters: string $filename
147 * Returns: true if we can write to the file, otherwise false
148 */
149 public function write($filename) {
150 // TODO: David: Needs to be placed in FileHandler.class.php. Here we just want to call our file handler.
151
152 trigger_log(SYSLOG_DEBUG, 'Opening the file "' . $filename . '"', __FILE__, __LINE__);
153 $file = new FileHandler($filename, 'w')
154 or trigger_log(SYSLOG_ERR, 'Opening the file "' . $filename . '"', __FILE__, __LINE__);
155
156 trigger_log(SYSLOG_DEBUG, 'Writing file "' . $filename . '"', __FILE__, __LINE__);
157 $file->write($file,$this->toString());
158
159 trigger_log(SYSLOG_DEBUG, 'Closing file "' . $filename . '"', __FILE__, __LINE__);
160 fclose($file);
161 }
162
163 /*
164 * Function: getNodeByName
165 * Description: Find the first KMLNode with the name $name and return its position. If not found, return false
166 * Parameters: string $name
167 * Returns: Position of node in our array, if not found false
168 */
169 public function getNodeByName($name) {
170 $nodesCount = count($this->KMLNodes);
171 for ($i = 0; $i < $nodesCount; $i++) {
172 if ($this->KMLNodes[$i]->getName() == $name) {
173 return $i;
174 }
175 }
176
177 return false;
178 }
179
180 /*
181 * Function: parseLocationFile
182 * Description: Parse the node location file updating or adding KMLNode objects to the current KMLFile object
183 * Parameters: string $file
184 * Returns: true is successfull, otherwise false
185 */
186 public function parseLocationFile($file) {
187 $nodesCount = preg_match_all('/\[[a-zA-Z0-9]*\]/i', $file, $nodes, PREG_OFFSET_CAPTURE);
188 for ($i = 0; $i < $nodesCount; $i++) {
189 // Looking for "location" of the node
190 if (!$location = $this->findInLocationFile($file, 'location', $nodes[0][$i][1])) {
191 trigger_log(SYSLOG_WARNING, 'Could not find the "location" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
192 continue;
193 }
194 // Looking for "status" of the node
195 if (!$status = $this->findInLocationFile($file, 'status', $nodes[0][$i][1])) {
196 trigger_log(SYSLOG_WARNING, 'Could not find the "status" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
197 continue;
198 }
199 // Looking for "latitude" of the node
200 if (!$latitude = $this->findInLocationFile($file, 'latitude', $nodes[0][$i][1])) {
201 trigger_log(SYSLOG_WARNING, 'Could not find the "latitude" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
202 continue;
203 }
204 // Looking for "longitude" of the node
205 if (!$longitude = $this->findInLocationFile($file, 'longitude', $nodes[0][$i][1])) {
206 trigger_log(SYSLOG_WARNING, 'Could not find the "longitude" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
207 continue;
208 }
209 // Looking for "interfaces" of the node
210 if (!$interfaces = $this->findInLocationFile($file, 'interfaces', $nodes[0][$i][1])) {
211 trigger_log(SYSLOG_WARNING, 'Could not find the "interfaces" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
212 continue;
213 }
214 // Looking for "masterip" of the node
215 if (!$masterip = $this->findInLocationFile($file, 'masterip', $nodes[0][$i][1])) {
216 trigger_log(SYSLOG_WARNING, 'Could not find the "masterip" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
217 continue;
218 }
219 // Looking for "nodetype" of the node
220 if (!$nodetype = $this->findInLocationFile($file, 'nodetype', $nodes[0][$i][1])) {
221 trigger_log(SYSLOG_WARNING, 'Could not find the "nodetype" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
222 continue;
223 }
224 // Looking for "name" of the node
225 if (!$name = $this->findInLocationFile($file, 'name', $nodes[0][$i][1])) {
226 trigger_log(SYSLOG_WARNING, 'Could not find the "name" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
227 continue;
228 }
229
230 // Creating a string with the complete description of the node using all data in the location file
231 $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/>';
232
233 if ($placemarkPosition = $this->getNodeByName($name)) {
234 // Updating an excisting placemark
235 $this->KMLNodes[$placemarkPosition]->setDescriptionLocation($descriptionLocation);
236 $this->KMLNodes[$placemarkPosition]->setLongitude($longitude);
237 $this->KMLNodes[$placemarkPosition]->setLatitude($latitude);
238 } else {
239 // Adding a new placemark
240 $placemark = new KMLNode();
241 $placemark->setID($name);
242 $placemark->setName($name);
243 $placemark->setDescriptionLocation($descriptionLocation);
244 $placemark->setLongitude($longitude);
245 $placemark->setLatitude($latitude);
246 $this->addNode($placemark);
247 }
248 }
249 }
250
251 /*
252 * Function: findInLocationFile
253 * Description: Find the $keyword in $file and return the value of $keyword, starting at $offset
254 * Parameters: string $file, string $keyword, integer $offset
255 * Returns: The value of the keyword if found, otherwise return false
256 */
257 private function findInLocationFile($file, $keyword, $offset) {
258 $start = strpos($file, $keyword, $offset) + strlen($keyword . ' = ');
259 $end = strpos($file, "\n", $start);
260
261 if ($start && $end && ($start < $end)) {
262 return substr($file, $start, $end - $start);
263 } else {
264 return false;
265 }
266 }
267
268 /*
269 * Function: parseStatusFile
270 * Description: Parse the node status file updating or adding KMLNode objects to the current KMLFile object
271 * Parameters: string $file
272 * Returns: true is successfull, otherwise false
273 */
274 public function parseStatusFile($file) {
275 $fileContents = explode("\r\n", $file);
276
277 if ($fileContents[0] != KMLFile::$fileFirst) {
278 trigger_log(SYSLOG_WARNING, 'Contents of file do not match with template of first line', __FILE__, __LINE__);
279 }
280
281 // For loop for all the lines in the file. Skipping first line (headers) and last line (blank)
282 $linesCount = count($fileContents);
283 for ($i = 1; $i < $linesCount - 1; $i++) {
284 $lineContent = explode(',', $fileContents[$i]);
285
286 if (count($lineContent) != count($this->fileContent)) {
287 trigger_log(LOG_WARNING, 'Contents of the file do not match with template of lines on line "' . $i . '"', __FILE__, __LINE__);
288 continue;
289 }
290
291 // Checking for valid entries on this line
292 for ($j = 0; $j < 8; $j++) {
293 try {
294 switch ($this->fileContent[$j]) {
295 case 'string':
296 $lineContent[$j] = (string) $lineContent[$j];
297 break;
298 case 'int':
299 $lineContent[$j] = (int) $lineContent[$j];
300 break;
301 case 'double':
302 $lineContent[$j] = (double) $lineContent[$j];
303 break;
304 default:
305 break;
306 }
307 } catch (Exception $err) {
308 trigger_log(SYSLOG_WARNING, 'The value "' . $j . '" on line "' . $i . '" is not valid, skipping to next line', __FILE__, __LINE__);
309 continue;
310 }
311 }
312
313 // Creating a string with the complete description of the node using all data in the status file
314 $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/>';
315
316 if ($placemarkPosition = $this->getNodeByName($lineContent[1])) {
317 // Updating an excisting placemark
318 $this->KMLNodes[$placemarkPosition]->setDescriptionStatus($descriptionStatus);
319 } else {
320 // Adding a new placemark
321 $placemark = new KMLNode();
322 $placemark->setID($lineContent[1]);
323 $placemark->setName($lineContent[1]);
324 $placemark->setDescriptionStatus($descriptionStatus);
325 $this->addNode($placemark);
326 }
327 }
328 }
329}
330?>
Note: See TracBrowser for help on using the repository browser.