-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLK8000Data.php
141 lines (122 loc) · 5.82 KB
/
LK8000Data.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
function get_file_extension($file_name)
{
return substr(strrchr($file_name,'.'),1);
}
function handle_request()
{
if(isset($_REQUEST['getJSON']))
{
$obj = new LK8000Data();
if (isset($_REQUEST['filter']))
$obj->filter = (int)$_REQUEST['filter'];
if (isset($_GET['dropcache']))
$obj->cacheFilename = false;
echo json_encode($obj->getData());
}
}
/**
* populate, store and provide data for Google Maps
* @author Michal migajek Gajek
*/
class LK8000Data {
private $dataDir = "/data/";
private $allowedExts = array("txt");
private $varNames = array("NAME", "DIR", "MAPZONE", "TOPOLOGY");
private $coordNames = array("LONMIN", "LONMAX", "LATMIN", "LATMAX");
// relative path to cache filename, provide false to avoid caching
public $cacheFilename = "_cache.";
public $filter = 0;
/**
* @param integer filter, 0 for no filtering
* @return array
* Parse all the files in dataDir and return an array
* where each item is an array containing following keys:
* file / latmin / latmax / lonmin / lonmax
*/
private function parseFiles()
{
$files = array();
$dir = opendir($this->dataDir);
// first of all, collect the filenames
if (!$dir)
throw new Exception("Unable to read directory {$this->dataDir}");
while (($file = readdir($dir)) !== false){
if(in_array(strtolower(get_file_extension($file)), $this->allowedExts))
$files[] = $file;
}
closedir($dir);
// each file has to be opened
$parsed = array();
foreach ($files as $file)
{
$content = file_get_contents($this->dataDir.$file);
$datas = array(
"file" => $file,
"color" => $this->getHashColor($file),
);
foreach ($this->varNames as $var)
{
$matches = array();
preg_match("/^\s*{$var}\s*=\s*(-?.+[\S])/mi", $content, $matches);
if (sizeof($matches))
$datas[strtolower($var)] = str_replace(',', '.', $matches[1]);
}
foreach ($this->coordNames as $var)
{
$matches = array();
preg_match("/^\s*{$var}\s*=\s*(-?[\d\.,]+)/mi", $content, $matches);
if (sizeof($matches))
$datas[strtolower($var)] = str_replace(',', '.', $matches[1]);
}
$res = array(1000, 500, 250, 90);
foreach ($res as $r)
{
$datas['res'][$r] = preg_match("/RES{$r}=YES/i", $content);
if($datas['res'][$r])
$datas['bestres'] = $r;
}
// filtering stuff, to be removed / rewritten later
if ($this->filter != 0)
{
if (($datas['bestres'] < $this->filter ) || (!$datas['res'][$this->filter]))
continue;
}
// end of filtering
$parsed[] = $datas;
}
return $parsed;
}
/**
* function to compute color for a specific string
* @param string source data
* @return string HTML color
*/
private function getHashColor($string)
{
$hash = md5($string);
return '#'.substr($hash, 0, 6);
}
public function __construct()
{
$this->dataDir = dirname(__FILE__).$this->dataDir;
if ($this->cacheFilename)
$this->cacheFilename = dirname(__FILE__)."/".$this->cacheFilename;
}
public function getData()
{
if ($this->cacheFilename && file_exists($this->cacheFilename.$this->filter))
{
$content = file_get_contents($this->cacheFilename.$this->filter);
if (!empty($content))
return unserialize($content);
}
$data = $this->parseFiles();
if ($this->cacheFilename && !empty($data))
{
file_put_contents($this->cacheFilename.$this->filter, serialize($data));
}
return $data;
}
}
handle_request();