Initial implementation
This commit is contained in:
57
upload.php
Normal file
57
upload.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
$location = 'uploads/';
|
||||
// check if directory exists and writable if not show error 500
|
||||
if (!is_dir($location) || !is_writable($location)) {
|
||||
header('HTTP/1.0 500 Internal Server Error');
|
||||
echo 'Directory does not exist or is not writable';
|
||||
exit;
|
||||
}
|
||||
// if .uploadsecret file does not exist, error 500
|
||||
if (!file_exists('.uploadsecret')) {
|
||||
header('HTTP/1.0 500 Internal Server Error');
|
||||
echo 'Config missing';
|
||||
exit;
|
||||
}
|
||||
|
||||
// read secret from .uploadsecret file
|
||||
$secret = trim(file_get_contents('.uploadsecret'));
|
||||
|
||||
// Upload file and verify GET argument with password
|
||||
if (isset($_GET['password']) && $_GET['password'] == $secret) {
|
||||
if (isset($_FILES['file'])) {
|
||||
$file = $_FILES['file'];
|
||||
$name = $file['name'];
|
||||
$tmp_name = $file['tmp_name'];
|
||||
// TODO: Check for safe name
|
||||
if (move_uploaded_file($tmp_name, $location.$name)) {
|
||||
echo 'File '.$location.$name." uploaded successfully\n";
|
||||
// if $name suffix ends with "split.final" then merge all files
|
||||
if (substr($name, -11) == 'split.final') {
|
||||
echo('Merging files...');
|
||||
// Remove only the "split.final" file
|
||||
unlink($location.$name);
|
||||
// new $nameorig without the ".split.final" suffix
|
||||
$nameorig = substr($name, 0, -12);
|
||||
// Merge all files with the same $nameorig using cat
|
||||
chdir($location);
|
||||
shell_exec('cat '.$nameorig.'.split* > '.$nameorig);
|
||||
// Remove all the split files
|
||||
shell_exec('rm '.$nameorig.'.split*');
|
||||
// show checksum sha512 of the merged file
|
||||
$out = shell_exec('sha512sum '.$nameorig);
|
||||
echo $out;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// If it is not upload, show upload script file: upload.sh as text/plain
|
||||
header('Content-Type: text/plain');
|
||||
$data = file_get_contents('upload.sh');
|
||||
// replace %%UPLOADHOST%% in $data with the current hostname of webserver
|
||||
$data = str_replace('%%UPLOADHOST%%', $_SERVER['HTTP_HOST'], $data);
|
||||
echo $data;
|
||||
}
|
||||
} else {
|
||||
// http error
|
||||
header('HTTP/1.0 403 Forbidden');
|
||||
echo 'Forbidden';
|
||||
}
|
||||
49
upload.sh
Executable file
49
upload.sh
Executable file
@@ -0,0 +1,49 @@
|
||||
#!/bin/bash
|
||||
# ask password, upload file to https://%%UPLOADHOST%%/upload.php?password=<given password>
|
||||
|
||||
# function to upload file with password
|
||||
upload() {
|
||||
# $1 is file
|
||||
# $2 is password
|
||||
echo "Uploading $1"
|
||||
curl -F "file=@$1" https://%%UPLOADHOST%%/upload.php?password=$2
|
||||
# if failed exit
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Upload $1 failed"
|
||||
exit 1
|
||||
fi
|
||||
echo "Upload $1 done"
|
||||
}
|
||||
|
||||
# verify argument is existing file
|
||||
if [ ! -f "$1" ]; then
|
||||
echo "File not found: $1"
|
||||
exit 1
|
||||
fi
|
||||
# ask password
|
||||
read -s -p "Password: " password
|
||||
|
||||
# If file larger than 5M then split it to pieces and make extension .split
|
||||
if [ $(stat -c%s "$1") -gt 1500000 ]; then
|
||||
# retrieve sha512 hash of file to variable sum
|
||||
sum=$(sha512sum "$1" | cut -d' ' -f1)
|
||||
split -b 1500000 "$1" "$1".split
|
||||
#rm "$1"
|
||||
echo "File too large, split to pieces"
|
||||
# touch final file that has ending .split.final
|
||||
touch "$1".split.final
|
||||
# Now upload them all same way as other files
|
||||
for file in "$1".split*; do
|
||||
upload $file $password
|
||||
done
|
||||
# Upload final file
|
||||
# upload "$1.split.final" $password
|
||||
# Delete all generated files
|
||||
rm "$1".split*
|
||||
# show hash of file
|
||||
echo "sha512sum of file: $sum"
|
||||
else
|
||||
upload $1 $password
|
||||
fi
|
||||
|
||||
|
||||
Reference in New Issue
Block a user