Skip to content

Commit

Permalink
Merge branch 'master' of github.com:24eme/signaturepdf
Browse files Browse the repository at this point in the history
  • Loading branch information
tale-fau committed Aug 1, 2024
2 parents f6260ee + 1252fc6 commit 0d727e8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
3 changes: 1 addition & 2 deletions app.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,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;
Expand Down
56 changes: 34 additions & 22 deletions lib/PDFSignature.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -30,43 +29,56 @@ 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) {
self::addSvgToPDF($finalFile, $layerFile, $bufferFile, false);
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) {
Expand Down

0 comments on commit 0d727e8

Please sign in to comment.