forked from SSilence/sum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.php
94 lines (80 loc) · 3 KB
/
backend.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
<?PHP
// checks whether user exists or not
function exist($db, $user) {
$sth = $db->prepare('SELECT count(*) FROM user WHERE user=:user');
$sth->bindParam(':user', $user, PDO::PARAM_STR);
$sth->execute();
$count = $sth->fetchColumn();
return $count != 0;
}
// open database connection
$db = new PDO('sqlite:userlist.db');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// is user table available?
$result = $db->query("SELECT name FROM sqlite_master WHERE type = 'table'");
$found = false;
foreach($result as $row)
if ($row['name']== 'user')
$found = true;
// create table user if not available
if ($found === false)
$db->exec("CREATE TABLE user (user TEXT, pulse TEXT, detail TEXT);");
// update pulse user information (timestamp, rooms)
if(isset($_POST["user"]) && isset($_POST["pulse"]) && strlen(trim($_POST["user"])) > 0 && strlen(trim($_POST["pulse"])) > 0) {
if (strlen($_POST["pulse"]) > 3145728) {
header("HTTP/1.0 400 BAD REQUEST");
die("pulse bigger than 3MB");
}
if (exist($db, $_POST["user"])) {
$sth = $db->prepare('UPDATE user SET pulse=:pulse WHERE user=:user');
} else {
$sth = $db->prepare('INSERT INTO user (user, pulse) VALUES (:user, :pulse)');
}
$sth->bindParam(':user', $_POST["user"], PDO::PARAM_STR);
$sth->bindParam(':pulse', $_POST["pulse"], PDO::PARAM_STR);
$sth->execute();
return;
}
// update detail user information (avatar, key, ip, port)
if(isset($_POST["user"]) && isset($_POST["detail"]) && strlen(trim($_POST["user"])) > 0 && strlen(trim($_POST["detail"]))> 0) {
if (strlen($_POST["detail"]) > 3145728) {
header("HTTP/1.0 400 BAD REQUEST");
die("detail bigger than 3MB");
}
if (exist($db, $_POST["user"])) {
$sth = $db->prepare('UPDATE user SET detail=:detail WHERE user=:user');
} else {
$sth = $db->prepare('INSERT INTO user (user, detail) VALUES (:user, :detail)');
}
$sth->bindParam(':user', $_POST["user"], PDO::PARAM_STR);
$sth->bindParam(':detail', $_POST["detail"], PDO::PARAM_STR);
$sth->execute();
return;
}
// delete user
if(isset($_POST["user"]) && isset($_POST["delete"])) {
$sth = $db->prepare('DELETE FROM user WHERE user=:user');
$sth->bindParam(':user', $_POST["user"], PDO::PARAM_STR);
$sth->execute();
return;
}
// get detail user information (avatar, key, ip, port)
if(isset($_GET["user"]) && strlen(trim($_GET["user"]))) {
$sth = $db->prepare("SELECT user, pulse, detail FROM user WHERE user = :user");
$sth->bindParam(':user', $_GET["user"], PDO::PARAM_STR);
$sth->execute();
while($row = $sth->fetch()) {
echo $row['detail'];
return;
}
header("HTTP/1.0 404 NOT FOUND");
return;
}
// otherwise send pulse user information
$sth = $db->prepare("SELECT user, pulse FROM user");
$sth->execute();
$users = array();
while($row = $sth->fetch())
$users[] = $row['pulse'];
header('Content-type: application/json');
die(json_encode($users));