-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
169 lines (151 loc) · 5.27 KB
/
index.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
164
165
166
167
168
169
<?php
include('includes/config.php');
include('includes/helpers.php');
$now = new DateTime('now');
$start->setTimezone(new DateTimeZone("UTC"));
$end->setTimezone(new DateTimeZone("UTC"));
$template = 'includes/main.php';
$error = '';
$json_string = file_get_contents("data/participants.json");
$participants = json_decode($json_string, true);
if ($now > $end) {
if ($published) {
$template = 'includes/list.php';
} else {
$template = 'includes/judging.php';
}
} elseif ($now > $start && $now < $end) {
$template = 'includes/upload.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$error = upload($participants);
if ($error === "") {
$template = 'includes/uploaded.php';
$authorName = $_POST['authorName'];
$mapName = $_POST['mapName'];
$description = $_POST['description'];
$instructions = $_POST['instructions'];
$filename = $_FILES['rms']['name'];
$directory = $participants[$authorName]['maps'][0]['hash'];
$submissionCode = substr(hash('sha256', $directory), 0, 6);
$imageFileName = $participants[$authorName]['maps'][0]['image'];
}
}
}
function upload(&$participants)
{
if (isset($_POST['authorName']) &&
isset($_POST['mapName']) &&
isset($_POST['description']) &&
isset($_POST['instructions']) &&
isset($_FILES['rms']) &&
isset($_FILES['image'])
) {
if ($_POST['authorName'] === '' ||
$_POST['mapName'] === '' ||
$_POST['description'] === '' ||
$_FILES['rms']['name'] === ''
) {
return 'Please fill in at least your name, the rms file, the map name, a description, and the game version.';
}
try {
$bytes = random_bytes(5);
$hash = bin2hex($bytes);
$authorName = $_POST['authorName'];
$imageName = null;
if ($_FILES['image']['name'] != '') {
$imageFileName = basename($_FILES["image"]["name"]);
$imageFileType = strtolower(pathinfo($imageFileName, PATHINFO_EXTENSION));
if ($imageFileType != "png" && $imageFileType != "jpg") {
return "Please upload a .png or .jpg file";
}
$imageName = 'image.' . $imageFileType;
}
$map = [
"authorName" => $authorName,
"mapName" => $_POST['mapName'],
"description" => $_POST['description'],
"instructions" => $_POST['instructions'],
"filename" => $_FILES['rms']['name'],
"image" => $imageName,
"hash" => $hash
];
if (!isset($participants[$authorName]['maps'])) {
$participants[$authorName]['maps'] = [];
}
array_unshift($participants[$authorName]['maps'], $map);
$target_dir = "maps/$hash/";
$target_file = $target_dir . basename($_FILES["rms"]["name"]);
$fileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
if ($fileType != "rms") {
return "Please upload a .rms file";
}
$success = mkdir($target_dir);
if ($success === false) {
return "Could not create folder";
}
if (!move_uploaded_file($_FILES["rms"]["tmp_name"], $target_file)) {
return "There was an error uploading your file";
}
if ($_FILES['image']['name'] != '') {
$targetImageFile = $target_dir . $imageName;
if (!move_uploaded_file($_FILES["image"]["tmp_name"], $targetImageFile)) {
return "There was an error uploading your image file";
}
}
$success = file_put_contents("data/participants.json", json_encode($participants, JSON_PRETTY_PRINT));
if ($success === false) {
return "Could not save metadata";
}
} catch (Exception $e) {
return 'Could not create ID.';
}
}
return '';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Random Map Contest</title>
<link rel="stylesheet" type="text/css" href="assets/dark-bootstrap.min.css">
<style>
html,
body {
height: 100%;
background: url("assets/bg.png") no-repeat fixed;
background-size: cover;
background-color: black;
}
.card {
background-color: rgba(48, 48, 48, 0.8);
margin-top: 1rem;
margin-bottom: 1rem;
}
.wrapper {
min-height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
}
h5 > .badge {
vertical-align: middle;
margin-top: -0.5em;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container">
<div class="card">
<div class="card-body">
<?php
include($template);
?>
</div>
</div>
</div>
</div>
</body>
</html>