dolibarr  13.0.2
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 
33 {
37  public $id;
38 
39  public $length;
40 
44  public $db;
45 
46  public $conf;
47  public $lang;
48  public $user;
49 
50 
59  public function __construct($db, $conf, $langs, $user)
60  {
61  $this->id = "standard";
62  $this->length = 10;
63 
64  $this->db = $db;
65  $this->conf = $conf;
66  $this->langs = $langs;
67  $this->user = $user;
68  }
69 
75  public function getDescription()
76  {
77  global $langs;
78  return $langs->trans("PasswordGenerationStandard", $this->length);
79  }
80 
86  public function getExample()
87  {
88  return $this->getNewGeneratedPassword();
89  }
90 
96  public function getNewGeneratedPassword()
97  {
98  // start with a blank password
99  $password = "";
100 
101  // define possible characters
102  $possible = "0123456789bcdfghjkmnpqrstvwxyz";
103 
104  // set up a counter
105  $i = 0;
106 
107  // add random characters to $password until $length is reached
108  while ($i < $this->length)
109  {
110  // pick a random character from the possible ones
111  $char = substr($possible, mt_rand(0, dol_strlen($possible) - 1), 1);
112 
113  // we don't want this character if it's already in the password
114  if (!strstr($password, $char))
115  {
116  $password .= $char;
117  $i++;
118  }
119  }
120 
121  // done!
122  return $password;
123  }
124 
131  public function validatePassword($password)
132  {
133  if (dol_strlen($password) < $this->length) return 0;
134  return 1;
135  }
136 }
Parent class for password rules/management modules.
conf($dolibarr_main_document_root)
Load conf file (file must exists)
Definition: inc.php:262
$conf db
API class for accounts.
Definition: inc.php:54
__construct($db, $conf, $langs, $user)
Constructor.
validatePassword($password)
Validate a password.
dol_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
getNewGeneratedPassword()
Build new password.
$conf db user
Definition: repair.php:109
Class to generate a password according to a dolibarr standard rule (8 random chars) ...
getExample()
Return an example of password generated by this module.
getDescription()
Return description of module.