-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.php
161 lines (128 loc) · 3.8 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
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
<?php
require_once "config.php";
try {
$db = new PDO("sqlite:" . DB_PATH);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Database connection failed: " . $e->getMessage();
exit;
}
/////////////
// HELPERS //
/////////////
function query($query) {
global $db;
try {
$result = $db->query($query);
} catch (PDOException $e) {
echo "Database query failed: " . $e->getMessage();
exit;
}
return $result;
}
function escape($value) {
global $db;
return $db->quote($value);
}
//////////
// AUTH //
//////////
function generateSessionUID() {
// throw some entropy in there
return md5(PASSWORD . microtime() . $_SERVER["HTTP_USER_AGENT"]);
}
function passwordValid($pass) {
return $pass == PASSWORD;
}
function startSession() {
$uid = generateSessionUID();
$expires = time() + (86400 * SESSION_LENGTH);
$useragent = escape($_SERVER["HTTP_USER_AGENT"]); // helps discern sessions when going through them manually, if need be
query("INSERT INTO session (uid, expires, useragent) VALUES (" . escape($uid) . ", $expires, $useragent)");
setcookie("sessionuid", $uid, $expires, "/");
}
function killSession() {
if (isset($_COOKIE["sessionuid"])) {
$uid = escape($_COOKIE["sessionuid"]);
query("DELETE FROM session WHERE uid = $uid");
}
}
function sessionValid() {
if (isset($_COOKIE["sessionuid"])) {
$uid = escape($_COOKIE["sessionuid"]);
$query = query("SELECT expires FROM session WHERE uid = $uid");
$result = $query->fetch();
$expires = $result["expires"];
return !empty($expires) && $expires >= time();
}
return false;
}
function vacuumExpiredSessions() {
query("DELETE FROM session WHERE expires < " . time());
}
/////////
// ADD //
/////////
function addWeight($weight) {
// allow new weight entry without a dot if within 10 kg of most recent weight
$result = query("SELECT * FROM weight WHERE id = (SELECT MAX(id) FROM weight)");
$weights = $result->fetch();
if (abs($weight - 10 * $weights["weight"]) < 100) {
$weight = $weight / 10;
}
$time = time();
$escapedWeight = escape($weight);
query("INSERT INTO weight (time, weight) VALUES ($time, $escapedWeight)");
return $weight;
}
function removeMostRecentWeight() {
query("DELETE FROM weight WHERE id = (SELECT MAX(id) FROM weight)");
}
//////////
// MISC //
//////////
function getMostRecentWeight() {
$result = query("SELECT * FROM weight WHERE id = (SELECT MAX(id) FROM weight)");
$weights = $result->fetch();
return $weights["weight"];
}
function getWeights($start = 0) {
$result = query("SELECT * FROM weight WHERE time >= $start ORDER BY id ASC");
$weights = $result->fetchAll();
return $weights;
}
///////////
// CHART //
///////////
function formatWeights($weights) {
$formatted = "[";
foreach ($weights as $weight) {
$date = date("Y-m-d\TH:i:s", $weight["time"]);
$weight = $weight["weight"];
$formatted .= "{x: new Date('$date'), y: $weight},";
}
$formatted .= "]";
return $formatted;
}
function getChartRange($weights, $steps = 25) {
$min = $weights[0]["weight"];
$max = $weights[0]["weight"];
foreach ($weights as $weight) {
if ($weight["weight"] < $min) {
$min = $weight["weight"];
} else if ($weight["weight"] > $max) {
$max = $weight["weight"];
}
}
$startValue = 2.5 * floor($min / 2.5);
$stepWidth = (2.5 * ceil($max / 2.5) - $startValue) / $steps;
return array("steps" => $steps, "stepWidth" => $stepWidth, "startValue" => $startValue);
}
/////////////////
// DANGER ZONE //
/////////////////
function resetDatabase() {
query("DROP TABLE IF EXISTS weight");
query("CREATE TABLE weight (id INTEGER NOT NULL, time INTEGER, weight DECIMAL(15,1), PRIMARY KEY (id))");
query("CREATE TABLE session (uid varchar NOT NULL, expires INTEGER, useragent TEXT, PRIMARY KEY (uid))");
}