dolibarr 21.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 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 $id;
41
42 public $picto = 'fa-shield-alt';
43
47 public $NbMaj;
51 public $NbNum;
55 public $NbSpe;
59 public $NbRepeat;
60
66 public $WithoutAmbi = 0;
67
71 public $Maj;
75 public $Min;
79 public $Nb;
83 public $Spe;
87 public $Ambi;
91 public $All;
92
101 public function __construct($db, $conf, $langs, $user)
102 {
103 $this->id = "Perso";
104 $this->length = $langs->trans("SetupPerso");
105
106 $this->db = $db;
107 $this->conf = $conf;
108 $this->langs = $langs;
109 $this->user = $user;
110
111 if (!getDolGlobalString('USER_PASSWORD_PATTERN')) {
112 // default value at auto generation (12 chars, 1 uppercase, 1 digit, 0 special char, 3 repeat max, exclude ambiguous characters).
113 dolibarr_set_const($db, "USER_PASSWORD_PATTERN", '12;1;1;0;3;1', 'chaine', 0, '', $conf->entity);
114 }
115
116 $this->Maj = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
117 $this->Min = strtolower($this->Maj);
118 $this->Nb = "0123456789";
119 $this->Spe = "!@#$%&*()_-+={}[]\\|:;'/";
120 $this->Ambi = array("1", "I", "l", "|", "O", "0");
121
122 $tabConf = explode(";", getDolGlobalString('USER_PASSWORD_PATTERN'));
123 $this->length2 = (int) $tabConf[0];
124 $this->NbMaj = $tabConf[1];
125 $this->NbNum = $tabConf[2];
126 $this->NbSpe = $tabConf[3];
127 $this->NbRepeat = $tabConf[4];
128 $this->WithoutAmbi = (int) $tabConf[5];
129 }
130
136 private function initAll()
137 {
138 if ($this->WithoutAmbi) {
139 $this->Maj = str_replace($this->Ambi, "", $this->Maj);
140 $this->Min = str_replace($this->Ambi, "", $this->Min);
141 $this->Nb = str_replace($this->Ambi, "", $this->Nb);
142 $this->Spe = str_replace($this->Ambi, "", $this->Spe);
143 }
144
145 $pattern = $this->Min.(!empty($this->NbMaj) ? $this->Maj : '').(!empty($this->NbNum) ? $this->Nb : '').(!empty($this->NbSpe) ? $this->Spe : '');
146 $this->All = str_shuffle($pattern);
147 }
148
154 public function getDescription()
155 {
156 global $langs;
157 return $langs->trans("PasswordGenerationPerso");
158 }
159
165 public function getExample()
166 {
167 return $this->getNewGeneratedPassword();
168 }
169
177 public function getNewGeneratedPassword()
178 {
179 $this->initAll();
180
181 $pass = "";
182 for ($i = 0; $i < $this->NbMaj; $i++) {
183 // Y
184 $pass .= $this->Maj[mt_rand(0, strlen($this->Maj) - 1)];
185 }
186
187 for ($i = 0; $i < $this->NbNum; $i++) {
188 // X
189 $pass .= $this->Nb[mt_rand(0, strlen($this->Nb) - 1)];
190 }
191
192 for ($i = 0; $i < $this->NbSpe; $i++) {
193 // @
194 $pass .= $this->Spe[mt_rand(0, strlen($this->Spe) - 1)];
195 }
196
197 for ($i = strlen($pass); $i < $this->length2; $i++) {
198 // y
199 $pass .= $this->All[mt_rand(0, strlen($this->All) - 1)];
200 }
201
202 $pass = str_shuffle($pass);
203
204 if ($this->validatePassword($pass)) {
205 return $pass;
206 }
207
208 return $this->getNewGeneratedPassword(); // warning, may generate infinite loop if conditions are not possible
209 }
210
218 public function validatePassword($password)
219 {
220 global $langs;
221
222 $this->initAll(); // For the case this method is called alone
223
224 $password_a = preg_split('//u', $password, 0, PREG_SPLIT_NO_EMPTY);
225 $maj = preg_split('//u', $this->Maj, 0, PREG_SPLIT_NO_EMPTY);
226 $num = preg_split('//u', $this->Nb, 0, PREG_SPLIT_NO_EMPTY);
227 $spe = preg_split('//u', $this->Spe, 0, PREG_SPLIT_NO_EMPTY);
228 /*
229 $password_a = str_split($password);
230 $maj = str_split($this->Maj);
231 $num = str_split($this->Nb);
232 $spe = str_split($this->Spe);
233 */
234
235 if (dol_strlen($password) < $this->length2) {
236 $langs->load("other");
237 $this->error = $langs->trans("YourPasswordMustHaveAtLeastXChars", $this->length2);
238 return 0;
239 }
240
241 if (count(array_intersect($password_a, $maj)) < $this->NbMaj) {
242 $langs->load("other");
243 $this->error = $langs->trans('PasswordNeedAtLeastXUpperCaseChars', $this->NbMaj);
244 return 0;
245 }
246
247 if (count(array_intersect($password_a, $num)) < $this->NbNum) {
248 $langs->load("other");
249 $this->error = $langs->trans('PasswordNeedAtLeastXDigitChars', $this->NbNum);
250 return 0;
251 }
252
253 if (count(array_intersect($password_a, $spe)) < $this->NbSpe) {
254 $langs->load("other");
255 $this->error = $langs->trans('PasswordNeedAtLeastXSpecialChars', $this->NbSpe);
256 return 0;
257 }
258
259 if (!$this->consecutiveIterationSameCharacter($password)) {
260 $langs->load("other");
261 $this->error = $langs->trans('PasswordNeedNoXConsecutiveChars', $this->NbRepeat);
262 return 0;
263 }
264
265 return 1;
266 }
267
274 public function consecutiveIterationSameCharacter($password)
275 {
276 $this->initAll();
277
278 if (empty($this->NbRepeat)) {
279 return true;
280 }
281
282 $char = preg_split('//u', $password, 0, PREG_SPLIT_NO_EMPTY);
283
284 $last = "";
285 $count = 0;
286 foreach ($char as $c) {
287 if ($c != $last) {
288 $last = $c;
289 $count = 1;
290 //print "Char $c - count = $count\n";
291 continue;
292 }
293
294 $count++;
295 //print "Char $c - count = $count\n";
296
297 if ($count > $this->NbRepeat) {
298 return false;
299 }
300 }
301
302 return true;
303 }
304}
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).
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')
Make a strlen call.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
conf($dolibarr_main_document_root)
Load conf file (file must exists)
Definition inc.php:420
$conf db user
Active Directory does not allow anonymous connections.
Definition repair.php:141