converter/index.php

52 lines
1.9 KiB
PHP

<?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]);
}
?>