-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_management.php
More file actions
100 lines (94 loc) · 2.85 KB
/
data_management.php
File metadata and controls
100 lines (94 loc) · 2.85 KB
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
<?php
/*
vIDS (virtual Information Display System) for VATSIM
Filename: data_management.php
Function: Handles I/O of persistent data
Created: 9/6/2021
Edited:
Changes:
*/
include_once "config.php";
include_once "common.php";
include_once "mysql_db.php";
$dbconn = null;
if (USE_DB) {
// Start DB connection and select the DB
$db = new MySQL_db(fetch_my_url(),$db_variables);
}
// For testing use only
//data_save('blacklist','dat','12345\n67890\n',false);
//data_read('blacklist','dat','array');
function data_read($item,$outputType,$queryString=null) {
if(USE_DB) {
if($queryString == null) {
$queryString = "SELECT payload FROM legacy WHERE token = '$item' LIMIT 1";
}
$data = $GLOBALS['db']->query($queryString);
//if(mysqli_num_rows($data)>0) {
if($GLOBALS['db']->row_exists($data)) {
//$data = mysqli_fetch_array($data);
$data = $GLOBALS['db']->fetch_array($data);
if($outputType == 'string') {
$data = $data[0];
//$data = implode('\n',mysqli_fetch_array($data));
}
elseif($outputType == 'array') {
//$data = mysqli_fetch_assoc($data);
//$data = preg_split('/\n/', $data[0], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); // array_filter removes blank fields...
$data = array_filter(preg_split('/\n/', $data[0])); // array_filter removes blank fields...
array_walk($data, function(&$value, $key) { $value .= "\n"; } ); // For compatibility with file operations, adds an end line character to each element - can eliminate this someday when we get away from file system operations
}
}
else {
$data = '';
if($outputType == 'array') {
$data = array();
}
}
}
else {
if(file_exists("data/" . $item)) {
if($outputType == 'string') {
$data = file_get_contents("data/" . $item);
}
elseif($outputType == 'array') {
$data = file("data/" . $item);
}
}
else {
$data = '';
}
}
return $data;
}
function data_save($item,$data,$overwrite=true,$queryString=null) {
if(USE_DB) {
$data = $GLOBALS['db']->escape($data);
if($queryString == null) {
// Determine if this is an insert or update query
//if(mysqli_num_rows($GLOBALS['db']->query("SELECT ref FROM legacy WHERE token = '$item'")) > 0) { // Update
if($GLOBALS['db']->row_exists($GLOBALS['db']->query("SELECT ref FROM legacy WHERE token = '$item'"))) { // Update
if($overwrite) {
$queryString = "UPDATE legacy SET payload='$data' WHERE token = '$item'";
}
else {
$queryString = "UPDATE legacy SET payload=CONCAT(payload,'$data') WHERE token = '$item'";
}
}
else { // Insert
$queryString = "INSERT INTO legacy VALUES (null,'$item','$data')";
}
}
//echo $queryString;
$data = $GLOBALS['db']->query($queryString);
}
else {
if(!$overwrite) {
file_put_contents("data/" . $item,$data,FILE_APPEND);
}
else {
file_put_contents("data/" . $item,$data);
}
}
}
?>