-
Notifications
You must be signed in to change notification settings - Fork 14
/
sldb.php
117 lines (97 loc) · 4.13 KB
/
sldb.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
<?php
class sldbRequest {
private $connection;
var $output;
var $result;
var $table;
function __construct($db_host, $db_user, $db_pass, $db_name, $table) {
require_once('config.php');
$this->connection = mysql_connect($db_host, $db_user, $db_pass) or die ('ERROR: CANNOT CONNECT TO DATABASE.');
mysql_select_db($db_name) or die('ERROR: CANNOT SELECT DATABASE.');
$this->table = $table;
}
/**
* Getter function for the output of the query.
* @param string $separator
* (optional) The separator to use. LSL cannot handle arrays or objects
* because it was slapped together and never fixed, so the output has to be
* a list with a constant separator that can be used to parse it.
*/
function getOutput($separator = '&') {
return implode($separator, (array)$this->output);
}
/**
* Create a table for use.
*/
function createTable() {
$sql = "CREATE TABLE IF NOT EXISTS `" . $this->table . "` (`uuid` varchar(32) NOT NULL DEFAULT '', `field` varchar(255) NOT NULL DEFAULT '', `value` longtext, `changed` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`uuid`,`field`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
$this->result = mysql_query($sql) or die(mysql_error());
$this->output = "SUCCESS";
}
/**
* Update uuid/field data pairs.
*
* @param string $uuid
* The user's uuid
* @param array $data
* An array of data to update, where the keys are the fields and their
* values are the values stored in the db.
* @param boolean $verbose
* TRUE for longer output.
*/
function updateData($uuid, $data, $verbose = FALSE) {
foreach($data as $key => $value) {
$sql = "INSERT INTO " . $this->table . " (uuid, field, value, changed) VALUES ('$uuid', '$key', '$value', UNIX_TIMESTAMP(NOW())) ON DUPLICATE KEY UPDATE value = '$value', changed = UNIX_TIMESTAMP(NOW())";
$this->result = mysql_query($sql) or die(mysql_error());
$this->output = $verbose ? "SUCCESS: " . mysql_affected_rows() : mysql_affected_rows();
}
}
/**
* Read data for a uuid or uuid/field combination.
*
* @param string $uuid
* The uuid for the user. The uuid can also be any unique string.
* @param array $fields
* (optional) The fields element provided with the request. Contains a list
* of fields. If not provided, all field/value combinations will be returned.
* @param boolean $verbose
* (optional) TRUE to return field/value pairs, FALSE for just the value.
* @param string $separator
* (optional) A glue string to implode the results. Default is '='.
*/
function readData($uuid, $fields = array(), $verbose = FALSE, $separator = '=') {
$fields = (array)$fields;
foreach($fields AS $key => $field) {
$fields[$key] = "'" . $field . "'";
}
$columns = $verbose ? 'field, value' : 'value';
$sql = "SELECT $columns FROM " . $this->table . " WHERE uuid = '$uuid'";
$sql .= empty($fields) ? '' : " AND field IN (" . implode(', ', (array)$fields) . ")";
$this->result = mysql_query($sql) or die(mysql_error());
while($record = mysql_fetch_assoc($this->result)) {
$record['value'] = $record['value'] == '' ? 'NULL' : $record['value'];
$this->output[] = implode($separator, $record);
}
}
/**
* Delete data for a uuid or a uuid/field combination.
*
* @param string $uuid
* The uuid for the user. The uuid can also be any unique string.
* @param array $fields
* (optional) An array of fields to delete. If no fields are provided, all
* fields for that user will be deleted.
* @param boolean $verbose
* (optional) TRUE for longer output.
*/
function deleteData($uuid, $fields = array(), $verbose = FALSE) {
$fields = (array)$fields;
foreach($fields AS $key => $field) {
$fields[$key] = "'" . $field . "'";
}
$sql = "DELETE FROM " . $this->table . " WHERE uuid = '$uuid'";
$sql .= empty($fields) ? '' : " AND field IN (" . implode(', ', (array)$fields) . ")";
$this->result = mysql_query($sql) or die(mysql_error());
$this->output = $verbose ? "SUCCESS: " . mysql_affected_rows() : mysql_affected_rows();
}
}