2018-06-13 20:07:24 +02:00
|
|
|
<?PHP
|
|
|
|
/**
|
2018-06-19 01:50:29 +02:00
|
|
|
* This script offers functionality related to uploading and deleting files.
|
2018-06-13 20:07:24 +02:00
|
|
|
*
|
|
|
|
* @author Joshua Ramon Enslin <joshua@jrenslin.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Require files and ensure environment.
|
|
|
|
*/
|
|
|
|
|
|
|
|
require_once __DIR__ . "/inc/functions.php";
|
|
|
|
|
2018-06-18 21:42:14 +02:00
|
|
|
ensureEnvironment(); // Ensure existence of system files.
|
|
|
|
$translations = loadLanguage($settings['defaultLang']); // Load translations.
|
|
|
|
ensureBackendEnv(); // Ensure session is started etc.
|
|
|
|
$pages = loadPages(); // Load overview of pages.
|
2018-06-13 20:07:24 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Load data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Check for vars.
|
2018-06-18 21:42:14 +02:00
|
|
|
loadHttpToGlobals(["subject", "task", "backTo"]);
|
2018-06-13 20:07:24 +02:00
|
|
|
if (!isset($task)) $task = "list";
|
|
|
|
|
|
|
|
define("fileDir", __DIR__ . "/../files");
|
|
|
|
|
|
|
|
if ($task == "list") {
|
|
|
|
|
|
|
|
$files = scanDirConts(fileDir);
|
2018-06-19 01:50:29 +02:00
|
|
|
|
|
|
|
$output = [];
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$output[] = [
|
|
|
|
"name" => $file,
|
|
|
|
"type" => mime_content_type(fileDir . "/$file"),
|
|
|
|
"size" => filesize(fileDir . "/$file"),
|
|
|
|
"mtime" => filemtime(fileDir . "/$file"),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($_GET['sort'])) {
|
|
|
|
if ($_GET['sort'] == "name") {
|
|
|
|
usort($output, function(array $a, array $b) {
|
|
|
|
return strnatcmp($a['name'], $b['name']);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if ($_GET['sort'] == "type") {
|
|
|
|
usort($output, function(array $a, array $b) {
|
|
|
|
return strnatcmp($a['type'], $b['type']);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if ($_GET['sort'] == "size") {
|
|
|
|
usort($output, function(array $a, array $b) {
|
|
|
|
if ($a['size'] == $b['size']) return 0;
|
|
|
|
return ($a['size'] > $b['size']) ? -1 : 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if ($_GET['sort'] == "mtime") {
|
|
|
|
usort($output, function(array $a, array $b) {
|
|
|
|
if ($a['mtime'] == $b['mtime']) return 0;
|
|
|
|
return ($a['mtime'] > $b['mtime']) ? -1 : 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo json_encode($output);
|
2018-06-13 20:07:24 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if ($task == "upload") {
|
|
|
|
|
2018-06-18 21:42:14 +02:00
|
|
|
$allowedFiletypes = [
|
|
|
|
"image/png",
|
|
|
|
"image/jpeg",
|
|
|
|
];
|
2018-06-13 20:07:24 +02:00
|
|
|
|
|
|
|
$uploaddir = fileDir . '/';
|
2018-06-18 21:42:14 +02:00
|
|
|
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
|
2018-06-13 20:07:24 +02:00
|
|
|
|
2018-06-18 21:42:14 +02:00
|
|
|
// Whitelist of allowed types.
|
|
|
|
if (!in_array($_FILES['file']['type'], $allowedFiletypes)) {
|
2018-06-21 13:25:38 +02:00
|
|
|
echo printErrorPage($settings, $translations['filetypeNotWhitelisted']);
|
2018-06-18 21:42:14 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-21 13:25:38 +02:00
|
|
|
if (filesize($_FILES['file']['tmp_name']) > $settings['maxFileSize']) {
|
|
|
|
echo printErrorPage($settings, $translations['fileTooLarge']);
|
2018-06-13 20:07:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-18 21:42:14 +02:00
|
|
|
if (!(move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))) {
|
2018-06-21 13:25:38 +02:00
|
|
|
echo printErrorPage($settings, $translations['fileUploadError']);
|
2018-06-18 21:42:14 +02:00
|
|
|
return;
|
2018-06-13 20:07:24 +02:00
|
|
|
}
|
2018-06-18 21:42:14 +02:00
|
|
|
|
|
|
|
$_SESSION["editHistory"] = ["changesStored", $translations['uploadedFile']];
|
|
|
|
|
|
|
|
// Refer back
|
|
|
|
if (isset($backTo)) header('Location: ' . $backTo);
|
|
|
|
else header('Location: ./');
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if ($task == "delete") {
|
|
|
|
|
|
|
|
if (!is_file(fileDir . "/$subject")) {
|
2018-06-21 13:25:38 +02:00
|
|
|
echo printErrorPage($settings, $translations['fileDoesNotExist']); return;
|
2018-06-13 20:07:24 +02:00
|
|
|
}
|
|
|
|
|
2018-06-18 21:42:14 +02:00
|
|
|
unlink(fileDir . "/$subject");
|
|
|
|
|
|
|
|
$_SESSION["editHistory"] = ["changesDeleted", $translations['deletedFile'] . " $subject"];
|
|
|
|
|
|
|
|
// Refer back
|
|
|
|
if (isset($backTo)) header('Location: ' . $backTo);
|
|
|
|
else header('Location: ./');
|
|
|
|
return;
|
|
|
|
|
2018-06-13 20:07:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|