2021-05-04 00:30:20 +02:00
|
|
|
<?php
|
|
|
|
|
2022-03-26 23:40:57 +01:00
|
|
|
$f3 = require(__DIR__.'/vendor/fatfree/base.php');
|
2021-09-21 23:44:34 +02:00
|
|
|
|
2021-10-31 22:20:18 +01:00
|
|
|
if(getenv("DEBUG")) {
|
|
|
|
$f3->set('DEBUG', getenv("DEBUG"));
|
|
|
|
}
|
|
|
|
|
2022-03-02 23:33:14 +01:00
|
|
|
$f3->set('XFRAME', null); // Allow use in an iframe
|
2021-09-21 23:44:34 +02:00
|
|
|
$f3->set('ROOT', __DIR__);
|
|
|
|
$f3->set('UI', $f3->get('ROOT')."/templates/");
|
2021-11-12 01:49:23 +01:00
|
|
|
$f3->set('UPLOADS', sys_get_temp_dir()."/");
|
2022-03-31 10:47:12 +02:00
|
|
|
$f3->set('STORAGE', sys_get_temp_dir()."/pdf/");
|
2021-05-04 00:30:20 +02:00
|
|
|
|
2021-11-12 02:24:28 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-05-04 00:30:20 +02:00
|
|
|
$f3->route('GET /',
|
|
|
|
function($f3) {
|
2022-03-26 11:02:47 +01:00
|
|
|
$f3->reroute('/signature');
|
2021-05-04 00:30:20 +02:00
|
|
|
}
|
|
|
|
);
|
2022-03-26 11:02:47 +01:00
|
|
|
$f3->route('GET /signature',
|
2021-05-04 00:30:20 +02:00
|
|
|
function($f3) {
|
2022-03-26 11:02:47 +01:00
|
|
|
$f3->set('maxSize', min(array(convertPHPSizeToBytes(ini_get('post_max_size')), convertPHPSizeToBytes(ini_get('upload_max_filesize')))));
|
2021-11-12 10:10:32 +01:00
|
|
|
$f3->set('maxPage', ini_get('max_file_uploads') - 1);
|
2021-11-11 10:06:42 +01:00
|
|
|
|
2022-03-26 11:02:47 +01:00
|
|
|
echo View::instance()->render('signature.html.php');
|
2021-09-21 23:44:34 +02:00
|
|
|
}
|
|
|
|
);
|
2022-03-31 11:46:03 +02:00
|
|
|
|
|
|
|
$f3->route('GET /signature/@hash',
|
|
|
|
function($f3, $param) {
|
|
|
|
$f3->set('hash', $param['hash']);
|
2022-03-31 16:35:26 +02:00
|
|
|
$port = $f3->get('PORT');
|
|
|
|
$f3->set('shareLink', $f3->set('urlbase', $f3->get('SCHEME').'://'.$_SERVER['SERVER_NAME'].(!in_array($port,[80,443])?(':'.$port):'').$f3->get('BASE')).$f3->get('URI'));
|
2022-03-31 11:46:03 +02:00
|
|
|
|
|
|
|
$f3->set('maxSize', min(array(convertPHPSizeToBytes(ini_get('post_max_size')), convertPHPSizeToBytes(ini_get('upload_max_filesize')))));
|
|
|
|
$f3->set('maxPage', ini_get('max_file_uploads') - 1);
|
|
|
|
|
|
|
|
echo View::instance()->render('signature.html.php');
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-03-28 00:56:00 +02:00
|
|
|
$f3->route('GET /organization',
|
|
|
|
function($f3) {
|
|
|
|
$f3->set('maxSize', min(array(convertPHPSizeToBytes(ini_get('post_max_size')), convertPHPSizeToBytes(ini_get('upload_max_filesize')))));
|
|
|
|
|
|
|
|
echo View::instance()->render('organization.html.php');
|
|
|
|
}
|
|
|
|
);
|
2021-09-20 00:10:22 +02:00
|
|
|
$f3->route('POST /image2svg',
|
|
|
|
function($f3) {
|
|
|
|
$files = Web::instance()->receive(function($file,$formFieldName){
|
2021-10-31 22:05:48 +01:00
|
|
|
if(strpos(Web::instance()->mime($file['tmp_name'], true), 'image/') !== 0) {
|
2021-09-20 00:10:22 +02:00
|
|
|
|
2021-10-31 22:05:48 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2021-11-11 09:28:13 +01:00
|
|
|
}, true, function($fileBaseName, $formFieldName) use ($f3) {
|
2021-09-20 00:10:22 +02:00
|
|
|
|
2021-11-11 09:28:13 +01:00
|
|
|
return basename(tempnam($f3->get('UPLOADS'), 'pdfsignature_image2svg'));
|
2021-10-31 22:05:48 +01:00
|
|
|
});
|
2021-09-20 00:10:22 +02:00
|
|
|
|
|
|
|
$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"));
|
2021-09-21 01:41:29 +02:00
|
|
|
shell_exec(sprintf("mkbitmap -x -f 8 %s -o %s", $imageFile.".bmp", $imageFile.".bpm"));
|
2021-09-20 00:10:22 +02:00
|
|
|
shell_exec(sprintf("potrace --svg %s -o %s", $imageFile.".bpm", $imageFile.".svg"));
|
|
|
|
|
|
|
|
header('Content-Type: image/svg+xml');
|
2021-09-27 15:19:35 +02:00
|
|
|
echo file_get_contents($imageFile.".svg");
|
2021-10-31 22:20:18 +01:00
|
|
|
|
|
|
|
if($f3->get('DEBUG')) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-12 01:49:23 +01:00
|
|
|
|
2021-10-31 22:05:48 +01:00
|
|
|
array_map('unlink', glob($imageFile."*"));
|
2021-09-20 00:10:22 +02:00
|
|
|
}
|
|
|
|
);
|
2021-11-12 09:03:35 +01:00
|
|
|
$f3->route('POST /sign',
|
2021-05-04 00:30:20 +02:00
|
|
|
function($f3) {
|
2021-11-12 09:03:35 +01:00
|
|
|
$filename = null;
|
2021-11-12 10:10:32 +01:00
|
|
|
$tmpfile = tempnam($f3->get('UPLOADS'), 'pdfsignature_sign');
|
|
|
|
unlink($tmpfile);
|
|
|
|
$svgFiles = "";
|
|
|
|
|
|
|
|
|
2021-11-12 01:49:23 +01:00
|
|
|
$files = Web::instance()->receive(function($file,$formFieldName){
|
2021-11-12 10:10:32 +01:00
|
|
|
if($formFieldName == "pdf" && strpos(Web::instance()->mime($file['tmp_name'], true), 'application/pdf') !== 0) {
|
|
|
|
$f3->error(403);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($formFieldName == "svg" && strpos(Web::instance()->mime($file['tmp_name'], true), 'image/svg+xml') !== 0) {
|
|
|
|
$f3->error(403);
|
2021-11-12 01:49:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2021-11-12 10:10:32 +01:00
|
|
|
}, false, function($fileBaseName, $formFieldName) use ($f3, $tmpfile, &$filename, &$svgFiles) {
|
|
|
|
if($formFieldName == "pdf") {
|
|
|
|
$filename = str_replace(".pdf", "_signe.pdf", $fileBaseName);
|
|
|
|
return basename($tmpfile).".pdf";
|
|
|
|
}
|
2021-11-12 01:49:23 +01:00
|
|
|
|
2021-11-12 10:10:32 +01:00
|
|
|
if($formFieldName == "svg") {
|
|
|
|
$svgFiles .= " ".$tmpfile."_".$fileBaseName;
|
|
|
|
|
|
|
|
return basename($tmpfile."_".$fileBaseName);
|
2021-11-12 01:49:23 +01:00
|
|
|
}
|
2021-11-12 10:10:32 +01:00
|
|
|
});
|
2021-11-12 01:49:23 +01:00
|
|
|
|
2021-11-12 10:10:32 +01:00
|
|
|
if(!is_file($tmpfile.".pdf")) {
|
2021-11-12 01:49:23 +01:00
|
|
|
$f3->error(403);
|
|
|
|
}
|
|
|
|
|
2021-11-12 10:10:32 +01:00
|
|
|
if(!$svgFiles) {
|
|
|
|
$f3->error(403);
|
2021-09-19 02:17:13 +02:00
|
|
|
}
|
|
|
|
|
2021-11-12 10:10:32 +01:00
|
|
|
shell_exec(sprintf("rsvg-convert -f pdf -o %s %s", $tmpfile.'.svg.pdf', $svgFiles));
|
2022-04-01 00:00:05 +02:00
|
|
|
shell_exec(sprintf("pdftk %s multistamp %s output %s", $tmpfile.".pdf", $tmpfile.'.svg.pdf', $tmpfile.'_signe.pdf'));
|
2021-10-31 22:05:48 +01:00
|
|
|
|
2021-11-12 10:10:32 +01:00
|
|
|
Web::instance()->send($tmpfile.'_signe.pdf', null, 0, TRUE, $filename);
|
2021-10-31 22:05:48 +01:00
|
|
|
|
2021-10-31 22:20:18 +01:00
|
|
|
if($f3->get('DEBUG')) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-12 10:10:32 +01:00
|
|
|
array_map('unlink', glob($tmpfile."*"));
|
2021-05-04 00:30:20 +02:00
|
|
|
}
|
|
|
|
);
|
2021-09-25 15:00:12 +02:00
|
|
|
|
2022-03-31 11:46:03 +02:00
|
|
|
$f3->route('POST /share',
|
|
|
|
function($f3) {
|
|
|
|
$hash = substr(hash('sha512', uniqid().rand()), 0, 20);
|
|
|
|
$sharingFolder = $f3->get('STORAGE').$hash."/";
|
|
|
|
$f3->set('UPLOADS', $sharingFolder);
|
|
|
|
mkdir($sharingFolder);
|
|
|
|
$filename = "original.pdf";
|
2022-03-31 18:37:30 +02:00
|
|
|
$tmpfile = tempnam($sharingFolder, date('YmdHis'));
|
|
|
|
$svgFiles = "";
|
2022-03-31 11:46:03 +02:00
|
|
|
|
|
|
|
$files = Web::instance()->receive(function($file,$formFieldName){
|
2022-03-31 18:37:30 +02:00
|
|
|
if($formFieldName == "pdf" && strpos(Web::instance()->mime($file['tmp_name'], true), 'application/pdf') !== 0) {
|
|
|
|
$f3->error(403);
|
|
|
|
}
|
|
|
|
if($formFieldName == "svg" && strpos(Web::instance()->mime($file['tmp_name'], true), 'image/svg+xml') !== 0) {
|
2022-03-31 11:46:03 +02:00
|
|
|
$f3->error(403);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2022-03-31 18:37:30 +02:00
|
|
|
}, false, function($fileBaseName, $formFieldName) use ($tmpfile, $filename, &$svgFiles) {
|
|
|
|
if($formFieldName == "pdf") {
|
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
if($formFieldName == "svg") {
|
|
|
|
$svgFiles .= " ".$tmpfile."_".$fileBaseName;
|
|
|
|
return basename($tmpfile."_".$fileBaseName);
|
|
|
|
}
|
2022-03-31 11:46:03 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if(!count($files)) {
|
|
|
|
$f3->error(403);
|
|
|
|
}
|
2022-03-31 18:37:30 +02:00
|
|
|
|
|
|
|
if(!$svgFiles) {
|
|
|
|
$f3->error(403);
|
|
|
|
}
|
|
|
|
|
|
|
|
shell_exec(sprintf("rsvg-convert -f pdf -o %s %s", $tmpfile.'.svg.pdf', $svgFiles));
|
|
|
|
|
|
|
|
if(!$f3->get('DEBUG')) {
|
|
|
|
array_map('unlink', $svgFiles);
|
|
|
|
}
|
|
|
|
|
2022-03-31 19:29:39 +02:00
|
|
|
$f3->reroute('/signature/'.$hash."#informations");
|
2022-03-31 11:46:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
);
|
|
|
|
|
2022-03-28 00:56:00 +02:00
|
|
|
$f3->route('POST /organize',
|
|
|
|
function($f3) {
|
|
|
|
$filename = null;
|
|
|
|
$tmpfile = tempnam($f3->get('UPLOADS'), 'pdfsignature_organize');
|
|
|
|
unlink($tmpfile);
|
2022-03-30 02:00:25 +02:00
|
|
|
$pages = explode(',', $f3->get('POST.pages'));
|
2022-03-28 00:56:00 +02:00
|
|
|
|
|
|
|
$files = Web::instance()->receive(function($file,$formFieldName){
|
|
|
|
if($formFieldName == "pdf" && strpos(Web::instance()->mime($file['tmp_name'], true), 'application/pdf') !== 0) {
|
|
|
|
$f3->error(403);
|
|
|
|
}
|
|
|
|
return true;
|
2022-03-30 02:00:25 +02:00
|
|
|
}, false, function($fileBaseName, $formFieldName) use ($f3, $tmpfile, &$filename, $pages) {
|
2022-03-28 00:56:00 +02:00
|
|
|
if($formFieldName == "pdf") {
|
2022-03-30 02:00:25 +02:00
|
|
|
$filename = str_replace(".pdf", "_page_".implode("-", $pages).".pdf", $fileBaseName);
|
2022-03-28 00:56:00 +02:00
|
|
|
return basename($tmpfile).".pdf";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if(!is_file($tmpfile.".pdf")) {
|
|
|
|
$f3->error(403);
|
|
|
|
}
|
|
|
|
|
2022-03-30 02:00:25 +02:00
|
|
|
shell_exec(sprintf("pdftk %s cat %s output %s", $tmpfile.".pdf", implode(" ", $pages), $tmpfile.'_organize.pdf'));
|
2022-03-28 00:56:00 +02:00
|
|
|
|
2022-03-30 02:00:25 +02:00
|
|
|
Web::instance()->send($tmpfile."_organize.pdf", null, 0, TRUE, $filename);
|
2022-03-28 00:56:00 +02:00
|
|
|
|
|
|
|
if($f3->get('DEBUG')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
array_map('unlink', glob($tmpfile."*"));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-03-31 12:15:26 +02:00
|
|
|
$f3->route('GET /signature/@hash/pdf',
|
|
|
|
function($f3) {
|
2022-03-31 18:38:12 +02:00
|
|
|
$sharingFolder = $f3->get('STORAGE').$f3->get('PARAMS.hash');
|
|
|
|
$files = scandir($sharingFolder);
|
|
|
|
$originalFile = $sharingFolder.'/original.pdf';
|
|
|
|
$finalFile = $sharingFolder.'/'.$f3->get('PARAMS.hash').'.pdf';
|
2022-03-31 12:15:26 +02:00
|
|
|
$layers = [];
|
|
|
|
foreach($files as $file) {
|
2022-03-31 14:55:45 +02:00
|
|
|
if(strpos($file, 'svg.pdf') !== false) {
|
2022-03-31 18:38:12 +02:00
|
|
|
$layers[] = $sharingFolder.'/'.$file;
|
2022-03-31 12:15:26 +02:00
|
|
|
}
|
|
|
|
}
|
2022-03-31 15:00:42 +02:00
|
|
|
if (!$layers) {
|
2022-03-31 18:38:12 +02:00
|
|
|
Web::instance()->send($originalFile, null, 0, TRUE, $f3->get('PARAMS.hash').'.pdf');
|
2022-03-31 14:58:27 +02:00
|
|
|
}
|
2022-03-31 18:38:12 +02:00
|
|
|
$bufferFile = str_replace('.pdf', '_tmp.pdf', $originalFile);
|
|
|
|
shell_exec(sprintf("cp %s %s", $originalFile, $finalFile));
|
|
|
|
foreach($layers as $layer) {
|
2022-04-01 00:00:05 +02:00
|
|
|
shell_exec(sprintf("pdftk %1\$s multistamp %2\$s output %3\$s && mv %3\$s %1\$s", $finalFile, $layer, $bufferFile));
|
2022-03-31 14:59:48 +02:00
|
|
|
}
|
2022-03-31 18:38:12 +02:00
|
|
|
Web::instance()->send($finalFile, null, 0, TRUE, $f3->get('PARAMS.hash').'.pdf');
|
2022-03-31 12:15:26 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-03-31 16:05:45 +02:00
|
|
|
$f3->route('POST /signature/@hash/save',
|
|
|
|
function($f3) {
|
2022-03-31 18:38:12 +02:00
|
|
|
$sharingFolder = $f3->get('STORAGE').$f3->get('PARAMS.hash').'/';
|
|
|
|
$f3->set('UPLOADS', $sharingFolder);
|
|
|
|
$tmpfile = tempnam($sharingFolder, date('YmdHis'));
|
2022-03-31 16:05:45 +02:00
|
|
|
unlink($tmpfile);
|
|
|
|
$svgFiles = "";
|
|
|
|
|
|
|
|
|
|
|
|
$files = Web::instance()->receive(function($file,$formFieldName){
|
|
|
|
if($formFieldName == "svg" && strpos(Web::instance()->mime($file['tmp_name'], true), 'image/svg+xml') !== 0) {
|
|
|
|
$f3->error(403);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}, false, function($fileBaseName, $formFieldName) use ($f3, $tmpfile, &$svgFiles) {
|
|
|
|
if($formFieldName == "svg") {
|
|
|
|
$svgFiles .= " ".$tmpfile."_".$fileBaseName;
|
|
|
|
return basename($tmpfile."_".$fileBaseName);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if(!$svgFiles) {
|
|
|
|
$f3->error(403);
|
|
|
|
}
|
|
|
|
|
|
|
|
shell_exec(sprintf("rsvg-convert -f pdf -o %s %s", $tmpfile.'.svg.pdf', $svgFiles));
|
|
|
|
|
|
|
|
if(!$f3->get('DEBUG')) {
|
|
|
|
array_map('unlink', $svgFiles);
|
|
|
|
}
|
|
|
|
|
2022-03-31 19:29:39 +02:00
|
|
|
$f3->reroute('/signature/'.$f3->get('PARAMS.hash')."#signed");
|
2022-03-31 12:15:26 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return $f3;
|