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 * \ingroup blockedlog
* \brief Page setup for blockedlog module * \brief Page setup for blockedlog module
*/ */
@ -36,8 +36,22 @@ if (! $user->admin) accessforbidden();
$action = GETPOST('action','alpha'); $action = GETPOST('action','alpha');
$showonlyerrors = GETPOST('showonlyerrors','int'); $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); $block_static = new BlockedLog($db);
/* /*
* Actions * Actions
*/ */
@ -114,7 +128,7 @@ else if($action === 'downloadcsv') {
* View * 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); $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 '<table class="noborder" width="100%">';
print '<tr class="liste_titre">'; print '<tr class="liste_titre">';
print getTitleFieldOfList($langs->trans('#'), 0, $_SERVER["PHP_SELF"],'','','','',$sortfield,$sortorder,'minwidth50 ')."\n"; print getTitleFieldOfList($langs->trans('#'), 0, $_SERVER["PHP_SELF"],'rowid','','','',$sortfield,$sortorder,'minwidth50 ')."\n";
print getTitleFieldOfList($langs->trans('Date'), 0, $_SERVER["PHP_SELF"],'','','','',$sortfield,$sortorder,'')."\n"; print getTitleFieldOfList($langs->trans('Date'), 0, $_SERVER["PHP_SELF"],'date_creation','','','',$sortfield,$sortorder,'')."\n";
print getTitleFieldOfList($langs->trans('Author'), 0, $_SERVER["PHP_SELF"],'','','','',$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('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('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('Amount'), 0, $_SERVER["PHP_SELF"],'','','','align="right"',$sortfield,$sortorder,'')."\n";
print getTitleFieldOfList($langs->trans('DataOfArchivedEvent'), 0, $_SERVER["PHP_SELF"],'','','','align="center"',$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 getTitleFieldOfList('<span id="blockchainstatus"></span>', 0, $_SERVER["PHP_SELF"],'','','','',$sortfield,$sortorder,'')."\n";
print '</tr>'; print '</tr>';
$atleastoneerror=0; $loweridinerror=0;
$checkresult=array();
foreach($blocks as &$block) { foreach($blocks as &$block) {
$checksignature = $block->checkSignature(); $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(); $object_link = $block->getObjectLink();
if (!$showonlyerrors || $block->error>0) { if (empty($showonlyerrors) || ! $checkresult[$block->id] || ($loweridinerror && $block->id >= $loweridinerror)) {
print '<tr class="oddeven">'; print '<tr class="oddeven">';
print '<td>'.$block->id.'</td>'; print '<td>'.$block->id.'</td>';
print '<td>'.dol_print_date($block->tms,'dayhour').'</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>'.$langs->trans('log'.$block->action).'</td>';
print '<td>'.$block->ref_object.'</td>'; print '<td>'.$block->ref_object.'</td>';
print '<td>'.$object_link.'</td>'; print '<td>'.$object_link.'</td>';
@ -178,15 +205,14 @@ foreach($blocks as &$block) {
print '</td>'; print '</td>';
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'); if (empty($checkresult[$block->id])) print img_picto($langs->trans('OkCheckFingerprintValidityButChainIsKo'), 'statut1');
else print img_picto($langs->trans('OkCheckFingerprintValidityButChainIsKo'), 'statut1'); else print img_picto($langs->trans('KoCheckFingerprintValidity'), 'statut8');
} }
else else
{ {
$atleastoneerror++; print img_picto($langs->trans('OkCheckFingerprintValidity'), 'statut4');
print img_picto($langs->trans('KoCheckFingerprintValidity'), 'statut8');
} }
if(!empty($conf->global->BLOCKEDLOG_USE_REMOTE_AUTHORITY) && !empty($conf->global->BLOCKEDLOG_AUTHORITY_URL)) { 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); $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(); $auth->signature = $block_static->getSignature();

View File

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

View File

@ -495,7 +495,7 @@ class BlockedLog
/** /**
* Check if current signature still correct compare to the chain * Check if current signature still correct compare to the chain
* *
* @return boolean * @return boolean True if OK, False if KO
*/ */
public function checkSignature() public function checkSignature()
{ {
@ -512,8 +512,8 @@ class BlockedLog
$res = ($signature === $this->signature); $res = ($signature === $this->signature);
if(!$res) { if (!$res) {
$this->error++; $this->error = 'Signature KO';
} }
return $res; return $res;
@ -578,10 +578,11 @@ class BlockedLog
* @param string $element element to search * @param string $element element to search
* @param int $fk_object id of object to search * @param int $fk_object id of object to search
* @param int $limit max number of element, 0 for all * @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 * @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; global $conf, $cachedlogs;
@ -609,7 +610,8 @@ class BlockedLog
WHERE element='".$element."' AND fk_object=".(int) $fk_object; 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; if($limit > 0 )$sql.=' LIMIT '.$limit;

View File

@ -38,7 +38,7 @@ function blockedlogadmin_prepare_head()
$head[$h][2] = 'blockedlog'; $head[$h][2] = 'blockedlog';
$h++; $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][1] = $langs->trans("Fingerprints");
$head[$h][2] = 'fingerprints'; $head[$h][2] = 'fingerprints';
$h++; $h++;