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

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