-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
executable file
·563 lines (479 loc) · 20.6 KB
/
db.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
<?php
/*
CNTP - DB Configuration Handler for MariaDB using PHP-MySQLi
v0.2 sen
*/
require_once('config.php');
class db {
/* :::::::::::::::::::::::::::::::::::::::::::::::
::: DB Connection :::
::::::::::::::::::::::::::::::::::::::::::::::: */
private $db_connection = 0;
private $user_id = 0;
function db_connect($user_id) {
$this->user_id = $user_id;
require_once('db_credentials.php');
$db_connection = new mysqli(db_cred::$host, db_cred::$user, db_cred::$pass, db_cred::$db);
if ($db_connection->connect_error) {
die('[MYSQLI ERROR] Could not connect to DB: ' . $db_connection->connect_error);
}
$this->db_connection = $db_connection;
}
function db_disconnect() {
$this->db_connection->close();
}
/* :::::::::::::::::::::::::::::::::::::::::::::::
::: DB Operation :::
::::::::::::::::::::::::::::::::::::::::::::::: */
function db_op($sql, $param, $op, $obj) {
if ($op === 'write' && $this->user_id === 1 && $obj !== 'weather') {
// write protection for demo user excluding weather cache
return false;
} else {
// prepare statement
$stmt = $this->db_connection->stmt_init();
if(!$stmt->prepare($sql))
{
error_log("[MYSQLI ERROR] Failed to prepare statement: ".$sql."\n--> ".mysqli_error($this->db_connection));
return false;
}
// prepare parameters --> "..." splat an array as multiple parameters
if ($param) {
$stmt->bind_param(...$param);
}
// execute statement
if(!$stmt->execute()) {
error_log("[MYSQLI ERROR] Failed to execute statement: ".$sql."\n--> ".mysqli_error($this->db_connection));
return false;
} else {
if ($op === 'write') {
return true;
} else {
$result = $stmt->get_result();
if ($op === 'read') {
if ($obj) {
// return an object
while($row = mysqli_fetch_object($result)) {
return $row->$obj;
}
} else {
// return an array (1 dimensional)
return mysqli_fetch_array($result, MYSQLI_NUM);
}
} else if ($op === 'read_multi') {
// return an array (2 dimensional)
return mysqli_fetch_all($result, MYSQLI_NUM);
}
mysqli_free_result($result);
}
}
}
}
####################### SQL #######################
/* :::::::::::::::::::::::::::::::::::::::::::::::
::: Theme :::
::::::::::::::::::::::::::::::::::::::::::::::: */
function get_theme() {
$sql = 'SELECT theme.min, theme_select.ext_css FROM theme_select
INNER JOIN theme ON theme_select.theme_id = theme.id
WHERE theme_select.user_id=?';
$param = array('s', $this->user_id);
return $this->db_op($sql, $param, 'read', '');
}
function get_themes() {
$sql = 'SELECT id, name, author, file FROM theme ORDER BY name';
return $this->db_op($sql, '', 'read_multi', 'file');
}
function get_theme_conf() {
$sql = 'SELECT theme_id, ext_css FROM theme_select WHERE user_id=?';
$param = array('s', $this->user_id);
return $this->db_op($sql, $param, 'read', '');
}
function post_theme($id, $ext_css) {
$sql = 'UPDATE theme_select SET theme_id=?, ext_css=? WHERE user_id=?';
$param = array('sss', $id, $ext_css, $this->user_id);
return $this->db_op($sql, $param, 'write', '');
}
/* :::::::::::::::::::::::::::::::::::::::::::::::
::: Page Layout :::
::::::::::::::::::::::::::::::::::::::::::::::: */
function get_layout() {
$sql = 'SELECT title, `table`, feed_count, b, n, w FROM layout WHERE user_id=?';
$param = array('s', $this->user_id);
return $this->db_op($sql, $param, 'read', '');
}
function post_layout($feed_count, $table, $b, $n, $w) {
$sql = 'UPDATE layout SET feed_count=?, `table`=?, b=?, n=?, w=? WHERE user_id=?';
$param = array('ssssss', $feed_count, $table, $b, $n, $w, $this->user_id);
return $this->db_op($sql, $param, 'write', '');
}
function post_layout2($title, $favicon_switch) {
$sql = 'UPDATE layout SET title=?, favicon_switch=? WHERE user_id=?';
$param = array('sss', $title, $favicon_switch, $this->user_id);
return $this->db_op($sql, $param, 'write', '');
}
/* :::::::::::::::::::::::::::::::::::::::::::::::
::: Notes :::
::::::::::::::::::::::::::::::::::::::::::::::: */
function get_notes() {
$sql = 'SELECT text FROM notes WHERE user_id=?';
$param = array('s', $this->user_id);
return $this->db_op($sql, $param, 'read', 'text');
}
function post_notes($text) {
$sql = 'UPDATE notes SET text=? WHERE user_id=?';
$param = array('ss', $text, $this->user_id);
return $this->db_op($sql, $param, 'write', '');
}
/* :::::::::::::::::::::::::::::::::::::::::::::::
::: Feeds :::
::::::::::::::::::::::::::::::::::::::::::::::: */
function get_feed_count($uid) {
$sql = 'SELECT feed_count FROM layout WHERE user_id=?';
$param = array('s', $uid);
return $this->db_op($sql, $param, 'read','feed_count');
}
function get_feed_id($uid, $set, $index) {
$sql = 'SELECT feed_id FROM feed_select WHERE user_id=? AND `set`=? AND`index`=?';
$param = array('sss', $uid, $set, $index);
return $this->db_op($sql, $param, 'read', 'feed_id');
}
function get_feed_para($id) {
$sql = 'SELECT name, description, url, website_url, img FROM feed WHERE id=?';
$param = array('s', $id);
return $this->db_op($sql, $param, 'read', '');
}
function get_feed_para_id($id) {
$sql = 'SELECT id, name, url, website_url, img, last_updated FROM feed WHERE id=?';
$param = array('s', $id);
return $this->db_op($sql, $param, 'read', '');
}
function get_feed_para_index($set, $index) {
$sql = 'SELECT id, name, url, website_url, img, last_updated FROM feed
INNER JOIN feed_select ON feed_select.feed_id = feed.id
WHERE feed_select.user_id=? AND feed_select.`set`=? AND feed_select.`index`=?';
$param = array('sss', $this->user_id, $set, $index);
return $this->db_op($sql, $param, 'read', '');
}
function get_feeds() {
$sql = 'SELECT id, name FROM feed
INNER JOIN feed_select ON feed.id = feed_select.feed_id
WHERE feed_select.user_id=? ORDER BY name';
$param = array('s', $this->user_id);
return $this->db_op($sql, $param, 'read_multi', '');
}
function get_all_feeds() {
$sql = 'SELECT id, name, description, img FROM feed ORDER BY name';
return $this->db_op($sql, '', 'read_multi', '');
}
function get_feed_cache($id) {
$sql = 'SELECT cache FROM feed WHERE id=?';
$param = array('s', $id);
return $this->db_op($sql, $param, 'read','cache');
}
function get_feed_status($id) {
$sql = 'SELECT user_id FROM feed_select WHERE feed_id =?';
$param = array('s', $id);
return $this->db_op($sql, $param, 'read_multi', '');
}
function post_feed_update($id, $name, $description, $url, $website_url, $img) {
$sql = 'UPDATE feed SET name=?, description=?, url=?, website_url=?, img=? WHERE id=?';
$param = array('ssssss', $name, $description, $url, $website_url, $img, $id);
return $this->db_op($sql, $param, 'write', '');
}
function post_feed($name, $description, $url, $website_url, $img) {
$sql = 'INSERT INTO feed (name, description, url, website_url, img) VALUES (?, ?, ?, ?, ?)';
$param = array('sssss', $name, $description, $url, $website_url, $img);
return $this->db_op($sql, $param, 'write', '');
}
function post_feed_select($set, $index, $feed_id) {
$sql = 'INSERT INTO feed_select (user_id, `set`, `index`, feed_id) VALUES (?, ?, ?, ?)
ON DUPLICATE KEY UPDATE feed_id=?';
$param = array('sssss', $this->user_id, $set, $index, $feed_id, $feed_id);
return $this->db_op($sql, $param, 'write', '');
}
function post_feed_count($feed_count) {
$sql = 'DELETE FROM feed_select WHERE user_id=? AND `index`>=?';
$param = array('ss', $this->user_id, $feed_count);
return $this->db_op($sql, $param, 'write', '');
}
function post_feed_cache($id, $cache, $timestamp) {
$sql = 'UPDATE feed SET cache=?, last_updated=? WHERE id=?';
$param = array('sss', $cache, $timestamp, $id);
return $this->db_op($sql, $param, 'write', '');
}
function post_feed_delete($id) {
$sql = 'DELETE FROM feed WHERE id=?';
$param = array('s', $id);
return $this->db_op($sql, $param, 'write', '');
}
/* :::::::::::::::::::::::::::::::::::::::::::::::
::: Bookmarks :::
::::::::::::::::::::::::::::::::::::::::::::::: */
function get_cats() {
$sql = 'SELECT id, name FROM bookmark_category WHERE user_id=?
ORDER BY name';
$param = array('s', $this->user_id);
return $this->db_op($sql, $param, 'read_multi', '');
}
function get_cat($id) {
$sql = 'SELECT bookmarks.id, bookmarks.name, bookmarks.url FROM bookmarks
INNER JOIN bookmark_category ON bookmarks.cat_id = bookmark_category.id
WHERE bookmarks.cat_id=? AND bookmark_category.user_id=?
ORDER BY bookmarks.name';
$param = array('ss', $id, $this->user_id);
return $this->db_op($sql, $param, 'read_multi', '');
}
function get_cat_pos($id) {
$sql = 'SELECT `position` FROM bookmark_category WHERE id=?';
$param = array('s', $id);
return $this->db_op($sql, $param, 'read', 'position');
}
function get_cat_own($id) {
$sql = 'SELECT user_id FROM bookmark_category WHERE id=?';
$param = array('s', $id);
return $this->db_op($sql, $param, 'read', 'user_id');
}
function get_bookmarks() {
$sql = 'SELECT bookmark_category.id, bookmark_category.`position`, bookmark_category.name, bookmarks.name, bookmarks.url, bookmarks.id, bookmarks.favicon
FROM bookmark_category
INNER JOIN bookmarks ON bookmark_category.id = bookmarks.cat_id
WHERE bookmark_category.user_id=?
ORDER BY bookmark_category.name, bookmark_category.id, bookmarks.name';
$param = array('s', $this->user_id);
return $this->db_op($sql, $param, 'read_multi', '');
}
function get_favicon_switch() {
$sql = 'SELECT favicon_switch FROM layout WHERE user_id=?';
$param = array('s', $this->user_id);
return $this->db_op($sql, $param, 'read', 'favicon_switch');
}
function get_bookmark_cache() {
$sql = 'SELECT bookmark_cache FROM layout WHERE user_id=?';
$param = array('s', $this->user_id);
return $this->db_op($sql, $param, 'read', 'bookmark_cache');
}
function post_favicon($id, $favicon) {
$sql = 'UPDATE bookmarks SET favicon=? WHERE id=?';
$param = array('ss', $favicon, $id);
return $this->db_op($sql, $param, 'write', '');
}
function post_cat_pos($id, $position) {
$sql = 'UPDATE bookmark_category SET position=? WHERE id=? AND user_id=?';
$param = array('sss', $position, $id, $this->user_id);
return $this->db_op($sql, $param, 'write', '');
}
function post_b_new($cat_id, $name, $url) {
$sql = 'INSERT INTO bookmarks (cat_id, name, url) VALUES (?, ?, ?)';
$param = array('sss', $cat_id, $name, $url);
return $this->db_op($sql, $param, 'write', '');
}
function post_b_del($id, $cat_id) {
$sql = 'DELETE FROM bookmarks WHERE id=? AND cat_id=?
AND EXISTS(SELECT * FROM bookmark_category WHERE bookmark_category.id=? AND bookmark_category.user_id=?)';
$param = array('ssss', $id, $cat_id, $cat_id, $this->user_id);
return $this->db_op($sql, $param, 'write', '');
}
function post_b_update($id, $name, $url) {
$sql = 'UPDATE bookmarks, bookmark_category SET bookmarks.name=?, bookmarks.url=?
WHERE bookmarks.cat_id = bookmark_category.id
AND bookmarks.id=? AND bookmark_category.user_id=?';
$param = array('ssss', $name, $url, $id, $this->user_id);
return $this->db_op($sql, $param, 'write', '');
}
function post_cat_new($name) {
$sql = 'INSERT INTO bookmark_category (user_id, `position`, name) VALUES (?, 0, ?)';
$param = array('ss', $this->user_id, $name);
return $this->db_op($sql, $param, 'write', '');
}
function post_cat_del($id) {
$sql = 'DELETE FROM bookmark_category WHERE id=? AND user_id=?';
$param = array('ss', $id, $this->user_id);
return $this->db_op($sql, $param, 'write', '');
}
function post_bookmark_cache($cache) {
$sql = 'UPDATE layout SET bookmark_cache=? WHERE user_id=?';
$param = array('ss', $cache, $this->user_id);
return $this->db_op($sql, $param, 'write', '');
}
/* :::::::::::::::::::::::::::::::::::::::::::::::
::: Weather :::
::::::::::::::::::::::::::::::::::::::::::::::: */
function get_weather_data() {
$sql = 'SELECT place, place_id, last_updated, forecast, forecast_h, weather_icons.name, weather_icons.img_format
FROM weather
INNER JOIN weather_icons ON weather.icons = weather_icons.id
WHERE user_id=?';
$param = array('s', $this->user_id);
return $this->db_op($sql, $param, 'read', '');
}
function get_weather_icons() {
$sql = 'SELECT id, name FROM weather_icons';
return $this->db_op($sql, '', 'read_multi', '');
}
function get_weather_icon($id) {
$sql = 'SELECT name, img_format, url FROM weather_icons WHERE id=?';
$param = array('s', $id);
return $this->db_op($sql, $param, 'read', '');
}
function get_weather_cache() {
$sql = 'SELECT cache FROM weather WHERE user_id=?';
$param = array('s', $this->user_id);
return $this->db_op($sql, $param, 'read', 'cache');
}
function post_weather_cache($cache, $timestamp) {
$sql = 'UPDATE weather SET cache=?, last_updated=? WHERE user_id=?';
$param = array('sss', $cache, $timestamp, $this->user_id);
return $this->db_op($sql, $param, 'write', 'weather');
}
function post_weather_data($place_id, $place, $forecast, $forecast_h, $icons) {
$sql = 'UPDATE weather SET place_id=?, place=?, forecast=?, forecast_h=?, icons=? WHERE user_id=?';
$param = array('ssssss', $place_id, $place, $forecast, $forecast_h, $icons, $this->user_id);
return $this->db_op($sql, $param, 'write', '');
}
/* :::::::::::::::::::::::::::::::::::::::::::::::
::: User Management :::
::::::::::::::::::::::::::::::::::::::::::::::: */
function get_uid($mail) {
$sql = 'SELECT id FROM `user` WHERE mail=?';
$param = array('s', $mail);
return $this->db_op($sql, $param, 'read', 'id');
}
function get_pwd($uid) {
$sql = 'SELECT pwd_hash FROM `user` WHERE id=?';
$param = array('s', $uid);
return $this->db_op($sql, $param, 'read', 'pwd_hash');
}
function get_mail() {
$sql = 'SELECT mail FROM `user` WHERE id=?';
$param = array('s', $this->user_id);
return $this->db_op($sql, $param, 'read', 'mail');
}
function get_verified($uid) {
$sql = 'SELECT verified FROM `user` WHERE id=?';
$param = array('s', $uid);
return $this->db_op($sql, $param, 'read', 'verified');
}
function get_token($uid) {
$sql = 'SELECT token FROM `user` WHERE id=?';
$param = array('s', $uid);
return $this->db_op($sql, $param, 'read', 'token');
}
function get_active_users() {
$sql = 'SELECT id FROM `user` WHERE last_visit > CURDATE()-2';
return $this->db_op($sql, '', 'read_multi', '');
}
function post_last_visit($version, $timestamp) {
$sql = 'UPDATE `user` SET version=?, last_visit=? WHERE id=?';
$param = array('sss', $version, $timestamp, $this->user_id);
return $this->db_op($sql, $param, 'write', '');
}
function post_pwd($uid, $pwd_hash) {
// write protection --> demo user
if ($uid === 1) {
return true;
}
$sql = 'UPDATE `user` SET pwd_hash=? WHERE id=?';
$param = array('ss', $pwd_hash, $uid);
return $this->db_op($sql, $param, 'write', '');
}
function post_verified($status, $uid) {
// write protection --> demo user
if ($uid === 1) {
return true;
}
$sql = 'UPDATE `user` SET verified=? WHERE id=?';
$param = array('ss', $status, $uid);
return $this->db_op($sql, $param, 'write', '');
}
function post_token($uid, $token) {
// write protection --> demo user
if ($uid === 1) {
return true;
}
$sql = 'UPDATE `user` SET token=? WHERE id=?';
$param = array('ss', $token, $uid);
return $this->db_op($sql, $param, 'write', '');
}
function post_mail($mail) {
$sql = 'UPDATE `user` SET mail=? WHERE id=?';
$param = array('ss', $mail, $this->user_id);
return $this->db_op($sql, $param, 'write', '');
}
function delete_user($uid) {
// write protection --> demo user
if ($uid === 1 || $uid === 2) {
return true;
}
$sql = 'DELETE FROM `user` WHERE id=?';
$param = array('s', $uid);
return $this->db_op($sql, $param, 'write', '');
}
function register_user($mail, $pwd_hash, $token) {
$sql = 'INSERT INTO `user` (mail, pwd_hash, token, verified) VALUES (?, ?, ?, ?)';
$param = array('ssss', $mail, $pwd_hash, $token, 0);
if ($this->db_op($sql, $param, 'write', '')) {
$uid = $this->get_uid($mail);
if (!$this->restore_defaults($uid)) {
$this->delete_user($uid);
return false;
}
else {
return true;
}
}
}
function restore_defaults($uid) {
// clone from demo-user (uid=1) over to $uid
$sql = 'CREATE TEMPORARY TABLE tmp SELECT * FROM bookmark_category WHERE user_id=1;
ALTER TABLE tmp drop id;
UPDATE tmp SET user_id=' . $uid . ' WHERE user_id=1;
INSERT INTO bookmark_category (user_id, `position`, name) SELECT user_id, `position`, name FROM tmp;
DROP TEMPORARY TABLE tmp;
CREATE TEMPORARY TABLE tmp SELECT bookmarks.* FROM bookmarks INNER JOIN bookmark_category ON bookmarks.cat_id = bookmark_category.id
WHERE bookmark_category.user_id=1 AND bookmark_category.name="Search";
ALTER TABLE tmp drop id;
UPDATE tmp SET cat_id= (SELECT id FROM bookmark_category WHERE bookmark_category.user_id=' . $uid . ' AND bookmark_category.name="Search");
INSERT INTO bookmarks (cat_id, name, url, favicon) SELECT * FROM tmp;
DROP TEMPORARY TABLE tmp;
CREATE TEMPORARY TABLE tmp SELECT bookmarks.* FROM bookmarks INNER JOIN bookmark_category ON bookmarks.cat_id = bookmark_category.id
WHERE bookmark_category.user_id=1 AND bookmark_category.name="Music";
ALTER TABLE tmp drop id;
UPDATE tmp SET cat_id= (SELECT id FROM bookmark_category WHERE bookmark_category.user_id=' . $uid . ' AND bookmark_category.name="Music");
INSERT INTO bookmarks (cat_id, name, url, favicon) SELECT * FROM tmp;
DROP TEMPORARY TABLE tmp;
CREATE TEMPORARY TABLE tmp SELECT bookmarks.* FROM bookmarks INNER JOIN bookmark_category ON bookmarks.cat_id = bookmark_category.id
WHERE bookmark_category.user_id=1 AND bookmark_category.name="Video";
ALTER TABLE tmp drop id;
UPDATE tmp SET cat_id= (SELECT id FROM bookmark_category WHERE bookmark_category.user_id=' . $uid . ' AND bookmark_category.name="Video");
INSERT INTO bookmarks (cat_id, name, url, favicon) SELECT * FROM tmp;
DROP TEMPORARY TABLE tmp;
CREATE TEMPORARY TABLE tmp SELECT * FROM feed_select WHERE user_id=1;
UPDATE tmp SET user_id=' . $uid . ' WHERE user_id=1;
INSERT INTO feed_select SELECT * FROM tmp;
DROP TEMPORARY TABLE tmp;
CREATE TEMPORARY TABLE tmp SELECT * FROM layout WHERE user_id=1;
UPDATE tmp SET user_id=' . $uid . ' WHERE user_id=1;
INSERT INTO layout SELECT * FROM tmp;
DROP TEMPORARY TABLE tmp;
CREATE TEMPORARY TABLE tmp SELECT * FROM notes WHERE user_id=1;
UPDATE tmp SET user_id=' . $uid . ' WHERE user_id=1;
INSERT INTO notes SELECT * FROM tmp;
DROP TEMPORARY TABLE tmp;
CREATE TEMPORARY TABLE tmp SELECT * FROM theme_select WHERE user_id=1;
UPDATE tmp SET user_id=' . $uid . ' WHERE user_id=1;
INSERT INTO theme_select SELECT * FROM tmp;
DROP TEMPORARY TABLE tmp;
CREATE TEMPORARY TABLE tmp SELECT * FROM weather WHERE user_id=1;
UPDATE tmp SET user_id=' . $uid . ' WHERE user_id=1;
INSERT INTO weather SELECT * FROM tmp;
DROP TEMPORARY TABLE tmp;';
if (!$this->db_connection->multi_query($sql)) {
error_log("[MYSQLI ERROR] Failed to create user: " . mysqli_error($this->db_connection));
return false;
} else {
return true;
}
}
}
?>