dolibarr 19.0.3
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 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
27require_once DOL_DOCUMENT_ROOT.'/core/modules/security/generate/modules_genpassword.php';
28
29
34{
38 public $id;
39
40 public $picto = 'fa-shield-alt';
41
47 public $length;
48
54 public $length2;
55
56 public $NbMaj;
57 public $NbNum;
58 public $NbSpe;
59 public $NbRepeat;
60
66 public $WithoutAmbi = 0;
67
68 public $Maj;
69 public $Min;
70 public $Nb;
71 public $Spe;
72 public $Ambi;
73 public $All;
74
83 public function __construct($db, $conf, $langs, $user)
84 {
85 $this->id = "Perso";
86 $this->length = $langs->trans("SetupPerso");
87
88 $this->db = $db;
89 $this->conf = $conf;
90 $this->langs = $langs;
91 $this->user = $user;
92
93 if (!getDolGlobalString('USER_PASSWORD_PATTERN')) {
94 // default value at auto generation (12 chars, 1 uppercase, 1 digit, 0 special char, 3 repeat max, exclude ambiguous characters).
95 dolibarr_set_const($db, "USER_PASSWORD_PATTERN", '12;1;1;0;3;1', 'chaine', 0, '', $conf->entity);
96 }
97
98 $this->Maj = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
99 $this->Min = strtolower($this->Maj);
100 $this->Nb = "0123456789";
101 $this->Spe = "!@#$%&*()_-+={}[]\\|:;'/";
102 $this->Ambi = array("1", "I", "l", "|", "O", "0");
103
104 $tabConf = explode(";", getDolGlobalString('USER_PASSWORD_PATTERN'));
105 $this->length2 = $tabConf[0];
106 $this->NbMaj = $tabConf[1];
107 $this->NbNum = $tabConf[2];
108 $this->NbSpe = $tabConf[3];
109 $this->NbRepeat = $tabConf[4];
110 $this->WithoutAmbi = $tabConf[5];
111 }
112
118 private function initAll()
119 {
120 if ($this->WithoutAmbi) {
121 $this->Maj = str_replace($this->Ambi, "", $this->Maj);
122 $this->Min = str_replace($this->Ambi, "", $this->Min);
123 $this->Nb = str_replace($this->Ambi, "", $this->Nb);
124 $this->Spe = str_replace($this->Ambi, "", $this->Spe);
125 }
126
127 $pattern = $this->Min.(!empty($this->NbMaj) ? $this->Maj : '').(!empty($this->NbNum) ? $this->Nb : '').(!empty($this->NbSpe) ? $this->Spe : '');
128 $this->All = str_shuffle($pattern);
129 }
130
136 public function getDescription()
137 {
138 global $langs;
139 return $langs->trans("PasswordGenerationPerso");
140 }
141
147 public function getExample()
148 {
149 return $this->getNewGeneratedPassword();
150 }
151
157 public function getNewGeneratedPassword()
158 {
159 $this->initAll();
160
161 $pass = "";
162 for ($i = 0; $i < $this->NbMaj; $i++) {
163 // Y
164 $pass .= $this->Maj[mt_rand(0, strlen($this->Maj) - 1)];
165 }
166
167 for ($i = 0; $i < $this->NbNum; $i++) {
168 // X
169 $pass .= $this->Nb[mt_rand(0, strlen($this->Nb) - 1)];
170 }
171
172 for ($i = 0; $i < $this->NbSpe; $i++) {
173 // @
174 $pass .= $this->Spe[mt_rand(0, strlen($this->Spe) - 1)];
175 }
176
177 for ($i = strlen($pass); $i < $this->length2; $i++) {
178 // y
179 $pass .= $this->All[mt_rand(0, strlen($this->All) - 1)];
180 }
181
182 $pass = str_shuffle($pass);
183
184 if ($this->validatePassword($pass)) {
185 return $pass;
186 }
187
188 return $this->getNewGeneratedPassword(); // warning, may generate infinite loop if conditions are not possible
189 }
190
198 public function validatePassword($password)
199 {
200 global $langs;
201
202 $this->initAll(); // For the case this method is called alone
203
204 $password_a = preg_split('//u', $password, null, PREG_SPLIT_NO_EMPTY);
205 $maj = preg_split('//u', $this->Maj, null, PREG_SPLIT_NO_EMPTY);
206 $num = preg_split('//u', $this->Nb, null, PREG_SPLIT_NO_EMPTY);
207 $spe = preg_split('//u', $this->Spe, null, PREG_SPLIT_NO_EMPTY);
208 /*
209 $password_a = str_split($password);
210 $maj = str_split($this->Maj);
211 $num = str_split($this->Nb);
212 $spe = str_split($this->Spe);
213 */
214
215 if (dol_strlen($password) < $this->length2) {
216 $langs->load("other");
217 $this->error = $langs->trans("YourPasswordMustHaveAtLeastXChars", $this->length2);
218 return 0;
219 }
220
221 if (count(array_intersect($password_a, $maj)) < $this->NbMaj) {
222 $langs->load("other");
223 $this->error = $langs->trans('PasswordNeedAtLeastXUpperCaseChars', $this->NbMaj);
224 return 0;
225 }
226
227 if (count(array_intersect($password_a, $num)) < $this->NbNum) {
228 $langs->load("other");
229 $this->error = $langs->trans('PasswordNeedAtLeastXDigitChars', $this->NbNum);
230 return 0;
231 }
232
233 if (count(array_intersect($password_a, $spe)) < $this->NbSpe) {
234 $langs->load("other");
235 $this->error = $langs->trans('PasswordNeedAtLeastXSpecialChars', $this->NbSpe);
236 return 0;
237 }
238
239 if (!$this->consecutiveIterationSameCharacter($password)) {
240 $langs->load("other");
241 $this->error = $langs->trans('PasswordNeedNoXConsecutiveChars', $this->NbRepeat);
242 return 0;
243 }
244
245 return 1;
246 }
247
254 public function consecutiveIterationSameCharacter($password)
255 {
256 $this->initAll();
257
258 if (empty($this->NbRepeat)) {
259 return true;
260 }
261
262 $char = preg_split('//u', $password, null, PREG_SPLIT_NO_EMPTY);
263
264 $last = "";
265 $count = 0;
266 foreach ($char as $c) {
267 if ($c != $last) {
268 $last = $c;
269 $count = 1;
270 //print "Char $c - count = $count\n";
271 continue;
272 }
273
274 $count++;
275 //print "Char $c - count = $count\n";
276
277 if ($count > $this->NbRepeat) {
278 return false;
279 }
280 }
281
282 return true;
283 }
284}
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 dolibarr global constant string value.
conf($dolibarr_main_document_root)
Load conf file (file must exists)
Definition inc.php:403
$conf db user
Definition repair.php:125