web-ft/index.php

133 lines
5.3 KiB
PHP
Raw Normal View History

2019-04-28 15:27:55 +02:00
<?php
// Load configuration
include 'conf/config.php';
// Delete directory recursively
function delTree($dir) {
$files = array_diff(scandir($dir), array('.','..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}
function formatBytes($size, $precision = 2) {
$base = log($size, 1024);
$suffixes = array('', 'KB', 'MB', 'GB', 'TB');
return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
}
2019-04-28 15:27:55 +02:00
// 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);
2019-04-28 16:43:14 +02:00
if ($data_conf['expire'] < time())
2019-04-28 15:27:55 +02:00
{
delTree(_DATA_DIR . "/" . $data_dir_content);
2019-04-28 15:27:55 +02:00
}
}
2019-10-23 17:22:25 +02:00
if (_PRETTY_URL === true)
{
$downloadurl = _HTTP_PROTO . '://' . _HTTP_DOMAIN . _HTTP_PATH ;
$uploadurl = $downloadurl ;
}
else
{
$downloadurl = _HTTP_PROTO . '://' . _HTTP_DOMAIN . _HTTP_PATH . 'index.php?id=' ;
$uploadurl = _HTTP_PROTO . '://' . _HTTP_DOMAIN . _HTTP_PATH . 'index.php?name=my_file' ;
}
2019-04-28 15:27:55 +02:00
// Upload File
if ($_SERVER['REQUEST_METHOD'] === 'PUT')
{
2020-02-24 22:49:25 +01:00
do
{
$rnd_data = mt_rand(00000,99999);
} while (file_exists(_DATA_DIR . "/" . $rnd_data));
$filesize = 0 ;
2019-04-28 15:27:55 +02:00
mkdir(_DATA_DIR . "/" . $rnd_data);
2019-04-28 17:00:44 +02:00
$info_json = new stdClass();
2019-04-28 15:27:55 +02:00
$info_json->expire = time() + (7 * 24 * 60 * 60);
2019-10-23 15:38:13 +02:00
if ($_GET['name'])
{
$info_json->name = $_GET['name'];
}
2019-04-28 15:27:55 +02:00
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) and $filesize < _SIZE_LIMIT )
2019-04-28 15:27:55 +02:00
{
$filesize += 1024 ;
2019-04-28 15:27:55 +02:00
fwrite($fp, $data);
}
fclose($putdata);
fclose($fp);
if ($filesize < _SIZE_LIMIT )
{
print($downloadurl . $rnd_data . "\n");
}
else
{
delTree(_DATA_DIR . "/" . $rnd_data);
print("File size exceeded (Max " . formatBytes(_SIZE_LIMIT) . " bytes)");
}
2019-04-28 15:27:55 +02:00
}
// Informations for user
if ($_SERVER['REQUEST_METHOD'] === 'GET')
{
2019-10-23 15:56:55 +02:00
if (isset($_GET['id']))
2019-04-28 15:27:55 +02:00
{
2019-10-23 15:38:13 +02:00
if (is_dir(_DATA_DIR . "/" . $_GET['id']))
{
$data_conf = json_decode(file_get_contents(_DATA_DIR . "/" . $_GET['id'] . "/info.json"),true);
if ($data_conf['name'])
{
$filename = $data_conf['name'];
}
else
{
$filename = "file";
}
$file = _DATA_DIR . "/" . $_GET['id'] . "/file" ;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header('Content-Length: ' . filesize($file));
header("Content-disposition: attachment; filename=\"" . $filename . "\"");
readfile($file);
}
else
{
http_response_code(404);
}
2019-04-28 15:27:55 +02:00
}
else
{
2019-10-23 15:38:13 +02:00
if (stristr($_SERVER["HTTP_USER_AGENT"], 'curl') or stristr($_SERVER["HTTP_USER_AGENT"], 'Wget'))
{
print("To upload file (max " . formatBytes(_SIZE_LIMIT) . "):\n");
2020-02-24 23:06:38 +01:00
print("* curl --upload-file my_file " . $uploadurl . "\n");
print("* wget -q --body-file my_file --method=PUT -O - " . $uploadurl . "\n");
print("\n");
print("To download file :\n");
print("* curl -OJ " . $downloadurl . "99999\n");
print("* wget --content-disposition " . $downloadurl . "99999\n");
2019-10-23 15:38:13 +02:00
}
else
{
include("front.php" );
}
2019-04-28 15:27:55 +02:00
}
2019-10-23 16:16:21 +02:00
}
2019-04-28 15:27:55 +02:00
?>