Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
titaniumnetwork-dev
GitHub Repository: titaniumnetwork-dev/Incognito-old
Path: blob/main/static/src/gs/public/pacman/data/db-handler.php
1324 views
1
<?php header('Content-Type: application/json');
2
3
/* IMPORTANT:
4
* change this to the main url of where you host the application, otherwise, every entry will be marked as a cheater
5
*/
6
$hostdomain = 'pacman.platzh1rsch.ch';
7
8
if (isset($_POST['action'])) {
9
switch ($_POST['action']) {
10
case 'get':
11
if(isset($_POST['page'])) {
12
echo getHighscore($_POST['page']);
13
} else {
14
echo getHighscore();
15
}
16
break;
17
case 'add':
18
if(isset($_POST['name']) || isset($_POST['score']) || isset($_POST['level']))
19
echo addHighscore($_POST['name'],$_POST['score'], $_POST['level']);
20
break;
21
case 'reset':
22
echo resetHighscore();
23
break;
24
}
25
} else if (isset($_GET['action'])) {
26
if ($_GET['action'] == 'get') {
27
if(isset($_GET['page'])) {
28
echo getHighscore($_GET['page']);
29
} else {
30
echo getHighscore();
31
}
32
}
33
} else echo "define action to call";
34
35
36
function getHighscore($page = 1) {
37
38
$db = new SQLite3('pacman.db');
39
createDataBase($db);
40
$results = $db->query('SELECT name, score FROM highscore WHERE cheater = 0 AND name != "" ORDER BY score DESC LIMIT 10 OFFSET ' . ($page-1)*10);
41
while ($row = $results->fetchArray()) {
42
$tmp["name"] = htmlspecialchars($row['name']);
43
$tmp["score"] = strval($row['score']);
44
$response[] = $tmp;
45
}
46
if (!isset($response) || is_null($response)) {
47
return "[]";
48
} else {
49
return json_encode($response);
50
}
51
}
52
53
function addHighscore($name, $score, $level) {
54
55
$db = new SQLite3('pacman.db');
56
$date = date('Y-m-d h:i:s', time());
57
createDataBase($db);
58
$ref = isset($_SERVER[ 'HTTP_REFERER']) ? $_SERVER[ 'HTTP_REFERER'] : "";
59
$ua = isset($_SERVER[ 'HTTP_USER_AGENT']) ? $_SERVER[ 'HTTP_USER_AGENT'] : "";
60
$remA = isset($_SERVER[ 'REMOTE_ADDR']) ? $_SERVER[ 'REMOTE_ADDR'] : "";
61
$remH = isset($_SERVER[ 'REMOTE_HOST']) ? $_SERVER[ 'REMOTE_HOST'] : "";
62
63
// some simple checks to avoid cheaters
64
$ref_assert = preg_match('/http(s)?:\/\/.*' . $hostdomain . '/', $ref) > 0;
65
$ua_assert = ($ua != "");
66
$cheater = 0;
67
if (!$ref_assert || !$ua_assert) {
68
$cheater = 1;
69
}
70
71
$maxlvlpoints_pills = 104 * 10;
72
$maxlvlpoints_powerpills = 4 * 50;
73
$maxlvlpoints_ghosts = 4 * 4 * 100;
74
// check if score is even possible
75
if ($level < 1) {
76
$cheater = 1;
77
} else if (($score / $level) > (1600 + 1240)) {
78
$cheater = 1;
79
}
80
81
$name_clean = htmlspecialchars($name);
82
$score_clean = htmlspecialchars($score);
83
84
$db->exec('INSERT INTO highscore (name, score, level, date, log_referer, log_user_agent, log_remote_addr, log_remote_host, cheater) '
85
. 'VALUES ("'
86
. $name . '", '
87
. $score . ', '
88
. $level . ', "'
89
. $date . '", "'
90
. $ref .'", "'
91
. $ua . '", "'
92
. $remA .'", "'
93
. $remH . '", "'
94
. $cheater
95
.'")'
96
);
97
98
$response['status'] = "success";
99
$response['level'] = $level;
100
$response['name'] = $name;
101
$response['score'] = $score;
102
$response['cheater'] = $cheater;
103
return json_encode($response);
104
}
105
106
function resetHighscore() {
107
$db = new SQLite3('pacman.db');
108
$date = date('Y-m-d h:i:s', time());
109
$db->exec('DROP TABLE IF EXISTS highscore');
110
createDataBase($db);
111
}
112
113
function createDataBase($db) {
114
$db->exec('CREATE TABLE IF NOT EXISTS highscore(name VARCHAR(60),score INT, level INT, date DATETIME, log_referer VARCHAR(200), log_user_agent VARCHAR(200), log_remote_addr VARCHAR(200), log_remote_host VARCHAR(200), cheater BOOLEAN)');
115
}
116
117
?>
118
119