BlockLog/blocklog_fonctions.php

187 lines
4.1 KiB
PHP
Executable file

<?php
/**
* Fonctions utiles au plugin Block Log
*
* @plugin Block Log
* @copyright 2019
* @author tofulm
* @licence GNU/GPL
* @package SPIP\Blocklog\Fonctions
*/
if (!defined('_ECRIRE_INC_VERSION')) {
return;
}
function trouver_blocklog($id_objet, $objet, $all = false){
static $r;
if ($r == null) {
$from = array(
'spip_blocklogs',
);
$where = array(
'id_objet='.intval($id_objet),
'objet='.sql_quote($objet),
);
$r = sql_fetsel('id_blocklog, blockchaine, chaine, requete', $from, $where);
}
if ($all) {
return $r;
}
return $r['blockchaine'];
}
function blockchaine_sur_pdf(){
if (!defined('_BLOCKLOG_CACHER_PDF') or _BLOCKLOG_CACHER_PDF !== true){
return true;
}
return false;
}
function afficher_blocklog($texte, $id_dossier, $objet){
$objet = trim($objet);
$blockchaine = '<span class="affichage_blockchaine">';
$img = find_in_path('img/icone_blockchaine.svg');
if ($img) {
$img = '<img class="icone_blockchaine" src="'.$img.'" style="height:1em;width: 1em;">';
$img .= "&nbsp;";
}
if (!defined('_BLOCKLOG_CACHER_PDF') or _BLOCKLOG_CACHER_PDF !== true){
if (intval($id_dossier) and $objet) {
$blockchaine .= $img;
$blockchaine .= trouver_blocklog($id_dossier, $objet);
$blockchaine .= '</span>';
}
}
$texte = str_replace('@@blockchaine@@', $blockchaine, $texte);
$texte = str_replace('@@blocklog@@', $blockchaine, $texte);
return $texte;
}
function blocklog_verifier_blockchaine($num, $objet){
if (intval($num) and $objet) {
$where = array(
'num='.intval($num),
'objet='.sql_quote($objet),
);
$r = sql_fetsel('id_blocklog, blockchaine, chaine, requete', 'spip_blocklogs', $where);
$where = array(
'objet='.sql_quote($objet),
'id_blocklog<'.intval($r['id_blocklog'])
);
$blockchaine_prec = sql_getfetsel('blockchaine', 'spip_blocklogs', $where, 'id_blocklog DESC', 1);
$blockchaine = $blockchaine_prec . $r['chaine'];
$blockchaine = hash(_BLOCKLOG_ALGO, $blockchaine);
return $blockchaine === $r['blockchaine'];
}
}
function blocklog_verifier_chaine($num, $objet){
if (intval($num) and $objet) {
$where = array(
'num='.intval($num),
'objet='.sql_quote($objet),
);
$r = sql_fetsel('id_blocklog, blockchaine, chaine, requete', 'spip_blocklogs', $where);
$chaine_bdd = $r['chaine'];
$chaine_dynamique = blocklog_creer_chaine($r['requete']);
return $chaine_bdd === $chaine_dynamique;
}
}
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 = 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;
}
//function blocklog_verifier_blockchaines($objet, $date_debut = '', $date_fin = ''){
//if (empty($objet)) {
//return [];
//}
//if (empty($date_debut)) {
//$date_debut = date_create() -> modify('-1 year')->format('Y-m-d H:i:s');
//} else {
//$date_debut = date_create($date_debut)->format('Y-m-d H:i:s');
//}
//if (empty($date_fin)) {
//$date_fin = date_create()->format('Y-m-d H:i:s');
//} else {
//$date_fin = date_create($date_fin)->format('Y-m-d 23:59:59');
//}
//$from = 'spip_blocklogs';
//$where = array(
//'objet='.sql_quote($objet),
//'maj>='.sql_quote($date_debut),
//'maj<='.sql_quote($date_fin),
//);
//$res = sql_allfetsel('*', $from, $where);
//$retour = [];
//if (count($res)) {
//$blockchaine = "";
//foreach ($res as $r) {
//$blockchaine .= $r['chaine'];
//$blockchaine = hash(_BLOCKLOG_ALGO, $blockchaine);
//if ($blockchaine === $r['blockchaine']){
//$r['v'] = 'ok';
//$retour[] = $r;
//} else {
//$r['v'] = 'error';
//$retour[] = $r;
//break;
//}
//}
//}
//return $retour;
//}