-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathUnitySQL.php
More file actions
346 lines (318 loc) · 10.8 KB
/
Copy pathUnitySQL.php
File metadata and controls
346 lines (318 loc) · 10.8 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
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
<?php
namespace UnityWebPortal\lib;
use PDO;
use PDOException;
/**
* @phpstan-type user_last_login array{operator: string, last_login: int}
* @phpstan-type pi_group_expiration_date array{gid: string, expiration_date: int}
* @phpstan-type request array{request_for: string, uid: string, timestamp: string}
*/
class UnitySQL
{
private const string TABLE_REQS = "requests";
private const string TABLE_AUDIT_LOG = "audit_log";
private const string TABLE_USER_LAST_LOGINS = "user_last_logins";
private const string TABLE_PI_GROUP_EXPIRATION_DATES = "pi_group_expiration_dates";
// FIXME this string should be changed to something more intuitive, requires production change
public const string REQUEST_BECOME_PI = "admin";
private const int TABLE_AUDIT_LOG_RECIPIENT_MAX_MB_STR_LEN = 768;
private PDO $conn;
/** @throws PDOException */
public function __construct()
{
$this->conn = new PDO(
"mysql:host=" . CONFIG["sql"]["host"] . ";dbname=" . CONFIG["sql"]["dbname"],
CONFIG["sql"]["user"],
CONFIG["sql"]["pass"],
);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function getConn(): PDO
{
return $this->conn;
}
/** @throws PDOException */
public function addRequest(string $requestor, string $dest): void
{
if ($this->requestExists($requestor, $dest)) {
return;
}
$stmt = $this->conn->prepare(
"INSERT INTO " . self::TABLE_REQS . " (uid, request_for) VALUES (:uid, :request_for)",
);
$stmt->bindParam(":uid", $requestor);
$stmt->bindParam(":request_for", $dest);
$stmt->execute();
}
/** @throws PDOException */
public function removeRequest(string $requestor, string $dest): void
{
if (!$this->requestExists($requestor, $dest)) {
return;
}
$stmt = $this->conn->prepare(
"DELETE FROM " . self::TABLE_REQS . " WHERE uid=:uid and request_for=:request_for",
);
$stmt->bindParam(":uid", $requestor);
$stmt->bindParam(":request_for", $dest);
$stmt->execute();
}
/** @throws PDOException */
public function removeRequests(string $dest): void
{
$stmt = $this->conn->prepare(
"DELETE FROM " . self::TABLE_REQS . " WHERE request_for=:request_for",
);
$stmt->bindParam(":request_for", $dest);
$stmt->execute();
}
/**
* @throws PDOException
* @throws \Exception if the request is not found or multiple requests are found (FIXME)
* @return request
*/
public function getRequest(string $user, string $dest): array
{
$stmt = $this->conn->prepare(
"SELECT * FROM " . self::TABLE_REQS . " WHERE uid=:uid and request_for=:request_for",
);
$stmt->bindParam(":uid", $user);
$stmt->bindParam(":request_for", $dest);
$stmt->execute();
$result = $stmt->fetchAll();
if (count($result) == 0) {
throw new \Exception("no such request: uid='$user' request_for='$dest'");
}
if (count($result) > 1) {
throw new \Exception("multiple requests for uid='$user' request_for='$dest'");
}
return $result[0];
}
/** @throws PDOException */
public function requestExists(string $requestor, string $dest): bool
{
try {
$this->getRequest($requestor, $dest);
return true;
// FIXME use a specific exception
} catch (\Exception) {
return false;
}
}
/**
* @return request[]
* @throws PDOException
*/
public function getAllRequests(): array
{
$stmt = $this->conn->prepare("SELECT * FROM " . self::TABLE_REQS);
$stmt->execute();
return $stmt->fetchAll();
}
/**
* @return request[]
* @throws PDOException
*/
public function getRequests(string $dest): array
{
$stmt = $this->conn->prepare(
"SELECT * FROM " . self::TABLE_REQS . " WHERE request_for=:request_for",
);
$stmt->bindParam(":request_for", $dest);
$stmt->execute();
return $stmt->fetchAll();
}
/**
* @return request[]
* @throws PDOException
*/
public function getRequestsByUser(string $user): array
{
$stmt = $this->conn->prepare("SELECT * FROM " . self::TABLE_REQS . " WHERE uid=:uid");
$stmt->bindParam(":uid", $user);
$stmt->execute();
return $stmt->fetchAll();
}
/** @throws PDOException */
public function deleteRequestsByUser(string $user): void
{
$stmt = $this->conn->prepare("DELETE FROM " . self::TABLE_REQS . " WHERE uid=:uid");
$stmt->bindParam(":uid", $user);
$stmt->execute();
}
/** @throws PDOException */
public function addLog(string $action_type, string $recipient): void
{
if (mb_strlen($recipient, "UTF-8") > self::TABLE_AUDIT_LOG_RECIPIENT_MAX_MB_STR_LEN) {
UnityHTTPD::errorLog("warning", "audit log recipient truncated", data: $recipient);
$recipient = mb_substr(
$recipient,
0,
self::TABLE_AUDIT_LOG_RECIPIENT_MAX_MB_STR_LEN,
"UTF-8",
);
}
$table = self::TABLE_AUDIT_LOG;
$stmt = $this->conn->prepare(
"INSERT INTO $table (operator, operator_ip, action_type, recipient)
VALUE (:operator, :operator_ip, :action_type, :recipient)",
);
$stmt->bindValue(":operator", $_SESSION["OPERATOR"] ?? "");
$stmt->bindValue(":operator_ip", $_SESSION["OPERATOR_IP"] ?? "");
$stmt->bindParam(":action_type", $action_type);
$stmt->bindParam(":recipient", $recipient);
$stmt->execute();
}
/** @throws PDOException */
public function updateUserLastLogin(string $uid): void
{
$table = self::TABLE_USER_LAST_LOGINS;
$stmt = $this->conn->prepare("
INSERT INTO $table
(operator, last_login)
VALUES(:uid, CURRENT_TIMESTAMP)
ON DUPLICATE KEY UPDATE
last_login=CURRENT_TIMESTAMP
");
$stmt->bindParam(":uid", $uid);
$stmt->execute();
}
/**
* @return user_last_login[]
* @throws PDOException
*/
public function getAllUserLastLogins(): array
{
$stmt = $this->conn->prepare("SELECT * FROM " . self::TABLE_USER_LAST_LOGINS);
$stmt->execute();
$records = $stmt->fetchAll();
$output = [];
foreach ($records as $record) {
array_push($output, [
"operator" => $record["operator"],
"last_login" => strtotime($record["last_login"]),
]);
}
return $output;
}
/** @throws PDOException */
public function convertLastLoginToDaysIdle(?int $timestamp, ?int $now = null): int
{
if ($timestamp === null) {
return 0;
}
$now ??= time();
$idle_seconds = $now - $timestamp;
return intdiv($idle_seconds, 60 * 60 * 24);
}
/**
* for testing purposes
* @throws PDOException
*/
private function setUserLastLogin(string $uid, int $timestamp): void
{
$datetime = date("Y-m-d H:i:s", $timestamp);
$table = self::TABLE_USER_LAST_LOGINS;
$stmt = $this->conn->prepare("
INSERT INTO $table
VALUES (:uid, :datetime)
ON DUPLICATE KEY
UPDATE last_login=:datetime;
");
$stmt->bindParam(":uid", $uid);
$stmt->bindParam(":datetime", $datetime);
$stmt->execute();
}
/**
* for testing purposes
* @throws PDOException
*/
private function removeUserLastLogin(string $uid): void
{
$table = self::TABLE_USER_LAST_LOGINS;
$stmt = $this->conn->prepare("DELETE FROM $table WHERE operator=:uid");
$stmt->bindParam(":uid", $uid);
$stmt->execute();
}
/**
* @throws PDOException
* @throws \Exception if multiple records are found (this should never happen)
*/
public function getUserLastLogin(string $uid): ?int
{
$table = self::TABLE_USER_LAST_LOGINS;
$stmt = $this->conn->prepare("SELECT * FROM $table WHERE operator=:uid");
$stmt->bindParam(":uid", $uid);
$stmt->execute();
$result = $stmt->fetchAll();
if (count($result) == 0) {
return null;
}
if (count($result) > 1) {
throw new \Exception("multiple records found with operator '$uid'");
}
$timestamp_str = $result[0]["last_login"];
return strtotime($timestamp_str);
}
/**
* @throws PDOException
* @throws \Exception if multiple records are found (this should never happen)
*/
public function getPIGroupExpirationDate(string $gid): int|null
{
$table = self::TABLE_PI_GROUP_EXPIRATION_DATES;
$stmt = $this->conn->prepare("SELECT * FROM $table WHERE gid=:gid");
$stmt->bindParam(":gid", $gid);
$stmt->execute();
$result = $stmt->fetchAll();
if (count($result) == 0) {
return null;
}
if (count($result) > 1) {
throw new \Exception("multiple records found with gid '$gid'");
}
$timestamp_str = $result[0]["expiration_date"];
return strtotime($timestamp_str);
}
/** @throws PDOException */
public function setPIGroupExpirationDate(string $gid, int $expiration_date): void
{
$table = self::TABLE_PI_GROUP_EXPIRATION_DATES;
$stmt = $this->conn->prepare("
INSERT INTO $table
VALUES (:gid, :expiration_date)
ON DUPLICATE KEY
UPDATE expiration_date=:expiration_date
");
$stmt->bindParam(":gid", $gid);
$expiration_date_str = date("Y-m-d H:i:s", $expiration_date);
$stmt->bindParam(":expiration_date", $expiration_date_str);
$stmt->execute();
}
/** @throws PDOException */
public function removePIGroupExpirationDate(string $gid): void
{
$table = self::TABLE_PI_GROUP_EXPIRATION_DATES;
$stmt = $this->conn->prepare("DELETE FROM $table WHERE gid=:gid");
$stmt->bindParam(":gid", $gid);
$stmt->execute();
}
/**
* @throws PDOException
* @return pi_group_expiration_date[]
*/
public function getAllPIGroupExpirationDates(): array
{
$stmt = $this->conn->prepare("SELECT * FROM " . self::TABLE_PI_GROUP_EXPIRATION_DATES);
$stmt->execute();
$records = $stmt->fetchAll();
$output = [];
foreach ($records as $record) {
array_push($output, [
"gid" => $record["gid"],
"expiration_date" => strtotime($record["expiration_date"]),
]);
}
return $output;
}
}