dolibarr 19.0.4
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
25require_once DOL_DOCUMENT_ROOT.'/core/modules/security/generate/modules_genpassword.php';
26
27
32{
36 public $id;
37
38 public $picto = 'fa-shield-alt';
39
45 public $length;
46
52 public $length2;
53
62 public function __construct($db, $conf, $langs, $user)
63 {
64 $this->id = "standard";
65 $this->length = 12;
66 $this->length2 = 12;
67
68 $this->db = $db;
69 $this->conf = $conf;
70 $this->langs = $langs;
71 $this->user = $user;
72 }
73
79 public function getDescription()
80 {
81 global $langs;
82 return $langs->trans("PasswordGenerationStandard", $this->length);
83 }
84
90 public function getExample()
91 {
92 return $this->getNewGeneratedPassword();
93 }
94
100 public function getNewGeneratedPassword()
101 {
102 // start with a blank password
103 $password = "";
104
105 // define possible characters
106 $possible = "0123456789qwertyuiopasdfghjklzxcvbnmASDFGHJKLZXCVBNMQWERTYUIOP";
107
108 // set up a counter
109 $i = 0;
110
111 // add random characters to $password until $length is reached
112 while ($i < $this->length) {
113 // pick a random character from the possible ones
114 if (function_exists('random_int')) { // Cryptographic random
115 $char = substr($possible, random_int(0, dol_strlen($possible) - 1), 1);
116 } else {
117 $char = substr($possible, mt_rand(0, dol_strlen($possible) - 1), 1);
118 }
119
120 if (substr_count($password, $char) <= 6) { // we don't want this character if it's already 5 times in the password
121 $password .= $char;
122 $i++;
123 }
124 }
125
126 // done!
127 return $password;
128 }
129
137 public function validatePassword($password)
138 {
139 global $langs;
140
141 if (dol_strlen($password) < $this->length2) {
142 $langs->load("other");
143 $this->error = $langs->trans("YourPasswordMustHaveAtLeastXChars", $this->length2);
144 return 0;
145 }
146
147 return 1;
148 }
149}
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.
__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:403
$conf db user
Definition repair.php:125