From 5ace25b3b8e74927bff68060fa80678f7c0fb9d4 Mon Sep 17 00:00:00 2001 From: Vincent LAURENT Date: Wed, 31 Jul 2024 11:04:49 +0200 Subject: [PATCH] =?UTF-8?q?D=C3=A9localisation=20du=20code=20pour=20g?= =?UTF-8?q?=C3=A9n=C3=A9rer=20un=20pdf=20dans=20une=20classe?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.php | 48 +++++----------------------- lib/PDFSignature.class.php | 64 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 40 deletions(-) create mode 100644 lib/PDFSignature.class.php diff --git a/app.php b/app.php index 9d0bb24..16a4d11 100644 --- a/app.php +++ b/app.php @@ -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'); @@ -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); } } @@ -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(); } ); diff --git a/lib/PDFSignature.class.php b/lib/PDFSignature.class.php new file mode 100644 index 0000000..e0c8848 --- /dev/null +++ b/lib/PDFSignature.class.php @@ -0,0 +1,64 @@ +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); + } + } + +}