Initial commit

This commit is contained in:
Beu 2019-04-28 15:27:55 +02:00
commit a3aeb75f5a
7 changed files with 160 additions and 0 deletions

5
README.md Normal file
View File

@ -0,0 +1,5 @@
# web-ft
Self-Hosted file transfer tool compatible with curl
**CAREFUL, I don't know if it's secure**

6
conf/config.sample.php Normal file
View File

@ -0,0 +1,6 @@
<?php
define('_DATA_DIR','data');
define('_HTTP_PROTO','http');
define('_HTTP_DOMAIN','localhost:8080'); // domain/IP with port if needed
define('_HTTP_PATH','/');
?>

0
data/.gitkeep Normal file
View File

18
front.php Normal file
View File

@ -0,0 +1,18 @@
<html>
<head>
<title>VirtIT ft</title>
<link rel="stylesheet" type="text/css" href="<?php print(_HTTP_PATH . "style.css"); ?> ">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="<?php print(_HTTP_PATH . "script.js"); ?> "></script>
</head>
<body>
<div class=dragndrop ondrop="upload_file(event);" ondragover="return false" >
Drag and Drop your file or <input type="button" value="Select File" onclick="file_explorer();">
<input type="file" id="uploadfile" hidden>
<br><br><br><br>
<div id=divresponse></div><button id=copytoclipboard style="visibility: hidden;" onclick="copytoclipboard();">Copy to clipboard</button>
</div>
</body>

58
index.php Normal file
View File

@ -0,0 +1,58 @@
<?php
// Load configuration
include 'conf/config.php';
// Check if file have to be deleted
$data_dir = array_diff(scandir(_DATA_DIR), array('..', '.', '.gitkeep'));
foreach ($data_dir as $data_dir_content)
{
$data_conf = json_decode(file_get_contents(_DATA_DIR . "/" . $data_dir_content . "/info.json"),true);
if ($data_conf[expire] < time())
{
$i = new DirectoryIterator(_DATA_DIR . "/" . $data_dir_content);
foreach($i as $f) {
if($f->isFile())
{
unlink($f->getRealPath());
} else if(!$f->isDot() && $f->isDir()) {
rrmdir($f->getRealPath());
}
}
rmdir(_DATA_DIR . "/" . $data_dir_content );
}
}
// Upload File
if ($_SERVER['REQUEST_METHOD'] === 'PUT')
{
$rnd_data = mt_rand(10000,99999);
mkdir(_DATA_DIR . "/" . $rnd_data);
$info_json->expire = time() + (7 * 24 * 60 * 60);
file_put_contents(_DATA_DIR . "/" . $rnd_data . "/info.json", json_encode($info_json));
$putdata = fopen("php://input", "r");
$fp = fopen(_DATA_DIR . "/" . $rnd_data . "/file", "w");
while ($data = fread($putdata, 1024))
{
fwrite($fp, $data);
}
fclose($fp);
fclose($putdata);
print(_HTTP_PROTO . '://' . _HTTP_DOMAIN . _HTTP_PATH . 'data/' . $rnd_data . '/file');
}
// Informations for user
if ($_SERVER['REQUEST_METHOD'] === 'GET')
{
if (stristr($_SERVER["HTTP_USER_AGENT"], 'curl'))
{
print("To upload file, use # curl --upload-file my_file " . _HTTP_PROTO . '://' . _HTTP_DOMAIN . _HTTP_PATH . "index.php\n");
}
else
{
include("front.php" );
}
}
?>

46
script.js Normal file
View File

@ -0,0 +1,46 @@
var fileobj;
function upload_file(e)
{
e.preventDefault();
fileobj = e.dataTransfer.files[0];
ajax_file_upload(fileobj);
}
function file_explorer() {
document.getElementById('uploadfile').click();
document.getElementById('uploadfile').onchange = function()
{
fileobj = document.getElementById('uploadfile').files[0];
ajax_file_upload(fileobj);
};
}
function copytoclipboard()
{
var elm = document.getElementById("divresponse");
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(elm);
selection.removeAllRanges();
selection.addRange(range);
document.execCommand("Copy");
}
function ajax_file_upload(file_obj)
{
$.ajax(
{
type: 'PUT',
url: 'index.php',
contentType: false,
processData: false,
data: file_obj,
success:function(response)
{;
var divresponse = document.getElementById('divresponse');
divresponse.innerHTML = response ;
document.getElementById('copytoclipboard').style.visibility = "visible";
}
});
}

27
style.css Normal file
View File

@ -0,0 +1,27 @@
html {
max-width: 1500px;
margin: auto;
display: inline-block;
padding-top:50px;
text-align: center;
}
.dragndrop {
margin: 20%;
margin-top: 10%;
background-color: #a3a3a3;
padding: 1px;
padding-top: 5%;
height:40%;
font-size: 20px;
cursor: pointer;
border: 6px dashed #444444;
border-radius: 20px;
}
.dragndrop:hover {
background-color: #888888;
}