dolibarr/htdocs/blockedlog/class/authority.class.php

328 lines
6.9 KiB
PHP
Raw Permalink Normal View History

2017-11-30 15:13:23 +01:00
<?php
2017-06-10 09:25:32 +02:00
/* Copyright (C) 2017 ATM Consulting <contact@atm-consulting.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2017-06-10 09:25:32 +02:00
*/
/**
* Class to manage certif authority
*/
class BlockedLogAuthority
2017-06-10 10:10:34 +02:00
{
2023-03-22 00:13:21 +01:00
/**
* DoliDB
* @var DoliDB
*/
public $db;
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
/**
2023-08-06 12:26:27 +02:00
* Id of the authority
2017-06-10 09:25:32 +02:00
* @var int
*/
public $id;
2017-11-30 15:13:23 +01:00
2023-08-06 12:26:27 +02:00
/**
* @var string Ref of the authority
*/
public $ref;
2017-06-10 09:25:32 +02:00
/**
* Unique fingerprint of the blockchain to store
* @var string
*/
public $signature = '';
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
/**
* Entire fingerprints blockchain
* @var string
*/
public $blockchain = '';
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
/**
* timestamp
* @var int
*/
public $tms = 0;
2017-11-30 15:13:23 +01:00
2023-03-22 00:13:21 +01:00
/**
* Error message
* @var string
*/
public $error;
2017-06-10 09:25:32 +02:00
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
public function __construct($db)
{
$this->db = $db;
}
2017-11-30 15:13:23 +01:00
/**
2017-11-30 15:13:23 +01:00
* Get the blockchain
*
* @return string blockchain
*/
public function getLocalBlockChain()
{
$block_static = new BlockedLog($this->db);
2017-11-30 15:13:23 +01:00
$this->signature = $block_static->getSignature();
2017-11-30 15:13:23 +01:00
$blocks = $block_static->getLog('all', 0, 0, 'rowid', 'ASC');
2017-11-30 15:13:23 +01:00
$this->blockchain = '';
2017-11-30 15:13:23 +01:00
2021-06-10 00:13:23 +02:00
if (is_array($blocks)) {
2021-05-04 01:14:44 +02:00
foreach ($blocks as &$b) {
$this->blockchain .= $b->signature;
}
}
2017-11-30 15:13:23 +01:00
return $this->blockchain;
}
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
/**
* Get hash of the block chain to check
*
* @return string hash md5 of blockchain
*/
public function getBlockchainHash()
{
2017-06-10 09:25:32 +02:00
return md5($this->signature.$this->blockchain);
}
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
/**
* Get hash of the block chain to check
*
* @param string $hash hash md5 of blockchain to test
* @return boolean
*/
public function checkBlockchain($hash)
{
return ($hash === $this->getBlockchainHash());
}
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
/**
* Add a new block to the chain
*
* @param string $block new block to chain
* @return void
2017-06-10 09:25:32 +02:00
*/
public function addBlock($block)
{
$this->blockchain .= $block;
}
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
/**
* hash already exist into chain ?
*
* @param string $block new block to chain
* @return boolean
*/
public function checkBlock($block)
{
2021-03-01 00:19:52 +01:00
if (strlen($block) != 64) {
return false;
}
2017-11-30 15:13:23 +01:00
$blocks = str_split($this->blockchain, 64);
2017-11-30 15:13:23 +01:00
if (!in_array($block, $blocks)) {
2017-06-10 09:25:32 +02:00
return true;
2020-05-21 15:05:19 +02:00
} else {
2017-06-10 09:25:32 +02:00
return false;
}
}
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
/**
* Get object from database
*
2017-06-10 10:10:34 +02:00
* @param int $id Id of object to load
* @param string $signature Signature of object to load
* @return int >0 if OK, <0 if KO, 0 if not found
2017-06-10 09:25:32 +02:00
*/
public function fetch($id, $signature = '')
{
2017-06-10 09:25:32 +02:00
global $langs;
2017-11-30 15:13:23 +01:00
2021-04-24 20:18:11 +02:00
dol_syslog(get_class($this)."::fetch id=".((int) $id), LOG_DEBUG);
2017-11-30 15:13:23 +01:00
2021-03-01 00:19:52 +01:00
if (empty($id) && empty($signature)) {
$this->error = 'BadParameter';
2017-06-10 09:25:32 +02:00
return -1;
}
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
$langs->load("blockedlog");
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
$sql = "SELECT b.rowid, b.signature, b.blockchain, b.tms";
$sql .= " FROM ".MAIN_DB_PREFIX."blockedlog_authority as b";
2017-11-30 15:13:23 +01:00
2021-03-01 00:19:52 +01:00
if ($id) {
2021-03-14 11:48:39 +01:00
$sql .= " WHERE b.rowid = ".((int) $id);
2021-03-01 00:19:52 +01:00
} elseif ($signature) {
$sql .= " WHERE b.signature = '".$this->db->escape($signature)."'";
}
2017-11-30 15:13:23 +01:00
$resql = $this->db->query($sql);
2021-03-01 00:19:52 +01:00
if ($resql) {
if ($this->db->num_rows($resql)) {
2017-06-10 09:25:32 +02:00
$obj = $this->db->fetch_object($resql);
2017-11-30 15:13:23 +01:00
$this->id = $obj->rowid;
$this->ref = $obj->rowid;
2017-11-30 15:13:23 +01:00
$this->signature = $obj->signature;
$this->blockchain = $obj->blockchain;
2017-11-30 15:13:23 +01:00
$this->tms = $this->db->jdate($obj->tms);
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
return 1;
2020-05-21 15:05:19 +02:00
} else {
$this->error = $langs->trans("RecordNotFound");
2017-06-10 09:25:32 +02:00
return 0;
}
2020-05-21 15:05:19 +02:00
} else {
$this->error = $this->db->error();
2017-06-10 09:25:32 +02:00
return -1;
}
}
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
/**
* Create authority in database.
*
* @param User $user Object user that create
2023-12-01 19:51:32 +01:00
* @return int Return integer <0 if KO, >0 if OK
2017-06-10 09:25:32 +02:00
*/
public function create($user)
{
global $conf, $langs, $hookmanager;
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
$langs->load('blockedlog');
2017-11-30 15:13:23 +01:00
$error = 0;
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
dol_syslog(get_class($this).'::create', LOG_DEBUG);
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
$this->db->begin();
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
$sql = "INSERT INTO ".MAIN_DB_PREFIX."blockedlog_authority (";
$sql .= " signature,";
$sql .= " blockchain";
$sql .= ") VALUES (";
$sql .= "'".$this->db->escape($this->signature)."',";
$sql .= "'".$this->db->escape($this->blockchain)."'";
$sql .= ")";
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
$res = $this->db->query($sql);
2021-03-01 00:19:52 +01:00
if ($res) {
2017-06-10 09:25:32 +02:00
$id = $this->db->last_insert_id(MAIN_DB_PREFIX."blockedlog_authority");
2017-11-30 15:13:23 +01:00
2021-03-01 00:19:52 +01:00
if ($id > 0) {
2017-06-10 09:25:32 +02:00
$this->id = $id;
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
$this->db->commit();
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
return $this->id;
2020-05-21 15:05:19 +02:00
} else {
2017-06-10 09:25:32 +02:00
$this->db->rollback();
return -2;
}
2020-05-21 15:05:19 +02:00
} else {
$this->error = $this->db->error();
2017-06-10 09:25:32 +02:00
$this->db->rollback();
return -1;
}
}
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
/**
* Create authority in database.
*
* @param User $user Object user that create
2023-12-01 19:51:32 +01:00
* @return int Return integer <0 if KO, >0 if OK
2017-06-10 09:25:32 +02:00
*/
public function update($user)
{
global $conf, $langs, $hookmanager;
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
$langs->load('blockedlog');
2017-11-30 15:13:23 +01:00
$error = 0;
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
dol_syslog(get_class($this).'::create', LOG_DEBUG);
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
$this->db->begin();
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
$sql = "UPDATE ".MAIN_DB_PREFIX."blockedlog_authority SET ";
$sql .= " blockchain='".$this->db->escape($this->blockchain)."'";
2021-03-14 12:20:23 +01:00
$sql .= " WHERE rowid=".((int) $this->id);
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
$res = $this->db->query($sql);
2021-03-01 00:19:52 +01:00
if ($res) {
2017-06-10 09:25:32 +02:00
$this->db->commit();
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
return 1;
2020-05-21 15:05:19 +02:00
} else {
$this->error = $this->db->error();
2017-06-10 09:25:32 +02:00
$this->db->rollback();
return -1;
}
}
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
/**
* For cron to sync to authority.
*
2023-12-01 19:51:32 +01:00
* @return int Return integer <0 if KO, >0 if OK
2017-06-10 09:25:32 +02:00
*/
public function syncSignatureWithAuthority()
{
2017-06-10 09:25:32 +02:00
global $conf, $langs;
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
//TODO create cron task on activation
2017-11-30 15:13:23 +01:00
2023-11-27 11:39:32 +01:00
if (!getDolGlobalString('BLOCKEDLOG_AUTHORITY_URL') || !getDolGlobalString('BLOCKEDLOG_USE_REMOTE_AUTHORITY')) {
2017-06-10 09:25:32 +02:00
$this->error = $langs->trans('NoAuthorityURLDefined');
return -2;
}
require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php';
2017-11-30 15:13:23 +01:00
2017-06-10 09:25:32 +02:00
$block_static = new BlockedLog($this->db);
2017-11-30 15:13:23 +01:00
$blocks = $block_static->getLog('not_certified', 0, 0, 'rowid', 'ASC');
$signature = $block_static->getSignature();
2017-11-30 15:13:23 +01:00
2021-06-10 00:13:23 +02:00
if (is_array($blocks)) {
foreach ($blocks as &$block) {
2023-10-15 15:32:35 +02:00
$url = getDolGlobalString('BLOCKEDLOG_AUTHORITY_URL') . '/blockedlog/ajax/authority.php?s='.$signature.'&b='.$block->signature;
2021-06-10 00:13:23 +02:00
$res = getURLContent($url);
2022-12-30 12:10:23 +01:00
echo $block->signature.' '.$url.' '.$res['content'].'<br>';
2022-12-30 19:51:07 +01:00
if ($res['content'] === 'blockalreadyadded' || $res['content'] === 'blockadded') {
2021-06-10 00:13:23 +02:00
$block->setCertified();
} else {
$this->error = $langs->trans('ImpossibleToContactAuthority', $url);
2021-06-10 00:13:23 +02:00
return -1;
}
2017-06-10 09:25:32 +02:00
}
}
2017-11-30 15:13:23 +01:00
return 1;
}
2018-08-15 17:34:35 +02:00
}