-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.php
More file actions
454 lines (426 loc) · 15.5 KB
/
Database.php
File metadata and controls
454 lines (426 loc) · 15.5 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
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
<?php
class Database
{
public $configs = [];
public $redis = [];
public $PDO = [];
public $response = [
'ok' => 0
];
public $tables = [
'users',
'groups',
'channels'
];
public function __construct ($configs) {
$this->configs = $configs;
if ($this->configs['redis']['status']) {
$this->response = $this->redisConnect();
if (!$this->response['ok']) {
return $this->response;
}
}
if ($this->configs['database']['status']) {
$this->response = $this->dbConnect();
if (!$this->response['ok']) {
return $this->response;
}
}
return $this->response = ['ok' => 1];
}
# Redis Connection
public function redisConnect () {
if (class_exists('Redis')) {
try {
$this->redis = new Redis();
$this->redis->connect($this->configs['redis']['host'], $this->configs['redis']['port']);
if ($this->configs['redis']['password']) $this->redis->auth($this->configs['redis']['password']);
if ($this->configs['redis']['database']) $this->redis->select($this->configs['redis']['database']);
return ['ok' => 1];
} catch (Exception $e) {
return ['ok' => 0, 'error_code' => 500, 'description' => 'Redis Error: ' . $e->getMessage()];
}
} else {
return ['ok' => 0, 'error_code' => 500, 'description' => 'Internal Server Error: Redis class not exists'];
}
}
# Redis get
public function rget ($key) {
if ($this->redis) {
try {
return $this->redis->get($key);
} catch (Exception $e) {
}
}
return 0;
}
# Redis set
public function rset ($key, $value, $time = 0) {
if ($this->redis) {
try {
return $this->redis->set($key, $value, $time);
} catch (Exception $e) {
}
}
return 0;
}
# Redis del
public function rdel ($key) {
if ($this->redis) {
try {
return $this->redis->del($key);
} catch (Exception $e) {
}
}
return 0;
}
# Redis keys
public function rkeys ($key) {
if ($this->redis) {
try {
return $this->redis->keys($key);
} catch (Exception $e) {
}
}
return 0;
}
# Redis lpush
public function rladd ($key, $value) {
if ($this->redis) {
try {
return $this->redis->lPush($key, $value);
} catch (Exception $e) {
}
}
return 0;
}
# Redis lrange
public function rlget ($key, $offset = 0, $limit = 50) {
if ($this->redis) {
try {
return $this->redis->lRange($key, $offset, $limit);
} catch (Exception $e) {
}
}
return 0;
}
# Redis lrem
public function rldel ($key, $value = 0, $count = 50) {
if ($this->redis) {
try {
return $this->redis->lRem($key, $value, $count);
} catch (Exception $e) {
}
}
return 0;
}
# Database Connection
public function dbConnect () {
if (class_exists('PDO')) {
$this->configs['database']['type'] = strtolower($this->configs['database']['type']);
if ($this->configs['database']['type'] == 'sqlite') {
return $this->sqliteConnect();
} elseif ($this->configs['database']['type'] == 'mysql') {
return $this->mysqlConnect();
} elseif ($this->configs['database']['type'] == 'pgsql') {
return $this->pgsqlConnect();
} else {
return ['ok' => 0, 'error_code' => 500, 'description' => 'Internal Server Error: unknown database type'];
}
} else {
return ['ok' => 0, 'error_code' => 500, 'description' => 'Internal Server Error: PDO class not found'];
}
}
# PDO Connection with SQLite
private function sqliteConnect () {
try {
$this->PDO = new PDO('sqlite:' . $this->configs['database']['database_name']);
return ['ok' => 1];
} catch (PDOException $e) {
return ['ok' => 0, 'error_code' => 500, 'description' => 'PDO Error: ' . $e->getMessage()];
}
}
# PDO Connection with MySQL
private function mysqlConnect () {
try {
$this->PDO = new PDO('mysql:host=' . $this->configs['database']['host'] . ';dbname=' . $this->configs['database']['database_name'] . ';charset=utf8mb4', $this->configs['database']['user'], $this->configs['database']['password']);
return ['ok' => 1];
} catch (PDOException $e) {
return ['ok' => 0, 'error_code' => 500, 'description' => 'PDO Error: ' . $e->getMessage()];
}
}
# PDO Connection with PostgreSQL
private function pgsqlConnect () {
try {
$this->PDO = new PDO('pgsql:host=' . $this->configs['database']['host'] . ';dbname=' . strtolower($this->configs['database']['database_name']), strtolower($this->configs['database']['user']), $this->configs['database']['password']);
return ['ok' => 1];
} catch (PDOException $e) {
return ['ok' => 0, 'error_code' => 500, 'description' => 'PDO Error: ' . $e->getMessage()];
}
}
# General PDO custom query
public function query ($query, $args = 0, $return = 0) {
if (!is_string($query)) return ['error' => 'First parameter must be a string'];
if (!$this->PDO) return ['error' => 'PDO class not found'];
try {
$q = $this->PDO->prepare($query);
if ($err = $this->PDO->errorInfo() and $err[0] !== '00000') return ['error' => $err];
if (is_array($args) and !empty($args)) {
$q->execute($args);
} else {
$q->execute();
}
if ($err = $q->errorInfo() and $err[1] !== null) return ['error' => $err];
if (!$return) {
return 1;
} elseif ($return === 1) {
return $q->fetch(\PDO::FETCH_ASSOC);
} else {
return $q->fetchAll();
}
} catch (PDOException $e) {
return ['error' => 'PDO Exception: ' . $e];
}
}
# Create LIMIT with OFFSET
public function limit ($limit = 'ALL', $offset = 0) {
if ($this->configs['database']['type'] == 'sqlite') {
return 'LIMIT ' . $limit . ' OFFSET ' . round($offset);
} elseif ($this->configs['database']['type'] == 'mysql') {
return 'LIMIT ' . round($offset) . ', ' . $limit;
} elseif ($this->configs['database']['type'] == 'pgsql') {
return 'LIMIT ' . $limit . ' OFFSET ' . round($offset);
}
return;
}
# Tables Setup for NeleBot X
public function setup() {
$tables = [];
foreach ($this->tables as $table) {
$tables[$table] = $this->createTemplateTable($table);
}
return $tables;
}
# Templates of users, groups and channels table
public function createTemplateTable ($table) {
if ($table == 'users') {
return $this->query('CREATE TABLE IF NOT EXISTS users (
id BIGINT PRIMARY KEY,
name VARCHAR(64) NOT NULL,
surname VARCHAR(64),
username VARCHAR(32),
lang VARCHAR(16) NOT NULL DEFAULT \'en\',
settings TEXT NOT NULL DEFAULT \'{}\',
registration INT NOT NULL,
last_seen INT NOT NULL,
ban INT NOT NULL DEFAULT \'0\',
status VARCHAR(64) NOT NULL DEFAULT \'Unknown\'
);');
} elseif ($table == 'groups') {
return $this->query('CREATE TABLE IF NOT EXISTS groups (
id BIGINT PRIMARY KEY,
title VARCHAR(64) NOT NULL,
description VARCHAR(256),
username VARCHAR(32),
admins TEXT NOT NULL DEFAULT \'{}\',
permissions TEXT NOT NULL DEFAULT \'{}\',
settings TEXT NOT NULL DEFAULT \'{}\',
registration INT NOT NULL,
last_seen INT NOT NULL,
ban INT NOT NULL DEFAULT \'0\',
status VARCHAR(64) NOT NULL DEFAULT \'Unknown\'
);');
} elseif ($table == 'channels') {
return $this->query('CREATE TABLE IF NOT EXISTS channels (
id BIGINT PRIMARY KEY,
title VARCHAR(64) NOT NULL,
description VARCHAR(256),
username VARCHAR(32),
admins TEXT NOT NULL DEFAULT \'{}\',
settings TEXT NOT NULL DEFAULT \'{}\',
registration INT NOT NULL,
last_seen INT NOT NULL,
ban INT NOT NULL DEFAULT \'0\',
status VARCHAR(64) NOT NULL DEFAULT \'Unknown\'
);');
}
return ['error' => 'Table template not found'];
}
# User on Database
public function getUser ($user) {
if (!is_array($user)) return ['error' => 'Bad Request: User must be an array'];
if (isset($user['id']) or isset($user['username'])) {
if (isset($user['id'])) {
if ($user['id'] <= 0 or $user['id'] >= 18446744073709551615) return ['error' => 'Bad Request: id is out of range'];
$q = $this->query('SELECT * FROM users WHERE id = ?', [round($user['id'])], 1);
} else {
if (strlen($user['username']) > 32) return ['error' => 'Bad Request: username has too many characters'];
$q = $this->query('SELECT * FROM users WHERE LOWER(username) = LOWER(?)', [$user['username']], 1);
}
if (!isset($q['id']) and $user['first_name']) {
if (!isset($user['language_code'])) $user['language_code'] = 'en';
$args = [
$user['id'],
$user['first_name'],
$user['last_name'],
$user['username'],
$user['language_code'],
time(),
time()
];
$i = $this->query('INSERT INTO users (id, name, surname, username, lang, registration, last_seen) VALUES (?,?,?,?,?,?,?)', $args);
$q = $this->query('SELECT * FROM users WHERE id = ?', [$user['id']], 1);
if (!isset($q['id'])) {
return ['error' => 'Error to load user info', 'INSERT' => $i];
}
} elseif (isset($user['first_name']) and (($user['first_name'] !== $q['name'] or $user['last_name'] !== $q['surname'] or $user['username'] !== $q['username']) or $q['last_seen'] <= (time() - 60 * 60))) {
$q['name'] = $user['first_name'];
$q['surname'] = $user['last_name'];
$q['username'] = $user['username'];
$q['last_seen'] = time();
$this->query('UPDATE users SET name = ?, surname = ?, username = ?, last_seen = ? WHERE id = ?', [$user['first_name'], $user['last_name'], $user['username'], $q['last_seen'], $user['id']]);
}
// Use this json_decode only if you need it!
/*$q['settings'] = json_decode($q['settings'], 1);
if (!is_array($q['settings'])) $q['settings'] = [];*/
return $q;
} else {
return ['error' => 'There is no id or username'];
}
}
# Group on Database
public function getGroup ($chat) {
if (!is_array($chat)) return ['error' => 'Bad Request: Chat must be an array'];
if (isset($chat['id']) or isset($chat['username'])) {
if (isset($chat['id'])) {
if ($chat['id'] >= 0) return ['error' => 'Bad Request: id is out of range'];
$q = $this->query('SELECT * FROM groups WHERE id = ?', [round($chat['id'])], 1);
} else {
if (strlen($chat['username']) > 32) return ['error' => 'Bad Request: username has too many characters'];
$q = $this->query('SELECT * FROM groups WHERE LOWER(username) = LOWER(?)', [$chat['username']], 1);
}
if (!isset($q['id']) and $chat['title']) {
$args = [
$chat['id'],
$chat['title'],
$chat['username'],
time(),
time()
];
$i = $this->query('INSERT INTO groups (id, title, username, registration, last_seen) VALUES (?,?,?,?,?)', $args);
$q = $this->query('SELECT * FROM groups WHERE id = ?', [$chat['id']], 1);
if (!isset($q['id'])) {
return ['error' => 'Error to load chat info', 'INSERT' => $i];
}
} elseif (isset($chat['title']) and ($user['title'] !== $q['title'] or $q['last_seen'] <= (time() - 60 * 60))) {
$q['title'] = $chat['title'];
$q['last_seen'] = time();
$this->query('UPDATE groups SET title = ?, last_seen = ? WHERE id = ?', [$chat['title'], $q['last_seen'], $chat['id']]);
}
// Use this json_decode only if you need it! else comment these 2 strings
$q['settings'] = json_decode($q['settings'], 1);
if (!is_array($q['settings'])) $q['settings'] = [];
$q['permissions'] = json_decode($q['permissions'], 1);
if (!is_array($q['permissions'])) $q['permissions'] = [];
$q['admins'] = json_decode($q['admins'], 1);
if (!is_array($q['admins'])) $q['admins'] = [];
return $q;
} else {
return ['error' => 'There is no id or username'];
}
}
# Channel on Database
public function getChannel ($chat) {
if (!is_array($chat)) return ['error' => 'Bad Request: Chat must be an array'];
if (isset($chat['id']) or isset($chat['username'])) {
if (isset($chat['id'])) {
if ($chat['id'] >= 0) return ['error' => 'Bad Request: id is out of range'];
$q = $this->query('SELECT * FROM channels WHERE id = ?', [round($chat['id'])], 1);
} else {
if (strlen($chat['username']) > 32) return ['error' => 'Bad Request: username has too many characters'];
$q = $this->query('SELECT * FROM channels WHERE LOWER(username) = LOWER(?)', [$chat['username']], 1);
}
if (!isset($q['id']) and $chat['title']) {
$args = [
$chat['id'],
$chat['title'],
$chat['username'],
time(),
time()
];
$i = $this->query('INSERT INTO channels (id, title, username, registration, last_seen) VALUES (?,?,?,?,?)', $args);
$q = $this->query('SELECT * FROM channels WHERE id = ?', [$chat['id']], 1);
if (!isset($q['id'])) {
return ['error' => 'Error to load chat info', 'INSERT' => $i];
}
} elseif (isset($chat['title']) and ($user['title'] !== $q['title'] or $q['last_seen'] <= (time() - 60 * 60))) {
$q['title'] = $chat['title'];
$q['last_seen'] = time();
$this->query('UPDATE channels SET title = ?, last_seen = ? WHERE id = ?', [$chat['title'], $q['last_seen'], $chat['id']]);
}
// Use this json_decode only if you need it! else comment these 2 strings
$q['settings'] = json_decode($q['settings'], 1);
if (!is_array($q['settings'])) $q['settings'] = [];
$q['admins'] = json_decode($q['admins'], 1);
if (!is_array($q['admins'])) $q['admins'] = [];
return $q;
} else {
return ['error' => 'There is no id or username'];
}
}
# Get Chats by Admin (PostgreSQL only)
public function getChatsByAdmin ($user_id, $limit = 20) {
if (!is_numeric($user_id)) return [];
$chats = [];
$groups = $this->query('SELECT * FROM groups WHERE admins @> \'[{"user":{"id": ' . $user_id . '}}]\' ' . $this->limit(round($limit / 2)), [], 2);
if (isset($groups[0]['id'])) {
$chats = array_merge($chats, $groups);
}
$channels = $this->query('SELECT * FROM channels WHERE admins @> \'[{"user":{"id": ' . $user_id . '}}]\' ' . $this->limit(round($limit / 2)), [], 2);
if (isset($channels[0]['id'])) {
$chats = array_merge($chats, $channels);
}
return $chats;
}
# Ban by id
public function ban ($id) {
return [
$this->query('UPDATE users SET ban = ? WHERE id = ?', [1, $id]),
$this->query('UPDATE groups SET ban = ? WHERE id = ?', [1, $id]),
$this->query('UPDATE channels SET ban = ? WHERE id = ?', [1, $id])
];
}
# UnBan by id
public function unban ($id) {
return [
$this->query('UPDATE users SET ban = ? WHERE id = ?', [0, $id], 1),
$this->query('UPDATE groups SET ban = ? WHERE id = ?', [0, $id], 1),
$this->query('UPDATE channels SET ban = ? WHERE id = ?', [0, $id], 1)
];
}
# Get User ban by user_id
public function isBanned ($id) {
$q = $this->query('SELECT ban FROM users WHERE id = ?', [$id], 1);
if ($q['ban']) return 1;
$q = $this->query('SELECT ban FROM groups WHERE id = ?', [$id], 1);
if ($q['ban']) return 1;
$q = $this->query('SELECT ban FROM channels WHERE id = ?', [$id], 1);
if ($q['ban']) return 1;
return 0;
}
# Get User language by user_id
public function getLanguage ($id) {
if (is_numeric($id) and isset($id) and $id > 0 and $id < 18446744073709551615) {
$q = $this->query('SELECT lang FROM users WHERE id = ? LIMIT 1', [round($id)], 1);
if (isset($q['lang'])) {
return $q['lang'];
}
}
return 'en';
}
# Set user Telegram status
public function setStatus ($id, $status = 'Unknown', $table = 0) {
return $this->query('UPDATE ' . $this->tables[$table] . ' SET status = ? WHERE id = ?', [$status, $id]);
}
}
?>