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 | <\?xml version="1.0" encoding="UTF-8"?>
|
---|
11 | <kml xmlns="http://www.opengis.net/kml/2.2">
|
---|
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>
|
---|
51 | </kml>';
|
---|
52 |
|
---|
53 | // First line of the status file must be like this:
|
---|
54 | static $fileFirst = 'type,host_name,has_been_checked,check_execution_time,current_state,last_hard_state,last_check,problem_has_been_acknowledged';
|
---|
55 | // Every following line will be checked using these functions
|
---|
56 | private $fileContent = array('string', 'string', 'int', 'double', 'int', 'int', 'int', 'int');
|
---|
57 |
|
---|
58 | private $KMLPlacemarks;
|
---|
59 |
|
---|
60 | /*
|
---|
61 | * Function: __construct (constructor)
|
---|
62 | * Description: Creating a new KMLFile
|
---|
63 | * Parameters: -
|
---|
64 | * Returns: -
|
---|
65 | */
|
---|
66 | public function __construct() {
|
---|
67 | $this->KMLPlacemarks = array();
|
---|
68 | }
|
---|
69 |
|
---|
70 | /*
|
---|
71 | * Function: addPlacemark
|
---|
72 | * Description: Add a placemark to the local placemark array
|
---|
73 | * Parameters: KMLPlacemark $placemark
|
---|
74 | * Returns: -
|
---|
75 | */
|
---|
76 | public function addPlacemark(KMLPlacemark $placemark) {
|
---|
77 | $this->KMLPlacemarks[] = $placemark;
|
---|
78 | }
|
---|
79 |
|
---|
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 | */
|
---|
86 | public function toString() {
|
---|
87 | global $config;
|
---|
88 |
|
---|
89 | $toString = $this->template;
|
---|
90 |
|
---|
91 | $placemarkString = '';
|
---|
92 | $placemarkCount = count($this->KMLPlacemarks);
|
---|
93 | for ($i = 0; $i < $placemarkCount; $i++) {
|
---|
94 | $placemarkString .= $this->KMLPlacemarks[$i]->toString();
|
---|
95 | }
|
---|
96 |
|
---|
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);
|
---|
100 | $toString = str_replace('%CONTENT%', $placemarkString, $toString);
|
---|
101 |
|
---|
102 | return $toString;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /*
|
---|
106 | * Function: write
|
---|
107 | * Description: Write KMLFile to a KML file
|
---|
108 | * Parameters: string $filename
|
---|
109 | * Returns: true if we can write to the file, otherwise false
|
---|
110 | */
|
---|
111 | public function write($filename) {
|
---|
112 | // TODO: David: Needs to be placed in FileHandler.class.php. Here we just want to call our file handler.
|
---|
113 |
|
---|
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);
|
---|
123 | }
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * Function: getPlacemarkByName
|
---|
127 | * Description: Find the first KMLPlacemark with the name $name and return its position. If not found, return false
|
---|
128 | * Parameters: string $name
|
---|
129 | * Returns: Position of placemark in our array, if not found false
|
---|
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 | }
|
---|
138 |
|
---|
139 | return false;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /*
|
---|
143 | * Function: parseLocationFile
|
---|
144 | * Description: Parse the node location file updating or adding KMLPlacemark objects to the current KMLFile object
|
---|
145 | * Parameters: string $file
|
---|
146 | * Returns: true is successfull, otherwise false
|
---|
147 | */
|
---|
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++) {
|
---|
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 | }
|
---|
191 |
|
---|
192 | // Creating a string with the complete description of the node using all data in the location file
|
---|
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)) {
|
---|
196 | // Updating an excisting placemark
|
---|
197 | $this->KMLPlacemarks[$placemarkPosition]->setDescriptionLocation($descriptionLocation);
|
---|
198 | $this->KMLPlacemarks[$placemarkPosition]->setLongitude($longitude);
|
---|
199 | $this->KMLPlacemarks[$placemarkPosition]->setLatitude($latitude);
|
---|
200 | } else {
|
---|
201 | // Adding a new placemark
|
---|
202 | $placemark = new KMLPlacemark();
|
---|
203 | $placemark->setID($name);
|
---|
204 | $placemark->setName($name);
|
---|
205 | $placemark->setDescriptionLocation($descriptionLocation);
|
---|
206 | $placemark->setLongitude($longitude);
|
---|
207 | $placemark->setLatitude($latitude);
|
---|
208 | $this->addPlacemark($placemark);
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | /*
|
---|
214 | * Function: findInLocationFile
|
---|
215 | * Description: Find the $keyword in $file and return the value of $keyword, starting at $offset
|
---|
216 | * Parameters: string $file, string $keyword, integer $offset
|
---|
217 | * Returns: The value of the keyword if found, otherwise return false
|
---|
218 | */
|
---|
219 | private function findInLocationFile($file, $keyword, $offset) {
|
---|
220 | $start = strpos($file, $keyword, $offset) + strlen($keyword . ' = ');
|
---|
221 | $end = strpos($file, "\n", $start);
|
---|
222 |
|
---|
223 | if ($start && $end && ($start < $end)) {
|
---|
224 | return substr($file, $start, $end - $start);
|
---|
225 | } else {
|
---|
226 | return false;
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | /*
|
---|
231 | * Function: parseStatusFile
|
---|
232 | * Description: Parse the node status file updating or adding KMLPlacemark objects to the current KMLFile object
|
---|
233 | * Parameters: string $file
|
---|
234 | * Returns: true is successfull, otherwise false
|
---|
235 | */
|
---|
236 | public function parseStatusFile($file) {
|
---|
237 | $fileContents = explode("\r\n", $file);
|
---|
238 |
|
---|
239 | if ($fileContents[0] != KMLFile::$fileFirst) {
|
---|
240 | trigger_log(SYSLOG_WARNING, 'Contents of file do not match with template of first line', __FILE__, __LINE__);
|
---|
241 | }
|
---|
242 |
|
---|
243 | // For loop for all the lines in the file. Skipping first line (headers) and last line (blank)
|
---|
244 | $linesCount = count($fileContents);
|
---|
245 | for ($i = 1; $i < $linesCount - 1; $i++) {
|
---|
246 | $lineContent = explode(',', $fileContents[$i]);
|
---|
247 |
|
---|
248 | if (count($lineContent) != count($this->fileContent)) {
|
---|
249 | trigger_log(LOG_WARNING, 'Contents of the file do not match with template of lines on line "' . $i . '"', __FILE__, __LINE__);
|
---|
250 | continue;
|
---|
251 | }
|
---|
252 |
|
---|
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 | }
|
---|
274 |
|
---|
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/>';
|
---|
277 |
|
---|
278 | if ($placemarkPosition = $this->getPlacemarkByName($lineContent[1])) {
|
---|
279 | // Updating an excisting placemark
|
---|
280 | $this->KMLPlacemarks[$placemarkPosition]->setDescriptionStatus($descriptionStatus);
|
---|
281 | } else {
|
---|
282 | // Adding a new placemark
|
---|
283 | $placemark = new KMLPlacemark();
|
---|
284 | $placemark->setID($lineContent[1]);
|
---|
285 | $placemark->setName($lineContent[1]);
|
---|
286 | $placemark->setDescriptionStatus($descriptionStatus);
|
---|
287 | $this->addPlacemark($placemark);
|
---|
288 | }
|
---|
289 | }
|
---|
290 | }
|
---|
291 | }
|
---|
292 | ?>
|
---|