777 lines
29 KiB
PHP
777 lines
29 KiB
PHP
<?php
|
|
/* Copyright (C) 2017 ATM Consulting <contact@atm-consulting.fr>
|
|
* Copyright (C) 2018 Destailleur Laurent <eldy@users.sourceforge.net>
|
|
* Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
|
|
* Copyright (C) 2026 Frédéric France <frederic.france@free.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
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
/**
|
|
* \file htdocs/blockedlog/lib/blockedlog.lib.php
|
|
* \ingroup system
|
|
* \brief Library for common blockedlog functions
|
|
*/
|
|
|
|
include_once DOL_DOCUMENT_ROOT.'/blockedlog/versionmod.inc.php';
|
|
|
|
|
|
/**
|
|
* Define head array for tabs of blockedlog tools setup pages
|
|
*
|
|
* @return string Version
|
|
*/
|
|
function getBlockedLogVersionToShow()
|
|
{
|
|
$versionbadge = constant('DOLCERT_VERSION');
|
|
|
|
// Protection if we used a past old version not yet certified, we change version shown.
|
|
if (!constant('CERTIF_LNE')) { // Hard coded in version
|
|
// $versionbadge = preg_replace('/^(\d)\./', '\1b.', $versionbadge); // Not yet required.
|
|
}
|
|
|
|
if (constant('DOLCERT_NAME') != 'BlockedLog') { // Can add a prefix namevefore version text for clarity.
|
|
$versionbadge = constant('DOLCERT_NAME').'-'.$versionbadge;
|
|
}
|
|
|
|
return $versionbadge;
|
|
}
|
|
|
|
|
|
/**
|
|
* Define head array for tabs of blockedlog tools setup pages
|
|
*
|
|
* @param int $withtabsetup Add also the tab "Setup"
|
|
* @return array<array{0:string,1:string,2:string}> Array of head
|
|
*/
|
|
function blockedlogadmin_prepare_head($withtabsetup)
|
|
{
|
|
global $db, $langs, $conf, $mysoc;
|
|
|
|
$langs->load("blockedlog");
|
|
|
|
require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php';
|
|
|
|
$param = '';
|
|
$param .= ($withtabsetup? "?withtab=".$withtabsetup : "");
|
|
$param .= (GETPOST('origin') ? ($param ? '&' : '?').'origin='.GETPOST('origin') : '');
|
|
|
|
$h = 0;
|
|
$head = array();
|
|
|
|
if (!userIsTaxAuditor()) {
|
|
$head[$h][0] = DOL_URL_ROOT."/blockedlog/admin/registration.php".$param;
|
|
$head[$h][1] = $langs->trans("UserRegistration");
|
|
$head[$h][2] = 'registration';
|
|
$h++;
|
|
}
|
|
|
|
if (!userIsTaxAuditor()) {
|
|
$b = new BlockedLog($db);
|
|
$head[$h][0] = DOL_URL_ROOT."/blockedlog/admin/blockedlog_list.php".$param;
|
|
$head[$h][1] = $langs->trans("BrowseBlockedLog");
|
|
if ($b->alreadyUsed()) {
|
|
$head[$h][1] .= (!getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER') ? '<span class="badge marginleftonlyshort">...</span>' : '');
|
|
}
|
|
$head[$h][2] = 'fingerprints';
|
|
$h++;
|
|
}
|
|
|
|
$head[$h][0] = DOL_URL_ROOT."/blockedlog/admin/blockedlog_archives.php".$param;
|
|
$head[$h][1] = $langs->trans("Archives");
|
|
// Add badge on nb of files
|
|
$block_static = new BlockedLog($db);
|
|
$upload_dir = getMultidirOutput($block_static, 'blockedlog').'/archives';
|
|
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
|
|
require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
|
|
$nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
|
|
$nbLinks = 0;
|
|
if (($nbFiles + $nbLinks) > 0) {
|
|
$head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
|
|
}
|
|
$head[$h][2] = 'archives';
|
|
$h++;
|
|
|
|
if (userIsTaxAuditor()) {
|
|
$head[$h][0] = DOL_URL_ROOT."/blockedlog/admin/blockedlog_control.php".$param;
|
|
$head[$h][1] = $langs->trans("OtherControl");
|
|
$head[$h][2] = 'control';
|
|
$h++;
|
|
}
|
|
|
|
if ($mysoc->country_code == 'FR') {
|
|
$head[$h][0] = DOL_URL_ROOT."/blockedlog/admin/documentation.php".$param;
|
|
$head[$h][1] = $langs->trans("Documentation");
|
|
$head[$h][2] = 'documentation';
|
|
$h++;
|
|
}
|
|
|
|
if ($withtabsetup && !userIsTaxAuditor()) {
|
|
$head[$h][0] = DOL_URL_ROOT."/blockedlog/admin/blockedlog.php".$param;
|
|
$head[$h][1] = $langs->trans("TechnicalInformation");
|
|
$head[$h][2] = 'technicalinfo';
|
|
$h++;
|
|
}
|
|
|
|
|
|
$object = new stdClass();
|
|
|
|
// Show more tabs from modules
|
|
// Entries must be declared in modules descriptor with line
|
|
// $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
|
|
// $this->tabs = array('entity:-tabname); to remove a tab
|
|
complete_head_from_modules($conf, $langs, $object, $head, $h, 'blockedlog');
|
|
|
|
complete_head_from_modules($conf, $langs, $object, $head, $h, 'blockedlog', 'remove');
|
|
|
|
return $head;
|
|
}
|
|
|
|
|
|
/**
|
|
* Return if the KYC mandatory parameters are set
|
|
* Must be the same fields than the one defined as mandatory into the registration form.
|
|
*
|
|
* @return boolean True or false
|
|
*/
|
|
function isRegistrationDataSaved()
|
|
{
|
|
global $mysoc;
|
|
|
|
$companyname = getDolGlobalString('BLOCKEDLOG_REGISTRATION_NAME', $mysoc->name);
|
|
$companyemail = getDolGlobalString('BLOCKEDLOG_REGISTRATION_EMAIL', $mysoc->email);
|
|
$companycountrycode = $mysoc->country_code;
|
|
$companyidprof1 = getDolGlobalString('MAIN_INFO_SIREN', $mysoc->idprof1);
|
|
$companyidprof2 = getDolGlobalString('MAIN_INFO_SIRET', $mysoc->idprof2);
|
|
//$companytel = getDolGlobalString('BLOCKEDLOG_REGISTRATION_TEL', $mysoc->phone);
|
|
|
|
if (empty($companyname) || empty($companycountrycode) || empty($companyidprof1) || empty($companyidprof2) || empty($companyemail)) {
|
|
return false;
|
|
}
|
|
/*
|
|
$providerset = getDolGlobalString('MAIN_INFO_ITPROVIDER_NAME'); // Can be 'myself'
|
|
|
|
if (empty($providerset)) {
|
|
return false;
|
|
}
|
|
*/
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
/**
|
|
* Return if the KYC mandatory parameters are set AND pushed/registered centralized server
|
|
*
|
|
* @return boolean True or false
|
|
*/
|
|
function isRegistrationDataSavedAndPushed()
|
|
{
|
|
return isRegistrationDataSaved() && (bool) getDolGlobalString('MAIN_FIRST_REGISTRATION_OK_DATE');
|
|
}
|
|
|
|
|
|
/**
|
|
* Return a hash unique identifier of the registration (used to identify the registration of instance without disclosing personal data)
|
|
*
|
|
* @param string $algo Algorithm to use for hash key
|
|
* @return string Hash unique ID
|
|
*/
|
|
function getHashUniqueIdOfRegistration($algo = 'sha256')
|
|
{
|
|
global $conf;
|
|
|
|
return dol_hash('dolibarr'.$conf->file->instance_unique_id.($conf->entity > 1 ? $conf->entity : ''), $algo, 1);
|
|
}
|
|
|
|
|
|
/**
|
|
* Return if the version is a candidate version to get the LNE certification and if the prerequisites are OK in production to be switched to LNE certified mode.
|
|
* The difference with isALNERunningVersion() is that isALNEQualifiedVersion() just checks if it has a sense or not to activate
|
|
* the restrictions (it is not a check to say if we are or not in a mode with restrictions activated, but if we are in a context that has a sense to activate them).
|
|
* It can be used to show warnings or alerts to end users.
|
|
*
|
|
* @param int<0,1> $ignoredev Set this to 1 to ignore the fact the version is an alpha or beta version (to avoid return false on such version)
|
|
* @param int<0,1> $ignoremodule Set this to 1 to not take into account if module BlockedLog is on, so function can be used during module activation.
|
|
* @return string '' if false, or a string if true
|
|
*/
|
|
function isALNEQualifiedVersion($ignoredev = 0, $ignoremodule = 0)
|
|
{
|
|
global $mysoc;
|
|
|
|
// For Dev/Debug purpose: Constant set by developer to force all LNE restrictions even if country is not France so we can test them on any dev instance.
|
|
// Note that you can force, with this option, the enabling of the LNE restrictions, but there is no way to force the disabling of the LNE restriction.
|
|
if (defined('CERTIF_LNE') && (int) constant('CERTIF_LNE') === 2) {
|
|
return 'CERTIF_LNE_IS_2';
|
|
}
|
|
|
|
if (!$ignoredev && preg_match('/\-/', DOL_VERSION)) { // This is not a stable version (dev version), it can't be the certified versions.
|
|
return '';
|
|
}
|
|
if ($mysoc->country_code != 'FR') {
|
|
return '';
|
|
}
|
|
if (!defined('CERTIF_LNE') || (int) constant('CERTIF_LNE') === 0) {
|
|
return '';
|
|
}
|
|
if (!$ignoremodule && !isModEnabled('blockedlog')) {
|
|
return '';
|
|
}
|
|
|
|
// All conditions are ok to become a LNE certified version
|
|
return ($ignoredev ? '' : 'NOT_BETA+').'FR+CERTIF_LNE_IS_1'.($ignoremodule ? '' : '+MODENABLED');
|
|
}
|
|
|
|
|
|
/**
|
|
* Return if the application is executed with the LNE requirements on.
|
|
* This function can be used to block some features like custom receipts, or to enable others like showing the information "Certified LNE".
|
|
*
|
|
* @param int $blockedlogtestalreadydone Test on blockedlog used already done and we suppose it is true.
|
|
* @param int $blockedlogmodulealreadydone Test on blockedlog module already done and we suppose it is true.
|
|
* @return boolean True or false
|
|
*/
|
|
function isALNERunningVersion($blockedlogtestalreadydone = 0, $blockedlogmodulealreadydone = 0)
|
|
{
|
|
// For Debug help: Constant CERTIF_LNE can be set 2 by developers get mode 1 compliant with dev env.
|
|
// Note that you can force, with this constant, the enabling of the restrictions,
|
|
// but there is no way to force the disabling of a restriction.
|
|
if (defined('CERTIF_LNE') && (int) constant('CERTIF_LNE') === 2 // Value is 2 for debug purpose to enable restriction except https for dev env.
|
|
&& ($blockedlogmodulealreadydone || isModEnabled('blockedlog'))
|
|
&& ($blockedlogtestalreadydone || isBlockedLogUsed())) {
|
|
return true;
|
|
}
|
|
if (defined('CERTIF_LNE') && (int) constant('CERTIF_LNE') === 1 // Value is 1 when version is certified
|
|
&& ($blockedlogmodulealreadydone || isModEnabled('blockedlog'))
|
|
&& ($blockedlogtestalreadydone || isBlockedLogUsed())) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Return if the blocked log was already used to block some events.
|
|
*
|
|
* @param int<0,1> $ignoresystem Ignore system events for the test
|
|
* @return boolean True if blocked log was already used, false if not
|
|
*/
|
|
function isBlockedLogUsed($ignoresystem = 0)
|
|
{
|
|
global $conf, $db;
|
|
|
|
$result = true; // by default restrictions are on, so we can't disable them
|
|
|
|
// Note: if module on, we suppose it is used, if not, we check in case of it was disabled.
|
|
if (!isModEnabled('blockedlog')) {
|
|
// Test the cache key
|
|
if (array_key_exists('isblockedlogused', $conf->cache)) {
|
|
return $conf->cache['isblockedlogused'.$ignoresystem];
|
|
}
|
|
|
|
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."blockedlog";
|
|
$sql .= " WHERE entity = ".((int) $conf->entity); // Sharing entity in blocked log will never be allowed
|
|
if ($ignoresystem) {
|
|
$sql .= " AND action NOT IN ('MODULE_SET', 'MODULE_RESET')";
|
|
}
|
|
$sql .= $db->plimit(1);
|
|
|
|
$resql = $db->query($sql);
|
|
if ($resql !== false) {
|
|
$obj = $db->fetch_object($resql);
|
|
if (!$obj) {
|
|
$result = false;
|
|
}
|
|
} else {
|
|
dol_print_error($db);
|
|
}
|
|
|
|
$conf->cache['isblockedlogused'.$ignoresystem] = $result;
|
|
}
|
|
|
|
dol_syslog("isBlockedLogUsed: ignoresystem=".$ignoresystem." returns ".(string) $result);
|
|
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* Add legal mention
|
|
*
|
|
* @param TCPDF $pdf Object PDF
|
|
* @param Translate $outputlangs Object lang
|
|
* @param Societe $seller Seller company
|
|
* @param int $default_font_size Default font size
|
|
* @param float $posy Y position
|
|
* @param CommonDocGenerator $pdftemplate PDF template
|
|
* @return int 0 if nothing done, 1 if a mention was printed
|
|
*/
|
|
function pdfCertifMentionblockedLog(&$pdf, $outputlangs, $seller, $default_font_size, &$posy, $pdftemplate)
|
|
{
|
|
$result = 0;
|
|
|
|
if (in_array($seller->country_code, array('FR'))) {
|
|
$outputlangs->load("blockedlog");
|
|
$blockedlog_mention = '';
|
|
|
|
$isalne = isALNEQualifiedVersion(); // If necessary, we could replace with "if isALNERunningVersion()"
|
|
if ($isalne == 'CERTIF_LNE_IS_2') {
|
|
$blockedlog_mention = $outputlangs->transnoentitiesnoconv("InvoiceGeneratedWithLNECandidatePOSSystem");
|
|
} elseif ($isalne) {
|
|
$blockedlog_mention = $outputlangs->transnoentitiesnoconv("InvoiceGeneratedWithLNECertifiedPOSSystem");
|
|
}
|
|
|
|
if ($blockedlog_mention) {
|
|
$pdf->SetFont('', '', $default_font_size - 2);
|
|
$pdf->SetXY($pdftemplate->marge_gauche, $posy);
|
|
$pdf->MultiCell(100, 3, $blockedlog_mention, 0, 'L', false);
|
|
$posy = $pdf->GetY();
|
|
$result = 1;
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* sumAmountsForUnalterableEvent
|
|
*
|
|
* @param BlockedLog $block Object BlockedLog
|
|
* @param array<string,int> $refinvoicefound Array of ref of invoice already found (to avoid duplicates. Should be useless but just in case of)
|
|
* @param array<string,array<string,float>> $totalhtamount Array of total per code event and module
|
|
* @param array<string,array<string,float>> $totalvatamount Array of total per code event and module
|
|
* @param array<string,array<string,float>> $totalamount Array of total per code event and module
|
|
* @param float $total_ht Total HT
|
|
* @param float $total_vat Total VAT
|
|
* @param float $total_ttc Total TTC
|
|
* @return int Return > 0
|
|
*/
|
|
function sumAmountsForUnalterableEvent($block, &$refinvoicefound, &$totalhtamount, &$totalvatamount, &$totalamount, &$total_ht, &$total_vat, &$total_ttc)
|
|
{
|
|
if ($block->action == 'BILL_VALIDATE') {
|
|
$total_ht = $block->object_data->total_ht;
|
|
$total_vat = $block->object_data->total_tva;
|
|
$total_ttc = $block->object_data->total_ttc;
|
|
|
|
// Init to avoid warnings if not initialized yet
|
|
if (!isset($totalamount[$block->action][$block->module_source])) {
|
|
$totalhtamount[$block->action][$block->module_source] = 0;
|
|
$totalvatamount[$block->action][$block->module_source] = 0;
|
|
$totalamount[$block->action][$block->module_source] = 0;
|
|
}
|
|
|
|
// We add total for the invoice if "invoice validate event" not yet met.
|
|
// If we already met the event for this object, we keep only first one but this should not happen because edition of validated invoice is not allowed on secured versions.
|
|
if (empty($refinvoicefound[$block->ref_object])) {
|
|
$totalhtamount[$block->action][$block->module_source] += $total_ht;
|
|
$totalvatamount[$block->action][$block->module_source] += $total_vat;
|
|
$totalamount[$block->action][$block->module_source] += $total_ttc;
|
|
}
|
|
$refinvoicefound[$block->ref_object] = 1;
|
|
} elseif ($block->action == 'PAYMENT_CUSTOMER_CREATE' || $block->action == 'PAYMENT_CUSTOMER_DELETE') {
|
|
$total_ht = $block->object_data->amount;
|
|
$total_vat = 0;
|
|
$total_ttc = $block->object_data->amount;
|
|
|
|
//$actionkey = $block->action;
|
|
$actionkey = 'PAYMENT_CUSTOMER';
|
|
|
|
// Init to avoid warnings if not initialized yet
|
|
if (!isset($totalamount[$actionkey][$block->module_source])) {
|
|
$totalhtamount[$actionkey][$block->module_source] = 0;
|
|
$totalvatamount[$actionkey][$block->module_source] = 0;
|
|
$totalamount[$actionkey][$block->module_source] = 0;
|
|
}
|
|
|
|
$totalhtamount[$actionkey][$block->module_source] += $total_ht;
|
|
$totalvatamount[$actionkey][$block->module_source] += $total_vat;
|
|
$totalamount[$actionkey][$block->module_source] += $total_ttc;
|
|
} else {
|
|
$total_ttc = $block->amounts;
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
|
|
/**
|
|
* Call remote API service to get the obfuscation key.
|
|
* This function is only called by blockedlog->getObfuscationKey();
|
|
*
|
|
* @param string $idprof1 Counter ID/value of ne record
|
|
* @param string $registrationnumber Registration number
|
|
* @param boolean $force False. Use true for tests.
|
|
* @return string Obfuscationkey or 'ERROR ...' if error.
|
|
*/
|
|
function callApiToGetObfuscationKey($idprof1, $registrationnumber, $force = false)
|
|
{
|
|
global $mysoc, $conf;
|
|
|
|
$obfuscationkey = 'ERROR';
|
|
|
|
if (empty($idprof1)) {
|
|
dol_syslog("callApiToGetObfuscationKey was called with empty idprof1", LOG_DEBUG);
|
|
return 'ERROR callApiToGetObfuscationKey was called with empty idprof1';
|
|
}
|
|
|
|
//if ((isALNERunningVersion(1) || $force) && $mysoc->country_code == 'FR') {
|
|
$url_for_ping = getDolGlobalString('MAIN_URL_FOR_PING', "https://ping.dolibarr.org/");
|
|
|
|
$algo = 'sha256';
|
|
$hash_unique_id = getHashUniqueIdOfRegistration($algo); // The hash of the unique IDof instance
|
|
|
|
$t = microtime(true);
|
|
$micro = sprintf("%06d", (int) (($t - floor($t)) * 1000000));
|
|
|
|
$data = '';
|
|
$data .= 'hash_algo=dol_hash-'.urlencode($algo);
|
|
$data .= '&hash_unique_id='.urlencode($hash_unique_id);
|
|
$data .= '&action=dolibarrgetkeyobfuscation';
|
|
$data .= '&datesys='.urlencode(dol_print_date(dol_now('gmt'), 'standard', 'gmt').'.'.$micro);
|
|
$data .= '&version='.(float) DOL_VERSION;
|
|
$data .= '&version_full='.urlencode(DOL_VERSION);
|
|
$data .= '&versionblockedlog='.(float) getBlockedLogVersionToShow();
|
|
$data .= '&versionblockedlog_full='.urlencode(getBlockedLogVersionToShow());
|
|
|
|
$data .= '&entity='.(int) $conf->entity;
|
|
|
|
$data .= '&company_idprof1='.urlencode(dol_sanitizeKeyCode($idprof1));
|
|
$data .= '®istrationnumber='.urlencode(dol_sanitizeKeyCode($registrationnumber));
|
|
|
|
$addheaders = array();
|
|
$timeoutconnect = 3;
|
|
$timeoutresponse = 3;
|
|
|
|
dol_syslog("callApiToGetObfuscationKey call remote URL idprod1=".dol_sanitizeKeyCode($idprof1), LOG_DEBUG);
|
|
dol_syslog("callApiToGetObfuscationKey call remote URL idprod1=".dol_sanitizeKeyCode($idprof1), LOG_DEBUG, 0, '_dolibarrgetkeyobfuscation');
|
|
|
|
include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
|
|
try {
|
|
$tmpresult = getURLContent($url_for_ping, 'POST', $data, 1, $addheaders, array('https'), 0, -1, $timeoutconnect, $timeoutresponse, array());
|
|
usleep(10000);
|
|
|
|
// Add a warning in log in case of error
|
|
if ($tmpresult['http_code'] == 0 && !empty($tmpresult['curl_error_msg'])) {
|
|
$logerrormessage = 'Error: '.$tmpresult['curl_error_msg'];
|
|
$obfuscationkey .= ' '.$tmpresult['curl_error_msg'];
|
|
dol_syslog("callApiToGetObfuscationKey result error when getting obfuscation key: ".$logerrormessage, LOG_WARNING);
|
|
dol_syslog("callApiToGetObfuscationKey result error when getting obfuscation key: ".$logerrormessage, LOG_WARNING, 0, '_dolibarrgetkeyobfuscation');
|
|
} elseif ($tmpresult['http_code'] != 200) {
|
|
$logerrormessage = 'Error: '.$tmpresult['http_code'].' '.$tmpresult['content'];
|
|
$obfuscationkey .= ' '.$tmpresult['http_code'].' '.$tmpresult['content'];
|
|
dol_syslog("callApiToGetObfuscationKey result error when getting obfuscation key: ".$logerrormessage, LOG_WARNING);
|
|
dol_syslog("callApiToGetObfuscationKey result error when getting obfuscation key: ".$logerrormessage, LOG_WARNING, 0, '_dolibarrgetkeyobfuscation');
|
|
} else {
|
|
$reg = array();
|
|
if (preg_match('/(DOLOBFUSCKEY.*)/', $tmpresult['content'], $reg)) { // gitleaks:allow $tmpresult['content'] may contains text comments before the line 'DOLOBFUSCKEY1...,DOLOBFUSCKEY2...'
|
|
$obfuscationkey = $reg[1];
|
|
dol_syslog("callApiToGetObfuscationKey we got the remote obfuscation key", LOG_DEBUG);
|
|
dol_syslog("callApiToGetObfuscationKey we got the remote obfuscation key", LOG_DEBUG, 0, '_dolibarrgetkeyobfuscation');
|
|
} else {
|
|
$obfuscationkey .= ' '.$tmpresult['content'];
|
|
dol_syslog("callApiToGetObfuscationKey result error when getting obfuscation key: ".$tmpresult['content'], LOG_WARNING);
|
|
dol_syslog("callApiToGetObfuscationKey result error when getting obfuscation key: ".$tmpresult['content'], LOG_WARNING, 0, '_dolibarrgetkeyobfuscation');
|
|
}
|
|
}
|
|
} catch (Exception $e) {
|
|
$obfuscationkey .= ' '.$e->getMessage();
|
|
dol_syslog("callApiToGetObfuscationKey result error ".$e->getMessage(), LOG_ERR);
|
|
dol_syslog("callApiToGetObfuscationKey result error ".$e->getMessage(), LOG_ERR, 0, '_dolibarrgetkeyobfuscation');
|
|
}
|
|
|
|
return $obfuscationkey;
|
|
}
|
|
|
|
|
|
/**
|
|
* Call remote API service to push the last counter and signature
|
|
*
|
|
* @param int $id Counter ID/value of ne record
|
|
* @param string $signature Signature of new record
|
|
* @param int $datecreation Date creation of new record
|
|
* @param int $test Add property test to 1 if it is for test
|
|
* @param int $previousid Counter ID/value of previous record
|
|
* @param string $previoussignature Signature of previous record
|
|
* @param int $previousdatecreation Date creation of previous record
|
|
* @return int Return <0 if KO, 0 if nothing done, >0 if OK
|
|
*/
|
|
/*
|
|
function callApiToPushCounter($id, $signature, $datecreation, $test, $previousid, $previoussignature, $previousdatecreation)
|
|
{
|
|
global $mysoc, $conf;
|
|
|
|
if (isALNERunningVersion(1) && $mysoc->country_code == 'FR') {
|
|
// Push last rowid + signature to remote dolibarr server
|
|
// TODO Do it only for selected events: BILL_VALIDATE ?
|
|
|
|
// Code here is similar to the one into printCodeForPing(), except that message code/properties/fields may differ.
|
|
$url_for_ping = getDolGlobalString('MAIN_URL_FOR_PING', "https://ping.dolibarr.org/");
|
|
|
|
$algo = 'sha256';
|
|
$hash_unique_id = getHashUniqueIdOfRegistration($algo); // The hash of the unique IDof instance
|
|
|
|
$t = microtime(true);
|
|
$micro = sprintf("%06d", (int) (($t - floor($t)) * 1000000));
|
|
|
|
$data = '';
|
|
$data .= 'hash_algo=dol_hash-'.urlencode($algo);
|
|
$data .= '&hash_unique_id='.urlencode($hash_unique_id);
|
|
$data .= '&action=dolibarrpushcounter';
|
|
$data .= '&datesys='.urlencode(dol_print_date(dol_now('gmt'), 'standard', 'gmt').'.'.$micro);
|
|
$data .= '&version='.(float) DOL_VERSION;
|
|
$data .= '&version_full='.urlencode(DOL_VERSION);
|
|
$data .= '&versionblockedlog='.(float) getBlockedLogVersionToShow();
|
|
$data .= '&versionblockedlog_full='.urlencode(getBlockedLogVersionToShow());
|
|
|
|
$data .= '&entity='.(int) $conf->entity;
|
|
|
|
$data .= '&lastrowid='.(int) $id;
|
|
$data .= '&lastsignature='.urlencode($signature);
|
|
$data .= '&lastdatecreation='.urlencode(dol_print_date($datecreation, 'standard', 'gmt'));
|
|
$data .= '&previousrowid='.(int) $previousid;
|
|
$data .= '&previoussignature='.urlencode($previoussignature);
|
|
$data .= '&previousdatecreation='.urlencode(dol_print_date($previousdatecreation, 'standard', 'gmt'));
|
|
if ($test) {
|
|
$data .= '&test=1';
|
|
}
|
|
|
|
$addheaders = array();
|
|
$timeoutconnect = 1;
|
|
$timeoutresponse = 1;
|
|
|
|
$conf->global->BLOCKEDLOG_RANDOMRANGE_FOR_TRACKING = 1; // Force probability to 1
|
|
|
|
// Probability will be between 1/10 by default and 1/1 if const BLOCKEDLOG_RANDOMRANGE_FOR_TRACKING is set to 1. Can't be lower than 1/10.
|
|
$BLOCKEDLOG_RANDOMRANGE_FOR_TRACKING = min(10, getDolGlobalInt('BLOCKEDLOG_RANDOMRANGE_FOR_TRACKING', 10));
|
|
$random = 1;
|
|
//$BLOCKEDLOG_RANDOMRANGE_FOR_TRACKING = 1; // To force track at every call
|
|
if ($BLOCKEDLOG_RANDOMRANGE_FOR_TRACKING > 1) {
|
|
$random = random_int(1, (int) $BLOCKEDLOG_RANDOMRANGE_FOR_TRACKING);
|
|
}
|
|
|
|
if ($random == 1) { // 1 chance on BLOCKEDLOG_RANDOMRANGE_FOR_TRACKING
|
|
dol_syslog("callApiToPushCounter create Record is selected to be remotely pushed for tracking", LOG_DEBUG);
|
|
|
|
include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
|
|
try {
|
|
$tmpresult = getURLContent($url_for_ping, 'POST', $data, 1, $addheaders, array('https'), 0, -1, $timeoutconnect, $timeoutresponse, array(), '_dolibarrpushcounter');
|
|
usleep(1000);
|
|
|
|
// Add a warning in log in case of error
|
|
if ($tmpresult['http_code'] != 200) {
|
|
$logerrormessage = 'Error: '.$tmpresult['http_code'].' '.$tmpresult['content'];
|
|
dol_syslog("callApiToPushCounter create Error when pushing track info: ".$logerrormessage, LOG_WARNING);
|
|
}
|
|
} catch (Exception $e) {
|
|
dol_syslog("callApiToPushCounter create Error ".$e->getMessage(), LOG_ERR);
|
|
}
|
|
} else {
|
|
dol_syslog("callApiToPushCounter create Record is NOT selected to be remotely pushed for tracking", LOG_DEBUG);
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
*/
|
|
|
|
|
|
/**
|
|
* Return if user is a tax auditor.
|
|
* Must be an external user and BLOCKEDLOG_FOR_TAX_AUDITOR must be set to 1 OR
|
|
* BLOCKEDLOG_FOR_TAX_AUDITOR must be set to 2
|
|
*
|
|
* @return int Return > 0 if user is an external user so must be restricted to archive control feature
|
|
*/
|
|
function userIsTaxAuditor()
|
|
{
|
|
global $user;
|
|
|
|
return (((getDolGlobalString('BLOCKEDLOG_FOR_TAX_AUDITOR') && $user->socid) || getDolGlobalString('BLOCKEDLOG_FOR_TAX_AUDITOR') == '2') ? 1 : 0);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Add some information from the blockedlog module
|
|
*
|
|
* @param TCPDF $pdf Object PDF
|
|
* @param Translate $outputlangs Object lang for output
|
|
* @param float $page_height Height of page
|
|
* @param Facture $object Object invoice
|
|
* @param int $w Width for text
|
|
* @param float $posx Pos x
|
|
* @param float $posy Pos y
|
|
* @return void
|
|
*/
|
|
function pdfWriteBlockedLogSignature(&$pdf, $outputlangs, $page_height, $object, &$w, &$posx, &$posy)
|
|
{
|
|
global $db;
|
|
|
|
// Transaction ID
|
|
if (isALNERunningVersion() && isModEnabled('blockedlog')) {
|
|
if ($object->status > $object::STATUS_DRAFT) {
|
|
$unalterablelogid = 'UNDEFINED'; // By default
|
|
|
|
$sql = "SELECT signature FROM ".MAIN_DB_PREFIX."blockedlog";
|
|
$sql .= " WHERE action = 'BILL_VALIDATE' AND element = 'facture' AND ref_object = '".$db->escape($object->ref)."'";
|
|
$sql .= $db->order('rowid', 'DESC');
|
|
$sql .= $db->plimit(1);
|
|
|
|
$resql = $db->query($sql);
|
|
if ($resql) {
|
|
$obj = $db->fetch_object($resql);
|
|
if ($obj) {
|
|
$unalterablelogid = $obj->signature;
|
|
}
|
|
}
|
|
|
|
if ($unalterablelogid != 'UNDEFINED') {
|
|
$pdf->SetXY($posx, $posy);
|
|
$pdf->SetTextColor(0, 0, 60);
|
|
$pdf->MultiCell($w, 3, $outputlangs->transnoentities("SignatureID")." : ".dol_trunc(strtoupper($unalterablelogid), 10), '', 'R');
|
|
$posy += 3;
|
|
}
|
|
|
|
$isADuplicata = ($object->pos_print_counter >= 2);
|
|
if ($isADuplicata) {
|
|
$pdf->SetXY($posx, $posy);
|
|
$pdf->SetTextColor(0, 0, 60);
|
|
$pdf->MultiCell($w, 3, '*** '.$outputlangs->trans("DUPLICATA").(getDolGlobalString('TAKEPOS_SHOW_PRINT_COUNTER_ON_RECEIPT') ? ' (no '.($object->pos_print_counter - 1).')' : '').' ***', '', 'R');
|
|
$posy += 3;
|
|
}
|
|
}
|
|
|
|
if ($object->status == $object::STATUS_DRAFT) {
|
|
$pdf->SetXY($posx, $posy);
|
|
$pdf->SetTextColor(0, 0, 60);
|
|
$pdf->MultiCell($w, 3, '*** '.strtoupper($outputlangs->trans("TemporaryReceipt")).' ***', '', 'R');
|
|
$posy += 3;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Migrate an old database to add the .end flag.
|
|
*
|
|
* @return int Return -1 if KO, 1 if OK
|
|
*/
|
|
function migrate_blockedlog_add_end_file()
|
|
{
|
|
global $conf, $db, $langs, $mysoc;
|
|
|
|
include_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php';
|
|
|
|
$blocklog_static = new BlockedLog($db);
|
|
|
|
// Loop on all entities found in llx_blockedlog
|
|
$listofentities = array();
|
|
$sql = "SELECT DISTINCT entity FROM ".MAIN_DB_PREFIX."blockedlog";
|
|
$resql = $db->query($sql);
|
|
if ($resql) {
|
|
while ($obj = $db->fetch_object($resql)) {
|
|
$listofentities[] = $obj->entity;
|
|
}
|
|
} else {
|
|
print '<tr class="trforrunsql"><td colspan="4">';
|
|
print '<b>'.$langs->trans('InitEndFlagFile')."</b>:\n";
|
|
print 'Error: Failed to get list of entities in blockedlog table';
|
|
print '</td></tr>';
|
|
return -1;
|
|
}
|
|
|
|
foreach ($listofentities as $entity) {
|
|
print '<tr class="trforrunsql"><td colspan="4">';
|
|
print '<b>'.$langs->trans('InitEndFlagFile')." (entity = ".$entity.")</b>:\n";
|
|
|
|
// Load the data of company (country, ...). Erase current one so this function can be used by migration script only.
|
|
$conf->setEntityValues($db, $entity);
|
|
$mysoc = new Societe($db);
|
|
$mysoc->setMysoc($conf);
|
|
|
|
$maxid = 0;
|
|
$sql = "SELECT MAX(rowid) as maxid FROM ".MAIN_DB_PREFIX."blockedlog WHERE entity = ".((int) $entity);
|
|
$resql = $db->query($sql);
|
|
if ($resql) {
|
|
$obj = $db->fetch_object($resql);
|
|
if ($obj) {
|
|
$maxid = $obj->maxid;
|
|
}
|
|
} else {
|
|
print 'Error: Failed to get max id of blockedlog table';
|
|
print '</td></tr>';
|
|
return -1;
|
|
}
|
|
|
|
$blocklog_static->fetch($maxid);
|
|
|
|
$lockfile = $blocklog_static->getEndOfChainFlagFile();
|
|
|
|
// Lock acquired, we can now write the new .end file
|
|
$stringtowrite = 'BLOCKEDLOGHEAD '.$blocklog_static->id." ".dol_print_date($blocklog_static->date_creation, 'dayhourrfc', 'gmt')." ".(string) $blocklog_static->signature;
|
|
|
|
if (isALNERunningVersion(1) && $mysoc->country_code == 'FR') {
|
|
$remoteobfuscationkey = $blocklog_static->getObfuscationKey();
|
|
if (empty($remoteobfuscationkey)) {
|
|
print "Error: Failed to get the remote obfuscation key. We can't record the end of chain flag file so we abort the transaction.";
|
|
print '</td></tr>';
|
|
return -1;
|
|
}
|
|
|
|
$stringtowriteencoded = dolEncrypt($stringtowrite, $remoteobfuscationkey, '', '', 'dolobfuscationv1-'.$mysoc->idprof1.'-'.$blocklog_static->id);
|
|
} else {
|
|
$stringtowriteencoded = dolEncrypt($stringtowrite, '', '', '', 'dolcrypt-'.$mysoc->idprof1.'-'.$blocklog_static->id);
|
|
}
|
|
|
|
// Update or create the .end flag file.
|
|
if (defined('BLOCKEDLOG_END_FLAG_IN_A_FILE')) {
|
|
$lockhandle = fopen($lockfile, 'w+');
|
|
if ($lockhandle) {
|
|
if (fwrite($lockhandle, $stringtowriteencoded."\n") === false) {
|
|
print "Cannot write to the blockedlog .end file ".$lockfile;
|
|
print '</td></tr>';
|
|
return -1;
|
|
}
|
|
|
|
fclose($lockhandle); // Remove the lock
|
|
dolChmod($lockfile);
|
|
} else {
|
|
print "Cannot open for writing the blockedlog .end file ".$lockfile;
|
|
print '</td></tr>';
|
|
return -1;
|
|
}
|
|
} else {
|
|
$sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
|
|
$sql .= " WHERE name = '".$db->escape(basename($lockfile))."' AND entity = ".((int) $conf->entity);
|
|
$resql = $db->query($sql);
|
|
|
|
$sql = "INSERT INTO ".MAIN_DB_PREFIX."const(name, value, type, visible, note, entity)";
|
|
$sql .= " VALUES('".$db->escape(basename($lockfile))."', '".$db->escape($stringtowriteencoded)."', 'chaine', 0, 'Blockedlog end of chain flag', ".((int) $conf->entity).")";
|
|
$resql = $db->query($sql);
|
|
if (!$resql) {
|
|
print "Cannot open for writing the blockedlog .end file ".basename($lockfile);
|
|
print '</td></tr>';
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
print $langs->trans("Done");
|
|
print '</td></tr>';
|
|
}
|
|
|
|
return 1;
|
|
}
|