1
0
Fork 0
mirror of https://github.com/24eme/signaturepdf synced 2026-03-14 13:55:44 +01:00

Méthode plus indépendante

This commit is contained in:
Vincent LAURENT 2024-08-01 10:50:54 +02:00
commit 9e223624ef
2 changed files with 35 additions and 24 deletions

View file

@ -281,8 +281,7 @@ $f3->route('GET /signature/@hash/pdf',
$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;

View file

@ -15,7 +15,6 @@ class PDFSignature
$this->gpg = new GPGCryptography($symmetricKey, $pathHash);
}
public function createShare($duration) {
mkdir($this->pathHash);
$expireFile = $this->pathHash.".expire";
@ -30,31 +29,22 @@ class PDFSignature
}
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(!count($layers)) {
return [$originalFile, $filename];
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 = str_replace('.pdf', '_signe-'.count($layers).'x.pdf', $filename);
copy($originalFile, $finalFile);
$bufferFile = $finalFile.".tmp";
foreach($layers as $layerFile) {
@ -62,11 +52,33 @@ class PDFSignature
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) {