diff --git a/lib/GPGCryptography.class.php b/lib/GPGCryptography.class.php index 0cae24c..5df6c54 100644 --- a/lib/GPGCryptography.class.php +++ b/lib/GPGCryptography.class.php @@ -57,21 +57,6 @@ public function decryptFile($file) { return $decryptTmpFile; } - public function decrypt() { - if (!$this->isEncrypted()) { - return $this->pathHash; - } - if (!$this->symmetricKey) { - return false; - } - $decryptFolder = sys_get_temp_dir()."/".uniqid('pdfsignature.decrypted.'.getmypid(), true); - mkdir($decryptFolder); - foreach ($this->getFiles(true) as $file) { - $this->runDecryptFile($file, $decryptFolder."/".str_replace(".gpg", "", basename($file))); - } - return $decryptFolder; - } - public function runDecryptFile($file, $outputFile) { putenv('HOME='.sys_get_temp_dir()); return shell_exec("gpg --batch --passphrase $this->symmetricKey --decrypt -o $outputFile $file > /dev/null"); diff --git a/lib/PDFSignature.class.php b/lib/PDFSignature.class.php index d5fcde9..ccd7021 100644 --- a/lib/PDFSignature.class.php +++ b/lib/PDFSignature.class.php @@ -45,13 +45,17 @@ public function isEncrypted() { public function getDecryptFile($file) { if(!$this->isEncrypted()) { + + return $file; + } + $file = preg_replace("/\.gpg$/", "", $file); + if(file_exists($file)) { return $file; } if(array_key_exists($file, $this->cacheDecryptFiles)) { return $this->cacheDecryptFiles[$file]; } - $decryptFile = $this->gpg->decryptFile($file); $this->toClean[] = $decryptFile; $this->cacheDecryptFiles[$file] = $decryptFile; @@ -103,35 +107,22 @@ public function compile() { } if($this->isCompileLock()) { - usleep(10000); return $this->compile(); } $this->lockCompile(); - $pathHashDecrypted = $this->gpg->decrypt(); - - if ($pathHashDecrypted == false) { - throw new Exception("PDF file could not be decrypted. Cookie encryption key might be missing."); - } - if ($this->pathHash != $pathHashDecrypted && $this->isEncrypted()) { - $this->toClean[] = $pathHashDecrypted; - } - - $layers = $this->getLayers($pathHashDecrypted); - $currentSignedFile = $pathHashDecrypted.'/original.pdf'; + $layers = $this->getLayers($this->pathHash); + $currentSignedFile = $this->pathHash.'/original.pdf'; $signedFileToCopy = []; foreach($layers as $layerFile) { $signedFile = str_replace('.svg.pdf', '.sign.pdf', $layerFile); if(!file_exists($signedFile)) { - self::addSvgToPDF($currentSignedFile, $layerFile, $signedFile, false); - if ($this->pathHash != $pathHashDecrypted && $this->isEncrypted()) { - copy($signedFile, str_replace($pathHashDecrypted ,$this->pathHash, $signedFile)); - } + $signedFile = preg_replace("/\.gpg$/", '', $signedFile); + self::addSvgToPDF($this->getDecryptFile($currentSignedFile), $this->getDecryptFile($layerFile), $signedFile, false); } $currentSignedFile = $signedFile; } - copy($currentSignedFile, $this->pathHash.'/final.pdf'); if($this->isEncrypted()) { @@ -159,6 +150,9 @@ public function getLayers($pathHash = null) { if(is_null($pathHash)) { $pathHash = $this->pathHash; } + if(!file_exists($pathHash)) { + return []; + } $files = scandir($pathHash); $layers = []; foreach($files as $file) {