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>
|
---|
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 | private $NetworkList;
|
---|
79 |
|
---|
80 | /*
|
---|
81 | * Function: __construct (constructor)
|
---|
82 | * Description: Creating a new KMLFile
|
---|
83 | * Parameters: -
|
---|
84 | * Returns: -
|
---|
85 | */
|
---|
86 | public function __construct() {
|
---|
87 | $this->KMLNodes = array();
|
---|
88 | $this->KMLLines = array();
|
---|
89 | $this->NetworkList = new NetworkList();
|
---|
90 | }
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * Function: addNode
|
---|
94 | * Description: Add a node to the local node array
|
---|
95 | * Parameters: KMLNode $node
|
---|
96 | * Returns: -
|
---|
97 | */
|
---|
98 | public function addNode(KMLNode $node) {
|
---|
99 | $this->KMLNodes[] = $node;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /*
|
---|
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 | /*
|
---|
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 |
|
---|
123 | // Add all nodes to the string
|
---|
124 | $nodeString = '';
|
---|
125 | $nodeCount = count($this->KMLNodes);
|
---|
126 | for ($i = 0; $i < $nodeCount; $i++) {
|
---|
127 | $nodeString .= $this->KMLNodes[$i]->toString();
|
---|
128 | }
|
---|
129 |
|
---|
130 | // Add all connected lines to the string
|
---|
131 | $lineString = '';
|
---|
132 | $lineCount = count($this->KMLLines);
|
---|
133 | for ($i = 0; $i < $lineCount; $i++) {
|
---|
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 | }
|
---|
139 | }
|
---|
140 |
|
---|
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);
|
---|
147 |
|
---|
148 | return $toString;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /*
|
---|
152 | * Function: getNodeByName
|
---|
153 | * Description: Find the first KMLNode with the name $name and return its position. If not found, return false
|
---|
154 | * Parameters: string $name
|
---|
155 | * Returns: Position of node in our array, if not found false
|
---|
156 | */
|
---|
157 | public function getNodeByName($name) {
|
---|
158 | $nodesCount = count($this->KMLNodes);
|
---|
159 | for ($i = 0; $i < $nodesCount; $i++) {
|
---|
160 | if ($this->KMLNodes[$i]->getName() == $name) {
|
---|
161 | return $i;
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | return false;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /*
|
---|
169 | * Function: parseLocationFile
|
---|
170 | * Description: Parse the node location file updating or adding KMLNode objects to the current KMLFile object
|
---|
171 | * Parameters: string $file
|
---|
172 | * Returns: true is successfull, otherwise false
|
---|
173 | */
|
---|
174 | public function parseLocationFile($file) {
|
---|
175 | // We want to find all the nodes in the location file and store information of the nodes...
|
---|
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
|
---|
179 | if (!$location = $this->findInLocationFile($file, 'location = ', $nodes[0][$i][1])) {
|
---|
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
|
---|
184 | if (!$status = $this->findInLocationFile($file, 'status = ', $nodes[0][$i][1])) {
|
---|
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
|
---|
189 | if (!$latitude = $this->findInLocationFile($file, 'latitude = ', $nodes[0][$i][1])) {
|
---|
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
|
---|
194 | if (!$longitude = $this->findInLocationFile($file, 'longitude = ', $nodes[0][$i][1])) {
|
---|
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
|
---|
199 | if (!$interfaces = $this->findInLocationFile($file, 'interfaces = ', $nodes[0][$i][1])) {
|
---|
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
|
---|
204 | if (!$masterip = $this->findInLocationFile($file, 'masterip = ', $nodes[0][$i][1])) {
|
---|
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
|
---|
209 | if (!$nodetype = $this->findInLocationFile($file, 'nodetype = ', $nodes[0][$i][1])) {
|
---|
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
|
---|
214 | if (!$name = $this->findInLocationFile($file, 'name = ', $nodes[0][$i][1])) {
|
---|
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 |
|
---|
222 | if ($placemarkPosition = $this->getNodeByName($name)) {
|
---|
223 | // Updating an excisting placemark
|
---|
224 | $this->KMLNodes[$placemarkPosition]->setDescriptionLocation($descriptionLocation);
|
---|
225 | $this->KMLNodes[$placemarkPosition]->setLongitude($longitude);
|
---|
226 | $this->KMLNodes[$placemarkPosition]->setLatitude($latitude);
|
---|
227 | } else {
|
---|
228 | // Adding a new placemark
|
---|
229 | $placemark = new KMLNode();
|
---|
230 | $placemark->setID($name);
|
---|
231 | $placemark->setName($name);
|
---|
232 | $placemark->setDescriptionLocation($descriptionLocation);
|
---|
233 | $placemark->setLongitude($longitude);
|
---|
234 | $placemark->setLatitude($latitude);
|
---|
235 | $this->addNode($placemark);
|
---|
236 | }
|
---|
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 | }
|
---|
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) {
|
---|
280 | $start = strpos($file, $keyword, $offset) + strlen($keyword);
|
---|
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
|
---|
292 | * Description: Parse the node status file updating or adding KMLNode objects to the current KMLFile object
|
---|
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 |
|
---|
338 | if ($placemarkPosition = $this->getNodeByName($lineContent[1])) {
|
---|
339 | // Updating an excisting placemark
|
---|
340 | $this->KMLNodes[$placemarkPosition]->setDescriptionStatus($descriptionStatus);
|
---|
341 | } else {
|
---|
342 | // Adding a new placemark
|
---|
343 | $placemark = new KMLNode();
|
---|
344 | $placemark->setID($lineContent[1]);
|
---|
345 | $placemark->setName($lineContent[1]);
|
---|
346 | $placemark->setDescriptionStatus($descriptionStatus);
|
---|
347 | $this->addNode($placemark);
|
---|
348 | }
|
---|
349 | }
|
---|
350 | }
|
---|
351 | }
|
---|
352 | ?>
|
---|