-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdcountdata.php
250 lines (213 loc) · 6.89 KB
/
mdcountdata.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
/*
See https://github.com/jxmot/markdown-hitcounter for detailed
information not contained here.
Usage:
Return data for all counters -
GET http[s]://your-server/path-to-file/mdcountdata.php
OR
Return data for a specific counter -
GET http[s]://your-server/path-to-file/mdcountdata.php?id=testtest
OR
Return data for all counters, but sorted -
GET http[s]://your-server/path-to-file/mdcountdata.php?[csort|tsort|isort]=[a|d]
Where:
csort - sort by count
tsort - sort by time of last count
isort - sort by counter ID
a - sort ascending
d - sort descending
Return sorted data, but limit the quantity of counters returned
GET http[s]://your-server/path-to-file/mdcountdata.php?csort=a|[&limit=[1-n]]
Where:
limit - limit the quantity of counters to return
1-n - quantity
Report data returned:
One or more counters -
[
{
"id": "sensornet",
"data": {
"count": 30,
"time": 1616458038,
"dtime": [
"20210322",
"190718"
]
}
}
]
*/
class logdata {
public $id = '';
public $count = 0;
public $time = 0;
public $dtime = array('19700101','000001');
}
// if false run normally, if true then fake the query
define('_DEBUG', false);
// counter ID file and output path
define('VALID_COUNTERS', './counters.json');
define('LOG_FOLDER', './logs/');
function compasc($a, $b) {
return ($a === $b ? 0 : ($a < $b ? -1 : 1));
}
function compdesc($a, $b) {
return ($a === $b ? 0 : ($a > $b ? -1 : 1));
}
function limitqty($data, $limit) {
if($limit !== null && ($limit > 0 && $limit < count($data))){
$data = array_slice($data, 0, $limit);
}
return $data;
}
function countsort($sort, $out, $limit = null) {
$data = json_decode($out);
// ascending
if($sort === 'a' || $sort === '') {
usort($data, function($a, $b) {
return compasc($a->data->count, $b->data->count);
});
}
// descending
if($sort === 'd') {
usort($data, function($a, $b) {
return compdesc($a->data->count, $b->data->count);
});
}
// done!
return json_encode(limitqty($data, $limit));
}
function timesort($sort, $out, $limit = null) {
$data = json_decode($out);
// ascending
if($sort === 'a' || $sort === '') {
usort($data, function($a, $b) {
return compasc($a->data->time, $b->data->time);
});
}
// descending
if($sort === 'd') {
usort($data, function($a, $b) {
return compdesc($a->data->time, $b->data->time);
});
}
// done!
return json_encode(limitqty($data, $limit));
}
function idsort($sort, $out, $limit = null) {
$data = json_decode($out);
// ascending
if($sort === 'a' || $sort === '') {
usort($data, function($a, $b) {
return compasc($a->id, $b->id);
});
}
// descending
if($sort === 'd') {
usort($data, function($a, $b) {
return compdesc($a->id, $b->id);
});
}
// done!
return json_encode(limitqty($data, $limit));
}
// check for debug/test mode
if(!defined('_DEBUG') || _DEBUG === false) {
// MUST be done like this for PHP files that are 'linked'
$queries = array();
parse_str($_SERVER['QUERY_STRING'], $queries);
// return a single counter by ID
$id = (isset($queries['id']) ? strtolower($queries['id']) : null);
// return all counters, ordered by count
$csort = (isset($queries['csort']) ? strtolower($queries['csort']) : null);
// return all counters, ordered by time of last count
$tsort = (isset($queries['tsort']) ? strtolower($queries['tsort']) : null);
// return all counters, ordered by ID
$isort = (isset($queries['isort']) ? strtolower($queries['isort']) : null);
// for sorts, limit number of counters returned
$limit = (isset($queries['limit']) ? $queries['limit'] : null);
// if $count is true then only the count value will be returned
$count = (isset($queries['count']) ? true : false);
} else {
// set as needed for testing
$id = null;
$csort = 'a';
$tsort = null;
$isort = null;
$limit = 2;
$count = false;
}
// get the valid counter ID list
$_idlist = json_decode(file_get_contents(VALID_COUNTERS));
$idlist = array_map('strtolower', $_idlist->valid);
// did we get a counter ID?
if($id !== null) {
// is the counter ID known?
if(in_array($id, $idlist)) {
// build the log file name from the ID and "_count.log"
$counter = $id . '_count.json';
// path + counter file
$cntfile = LOG_FOLDER . $counter;
if(file_exists($cntfile)) {
$filecnt = fopen($cntfile,'r');
// get 128 characters, it's unlikely that counter
// would get that big.
$json = fgets($filecnt,128);
fclose($filecnt);
if($count === false) {
$result = '[{"id":"'.$id.'","data":'.$json.'}]';
} else {
$tmp = json_decode($json);
$result = $tmp->{'count'};
}
} else {
$result = '[{"error":true,"msg":"file ['.$cntfile.'] does not exist"}]';
}
} else {
$result = '[{"error":true,"msg":"['.$id.'] not found in '.VALID_COUNTERS.'"}]';
}
} else {
$ix = 0;
$out = '[';
foreach($idlist as $id) {
// build the log file name from the ID and "_count.log"
//$counter = $id . '_count.json';
// path + counter file
$cntfile = LOG_FOLDER . $id . '_count.json';;
if(file_exists($cntfile)) {
$filecnt = fopen($cntfile,'r');
// get 128 characters, it's unlikely that a counter
// would get that big.
$json = fgets($filecnt,128);
fclose($filecnt);
// add a counter to the array
$out = $out . ($ix > 0 ? ',' : '');
$out = $out . '{"id":"'.$id.'","data":';
$out = $out . $json . '}';
// only used above to decide if we need a comma
$ix += 1;
}
}
$out = $out . ']';
// is sorting selected?
if($csort !== null) {
// by count
$out = countsort($csort, $out, $limit);
}
if($tsort !== null) {
// by time
$out = timesort($tsort, $out, $limit);
}
if($isort !== null) {
// by ID
$out = idsort($isort, $out, $limit);
}
$result = $out;
}
header("HTTP/1.0 200 OK");
header("Content-Type: application/json; charset=utf-8");
header("Content-Encoding: text");
echo $result;
exit;
?>