From 2fcf94bc55a72464a947a7640f94f35368b19b90 Mon Sep 17 00:00:00 2001 From: tofulm Date: Wed, 14 Oct 2020 15:29:55 +0200 Subject: [PATCH] On change le fonctionnement, on stocke dans le log la requete sql pour etre capable de recalculer la chaine. on passe en fonction la creation d'une chaine et d'une blochaine --- base/blocklog.php | 17 ++++++------ blocklog_administrations.php | 2 +- blocklog_fonctions.php | 41 ++++++++++++++++++++++++++--- blocklog_options.php | 2 ++ inc/blocklog.php | 50 ++++++++++++++---------------------- paquet.xml | 4 +-- 6 files changed, 71 insertions(+), 45 deletions(-) diff --git a/base/blocklog.php b/base/blocklog.php index df8684d..23887f9 100755 --- a/base/blocklog.php +++ b/base/blocklog.php @@ -46,18 +46,19 @@ function blocklog_declarer_tables_objets_sql($tables) { 'type' => 'blocklog', 'principale' => 'oui', 'field'=> array( - 'id_blocklog' => 'bigint(21) NOT NULL', - 'id_objet' => 'bigint(21) DEFAULT "0" NOT NULL', - 'objet' => 'VARCHAR(25) DEFAULT "" NOT NULL', - 'num' => 'bigint(21) NOT NULL DEFAULT 0', - 'blockchaine' => 'text NOT NULL DEFAULT ""', - 'chaine' => 'text NOT NULL DEFAULT ""', - 'maj' => 'TIMESTAMP' + 'id_blocklog' => 'bigint(21) NOT NULL', + 'id_objet' => 'bigint(21) DEFAULT "0" NOT NULL', + 'objet' => 'VARCHAR(25) DEFAULT "" NOT NULL', + 'num' => 'bigint(21) NOT NULL DEFAULT 0', + 'blockchaine' => 'text NOT NULL DEFAULT ""', + 'chaine' => 'text NOT NULL DEFAULT ""', + 'requete' => 'text NOT NULL DEFAULT ""', + 'maj' => 'TIMESTAMP' ), 'key' => array( 'PRIMARY KEY' => 'id_blocklog', ), - 'champs_editables' => array('num', 'blockchaine', 'chaine','id_objet', 'objet'), + 'champs_editables' => array('num', 'blockchaine', 'chaine','id_objet', 'objet', 'requete'), ); return $tables; diff --git a/blocklog_administrations.php b/blocklog_administrations.php index 7e4efb0..8b5c0e9 100755 --- a/blocklog_administrations.php +++ b/blocklog_administrations.php @@ -27,7 +27,7 @@ function blocklog_upgrade($nom_meta_base_version, $version_cible) { $maj = array(); $maj['create'] = array(array('maj_tables', array('spip_blocklogs'))); - $maj['1.0.3'] = array(array('maj_tables', array('spip_blocklogs'))); + $maj['1.0.7'] = array(array('maj_tables', array('spip_blocklogs'))); include_spip('base/upgrade'); maj_plugin($nom_meta_base_version, $version_cible, $maj); diff --git a/blocklog_fonctions.php b/blocklog_fonctions.php index 117b2b9..1daf182 100755 --- a/blocklog_fonctions.php +++ b/blocklog_fonctions.php @@ -118,9 +118,44 @@ function blocklog_verifier_blockchaines($objet, $date_debut = '', $date_fin = '' } } } - var_dump($retour); - die; - return $retour; } + +function blocklog_creer_chaine($requete = ""){ + if ( strlen($requete) < 5 ) { + return ''; + } + + $r = sql_query($requete); + $chaine = sql_fetch($r); + + if (is_array($chaine)) { + $c = []; + foreach ($chaine as $i => $v) { + $c[] = $v; + } + $chaine = implode('|', $c); + } + + if (defined('_DEBUG_BLOCKLOG')) { + spip_log($chaine, 'blocklog'); + } + + $chaine .= "|"; + $chaine = hash(_BLOCKLOG_ALGO, $chaine); + + return $chaine; +} + +function blocklog_creer_blockchaine($chaine, $objet){ + $where = array( + 'objet='.sql_quote($objet) + ); + $blockchaine = sql_getfetsel('blockchaine', 'spip_blocklogs', $where,'', 'id_blocklog DESC', '1'); + + $blockchaine .= "|" . $chaine; + $blockchaine = hash(_BLOCKLOG_ALGO, $blockchaine); + + return $blockchaine; +} diff --git a/blocklog_options.php b/blocklog_options.php index 6ccddae..8c8c305 100755 --- a/blocklog_options.php +++ b/blocklog_options.php @@ -16,3 +16,5 @@ if (!defined('_ECRIRE_INC_VERSION')) { if (! defined("_BLOCKLOG_ALGO")) { define("_BLOCKLOG_ALGO", 'sha256'); } + +$GLOBALS['blocklog_select'] = []; diff --git a/inc/blocklog.php b/inc/blocklog.php index 0887ffc..d517e7f 100755 --- a/inc/blocklog.php +++ b/inc/blocklog.php @@ -12,10 +12,12 @@ if (!defined('_ECRIRE_INC_VERSION')){ * @param string | array $chaine : ensemble des valeurs utilisées pour creer la chaine * @return array [id_blocklog, chaine, blockchaine] */ -function inc_blocklog_dist($objet, $id_objet = 0, $num = 0, $chaine = '') { - if (!$objet or !$id_objet or !$chaine) { +function inc_blocklog_dist($objet, $id_objet = 0, $num = 0, $requete = '') { + if (!$objet or !$id_objet or !$requete) { return false; } + include_spip('blocklog_fonctions'); + if (defined('_DEBUG_BLOCKLOG')) { spip_log('objet = '.$objet, 'blocklog'); @@ -28,36 +30,16 @@ function inc_blocklog_dist($objet, $id_objet = 0, $num = 0, $chaine = '') { define("_BLOCKLOG_ALGO", 'sha256'); } - if (is_array($chaine)) { - $c = []; - foreach ($chaine as $i => $v) { - $c[] = $v; - } - $chaine = implode('|', $c); - } - if (defined('_DEBUG_BLOCKLOG')) { - spip_log($chaine, 'blocklog'); - } - - $chaine .= "|"; - $chaine = hash(_BLOCKLOG_ALGO, $chaine); - - - $where = array( - 'objet='.sql_quote($objet) - ); - $blockchaine = sql_getfetsel('blockchaine', 'spip_blocklogs', $where,'', 'id_blocklog DESC', '1'); - - $blockchaine .= "|" . $chaine; - $blockchaine = hash(_BLOCKLOG_ALGO, $blockchaine); - + $chaine = blocklog_creer_chaine($requete); + $blockchaine = blocklog_creer_blockchaine($chaine, $objet); $set = array ( 'objet' => $objet, 'id_objet' => intval($id_objet), 'blockchaine' => $blockchaine, 'chaine' => $chaine, - 'num' => intval($num) + 'num' => intval($num), + 'requete' => $requete ); if (defined('_DEBUG_BLOCKLOG')) { @@ -65,12 +47,18 @@ function inc_blocklog_dist($objet, $id_objet = 0, $num = 0, $chaine = '') { spip_log('================', 'blocklog'); } - include_spip('action/editer_objet'); - set_request('id_auteur', null); - $r = objet_inserer("blocklog", null, $set); - if (!intval($r)) { + // test la presence en bdd du couple objet / id_objet, si c'est le cas, + // c'est pas bon signe + $id_blocklog = sql_getfetsel('id_blocklog', 'spip_blocklogs', [ + 'id_objet='.intval($id_objet), + 'objet='.sql_quote($objet) + ]); + if (intval($id_blocklog)) { + spip_log("Modification log $id_blocklog","blocklog_modif"); spip_log("la blockchaine pour $objet / $id_objet ne pas pas etre inserer en bdd", 'blocklog'._LOG_ERREUR); + } else { + $id_blocklog = sql_insertq('spip_blocklogs', $set); } - return $r; + return $id_blocklog; } diff --git a/paquet.xml b/paquet.xml index 8347a05..9eab5c0 100755 --- a/paquet.xml +++ b/paquet.xml @@ -1,12 +1,12 @@ Block Log