diff --git a/CHANGES.md b/CHANGES.md index 0bb78548..6946b25d 100755 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,10 @@ # Changelog +## Version 13.2.00 + +### Correction + +Modification de la fonction d'écriture des données de la classe jsonDB dans le but de s'assurer de l'intégrité des données écrites. Un trafic intense en pointe sur des fichiers volumineux et sur un serveur peu puissant pouvait occasionner des erreurs d'écriture ou un mauvais formatage des données json. ## Version 13.1.08 diff --git a/LISEZMOI.md b/LISEZMOI.md index dbe826ce..d0a69d81 100644 --- a/LISEZMOI.md +++ b/LISEZMOI.md @@ -1,4 +1,4 @@ -# ZwiiCMS 13.1.08 +# ZwiiCMS 13.2.00 Zwii est un CMS sans base de données (flat-file) qui permet de créer et gérer facilement un site web sans aucune connaissance en programmation. diff --git a/README.md b/README.md index ade774d8..3a37fa3d 100755 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# ZwiiCMS 13.1.08 +# ZwiiCMS 13.2.00 Zwii is a database-less (flat-file) CMS that allows you to easily create and manage a web site without any programming knowledge. diff --git a/core/class/jsondb/JsonDb.class.php b/core/class/jsondb/JsonDb.class.php index c8aefdec..cff3bf40 100644 --- a/core/class/jsondb/JsonDb.class.php +++ b/core/class/jsondb/JsonDb.class.php @@ -18,6 +18,12 @@ class JsonDb extends \Prowebcraft\Dot protected $db = ''; protected $data = null; protected $config = []; +// Tentative d'encodage après échec + const MAX_JSON_ENCODE_ATTEMPTS = 5; +// Tentative d'écriture après échec + const MAX_FILE_WRITE_ATTEMPTS = 5; + // Délais entre deux tentaives + const RETRY_DELAY_SECONDS = 1; public function __construct($config = []) { @@ -142,21 +148,53 @@ class JsonDb extends \Prowebcraft\Dot */ public function save() { - $v = json_encode($this->data, JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT ); - // $v = json_encode($this->data, JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT); - $l = strlen($v); - $t = 0; - while ($t < 5) { - $w = file_put_contents($this->db, $v); // Multi user get a locker - if ($w == $l) { - break; + $jsonOptions = JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_FORCE_OBJECT; + $jsonData = json_encode($this->data, $jsonOptions); + + $attempts = 0; + while ($attempts < self::MAX_JSON_ENCODE_ATTEMPTS) { + if ($jsonData !== false) { + break; // Sortir de la boucle si l'encodage réussit } - $t++; - } - if ($w !== $l) { - error_log('Erreur d\'écriture, les données n\'ont pas été sauvegardées.'); - exit('Erreur d\'écriture, les données n\'ont pas été sauvegardées.'); + $attempts++; + error_log('Erreur d\'encodage JSON (tentative ' . $attempts . ') : ' . json_last_error_msg()); + $jsonData = json_encode($this->data, $jsonOptions); // Réessayer l'encodage + sleep(self::RETRY_DELAY_SECONDS); // Attendre avant de réessayer } + if ($jsonData === false) { + error_log('Impossible d\'encoder les données en format JSON.'); + return false; + } + + $lockFile = $this->db . '.lock'; + $lockHandle = fopen($lockFile, 'w'); + + if (flock($lockHandle, LOCK_EX)) { + $attempts = 0; + $bytesWritten = false; + while ($attempts < self::MAX_FILE_WRITE_ATTEMPTS && $bytesWritten === false) { + $bytesWritten = file_put_contents($this->db, $jsonData); + if ($bytesWritten === false) { + $attempts++; + error_log('Erreur d\'écriture (tentative ' . $attempts . ') : impossible de sauvegarder les données.'); + sleep(self::RETRY_DELAY_SECONDS); // Attendre avant de réessayer + } + } + flock($lockHandle, LOCK_UN); + fclose($lockHandle); + + if ($bytesWritten === false || $bytesWritten != strlen($jsonData)) { + error_log('Erreur d\'écriture, les données n\'ont pas été sauvegardées.'); + return false; + } + } else { + error_log('Impossible d\'obtenir un verrouillage sur le fichier de base de données.'); + fclose($lockHandle); + return false; + } + + return true; } + } \ No newline at end of file diff --git a/core/core.php b/core/core.php index 47c52e30..a7e4f301 100644 --- a/core/core.php +++ b/core/core.php @@ -51,7 +51,7 @@ class common const ACCESS_TIMER = 1800; // Numéro de version - const ZWII_VERSION = '13.1.09'; + const ZWII_VERSION = '13.2.00'; // URL autoupdate const ZWII_UPDATE_URL = 'https://forge.chapril.org/ZwiiCMS-Team/cms-update/raw/branch/master/';