1 | <?
|
---|
2 | /*
|
---|
3 | * Project: NodeMap2.0
|
---|
4 | * File: KMLHandler.class.php
|
---|
5 | * Purpose: Creating of editing KML files
|
---|
6 | */
|
---|
7 |
|
---|
8 | define('PLACEMARK_GREEN', 'http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png');
|
---|
9 | define('PLACEMARK_ORANGE', 'http://www.google.com/intl/en_us/mapfiles/ms/micons/orange-dot.png');
|
---|
10 | define('PLACEMARK_RED', 'http://www.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png');
|
---|
11 |
|
---|
12 | class 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 = array();
|
---|
61 |
|
---|
62 | /*
|
---|
63 | * Function: __construct (constructor)
|
---|
64 | * Parameters: -
|
---|
65 | * Function: Creating a new KMLFile
|
---|
66 | */
|
---|
67 | public function __construct() {
|
---|
68 | }
|
---|
69 |
|
---|
70 | public function addPlacemark(KMLPlacemark $placemark) {
|
---|
71 | $this->KMLPlacemarks[] = $placemark;
|
---|
72 | }
|
---|
73 |
|
---|
74 | public function toString() {
|
---|
75 | $toString = $this->template;
|
---|
76 |
|
---|
77 | $placemarkString = '';
|
---|
78 | $placemarkCount = count($this->KMLPlacemarks);
|
---|
79 | for ($i = 0; $i < $placemarkCount; $i++) {
|
---|
80 | $placemarkString .= $this->KMLPlacemarks[$i]->toString();
|
---|
81 | }
|
---|
82 |
|
---|
83 | $toString = str_replace('%PLACEMARK_GREEN%', PLACEMARK_GREEN, $toString);
|
---|
84 | $toString = str_replace('%PLACEMARK_ORANGE%', PLACEMARK_ORANGE, $toString);
|
---|
85 | $toString = str_replace('%PLACEMARK_RED%', PLACEMARK_RED, $toString);
|
---|
86 | $toString = str_replace('%CONTENT%', $placemarkString, $toString);
|
---|
87 |
|
---|
88 | return $toString;
|
---|
89 | }
|
---|
90 |
|
---|
91 | public function write($filename) {
|
---|
92 | // TODO: Write KMLFile to a KML file
|
---|
93 | }
|
---|
94 |
|
---|
95 | public function parseLocationFile($file) {
|
---|
96 | //preg_match_all('#\[(^\/.+?)\]#is', $file, $matches);
|
---|
97 | $nodesCount = preg_match_all('/\[[a-zA-Z0-9]*\]/i', $file, $nodes, PREG_OFFSET_CAPTURE);
|
---|
98 | for ($i = 0; $i < $nodesCount; $i++) {
|
---|
99 | $location = $this->findInLocationFile($file, 'location', $nodes[0][$i][1]);
|
---|
100 | $status = $this->findInLocationFile($file, 'status', $nodes[0][$i][1]);
|
---|
101 | $latitude = $this->findInLocationFile($file, 'latitude', $nodes[0][$i][1]);
|
---|
102 | $longitude = $this->findInLocationFile($file, 'longitude', $nodes[0][$i][1]);
|
---|
103 | $interfaces = $this->findInLocationFile($file, 'interfaces', $nodes[0][$i][1]);
|
---|
104 | $masterip = $this->findInLocationFile($file, 'masterip', $nodes[0][$i][1]);
|
---|
105 | $nodetype = $this->findInLocationFile($file, 'nodetype', $nodes[0][$i][1]);
|
---|
106 | $name = $this->findInLocationFile($file, 'name', $nodes[0][$i][1]);
|
---|
107 |
|
---|
108 | $placemark = new KMLPlacemark();
|
---|
109 | $placemark->setName($name);
|
---|
110 | $placemark->setDescription('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);
|
---|
111 | $placemark->setLongitude($longitude);
|
---|
112 | $placemark->setLatitude($latitude);
|
---|
113 | $placemark->setStyle($status == 'up' ? 'greenArrowIcon' : 'redArrowIcon');
|
---|
114 | $this->addPlacemark($placemark);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | private function findInLocationFile($file, $keyword, $offset) {
|
---|
119 | $start = strpos($file, $keyword, $offset) + strlen($keyword . ' = ');
|
---|
120 | $end = strpos($file, "\n", $start);
|
---|
121 | return substr($file, $start, $end - $start);
|
---|
122 | }
|
---|
123 |
|
---|
124 | public function parseStatusFile($file) {
|
---|
125 | /*
|
---|
126 | * TODO: Needs to be rebuild so it doesn't create a new KMLFile object,
|
---|
127 | * but add the KMLPlacemarks to the current object.
|
---|
128 | */
|
---|
129 | $fileContents = explode("\r\n", $file);
|
---|
130 |
|
---|
131 | if ($fileContents[0] != KMLFile::$fileFirst) {
|
---|
132 | trigger_log(LOG_WARNING, 'Contents of file do not match with template of first line', __FILE__, __LINE__);
|
---|
133 | }
|
---|
134 |
|
---|
135 | $lines = count($fileContents);
|
---|
136 | for ($i = 1; $i < count($lines); $i++) {
|
---|
137 | $lineContent = explode(',', $fileContents[$i]);
|
---|
138 |
|
---|
139 | if (count($lineContent) != count($this->fileContent)) {
|
---|
140 | trigger_log(LOG_WARNING, 'Contents of file do not match with template of lines', __FILE__, __LINE__);
|
---|
141 | }
|
---|
142 |
|
---|
143 | // TODO: Process all lines and create KMLPlacemark objects we can add to a new KMLFile object
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 | ?>
|
---|