dolibarr  13.0.2
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 
27 require_once DOL_DOCUMENT_ROOT.'/core/modules/security/generate/modules_genpassword.php';
28 
29 
35 {
39  public $id;
40 
41  public $length;
42  public $length2; // didn't overright display
43  public $NbMaj;
44  public $NbNum;
45  public $NbSpe;
46  public $NbRepeat;
47  public $WithoutAmbi;
48 
52  public $db;
53 
54  public $conf;
55  public $lang;
56  public $user;
57 
58  public $Maj;
59  public $Min;
60  public $Nb;
61  public $Spe;
62  public $Ambi;
63  public $All;
64 
73  public function __construct($db, $conf, $langs, $user)
74  {
75  $this->id = "Perso";
76  $this->length = $langs->trans("SetupPerso");
77 
78  $this->db = $db;
79  $this->conf = $conf;
80  $this->langs = $langs;
81  $this->user = $user;
82 
83  if (empty($conf->global->USER_PASSWORD_PATTERN)) {
84  // default value (10carac, 1maj, 1digit, 1spe, 3 repeat, no ambi at auto generation.
85  dolibarr_set_const($db, "USER_PASSWORD_PATTERN", '10;1;1;1;3;1', 'chaine', 0, '', $conf->entity);
86  }
87 
88  $this->Maj = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
89  $this->Min = strtolower($this->Maj);
90  $this->Nb = "0123456789";
91  $this->Spe = "!@#$%&*()_-+={}[]\\|:;'/";
92  $this->Ambi = array("1", "I", "l", "|", "O", "0");
93 
94  $tabConf = explode(";", $conf->global->USER_PASSWORD_PATTERN);
95  $this->length2 = $tabConf[0];
96  $this->NbMaj = $tabConf[1];
97  $this->NbNum = $tabConf[2];
98  $this->NbSpe = $tabConf[3];
99  $this->NbRepeat = $tabConf[4];
100  $this->WithoutAmbi = $tabConf[5];
101 
102  if ($this->WithoutAmbi)
103  {
104  $this->Maj = str_replace($this->Ambi, "", $this->Maj);
105  $this->Min = str_replace($this->Ambi, "", $this->Min);
106  $this->Nb = str_replace($this->Ambi, "", $this->Nb);
107  $this->Spe = str_replace($this->Ambi, "", $this->Spe);
108  }
109 
110  $pattern = $this->Min.(!empty($this->NbMaj) ? $this->Maj : '').(!empty($this->NbNum) ? $this->Nb : '').(!empty($this->NbSpe) ? $this->Spe : '');
111  $this->All = str_shuffle($pattern);
112 
113  //$this->All = str_shuffle($this->Maj. $this->Min. $this->Nb. $this->Spe);
114  //$this->All = $this->Maj. $this->Min. $this->Nb. $this->Spe;
115  //$this->All = $this->Spe;
116  }
117 
123  public function getDescription()
124  {
125  global $langs;
126  return $langs->trans("PasswordGenerationPerso");
127  }
128 
134  public function getExample()
135  {
136  return $this->getNewGeneratedPassword();
137  }
138 
144  public function getNewGeneratedPassword()
145  {
146  $pass = "";
147  for ($i = 0; $i < $this->NbMaj; $i++) {
148  // Y
149  $pass .= $this->Maj[mt_rand(0, strlen($this->Maj) - 1)];
150  }
151 
152  for ($i = 0; $i < $this->NbNum; $i++) {
153  // X
154  $pass .= $this->Nb[mt_rand(0, strlen($this->Nb) - 1)];
155  }
156 
157  for ($i = 0; $i < $this->NbSpe; $i++) {
158  // @
159  $pass .= $this->Spe[mt_rand(0, strlen($this->Spe) - 1)];
160  }
161 
162  for ($i = strlen($pass); $i < $this->length2; $i++) {
163  // y
164  $pass .= $this->All[mt_rand(0, strlen($this->All) - 1)];
165  }
166 
167  $pass = str_shuffle($pass);
168 
169  if ($this->validatePassword($pass)) {
170  return $pass;
171  }
172 
173  return $this->getNewGeneratedPassword();
174  }
175 
182  public function validatePassword($password)
183  {
184  $password_a = str_split($password);
185  $maj = str_split($this->Maj);
186  $num = str_split($this->Nb);
187  $spe = str_split($this->Spe);
188 
189  if (count(array_intersect($password_a, $maj)) < $this->NbMaj) {
190  return false;
191  }
192 
193  if (count(array_intersect($password_a, $num)) < $this->NbNum) {
194  return false;
195  }
196 
197  if (count(array_intersect($password_a, $spe)) < $this->NbSpe) {
198  return false;
199  }
200 
201  if (!$this->consecutiveInterationSameCharacter($password)) {
202  return false;
203  }
204 
205  return true;
206  }
207 
214  private function consecutiveInterationSameCharacter($password)
215  {
216  $last = "";
217 
218  if (empty($this->NbRepeat)) return 1;
219 
220  $count = 0;
221  $char = str_split($password);
222 
223  foreach ($char as $c) {
224  if ($c != $last) {
225  $last = $c;
226  $count = 0;
227 
228  continue;
229  }
230 
231  $count++;
232  if ($count > $this->NbRepeat) {
233  return false;
234  }
235  }
236 
237  return true;
238  }
239 }
Parent class for password rules/management modules.
Class to generate a password according to personal rules.
conf($dolibarr_main_document_root)
Load conf file (file must exists)
Definition: inc.php:262
validatePassword($password)
Validate a password.
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).
Definition: admin.lib.php:575
$conf db
API class for accounts.
Definition: inc.php:54
consecutiveInterationSameCharacter($password)
Check the consecutive iterations of the same character.
getNewGeneratedPassword()
Build new password.
$conf db user
Definition: repair.php:109
getExample()
Return an example of password generated by this module.
__construct($db, $conf, $langs, $user)
Constructor.
getDescription()
Return description of module.