Skip to content

Commit

Permalink
La compilation du pdf signée ne déchiffre que les pdf nécessaire pour
Browse files Browse the repository at this point in the history
éviter les lenteurs quand il y a beaucoup de signatures
  • Loading branch information
wincelau committed Sep 15, 2024
1 parent f040c88 commit a23e39d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 33 deletions.
15 changes: 0 additions & 15 deletions lib/GPGCryptography.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
30 changes: 12 additions & 18 deletions lib/PDFSignature.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a23e39d

Please sign in to comment.