-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave.php
61 lines (51 loc) · 1.53 KB
/
save.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
<?php
require(__DIR__ . "/includes/config.php");
// ensure proper usage
if ( !isset($_POST["score"]) or !isset($_SESSION["id"]) or $_SERVER["REQUEST_METHOD"] != "POST")
{
http_response_code(400);
exit;
}
// authenticate sender
if ($_POST["code"] != CODE)
{
http_response_code(403);
exit;
}
// escape user's input
$score = urlencode($_POST["score"]);
$response["success"] = true;
// save only score
if ($_POST["mode"] == "score")
{
// save new score
$rows = query("UPDATE `users` SET score = ? WHERE id = ?", $score, $_SESSION["id"]);
}
// save score and sprite
elseif ($_POST["mode"] == "sprite")
{
// escape user's input
$sprite = urlencode($_POST["sprite"]);
$rows = query("UPDATE `users` SET score = ?, sourceid = ? WHERE id = ?", $score, $sprite, $_SESSION["id"]);
}
// save score, sprite and owned sprites
else
{
// escape user's input
$sprite = urlencode($_POST["sprite"]);
$owned = $_POST["owned"];
$rows = query("UPDATE `users` SET score = ?, sourceid = ? WHERE id = ?", $score, $sprite, $_SESSION["id"]);
if ($rows === false)
{
$response["success"] = false;
}
$rows = query("INSERT INTO `history` (userid, sourceid, permanent) VALUES (?, ?, 1)", $_SESSION["id"], $owned);
}
if ($rows === false)
{
$response["success"] = false;
}
// output repsonse as JSON (pretty-printed for debugging convenience)
header("Content-type: application/json");
print(json_encode($response, JSON_PRETTY_PRINT));
?>