-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathITpings__database_queries.php
525 lines (457 loc) · 18.9 KB
/
ITpings__database_queries.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<?php
//PREDEFINED QUERIES
define('NO_SQL_QUERY', 'none');
//region ===== PROCESS GET QUERY ==================================================================
/**
* Queries can be:
* - Predefined Tables
* - Predefined Views
* - Custom Queries
*
* Process URI with ?query=xxxx
*
*/
function process_Table_or_View_query()
{
global $QueryStringParameters;
$sql = EMPTY_STRING;
$query = TABLE_PREFIX . $QueryStringParameters['query'];
$updateview = $QueryStringParameters['updateview'];
if ($updateview) {
add_JSON_message_to_JSON_response('ViewUpdate: ' . $query);
Create_Or_Replace_View($query);
}
/** User can only request for limitted table/view names, this is the place to deny access to Tables/Views **/
switch ($query) {
/**!!!!!!!!!!!!!!!!! ALWAYS CREATE OR REPLACE VIEW ON EVERY ENDPOINT CALL !!!!!!!!!!!!!!!!!!!!!!!!!!!!**/
// case VIEWNAME_DATA_TEMPERATURE:
// case VIEWNAME_DATA_LUMINOSITY:
// case VIEWNAME_DATA_BATTERY:
case 'ALWAYS_CREATE_OR_REPLACE_VIEW'://todo: read VIEW NAME to be updated from querystring parameter
add_JSON_message_to_JSON_response('ViewUpdate: ' . $query);
Create_Or_Replace_View($query);
break;
/** Process regular View/Table names **/
case VIEWNAME_DATA_TEMPERATURE:
case VIEWNAME_DATA_LUMINOSITY:
case VIEWNAME_DATA_BATTERY:
case VIEWNAME_SENSORVALUES:
case VIEWNAME_SENSORVALUES_UPDATE:
case VIEWNAME_EVENTS:
case VIEWNAME_PINGEDDEVICES:
case VIEWNAME_PINGEDGATEWAYS:
case VIEWNAME_GATEWAYS:
case VIEWNAME_APPLICATIONDEVICES:
break;
case TABLE_EVENTS:
case TABLE_POSTREQUESTS:
case TABLE_APPLICATIONS:
case TABLE_DEVICES:
//case TABLE_APPLICATIONDEVICES:// User can not access this table
case TABLE_GATEWAYS:
case TABLE_LOCATIONS:
case TABLE_PINGS:
case TABLE_PINGEDGATEWAYS:
case TABLE_SENSORS:
case TABLE_SENSORVALUES:
break;
default:
$sql = process_Predefined_Query();
$query = false;
break;
}
post_process_Query($query, $sql);
}
/**
* Execute queries defined as ?query=[name] URI parameter
*
* @return string - $sql
*/
function process_Predefined_Query()
{
global $QueryStringParameters;
$query = $QueryStringParameters['query'];
$sql = EMPTY_STRING;
switch ($query) {
/** every heartbeat milliseconds The Dashboard polls for a sinle maximum _pingid **/
/** ?query=PingID **/
case 'PingID': // query=PingID // smallest JSON payload as possible, single pingID
exit(SQL_Query("SELECT MAX(" . PRIMARYKEY_Ping . ") AS ID FROM " . TABLE_PINGS)['ID']);
break;
/** when the _pingid has increased the Dashboard polls for all Table/View ID values **/
/** ?query=IDs **/
case 'IDs': // all relevant IDs , smallest JSON payload as possible
attach_Max_IDs_to_JSON_response();
$sql = NO_SQL_QUERY;
break;
/** return full (original) TTN JSON request from POSTrequests Table **/
/** ?query=ping **/
case 'ping':
$pingid = $QueryStringParameters[PRIMARYKEY_Ping];
$sql = "SELECT " . ITPINGS_POST_body . " from " . TABLE_POSTREQUESTS . " WHERE " . PRIMARYKEY_Ping . "=$pingid";
$body = SQL_Query($sql)['body'];
if ($body) {
print $body;
exit();
} else {
exit("Sorry, " . PRIMARYKEY_Ping . "=$pingid has already been purged from " . TABLE_POSTREQUESTS);
}
break;
/** ?query=Devices **/
case 'Devices':
$sql = "SELECT AD . " . PRIMARYKEY_ApplicationDevice;
$sql .= " ,AD . " . ITPINGS_APPLICATION_ID;
$sql .= " ,AD . " . ITPINGS_DESCRIPTION;
$sql .= " ,AD . " . ITPINGS_DEVICE_ID;
$sql .= " ,AD . " . ITPINGS_HARDWARE_SERIAL;
$sql .= " ,LSV . FirstSeen, LSV . LastSeen";
$sql .= " FROM " . VIEWNAME_APPLICATIONDEVICES . " AD";
$sql .= " INNER JOIN(";
$sql .= " SELECT " . PRIMARYKEY_ApplicationDevice;
$sql .= " , min(" . ITPINGS_CREATED_TIMESTAMP . ") as FirstSeen";
$sql .= " , max(" . ITPINGS_CREATED_TIMESTAMP . ") as LastSeen";
$sql .= " FROM " . TABLE_PINGS;
$sql .= " GROUP BY " . PRIMARYKEY_ApplicationDevice;
$sql .= " ) LSV";
$sql .= " WHERE AD . " . PRIMARYKEY_ApplicationDevice . " = LSV . " . PRIMARYKEY_ApplicationDevice;
if ($QueryStringParameters[QUERY_PARAMETER_FILTER] ?? false) {
$sql .= process_QueryParameter_Filter('', ' AND AD.', $QueryStringParameters[QUERY_PARAMETER_FILTER]);
}
break;
/** ?query=DBInfo **/
case 'DBInfo':
attach_Max_IDs_to_JSON_response();
attach_period_to_JSON_response();
$sql = "SELECT REPLACE(S . TABLE_NAME, '" . TABLE_PREFIX . "', '') AS 'Table'";
$sql .= ",S . TABLE_ROWS AS Rows";
$sql .= ",S . AVG_ROW_LENGTH AS RowLength, S . DATA_LENGTH AS DataLength";
$sql .= ",S . INDEX_LENGTH AS IndexLength,S . DATA_FREE AS Free";
$sql .= " FROM information_schema . tables S";
$sql .= " WHERE table_name LIKE '" . TABLE_PREFIX . "%'";
$sql .= " AND TABLE_TYPE = 'BASE TABLE'";
$sql .= " ORDER BY DataLength ASC";
break;
/** ?query=Period **/
case 'Period':
attach_period_to_JSON_response();
$sql = NO_SQL_QUERY;
break;
/** Delete queries, called from Dashboard */
/** ?query=DeletePing&_pingid=[id] */
case 'DeletePingID':
$pingid = $QueryStringParameters[PRIMARYKEY_Ping];
if ($pingid) {
Delete_By_Ping_ID($pingid);
}
break;
/** Delete queries, to be called by handcrafting the URI */
case 'DeleteNullPings':
$sql = "SELECT * FROM " . TABLE_PINGS . " P";
$sql .= " JOIN " . TABLE_POSTREQUESTS . " PR ON PR." . PRIMARYKEY_Ping . " = P." . PRIMARYKEY_Ping;
$sql .= " WHERE " . PRIMARYKEY_ApplicationDevice . " IS null ORDER BY " . PRIMARYKEY_Ping . " DESC";
$rows = SQL_QUERY_ROWS($sql);
add_JSON_message_to_JSON_response($sql);
if ($rows) {
foreach ($rows as $row) {
// $request = json_decode($row[ITPINGS_POST_body], TRUE);
// process_Ping_from_JSON_request($request);
// add_JSON_message($row[PRIMARYKEY_Ping]);
Delete_By_Ping_ID($row[PRIMARYKEY_Ping]);
}
}
break;
/** ?query=DeleteApplicationByID&appid=9 **/
case 'DeleteApplicationByID':
$appid = $QueryStringParameters[PRIMARYKEY_Application];
if ($appid) Delete_By_ApplicationDeviceID($appid);
break;
/** ?query=CleanDataTables **/
case 'CleanDataTables':
Clean_DataTables();
break;
// SELECT * FROM ITpings.ITpings__PingedGateways where time='0000-00-00 00:00:00';
default:
http_response_code(404);
echo "Error query:'$query' does not exist or can not be processed";
die();
break;
}
return $sql;
}
/**
* Process QueryString part for LT and GT like operators; and return a valid SQL $where clause
* @param $where
* @param $and
* @param $parameter_value
* @return string
*/
function process_QueryParameter_Filter($where, $and, $parameter_value)
{
/** Process: ' ... &filter=_devid ge 1,_appid lt 2' **/
// Documentation OData Filters: https://msdn.microsoft.com/en-us/library/hh169248(v=nav.90).aspx
// MySQL: https://dev.mysql.com/doc/refman/5.7/en/non-typed-operators.html
foreach (explode(QUERY_PARAMETER_SEPARATOR, $parameter_value) as $filter) {
$where_filter = explode(' ', $filter);
$where .= $and . $where_filter[0]; // fieldname
$operator = strtolower($where_filter[1]); // lt gt ge le
$value = $where_filter[2];
if ($operator === 'lt') $where .= " < ";
elseif ($operator === 'gt') $where .= " > ";
elseif ($operator === 'ge') $where .= " >= ";
elseif ($operator === 'le') $where .= " <= ";
elseif ($operator === 'eq') $where .= " = ";
$where .= $value;
$and = ' AND ';
}
return $where;
}
/**
* @param $table
* @param $key
* @param bool $value
* @return bool|mixed
*/
function get_Maximum_ID($table, $key, $value = false)
{
global $JSON_response;
if ($value === FALSE) {
$value = SQL_Query("SELECT MAX($key) AS mx FROM $table")['mx'];
}
$table = str_replace(TABLE_PREFIX, '', $table);
if (!isset($JSON_response[QUERYKEY_maxids][$table])) $JSON_response['maxids'][$table] = [];
$JSON_response[QUERYKEY_maxids][$table][$key] = (int)$value;
return $value;
}
function attach_period_to_JSON_response()
{
global $JSON_response;
$created = ITPINGS_CREATED_TIMESTAMP;
$period = SQL_Query("SELECT MIN($created) AS Start,MAX($created) AS End FROM " . TABLE_PINGS);
$JSON_response['periodStart'] = $period['Start'];
$JSON_response['periodEnd'] = $period['End'];
}
/**
* create a JSON structure with the most recent PrimaryKey value for Tables and Views
* The Browser polls this endpoint and thus only updates HTML Tables/Graphs when there is NEW data
*/
function attach_Max_IDs_to_JSON_response()
{
global $JSON_response;
$JSON_response[QUERYKEY_maxids] = array();
get_Maximum_ID(TABLE_APPLICATIONS, PRIMARYKEY_Application);
get_Maximum_ID(TABLE_DEVICES, PRIMARYKEY_Device);
$event_pingid = get_Maximum_ID(TABLE_EVENTS, PRIMARYKEY_Ping);
$gtwid = get_Maximum_ID(TABLE_GATEWAYS, PRIMARYKEY_Gateway);
get_Maximum_ID(TABLE_LOCATIONS, PRIMARYKEY_Location);
$pingid = get_Maximum_ID(TABLE_PINGS, PRIMARYKEY_Ping);
get_Maximum_ID(TABLE_DATA_TEMPERATURE, PRIMARYKEY_Ping);
get_Maximum_ID(TABLE_SENSORS, PRIMARYKEY_Sensor);
//reuse already found visible_ids
get_Maximum_ID(VIEWNAME_SENSORVALUES, PRIMARYKEY_Ping, $pingid);
get_Maximum_ID(VIEWNAME_PINGEDGATEWAYS, PRIMARYKEY_Ping, $pingid);
get_Maximum_ID(VIEWNAME_PINGEDDEVICES, PRIMARYKEY_Ping, $pingid);
get_Maximum_ID(VIEWNAME_EVENTS, PRIMARYKEY_Ping, $event_pingid);
get_Maximum_ID(VIEWNAME_GATEWAYS, PRIMARYKEY_Gateway, $gtwid);
}
/**
* Manage Database
**/
/**
* @param $table
* @param $key
* @param $value
*/
function Delete_By_Key_Value($table, $key, $value)
{
$sql = "DELETE FROM $table WHERE $key = $value;";
SQL_DELETE($sql);
}
/**
* @param $table
* @param $key
* @param $reference_table
*/
function Delete_Unreferenced($table, $key, $reference_table)
{
$sql = "DELETE FROM $table WHERE $key NOT IN(SELECT $key FROM $reference_table);";
SQL_DELETE($sql);
}
/**
*
*/
function Delete_Unreferenced_From_All_Tables()
{
Delete_Unreferenced(TABLE_SENSORS, PRIMARYKEY_Sensor, TABLE_SENSORVALUES);
Delete_Unreferenced(TABLE_GATEWAYS, PRIMARYKEY_Gateway, TABLE_PINGEDGATEWAYS);
Delete_Unreferenced(TABLE_APPLICATIONDEVICES, PRIMARYKEY_ApplicationDevice, TABLE_PINGS);
Delete_Unreferenced(TABLE_APPLICATIONS, PRIMARYKEY_Application, TABLE_APPLICATIONDEVICES);
Delete_Unreferenced(TABLE_DEVICES, PRIMARYKEY_Device, TABLE_APPLICATIONDEVICES);
}
/**
* @param $pingID
*/
function Delete_By_Ping_ID($pingID)
{
Delete_By_Key_Value(TABLE_EVENTS, PRIMARYKEY_Ping, $pingID);
Delete_By_Key_Value(TABLE_SENSORVALUES, PRIMARYKEY_Ping, $pingID);
Delete_By_Key_Value(TABLE_PINGEDGATEWAYS, PRIMARYKEY_Ping, $pingID);
Delete_By_Key_Value(TABLE_PINGS, PRIMARYKEY_Ping, $pingID);
Delete_Unreferenced(TABLE_LOCATIONS, PRIMARYKEY_ApplicationDevice, TABLE_PINGS);
}
/**
* @param $_appdevid
*/
function Delete_By_ApplicationDeviceID($_appdevid)
{
$sql = "SELECT " . PRIMARYKEY_Ping . " FROM " . TABLE_PINGS;
$sql .= " WHERE " . PRIMARYKEY_ApplicationDevice;
$sql .= " IN(SELECT " . PRIMARYKEY_ApplicationDevice . " FROM " . TABLE_APPLICATIONDEVICES . " WHERE " . PRIMARYKEY_ApplicationDevice . " = $_appdevid)";
$rows = SQL_QUERY_ROWS($sql);
add_JSON_message_to_JSON_response($sql);
add_JSON_message_to_JSON_response(json_encode($rows));
if ($rows) {
foreach ($rows as $row) {
// add_JSON_message(json_encode($row));
// add_JSON_message($row[PRIMARYKEY_Ping]);
Delete_By_Ping_ID($row[PRIMARYKEY_Ping]);
}
}
Delete_Unreferenced_From_All_Tables();
}
/**
* For debugging purposes, add data to the JSON output
* @param $key
* @param $value
*/
function QueryTrace($key, $value)
{
global $JSON_response;
if (!isset($JSON_response['QueryTrace'])) $JSON_response['QueryTrace'] = [];
$JSON_response['QueryTrace'][$key] = $value;
}
/**
* Used as CallBack from the Implode
* @param $value
* @return string
*/
function convertValue_to_QuotedString_or_Integer($value)
{
return is_string($value) ? Quoted($value) : $value;
}
/**
* Continue from previous function(process_Query_with_QueryString_Parameters), built a valid $sql
* @param $table_name
* @param $sql
*/
function post_process_Query($table_name, $sql)
{
global $QueryStringParameters;
global $JSON_response;
global $_VALID_QUERY_PARAMETERS;
global $_QUERY_ALLOWED_INTERVALUNITS;
if ($table_name) {
/** process all (by ITpings defined!!!) QueryString parameters , so user can not add roque SQL
* So usage is very strict with:
* Query_Parameters have to be:
* - defined as constant
* - referenced in $_VALID_QUERY_PARAMETERS
* - process in switch below
**/
$where = EMPTY_STRING;
$order = EMPTY_STRING;
$limit = EMPTY_STRING;
foreach ($_VALID_QUERY_PARAMETERS as $parameter) {
/** accept safe parameter values only **/
$parameter_value = SQL_InjectionSave_OneWordString($QueryStringParameters[$parameter] ?? '');
if (ITPINGS_QUERY_TRACE) QueryTrace($parameter, $parameter_value);
if ($parameter_value) {
$PARAMETER_HAS_SEPARATOR = contains($parameter_value, QUERY_PARAMETER_SEPARATOR);
$and = $where === EMPTY_STRING ? EMPTY_STRING : " AND ";
switch ($parameter) {
case QUERY_PARAMETER_FILTER:
//do NOT ADD to $where, the function creates a new $where with previous content prepended
$where = process_QueryParameter_Filter($where, $and, $parameter_value);
break;
case QUERY_PARAMETER_INTERVAL:
//https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-add
$interval_unit = strtoupper($QueryStringParameters[QUERY_PARAMETER_INTERVALUNIT]);
if (!in_array($interval_unit, $_QUERY_ALLOWED_INTERVALUNITS)) {
$interval_unit = 'HOUR';
}
$where .= $and . ITPINGS_CREATED_TIMESTAMP . " >= DATE_SUB(NOW(), INTERVAL " . (int)$parameter_value . " $interval_unit)";
break;
case QUERY_PARAMETER_INTERVALUNIT:// processed in previous interval case
break;
case QUERY_PARAMETER_BY10MINUTES:
$where .= $and . " MINUTE(" . ITPINGS_CREATED_TIMESTAMP . ") IN (0,10,20,30,40,50) ";
break;
case QUERY_PARAMETER_ORDERBY:
if ($PARAMETER_HAS_SEPARATOR) {
$orderbyfields = [];
//accept only valid fieldnames
foreach (explode(QUERY_PARAMETER_SEPARATOR, $parameter_value) as $field) {
$field = explode(' ', $field);
$fieldname = $field[0];
$fieldsort = $field[1];
if (!$fieldsort) $fieldsort = 'ASC';
if (in_array($fieldname, $_VALID_QUERY_PARAMETERS)) {
$orderbyfields[] .= $fieldname . ' ' . $fieldsort;
}
}
$parameter_value = implode(QUERY_PARAMETER_SEPARATOR, $orderbyfields);
}
$order .= " ORDER BY " . $parameter_value;
break;
case QUERY_PARAMETER_LIMIT:
switch (strtoupper($parameter_value)) {
case 'NONE':
case 'FALSE':
case '':
$limit = 'NONE';
break;
default:
$limit = Valued($parameter_value);
}
break;
default:
if (contains($parameter_value, '%')) {
$where .= $and . "$parameter LIKE " . Quoted($parameter_value);
} else {
if ($PARAMETER_HAS_SEPARATOR) {
$where .= $and . "$parameter IN(";
$glue = QUERY_PARAMETER_SEPARATOR;
$where .= implode($glue, array_map('convertValue_to_QuotedString_or_Integer', explode($glue, $parameter_value)));
$where .= ")";
} else {
$parameter_value = (is_numeric($parameter_value) ? Valued($parameter_value) : Quoted($parameter_value));
$where .= $and . "$parameter = " . $parameter_value;
}
}
if (ITPINGS_QUERY_TRACE) QueryTrace($parameter, $parameter_value);
break;
}
}
}
$sql = "SELECT * FROM $table_name";
if ($where !== EMPTY_STRING) $sql .= " WHERE $where";
if ($limit === 'NONE') {
$limit = EMPTY_STRING;
} else if ($limit === EMPTY_STRING) {
$limit = " LIMIT " . SQL_LIMIT_DEFAULT;
} else {
$limit = " LIMIT " . $limit;
}
$sql .= $order . $limit;
}
if ($sql === EMPTY_STRING) {
$JSON_response[QUERYKEY_errors] .= "Error: Empty SQL statement";
} else {
if ($sql === NO_SQL_QUERY) {
remove_no_longer_required_keys_from_JSON_response();
return_JSON_response();
} else {
SQL_Query($sql, TRUE);
}
}
}
//endregion == PROCESS GET QUERY ==================================================================