From 9e223624ef417730945a7f34e5ff70434c05cb89 Mon Sep 17 00:00:00 2001 From: Vincent LAURENT Date: Thu, 1 Aug 2024 10:50:54 +0200 Subject: [PATCH] =?UTF-8?q?M=C3=A9thode=20plus=20ind=C3=A9pendante?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.php | 3 +- lib/PDFSignature.class.php | 56 +++++++++++++++++++++++--------------- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/app.php b/app.php index 997beab..480ba32 100644 --- a/app.php +++ b/app.php @@ -281,8 +281,7 @@ function($f3) { $symmetricKey = (isset($_COOKIE[$hash])) ? GPGCryptography::protectSymmetricKey($_COOKIE[$hash]) : null; $pdfSignature = new PDFSignature($f3->get('PDF_STORAGE_PATH').$hash, $symmetricKey); - $pdf = $pdfSignature->getPDF(); - Web::instance()->send($pdf[0], null, 0, TRUE, $pdf[1]); + Web::instance()->send($pdfSignature->getPDF(), null, 0, TRUE, $pdfSignature->getPublicFilename()); if($f3->get('DEBUG')) { return; diff --git a/lib/PDFSignature.class.php b/lib/PDFSignature.class.php index 7244893..b192a1c 100644 --- a/lib/PDFSignature.class.php +++ b/lib/PDFSignature.class.php @@ -15,7 +15,6 @@ public function __construct($pathHash, $symmetricKey = null) { $this->gpg = new GPGCryptography($symmetricKey, $pathHash); } - public function createShare($duration) { mkdir($this->pathHash); $expireFile = $this->pathHash.".expire"; @@ -30,31 +29,22 @@ public function saveShare() { } public function getPDF() { - $sharingFolder = $this->gpg->decrypt(); - if ($sharingFolder == false) { + $originalPathHash = $this->pathHash; + $this->pathHash = $this->gpg->decrypt(); + if ($this->pathHash == 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 ($this->pathHash != $originalPathHash && $this->gpg->isEncrypted()) { + $this->toClean[] = $this->pathHash; } + + $originalFile = $this->pathHash.'/original.pdf'; + $finalFile = $this->pathHash.'/'.$this->hash.uniqid().'.pdf'; + $layers = $this->getLayers(); if(!count($layers)) { - return [$originalFile, $filename]; + return $originalFile; } - $filename = str_replace('.pdf', '_signe-'.count($layers).'x.pdf', $filename); copy($originalFile, $finalFile); $bufferFile = $finalFile.".tmp"; foreach($layers as $layerFile) { @@ -62,11 +52,33 @@ public function getPDF() { rename($bufferFile, $finalFile); } - if ($this->pathHash == $sharingFolder && !$this->gpg->isEncrypted()) { + if ($this->pathHash == $originalPathHash && !$this->gpg->isEncrypted()) { $this->toClean[] = $finalFile; } - return [$finalFile, $filename]; + return $finalFile; + } + + public function getPublicFilename() { + $filename = $this->hash.'.pdf'; + if(file_exists($this->pathHash."/filename.txt")) { + $filename = file_get_contents($this->pathHash."/filename.txt"); + } + + $filename = str_replace('.pdf', '_signe-'.count($this->getLayers()).'x.pdf', $filename); + + return $filename; + } + + protected function getLayers() { + $files = scandir($this->pathHash); + $layers = []; + foreach($files as $file) { + if(strpos($file, '.svg.pdf') !== false) { + $layers[] = $this->pathHash.'/'.$file; + } + } + return $layers; } public function addSignature(array $svgFiles, $outputPdfFile) {