-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_reinitialize_canva.php
96 lines (71 loc) · 2.63 KB
/
admin_reinitialize_canva.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
<?php
session_start();
// Vérifier si l'utilisateur est authentifié
if (!isset($_SESSION['user_type']) || $_SESSION['user_type'] !== 'admin') {
header("Location: admin_login.html");
exit();
}
// Check if line and column parameters are set
if (isset($_POST['line']) && isset($_POST['column'])) {
$height = filter_var($_POST['line'], FILTER_VALIDATE_INT);
$width = filter_var($_POST['column'], FILTER_VALIDATE_INT);
// Check if the values are valid integers and greater than zero
if ($height === false || $width === false || $height <= 0 || $width <= 0) {
header("Location: admin_control.php?error_reinitialize=true");
exit();
}
// *** part about saving the current state of the canvas *** //
$directory = 'private/';
$baseFilename = 'pixels_save_';
$extension = '.bin';
// Initialize the counter to 1
$counter = 1;
// Construct the initial filename
$filename = $directory . $baseFilename . $counter . $extension;
// Loop to find the next available filename
while (file_exists($filename)) {
$counter++;
$filename = $directory . $baseFilename . $counter . $extension;
}
// Copy the main file to the new backup filename
if (file_exists($directory . 'pixels.bin')) {
copy($directory . 'pixels.bin', $filename);
} else {
header("Location: admin_control.php?error_reinitialize=true");
exit();
}
// *** part about reinitializing the canva *** //
// Calculate the number of bytes needed
$bytes = (int) ($width * $height / 2);
// Open the file for writing
$file = fopen($directory . 'pixels.bin', 'wb');
if ($file === false) {
header("Location: admin_control.php?error_reinitialize=true");
exit();
}
$bytesString = str_repeat(pack('C', 0), $bytes);
fwrite($file, $bytesString);
// Close the file
fclose($file);
// Update the height and width values in the taille.txt file
$file = fopen($directory . 'taille.txt', 'w');
if ($file === false) {
header("Location: admin_control.php?error_reinitialize=true");
exit();
}
$data = $width . ',' . $height;
fwrite($file, $data);
// Close the file
fclose($file);
// *** part about deleting all the info from the DB *** //
$dbPath = 'private/ip_timestamp.sqlite';
// Connect to the SQLite database
$db = new SQLite3($dbPath);
// Delete all data from the ip_timestamp table
$db->exec("DELETE FROM ip_timestamp");
header("Location: admin_control.php");
exit();
} else {
header("Location: admin_control.php?error_reinitialize=true");
exit();
}