This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
db.php
320 lines (283 loc) · 8.49 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
<?php
require_once 'res/medoo/Medoo.php';
use Medoo\Medoo;
require_once 'config/config.db.php';
require_once 'config/config.features.php';
require_once 'poll.model.php';
class DB {
private $db = null;
function __construct() {
try {
$this->db = new medoo([
'database_type' => 'mysql',
'database_name' => SPR_DB_NAME,
'server' => SPR_DB_SERVER,
'username' => SPR_DB_USERNAME,
'password' => SPR_DB_PASSWORD,
'charset' => 'utf8'
]);
} catch (Exception $ex) { die($ex->getMessage()); }
}
function __destruct() {
if ( $this->db !== null ) { $this->db = null; }
}
function getPoll($id){
$db = $this->db;
//get poll data from db
$pollData = $db->get("polls", "*", ["pollId" => $id]);
//return false if poll with $id not found
if (!$pollData) return false;
//create poll model instance
$poll = new Poll(
$pollData["pollId"],
$pollData["pollAdminId"],
$pollData["title"],
$pollData["details"],
$pollData["changed"]
);
//populate entries, dates and comments
$poll->setEntries($db->select("entries", "*", ["pollId" => $id]));
$poll->setComments($db->select("comments", "*", ["pollId" => $id]));
$poll->setDates($db->select("dates", "*", ["pollId" => $id]));
return $poll;
}
function getPollsOverviewData(){
return $this->db->select("polls", ["pollId", "pollAdminId", "title"]);
}
function createPoll($poll){
$db = $this->db;
//// save poll data to polls table
$db->action(function($db) use ($poll) {
$db->insert("polls", [
"pollId" => $poll->getId(),
"pollAdminId" => $poll->getAdminId(),
"title" => $poll->getTitle(),
"details" => $poll->getDetails(),
"changed" => $this->getDate()
]);
});
//// save dates data to dates table
$db->action(function($db) use ($poll) {
$db->insert("dates", $poll->getDates());
});
}
function deletePoll($pollId){
$db = $this->db;
$db->action(function($db) use ($pollId) {
//delete from polls table
$db->delete("polls", ["pollId" => $pollId]);
//delete from entries table
$db->delete("entries", ["pollId" => $pollId]);
//delete from dates table
$db->delete("dates", ["pollId" => $pollId]);
//delete from comments table
$db->delete("comments", ["pollId" => $pollId]);
});
//removed poll?
if ($db->has("polls", ["pollId" => $pollId])){
echo "ERROR: Something went wrong..." . PHP_EOL;
return false;
} else {
return true;
}
}
function transformPollDates($pollId, $datesRaw){
$datesRaw = array_unique(array_values($datesRaw));
$datesPrepared = array();
for ($i = 0; $i < sizeof($datesRaw); $i++) {
array_push(
$datesPrepared,
array(
"pollId" => $pollId,
"date" => trim($datesRaw[$i]),
"sort" => $i
)
);
}
return $datesPrepared;
}
function saveEntry($pollId, $name, $dates, $values){
$name = trim($name);
if (strlen($name) == 0) return;
$db = $this->db;
//prepare distinct entries
$entries = array();
for ($i = 0; $i < sizeof($dates); $i++) {
array_push($entries, array("pollId" => $pollId, "date" => $dates[$i], "name" => $name, "value"=> $values[$i]));
}
//if no dates are checked, insert dummy data
if (sizeof($entries) == 0){
$dummy = substr(hash("md4",time()), 0, 16);
array_push($entries, array("pollId" => $pollId, "date" => $dummy, "name" => $name));
}
return $db->action(function($db) use ($pollId, $entries) {
//write data to entries table
$db->insert("entries", $entries);
//update change date in polls table
$db->update("polls", array("changed" => $this->getDate()), array("pollId" => $pollId));
});
}
function deleteEntry($pollId, $name){
$db = $this->db;
$db->action(function($db) use ($pollId, $name) {
$db->delete("entries", [
"AND" => [
"pollId" => $pollId,
"name" => $name
]
]);
});
}
function deleteDate($pollId, $date){
$db = $this->db;
$db->action(function($db) use ($pollId, $date) {
$db->delete("dates", [
"AND" => [
"pollId" => $pollId,
"date" => $date
]
]);
$db->delete("entries", [
"AND" => [
"pollId" => $pollId,
"date" => $date
]
]);
});
}
function saveComment($comment){
$db = $this->db;
$comment["date"] = $this->getDateTime();
$db->action(function($db) use ($comment) {
//write data to comments table
$db->insert("comments", $comment);
//update change date in polls table
$db->update("polls", array("changed" => $this->getDate()), array("pollId" => $comment["pollId"]));
});
}
function getDate(){
return date("Y-m-d");
}
function getDateTime(){
return date("Y-m-d H:i:s");
}
function getAdminId($pollId){
return $this->db->get("polls", "pollAdminId", ["pollId" => $pollId]);
}
function antiSpam($ip){
//is anti-spam enabled at all?
if (!SPR_ANTISPAM) return true;
$db = $this->db; // db object ref
$pass = true; // action allowed
$ipHash = hash('md4', $ip); // create ip hash
$time = time(); // create timestamp
$record; // ip hash record
//// check if ip hash is known
if ($db->has("antispam", ["ipHash" => $ipHash])){
//// KNOWN
$record = $db->get("antispam", "*", ["ipHash" => $ipHash]);
// is ip already blocked?
if ($record["blocked"]){
// still within blocktime?
if ($time - $record["blocktime"] < SPR_ANTISPAM_BLOCKTIME){
//just keep blocking
return false;
} else {
// enough blocking, RESET record
$record["counter"] = 1;
$record["time"] = $time;
$record["blocked"] = false;
$record["blocktime"] = 0;
}
} else if ($time - $record["time"] <= SPR_ANTISPAM_SECONDS){
// last action was not long enough ago :(
// within set seconds
$record["counter"]++; // increment counter
if ($record["counter"] > SPR_ANTISPAM_ACTIONS){
$pass = false; // action not allowed, spam rules hit
$record["blocked"] = true;
$record["blocktime"] = $time;
}
} else {
// longer ago, RESET record
$record["counter"] = 1;
$record["time"] = $time;
}
// update record in db
$db->action(function($db) use ($record, $ipHash) {
$db->update("antispam", $record, ["ipHash" => $ipHash]);
});
} else {
//// UNKNOWN
$record = [
"ipHash" => $ipHash,
"time" => $time,
"counter" => 1,
"blocked" => false,
"blocktime" => 0
];
//write new record to db
$db->action(function($db) use ($record) {
$db->insert("antispam", $record);
});
}
// let the user pass or don't.
return $pass;
}
function install(){
$db = $this->db;
$success = true;
// create table "polls"
$query = "CREATE TABLE `polls` (
`pollId` varchar(32) NOT NULL,
`pollAdminId` varchar(32) NOT NULL,
`title` varchar(256) NOT NULL,
`details` varchar(512) NOT NULL,
`changed` date NOT NULL,
PRIMARY KEY (`pollId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8";
$success = $db->query($query) ? $success : false;
// create table "entries"
$query = "CREATE TABLE `entries` (
`pollId` varchar(32) NOT NULL,
`date` varchar(32) NOT NULL,
`name` varchar(32) NOT NULL,
`value` tinyint(4) NOT NULL,
KEY `pollId` (`pollId`,`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8";
$success = $db->query($query) ? $success : false;
// create table "dates"
$query = "CREATE TABLE `dates` (
`pollId` varchar(32) NOT NULL,
`date` varchar(32) NOT NULL,
`sort` tinyint(4) NOT NULL,
KEY `pollId` (`pollId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8";
$success = $db->query($query) ? $success : false;
// create table "comments"
$query = "CREATE TABLE `comments` (
`pollId` varchar(32) NOT NULL,
`text` varchar(512) NOT NULL,
`name` varchar(32) NOT NULL,
`date` varchar(32) NOT NULL,
KEY `pollId` (`pollId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8";
$success = $db->query($query) ? $success : false;
// create table "antispam"
$query = "CREATE TABLE `antispam` (
`ipHash` varchar(32) NOT NULL,
`time` bigint NOT NULL,
`counter` tinyint(4) NOT NULL,
`blocked` boolean,
`blocktime` bigint NOT NULL,
KEY `ipHash` (`ipHash`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8";
$success = $db->query($query) ? $success : false;
// return true if everything went schmuhfli
return $success;
}
function getPollsInactiveSince($date){
return $this->db->select("polls", "pollId", ["changed[<]" => $date]);
}
}
?>