Skip to content

Commit

Permalink
addSignature in model
Browse files Browse the repository at this point in the history
  • Loading branch information
wincelau committed Jul 31, 2024
1 parent 5ace25b commit e0a7a0a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
33 changes: 12 additions & 21 deletions app.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ function($f3) {
touch($expireFile, date_format(date_modify(date_create(), file_get_contents($expireFile)), 'U'));
$filename = "original.pdf";
$tmpfile = tempnam($sharingFolder, date('YmdHis'));
unlink($tmpfile);
$svgFiles = "";
$files = Web::instance()->receive(function($file,$formFieldName){
if($formFieldName == "pdf" && strpos(Web::instance()->mime($file['tmp_name'], true), 'application/pdf') !== 0) {
Expand Down Expand Up @@ -290,47 +291,37 @@ function($f3) {
$f3->route('POST /signature/@hash/save',
function($f3) {
$hash = Web::instance()->slug($f3->get('PARAMS.hash'));
$sharingFolder = $f3->get('PDF_STORAGE_PATH').$hash;
$f3->set('UPLOADS', $sharingFolder.'/');
$tmpfile = tempnam($sharingFolder, date('YmdHis'));
unlink($tmpfile);
$svgFiles = "";

$expireFile = $sharingFolder.".expire";
touch($expireFile, date_format(date_modify(date_create(), file_get_contents($expireFile)), 'U'));
$symmetricKey = (isset($_COOKIE[$hash])) ? GPGCryptography::protectSymmetricKey($_COOKIE[$hash]) : null;

$f3->set('UPLOADS', $f3->get('PDF_STORAGE_PATH').$hash."/");
$tmpfile = tempnam($f3->get('PDF_STORAGE_PATH').$hash, date('YmdHis'));
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;
$svgFiles[] = $tmpfile."_".$fileBaseName;
return basename($tmpfile."_".$fileBaseName);
}
});

if(!$svgFiles) {
if(!count($svgFiles)) {
$f3->error(403);
}

shell_exec(sprintf("rsvg-convert -f pdf -o %s %s", $tmpfile.'.svg.pdf', $svgFiles));
$pdfSignature = new PDFSignature($f3->get('PDF_STORAGE_PATH').$hash, $symmetricKey);
$pdfSignature->addSignature($svgFiles, $tmpfile."svg.pdf");

if(!$f3->get('DEBUG')) {
array_map('unlink', explode(' ', trim($svgFiles)));
}

$symmetricKey = "";
if (isset($_COOKIE[$hash])) {
$symmetricKey = "#" . $_COOKIE[$hash];
$encryptor = new GPGCryptography($_COOKIE[$hash], $f3->get('PDF_STORAGE_PATH').$hash);
$encryptor->encrypt();
$pdfSignature->clean();
}

\Flash::instance()->setKey('openModal', 'signed');

$f3->reroute($f3->get('REVERSE_PROXY_URL').'/signature/'.$hash.$symmetricKey);
$f3->reroute($f3->get('REVERSE_PROXY_URL').'/signature/'.$hash."#".$symmetricKey);
}
);

Expand Down
12 changes: 12 additions & 0 deletions lib/PDFSignature.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ public function getPDF() {
return [$finalFile, $filename];
}

public function addSignature(array $svgFiles, $outputPdfFile) {
$expireFile = $this->pathHash.".expire";
touch($expireFile, date_format(date_modify(date_create(), file_get_contents($expireFile)), 'U'));

shell_exec(sprintf("rsvg-convert -f pdf -o %s %s", $outputPdfFile, implode(" ", $svgFiles)));

if($this->gpg->isEncrypted()) {
$this->gpg->encrypt();
}
$this->toClean = array_merge($this->toClean, $svgFiles);
}

public function clean() {
foreach($this->toClean as $path) {
GPGCryptography::hardUnlink($path);
Expand Down

0 comments on commit e0a7a0a

Please sign in to comment.