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