-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_pixel.php
163 lines (127 loc) · 4.53 KB
/
write_pixel.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
<?php
include('./private/config.php');
// Function to verify if the IP is allowed to write a pixel, data stored in an SQLite database
function authenticateIP($ip, $delay) {
$dbPath = 'private/ip_timestamp.sqlite';
// Connect to the SQLite database
$db = new SQLite3($dbPath);
// Check if the IP exists in the database
$stmt = $db->prepare('SELECT timestamp FROM ip_timestamp WHERE ip = :ip');
$stmt->bindValue(':ip', $ip, SQLITE3_TEXT);
$result = $stmt->execute();
$foundIP = false;
$currentTime = time();
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$foundIP = true;
$timestamp = $row['timestamp'];
// If IP is found, check the timestamp
if (($currentTime - $timestamp) > $delay) {
// Update timestamp and return true
$stmt = $db->prepare('UPDATE ip_timestamp SET timestamp = :timestamp WHERE ip = :ip');
$stmt->bindValue(':timestamp', $currentTime, SQLITE3_INTEGER);
$stmt->bindValue(':ip', $ip, SQLITE3_TEXT);
$stmt->execute();
return true;
} else {
// IP found, but not allowed yet
echo "Too many requests";
return false;
}
}
// IP not found, add to database and return true
if (!$foundIP) {
$stmt = $db->prepare('INSERT INTO ip_timestamp (ip, timestamp) VALUES (:ip, :timestamp)');
$stmt->bindValue(':ip', $ip, SQLITE3_TEXT);
$stmt->bindValue(':timestamp', $currentTime, SQLITE3_INTEGER);
$stmt->execute();
return true;
}
return false;
}
function getIp(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
$file = 'private/is_pixel_war_active.txt';
// Read the file contents
$contents = file_get_contents($file);
// Check if the pixel war is active
if ($contents == 'false') {
// Pixel war is not active
echo "Pixel war is not active";
header("HTTP/1.1 403 Forbidden");
exit();
}
$dimensions = file_get_contents('private/taille.txt');
list($width, $height) = explode(',', $dimensions);
$data = json_decode(file_get_contents('php://input'), true);
// Vérifier si les données sont présentes
if (isset($data['x']) && isset($data['y']) && isset($data['color'])) {
//x est la position suivant l'axe x (correspond donc à la colonne) et y est la position suivant l'axe y (correspond donc à la ligne)
$line = $data['y'];
$column = $data['x'];
$color = $data['color'];
// Vérifier si les données sont valides
$colors = [
"#ffffff", "#e4e4e4", "#888888", "#222222",
"#ffa7d1", "#e50000", "#e59500", "#a06a42",
"#e5d900", "#94e044", "#02be01", "#00d3dd",
"#0083c7", "#0000ea", "#cd6eea", "#820080"
];
$isValid = true;
if (!is_int($column) || $column < 0 || $column >= $width) {
$isValid = false;
}
if (!is_int($line) || $line < 0 || $line >= $height) {
$isValid = false;
}
if (!in_array($color, $colors)) {
$isValid = false;
}
if (!$isValid) {
// Invalid data
header("HTTP/1.1 400 Bad Request");
exit();
}
if (!authenticateIP(getIp(), $delay)) {
// Too many requests
header("HTTP/1.1 429 Too Many Requests");
exit();
}
// Transforme la couleur en entier en fonction de sa position dans la liste et l'utilise pour écrire le pixel dans le fichier
$colorIndex = array_search($color, $colors);
$filename = 'private/pixels.bin';
//calcul de l'offset
$offset = (int)(($line * $width + $column) / 2);
$file = fopen($filename, 'r+b');
fseek($file, $offset, SEEK_SET);
$byte = fread($file, 1);
// Lire un octet du fichier fait avancer le curseur, il faut donc re-seek au bon offset
fseek($file, $offset, SEEK_SET);
// On réutilise pack et unpack, comme ça a été fait à l'initialisation (dans init_pixels.php)
$byte = unpack("C", $byte);
$byte = $byte[1];
//ecriture du pixel en fonction de sa parité en
if (($line * $width + $column) % 2 == 1) {
$byte = ($byte & 0xF0) | $colorIndex;
} else {
$byte = ($byte & 0x0F) | ($colorIndex << 4);
}
fwrite($file, pack("C", $byte));
fclose($file);
echo "success";
exit();
} else {
// Données manquantes
echo "failed";
header("HTTP/1.1 400 Bad Request");
exit();
}