This commit is contained in:
Pavel Dmitriev 2020-12-08 11:59:37 +03:00
parent 1c43e61ed9
commit 1fc344a672
4 changed files with 104 additions and 0 deletions

4
.htaccess Normal file
View File

@ -0,0 +1,4 @@
<Files "*.sh">
deny from all
</Files>

18
expunge.sh Normal file
View File

@ -0,0 +1,18 @@
#!/bin/bash
uploaddir='/var/www/html/files';
timeout=30
list=`ls ${uploaddir}`
for file in ${list}
do
prefix=`echo ${file} | awk -F '-' '{ print $1}'`
stamp=`date +%s`
delta=`expr ${stamp} - ${prefix}`
if [[ "$delta" -gt "$timeout" ]]
then
rm -f "${uploaddir}/${file}"
fi
done

52
index.php Normal file
View File

@ -0,0 +1,52 @@
<?php
/* Clent unoconv -n -f pdf /var/www/html/report_by_service\ 2020-11-14\ 12\:55\:29.ods
* Server libreoffice7.0 --nologo --headless --nofirststartwizard --accept='socket,host=127.0.0.1,port=2002,tcpNoDelay=1;urp'
*
* TEST Standalone convert libreoffice7.0 --headless --convert-to pdf /var/www/html/sla_test.odt
*/
$uploaddir = '/var/www/html/files';
$allowed_ext=["ods","odt","doc","docx","xls","xlsx"];
$allowed_types=["ods","odt","doc","docx","xls","xlsx","pdf"];
$res=null;
$code=null;
$r=null;
if (!empty($_POST["rtype"] && $_POST["rtype"]=='html')) {
$rtype='html';
} else {
$rtype='json';
}
$type=strtolower($_POST["format"]);
if (empty($_POST["format"])) { print json_encode(["status"=>"ERR","message"=>"Empty type"]); exit;}
if (array_search($type, $allowed_types) === false) {print json_encode(["status"=>"ERR","message"=>"Invalid type"]); exit;}
if (preg_match("/\.([^\.]*)$/", $_FILES['userfile']['name'], $r)) {
$name_tmp=preg_replace("![\.]!","-",time().".".uniqid(null,true));
if (array_search(strtolower($r[1]), $allowed_ext) === false) {print json_encode(["status"=>"ERR","message"=>"Invalid src ext"]); exit;}
$src=$uploaddir."/".$name_tmp.".".$r[1];
$dst=$uploaddir."/".$name_tmp.".".$type;
move_uploaded_file($_FILES['userfile']['tmp_name'], $src);
exec("/usr/bin/unoconv -n -f ".$type." -o '".$dst."' '".$src."'",$res,$code);
if ($code ==0) {
exec("rm -f ".$src);
} else {
exec("rm -f $src");
exec("rm -f $dst");
print json_encode(["status"=>"ERR","message"=>"Converter error. Code $code"]);
exit;
}
} else {
print json_encode(["status"=>"ERR","message"=>"Invalid src filename"]);
exit;
}
if ($rtype=='html') {
print "<html><a href='/files/".$name_tmp.".".$type."'>$name_tmp.$type</a>";
} else {
print json_encode(["status"=>"OK","result"=>"/files/".$name_tmp.".".$type]);
}
?>

30
test.php Normal file
View File

@ -0,0 +1,30 @@
<html><head>
</head>
<body>
<!-- Тип кодирования данных, enctype, ДОЛЖЕН БЫТЬ указан ИМЕННО так -->
<form enctype="multipart/form-data" action="/" method="POST">
<!-- Поле MAX_FILE_SIZE должно быть указано до поля загрузки файла -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000000" />
<!-- Название элемента input определяет имя в массиве $_FILES -->
Отправить этот файл: <input name="userfile" type="file" />
<select name="format">
<option value="pdf">PDF</option>
<option value="docx">Word 2010</option>
<option value="doc">Word 2003</option>
<option value="xlsx">Excel 2010</option>
<option value="xls">Excel 2003</option>
</select>
<select name="rtype">
<option value="json">Вернуть JSON</option>
<option value="html">Вернуть HTML</option>
</select>
<input type="submit" value="Отправить файл" />
</form>
</body>
</html>