web-utils/modules/quickupload/quickupload.php

121 lines
2.8 KiB
PHP
Raw Normal View History

2021-08-08 17:23:49 +00:00
<?php
$path = __DIR__ . '/../../tmp/quickupload';
if (!file_exists($path)) {
mkdir($path);
}
$path = realpath($path);
function slugify($string){
return strtolower(trim(preg_replace('/[^.A-Za-z0-9-]+/', '-', $string), '-'));
}
2021-10-03 16:10:30 +00:00
if (strpos($_SERVER['REQUEST_URI'], '/delete') === 0) {
$fileName = $_GET['name'] ?? '';
if (empty($fileName)) {
echo "ERR: need a file name to work with";
http_response_code(400);
exit();
}
if (dirname($path . '/' . $fileName) !== $path) {
echo "ERR: invalid path";
http_response_code(400);
exit();
}
$fileName = str_replace('/', '', $fileName);
if (!file_exists($path . '/' . $fileName)) {
echo "ERR: invalid path";
http_response_code(400);
exit();
}
unlink($path . '/' . $fileName);
if (isset($_GET['redirect']) && $_GET['redirect'] === 'yes') {
header('Location: /list');
http_response_code(302);
exit();
}
exit();
}
if ($_SERVER['REQUEST_URI'] === '/list') {
$files = getFilesAsArray($path);
?>
<h3>List of uploaded files</h3>
2021-10-03 16:10:30 +00:00
<div>
<?php
foreach ($files as $fileName) {
?>
2021-10-03 16:10:30 +00:00
<div
style="
max-width: 900px;
display: flex;
justify-content: space-between;
border: 1px solid gray;
margin-bottom: .4em
">
<div>
<a href="/file/<?= $fileName ?>"><?= $fileName ?></a>
</div>
<a href="/delete?name=<?= $fileName ?>&redirect=yes">Delete</a>
</div>
<?php
}
?>
2021-10-03 16:10:30 +00:00
</div>
<?php
exit();
}
$file = null;
$isAutoGeneratedFileName = false;
if (isset($_POST['name']) && !empty($_POST['name'])) {
$fileName = $_POST['name'];
} else {
$fileName = generateRandomString(6);
$isAutoGeneratedFileName = true;
}
if (isset($_POST['text']) && !empty($_POST['text'])) {
if ($isAutoGeneratedFileName) $fileName .= '.txt';
file_put_contents($path . '/' . $fileName, $_POST['text']);
$file = true;
}
else if (isset($_FILES['file'])) {
$file = $_FILES['file'];
if ($isAutoGeneratedFileName) $fileName .= '_' . slugify($file['name']);
copy($file['tmp_name'], $path . '/' . $fileName);
}
if ($file !== null) {
2021-08-08 17:23:49 +00:00
?>
<div style="padding: 20px; border: 1px solid green; background: green; color: white">
File uploaded! <a href="/file/<?= $fileName ?>">Click here to find it</a>
2021-08-08 17:23:49 +00:00
</div>
<?php
}
2021-08-08 17:23:49 +00:00
?>
<!DOCTYPE html>
<html>
<head>
<title>QuickUpload</title>
</head>
<body>
<h1>QuickUpload</h1>
2021-08-08 17:23:49 +00:00
<form enctype="multipart/form-data" method="POST">
Upload a direct file
<input type="file" name="file" /><input type="submit" value="Submit">
<br>
<br>
Or paste your data:
<textarea name="text" style="margin: 1em 0; width:100%; height: 60vh"></textarea><br>
<label for="name">File name:</label>
<input type="text" name="name" />
<br>
<br>
<input type="submit" value="Submit" style="width: 6em; height: 3em;">
</form>
</body>
</html>