2021-05-04 00:30:20 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
$f3 = require(__DIR__.'/vendor/fatfree/lib/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"));
|
|
|
|
}
|
|
|
|
|
2021-09-21 23:44:34 +02:00
|
|
|
$f3->set('ROOT', __DIR__);
|
|
|
|
$f3->set('UI', $f3->get('ROOT')."/templates/");
|
2021-05-04 00:30:20 +02:00
|
|
|
$f3->set('UPLOADS', $f3->get('ROOT').'/data/');
|
|
|
|
|
|
|
|
if(!is_dir($f3->get('UPLOADS'))) {
|
|
|
|
mkdir($f3->get('UPLOADS'));
|
|
|
|
}
|
|
|
|
$f3->route('GET /',
|
|
|
|
function($f3) {
|
|
|
|
$f3->set('key', $f3->get('PARAMS.key'));
|
2021-09-21 23:44:34 +02:00
|
|
|
echo View::instance()->render('index.html.php');
|
2021-05-04 00:30:20 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
$f3->route('POST /upload',
|
|
|
|
function($f3) {
|
2021-11-01 02:28:18 +01:00
|
|
|
$fileName = null;
|
2021-05-04 00:30:20 +02:00
|
|
|
$files = Web::instance()->receive(function($file,$formFieldName){
|
2021-10-31 22:05:48 +01:00
|
|
|
if(Web::instance()->mime($file['tmp_name'], true) != 'application/pdf') {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if($file['size'] > (20 * 1024 * 1024)) { // if bigger than 20 MB
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2021-11-01 02:28:18 +01:00
|
|
|
}, true, function($fileBaseName, $formFieldName) use (&$fileName) {
|
|
|
|
$fileName = $fileBaseName;
|
2021-10-31 22:05:48 +01:00
|
|
|
return substr(hash('sha256', $fileBaseName.uniqid().mt_rand()), 0, 24).".pdf";
|
|
|
|
});
|
|
|
|
|
2021-05-04 00:30:20 +02:00
|
|
|
foreach($files as $file => $valid) {
|
|
|
|
if(!$valid) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-10-31 22:05:48 +01:00
|
|
|
|
|
|
|
$key = str_replace(".pdf", "", basename($file));
|
2021-05-04 00:30:20 +02:00
|
|
|
}
|
2021-10-31 22:05:48 +01:00
|
|
|
|
2021-05-04 00:30:20 +02:00
|
|
|
if(!$key) {
|
|
|
|
$f3->error(403);
|
|
|
|
}
|
|
|
|
|
2021-11-01 02:28:18 +01:00
|
|
|
if($fileName) {
|
|
|
|
$f3->set('SESSION.fileName', $fileName);
|
|
|
|
}
|
|
|
|
|
2021-05-04 00:30:20 +02:00
|
|
|
return $f3->reroute('/'.$key);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$f3->route('GET /@key',
|
|
|
|
function($f3) {
|
|
|
|
$f3->set('key', $f3->get('PARAMS.key'));
|
2021-11-01 02:28:18 +01:00
|
|
|
if($f3->get('SESSION.fileName')) {
|
|
|
|
$f3->set('fileName', $f3->get('SESSION.fileName'));
|
|
|
|
$f3->clear('SESSION.fileName');
|
|
|
|
}
|
2021-09-21 23:44:34 +02:00
|
|
|
echo View::instance()->render('pdf.html.php');
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$f3->route('GET /@key/pdf',
|
|
|
|
function($f3) {
|
|
|
|
$key = $f3->get('PARAMS.key');
|
|
|
|
Web::instance()->send($f3->get('UPLOADS').$key.'.pdf');
|
2021-05-04 00:30:20 +02:00
|
|
|
}
|
|
|
|
);
|
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;
|
|
|
|
}
|
|
|
|
if($file['size'] > (20 * 1024 * 1024)) { // if bigger than 20 MB
|
|
|
|
|
|
|
|
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-10-31 22:05:48 +01:00
|
|
|
array_map('unlink', glob($imageFile."*"));
|
2021-09-20 00:10:22 +02:00
|
|
|
}
|
|
|
|
);
|
2021-05-04 00:30:20 +02:00
|
|
|
$f3->route('POST /@key/save',
|
|
|
|
function($f3) {
|
|
|
|
$key = $f3->get('PARAMS.key');
|
|
|
|
$svgData = $_POST['svg'];
|
2021-11-01 02:28:18 +01:00
|
|
|
$filename = null;
|
|
|
|
if(isset($_POST['filename']) && $_POST['filename']) {
|
|
|
|
$filename = str_replace(".pdf", "_signe.pdf", $_POST['filename']);
|
|
|
|
}
|
2021-09-19 02:17:13 +02:00
|
|
|
|
|
|
|
$svgFiles = "";
|
|
|
|
foreach($svgData as $index => $svgItem) {
|
2021-09-21 23:44:34 +02:00
|
|
|
$svgFile = $f3->get('UPLOADS').$key.'_'.$index.'.svg';
|
2021-09-19 02:17:13 +02:00
|
|
|
file_put_contents($svgFile, $svgItem);
|
|
|
|
$svgFiles .= $svgFile . " ";
|
|
|
|
}
|
|
|
|
|
2021-09-21 23:44:34 +02:00
|
|
|
shell_exec(sprintf("rsvg-convert -f pdf -o %s %s", $f3->get('UPLOADS').$key.'.svg.pdf', $svgFiles));
|
|
|
|
shell_exec(sprintf("pdftk %s multibackground %s output %s", $f3->get('UPLOADS').$key.'.svg.pdf', $f3->get('UPLOADS').$key.'.pdf', $f3->get('UPLOADS').$key.'_signe.pdf'));
|
2021-10-31 22:05:48 +01:00
|
|
|
|
2021-11-01 02:28:18 +01:00
|
|
|
Web::instance()->send($f3->get('UPLOADS').$key.'_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-10-31 22:05:48 +01:00
|
|
|
array_map('unlink', glob($f3->get('UPLOADS').$key."_*.svg"));
|
|
|
|
unlink($f3->get('UPLOADS').$key.'.svg.pdf');
|
|
|
|
unlink($f3->get('UPLOADS').$key.'_signe.pdf');
|
2021-05-04 00:30:20 +02:00
|
|
|
}
|
|
|
|
);
|
2021-09-25 15:00:12 +02:00
|
|
|
|
|
|
|
return $f3;
|