dolibarr 23.0.3
modBlockedLog.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2017-2025 Laurent Destailleur <eldy@users.sourcefore.net>
3 * Copyright (C) 2026 Regis Houssin <regis.houssin@inodbox.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
26include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
27include_once DOL_DOCUMENT_ROOT.'/blockedlog/lib/blockedlog.lib.php';
28
29
34{
40 public function __construct($db)
41 {
42 global $mysoc;
43
44 $this->db = $db;
45 $this->numero = 3200;
46 // Key text used to identify module (for permissions, menus, etc...)
47 $this->rights_class = 'blockedlog';
48
49 // Family can be 'crm','financial','hr','projects','products','ecm','technic','other'
50 // It is used to group modules in module setup page
51 $this->family = "base";
52 // Module position in the family on 2 digits ('01', '10', '20', ...)
53 $this->module_position = '76';
54 // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
55 $this->name = preg_replace('/^mod/i', '', get_class($this));
56 $this->description = "Enable a log on some business events into an unalterable log. This module may be mandatory for some countries.";
57
58 // Possible values for version are: 'development', 'experimental', 'dolibarr' or version
59 $this->version = 'dolibarr';
60 // Key used in llx_const table to save module status enabled/disabled (where MYMODULE is value of property name of module in uppercase)
61 $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
62 // Name of image file used for this module.
63 $this->picto = 'blockedlog';
64
65 // Data directories to create when module is enabled
66 $this->dirs = array();
67
68 // Config pages
69 //-------------
70 $this->config_page_url = array('blockedlog.php?withtab=1@blockedlog');
71
72 // Dependencies
73 //-------------
74 $this->hidden = false; // A condition to disable module
75 $this->depends = array('always'=>'modFacture'); // List of modules id that must be enabled if this module is enabled
76 $this->requiredby = array(); // List of modules id to disable if this one is disabled
77 $this->conflictwith = array(); // List of modules id this module is in conflict with
78 $this->langfiles = array('blockedlog');
79
80 $this->warnings_activation = array();
81 $this->warnings_activation_ext = array();
82 $this->warnings_unactivation = array('FR'=>'BlockedLogAreRequiredByYourCountryLegislation');
83
84 // Currently, activation is not automatic because only companies (in France) making invoices to non business customers must
85 // enable this module.
86 /*if (getDolGlobalString('BLOCKEDLOG_DISABLE_NOT_ALLOWED_FOR_COUNTRY')) {
87 $tmp = explode(',', getDolGlobalString('BLOCKEDLOG_DISABLE_NOT_ALLOWED_FOR_COUNTRY'));
88 $this->automatic_activation = array();
89 foreach($tmp as $countrycodekey)
90 {
91 $this->automatic_activation[$countrycodekey] = 'BlockedLogActivatedBecauseRequiredByYourCountryLegislation';
92 }
93 }*/
94 //var_dump($this->automatic_activation);
95
96 $this->always_enabled = (isModEnabled('blockedlog')
97 && getDolGlobalString('BLOCKEDLOG_DISABLE_NOT_ALLOWED_FOR_COUNTRY')
98 && in_array((empty($mysoc->country_code) ? '' : $mysoc->country_code), explode(',', getDolGlobalString('BLOCKEDLOG_DISABLE_NOT_ALLOWED_FOR_COUNTRY')))
99 && $this->alreadyUsed());
100
101 // Constants
102 //-----------
103 $this->const = array(
104 1=>array('BLOCKEDLOG_DISABLE_NOT_ALLOWED_FOR_COUNTRY', 'chaine', 'FR', 'This is list of country code where the module may be mandatory', 0, 'current', 0)
105 );
106
107 // New pages on tabs
108 // -----------------
109 $this->tabs = array();
110
111 // Boxes
112 //------
113 $this->boxes = array();
114
115 // Permissions
116 // -----------------
117 $this->rights = array(); // Permission array used by this module
118
119 $r = 1;
120 $this->rights[$r][0] = $this->numero + $r; // Permission id (must not be already used)
121 $this->rights[$r][1] = 'Read archived events and fingerprints'; // Permission label
122 $this->rights[$r][3] = 0; // Permission by default for new user (0/1)
123 $this->rights[$r][4] = 'read'; // In php code, permission will be checked by test if ($user->rights->mymodule->level1->level2)
124 $this->rights[$r][5] = '';
125
126 // Main menu entries
127 // -----------------
128 $r = 0;
129 $this->menu[$r] = array(
130 'fk_menu'=>'fk_mainmenu=tools', // Use 'fk_mainmenu=xxx' or 'fk_mainmenu=xxx,fk_leftmenu=yyy' where xxx is mainmenucode and yyy is a leftmenucode
131 'mainmenu'=>'tools',
132 'leftmenu'=>'blockedlogbrowser',
133 'type'=>'left', // This is a Left menu entry
134 'titre'=>'BrowseBlockedLog',
135 'prefix' => img_picto('', $this->picto, 'class="paddingright pictofixedwidth"'),
136 'url'=>'/blockedlog/admin/blockedlog_list.php?mainmenu=tools&leftmenu=blockedlogbrowser',
137 'langs'=>'blockedlog', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
138 'position'=>200,
139 'enabled'=>'isModEnabled("blockedlog")', // Define condition to show or hide menu entry. Use '$conf->mymodule->enabled' if entry must be visible if module is enabled. Use '$leftmenu==\'system\'' to show if leftmenu system is selected.
140 'perms'=>'$user->hasRight("blockedlog", "read")', // Use 'perms'=>'$user->hasRight("mymodule","level1","level2")' if you want your menu with a permission rules
141 'target'=>'',
142 'user'=>2, // 0=Menu for internal users, 1=external users, 2=both
143 );
144 $r++;
145 }
146
147
153 public function alreadyUsed()
154 {
155 require_once DOL_DOCUMENT_ROOT.'/blockedlog/lib/blockedlog.lib.php';
156
157 return isBlockedLogUsed();
158 }
159
160
169 public function init($options = '')
170 {
171 global $conf, $user;
172
173 $sql = array();
174
175
176 require_once DOL_DOCUMENT_ROOT . '/blockedlog/class/blockedlog.class.php';
177 $b = new BlockedLog($this->db);
178
179
180 $this->db->begin();
181
182 include_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
183 include_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
184
185 // Check that the HTTPS is forced
186 $s = $b->canBeEnabled();
187 if ($s) { // Activation not allowed
188 $this->error = $s;
189 return 0;
190 }
191
192 // Create HMAC if it does not exists yet
193 $hmac_encoded_secret_key = getDolGlobalString('BLOCKEDLOG_HMAC_KEY');
194 if (empty($hmac_encoded_secret_key)) {
195 // Add key
196 $hmac_secret_key = 'BLOCKEDLOGHMAC'.getRandomPassword(true); // This is using random_int for 32 chars
197
198 $result = dolibarr_set_const($this->db, 'BLOCKEDLOG_HMAC_KEY', $hmac_secret_key, 'chaine', 0, 'The secret key for HMAC used for blockedlog record', $conf->entity); // Will encrypt the value using dolCrypt and store it.
199
200 if ($result < 0) {
201 dol_print_error($this->db);
202 $this->db->rollback();
203
204 return 0;
205 }
206 } else {
207 // Decode the HMAC key
208 $hmac_secret_key = dolDecrypt($hmac_encoded_secret_key);
209
210 if (! preg_match('/^BLOCKEDLOGHMAC/', $hmac_secret_key)) {
211 $this->error = 'Error: Failed to decode the crypted value of the parameter BLOCKEDLOG_HMAC_KEY using the $dolibarr_main_crypt_key. A value was found in config parameters in database but decoding failed. May be the database data were restored onto another environment and the coding/decoding key $dolibarr_main_dolcrypt_key was not restored with the same value in conf.php file.';
212 $this->error .= 'Restore the value of $dolibarr_main_crypt_key that was used for encryption in database and restart the migration.';
213 $this->error .= 'If you don\'t use the Unalterable Log module, you can also remove the BLOCKEDLOG_HMAC_KEY entry from llx_const table. If you use the Unalterable Log, this is not possible because this will invalidate all past record.';
214 $this->db->rollback();
215
216 return 0;
217 }
218 }
219
220 $this->db->commit();
221
222
223 // We add an entry to show we enable module
224
225 $object = new stdClass();
226 $object->id = 0;
227 $object->element = 'module';
228 $object->ref = 'systemevent';
229 $object->entity = $conf->entity;
230 $object->date = dol_now();
231
232 // Add first entry in unalterable Log to track that module was activated
233 $action = 'MODULE_SET';
234 $result = $b->setObjectData($object, $action, 0, $user, null);
235
236 if ($result < 0) {
237 $this->error = $b->error;
238 $this->errors = $b->errors;
239 return 0;
240 }
241
242 $res = $b->create($user);
243 if ($res <= 0) {
244 $this->error = $b->error;
245 $this->errors = $b->errors;
246 return $res;
247 }
248
249 return $this->_init($sql, $options);
250 }
251
260 public function remove($options = '')
261 {
262 global $conf, $user;
263
264 $sql = array();
265
266 // If already used, we add an entry to show we enable module
267 require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php';
268
269 dol_syslog("modBlockedLog::remove option=".$options, LOG_DEBUG);
270
271 $object = new stdClass();
272 $object->id = 1;
273 $object->element = 'module';
274 $object->ref = 'systemevent';
275 $object->entity = $conf->entity;
276 $object->date = dol_now();
277 $object->label = 'Module disabled';
278
279 $b = new BlockedLog($this->db);
280 $result = $b->setObjectData($object, 'MODULE_RESET', 0, $user, null);
281 if ($result < 0) {
282 $this->error = $b->error;
283 $this->errors = $b->errors;
284 return 0;
285 }
286
287 if ($b->alreadyUsed(1)) {
288 // Unalterable log was already used.
289 if ($options != 'forcedisable' && !$b->canBeDisabled()) {
290 // Case we refuse to disable it
291 global $langs;
292 $this->error = $langs->trans('DisablingBlockedLogIsNotallowedOnceUsedExceptOnFullreset', $langs->transnoentitiesnoconv('BlockedLog'));
293 return 0;
294 } else {
295 // Case we disable it with a log
296 $res = $b->create($user, '0000000000'); // If already used for something else than SET or UNSET, we log with error
297 }
298 } else {
299 $res = $b->create($user);
300 }
301 if ($res <= 0) {
302 $this->error = $b->error;
303 $this->errors = $b->errors;
304 return $res;
305 }
306
307 return $this->_remove($sql, $options);
308 }
309
310
317 public function getDesc($foruseinpopupdesc = 0)
318 {
319 global $langs;
320 $langs->load("admin");
321
322 // If module description translation exists
323 $s = $langs->transnoentitiesnoconv("Module".$this->numero."Desc");
324
325 if ($foruseinpopupdesc) {
326 $langs->load("blockedlog");
327 $s .= '<br><br>';
328 if (isALNEQualifiedVersion(1, 1)) {
329 $s .= info_admin($langs->trans("UnalterableLogTool1FR"), 0, 0, 'warning');
330 }
331 }
332
333 return $s;
334 }
335}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
isALNEQualifiedVersion($ignoredev=0, $ignoremodule=0)
Return if the version is a candidate version to get the LNE certification and if the prerequisites ar...
isBlockedLogUsed($ignoresystem=0)
Return if the blocked log was already used to block some events.
Class to manage Blocked Log.
Class DolibarrModules.
_init($array_sql, $options='')
Enables a module.
_remove($array_sql, $options='')
Disable function.
Class to describe a BlockedLog module.
init($options='')
Function called when module is enabled.
alreadyUsed()
Check if module was already used before unactivation linked to warnings_unactivation property.
getDesc($foruseinpopupdesc=0)
Overwrite the common getDesc() method.
__construct($db)
Constructor.
global $mysoc
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_now($mode='gmt')
Return date for now.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='', $picto='')
Show information in HTML for admin users or standard users.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:128
dolDecrypt($chain, $key='')
Decode a string with a symmetric encryption.