dolibarr  21.0.0-alpha
modGeneratePassStandard.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) 2024 Frédéric France <frederic.france@free.fr>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  * or see https://www.gnu.org/
18  */
19 
26 require_once DOL_DOCUMENT_ROOT.'/core/modules/security/generate/modules_genpassword.php';
27 
28 
33 {
37  public $id;
38 
39  public $picto = 'fa-shield-alt';
40 
49  public function __construct($db, $conf, $langs, $user)
50  {
51  $this->id = "standard";
52  $this->length = '12';
53  $this->length2 = 12;
54 
55  $this->db = $db;
56  $this->conf = $conf;
57  $this->langs = $langs;
58  $this->user = $user;
59  }
60 
66  public function getDescription()
67  {
68  global $langs;
69  return $langs->trans("PasswordGenerationStandard", $this->length);
70  }
71 
77  public function getExample()
78  {
79  return $this->getNewGeneratedPassword();
80  }
81 
87  public function getNewGeneratedPassword()
88  {
89  // start with a blank password
90  $password = "";
91 
92  // define possible characters
93  $possible = "0123456789qwertyuiopasdfghjklzxcvbnmASDFGHJKLZXCVBNMQWERTYUIOP";
94 
95  // set up a counter
96  $i = 0;
97 
98  // add random characters to $password until $length is reached
99  while ($i < $this->length) {
100  // pick a random character from the possible ones
101  if (function_exists('random_int')) { // Cryptographic random
102  $char = substr($possible, random_int(0, dol_strlen($possible) - 1), 1);
103  } else {
104  $char = substr($possible, mt_rand(0, dol_strlen($possible) - 1), 1);
105  }
106 
107  if (substr_count($password, $char) <= 6) { // we don't want this character if it's already 5 times in the password
108  $password .= $char;
109  $i++;
110  }
111  }
112 
113  // done!
114  return $password;
115  }
116 
124  public function validatePassword($password)
125  {
126  global $langs;
127 
128  if (dol_strlen($password) < $this->length2) {
129  $langs->load("other");
130  $this->error = $langs->trans("YourPasswordMustHaveAtLeastXChars", $this->length2);
131  return 0;
132  }
133 
134  return 1;
135  }
136 }
Parent class for password rules/management modules.
Class to generate a password according to a dolibarr standard rule (12 random chars)
validatePassword($password)
Validate a password This function is called by User->setPassword() and internally to validate that th...
getDescription()
Return description of module.
getExample()
Return an example of password generated by this module.
getNewGeneratedPassword()
Build new password.
__construct($db, $conf, $langs, $user)
Constructor.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
conf($dolibarr_main_document_root)
Load conf file (file must exists)
Definition: inc.php:420
$conf db user
Active Directory does not allow anonymous connections.
Definition: repair.php:143