dolibarr  16.0.5
modGeneratePassStandard.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16  * or see https://www.gnu.org/
17  */
18 
25 require_once DOL_DOCUMENT_ROOT.'/core/modules/security/generate/modules_genpassword.php';
26 
27 
32 {
36  public $id;
37 
43  public $length;
44 
50  public $length2;
51 
55  public $db;
56 
57  public $conf;
58  public $lang;
59  public $user;
60 
61 
70  public function __construct($db, $conf, $langs, $user)
71  {
72  $this->id = "standard";
73  $this->length = 12;
74  $this->length2 = 12;
75 
76  $this->db = $db;
77  $this->conf = $conf;
78  $this->langs = $langs;
79  $this->user = $user;
80  }
81 
87  public function getDescription()
88  {
89  global $langs;
90  return $langs->trans("PasswordGenerationStandard", $this->length);
91  }
92 
98  public function getExample()
99  {
100  return $this->getNewGeneratedPassword();
101  }
102 
108  public function getNewGeneratedPassword()
109  {
110  // start with a blank password
111  $password = "";
112 
113  // define possible characters
114  $possible = "0123456789qwertyuiopasdfghjklzxcvbnmASDFGHJKLZXCVBNMQWERTYUIOP";
115 
116  // set up a counter
117  $i = 0;
118 
119  // add random characters to $password until $length is reached
120  while ($i < $this->length) {
121  // pick a random character from the possible ones
122  if (function_exists('random_int')) { // Cryptographic random
123  $char = substr($possible, random_int(0, dol_strlen($possible) - 1), 1);
124  } else {
125  $char = substr($possible, mt_rand(0, dol_strlen($possible) - 1), 1);
126  }
127 
128  if (substr_count($password, $char) <= 6) { // we don't want this character if it's already 5 times in the password
129  $password .= $char;
130  $i++;
131  }
132  }
133 
134  // done!
135  return $password;
136  }
137 
145  public function validatePassword($password)
146  {
147  global $langs;
148 
149  if (dol_strlen($password) < $this->length2) {
150  $langs->load("other");
151  $this->error = $langs->trans("YourPasswordMustHaveAtLeastXChars", $this->length2);
152  return 0;
153  }
154 
155  return 1;
156  }
157 }
db
$conf db
API class for accounts.
Definition: inc.php:41
modGeneratePassStandard\__construct
__construct($db, $conf, $langs, $user)
Constructor.
Definition: modGeneratePassStandard.class.php:70
modGeneratePassStandard\validatePassword
validatePassword($password)
Validate a password This function is called by User->setPassword() and internally to validate that th...
Definition: modGeneratePassStandard.class.php:145
modGeneratePassStandard\getDescription
getDescription()
Return description of module.
Definition: modGeneratePassStandard.class.php:87
conf
conf($dolibarr_main_document_root)
Load conf file (file must exists)
Definition: inc.php:300
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3747
modGeneratePassStandard\getNewGeneratedPassword
getNewGeneratedPassword()
Build new password.
Definition: modGeneratePassStandard.class.php:108
ModeleGenPassword
Parent class for password rules/management modules.
Definition: modules_genpassword.php:30
modGeneratePassStandard\getExample
getExample()
Return an example of password generated by this module.
Definition: modGeneratePassStandard.class.php:98
user
$conf db user
Definition: repair.php:123
modGeneratePassStandard
Class to generate a password according to a dolibarr standard rule (12 random chars)
Definition: modGeneratePassStandard.class.php:31