From 4e1d38d7ecacb701e16055d7e313cf27209a642e Mon Sep 17 00:00:00 2001 From: fredtempez Date: Tue, 6 Aug 2019 22:20:26 +0200 Subject: [PATCH] =?UTF-8?q?Finstone=20install=C3=A9=20-=20import=20et=20le?= =?UTF-8?q?cture=20=C3=A9criture?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/core.php | 251 +++++++++------ .../flintstone/{src => }/Cache/ArrayCache.php | 0 .../{src => }/Cache/CacheInterface.php | 0 core/vendor/flintstone/{src => }/Config.php | 0 core/vendor/flintstone/{src => }/Database.php | 0 .../vendor/flintstone/{src => }/Exception.php | 0 .../Formatter/FormatterInterface.php | 0 .../{src => }/Formatter/JsonFormatter.php | 0 .../Formatter/SerializeFormatter.php | 0 core/vendor/flintstone/{src => }/Line.php | 0 .../flintstone/{src => }/Validation.php | 0 core/vendor/flintstone/autoload.php | 12 + core/vendor/flintstone/flintstone.php | 296 +++++++++++++++++- core/vendor/flintstone/src/Flintstone.php | 285 ----------------- 14 files changed, 449 insertions(+), 395 deletions(-) rename core/vendor/flintstone/{src => }/Cache/ArrayCache.php (100%) rename core/vendor/flintstone/{src => }/Cache/CacheInterface.php (100%) rename core/vendor/flintstone/{src => }/Config.php (100%) rename core/vendor/flintstone/{src => }/Database.php (100%) rename core/vendor/flintstone/{src => }/Exception.php (100%) rename core/vendor/flintstone/{src => }/Formatter/FormatterInterface.php (100%) rename core/vendor/flintstone/{src => }/Formatter/JsonFormatter.php (100%) rename core/vendor/flintstone/{src => }/Formatter/SerializeFormatter.php (100%) rename core/vendor/flintstone/{src => }/Line.php (100%) rename core/vendor/flintstone/{src => }/Validation.php (100%) create mode 100644 core/vendor/flintstone/autoload.php delete mode 100644 core/vendor/flintstone/src/Flintstone.php diff --git a/core/core.php b/core/core.php index 64b63d56..99bec976 100644 --- a/core/core.php +++ b/core/core.php @@ -14,6 +14,12 @@ * @link http://zwiicms.com/ */ + // Chargement de classes personnalisées +require "core/vendor/sitemap/SitemapGenerator.php"; +require "core/vendor/flintstone/autoload.php"; +require "core/vendor/phpmailer/phpmailer.php"; +require "core/vendor/phpmailer/exception.php"; + class common { const DISPLAY_RAW = 0; @@ -45,6 +51,14 @@ class common { 'theme', 'user' ]; + public static $dataStage = [ + 'config', + 'core', + 'page', + 'module', + 'user', + 'theme' + ]; private $data = []; private $hierarchy = [ 'all' => [], @@ -123,6 +137,7 @@ class common { * Constructeur commun */ public function __construct() { + // Extraction des données http if(isset($_POST)) { $this->input['_POST'] = $_POST; @@ -132,11 +147,15 @@ class common { } // Import des données d'une version 8 - $this->importData(); + $this->importDataV8(); + $this->importDataV9(); // Génère le fichier de données lorque les deux fichiers sont absents ou seulement le thème est - installation fraîche par défaut - if(file_exists(self::DATA_DIR.'core.json') === false OR - file_exists(self::DATA_DIR.'theme.json') === false) { + if( + file_exists(self::DATA_DIR.'config.json') === false || + file_exists(self::DATA_DIR.'core.json') === false || + file_exists(self::DATA_DIR.'theme.json') === false || + file_exists(self::DATA_DIR.'user.json') === false ) { include_once('core/module/install/ressource/defaultdata.php'); $this->setData([install::$defaultData]); $this->saveData(); @@ -155,7 +174,8 @@ class common { // Utilisateur connecté if($this->user === []) { $this->user = $this->getData(['user', $this->getInput('ZWII_USER_ID')]); - } + } + // Construit la liste des pages parents/enfants if($this->hierarchy['all'] === []) { $pages = helper::arrayCollumn($this->getData(['page']), 'position', 'SORT_ASC'); @@ -221,61 +241,6 @@ class common { } } - /** - * Lecture des fichiers de données - * - */ - public function readData() { - // Trois tentatives - for($i = 0; $i < 3; $i++) { - $this->setData([json_decode(file_get_contents(self::DATA_DIR.'core.json'), true) + json_decode(file_get_contents(self::DATA_DIR.'theme.json'), true)]); - if($this->data) { - break; - } - elseif($i === 2) { - exit('Unable to read data file.'); - } - // Pause de 10 millisecondes - usleep(10000); - } - } - - /** - * Import des données de la version 8 - * Converti un fichier de données data.json puis le renomme - */ - public function importData() { - if(file_exists(self::DATA_DIR.'data.json')) { - // Trois tentatives - for($i = 0; $i < 3; $i++) { - $tempData = [json_decode(file_get_contents(self::DATA_DIR.'data.json'), true)]; - if($tempData) { - for($i = 0; $i < 3; $i++) { - if(file_put_contents(self::DATA_DIR.'core.json', json_encode(array_slice($tempData[0],0,5)), LOCK_EX) !== false) { - break; - } - // Pause de 10 millisecondes - usleep(10000); - } - for($i = 0; $i < 3; $i++) { - if(file_put_contents(self::DATA_DIR.'theme.json', json_encode(array_slice($tempData[0],5)), LOCK_EX) !== false) { - break; - } - // Pause de 10 millisecondes - usleep(10000); - } - rename (self::DATA_DIR.'data.json',self::DATA_DIR.'imported_data.json'); - break; - } - elseif($i === 2) { - exit('Unable to read data file.'); - } - // Pause de 10 millisecondes - usleep(10000); - } - - } - } /** * Ajoute les valeurs en sortie @@ -524,49 +489,140 @@ class common { * Enregistre les données dans deux fichiers séparés */ public function saveData() { - // Save config core page module et user - // 5 premières clés principales - // Trois tentatives - $users = new Flintstone\Flintstone('users', ['dir' => self::DATA_DIR]); - - // Set a key -$users->set('bob', ['email' => 'bob@site.com', 'password' => '123456']); - -// Get a key -$user = $users->get('bob'); -echo 'Bob, your email is ' . $user['email']; - -// Retrieve all key names -$keys = $users->getKeys(); // returns array('bob') - -// Retrieve all data -$data = $users->getAll(); // returns array('bob' => array('email' => 'bob@site.com', 'password' => '123456')); - - - for($i = 0; $i < 3; $i++) { - if(file_put_contents(self::DATA_DIR.'core.json', json_encode(array_slice($this->getData(),0,5)) , LOCK_EX) !== false) { - break; + // Langue du frontend en cours d'édition + // $lang = $this->readI18nData('frontend'); + $lang = 'fr'; + // Sauvegarde par niveau + foreach (self::$dataStage as $stageId) { + // Stockage dans un sous-dossier localisé + // Le dossier de langue existe t-il ? + if (!file_exists(self::DATA_DIR . '/' . $lang)) { + mkdir (self::DATA_DIR . '/' . $lang); } - // Pause de 10 millisecondes - usleep(10000); + // Sous-dossier localisé + if ($stageId === 'page' || + $stageId === 'module') { + $folder = self::DATA_DIR . $lang . '/'; + } else { + $folder = self::DATA_DIR; + } + $store[$stageId] = new Flintstone\Flintstone($stageId, [ + 'dir' => $folder, + 'ext' => 'json', + 'formatter' => new Flintstone\Formatter\JsonFormatter() + ]); + + $store[$stageId]->set($stageId,$this->getData([$stageId])); } - // Save theme - // dernière clé principale - // Trois tentatives - for($i = 0; $i < 3; $i++) { - if(file_put_contents(self::DATA_DIR.'theme.json', json_encode(array_slice($this->getData(),5)), LOCK_EX) !== false) { - break; + } + + + /** + * Lecture des fichiers de données + * + */ + public function readData() { + + // reset du tableau + $lang = 'fr'; + $data = []; + + // Boucle des modules + foreach (self::$dataStage as $stageId) { + // Sauf pour les pages et les modules + if ($stageId === 'page' || + $stageId === 'module') { + $folder = self::DATA_DIR . $lang . '/'; + } else { + $folder = self::DATA_DIR; } - // Pause de 10 millisecondes - usleep(10000); + $store[$stageId] = new Flintstone\Flintstone($stageId, [ + 'dir' => $folder, + 'ext' => 'json', + 'formatter' => new Flintstone\Formatter\JsonFormatter() + ]); + $tempData = $store[$stageId]->get($stageId); + if ($tempData) { + $data [$stageId] = $tempData; + } else { + $data [$stageId] = []; + } + } + $this->data = $data; + } + + /** + * Import des données de la version 8 + * Converti un fichier de données data.json puis le renomme + */ + public function importDataV8() { + if(file_exists(self::DATA_DIR.'data.json')) { + // Trois tentatives + for($i = 0; $i < 3; $i++) { + $tempData = [json_decode(file_get_contents(self::DATA_DIR.'data.json'), true)]; + if($tempData) { + for($i = 0; $i < 3; $i++) { + if(file_put_contents(self::DATA_DIR.'core.json', json_encode(array_slice($tempData[0],0,5)), LOCK_EX) !== false) { + break; + } + // Pause de 10 millisecondes + usleep(10000); + } + for($i = 0; $i < 3; $i++) { + if(file_put_contents(self::DATA_DIR.'theme.json', json_encode(array_slice($tempData[0],5)), LOCK_EX) !== false) { + break; + } + // Pause de 10 millisecondes + usleep(10000); + } + rename (self::DATA_DIR.'data.json',self::DATA_DIR.'imported_data.json'); + break; + } + elseif($i === 2) { + exit('Unable to read data file.'); + } + // Pause de 10 millisecondes + usleep(10000); + } + $this->saveData(); + } + } + + /** + * Import des données de la version 9 + * Converti un fichier de données data.json puis le renomme + */ + public function importDataV9() { + + // Détecter les fichiers d'une V9 + if (file_exists(self::DATA_DIR . 'core.php') && + file_exists(self::DATA_DIR . 'theme.php') && + !file_exists(self::DATA_DIR . 'config.php') && + !file_exists(self::DATA_DIR . 'user.php') ) { + + // Trois tentatives + for($i = 0; $i < 3; $i++) { + $this->setData([json_decode(file_get_contents(self::DATA_DIR.'core.json'), true) + json_decode(file_get_contents(self::DATA_DIR.'theme.json'), true)]); + if($this->data) { + break; + } + elseif($i === 2) { + exit('Unable to import data file.'); + } + // Pause de 10 millisecondes + usleep(10000); + } + rename (self::DATA_DIR.'data.json',self::DATA_DIR.'imported_data.json'); + rename (self::DATA_DIR.'theme.json',self::DATA_DIR.'imported_theme.json'); + $this->saveData(); } } /** * Génére un fichier json avec la liste des pages * -*/ + */ public function pages2Json() { // Sauve la liste des pages pour TinyMCE $parents = []; @@ -753,8 +809,6 @@ $data = $users->getAll(); // returns array('bob' => array('email' => 'bob@site.c */ public function sendMail($to, $subject, $content) { // Utilisation de PHPMailer version 6.0.6 - require "core/vendor/phpmailer/phpmailer.php"; - require "core/vendor/phpmailer/exception.php"; // Layout ob_start(); @@ -1157,11 +1211,12 @@ class core extends common { require 'core/vendor/' . $classPath; } // Classes personnalisées - if (!class_exists('flintstone')) { - require 'core/vendor/flintstone/flintstone.php'; } + elseif(is_readable('core/vendor/flinstone/' . $classPath)) { + require 'core/vendor/flintstone/' . $className . '.php'; + require 'core/vendor/flintstone/Formatter/' . $className . '.php'; + } - if (!class_exists('sitemapgenerator')) { - require_once "core/vendor/sitemap/SitemapGenerator.php"; } + } /** diff --git a/core/vendor/flintstone/src/Cache/ArrayCache.php b/core/vendor/flintstone/Cache/ArrayCache.php similarity index 100% rename from core/vendor/flintstone/src/Cache/ArrayCache.php rename to core/vendor/flintstone/Cache/ArrayCache.php diff --git a/core/vendor/flintstone/src/Cache/CacheInterface.php b/core/vendor/flintstone/Cache/CacheInterface.php similarity index 100% rename from core/vendor/flintstone/src/Cache/CacheInterface.php rename to core/vendor/flintstone/Cache/CacheInterface.php diff --git a/core/vendor/flintstone/src/Config.php b/core/vendor/flintstone/Config.php similarity index 100% rename from core/vendor/flintstone/src/Config.php rename to core/vendor/flintstone/Config.php diff --git a/core/vendor/flintstone/src/Database.php b/core/vendor/flintstone/Database.php similarity index 100% rename from core/vendor/flintstone/src/Database.php rename to core/vendor/flintstone/Database.php diff --git a/core/vendor/flintstone/src/Exception.php b/core/vendor/flintstone/Exception.php similarity index 100% rename from core/vendor/flintstone/src/Exception.php rename to core/vendor/flintstone/Exception.php diff --git a/core/vendor/flintstone/src/Formatter/FormatterInterface.php b/core/vendor/flintstone/Formatter/FormatterInterface.php similarity index 100% rename from core/vendor/flintstone/src/Formatter/FormatterInterface.php rename to core/vendor/flintstone/Formatter/FormatterInterface.php diff --git a/core/vendor/flintstone/src/Formatter/JsonFormatter.php b/core/vendor/flintstone/Formatter/JsonFormatter.php similarity index 100% rename from core/vendor/flintstone/src/Formatter/JsonFormatter.php rename to core/vendor/flintstone/Formatter/JsonFormatter.php diff --git a/core/vendor/flintstone/src/Formatter/SerializeFormatter.php b/core/vendor/flintstone/Formatter/SerializeFormatter.php similarity index 100% rename from core/vendor/flintstone/src/Formatter/SerializeFormatter.php rename to core/vendor/flintstone/Formatter/SerializeFormatter.php diff --git a/core/vendor/flintstone/src/Line.php b/core/vendor/flintstone/Line.php similarity index 100% rename from core/vendor/flintstone/src/Line.php rename to core/vendor/flintstone/Line.php diff --git a/core/vendor/flintstone/src/Validation.php b/core/vendor/flintstone/Validation.php similarity index 100% rename from core/vendor/flintstone/src/Validation.php rename to core/vendor/flintstone/Validation.php diff --git a/core/vendor/flintstone/autoload.php b/core/vendor/flintstone/autoload.php new file mode 100644 index 00000000..5df3df3d --- /dev/null +++ b/core/vendor/flintstone/autoload.php @@ -0,0 +1,12 @@ +setDatabase($database); + $this->setConfig($config); + } + + /** + * Get the database. + * + * @return Database + */ + public function getDatabase(): Database + { + return $this->database; + } + + /** + * Set the database. + * + * @param Database $database + */ + public function setDatabase(Database $database) + { + $this->database = $database; + } + + /** + * Get the config. + * + * @return Config + */ + public function getConfig(): Config + { + return $this->config; + } + + /** + * Set the config. + * + * @param Config $config + */ + public function setConfig(Config $config) + { + $this->config = $config; + $this->getDatabase()->setConfig($config); + } + + /** + * Get a key from the database. + * + * @param string $key + * + * @return mixed + */ + public function get(string $key) + { + Validation::validateKey($key); + + // Fetch the key from cache + if ($cache = $this->getConfig()->getCache()) { + if ($cache->contains($key)) { + return $cache->get($key); + } + } + + // Fetch the key from database + $file = $this->getDatabase()->readFromFile(); + $data = false; + + foreach ($file as $line) { + /** @var Line $line */ + if ($line->getKey() == $key) { + $data = $this->decodeData($line->getData()); + break; + } + } + + // Save the data to cache + if ($cache && $data !== false) { + $cache->set($key, $data); + } + + return $data; + } + + /** + * Set a key in the database. + * + * @param string $key + * @param mixed $data + */ + public function set(string $key, $data) + { + Validation::validateKey($key); + + // If the key already exists we need to replace it + if ($this->get($key) !== false) { + $this->replace($key, $data); + return; + } + + // Write the key to the database + $this->getDatabase()->appendToFile($this->getLineString($key, $data)); + + // Delete the key from cache + if ($cache = $this->getConfig()->getCache()) { + $cache->delete($key); + } + } + + /** + * Delete a key from the database. + * + * @param string $key + */ + public function delete(string $key) + { + Validation::validateKey($key); + + if ($this->get($key) !== false) { + $this->replace($key, false); + } + } + + /** + * Flush the database. + */ + public function flush() + { + $this->getDatabase()->flushFile(); + + // Flush the cache + if ($cache = $this->getConfig()->getCache()) { + $cache->flush(); + } + } + + /** + * Get all keys from the database. + * + * @return array + */ + public function getKeys(): array + { + $keys = []; + $file = $this->getDatabase()->readFromFile(); + + foreach ($file as $line) { + /** @var Line $line */ + $keys[] = $line->getKey(); + } + + return $keys; + } + + /** + * Get all data from the database. + * + * @return array + */ + public function getAll(): array + { + $data = []; + $file = $this->getDatabase()->readFromFile(); + + foreach ($file as $line) { + /** @var Line $line */ + $data[$line->getKey()] = $this->decodeData($line->getData()); + } + + return $data; + } + + /** + * Replace a key in the database. + * + * @param string $key + * @param mixed $data + */ + protected function replace(string $key, $data) + { + // Write a new database to a temporary file + $tmpFile = $this->getDatabase()->openTempFile(); + $file = $this->getDatabase()->readFromFile(); + + foreach ($file as $line) { + /** @var Line $line */ + if ($line->getKey() == $key) { + if ($data !== false) { + $tmpFile->fwrite($this->getLineString($key, $data)); + } + } else { + $tmpFile->fwrite($line->getLine() . "\n"); + } + } + + $tmpFile->rewind(); + + // Overwrite the database with the temporary file + $this->getDatabase()->writeTempToFile($tmpFile); + + // Delete the key from cache + if ($cache = $this->getConfig()->getCache()) { + $cache->delete($key); + } + } + + /** + * Get the line string to write. + * + * @param string $key + * @param mixed $data + * + * @return string + */ + protected function getLineString(string $key, $data): string + { + return $key . '=' . $this->encodeData($data) . "\n"; + } + + /** + * Decode a string into data. + * + * @param string $data + * + * @return mixed + */ + protected function decodeData(string $data) + { + return $this->getConfig()->getFormatter()->decode($data); + } + + /** + * Encode data into a string. + * + * @param mixed $data + * + * @return string + */ + protected function encodeData($data): string + { + return $this->getConfig()->getFormatter()->encode($data); + } +} diff --git a/core/vendor/flintstone/src/Flintstone.php b/core/vendor/flintstone/src/Flintstone.php deleted file mode 100644 index e2c21e92..00000000 --- a/core/vendor/flintstone/src/Flintstone.php +++ /dev/null @@ -1,285 +0,0 @@ -setDatabase($database); - $this->setConfig($config); - } - - /** - * Get the database. - * - * @return Database - */ - public function getDatabase(): Database - { - return $this->database; - } - - /** - * Set the database. - * - * @param Database $database - */ - public function setDatabase(Database $database) - { - $this->database = $database; - } - - /** - * Get the config. - * - * @return Config - */ - public function getConfig(): Config - { - return $this->config; - } - - /** - * Set the config. - * - * @param Config $config - */ - public function setConfig(Config $config) - { - $this->config = $config; - $this->getDatabase()->setConfig($config); - } - - /** - * Get a key from the database. - * - * @param string $key - * - * @return mixed - */ - public function get(string $key) - { - Validation::validateKey($key); - - // Fetch the key from cache - if ($cache = $this->getConfig()->getCache()) { - if ($cache->contains($key)) { - return $cache->get($key); - } - } - - // Fetch the key from database - $file = $this->getDatabase()->readFromFile(); - $data = false; - - foreach ($file as $line) { - /** @var Line $line */ - if ($line->getKey() == $key) { - $data = $this->decodeData($line->getData()); - break; - } - } - - // Save the data to cache - if ($cache && $data !== false) { - $cache->set($key, $data); - } - - return $data; - } - - /** - * Set a key in the database. - * - * @param string $key - * @param mixed $data - */ - public function set(string $key, $data) - { - Validation::validateKey($key); - - // If the key already exists we need to replace it - if ($this->get($key) !== false) { - $this->replace($key, $data); - return; - } - - // Write the key to the database - $this->getDatabase()->appendToFile($this->getLineString($key, $data)); - - // Delete the key from cache - if ($cache = $this->getConfig()->getCache()) { - $cache->delete($key); - } - } - - /** - * Delete a key from the database. - * - * @param string $key - */ - public function delete(string $key) - { - Validation::validateKey($key); - - if ($this->get($key) !== false) { - $this->replace($key, false); - } - } - - /** - * Flush the database. - */ - public function flush() - { - $this->getDatabase()->flushFile(); - - // Flush the cache - if ($cache = $this->getConfig()->getCache()) { - $cache->flush(); - } - } - - /** - * Get all keys from the database. - * - * @return array - */ - public function getKeys(): array - { - $keys = []; - $file = $this->getDatabase()->readFromFile(); - - foreach ($file as $line) { - /** @var Line $line */ - $keys[] = $line->getKey(); - } - - return $keys; - } - - /** - * Get all data from the database. - * - * @return array - */ - public function getAll(): array - { - $data = []; - $file = $this->getDatabase()->readFromFile(); - - foreach ($file as $line) { - /** @var Line $line */ - $data[$line->getKey()] = $this->decodeData($line->getData()); - } - - return $data; - } - - /** - * Replace a key in the database. - * - * @param string $key - * @param mixed $data - */ - protected function replace(string $key, $data) - { - // Write a new database to a temporary file - $tmpFile = $this->getDatabase()->openTempFile(); - $file = $this->getDatabase()->readFromFile(); - - foreach ($file as $line) { - /** @var Line $line */ - if ($line->getKey() == $key) { - if ($data !== false) { - $tmpFile->fwrite($this->getLineString($key, $data)); - } - } else { - $tmpFile->fwrite($line->getLine() . "\n"); - } - } - - $tmpFile->rewind(); - - // Overwrite the database with the temporary file - $this->getDatabase()->writeTempToFile($tmpFile); - - // Delete the key from cache - if ($cache = $this->getConfig()->getCache()) { - $cache->delete($key); - } - } - - /** - * Get the line string to write. - * - * @param string $key - * @param mixed $data - * - * @return string - */ - protected function getLineString(string $key, $data): string - { - return $key . '=' . $this->encodeData($data) . "\n"; - } - - /** - * Decode a string into data. - * - * @param string $data - * - * @return mixed - */ - protected function decodeData(string $data) - { - return $this->getConfig()->getFormatter()->decode($data); - } - - /** - * Encode data into a string. - * - * @param mixed $data - * - * @return string - */ - protected function encodeData($data): string - { - return $this->getConfig()->getFormatter()->encode($data); - } -}