1
0
mirror of https://github.com/24eme/signaturepdf.git synced 2023-08-25 09:33:08 +02:00
signaturepdf/app.php
Vincent LAURENT 84790c924c La route pour sauvegardé un pdf signé n'a plus besoin du paramètre key
et le nom de fichier peut être récupéré depuis le champs d'upload
2021-11-12 09:03:35 +01:00

145 lines
4.1 KiB
PHP

<?php
$f3 = require(__DIR__.'/vendor/fatfree/lib/base.php');
if(getenv("DEBUG")) {
$f3->set('DEBUG', getenv("DEBUG"));
}
$f3->set('ROOT', __DIR__);
$f3->set('UI', $f3->get('ROOT')."/templates/");
$f3->set('UPLOADS', sys_get_temp_dir()."/");
function convertPHPSizeToBytes($sSize)
{
//
$sSuffix = strtoupper(substr($sSize, -1));
if (!in_array($sSuffix,array('P','T','G','M','K'))){
return (int)$sSize;
}
$iValue = substr($sSize, 0, -1);
switch ($sSuffix) {
case 'P':
$iValue *= 1024;
// Fallthrough intended
case 'T':
$iValue *= 1024;
// Fallthrough intended
case 'G':
$iValue *= 1024;
// Fallthrough intended
case 'M':
$iValue *= 1024;
// Fallthrough intended
case 'K':
$iValue *= 1024;
break;
}
return (int)$iValue;
}
$f3->route('GET /',
function($f3) {
$f3->set('key', hash('md5', uniqid().rand()));
$f3->set('maxSize', min(array(convertPHPSizeToBytes(ini_get('post_max_size')), convertPHPSizeToBytes(ini_get('upload_max_filesize')))));
echo View::instance()->render('index.html.php');
}
);
$f3->route('GET /@key',
function($f3) {
$f3->set('key', $f3->get('PARAMS.key'));
echo View::instance()->render('pdf.html.php');
}
);
$f3->route('POST /image2svg',
function($f3) {
$files = Web::instance()->receive(function($file,$formFieldName){
if(strpos(Web::instance()->mime($file['tmp_name'], true), 'image/') !== 0) {
return false;
}
return true;
}, true, function($fileBaseName, $formFieldName) use ($f3) {
return basename(tempnam($f3->get('UPLOADS'), 'pdfsignature_image2svg'));
});
$imageFile = null;
foreach($files as $file => $valid) {
if(!$valid) {
continue;
}
$imageFile = $file;
}
if(!$imageFile) {
$f3->error(403);
}
shell_exec(sprintf("convert -background white -flatten %s %s", $imageFile, $imageFile.".bmp"));
shell_exec(sprintf("mkbitmap -x -f 8 %s -o %s", $imageFile.".bmp", $imageFile.".bpm"));
shell_exec(sprintf("potrace --svg %s -o %s", $imageFile.".bpm", $imageFile.".svg"));
header('Content-Type: image/svg+xml');
echo file_get_contents($imageFile.".svg");
if($f3->get('DEBUG')) {
return;
}
array_map('unlink', glob($imageFile."*"));
}
);
$f3->route('POST /sign',
function($f3) {
$filename = null;
$files = Web::instance()->receive(function($file,$formFieldName){
if(strpos(Web::instance()->mime($file['tmp_name'], true), 'application/pdf') !== 0) {
return false;
}
return true;
}, true, function($fileBaseName, $formFieldName) use ($f3, &$filename) {
$filename = str_replace(".pdf", "_signe.pdf", $fileBaseName);
$tmpfile = tempnam($f3->get('UPLOADS'), 'pdfsignature_pdf');
unlink($tmpfile);
return basename($tmpfile).".pdf";
});
$pdfFile = null;
foreach($files as $file => $valid) {
if(!$valid) {
continue;
}
$pdfFile = $file;
}
if(!$pdfFile) {
$f3->error(403);
}
$svgData = $_POST['svg'];
$svgFiles = "";
foreach($svgData as $index => $svgItem) {
$svgFile = $pdfFile.'_'.$index.'.svg';
file_put_contents($svgFile, $svgItem);
$svgFiles .= $svgFile . " ";
}
shell_exec(sprintf("rsvg-convert -f pdf -o %s %s", $pdfFile.'.svg.pdf', $svgFiles));
shell_exec(sprintf("pdftk %s multibackground %s output %s", $pdfFile.'.svg.pdf', $pdfFile, $pdfFile.'_signe.pdf'));
Web::instance()->send($pdfFile.'_signe.pdf', null, 0, TRUE, $filename);
if($f3->get('DEBUG')) {
return;
}
array_map('unlink', glob($pdfFile."*"));
}
);
return $f3;