dolibarr 25.0.0-alpha
modGeneratePassPerso.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2014 Teddy Andreotti <125155@supinfo.com>
4 * Copyright (C) 2017 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 * or see https://www.gnu.org/
21 */
22
29require_once DOL_DOCUMENT_ROOT.'/core/modules/security/generate/modules_genpassword.php';
30
31
36{
40 public $position = 60;
41
45 public $id;
46
47 public $picto = 'fa-shield-alt';
48
52 public $NbMaj;
56 public $NbNum;
60 public $NbSpe;
64 public $NbRepeat;
65
71 public $WithoutAmbi = 0;
72
76 public $Maj;
80 public $Min;
84 public $Nb;
88 public $Spe;
92 public $Ambi;
96 public $All;
97
106 public function __construct($db, $conf, $langs, $user)
107 {
108 $this->id = "Perso";
109 $this->length = $langs->trans("SetupPerso");
110
111 $this->db = $db;
112 $this->conf = $conf;
113 $this->langs = $langs;
114 $this->user = $user;
115
116 if (!getDolGlobalString('USER_PASSWORD_PATTERN')) {
117 // default value at auto generation (12 chars, 1 uppercase, 1 digit, 0 special char, 3 repeat max, exclude ambiguous characters).
118 dolibarr_set_const($db, "USER_PASSWORD_PATTERN", '12;1;1;0;3;1', 'chaine', 0, '', $conf->entity);
119 }
120
121 $this->Maj = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
122 $this->Min = strtolower($this->Maj);
123 $this->Nb = "0123456789";
124 $this->Spe = "!@#$%&*()_-+={}[]\\|:;'/";
125 $this->Ambi = array("1", "I", "l", "|", "O", "0");
126
127 $tabConf = explode(";", getDolGlobalString('USER_PASSWORD_PATTERN'));
128 // When the 'none' model is forbidden on this installation, the Perso model cannot enforce a
129 // minimum length below the floor returned by getPasswordPatternMinLength(), even if the
130 // stored pattern still holds a lower value.
131 $this->length2 = max((int) $tabConf[0], getPasswordPatternMinLength());
132 $this->NbMaj = $tabConf[1];
133 $this->NbNum = $tabConf[2];
134 $this->NbSpe = $tabConf[3];
135 $this->NbRepeat = $tabConf[4];
136 $this->WithoutAmbi = (int) $tabConf[5];
137 }
138
144 private function initAll()
145 {
146 if ($this->WithoutAmbi) {
147 $this->Maj = str_replace($this->Ambi, "", $this->Maj);
148 $this->Min = str_replace($this->Ambi, "", $this->Min);
149 $this->Nb = str_replace($this->Ambi, "", $this->Nb);
150 $this->Spe = str_replace($this->Ambi, "", $this->Spe);
151 }
152
153 $pattern = $this->Min.(!empty($this->NbMaj) ? $this->Maj : '').(!empty($this->NbNum) ? $this->Nb : '').(!empty($this->NbSpe) ? $this->Spe : '');
154
155 // Use the Fisher-Yate to shake (this replace str_shuffle)
156 $passwordArray = str_split($pattern);
157 for ($i = count($passwordArray) - 1; $i > 0; $i--) {
158 $j = random_int(0, $i);
159 $tmp = $passwordArray[$i];
160 $passwordArray[$i] = $passwordArray[$j];
161 $passwordArray[$j] = $tmp;
162 }
163 $this->All = implode('', $passwordArray);
164 }
165
171 public function getDescription()
172 {
173 global $langs;
174 return $langs->trans("PasswordGenerationPerso");
175 }
176
182 public function getExample()
183 {
184 return $this->getNewGeneratedPassword();
185 }
186
194 public function getNewGeneratedPassword()
195 {
196 $this->initAll();
197
198 $pass = "";
199 for ($i = 0; $i < $this->NbMaj; $i++) {
200 // Y
201 $pass .= $this->Maj[random_int(0, strlen($this->Maj) - 1)];
202 }
203
204 for ($i = 0; $i < $this->NbNum; $i++) {
205 // X
206 $pass .= $this->Nb[random_int(0, strlen($this->Nb) - 1)];
207 }
208
209 for ($i = 0; $i < $this->NbSpe; $i++) {
210 // @
211 $pass .= $this->Spe[random_int(0, strlen($this->Spe) - 1)];
212 }
213
214 for ($i = strlen($pass); $i < $this->length2; $i++) {
215 // y
216 $pass .= $this->All[random_int(0, strlen($this->All) - 1)];
217 }
218
219 // Use the Fisher-Yate to shake (this replace str_shuffle)
220 $passwordArray = str_split($pass);
221 for ($i = count($passwordArray) - 1; $i > 0; $i--) {
222 $j = random_int(0, $i);
223 $tmp = $passwordArray[$i];
224 $passwordArray[$i] = $passwordArray[$j];
225 $passwordArray[$j] = $tmp;
226 }
227 $pass = implode('', $passwordArray);
228
229 // Check that generation was ok
230 if ($this->validatePassword($pass)) {
231 return $pass;
232 }
233
234 return $this->getNewGeneratedPassword(); // warning, may generate infinite loop if conditions are not possible
235 }
236
244 public function validatePassword($password)
245 {
246 global $langs;
247
248 $this->initAll(); // For the case this method is called alone
249
250 dol_syslog("modGeneratePassPerso::validatePassword");
251
252 $password_a = preg_split('//u', $password, 0, PREG_SPLIT_NO_EMPTY);
253 $maj = preg_split('//u', $this->Maj, 0, PREG_SPLIT_NO_EMPTY);
254 $num = preg_split('//u', $this->Nb, 0, PREG_SPLIT_NO_EMPTY);
255 $spe = preg_split('//u', $this->Spe, 0, PREG_SPLIT_NO_EMPTY);
256 /*
257 $password_a = str_split($password);
258 $maj = str_split($this->Maj);
259 $num = str_split($this->Nb);
260 $spe = str_split($this->Spe);
261 */
262
263 if (dol_strlen($password) < $this->length2) {
264 $langs->load("other");
265 $this->error = $langs->trans("YourPasswordMustHaveAtLeastXChars", $this->length2);
266 return 0;
267 }
268
269 if (count(array_intersect($password_a, $maj)) < $this->NbMaj) {
270 $langs->load("other");
271 $this->error = $langs->trans('PasswordNeedAtLeastXUpperCaseChars', $this->NbMaj);
272 return 0;
273 }
274
275 if (count(array_intersect($password_a, $num)) < $this->NbNum) {
276 $langs->load("other");
277 $this->error = $langs->trans('PasswordNeedAtLeastXDigitChars', $this->NbNum);
278 return 0;
279 }
280
281 if (count(array_intersect($password_a, $spe)) < $this->NbSpe) {
282 $langs->load("other");
283 $this->error = $langs->trans('PasswordNeedAtLeastXSpecialChars', $this->NbSpe);
284 return 0;
285 }
286
287 if (!$this->consecutiveIterationSameCharacter($password)) {
288 $langs->load("other");
289 $this->error = $langs->trans('PasswordNeedNoXConsecutiveChars', $this->NbRepeat);
290 return 0;
291 }
292
293 return 1;
294 }
295
302 public function consecutiveIterationSameCharacter($password)
303 {
304 $this->initAll();
305
306 if (empty($this->NbRepeat)) {
307 return true;
308 }
309
310 $char = preg_split('//u', $password, 0, PREG_SPLIT_NO_EMPTY);
311
312 $last = "";
313 $count = 0;
314 foreach ($char as $c) {
315 if ($c != $last) {
316 $last = $c;
317 $count = 1;
318 //print "Char $c - count = $count\n";
319 continue;
320 }
321
322 $count++;
323 //print "Char $c - count = $count\n";
324
325 if ($count > $this->NbRepeat) {
326 return false;
327 }
328 }
329
330 return true;
331 }
332}
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).
$c
Definition line.php:353
Parent class for password rules/management modules.
Class to generate a password according to personal rules.
validatePassword($password)
Validate a password.
consecutiveIterationSameCharacter($password)
Check the consecutive iterations of the same character.
__construct($db, $conf, $langs, $user)
Constructor.
getExample()
Return an example of password generated by this module.
initAll()
Init the property ->All and clean ->Maj, ->Min, ->Nb and ->Spe with list of valid chars.
getNewGeneratedPassword()
Build new password.
getDescription()
Return description of module.
dol_strlen($string, $stringencoding='UTF-8', $binarycount=0)
Make a strlen call.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
conf($dolibarr_main_document_root, $realpathconf=null)
Load conf file (file must exists)
Definition inc.php:431
$conf db user
Active Directory does not allow anonymous connections.
Definition repair.php:166
getPasswordPatternMinLength()
Return the lowest value allowed for the minimum length (first field of USER_PASSWORD_PATTERN) of the ...