dolibarr  9.0.0
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 <http://www.gnu.org/licenses/>.
18  * or see http://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  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 (8carac, 1maj, 1digit, 1spe, 3 repeat, no ambi at auto generation.
85  dolibarr_set_const($db, "USER_PASSWORD_PATTERN", '8;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  function getDescription()
124  {
125  global $langs;
126  return $langs->trans("PasswordGenerationPerso");
127  }
128 
134  function getExample()
135  {
136  return $this->getNewGeneratedPassword();
137  }
138 
145  {
146  $pass = "";
147  for($i=0; $i<$this->NbMaj; $i++){ // Y
148  $pass .= $this->Maj[mt_rand(0,strlen($this->Maj) - 1)];
149  }
150 
151  for($i=0; $i<$this->NbNum; $i++){ // X
152  $pass .= $this->Nb[mt_rand(0,strlen($this->Nb) - 1)];
153  }
154 
155  for($i=0; $i<$this->NbSpe; $i++){ // @
156  $pass .= $this->Spe[mt_rand(0,strlen($this->Spe) - 1)];
157  }
158 
159  for($i=strlen($pass);$i<$this->length2; $i++){ // y
160  $pass .= $this->All[mt_rand(0,strlen($this->All) -1)];
161  }
162 
163  $pass = str_shuffle($pass);
164 
165  if ($this->validatePassword($pass))
166  {
167  return $pass;
168  }
169 
170  return $this->getNewGeneratedPassword();
171  }
172 
179  function validatePassword($password)
180  {
181  $password_a = str_split($password);
182  $maj = str_split($this->Maj);
183  $num = str_split($this->Nb);
184  $spe = str_split($this->Spe);
185 
186  if(count(array_intersect($password_a, $maj)) < $this->NbMaj){
187  return 0;
188  }
189 
190  if(count(array_intersect($password_a, $num)) < $this->NbNum){
191  return 0;
192  }
193 
194  if(count(array_intersect($password_a, $spe)) < $this->NbSpe){
195  return 0;
196  }
197 
198  if(!$this->consecutiveInterationSameCharacter($password)){
199  return 0;
200  }
201 
202  return 1;
203  }
204 
212  {
213  $last = "";
214  $count = 0;
215  $char = str_split($password);
216  foreach($char as $c){
217  if($c != $last){
218  $last = $c;
219  $count = 0;
220  }else{
221  $count++;
222  }
223 
224  if($count >= $this->NbRepeat) {
225  return 0;
226  }
227  }
228  return 1;
229  }
230 }
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:292
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:510
consecutiveInterationSameCharacter($password)
consecutive iterations of the same character
getNewGeneratedPassword()
Build new password.
getExample()
Return an example of password generated by this module.
$conf db user
Definition: repair.php:104
__construct($db, $conf, $langs, $user)
Constructor.
getDescription()
Return description of module.