forked from UB-Mannheim/PalMA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBConnector.class.php
331 lines (280 loc) · 10.9 KB
/
DBConnector.class.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
<?php
// Copyright (C) 2014 Universitätsbibliothek Mannheim
// See file LICENSE for license details.
// Authors: Alexander Wagner, Stefan Weil
// Test whether the script was called directly (used for unit test).
if (!isset($unittest)) {
$unittest = array();
}
$unittest[__FILE__] = (sizeof(get_included_files()) == 1);
function trace($text) {
error_log("palma: $text");
}
class DBConnector extends SQLite3
{
private $_WINDOWS = array();
private $_WINDOW_COUNT;
const SQL_CREATE_TABLES = <<< eod
PRAGMA foreign_keys = ON;
CREATE TABLE IF NOT EXISTS setting (
key VARCHAR (10) PRIMARY KEY,
value VARCHAR (20)
);
INSERT OR IGNORE INTO setting VALUES ('layout', 'g1x1');
INSERT OR IGNORE INTO setting VALUES ('pin', '');
CREATE TABLE IF NOT EXISTS user (
userid INTEGER PRIMARY KEY,
name VARCHAR (30) UNIQUE,
count INTEGER,
enabled INTEGER
);
-- Table with user name, IP address and device type (laptop, tablet, mobile).
CREATE TABLE IF NOT EXISTS address (
userid INTEGER,
address VARCHAR (30),
device VARCHAR (6),
FOREIGN KEY(userid) REFERENCES user(userid)
);
-- TODO: Entry 'userid' in table 'window' should refer to user(userid):
-- userid INTEGER REFERENCES user(userid)
-- The software currently does not handle this correctly, so we had to remove
-- the reference because it fails with pragma foreign_keys.
CREATE TABLE IF NOT EXISTS window (
id INTEGER PRIMARY KEY,
win_id VARCHAR (3),
section INTEGER,
state VARCHAR (10),
file VARCHAR (255),
handler VARCHAR (255),
userid INTEGER,
date DATETIME
);
eod;
const SQL_RESET_TABLES = <<< eod
DROP TABLE address;
DROP TABLE setting;
DROP TABLE IF EXISTS settings;
DROP TABLE user;
DROP TABLE window;
eod;
// TODO: allow additional flags for constructor:
// $flags = SQLITE3_OPEN_READWRITE|SQLITE3_OPEN_CREATE
// $encryption_key
public function __construct($filename = false) {
if (!$filename) {
$filename = dirname(__FILE__) . '/palma.db';
}
trace("db file = $filename");
parent::__construct($filename);
// Wait up to 10000 ms when the database is locked.
$this->busyTimeout(10000);
// Create any missing tables.
$this->exec(self::SQL_CREATE_TABLES);
$q_init = $this->query('SELECT MAX(id) FROM window WHERE state="active"');
/* if($q_init == true) {
$this->_WINDOW_COUNT = $this->countWindows();
$this->_WINDOWS = $this->getWindows();
} */
}
public function resetTables() {
$this->exec(self::SQL_RESET_TABLES . self::SQL_CREATE_TABLES);
}
public function countWindows() {
$numRows = $this->querySingle('SELECT count(*) FROM window WHERE state="active"');
return $numRows;
}
public function nextID() {
// Find the first unused monitor section and return its number.
$quadrant_ids = array(1, 2, 3, 4);
$window_db_ids = array();
$window_keys = @$this->query('SELECT DISTINCT(id) FROM window');
while ($row = $window_keys->fetchArray()) {
array_push($window_db_ids, $row['id']);
}
$window_keys->finalize();
//~ trace("ids in QD " . serialize($quadrant_ids));
//~ trace("ids in DB " . serialize($window_db_ids));
$next_ids = array_values(array_diff($quadrant_ids, $window_db_ids));
//~ trace("ids in NXT " . serialize($next_ids));
if (count($next_ids) > 0) {
$next = $next_ids[0];
} else {
$next = $this->querySingle('SELECT MAX(id) FROM window') + 1;
}
return $next;
}
public function ipAddress() {
$ip = 'unknown';
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
// Server is hidden behind a proxy.
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else if (isset($_SERVER['REMOTE_ADDR'])) {
// Client has direct access to the server.
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
public function addUser($username, $address, $device = 'laptop') {
// Add a new user with his/her address and the device to the database.
// TODO: Support more than one address for a given username.
$this->exec("INSERT OR IGNORE INTO user VALUES (NULL, '$username', 1, 0)");
$usercount = $this->querySingle("SELECT COUNT(*) FROM user");
$userid = $this->querySingle("SELECT userid from user where name='".$username."'");
$this->exec("INSERT INTO address VALUES ('$userid', '$address', '$device')");
trace("user $username connected with $device, $usercount user(s) now connected");
if ($usercount == 1) {
// First user connected. Always enable this person.
$this->enableUser($username);
}
}
public function delUser($username, $address) {
// Remove an existing user with his/her address from the database.
// TODO: Support more than one address for a given username.
$ip = $this->ipAddress();
$userid = $this->querySingle("SELECT userid from user where name='$username'");
$this->exec("DELETE FROM address WHERE userid = '$userid' AND address = '$ip'");
// TODO: Remove user only when no address refers to it.
$this->exec("DELETE FROM user WHERE userid = '$userid'");
$usercount = $this->querySingle("SELECT COUNT(*) FROM user");
trace("user $username disconnected, $usercount user(s) now connected");
if ($usercount == 0) {
// Last user disconnected.
// Clean some tables, just to be sure that nothing is left.
$this->exec("DELETE FROM address; DELETE FROM window;");
}
}
public function enableUser($username) {
$state = $this->exec("UPDATE user SET enabled=1 WHERE name='$username'");
if (!$state) {
trace("enableUser($username) failed");
}
}
public function getUsers() {
$users = array();
$rows = $this->query("SELECT name FROM user");
while ($row = $rows->fetchArray(SQLITE3_ASSOC)) {
array_push($users, $row['name']);
}
$rows->finalize();
return $users;
}
public function getWindows() {
$window_objs = array();
$windows = @$this->query('SELECT * FROM window');
while ($row = $windows->fetchArray()) {
array_push($window_objs, $row);
}
$windows->finalize();
return $window_objs;
}
public function getWindowsOrderBy($field, $order) {
$window_objs = array();
$windows = @$this->query("SELECT * FROM window ORDER BY $field $order");
while ($row = $windows->fetchArray()) {
array_push($window_objs, $row);
}
$windows->finalize();
return $window_objs;
}
public function getWindowIDBySection($section) {
$id = $this->querySingle("SELECT win_id FROM window WHERE section='$section'");
return $id;
}
public function getVNC_ClientInfo() {
$info = array();
$i = @$this->query('SELECT * FROM window WHERE handler="vnc"');
while ($row = $i->fetchArray()) {
array_push($info, $row);
}
$i->finalize();
return $info;
}
/*
public function getVNC_ClientWindowIDs() {
$ids = array();
$rows = @$this->query('SELECT win_id FROM window WHERE handler="vnc"');
while ($row = $rows->fetchArray()) {
array_push($ids, $row['win_id']);
}
$rows->finalize();
return $ids;
}
*/
public function getState_Window($window_id) {
$state = @$this->querySingle('SELECT state FROM window WHERE win_id="'.$window_id.'"');
return $state;
}
public function setState_Window($window_id, $state) {
$this->exec('UPDATE window SET state="'.$state.'" WHERE win_id="'.$window_id.'"');
}
public function insertWindow($window) {
// transfer ob complete window object/array necessary
$sql = 'INSERT INTO window (id, win_id, section, state, file, handler, userid, date) ' .
'VALUES ' . '("' .
$window[0] . '", "' . $window[1] . '", "' .
$window[2] . '", "' . $window[3] . '", "' .
$window[4] . '", "' . $window[5] . '", "' .
$window[6] . '", "' . $window[7] . '")';
$new = $this->exec($sql);
trace("sql=$sql, result=$new");
}
public function deleteWindow($window_id) {
$this->exec('DELETE FROM window WHERE win_id="'.$window_id.'"');
}
/*
public function deleteVNCWindow($vnc_id) {
$this->exec('DELETE FROM window WHERE file="'.$vnc_id.'"');
}
*/
public function deleteDebug($table, $id, $gt) {
$this->exec('DELETE FROM '.$table.' WHERE '.$id.' >"'.$gt.'"');
}
public function updateWindow($window_id, $field, $value) {
$this->exec('UPDATE window SET '.$field.'="'.$value.'" WHERE win_id="'.$window_id.'"');
}
}
if ($unittest[__FILE__]) {
// Run unit test.
function dbModifiedCallback() {
echo("Triggered callback\n");
}
//~ var_dump($_SERVER);
$db = new DBConnector('palma-test.db');
$db->resetTables();
//~ http://stackoverflow.com/questions/1964233/does-sqlite3-support-a-trigger-to-automatically-update-an-updated-on-datetime
//~ https://www.sqlite.org/lang_createtrigger.html
// TODO: Test database triggers (can be used in db.php).
//~ $db->createFunction('dbModifiedCallback', 'dbModifiedCallback', 0);
// {AFTER | BEFORE} {DELETE | INSERT | UPDATE } ON table
//~ $db->exec("CREATE TRIGGER dbchange AFTER UPDATE ON user
//~ BEGIN
//~ dbModifiedCallback();
//~ END");
//~ $db = new DBConnector('palma.db');
echo "Tables=" . $db->querySingle('SELECT count(*) FROM sqlite_master WHERE type="table"') . "\n";
var_dump($db->query("SELECT * from sqlite_master"));
$address = $db->ipAddress();
$db->addUser('testuser1', $address);
//~ $db->addUser('testuser2', $address);
//~ $db->addUser('testuser3', $address);
//~ $db->addUser('testuser4', $address);
$users = $db->getUsers();
$db->enableUser('testuser1');
var_dump($users);
$db->delUser('haenger', $address);
$db->delUser('ivwz', $address);
$db->delUser('skrieg', $address);
$db->delUser('testuser1', $address);
$db->delUser('testuser2', $address);
$db->delUser('testuser3', $address);
$db->delUser('testuser4', $address);
echo "Query=" . $db->querySingle('select name from user') . "\n";
echo "Query=" . $db->querySingle('select count(name) from user') . "\n";
echo "Query=" . $db->querySingle('select value from setting where key="layout"') . "\n";
$db->close();
//~ $db = new DBConnector('palma.db');
//~ $users = $db->getUsers();
//~ var_dump($users);
//~ $db->close();
}
?>