Debug blockedlog module

This commit is contained in:
Laurent Destailleur 2017-11-30 15:13:23 +01:00
parent 8987e923f1
commit 25bccfde8b
5 changed files with 136 additions and 108 deletions

View File

@ -16,7 +16,7 @@
*/
/**
* \file htdocs/blockedlog/admin/fingerprints.php
* \file htdocs/blockedlog/admin/blockedlog_list.php
* \ingroup blockedlog
* \brief Page setup for blockedlog module
*/
@ -36,8 +36,22 @@ if (! $user->admin) accessforbidden();
$action = GETPOST('action','alpha');
$showonlyerrors = GETPOST('showonlyerrors','int');
// Load variable for pagination
$limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
$sortfield = GETPOST('sortfield','alpha');
$sortorder = GETPOST('sortorder','alpha');
$page = GETPOST('page','int');
if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
$offset = $limit * $page;
$pageprev = $page - 1;
$pagenext = $page + 1;
if (empty($sortfield)) $sortfield='rowid';
if (empty($sortorder)) $sortorder='DESC';
$block_static = new BlockedLog($db);
/*
* Actions
*/
@ -114,7 +128,7 @@ else if($action === 'downloadcsv') {
* View
*/
$blocks = $block_static->getLog('all', 0, GETPOST('all','alpha') ? 0 : 50);
$blocks = $block_static->getLog('all', 0, GETPOST('all','alpha') ? 0 : 50, $sortfield, $sortorder);
$form=new Form($db);
@ -143,11 +157,11 @@ print '<div class="div-table-responsive">'; // You can use div-table-responsive
print '<table class="noborder" width="100%">';
print '<tr class="liste_titre">';
print getTitleFieldOfList($langs->trans('#'), 0, $_SERVER["PHP_SELF"],'','','','',$sortfield,$sortorder,'minwidth50 ')."\n";
print getTitleFieldOfList($langs->trans('Date'), 0, $_SERVER["PHP_SELF"],'','','','',$sortfield,$sortorder,'')."\n";
print getTitleFieldOfList($langs->trans('Author'), 0, $_SERVER["PHP_SELF"],'','','','',$sortfield,$sortorder,'')."\n";
print getTitleFieldOfList($langs->trans('#'), 0, $_SERVER["PHP_SELF"],'rowid','','','',$sortfield,$sortorder,'minwidth50 ')."\n";
print getTitleFieldOfList($langs->trans('Date'), 0, $_SERVER["PHP_SELF"],'date_creation','','','',$sortfield,$sortorder,'')."\n";
print getTitleFieldOfList($langs->trans('Author'), 0, $_SERVER["PHP_SELF"],'user_fullname','','','',$sortfield,$sortorder,'')."\n";
print getTitleFieldOfList($langs->trans('Action'), 0, $_SERVER["PHP_SELF"],'','','','',$sortfield,$sortorder,'')."\n";
print getTitleFieldOfList($langs->trans('Ref'), 0, $_SERVER["PHP_SELF"],'','','','',$sortfield,$sortorder,'')."\n";
print getTitleFieldOfList($langs->trans('Ref'), 0, $_SERVER["PHP_SELF"],'ref_object','','','',$sortfield,$sortorder,'')."\n";
print getTitleFieldOfList($langs->trans('Element'), 0, $_SERVER["PHP_SELF"],'','','','',$sortfield,$sortorder,'')."\n";
print getTitleFieldOfList($langs->trans('Amount'), 0, $_SERVER["PHP_SELF"],'','','','align="right"',$sortfield,$sortorder,'')."\n";
print getTitleFieldOfList($langs->trans('DataOfArchivedEvent'), 0, $_SERVER["PHP_SELF"],'','','','align="center"',$sortfield,$sortorder,'')."\n";
@ -155,18 +169,31 @@ print getTitleFieldOfList($langs->trans('Fingerprint'), 0, $_SERVER["PHP_SELF"],
print getTitleFieldOfList('<span id="blockchainstatus"></span>', 0, $_SERVER["PHP_SELF"],'','','','',$sortfield,$sortorder,'')."\n";
print '</tr>';
$atleastoneerror=0;
$loweridinerror=0;
$checkresult=array();
foreach($blocks as &$block) {
$checksignature = $block->checkSignature();
$checkresult[$block->id]=$checksignature; // false if error
if (! $checksignature)
{
if (empty($loweridinerror)) $loweridinerror=$block->id;
else $loweridinerror = min($loweridinerror, $block->id);
}
}
foreach($blocks as &$block) {
$object_link = $block->getObjectLink();
if (!$showonlyerrors || $block->error>0) {
if (empty($showonlyerrors) || ! $checkresult[$block->id] || ($loweridinerror && $block->id >= $loweridinerror)) {
print '<tr class="oddeven">';
print '<td>'.$block->id.'</td>';
print '<td>'.dol_print_date($block->tms,'dayhour').'</td>';
print '<td>'.$block->getUser().'</td>';
print '<td>';
//print $block->getUser()
print $block->user_fullname;
print '</td>';
print '<td>'.$langs->trans('log'.$block->action).'</td>';
print '<td>'.$block->ref_object.'</td>';
print '<td>'.$object_link.'</td>';
@ -178,15 +205,14 @@ foreach($blocks as &$block) {
print '</td>';
print '<td>';
if (empty($block->error))
if (! $checkresult[$block->id] || ($loweridinerror && $block->id >= $loweridinerror)) // If error
{
if (empty($atleastoneerror)) print img_picto($langs->trans('OkCheckFingerprintValidity'), 'statut4');
else print img_picto($langs->trans('OkCheckFingerprintValidityButChainIsKo'), 'statut1');
if (empty($checkresult[$block->id])) print img_picto($langs->trans('OkCheckFingerprintValidityButChainIsKo'), 'statut1');
else print img_picto($langs->trans('KoCheckFingerprintValidity'), 'statut8');
}
else
{
$atleastoneerror++;
print img_picto($langs->trans('KoCheckFingerprintValidity'), 'statut8');
print img_picto($langs->trans('OkCheckFingerprintValidity'), 'statut4');
}
if(!empty($conf->global->BLOCKEDLOG_USE_REMOTE_AUTHORITY) && !empty($conf->global->BLOCKEDLOG_AUTHORITY_URL)) {

View File

@ -45,7 +45,7 @@ $auth->syncSignatureWithAuthority();
$block_static = new BlockedLog($db);
$blocks = $block_static->getLog('just_certified', 0, 0, 1) ;
$blocks = $block_static->getLog('just_certified', 0, 0, 'rowid', 'ASC');
$auth->signature = $block_static->getSignature();

View File

@ -1,4 +1,4 @@
<?php
<?php
/* Copyright (C) 2017 ATM Consulting <contact@atm-consulting.fr>
*
* This program is free software; you can redistribute it and/or modify
@ -20,76 +20,76 @@
*/
class BlockedLogAuthority
{
/**
* Id of the log
* @var int
*/
public $id;
/**
* Unique fingerprint of the blockchain to store
* @var string
*/
public $signature = '';
/**
* Entire fingerprints blockchain
* @var string
*/
public $blockchain = '';
/**
* timestamp
* @var int
*/
public $tms = 0;
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
public function __construct($db) {
$this->db = $db;
}
/**
* Get the blockchain
* Get the blockchain
*
* @return string blockchain
*/
public function getLocalBlockChain() {
$block_static = new BlockedLog($this->db);
$this->signature = $block_static->getSignature();
$blocks = $block_static->getLog('all', 0, 0, 1) ;
$blocks = $block_static->getLog('all', 0, 0, 'rowid', 'ASC') ;
$this->blockchain = '';
foreach($blocks as &$b) {
$this->blockchain.=$b->signature;
}
return $this->blockchain;
}
/**
* Get hash of the block chain to check
*
* @return string hash md5 of blockchain
*/
public function getBlockchainHash() {
return md5($this->signature.$this->blockchain);
}
/**
* Get hash of the block chain to check
*
@ -97,22 +97,22 @@ class BlockedLogAuthority
* @return boolean
*/
public function checkBlockchain($hash) {
return ($hash === $this->getBlockchainHash() );
}
/**
* Add a new block to the chain
*
* @param string $block new block to chain
*/
public function addBlock($block) {
$this->blockchain.=$block;
}
/**
* hash already exist into chain ?
*
@ -120,20 +120,20 @@ class BlockedLogAuthority
* @return boolean
*/
public function checkBlock($block) {
if(strlen($block)!=64) return false;
$blocks = str_split($this->blockchain,64);
if(!in_array($block,$blocks)) {
return true;
}
}
else{
return false;
}
}
/**
* Get object from database
*
@ -142,40 +142,40 @@ class BlockedLogAuthority
* @return int >0 if OK, <0 if KO, 0 if not found
*/
public function fetch($id, $signature='') {
global $langs;
dol_syslog(get_class($this)."::fetch id=".$id, LOG_DEBUG);
if (empty($id) && empty($signature))
{
$this->error='BadParameter';
return -1;
}
$langs->load("blockedlog");
$sql = "SELECT b.rowid, b.signature, b.blockchain, b.tms";
$sql.= " FROM ".MAIN_DB_PREFIX."blockedlog_authority as b";
if ($id) $sql.= " WHERE b.rowid = ". $id;
else if($signature)$sql.= " WHERE b.signature = '". $this->db->escape( $signature ) ."'" ;
$resql=$this->db->query($sql);
if ($resql)
{
if ($this->db->num_rows($resql))
{
$obj = $this->db->fetch_object($resql);
$this->id = $obj->rowid;
$this->ref = $obj->rowid;
$this->signature = $obj->signature;
$this->blockchain = $obj->blockchain;
$this->tms = $this->db->jdate($obj->tms);
return 1;
}
else
@ -189,9 +189,9 @@ class BlockedLogAuthority
$this->error=$this->db->error();
return -1;
}
}
/**
* Create authority in database.
*
@ -199,17 +199,17 @@ class BlockedLogAuthority
* @return int <0 if KO, >0 if OK
*/
public function create($user) {
global $conf,$langs,$hookmanager;
$langs->load('blockedlog');
$error=0;
dol_syslog(get_class($this).'::create', LOG_DEBUG);
$this->db->begin();
$sql = "INSERT INTO ".MAIN_DB_PREFIX."blockedlog_authority (";
$sql.= " signature,";
$sql.= " blockchain";
@ -217,18 +217,18 @@ class BlockedLogAuthority
$sql.= "'".$this->db->escape($this->signature)."',";
$sql.= "'".$this->db->escape($this->blockchain)."'";
$sql.= ")";
$res = $this->db->query($sql);
if ($res)
{
$id = $this->db->last_insert_id(MAIN_DB_PREFIX."blockedlog_authority");
if ($id > 0)
{
$this->id = $id;
$this->db->commit();
return $this->id;
}
else
@ -243,9 +243,9 @@ class BlockedLogAuthority
$this->db->rollback();
return -1;
}
}
/**
* Create authority in database.
*
@ -253,26 +253,26 @@ class BlockedLogAuthority
* @return int <0 if KO, >0 if OK
*/
public function update($user) {
global $conf,$langs,$hookmanager;
$langs->load('blockedlog');
$error=0;
dol_syslog(get_class($this).'::create', LOG_DEBUG);
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."blockedlog_authority SET ";
$sql.= " blockchain='".$this->db->escape($this->blockchain)."'";
$sql.= " WHERE rowid=".$this->id;
$res = $this->db->query($sql);
if ($res)
{
$this->db->commit();
return 1;
}
else
@ -281,9 +281,9 @@ class BlockedLogAuthority
$this->db->rollback();
return -1;
}
}
/**
* For cron to sync to authority.
*
@ -291,43 +291,43 @@ class BlockedLogAuthority
*/
public function syncSignatureWithAuthority() {
global $conf, $langs;
//TODO create cron task on activation
if(empty($conf->global->BLOCKEDLOG_AUTHORITY_URL) || empty($conf->global->BLOCKEDLOG_USE_REMOTE_AUTHORITY)) {
$this->error = $langs->trans('NoAuthorityURLDefined');
return -2;
}
require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php';
$block_static = new BlockedLog($this->db);
$blocks = $block_static->getLog('not_certified', 0, 0, 1);
$blocks = $block_static->getLog('not_certified', 0, 0, 'rowid', 'ASC');
$signature=$block_static->getSignature();
foreach($blocks as &$block) {
$url = $conf->global->BLOCKEDLOG_AUTHORITY_URL.'/blockedlog/ajax/authority.php?s='.$signature.'&b='.$block->signature;
$res = file_get_contents($url);
echo $block->signature.' '.$url. ' '.$res.'<br>';
if($res === 'blockalreadyadded' || $res === 'blockadded') {
$block->setCertified();
}
else {
$this->error = $langs->trans('ImpossibleToContactAuthority ',$url);
return -1;
}
}
return 1;
}
}

View File

@ -495,7 +495,7 @@ class BlockedLog
/**
* Check if current signature still correct compare to the chain
*
* @return boolean
* @return boolean True if OK, False if KO
*/
public function checkSignature()
{
@ -512,8 +512,8 @@ class BlockedLog
$res = ($signature === $this->signature);
if(!$res) {
$this->error++;
if (!$res) {
$this->error = 'Signature KO';
}
return $res;
@ -578,10 +578,11 @@ class BlockedLog
* @param string $element element to search
* @param int $fk_object id of object to search
* @param int $limit max number of element, 0 for all
* @param string $order sort of query
* @param string $sortfield sort field
* @param string $sortorder sort order
* @return array array of object log
*/
public function getLog($element, $fk_object, $limit = 0, $order = -1)
public function getLog($element, $fk_object, $limit = 0, $sortfield = '', $sortorder = '')
{
global $conf, $cachedlogs;
@ -609,7 +610,8 @@ class BlockedLog
WHERE element='".$element."' AND fk_object=".(int) $fk_object;
}
$sql.=($order<0 ? ' ORDER BY rowid DESC ' : ' ORDER BY rowid ASC ');
$sql.=$this->db->order($sortfield, $sortorder);
//($order<0 ? ' ORDER BY rowid DESC ' : ' ORDER BY rowid ASC ');
if($limit > 0 )$sql.=' LIMIT '.$limit;

View File

@ -38,7 +38,7 @@ function blockedlogadmin_prepare_head()
$head[$h][2] = 'blockedlog';
$h++;
$head[$h][0] = DOL_URL_ROOT."/blockedlog/admin/fingerprints.php";
$head[$h][0] = DOL_URL_ROOT."/blockedlog/admin/blockedlog_list.php";
$head[$h][1] = $langs->trans("Fingerprints");
$head[$h][2] = 'fingerprints';
$h++;