dolibarr  7.0.0-beta
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@capnetworks.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 {
36  var $id;
37  var $length;
38  var $length2; // didn't overright display
39  var $NbMaj;
40  var $NbNum;
41  var $NbSpe;
42  var $NbRepeat;
43  var $WithoutAmbi;
44 
45  var $db;
46  var $conf;
47  var $lang;
48  var $user;
49 
50  var $Maj;
51  var $Min;
52  var $Nb;
53  var $Spe;
54  var $Ambi;
55  var $All;
56 
65  function __construct($db, $conf, $langs, $user)
66  {
67  $this->id = "Perso";
68  $this->length = $langs->trans("SetupPerso");
69 
70  $this->db=$db;
71  $this->conf=$conf;
72  $this->langs=$langs;
73  $this->user=$user;
74 
75  if(empty($conf->global->USER_PASSWORD_PATTERN)){
76  // default value (8carac, 1maj, 1digit, 1spe, 3 repeat, no ambi at auto generation.
77  dolibarr_set_const($db, "USER_PASSWORD_PATTERN", '8;1;1;1;3;1','chaine',0,'',$conf->entity);
78  }
79 
80  $this->Maj = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
81  $this->Min = strtolower($this->Maj);
82  $this->Nb = "0123456789";
83  $this->Spe = "!@#$%&*()_-+={}[]\\|:;'/";
84  $this->Ambi = array("1","I","l","|","O","0");
85 
86  $tabConf = explode(";",$conf->global->USER_PASSWORD_PATTERN);
87  $this->length2 = $tabConf[0];
88  $this->NbMaj = $tabConf[1];
89  $this->NbNum = $tabConf[2];
90  $this->NbSpe = $tabConf[3];
91  $this->NbRepeat = $tabConf[4];
92  $this->WithoutAmbi = $tabConf[5];
93 
94  if ($this->WithoutAmbi)
95  {
96  $this->Maj = str_replace($this->Ambi,"",$this->Maj);
97  $this->Min = str_replace($this->Ambi,"",$this->Min);
98  $this->Nb = str_replace($this->Ambi,"",$this->Nb);
99  $this->Spe = str_replace($this->Ambi,"",$this->Spe);
100  }
101 
102  $pattern = $this->Min . (! empty($this->NbMaj)?$this->Maj:'') . (! empty($this->NbNum)?$this->Nb:'') . (! empty($this->NbSpe)?$this->Spe:'');
103  $this->All = str_shuffle($pattern);
104 
105  //$this->All = str_shuffle($this->Maj. $this->Min. $this->Nb. $this->Spe);
106  //$this->All = $this->Maj. $this->Min. $this->Nb. $this->Spe;
107  //$this->All = $this->Spe;
108 
109  }
110 
116  function getDescription()
117  {
118  global $langs;
119  return $langs->trans("PasswordGenerationPerso");
120  }
121 
127  function getExample()
128  {
129  return $this->getNewGeneratedPassword();
130  }
131 
138  {
139  $pass = "";
140  for($i=0; $i<$this->NbMaj; $i++){ // Y
141  $pass .= $this->Maj[mt_rand(0,strlen($this->Maj) - 1)];
142  }
143 
144  for($i=0; $i<$this->NbNum; $i++){ // X
145  $pass .= $this->Nb[mt_rand(0,strlen($this->Nb) - 1)];
146  }
147 
148  for($i=0; $i<$this->NbSpe; $i++){ // @
149  $pass .= $this->Spe[mt_rand(0,strlen($this->Spe) - 1)];
150  }
151 
152  for($i=strlen($pass);$i<$this->length2; $i++){ // y
153  $pass .= $this->All[mt_rand(0,strlen($this->All) -1)];
154  }
155 
156  $pass = str_shuffle($pass);
157 
158  if ($this->validatePassword($pass))
159  {
160  return $pass;
161  }
162 
163  return $this->getNewGeneratedPassword();
164  }
165 
172  function validatePassword($password)
173  {
174  $password_a = str_split($password);
175  $maj = str_split($this->Maj);
176  $num = str_split($this->Nb);
177  $spe = str_split($this->Spe);
178 
179  if(count(array_intersect($password_a, $maj)) < $this->NbMaj){
180  return 0;
181  }
182 
183  if(count(array_intersect($password_a, $num)) < $this->NbNum){
184  return 0;
185  }
186 
187  if(count(array_intersect($password_a, $spe)) < $this->NbSpe){
188  return 0;
189  }
190 
191  if(!$this->consecutiveInterationSameCharacter($password)){
192  return 0;
193  }
194 
195  return 1;
196  }
197 
205  $last = "";
206  $count = 0;
207  $char = str_split($password);
208  foreach($char as $c){
209  if($c != $last){
210  $last = $c;
211  $count = 0;
212  }else{
213  $count++;
214  }
215 
216  if($count >= $this->NbRepeat) {
217  return 0;
218  }
219  }
220  return 1;
221  }
222 }
223 
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:282
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:485
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:105
__construct($db, $conf, $langs, $user)
Constructor.
getDescription()
Return description of module.