2017-06-10 09:25:32 +02:00
< ? php
2018-01-05 13:40:56 +01:00
/* Copyright ( C ) 2017 ATM Consulting < contact @ atm - consulting . fr >
2017-11-30 14:21:55 +01:00
* Copyright ( C ) 2017 Laurent Destailleur < eldy @ destailleur . fr >
2017-06-10 09:25:32 +02:00
*
* 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
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2017-11-30 14:21:55 +01:00
*
* See https :// medium . com /@ lhartikk / a - blockchain - in - 200 - lines - of - code - 963 cc1cc0e54
2017-06-10 09:25:32 +02:00
*/
2018-03-07 17:22:38 +01:00
/* ini_set ( 'unserialize_callback_func' , 'mycallback' );
function mycallback ( $classname )
{
//var_dump($classname);
include_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php' ;
} */
2017-06-10 09:25:32 +02:00
/**
* Class to manage Blocked Log
*/
2017-06-11 00:03:01 +02:00
class BlockedLog
2017-06-10 10:10:34 +02:00
{
2017-06-10 09:25:32 +02:00
/**
* Id of the log
* @ var int
*/
public $id ;
2018-09-01 23:04:46 +02:00
2017-12-15 15:15:14 +01:00
/**
* Entity
* @ var int
*/
public $entity ;
2017-06-16 11:51:33 +02:00
2018-09-20 14:19:52 +02:00
/**
* @ var string Error message
*/
2017-10-16 08:52:00 +02:00
public $error = '' ;
2018-09-01 23:04:46 +02:00
2018-08-23 16:54:24 +02:00
/**
* @ var string [] Error codes ( or messages )
*/
2017-10-16 08:52:00 +02:00
public $errors = array ();
2017-06-10 09:25:32 +02:00
/**
* Unique fingerprint of the log
* @ var string
*/
public $signature = '' ;
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
/**
* Unique fingerprint of the line log content
* @ var string
*/
public $signature_line = '' ;
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
public $amounts = null ;
2017-06-16 11:51:33 +02:00
2017-06-10 10:10:34 +02:00
/**
* trigger action
* @ var string
*/
2017-06-10 09:25:32 +02:00
public $action = '' ;
2017-06-16 11:51:33 +02:00
2017-06-10 10:10:34 +02:00
/**
* Object element
* @ var string
*/
2017-06-10 09:25:32 +02:00
public $element = '' ;
2017-06-16 11:51:33 +02:00
2017-06-10 10:10:34 +02:00
/**
* Object id
* @ var int
*/
2017-06-10 09:25:32 +02:00
public $fk_object = 0 ;
2017-06-16 11:51:33 +02:00
2017-06-10 10:10:34 +02:00
/**
* Log certified by remote authority or not
* @ var boolean
*/
2017-06-10 09:25:32 +02:00
public $certified = false ;
2017-06-16 11:51:33 +02:00
2017-06-10 10:10:34 +02:00
/**
* Author
* @ var int
*/
2017-06-10 09:25:32 +02:00
public $fk_user = 0 ;
2017-06-16 11:51:33 +02:00
2018-01-05 13:40:56 +01:00
public $date_creation ;
public $date_modification ;
2017-06-10 09:25:32 +02:00
public $date_object = 0 ;
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
public $ref_object = '' ;
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
public $object_data = null ;
2018-03-09 11:37:23 +01:00
public $user_fullname = '' ;
2017-06-16 11:51:33 +02:00
2017-12-16 22:26:25 +01:00
/**
* Array of tracked event codes
* @ var string []
*/
public $trackedevents = array ();
2017-10-16 08:52:00 +02:00
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
public function __construct ( DoliDB $db )
{
2017-12-16 22:26:25 +01:00
global $conf ;
2017-06-10 09:25:32 +02:00
$this -> db = $db ;
2017-06-16 11:51:33 +02:00
2017-12-16 22:26:25 +01:00
$this -> trackedevents = array ();
2018-01-05 04:31:48 +01:00
if ( $conf -> facture -> enabled ) $this -> trackedevents [ 'BILL_VALIDATE' ] = 'logBILL_VALIDATE' ;
if ( $conf -> facture -> enabled ) $this -> trackedevents [ 'BILL_DELETE' ] = 'logBILL_DELETE' ;
if ( $conf -> facture -> enabled ) $this -> trackedevents [ 'BILL_SENTBYMAIL' ] = 'logBILL_SENTBYMAIL' ;
if ( $conf -> facture -> enabled ) $this -> trackedevents [ 'DOC_DOWNLOAD' ] = 'BlockedLogBillDownload' ;
if ( $conf -> facture -> enabled ) $this -> trackedevents [ 'DOC_PREVIEW' ] = 'BlockedLogBillPreview' ;
2017-12-16 22:26:25 +01:00
2018-01-05 04:31:48 +01:00
if ( $conf -> facture -> enabled ) $this -> trackedevents [ 'PAYMENT_CUSTOMER_CREATE' ] = 'logPAYMENT_CUSTOMER_CREATE' ;
if ( $conf -> facture -> enabled ) $this -> trackedevents [ 'PAYMENT_CUSTOMER_DELETE' ] = 'logPAYMENT_CUSTOMER_DELETE' ;
2017-12-16 22:26:25 +01:00
/* Supplier
2018-01-05 04:31:48 +01:00
if ( $conf -> fournisseur -> enabled ) $this -> trackedevents [ 'BILL_SUPPLIER_VALIDATE' ] = 'BlockedLogSupplierBillValidate' ;
if ( $conf -> fournisseur -> enabled ) $this -> trackedevents [ 'BILL_SUPPLIER_DELETE' ] = 'BlockedLogSupplierBillDelete' ;
if ( $conf -> fournisseur -> enabled ) $this -> trackedevents [ 'BILL_SUPPLIER_SENTBYMAIL' ] = 'BlockedLogSupplierBillSentByEmail' ; // Trigger key does not exists, we want just into array to list it as done
if ( $conf -> fournisseur -> enabled ) $this -> trackedevents [ 'SUPPLIER_DOC_DOWNLOAD' ] = 'BlockedLogSupplierBillDownload' ; // Trigger key does not exists, we want just into array to list it as done
if ( $conf -> fournisseur -> enabled ) $this -> trackedevents [ 'SUPPLIER_DOC_PREVIEW' ] = 'BlockedLogSupplierBillPreview' ; // Trigger key does not exists, we want just into array to list it as done
if ( $conf -> fournisseur -> enabled ) $this -> trackedevents [ 'PAYMENT_SUPPLIER_CREATE' ] = 'BlockedLogSupplierBillPaymentCreate' ;
if ( $conf -> fournisseur -> enabled ) $this -> trackedevents [ 'PAYMENT_SUPPLIER_DELETE' ] = 'BlockedLogsupplierBillPaymentCreate' ;
2017-12-16 22:26:25 +01:00
*/
2018-01-05 04:31:48 +01:00
if ( $conf -> don -> enabled ) $this -> trackedevents [ 'DON_VALIDATE' ] = 'logDON_VALIDATE' ;
if ( $conf -> don -> enabled ) $this -> trackedevents [ 'DON_DELETE' ] = 'logDON_DELETE' ;
//if ($conf->don->enabled) $this->trackedevents['DON_SENTBYMAIL']='logDON_SENTBYMAIL';
if ( $conf -> don -> enabled ) $this -> trackedevents [ 'DONATION_PAYMENT_CREATE' ] = 'logDONATION_PAYMENT_CREATE' ;
if ( $conf -> don -> enabled ) $this -> trackedevents [ 'DONATION_PAYMENT_DELETE' ] = 'logDONATION_PAYMENT_DELETE' ;
2017-12-16 22:26:25 +01:00
/*
2018-01-05 04:31:48 +01:00
if ( $conf -> salary -> enabled ) $this -> trackedevents [ 'PAYMENT_SALARY_CREATE' ] = 'BlockedLogSalaryPaymentCreate' ;
if ( $conf -> salary -> enabled ) $this -> trackedevents [ 'PAYMENT_SALARY_MODIFY' ] = 'BlockedLogSalaryPaymentCreate' ;
if ( $conf -> salary -> enabled ) $this -> trackedevents [ 'PAYMENT_SALARY_DELETE' ] = 'BlockedLogSalaryPaymentCreate' ;
2017-12-16 22:26:25 +01:00
*/
2018-01-05 04:31:48 +01:00
if ( $conf -> adherent -> enabled ) $this -> trackedevents [ 'MEMBER_SUBSCRIPTION_CREATE' ] = 'logMEMBER_SUBSCRIPTION_CREATE' ;
if ( $conf -> adherent -> enabled ) $this -> trackedevents [ 'MEMBER_SUBSCRIPTION_MODIFY' ] = 'logMEMBER_SUBSCRIPTION_MODIFY' ;
if ( $conf -> adherent -> enabled ) $this -> trackedevents [ 'MEMBER_SUBSCRIPTION_DELETE' ] = 'logMEMBER_SUBSCRIPTION_DELETE' ;
2017-12-16 22:26:25 +01:00
2018-01-29 18:22:26 +01:00
2018-01-29 19:07:42 +01:00
if ( $conf -> banque -> enabled ) $this -> trackedevents [ 'PAYMENT_VARIOUS_CREATE' ] = 'logPAYMENT_VARIOUS_CREATE' ;
if ( $conf -> banque -> enabled ) $this -> trackedevents [ 'PAYMENT_VARIOUS_MODIFY' ] = 'logPAYMENT_VARIOUS_MODIFY' ;
if ( $conf -> banque -> enabled ) $this -> trackedevents [ 'PAYMENT_VARIOUS_DELETE' ] = 'logPAYMENT_VARIOUS_DELETE' ;
2018-12-17 10:37:56 +01:00
// $conf->global->BANK_ENABLE_POS_CASHCONTROL must be set to 1 by all POS modules
$moduleposenabled = ( $conf -> cashdesk -> enabled || $conf -> takepos -> enabled || ! empty ( $conf -> global -> BANK_ENABLE_POS_CASHCONTROL ));
if ( $moduleposenabled ) $this -> trackedevents [ 'CASHCONTROL_VALIDATE' ] = 'logCASHCONTROL_VALIDATE' ;
2017-06-10 09:25:32 +02:00
}
/**
2018-01-05 12:43:53 +01:00
* Try to retrieve source object ( it it still exists )
2018-08-14 12:23:10 +02:00
* @ return string
2017-06-10 09:25:32 +02:00
*/
2017-10-27 02:12:35 +02:00
public function getObjectLink ()
{
2017-06-10 09:25:32 +02:00
global $langs ;
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
if ( $this -> element === 'facture' ) {
require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php' ;
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
$object = new Facture ( $this -> db );
2018-01-05 12:43:53 +01:00
if ( $object -> fetch ( $this -> fk_object ) > 0 ) {
2017-06-10 09:25:32 +02:00
return $object -> getNomUrl ( 1 );
}
2017-06-13 10:04:05 +02:00
else {
$this -> error ++ ;
}
2017-06-10 09:25:32 +02:00
}
2017-10-26 12:13:47 +02:00
if ( $this -> element === 'invoice_supplier' ) {
require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php' ;
$object = new FactureFournisseur ( $this -> db );
2018-01-05 12:43:53 +01:00
if ( $object -> fetch ( $this -> fk_object ) > 0 ) {
2017-10-26 12:13:47 +02:00
return $object -> getNomUrl ( 1 );
}
else {
$this -> error ++ ;
}
}
2017-06-10 09:25:32 +02:00
else if ( $this -> element === 'payment' ) {
require_once DOL_DOCUMENT_ROOT . '/compta/paiement/class/paiement.class.php' ;
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
$object = new Paiement ( $this -> db );
2018-01-05 12:43:53 +01:00
if ( $object -> fetch ( $this -> fk_object ) > 0 ) {
2017-06-10 09:25:32 +02:00
return $object -> getNomUrl ( 1 );
}
2017-06-13 10:04:05 +02:00
else {
$this -> error ++ ;
}
2017-06-10 09:25:32 +02:00
}
2017-10-26 10:40:36 +02:00
else if ( $this -> element === 'payment_supplier' ) {
require_once DOL_DOCUMENT_ROOT . '/fourn/class/paiementfourn.class.php' ;
$object = new PaiementFourn ( $this -> db );
2018-01-05 12:43:53 +01:00
if ( $object -> fetch ( $this -> fk_object ) > 0 ) {
2017-10-26 10:40:36 +02:00
return $object -> getNomUrl ( 1 );
}
else {
$this -> error ++ ;
}
}
2018-01-05 04:31:48 +01:00
else if ( $this -> element === 'payment_donation' ) {
require_once DOL_DOCUMENT_ROOT . '/don/class/paymentdonation.class.php' ;
$object = new PaymentDonation ( $this -> db );
2018-01-05 12:43:53 +01:00
if ( $object -> fetch ( $this -> fk_object ) > 0 ) {
return $object -> getNomUrl ( 1 );
}
else {
$this -> error ++ ;
}
}
2018-01-29 18:22:26 +01:00
else if ( $this -> element === 'payment_various' ) {
require_once DOL_DOCUMENT_ROOT . '/compta/bank/class/paymentvarious.class.php' ;
$object = new PaymentVarious ( $this -> db );
if ( $object -> fetch ( $this -> fk_object ) > 0 ) {
return $object -> getNomUrl ( 1 );
}
else {
$this -> error ++ ;
}
}
2018-01-05 12:43:53 +01:00
else if ( $this -> element === 'don' || $this -> element === 'donation' ) {
require_once DOL_DOCUMENT_ROOT . '/don/class/don.class.php' ;
$object = new Don ( $this -> db );
if ( $object -> fetch ( $this -> fk_object ) > 0 ) {
2018-01-05 04:31:48 +01:00
return $object -> getNomUrl ( 1 );
}
else {
$this -> error ++ ;
}
}
2018-03-05 17:07:59 +01:00
else if ( $this -> element === 'subscription' ) {
require_once DOL_DOCUMENT_ROOT . '/adherents/class/subscription.class.php' ;
$object = new Subscription ( $this -> db );
if ( $object -> fetch ( $this -> fk_object ) > 0 ) {
return $object -> getNomUrl ( 1 );
}
else {
$this -> error ++ ;
}
}
2018-12-17 11:41:59 +01:00
else if ( $this -> element === 'cashcontrol' ) {
require_once DOL_DOCUMENT_ROOT . '/compta/cashcontrol/class/cashcontrol.class.php' ;
$object = new CashControl ( $this -> db );
if ( $object -> fetch ( $this -> fk_object ) > 0 ) {
return $object -> getNomUrl ( 1 );
}
else {
$this -> error ++ ;
}
}
2017-12-15 15:15:14 +01:00
else if ( $this -> action == 'MODULE_SET' )
{
return '<i class="opacitymedium">System to track events into unalterable logs were enabled</i>' ;
}
else if ( $this -> action == 'MODULE_RESET' )
{
if ( $this -> signature == '0000000000' )
{
return '<i class="opacitymedium">System to track events into unalterable logs were disabled after some recording were done. We saved a special Fingerprint to track the chain as broken.</i>' ;
}
else
{
return '<i class="opacitymedium">System to track events into unalterable logs were disabled. This is possible because no record were done yet.</i>' ;
}
}
2017-06-16 11:51:33 +02:00
2017-12-03 21:47:37 +01:00
return '<i class="opacitymedium">' . $langs -> trans ( 'ImpossibleToReloadObject' , $this -> element , $this -> fk_object ) . '</i>' ;
2017-06-10 09:25:32 +02:00
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
/**
* try to retrieve user author
2018-08-14 12:23:10 +02:00
* @ return string
2017-10-16 08:52:00 +02:00
*/
2017-10-27 02:12:35 +02:00
public function getUser ()
{
2017-06-10 09:25:32 +02:00
global $langs , $cachedUser ;
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
if ( empty ( $cachedUser )) $cachedUser = array ();
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
if ( empty ( $cachedUser [ $this -> fk_user ])) {
$u = new User ( $this -> db );
if ( $u -> fetch ( $this -> fk_user ) > 0 ) {
$cachedUser [ $this -> fk_user ] = $u ;
}
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
if ( ! empty ( $cachedUser [ $this -> fk_user ])) {
return $cachedUser [ $this -> fk_user ] -> getNomUrl ( 1 );
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
return $langs -> trans ( 'ImpossibleToRetrieveUser' , $this -> fk_user );
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
/**
2017-10-27 02:12:35 +02:00
* Populate properties of log from object data
2017-06-10 09:25:32 +02:00
*
2017-12-15 15:15:14 +01:00
* @ param Object $object object to store
* @ param string $action action
* @ param string $amounts amounts
2018-04-10 08:07:23 +02:00
* @ param User $fuser User object ( forced )
2017-12-15 15:15:14 +01:00
* @ return int > 0 if OK , < 0 if KO
2017-06-10 09:25:32 +02:00
*/
2018-04-10 08:07:23 +02:00
public function setObjectData ( & $object , $action , $amounts , $fuser = null )
2017-10-27 02:12:35 +02:00
{
2017-12-01 15:39:18 +01:00
global $langs , $user , $mysoc ;
2017-11-30 14:21:55 +01:00
2018-04-10 08:07:23 +02:00
if ( is_object ( $fuser )) $user = $fuser ;
2017-11-30 14:21:55 +01:00
// Generic fields
2017-11-30 11:16:28 +01:00
2017-11-30 14:21:55 +01:00
// action
$this -> action = $action ;
// amount
$this -> amounts = $amounts ;
// date
2017-10-31 23:57:37 +01:00
if ( $object -> element == 'payment' || $object -> element == 'payment_supplier' )
2017-10-24 15:22:46 +02:00
{
2017-06-10 09:25:32 +02:00
$this -> date_object = $object -> datepaye ;
}
2017-10-31 23:57:37 +01:00
elseif ( $object -> element == 'payment_salary' )
2017-10-24 15:22:46 +02:00
{
$this -> date_object = $object -> datev ;
}
2018-01-29 18:22:26 +01:00
elseif ( $object -> element == 'payment_donation' || $object -> element == 'payment_various' )
2018-01-05 04:31:48 +01:00
{
$this -> date_object = $object -> datepaid ? $object -> datepaid : $object -> datep ;
}
2018-03-05 17:07:59 +01:00
elseif ( $object -> element == 'subscription' )
{
$this -> date_object = $object -> dateh ;
}
2018-12-17 11:41:59 +01:00
elseif ( $object -> element == 'cashcontrol' )
{
$this -> date_object = $object -> date_creation ;
}
2017-10-24 15:22:46 +02:00
else {
2017-06-10 09:25:32 +02:00
$this -> date_object = $object -> date ;
}
2017-11-30 14:21:55 +01:00
// ref
2017-12-04 17:51:05 +01:00
$this -> ref_object = (( ! empty ( $object -> newref )) ? $object -> newref : $object -> ref ); // newref is set when validating a draft, ref is set in other cases
2017-11-30 14:21:55 +01:00
// type of object
2017-06-10 09:25:32 +02:00
$this -> element = $object -> element ;
2017-11-30 14:21:55 +01:00
// id of object
2017-06-10 09:25:32 +02:00
$this -> fk_object = $object -> id ;
2017-06-16 11:51:33 +02:00
2018-03-05 17:07:59 +01:00
// Set object_data
2017-06-10 09:25:32 +02:00
$this -> object_data = new stdClass ();
2018-12-17 11:41:59 +01:00
$arrayoffieldstoexclude = array (
'table_element' , 'fields' , 'ref_previous' , 'ref_next' , 'origin' , 'origin_id' , 'oldcopy' , 'picto' , 'error' , 'errors' , 'modelpdf' ,
'table_element_line' , 'ismultientitymanaged' , 'isextrafieldmanaged' ,
'linkedObjectsIds' , 'linkedObjects' , 'fk_delivery_address' ,
'context'
);
2017-06-16 11:51:33 +02:00
2017-11-30 14:21:55 +01:00
// Add thirdparty info
2017-12-03 11:48:09 +01:00
if ( empty ( $object -> thirdparty ) && method_exists ( $object , 'fetch_thirdparty' )) $object -> fetch_thirdparty ();
2017-11-30 14:21:55 +01:00
if ( ! empty ( $object -> thirdparty ))
2017-10-24 15:22:46 +02:00
{
2017-06-10 10:10:34 +02:00
$this -> object_data -> thirdparty = new stdClass ();
2017-06-16 11:51:33 +02:00
2017-11-30 14:21:55 +01:00
foreach ( $object -> thirdparty as $key => $value )
{
2018-03-05 17:07:59 +01:00
if ( in_array ( $key , $arrayoffieldstoexclude )) continue ; // Discard some properties
2017-11-30 11:16:28 +01:00
if ( ! in_array ( $key , array (
2017-11-30 14:21:55 +01:00
'name' , 'name_alias' , 'ref_ext' , 'address' , 'zip' , 'town' , 'state_code' , 'country_code' , 'idprof1' , 'idprof2' , 'idprof3' , 'idprof4' , 'idprof5' , 'idprof6' , 'phone' , 'fax' , 'email' , 'barcode' ,
'tva_intra' , 'localtax1_assuj' , 'localtax1_value' , 'localtax2_assuj' , 'localtax2_value' , 'managers' , 'capital' , 'typent_code' , 'forme_juridique_code' , 'code_client' , 'code_fournisseur'
))) continue ; // Discard if not into a dedicated list
2017-11-30 11:16:28 +01:00
if ( ! is_object ( $value )) $this -> object_data -> thirdparty -> { $key } = $value ;
}
2017-11-30 14:21:55 +01:00
}
2017-11-30 11:16:28 +01:00
2017-11-30 14:21:55 +01:00
// Add company info
if ( ! empty ( $mysoc ))
{
$this -> object_data -> mycompany = new stdClass ();
foreach ( $mysoc as $key => $value )
{
2018-03-05 17:07:59 +01:00
if ( in_array ( $key , $arrayoffieldstoexclude )) continue ; // Discard some properties
2017-11-30 11:16:28 +01:00
if ( ! in_array ( $key , array (
2017-11-30 14:21:55 +01:00
'name' , 'name_alias' , 'ref_ext' , 'address' , 'zip' , 'town' , 'state_code' , 'country_code' , 'idprof1' , 'idprof2' , 'idprof3' , 'idprof4' , 'idprof5' , 'idprof6' , 'phone' , 'fax' , 'email' , 'barcode' ,
2017-11-30 11:16:28 +01:00
'tva_intra' , 'localtax1_assuj' , 'localtax1_value' , 'localtax2_assuj' , 'localtax2_value' , 'managers' , 'capital' , 'typent_code' , 'forme_juridique_code' , 'code_client' , 'code_fournisseur'
))) continue ; // Discard if not into a dedicated list
if ( ! is_object ( $value )) $this -> object_data -> mycompany -> { $key } = $value ;
2017-06-10 09:25:32 +02:00
}
2017-11-30 14:21:55 +01:00
}
// Add user info
2018-01-14 11:56:45 +01:00
if ( ! empty ( $user ))
{
$this -> fk_user = $user -> id ;
$this -> user_fullname = $user -> getFullName ( $langs );
}
2017-11-30 14:21:55 +01:00
// Field specific to object
if ( $this -> element == 'facture' )
{
2017-12-15 15:15:14 +01:00
foreach ( $object as $key => $value )
{
2018-03-05 17:07:59 +01:00
if ( in_array ( $key , $arrayoffieldstoexclude )) continue ; // Discard some properties
2017-12-15 15:15:14 +01:00
if ( ! in_array ( $key , array (
2018-12-02 14:31:45 +01:00
'ref' , 'ref_client' , 'ref_supplier' , 'date' , 'datef' , 'type' , 'total_ht' , 'total_tva' , 'total_ttc' , 'localtax1' , 'localtax2' , 'revenuestamp' , 'datepointoftax' , 'note_public' , 'lines'
2017-12-15 15:15:14 +01:00
))) continue ; // Discard if not into a dedicated list
2018-01-25 12:57:36 +01:00
if ( $key == 'lines' )
{
$lineid = 0 ;
foreach ( $value as $tmpline ) // $tmpline is object FactureLine
{
$lineid ++ ;
foreach ( $tmpline as $keyline => $valueline )
{
if ( ! in_array ( $keyline , array (
'ref' , 'multicurrency_code' , 'multicurrency_total_ht' , 'multicurrency_total_tva' , 'multicurrency_total_ttc' , 'qty' , 'product_type' , 'vat_src_code' , 'tva_tx' , 'info_bits' , 'localtax1_tx' , 'localtax2_tx' , 'total_ht' , 'total_tva' , 'total_ttc' , 'total_localtax1' , 'total_localtax2'
))) continue ; // Discard if not into a dedicated list
if ( ! is_object ( $this -> object_data -> invoiceline [ $lineid ])) $this -> object_data -> invoiceline [ $lineid ] = new stdClass ();
$this -> object_data -> invoiceline [ $lineid ] -> { $keyline } = $valueline ;
}
}
}
else if ( ! is_object ( $value )) $this -> object_data -> { $key } = $value ;
2017-12-15 15:15:14 +01:00
}
2018-01-12 19:09:43 +01:00
if ( ! empty ( $object -> newref )) $this -> object_data -> ref = $object -> newref ;
2017-12-15 15:15:14 +01:00
}
elseif ( $this -> element == 'invoice_supplier' )
{
foreach ( $object as $key => $value )
{
2018-03-05 17:07:59 +01:00
if ( in_array ( $key , $arrayoffieldstoexclude )) continue ; // Discard some properties
2017-12-15 15:15:14 +01:00
if ( ! in_array ( $key , array (
2018-12-02 14:31:45 +01:00
'ref' , 'ref_client' , 'ref_supplier' , 'date' , 'datef' , 'type' , 'total_ht' , 'total_tva' , 'total_ttc' , 'localtax1' , 'localtax2' , 'revenuestamp' , 'datepointoftax' , 'note_public'
2017-12-15 15:15:14 +01:00
))) continue ; // Discard if not into a dedicated list
if ( ! is_object ( $value )) $this -> object_data -> { $key } = $value ;
2017-10-26 12:13:47 +02:00
}
2018-01-12 19:09:43 +01:00
if ( ! empty ( $object -> newref )) $this -> object_data -> ref = $object -> newref ;
2017-06-10 09:25:32 +02:00
}
2018-01-29 18:22:26 +01:00
elseif ( $this -> element == 'payment' || $this -> element == 'payment_supplier' || $this -> element == 'payment_donation' || $this -> element == 'payment_various' )
2017-10-24 15:22:46 +02:00
{
2018-01-05 04:31:48 +01:00
$datepayment = $object -> datepaye ? $object -> datepaye : ( $object -> datepaid ? $object -> datepaid : $object -> datep );
2018-01-29 18:22:26 +01:00
$paymenttypeid = $object -> paiementid ? $object -> paiementid : ( $object -> paymenttype ? $object -> paymenttype : $object -> type_payment );
2018-01-05 04:31:48 +01:00
2017-12-15 15:15:14 +01:00
$this -> object_data -> ref = $object -> ref ;
2018-01-05 04:31:48 +01:00
$this -> object_data -> date = $datepayment ;
$this -> object_data -> type_code = dol_getIdFromCode ( $this -> db , $paymenttypeid , 'c_paiement' , 'id' , 'code' );
2018-01-29 18:22:26 +01:00
$this -> object_data -> payment_num = ( $object -> num_paiement ? $object -> num_paiement : $object -> num_payment );
2017-12-15 15:15:14 +01:00
//$this->object_data->fk_account = $object->fk_account;
$this -> object_data -> note = $object -> note ;
//var_dump($this->object_data);exit;
2017-12-16 22:36:28 +01:00
$totalamount = 0 ;
2018-01-29 18:22:26 +01:00
if ( ! is_array ( $object -> amounts ) && $object -> amount )
{
$object -> amounts = array ( $object -> id => $object -> amount );
}
2017-12-15 15:15:14 +01:00
$paymentpartnumber = 0 ;
2018-01-05 04:31:48 +01:00
foreach ( $object -> amounts as $objid => $amount )
2017-12-15 15:15:14 +01:00
{
2017-12-16 22:36:28 +01:00
if ( empty ( $amount )) continue ;
$totalamount += $amount ;
2018-01-12 15:23:56 +01:00
$tmpobject = null ;
2017-12-15 15:15:14 +01:00
if ( $this -> element == 'payment_supplier' )
{
2018-01-05 04:31:48 +01:00
include_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php' ;
$tmpobject = new FactureFournisseur ( $this -> db );
2017-12-15 15:15:14 +01:00
}
2018-01-05 04:31:48 +01:00
elseif ( $this -> element == 'payment' )
2017-12-15 15:15:14 +01:00
{
2018-01-05 04:31:48 +01:00
include_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php' ;
$tmpobject = new Facture ( $this -> db );
2017-12-15 15:15:14 +01:00
}
2018-01-05 04:31:48 +01:00
elseif ( $this -> element == 'payment_donation' )
2017-12-15 15:15:14 +01:00
{
2018-01-05 04:31:48 +01:00
include_once DOL_DOCUMENT_ROOT . '/don/class/don.class.php' ;
$tmpobject = new Don ( $this -> db );
2017-12-15 15:15:14 +01:00
}
2018-01-29 18:22:26 +01:00
elseif ( $this -> element == 'payment_various' )
{
include_once DOL_DOCUMENT_ROOT . '/compta/bank/class/paymentvarious.class.php' ;
$tmpobject = new PaymentVarious ( $this -> db );
}
2018-01-12 15:23:56 +01:00
if ( ! is_object ( $tmpobject ))
{
continue ;
}
2018-01-05 04:31:48 +01:00
$result = $tmpobject -> fetch ( $objid );
2018-01-29 18:22:26 +01:00
2017-12-15 15:15:14 +01:00
if ( $result <= 0 )
{
2018-01-05 04:31:48 +01:00
$this -> error = $tmpobject -> error ;
$this -> errors = $tmpobject -> errors ;
2018-01-29 18:22:26 +01:00
dol_syslog ( " Failed to fetch object with id " . $objid , LOG_ERR );
2017-12-15 15:15:14 +01:00
return - 1 ;
}
$paymentpart = new stdClass ();
$paymentpart -> amount = $amount ;
2018-01-29 18:22:26 +01:00
if ( ! in_array ( $this -> element , array ( 'payment_donation' , 'payment_various' )))
2017-12-15 15:15:14 +01:00
{
2018-01-05 04:31:48 +01:00
$result = $tmpobject -> fetch_thirdparty ();
2018-01-29 18:22:26 +01:00
if ( $result == 0 )
{
$this -> error = 'Failed to fetch thirdparty for object with id ' . $tmpobject -> id ;
$this -> errors [] = $this -> error ;
dol_syslog ( " Failed to fetch thirdparty for object with id " . $tmpobject -> id , LOG_ERR );
return - 1 ;
}
elseif ( $result < 0 )
2018-01-05 04:31:48 +01:00
{
$this -> error = $tmpobject -> error ;
$this -> errors = $tmpobject -> errors ;
return - 1 ;
}
$paymentpart -> thirdparty = new stdClass ();
foreach ( $tmpobject -> thirdparty as $key => $value )
{
2018-03-05 17:07:59 +01:00
if ( in_array ( $key , $arrayoffieldstoexclude )) continue ; // Discard some properties
2018-01-05 04:31:48 +01:00
if ( ! in_array ( $key , array (
'name' , 'name_alias' , 'ref_ext' , 'address' , 'zip' , 'town' , 'state_code' , 'country_code' , 'idprof1' , 'idprof2' , 'idprof3' , 'idprof4' , 'idprof5' , 'idprof6' , 'phone' , 'fax' , 'email' , 'barcode' ,
'tva_intra' , 'localtax1_assuj' , 'localtax1_value' , 'localtax2_assuj' , 'localtax2_value' , 'managers' , 'capital' , 'typent_code' , 'forme_juridique_code' , 'code_client' , 'code_fournisseur'
))) continue ; // Discard if not into a dedicated list
if ( ! is_object ( $value )) $paymentpart -> thirdparty -> { $key } = $value ;
}
2017-12-15 15:15:14 +01:00
}
2018-01-05 04:31:48 +01:00
// Init object to avoid warnings
if ( $this -> element == 'payment_donation' ) $paymentpart -> donation = new stdClass ();
else $paymentpart -> invoice = new stdClass ();
2018-01-29 18:22:26 +01:00
if ( $this -> element != 'payment_various' )
2017-12-15 15:15:14 +01:00
{
2018-01-29 18:22:26 +01:00
foreach ( $tmpobject as $key => $value )
2018-01-05 04:31:48 +01:00
{
2018-03-05 17:07:59 +01:00
if ( in_array ( $key , $arrayoffieldstoexclude )) continue ; // Discard some properties
2018-01-29 18:22:26 +01:00
if ( ! in_array ( $key , array (
2018-12-02 14:31:45 +01:00
'ref' , 'ref_client' , 'ref_supplier' , 'date' , 'datef' , 'type' , 'total_ht' , 'total_tva' , 'total_ttc' , 'localtax1' , 'localtax2' , 'revenuestamp' , 'datepointoftax' , 'note_public'
2018-01-29 18:22:26 +01:00
))) continue ; // Discard if not into a dedicated list
if ( ! is_object ( $value ))
{
if ( $this -> element == 'payment_donation' ) $paymentpart -> donation -> { $key } = $value ;
elseif ( $this -> element == 'payment_various' ) $paymentpart -> various -> { $key } = $value ;
else $paymentpart -> invoice -> { $key } = $value ;
}
2018-01-05 04:31:48 +01:00
}
2017-12-15 15:15:14 +01:00
2018-01-29 18:22:26 +01:00
$paymentpartnumber ++ ; // first payment will be 1
$this -> object_data -> payment_part [ $paymentpartnumber ] = $paymentpart ;
}
2017-12-15 15:15:14 +01:00
}
2017-12-16 22:36:28 +01:00
$this -> object_data -> amount = $totalamount ;
2018-01-12 19:09:43 +01:00
if ( ! empty ( $object -> newref )) $this -> object_data -> ref = $object -> newref ;
2017-06-10 09:25:32 +02:00
}
2017-10-27 02:12:35 +02:00
elseif ( $this -> element == 'payment_salary' )
2017-10-24 15:22:46 +02:00
{
$this -> object_data -> amounts = array ( $object -> amount );
2018-01-12 19:09:43 +01:00
if ( ! empty ( $object -> newref )) $this -> object_data -> ref = $object -> newref ;
2017-10-24 15:22:46 +02:00
}
2018-03-05 17:07:59 +01:00
elseif ( $this -> element == 'subscription' )
{
foreach ( $object as $key => $value )
{
if ( in_array ( $key , $arrayoffieldstoexclude )) continue ; // Discard some properties
if ( ! in_array ( $key , array (
'id' , 'datec' , 'dateh' , 'datef' , 'fk_adherent' , 'amount' , 'import_key' , 'statut' , 'note'
))) continue ; // Discard if not into a dedicated list
if ( ! is_object ( $value )) $this -> object_data -> { $key } = $value ;
}
if ( ! empty ( $object -> newref )) $this -> object_data -> ref = $object -> newref ;
}
else // Generic case
{
foreach ( $object as $key => $value )
{
if ( in_array ( $key , $arrayoffieldstoexclude )) continue ; // Discard some properties
if ( ! is_object ( $value )) $this -> object_data -> { $key } = $value ;
}
if ( ! empty ( $object -> newref )) $this -> object_data -> ref = $object -> newref ;
}
2017-12-15 15:15:14 +01:00
return 1 ;
2017-06-10 09:25:32 +02:00
}
2017-06-16 11:51:33 +02: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
2017-06-10 09:25:32 +02:00
* @ return int > 0 if OK , < 0 if KO , 0 if not found
*/
2018-08-15 18:14:02 +02:00
public function fetch ( $id )
{
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
global $langs ;
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch id= " . $id , LOG_DEBUG );
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
if ( empty ( $id ))
{
$this -> error = 'BadParameter' ;
return - 1 ;
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
$langs -> load ( " blockedlog " );
2017-06-16 11:51:33 +02:00
2017-12-15 15:15:14 +01:00
$sql = " SELECT b.rowid, b.date_creation, b.signature, b.signature_line, b.amounts, b.action, b.element, b.fk_object, b.entity, " ;
$sql .= " b.certified, b.tms, b.fk_user, b.user_fullname, b.date_object, b.ref_object, b.object_data " ;
2017-06-10 09:25:32 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " blockedlog as b " ;
if ( $id ) $sql .= " WHERE b.rowid = " . $id ;
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
if ( $this -> db -> num_rows ( $resql ))
{
$obj = $this -> db -> fetch_object ( $resql );
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
$this -> id = $obj -> rowid ;
2017-12-15 15:15:14 +01:00
$this -> entity = $obj -> entity ;
2017-06-10 09:25:32 +02:00
$this -> ref = $obj -> rowid ;
2017-06-16 11:51:33 +02:00
2017-11-30 14:21:55 +01:00
$this -> date_creation = $this -> db -> jdate ( $obj -> date_creation );
$this -> tms = $this -> db -> jdate ( $obj -> tms );
2017-06-10 10:10:34 +02:00
$this -> amounts = ( double ) $obj -> amounts ;
2017-06-10 09:25:32 +02:00
$this -> action = $obj -> action ;
$this -> element = $obj -> element ;
2017-06-16 11:51:33 +02:00
2017-10-16 08:52:00 +02:00
$this -> fk_object = $obj -> fk_object ;
2017-06-10 09:25:32 +02:00
$this -> date_object = $this -> db -> jdate ( $obj -> date_object );
$this -> ref_object = $obj -> ref_object ;
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
$this -> fk_user = $obj -> fk_user ;
2017-11-30 14:21:55 +01:00
$this -> user_fullname = $obj -> user_fullname ;
2017-06-16 11:51:33 +02:00
2018-03-07 17:22:38 +01:00
$this -> object_data = $this -> dolDecodeBlockedData ( $obj -> object_data );
2017-06-16 11:51:33 +02:00
2017-11-30 14:21:55 +01:00
$this -> signature = $obj -> signature ;
$this -> signature_line = $obj -> signature_line ;
$this -> certified = ( $obj -> certified == 1 );
2017-06-10 09:25:32 +02:00
return 1 ;
}
else
{
$this -> error = $langs -> trans ( " RecordNotFound " );
return 0 ;
}
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
2017-06-16 11:51:33 +02:00
2018-03-07 17:22:38 +01:00
/**
* Decode data
*
2018-03-07 19:33:43 +01:00
* @ param string $data Data to unserialize
2018-03-07 17:22:38 +01:00
* @ param string $mode 0 = unserialize , 1 = json_decode
2018-03-07 19:33:43 +01:00
* @ return string Value unserialized
2018-03-07 17:22:38 +01:00
*/
public function dolDecodeBlockedData ( $data , $mode = 0 )
{
try
{
//include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
//include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
$aaa = unserialize ( $data );
//$aaa = unserialize($data);
}
catch ( Exception $e )
{
//print $e->getErrs);
}
return $aaa ;
}
2017-06-10 09:25:32 +02:00
/**
* Set block certified by authority
*
* @ return boolean
*/
2018-08-15 18:14:02 +02:00
public function setCertified ()
{
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
$res = $this -> db -> query ( " UPDATE " . MAIN_DB_PREFIX . " blockedlog SET certified=1 WHERE rowid= " . $this -> id );
if ( $res === false ) return false ;
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
return true ;
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
/**
* Create blocked log in database .
*
2017-12-15 15:15:14 +01:00
* @ param User $user Object user that create
* @ param int $forcesignature Force signature ( for example '0000000000' when we disabled the module )
* @ return int < 0 if KO , > 0 if OK
2017-06-10 09:25:32 +02:00
*/
2018-08-15 18:14:02 +02:00
public function create ( $user , $forcesignature = '' )
{
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
global $conf , $langs , $hookmanager ;
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
$langs -> load ( 'blockedlog' );
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
$error = 0 ;
2017-06-16 11:51:33 +02:00
2017-11-30 14:21:55 +01:00
// Clean data
$this -> amounts = ( double ) $this -> amounts ;
2017-06-16 11:51:33 +02:00
2017-11-30 14:21:55 +01:00
dol_syslog ( get_class ( $this ) . '::create action=' . $this -> action . ' fk_user=' . $this -> fk_user . ' user_fullname=' . $this -> user_fullname , LOG_DEBUG );
2017-06-16 11:51:33 +02:00
2017-11-30 14:21:55 +01:00
// Check parameters/properties
2017-12-15 15:15:14 +01:00
if ( ! isset ( $this -> amounts )) // amount can be 0 for some events (like when module is disabled)
2017-06-10 09:25:32 +02:00
{
$this -> error = $langs -> trans ( " BlockLogNeedAmountsValue " );
dol_syslog ( $this -> error , LOG_WARNING );
return - 1 ;
}
2017-06-16 11:51:33 +02:00
2017-12-15 15:15:14 +01:00
if ( empty ( $this -> element )) {
2017-06-10 09:25:32 +02:00
$this -> error = $langs -> trans ( " BlockLogNeedElement " );
dol_syslog ( $this -> error , LOG_WARNING );
return - 2 ;
}
2017-06-16 11:51:33 +02:00
2018-03-04 19:39:46 +01:00
if ( empty ( $this -> action )) {
2017-11-30 14:21:55 +01:00
$this -> error = $langs -> trans ( " BadParameterWhenCallingCreateOfBlockedLog " );
2017-06-10 09:25:32 +02:00
dol_syslog ( $this -> error , LOG_WARNING );
return - 3 ;
}
2018-03-04 19:39:46 +01:00
if ( empty ( $this -> fk_user )) $this -> user_fullname = '(Anonymous)' ;
2017-06-10 09:25:32 +02:00
2017-11-30 14:21:55 +01:00
$this -> date_creation = dol_now ();
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
$this -> db -> begin ();
2017-06-16 11:51:33 +02:00
2018-01-05 13:40:56 +01:00
$previoushash = $this -> getPreviousHash ( 1 , 0 ); // This get last record and lock database until insert is done
2017-11-30 14:21:55 +01:00
$keyforsignature = $this -> buildKeyForSignature ();
$this -> signature_line = dol_hash ( $keyforsignature , '5' ); // Not really usefull
$this -> signature = dol_hash ( $previoushash . $keyforsignature , '5' );
2017-12-15 15:15:14 +01:00
if ( $forcesignature ) $this -> signature = $forcesignature ;
2017-11-30 14:21:55 +01:00
//var_dump($keyforsignature);var_dump($previoushash);var_dump($this->signature_line);var_dump($this->signature);
2017-06-10 09:25:32 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " blockedlog ( " ;
2017-11-30 14:21:55 +01:00
$sql .= " date_creation, " ;
$sql .= " action, " ;
2017-06-10 09:25:32 +02:00
$sql .= " amounts, " ;
$sql .= " signature, " ;
$sql .= " signature_line, " ;
$sql .= " element, " ;
$sql .= " fk_object, " ;
$sql .= " date_object, " ;
$sql .= " ref_object, " ;
$sql .= " object_data, " ;
$sql .= " certified, " ;
$sql .= " fk_user, " ;
2017-11-30 14:21:55 +01:00
$sql .= " user_fullname, " ;
2017-06-10 09:25:32 +02:00
$sql .= " entity " ;
$sql .= " ) VALUES ( " ;
2017-11-30 14:21:55 +01:00
$sql .= " ' " . $this -> db -> idate ( $this -> date_creation ) . " ', " ;
2017-06-10 09:25:32 +02:00
$sql .= " ' " . $this -> db -> escape ( $this -> action ) . " ', " ;
2017-11-30 14:21:55 +01:00
$sql .= $this -> amounts . " , " ;
2017-06-10 09:25:32 +02:00
$sql .= " ' " . $this -> db -> escape ( $this -> signature ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $this -> signature_line ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $this -> element ) . " ', " ;
2017-11-30 14:21:55 +01:00
$sql .= $this -> fk_object . " , " ;
2017-06-10 09:25:32 +02:00
$sql .= " ' " . $this -> db -> idate ( $this -> date_object ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $this -> ref_object ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( serialize ( $this -> object_data )) . " ', " ;
$sql .= " 0, " ;
2017-11-30 14:21:55 +01:00
$sql .= $this -> fk_user . " , " ;
$sql .= " ' " . $this -> db -> escape ( $this -> user_fullname ) . " ', " ;
2017-12-15 15:15:14 +01:00
$sql .= ( $this -> entity ? $this -> entity : $conf -> entity );
2017-06-10 09:25:32 +02:00
$sql .= " ) " ;
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
$res = $this -> db -> query ( $sql );
if ( $res )
{
$id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " blockedlog " );
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
if ( $id > 0 )
{
$this -> id = $id ;
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
$this -> db -> commit ();
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
return $this -> id ;
}
else
{
$this -> db -> rollback ();
return - 2 ;
}
}
else
{
$this -> error = $this -> db -> error ();
$this -> db -> rollback ();
return - 1 ;
}
2017-06-16 11:51:33 +02:00
2017-11-30 14:21:55 +01:00
// The commit will release the lock so we can insert nex record
2017-06-10 09:25:32 +02:00
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
/**
2018-01-05 13:40:56 +01:00
* Check if current signature still correct compared to the value in chain
2017-06-10 09:25:32 +02:00
*
2018-01-05 13:40:56 +01:00
* @ param string $previoushash If previous signature hash is known , we can provide it to avoid to make a search of it in database .
* @ return boolean True if OK , False if KO
2017-06-10 09:25:32 +02:00
*/
2018-01-05 13:40:56 +01:00
public function checkSignature ( $previoushash = '' )
2017-11-30 14:21:55 +01:00
{
2018-01-05 13:40:56 +01:00
if ( empty ( $previoushash ))
{
$previoushash = $this -> getPreviousHash ( 0 , $this -> id );
}
2017-11-30 14:21:55 +01:00
// Recalculate hash
$keyforsignature = $this -> buildKeyForSignature ();
2018-01-05 13:40:56 +01:00
2017-11-30 14:21:55 +01:00
$signature_line = dol_hash ( $keyforsignature , '5' ); // Not really usefull
$signature = dol_hash ( $previoushash . $keyforsignature , '5' );
//var_dump($previoushash); var_dump($keyforsignature); var_dump($signature_line); var_dump($signature);
2017-06-16 11:51:33 +02:00
2017-11-30 14:21:55 +01:00
$res = ( $signature === $this -> signature );
2017-06-16 11:51:33 +02:00
2017-11-30 15:13:23 +01:00
if ( ! $res ) {
$this -> error = 'Signature KO' ;
2017-06-13 10:04:05 +02:00
}
2017-06-16 11:51:33 +02:00
2017-06-13 10:04:05 +02:00
return $res ;
2017-06-10 09:25:32 +02:00
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
/**
2018-01-25 17:53:02 +01:00
* Return a string for signature .
* Note : rowid of line not included as it is not a business data and this allow to make backup of a year
* and restore it into another database with different id wihtout comprimising checksums
2017-11-30 14:21:55 +01:00
*
* @ return string Key for signature
2017-06-10 09:25:32 +02:00
*/
2017-11-30 14:21:55 +01:00
private function buildKeyForSignature ()
{
//print_r($this->object_data);
return $this -> date_creation . '|' . $this -> action . '|' . $this -> amounts . '|' . $this -> ref_object . '|' . $this -> date_object . '|' . $this -> user_fullname . '|' . print_r ( $this -> object_data , true );
}
2017-06-16 11:51:33 +02:00
2017-11-30 14:21:55 +01:00
/**
* Get previous signature / hash in chain
*
* @ param int $withlock 1 = With a lock
2018-01-05 13:40:56 +01:00
* @ param int $beforeid ID of a record
* @ return string Hash of previous record ( if beforeid is defined ) or hash of last record ( if beforeid is 0 )
2017-11-30 14:21:55 +01:00
*/
2018-01-05 13:40:56 +01:00
public function getPreviousHash ( $withlock = 0 , $beforeid = 0 )
2017-11-30 14:21:55 +01:00
{
global $conf ;
$previoussignature = '' ;
2017-12-15 15:15:14 +01:00
$sql = " SELECT rowid, signature FROM " . MAIN_DB_PREFIX . " blockedlog " ;
$sql .= " WHERE entity= " . $conf -> entity ;
2017-11-30 14:21:55 +01:00
if ( $beforeid ) $sql .= " AND rowid < " . ( int ) $beforeid ;
$sql .= " ORDER BY rowid DESC LIMIT 1 " ;
$sql .= ( $withlock ? " FOR UPDATE " : " " );
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$obj = $this -> db -> fetch_object ( $resql );
if ( $obj )
{
$previoussignature = $obj -> signature ;
}
}
else
{
dol_print_error ( $this -> db );
exit ;
}
if ( empty ( $previoussignature ))
{
// First signature line (line 0)
$previoussignature = $this -> getSignature ();
}
return $previoussignature ;
2017-06-10 09:25:32 +02:00
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
/**
2017-11-30 14:21:55 +01:00
* Return array of log objects ( with criterias )
2017-06-10 09:25:32 +02:00
*
2017-10-16 08:52:00 +02:00
* @ 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
2017-11-30 15:13:23 +01:00
* @ param string $sortfield sort field
* @ param string $sortorder sort order
2017-12-16 21:36:36 +01:00
* @ param int $search_fk_user id of user ( s )
2017-12-08 17:15:24 +01:00
* @ param int $search_start start time limit
* @ param int $search_end end time limit
2017-12-14 17:08:43 +01:00
* @ param string $search_ref search ref
* @ param string $search_amount search amount
2018-01-05 04:31:48 +01:00
* @ param string $search_code search code
2018-01-12 20:24:09 +01:00
* @ return array | int Array of object log or < 0 if error
2017-06-10 09:25:32 +02:00
*/
2018-01-05 04:31:48 +01:00
public function getLog ( $element , $fk_object , $limit = 0 , $sortfield = '' , $sortorder = '' , $search_fk_user = - 1 , $search_start = - 1 , $search_end = - 1 , $search_ref = '' , $search_amount = '' , $search_code = '' )
2017-11-30 14:21:55 +01:00
{
global $conf , $cachedlogs ;
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
/* $cachedlogs allow fastest search */
2017-11-30 14:21:55 +01:00
if ( empty ( $cachedlogs )) $cachedlogs = array ();
2017-06-16 11:51:33 +02:00
2017-11-30 14:21:55 +01:00
if ( $element == 'all' ) {
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " blockedlog
WHERE entity = " . $conf->entity ;
}
2017-11-30 14:21:55 +01:00
else if ( $element == 'not_certified' ) {
2017-06-10 09:25:32 +02:00
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " blockedlog
WHERE entity = " . $conf->entity . " AND certified = 0 " ;
}
2017-11-30 14:21:55 +01:00
else if ( $element == 'just_certified' ) {
2017-06-10 09:25:32 +02:00
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " blockedlog
WHERE entity = " . $conf->entity . " AND certified = 1 " ;
}
else {
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " blockedlog
2017-12-15 15:15:14 +01:00
WHERE entity = " . $conf->entity . " AND element = '".$element."' AND fk_object = " .(int) $fk_object ;
2017-06-10 09:25:32 +02:00
}
2017-06-16 11:51:33 +02:00
2018-01-05 04:31:48 +01:00
if ( $search_fk_user > 0 ) $sql .= natural_search ( " fk_user " , $search_fk_user , 2 );
if ( $search_start > 0 ) $sql .= " AND date_creation >= ' " . $this -> db -> idate ( $search_start ) . " ' " ;
if ( $search_end > 0 ) $sql .= " AND date_creation <= ' " . $this -> db -> idate ( $search_end ) . " ' " ;
if ( $search_ref != '' ) $sql .= natural_search ( " ref_object " , $search_ref );
2017-12-14 17:08:43 +01:00
if ( $search_amount != '' ) $sql .= natural_search ( " amounts " , $search_amount , 1 );
2018-01-05 12:43:53 +01:00
if ( $search_code != '' && $search_code != '-1' ) $sql .= natural_search ( " action " , $search_code , 3 );
2017-12-09 00:40:22 +01:00
2017-11-30 15:13:23 +01:00
$sql .= $this -> db -> order ( $sortfield , $sortorder );
2018-01-12 20:24:09 +01:00
$sql .= $this -> db -> plimit ( $limit + 1 ); // We want more, because we will stop into loop later with error if we reach max
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
$res = $this -> db -> query ( $sql );
if ( $res ) {
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
$results = array ();
2017-06-16 11:51:33 +02:00
2018-01-12 20:24:09 +01:00
$i = 0 ;
while ( $obj = $this -> db -> fetch_object ( $res ))
{
$i ++ ;
if ( $i > $limit )
{
// Too many record, we will consume too much memory
return - 2 ;
}
2017-06-16 11:51:33 +02:00
2018-01-12 20:24:09 +01:00
if ( ! isset ( $cachedlogs [ $obj -> rowid ]))
{
2017-06-10 09:25:32 +02:00
$b = new BlockedLog ( $this -> db );
$b -> fetch ( $obj -> rowid );
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
$cachedlogs [ $obj -> rowid ] = $b ;
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
$results [] = $cachedlogs [ $obj -> rowid ];
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
return $results ;
}
2018-01-12 20:24:09 +01:00
return - 1 ;
2017-06-10 09:25:32 +02:00
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
/**
2017-12-15 15:15:14 +01:00
* Return the signature ( hash ) of the " genesis-block " ( Block 0 ) .
2017-06-10 09:25:32 +02:00
*
2017-11-30 14:21:55 +01:00
* @ return string Signature of genesis - block for current conf -> entity
2017-06-10 09:25:32 +02:00
*/
2017-11-30 14:21:55 +01:00
public function getSignature ()
{
2017-06-10 09:25:32 +02:00
global $db , $conf , $mysoc ;
2017-06-16 11:51:33 +02:00
if ( empty ( $conf -> global -> BLOCKEDLOG_ENTITY_FINGERPRINT )) { // creation of a unique fingerprint
2017-06-10 09:25:32 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/admin.lib.php' ;
2018-01-02 09:36:37 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/security2.lib.php' ;
2017-06-16 11:51:33 +02:00
2017-11-30 14:21:55 +01:00
$fingerprint = dol_hash ( print_r ( $mysoc , true ) . getRandomPassword ( 1 ), '5' );
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
dolibarr_set_const ( $db , 'BLOCKEDLOG_ENTITY_FINGERPRINT' , $fingerprint , 'chaine' , 0 , 'Numeric Unique Fingerprint' , $conf -> entity );
2017-06-16 11:51:33 +02:00
$conf -> global -> BLOCKEDLOG_ENTITY_FINGERPRINT = $fingerprint ;
2017-06-10 09:25:32 +02:00
}
2017-06-16 11:51:33 +02:00
2017-06-10 09:25:32 +02:00
return $conf -> global -> BLOCKEDLOG_ENTITY_FINGERPRINT ;
}
2017-06-16 11:51:33 +02:00
2017-12-15 15:15:14 +01:00
/**
* Check if module was already used or not for at least one recording .
*
* @ param int $ignoresystem Ignore system events for the test
2018-08-14 12:23:10 +02:00
* @ return bool
2017-12-15 15:15:14 +01:00
*/
function alreadyUsed ( $ignoresystem = 0 )
{
global $conf ;
$result = false ;
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " blockedlog " ;
$sql .= " WHERE entity = " . $conf -> entity ;
if ( $ignoresystem ) $sql .= " AND action not in ('MODULE_SET','MODULE_RESET') " ;
$sql .= $this -> db -> plimit ( 1 );
$res = $this -> db -> query ( $sql );
if ( $res !== false )
{
$obj = $this -> db -> fetch_object ( $res );
if ( $obj ) $result = true ;
}
else dol_print_error ( $this -> db );
dol_syslog ( " Module Blockedlog alreadyUsed with ignoresystem= " . $ignoresystem . " is " . $result );
return $result ;
}
2017-06-10 09:25:32 +02:00
}