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

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

Fixed KML output, it's now valid. Added ID's to the placemarkers.

File size: 7.5 KB
Line 
1<?php
2/*
3 * Project: NodeMap2.0
4 * File: KMLHandler.class.php
5 * Purpose: Creating of editing KML files
6 */
7
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
12class KMLFile {
13 private $template = '
14 <\?xml version="1.0" encoding="UTF-8"?>
15 <kml xmlns="http://www.opengis.net/kml/2.2">
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>
55 </kml>';
56
57 static $fileFirst = 'type,host_name,has_been_checked,check_execution_time,current_state,last_hard_state,last_check,problem_has_been_acknowledged';
58 private $fileContent = array('string', 'string', 'integer', 'double', 'integer', 'integer', 'integer', 'integer');
59
60 private $KMLPlacemarks;
61
62 /*
63 * Function: __construct (constructor)
64 * Parameters: -
65 * Function: Creating a new KMLFile
66 */
67 public function __construct() {
68 $this->KMLPlacemarks = array();
69 }
70
71 public function addPlacemark(KMLPlacemark $placemark) {
72 $this->KMLPlacemarks[] = $placemark;
73 }
74
75 public function toString() {
76 $toString = $this->template;
77
78 $placemarkString = '';
79 $placemarkCount = count($this->KMLPlacemarks);
80 for ($i = 0; $i < $placemarkCount; $i++) {
81 $placemarkString .= $this->KMLPlacemarks[$i]->toString();
82 }
83
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);
87 $toString = str_replace('%CONTENT%', $placemarkString, $toString);
88
89 return $toString;
90 }
91
92 public function write($filename) {
93 // TODO: Write KMLFile to a KML file
94 }
95
96 /*
97 * Function: getPlacemarkByName
98 * Parameters: string $name
99 * Function: Find the first KMLPlacemark with the name $name and return its position. If not found, return false
100 */
101 public function getPlacemarkByName($name) {
102 $nodesCount = count($this->KMLPlacemarks);
103 for ($i = 0; $i < $nodesCount; $i++) {
104 if ($this->KMLPlacemarks[$i]->getName() == $name) {
105 return $i;
106 }
107 }
108 return false;
109 }
110
111 /*
112 * Function: parseLocationFile
113 * Parameters: string $file
114 * Function: Parse the node location file updating or adding KMLPlacemark objects to the current KMLFile object
115 */
116 public function parseLocationFile($file) {
117 $nodesCount = preg_match_all('/\[[a-zA-Z0-9]*\]/i', $file, $nodes, PREG_OFFSET_CAPTURE);
118 for ($i = 0; $i < $nodesCount; $i++) {
119 // TODO: Needs checking for parsing errors
120 $location = $this->findInLocationFile($file, 'location', $nodes[0][$i][1]);
121 $status = $this->findInLocationFile($file, 'status', $nodes[0][$i][1]);
122 $latitude = $this->findInLocationFile($file, 'latitude', $nodes[0][$i][1]);
123 $longitude = $this->findInLocationFile($file, 'longitude', $nodes[0][$i][1]);
124 $interfaces = $this->findInLocationFile($file, 'interfaces', $nodes[0][$i][1]);
125 $masterip = $this->findInLocationFile($file, 'masterip', $nodes[0][$i][1]);
126 $nodetype = $this->findInLocationFile($file, 'nodetype', $nodes[0][$i][1]);
127 $name = $this->findInLocationFile($file, 'name', $nodes[0][$i][1]);
128
129 $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/>';
130
131 if ($placemarkPosition = $this->getPlacemarkByName($name)) {
132 $this->KMLPlacemarks[$placemarkPosition]->setDescriptionLocation($descriptionLocation);
133 $this->KMLPlacemarks[$placemarkPosition]->setLongitude($longitude);
134 $this->KMLPlacemarks[$placemarkPosition]->setLatitude($latitude);
135 } else {
136 $placemark = new KMLPlacemark();
137 $placemark->setID($name);
138 $placemark->setName($name);
139 $placemark->setDescriptionLocation($descriptionLocation);
140 $placemark->setLongitude($longitude);
141 $placemark->setLatitude($latitude);
142 $this->addPlacemark($placemark);
143 }
144 }
145 }
146
147 /*
148 * Function: findInLocationFile
149 * Parameters: string $file, string $keyword, integer $offset
150 * Function: Find the $keyword in $file and return the value of $keyword, starting at $offset, on error return false
151 */
152 private function findInLocationFile($file, $keyword, $offset) {
153 $start = strpos($file, $keyword, $offset) + strlen($keyword . ' = ');
154 $end = strpos($file, "\n", $start);
155
156 if ($start && $end && ($start < $end)) {
157 return substr($file, $start, $end - $start);
158 } else {
159 return false;
160 }
161 }
162
163 /*
164 * Function: parseStatusFile
165 * Parameters: string $file
166 * Function: Parse the node status file updating or adding KMLPlacemark objects to the current KMLFile object
167 */
168 public function parseStatusFile($file) {
169 $fileContents = explode("\r\n", $file);
170
171 if ($fileContents[0] != KMLFile::$fileFirst) {
172 trigger_log(LOG_WARNING, 'Contents of file do not match with template of first line', __FILE__, __LINE__);
173 }
174
175 $linesCount = count($fileContents);
176 for ($i = 1; $i < $linesCount - 1; $i++) {
177 $lineContent = explode(',', $fileContents[$i]);
178
179 if (count($lineContent) != count($this->fileContent)) {
180 trigger_log(LOG_WARNING, 'Contents of file do not match with template of lines', __FILE__, __LINE__);
181 }
182
183 // TODO: Process all lines and update KMLPlacemark objects we already have in the current object
184 $type = $lineContent[0];
185 $host_name = str_replace('CNode', '', $lineContent[1]);
186 $has_been_checked = $lineContent[2];
187 $check_execution_time = $lineContent[3];
188 $current_state = $lineContent[4];
189 $last_hard_state = $lineContent[5];
190 $last_check = $lineContent[6];
191 $problem_has_been_acknowledged = $lineContent[7];
192
193 $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/>';
194
195 if ($placemarkPosition = $this->getPlacemarkByName($host_name)) {
196 $this->KMLPlacemarks[$placemarkPosition]->setDescriptionStatus($descriptionStatus);
197 } else {
198 $placemark = new KMLPlacemark();
199 $placemark->setID($host_name);
200 $placemark->setName($host_name);
201 $placemark->setDescriptionStatus($descriptionStatus);
202 $this->addPlacemark($placemark);
203 }
204 }
205 }
206}
207?>
Note: See TracBrowser for help on using the repository browser.