-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.php
271 lines (260 loc) · 9.18 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
include "includes/pollyfills.inc.php";
if (!file_exists('config.php')) {
http_response_code(503);
include "includes/notinstalled.inc.php";
exit();
}
include "config.php";
if (!isset($_POST) || !isset($_POST['action'])) {
http_response_code(400);
echo 'Bad Request, the Request should use HTTP POST method with an "action" parameter.';
exit;
}
$action = $_POST['action'];
$secret_key = $_POST['secret-key'];
$username = $_POST['username'];
$password = $_POST['password'];
$data_key = isset($_POST['data-key']) ? $_POST['data-key'] : false;
$data_value = isset($_POST['data-value']) ? $_POST['data-value'] : false;
$type = isset($_POST['type']) ? $_POST['type'] : 'user';
$create_account = isset($_POST['create-account']) ? true : false;
$filename = isset($_POST['file-name']) ? $_POST['file-name'] : $data_key;
if ($secret_key !== SECRET_KEY) {
http_response_code(400);
exit("The Secret Key is invalid.");
}
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($mysqli->connect_errno) {
http_response_code(500);
echo "Error: Failed to make a MySQL connection, here is why: \n";
echo "Errno: " . $mysqli->connect_errno . "\n";
echo "Error: " . $mysqli->connect_error . "\n";
exit("The MySQL connection failed.");
}
$get_user_sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = $mysqli->query($get_user_sql);
if (!$result) {
http_response_code(500);
echo "Error: Failed to Execute Query, here is why: \n";
echo "Query: " . $get_user_sql . "\n";
echo "Errno: " . $mysqli->errno . "\n";
echo "Error: " . $mysqli->error . "\n";
exit;
}
if ($result->num_rows === 0) {
if ($create_account) {
$date = date("Y-m-d H:i:s");
$add_user_sql = "INSERT INTO users (`username`, `password`, `type`, `registered`) VALUES ('$username', '$password', '$type', '$date')";
$result = $mysqli->query($add_user_sql);
if (!$result) {
http_response_code(500);
echo "Error: Failed to Execute Query, here is why: \n";
echo "Query: " . $add_user_sql . "\n";
echo "Errno: " . $mysqli->errno . "\n";
echo "Error: " . $mysqli->error . "\n";
exit;
}
$result = $mysqli->query($get_user_sql);
if (!$result) {
http_response_code(500);
echo "Error: Failed to Execute Query, here is why: \n";
echo "Query: " . $get_user_sql . "\n";
echo "Errno: " . $mysqli->errno . "\n";
echo "Error: " . $mysqli->error . "\n";
exit;
}
} else {
http_response_code(500);
echo "We could not find a match for username $username, sorry about that. Please try again.";
exit;
}
}
$user = $result->fetch_assoc();
switch ($action) {
case 'getfileurl':
$filepath = merge_paths(UPLOAD_FOLDER, $username, $filename);
if (file_exists($filepath)) {
$filepath = str_replace(realpath($_SERVER['DOCUMENT_ROOT']), '', $filepath);
$filepath = str_replace('\\', '/', $filepath);
$url = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url .= $_SERVER['HTTP_HOST'] . $filepath;
http_response_code(200);
echo $url;
exit;
} else {
http_response_code(500);
echo "Error: The Requested File does not Exists.";
exit;
}
break;
case 'uploadfile':
if (isset($_FILES) && isset($_FILES['file'])) {
if ($_FILES['file']['error'] == UPLOAD_ERR_OK) {
$filepath = merge_paths(UPLOAD_FOLDER, $username, $_FILES['file']['name']);
if (!file_exists(dirname($filepath))) {
mkdir(dirname($filepath), 0777, true);
}
if (move_uploaded_file($_FILES['file']['tmp_name'], $filepath)) {
http_response_code(200);
exit("File Successfully Uploaded");
} else {
http_response_code(500);
echo "Error: Upload Failed, maybe the file is invalid or there is some problem with the file.";
exit;
}
} else {
http_response_code(500);
echo "Error: Upload Failed, here is why: \n";
switch ($_FILES['file']['error']) {
case UPLOAD_ERR_INI_SIZE:
$message = "The uploaded file exceeds the upload_max_filesize directive in php.ini";
break;
case UPLOAD_ERR_FORM_SIZE:
$message = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
break;
case UPLOAD_ERR_PARTIAL:
$message = "The uploaded file was only partially uploaded";
break;
case UPLOAD_ERR_NO_FILE:
$message = "No file was uploaded";
break;
case UPLOAD_ERR_NO_TMP_DIR:
$message = "Missing a temporary folder";
break;
case UPLOAD_ERR_CANT_WRITE:
$message = "Failed to write file to disk";
break;
case UPLOAD_ERR_EXTENSION:
$message = "File upload stopped by extension";
break;
default:
$message = "Unknown upload error";
break;
}
echo $message;
exit;
}
} else {
http_response_code(500);
echo "Error: No File Received, Upload Failed.";
exit;
}
break;
case 'downloadfile':
$filepath = merge_paths(UPLOAD_FOLDER, $username, $filename);
if (file_exists($filepath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header("Content-Length: " . filesize($filepath));
readfile($filepath);
} else {
http_response_code(500);
echo "Error: Requested File does not Exists, Download Failed.";
exit;
}
break;
case 'save':
$load_sql = "SELECT * FROM saves WHERE user_id='" . $user['ID'] . "' AND data_key='$data_key'";
$result = $mysqli->query($load_sql);
if (!$result) {
http_response_code(500);
echo "Error: Failed to Execute Query, here is why: \n";
echo "Query: " . $load_sql . "\n";
echo "Errno: " . $mysqli->errno . "\n";
echo "Error: " . $mysqli->error . "\n";
exit;
}
if ($result->num_rows === 0) {
$save_sql = "INSERT INTO saves (`user_id`, `data_key`, `data_value`) VALUES ('" . $user['ID'] . "', '$data_key', '$data_value')";
} else {
$save_sql = "UPDATE saves SET data_value='$data_value' WHERE user_id='" . $user['ID'] . "' AND data_key='$data_key'";
}
$result = $mysqli->query($save_sql);
if (!$result) {
http_response_code(500);
echo "Error: Failed to Execute Query, here is why: \n";
echo "Query: " . $save_sql . "\n";
echo "Errno: " . $mysqli->errno . "\n";
echo "Error: " . $mysqli->error . "\n";
exit;
}
http_response_code(200);
$mysqli->close();
exit("Data Successfully Saved");
break;
case 'load':
$load_sql = "SELECT * FROM saves WHERE user_id='" . $user['ID'] . "' AND data_key='$data_key'";
$result = $mysqli->query($load_sql);
if (!$result) {
http_response_code(500);
echo "Error: Failed to Execute Query, here is why: \n";
echo "Query: " . $load_sql . "\n";
echo "Errno: " . $mysqli->errno . "\n";
echo "Error: " . $mysqli->error . "\n";
exit;
}
if ($result->num_rows === 0) {
http_response_code(500);
exit("Error: The Data with given identifier does not found.");
}
$data = $result->fetch_assoc();
http_response_code(200);
$mysqli->close();
exit($data['data_value']);
break;
case 'delete':
$delete_sql = "DELETE FROM saves WHERE user_id='" . $user['ID'] . "' AND data_key='$data_key'";
$result = $mysqli->query($delete_sql);
if (!$result) {
http_response_code(500);
echo "Error: Failed to Execute Query, here is why: \n";
echo "Query: " . $delete_sql . "\n";
echo "Errno: " . $mysqli->errno . "\n";
echo "Error: " . $mysqli->error . "\n";
exit;
}
http_response_code(200);
$mysqli->close();
exit("User Data Successfully Deleted");
break;
case 'deleteaccount':
$delete_sql = "DELETE FROM users WHERE ID='" . $user['ID'] . "'";
$result = $mysqli->query($delete_sql);
if (!$result) {
http_response_code(500);
echo "Error: Failed to Execute Query, here is why: \n";
echo "Query: " . $delete_sql . "\n";
echo "Errno: " . $mysqli->errno . "\n";
echo "Error: " . $mysqli->error . "\n";
exit;
}
http_response_code(200);
$mysqli->close();
exit("User Account Successfully Deleted");
break;
case 'clear':
$clear_sql = "DELETE FROM saves WHERE user_id='" . $user['ID'] . "'";
$result = $mysqli->query($clear_sql);
if (!$result) {
http_response_code(500);
echo "Error: Failed to Execute Query, here is why: \n";
echo "Query: " . $clear_sql . "\n";
echo "Errno: " . $mysqli->errno . "\n";
echo "Error: " . $mysqli->error . "\n";
exit;
}
http_response_code(200);
$mysqli->close();
exit("User Data Successfully Cleared");
break;
default:
http_response_code(400);
$mysqli->close();
exit("The given action does not exists: $action");
break;
}