Skip to content

Commit

Permalink
Délocalisation du code pour générer un pdf dans une classe
Browse files Browse the repository at this point in the history
  • Loading branch information
wincelau committed Jul 31, 2024
1 parent b75be48 commit 5ace25b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 40 deletions.
48 changes: 8 additions & 40 deletions app.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

setlocale(LC_ALL, "");
require(__DIR__.'/lib/GPGCryptography.class.php');
require(__DIR__.'/lib/PDFSignature.class.php');

$f3 = require(__DIR__.'/vendor/fatfree/base.php');

Expand Down Expand Up @@ -256,7 +257,7 @@ function($f3) {
$symmetricKey = "#" . $_COOKIE[$hash];
$encryptor = new GPGCryptography($_COOKIE[$hash], $f3->get('PDF_STORAGE_PATH').$hash);
if (!$encryptor->encrypt()) {
shell_exec("rm -rf $sharingFolder");
GPGCryptography::hardUnlink($sharingFolder);
$f3->error(500);
}
}
Expand All @@ -272,50 +273,17 @@ function($f3) {
function($f3) {
$f3->set('activeTab', 'sign');
$hash = Web::instance()->slug($f3->get('PARAMS.hash'));
$symmetricKey = null;
if (isset($_COOKIE[$hash])) {
$symmetricKey = GPGCryptography::protectSymmetricKey($_COOKIE[$hash]);
}

$cryptor = new GPGCryptography($symmetricKey, $f3->get('PDF_STORAGE_PATH').$hash);
$sharingFolder = $cryptor->decrypt();
if ($sharingFolder == false) {
$f3->error(500, "PDF file could not be decrypted. Cookie encryption key might be missing.");
}
$symmetricKey = (isset($_COOKIE[$hash])) ? GPGCryptography::protectSymmetricKey($_COOKIE[$hash]) : null;

$files = scandir($sharingFolder);
$originalFile = $sharingFolder.'/original.pdf';
$finalFile = $sharingFolder.'/'.$f3->get('PARAMS.hash').uniqid().'.pdf';
$filename = $f3->get('PARAMS.hash').'.pdf';
if(file_exists($sharingFolder."/filename.txt")) {
$filename = file_get_contents($sharingFolder."/filename.txt");
}
$layers = [];
foreach($files as $file) {
if(strpos($file, 'svg.pdf') !== false) {
$layers[] = $sharingFolder.'/'.$file;
}
}
if (!$layers) {
Web::instance()->send($originalFile, null, 0, TRUE, $filename);
}
$filename = str_replace('.pdf', '_signe-'.count($layers).'x.pdf', $filename);
copy($originalFile, $finalFile);
$bufferFile = $finalFile.".tmp";
foreach($layers as $layerFile) {
shell_exec(sprintf("pdftk %s multistamp %s output %s", $finalFile, $layerFile, $bufferFile));
rename($bufferFile, $finalFile);
}
Web::instance()->send($finalFile, null, 0, TRUE, $filename);
$pdfSignature = new PDFSignature($f3->get('PDF_STORAGE_PATH').$hash, $symmetricKey);
$pdf = $pdfSignature->getPDF();
Web::instance()->send($pdf[0], null, 0, TRUE, $pdf[1]);

if($f3->get('DEBUG')) {
return;
}
if ($f3->get('PDF_STORAGE_PATH') != $sharingFolder && $cryptor->isEncrypted()) {
GPGCryptography::hardUnlink($sharingFolder);
} else {
array_map('unlink', glob($finalFile."*"));
}

$pdfSignature->clean();
}
);

Expand Down
64 changes: 64 additions & 0 deletions lib/PDFSignature.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

class PDFSignature
{
protected $symmetricKey = null;
protected $pathHash = null;
protected $hash = null;
protected $gpg = null;
protected $toClean = [];

public function __construct($pathHash, $symmetricKey = null) {
$this->symmetricKey = $symmetricKey;
$this->pathHash = $pathHash;
$this->hash = basename($this->pathHash);
$this->gpg = new GPGCryptography($symmetricKey, $pathHash);
}

public function getPDF() {
$sharingFolder = $this->gpg->decrypt();
if ($sharingFolder == false) {
throw new Exception( "PDF file could not be decrypted. Cookie encryption key might be missing.");
}
if ($this->pathHash != $sharingFolder && $this->gpg->isEncrypted()) {
$this->toClean[] = $sharingFolder;
}
$files = scandir($sharingFolder);
$originalFile = $sharingFolder.'/original.pdf';
$finalFile = $sharingFolder.'/'.$this->hash.uniqid().'.pdf';
$filename = $this->hash.'.pdf';
if(file_exists($sharingFolder."/filename.txt")) {
$filename = file_get_contents($sharingFolder."/filename.txt");
}
$layers = [];
foreach($files as $file) {
if(strpos($file, 'svg.pdf') !== false) {
$layers[] = $sharingFolder.'/'.$file;
}
}
if(!count($layers)) {
return [$originalFile, $filename];
}

$filename = str_replace('.pdf', '_signe-'.count($layers).'x.pdf', $filename);
copy($originalFile, $finalFile);
$bufferFile = $finalFile.".tmp";
foreach($layers as $layerFile) {
shell_exec(sprintf("pdftk %s multistamp %s output %s", $finalFile, $layerFile, $bufferFile));
rename($bufferFile, $finalFile);
}

if ($this->pathHash == $sharingFolder && !$this->gpg->isEncrypted()) {
$this->toClean[] = $finalFile;
}

return [$finalFile, $filename];
}

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

}

0 comments on commit 5ace25b

Please sign in to comment.