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
|
<?php
/*
* SCROTT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to UNLICENSE
*/
require_once "class/database.class.php";
/*
* This class provides functionality for retrieving data from and updating
* data in a database table.
*/
abstract class table
{
/*
* This is an array acting as a dictionary, mapping a table name (key)
* into another array acting as a list (value) of the fields in that
* table. Classes that extend this class, should append a key=>val to
* this array that represents the table being modeled. This array is
* used by db table IO functions to construct SQL queries.
*/
protected $fields = array();
/*
* Instanciate an object representing an existing database entity
* named by the given GUID. If no such GUID exists, an exception
* is thrown. If NULL is passed, no database lookups are attempted.
* This is helpful for assembling new database objects.
*/
public function __construct(?string $guid = NULL)
{
if ($guid)
$this->loadObj($guid);
}
/*
* This function will iterate the $fields array and initialize this
* object's class properties based on the table=>fields mapping. If
* no such GUID exists, an exception is thrown.
*/
private function loadObj(string $guid) : void
{
$guid = database::esc($guid);
if (!$this->isGUID($guid))
throw new Exception("GUID " . $guid . " does not exist");
foreach ($this->fields as $tbl => $flds)
{
$tbl = database::esc($tbl);
$query = "SELECT * FROM " . $tbl . " WHERE guid = '" . $guid . "'";
$res = database::query($query)[0];
foreach ($flds as $fld)
{
if (isset($res[$fld]))
$this->$fld = $res[$fld];
}
}
}
/*
* This function will update this object in the database, or insert new
* data if this object does not yet have a GUID. This function uses the
* $fields array to construct SQL queries.
*/
public function saveObj() : void
{
/* update existing object */
if (isset($this->guid))
{
$this->updated = $this->getCurrentTimestamp();
foreach ($this->fields as $tbl => $flds)
{
$tbl = database::esc($tbl);
$udstr = "";
foreach ($flds as $fld)
{
if (!isset($this->$fld))
continue;
$fld = database::esc($fld);
$udstr .= $fld . " = '" . database::esc($this->$fld) . "', ";
}
if (strlen($udstr) > 0)
{
$udstr = substr($udstr, 0, -2); // remove ", " from the end
$query = "UPDATE " . $tbl . " SET " . $udstr . " WHERE guid = '" . database::esc($this->guid) . "'";
database::query($query);
}
}
}
/* create new object */
else
{
$this->guid = $this->getNewGUID();
$this->created = $this->getCurrentTimestamp();
$this->updated = $this->created;
foreach ($this->fields as $tbl => $flds)
{
$tbl = database::esc($tbl);
$fldstr = "";
$valstr = "";
foreach ($flds as $fld)
{
if (!isset($this->$fld))
continue;
$fld = database::esc($fld);
$fldstr .= $fld . ", ";
$valstr .= "'" . database::esc($this->$fld) . "', ";
}
if (strlen($fldstr) > 0)
{
$fldstr = substr($fldstr, 0, -2); // remove ", "
$valstr = substr($valstr, 0, -2);
$query = "INSERT INTO " . $tbl . " (" . $fldstr . ") VALUES (" . $valstr . ")";
database::query($query);
}
}
}
}
/*
* This function will remove this object from the database by deleting
* rows with this object's GUID from tables in $fields. If this object
* does not have a GUID, throw an exception.
*/
public function delObj() : void
{
if (!isset($this->guid))
throw new Exception("GUID (null) does not exist");
$guid = database::esc($this->guid);
foreach ($this->fields as $tbl => $flds)
{
$tbl = database::esc($tbl);
$query = "DELETE FROM " . $tbl . " WHERE guid = '" . $guid . "'";
database::query($query);
}
/* garbage collection */
$query = "DELETE FROM members WHERE guid = '" . $guid . "' OR member = '" . $guid . "'";
database::query($query);
$query = "DELETE FROM views WHERE guid = '" . $guid . "' OR viewer = '" . $guid . "'";
database::query($query);
}
/*
* Get a random sha256 blob, returned as a hexadecimal string
*/
public function getBlob() : string
{
return hash("sha256", openssl_random_pseudo_bytes(64));
}
/*
* Get current timestamp as a string for object database purposes
*/
private function getCurrentTimestamp() : string
{
$query = "SELECT now() AS stamp";
$res = database::query($query);
return $res[0]['stamp'];
}
/*
* Check whether the given GUID exists
*/
private function isGUID(string $guid) : bool
{
$guid = database::esc($guid);
$query = "SELECT guid FROM objects WHERE guid = '" . $guid . "'";
$res = database::query($query);
return count($res) > 0;
}
/*
* Get a new, unique GUID for a new system object
*/
private function getNewGUID() : string
{
do $guid = substr($this->getBlob(), 0, 8);
while ($this->isGUID($guid));
return $guid;
}
}
?>
|