From eca93fb3596b4aa7acd9bcf3ce94efa01733f6b0 Mon Sep 17 00:00:00 2001 From: Vincent LAURENT Date: Wed, 27 Apr 2022 00:41:44 +0200 Subject: [PATCH] PDF storage expiration time for multi-signature mode --- app.php | 29 ++++++++++++++++++++++++----- public/js/signature.js | 9 +++++++++ templates/signature.html.php | 6 ++++-- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/app.php b/app.php index 7197a48..18cc252 100644 --- a/app.php +++ b/app.php @@ -140,8 +140,8 @@ $f3->route('POST /sign', $f3->route('POST /share', function($f3) { $hash = substr(hash('sha512', uniqid().rand()), 0, 20); - $sharingFolder = $f3->get('PDF_STORAGE_PATH').$hash."/"; - $f3->set('UPLOADS', $sharingFolder); + $sharingFolder = $f3->get('PDF_STORAGE_PATH').$hash; + $f3->set('UPLOADS', $sharingFolder."/"); if (!is_dir($f3->get('PDF_STORAGE_PATH'))) { $f3->error(500, 'Sharing folder doesn\'t exist'); } @@ -149,6 +149,9 @@ $f3->route('POST /share', $f3->error(500, 'Sharing folder is not writable'); } mkdir($sharingFolder); + $expireFile = $sharingFolder.".expire"; + file_put_contents($expireFile, $f3->get('POST.duration')); + touch($expireFile, date_format(date_modify(date_create(), file_get_contents($expireFile)), 'U')); $filename = "original.pdf"; $tmpfile = tempnam($sharingFolder, date('YmdHis')); $svgFiles = ""; @@ -164,7 +167,7 @@ $f3->route('POST /share', return true; }, false, function($fileBaseName, $formFieldName) use ($tmpfile, $filename, $sharingFolder, &$svgFiles) { if($formFieldName == "pdf") { - file_put_contents($sharingFolder."filename.txt", $fileBaseName); + file_put_contents($sharingFolder."/filename.txt", $fileBaseName); return $filename; } if($formFieldName == "svg") { @@ -229,12 +232,15 @@ $f3->route('GET /signature/@hash/pdf', $f3->route('POST /signature/@hash/save', function($f3) { $hash = Web::instance()->slug($f3->get('PARAMS.hash')); - $sharingFolder = $f3->get('PDF_STORAGE_PATH').$hash.'/'; - $f3->set('UPLOADS', $sharingFolder); + $sharingFolder = $f3->get('PDF_STORAGE_PATH').$hash; + $f3->set('UPLOADS', $sharingFolder.'/'); $tmpfile = tempnam($sharingFolder, date('YmdHis')); unlink($tmpfile); $svgFiles = ""; + $expireFile = $sharingFolder.".expire"; + touch($expireFile, date_format(date_modify(date_create(), file_get_contents($expireFile)), 'U')); + $files = Web::instance()->receive(function($file,$formFieldName){ if($formFieldName == "svg" && strpos(Web::instance()->mime($file['tmp_name'], true), 'image/svg+xml') !== 0) { $f3->error(403); @@ -275,6 +281,19 @@ $f3->route('GET /signature/@hash/nblayers', } ); +$f3->route('GET /cron', function($f3) { + $sharingFolder = $f3->get('PDF_STORAGE_PATH'); + foreach(glob($sharingFolder.'*.expire') as $expireFile) { + if(filemtime($expireFile) > time()) { + continue; + } + $expiredFolder = str_replace('.expire', '', $expireFile); + array_map('unlink', glob($expiredFolder."/*")); + rmdir($expiredFolder); + unlink($expireFile); + } +}); + $f3->route('GET /organization', function($f3) { $f3->set('maxSize', min(array(convertPHPSizeToBytes(ini_get('post_max_size')), convertPHPSizeToBytes(ini_get('upload_max_filesize'))))); diff --git a/public/js/signature.js b/public/js/signature.js index 54d37d2..71d4135 100644 --- a/public/js/signature.js +++ b/public/js/signature.js @@ -984,6 +984,12 @@ var modalSharing = function() { } } +var runCron = function() { + const xhr = new XMLHttpRequest(); + xhr.open('GET', '/cron'); + xhr.send(); +} + var pageUpload = async function() { document.querySelector('body').classList.remove('bg-light'); document.getElementById('input_pdf_upload').value = ''; @@ -1080,6 +1086,9 @@ var pageSignature = async function(url) { }; (function () { + if(sharingMode) { + setTimeout(function() { runCron() }, 2000); + } if(hash) { pageSignature('/signature/'+hash+'/pdf'); window.addEventListener('hashchange', function() { diff --git a/templates/signature.html.php b/templates/signature.html.php index 2be1aaa..e213ee1 100644 --- a/templates/signature.html.php +++ b/templates/signature.html.php @@ -175,6 +175,7 @@ -

Chacun des signataires pourra à tout moment télécharger la dernière version du PDF signé.

+

Chacun des signataires pourra à tout moment télécharger la dernière version du PDF signé.