Skip to content

Commit

Permalink
Manage concurrency decryption
Browse files Browse the repository at this point in the history
  • Loading branch information
wincelau committed Jul 30, 2024
1 parent a344b94 commit aa16fe6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
7 changes: 2 additions & 5 deletions app.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,14 @@ function($f3) {
function($f3) {
$f3->set('activeTab', 'sign');
$hash = Web::instance()->slug($f3->get('PARAMS.hash'));
$sharingFolder = $f3->get('PDF_STORAGE_PATH').$hash;
$symmetricKey = null;
if (isset($_COOKIE[$hash])) {
$symmetricKey = CryptographyClass::protectSymmetricKey($_COOKIE[$hash]);
}

$cryptor = new CryptographyClass($symmetricKey, $f3->get('PDF_STORAGE_PATH').$hash);
if ($cryptor->decrypt() == false) {
$sharingFolder = $cryptor->decrypt();
if ($sharingFolder == false) {
$f3->error(500, "PDF file could not be decrypted. Cookie encryption key might be missing.");
}

Expand Down Expand Up @@ -308,9 +308,6 @@ function($f3) {
}
Web::instance()->send($finalFile, null, 0, TRUE, $filename);

if ($symmetricKey) {
$cryptor->encrypt($hash);
}
if($f3->get('DEBUG')) {
return;
}
Expand Down
14 changes: 7 additions & 7 deletions lib/cryptography.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ public function encrypt() {

public function decrypt() {
if (!$this->isEncrypted()) {
return true;
return $this->pathHash;
}
if (!$this->symmetricKey) {
return false;
}
$decryptFolder = sys_get_temp_dir()."/".uniqid('pdfsignature.'.getmypid(), true);
echo $decryptFolder."\n";
mkdir($decryptFolder);
foreach ($this->getFiles(true) as $file) {
$outputFile = str_replace(".gpg", "", $file);
$outputFile = $decryptFolder."/".str_replace(".gpg", "", basename($file));
$command = "gpg --batch --passphrase $this->symmetricKey --decrypt -o $outputFile $file";
$result = shell_exec($command);
if ($result) {
echo "Decypher failure";
return $result;
throw new Exception("Decypher failure");
}

$this->hardUnlink($file);
}
return true;
return $decryptFolder;
}

public function isEncrypted() {
Expand Down

0 comments on commit aa16fe6

Please sign in to comment.