-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.php
More file actions
52 lines (37 loc) · 1.49 KB
/
create.php
File metadata and controls
52 lines (37 loc) · 1.49 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
<?php
// Check if the request method is POST and the content type is JSON
if ($_SERVER['REQUEST_METHOD'] === 'POST' &&
isset($_SERVER['CONTENT_TYPE']) &&
$_SERVER['CONTENT_TYPE'] === 'application/json') {
// Get the JSON request body
$jsonPayload = file_get_contents('php://input');
// Attempt to decode the JSON data
$data = json_decode($jsonPayload, true); // Use 'true' for associative array, omit for object
// Check if JSON decoding was successful
if ($data === null && json_last_error() !== JSON_ERROR_NONE) {
// JSON decoding failed
http_response_code(400); // Bad Request
echo json_encode(['error' => 'Invalid JSON']);
exit;
}
if(isset($_REQUEST['password'])&&$_REQUEST['password']=="coolrules"){
// Read the JSON data from setups.json
$jsonFile = 'setups.json';
$jsonData = file_get_contents($jsonFile);
// Decode the JSON data into an associative array
$setups = json_decode($jsonData, true);
$setups[$_REQUEST['difficulty']][$_REQUEST['name']] = $data;
// Encode the modified array back to JSON
$newJsonData = json_encode($setups, JSON_PRETTY_PRINT);
// Save the modified JSON back to setups.json
file_put_contents($jsonFile, $newJsonData);
echo "Puzzle ".$_REQUEST['name']." added";
}else{
http_response_code(403);
echo "Totally the correct password";
}
} else {
http_response_code(405); // Method Not Allowed
echo json_encode(['error' => 'Invalid request method or content type']);
}
?>