-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathajax.php
More file actions
76 lines (66 loc) · 2.2 KB
/
ajax.php
File metadata and controls
76 lines (66 loc) · 2.2 KB
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
<?php
header("Content-Type: application/json");
function sanitize($str)
{
$str = trim($str);
$str = str_replace(";", "", $str);
$str = filter_var($str, FILTER_SANITIZE_STRING);
if (strlen($str) > 10)
$str = substr($str, 0, 10);
return $str;
}
if (isset($_GET["action"]))
{
$action = $_GET["action"];
switch($action)
{
case "getTopScores":
try
{
$strTopScores = file_get_contents("top_scores.txt");
$lines = explode("\n", $strTopScores);
$jsonScores = array();
foreach ($lines as $line)
{
if (strpos($line, ';') == false)
continue;
$fields = explode(";", $line);
$name = sanitize($fields[0]);
$score = sanitize($fields[1]);
if (strlen($name) == 0 || strlen($score) == 0 || !is_numeric($score))
continue;
array_push($jsonScores, "{ \"name\": \"" . $name . "\", \"score\": " . $score . " }");
}
echo "[" . implode(",", $jsonScores) . "]";
}
catch(Exception $e)
{
echo "[]";
}
break;
case "saveTopScores":
try
{
if (isset($_POST["topScores"]) && strlen($_POST["topScores"]) < 1000)
{
$jsonTopScores = json_decode($_POST["topScores"]);
$fw = fopen("top_scores.txt", "w+");
foreach ($jsonTopScores as $jsonTopScore)
{
$name = sanitize($jsonTopScore->name);
$score = sanitize($jsonTopScore->score);
if (strlen($name) > 0 && strlen($score) > 0 && is_numeric($score))
fwrite($fw, $name . ";" . $score . "\n");
}
fclose($fw);
echo "{\"success\": true}";
}
}
catch (Exception $e)
{
echo "{\"success\": false}";
}
break;
}
}
?>