dolibarr/htdocs/core/modules/security/generate/modGeneratePassStandard.class.php

158 lines
3.8 KiB
PHP
Raw Normal View History

<?php
/* Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-09-23 19:55:30 +00:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* or see https://www.gnu.org/
*/
/**
* \file htdocs/core/modules/security/generate/modGeneratePassStandard.class.php
2008-10-25 11:33:59 +00:00
* \ingroup core
* \brief File to manage password generation according to standard rule
*/
require_once DOL_DOCUMENT_ROOT.'/core/modules/security/generate/modules_genpassword.php';
/**
2022-01-03 11:26:57 +00:00
* Class to generate a password according to a dolibarr standard rule (12 random chars)
2010-07-21 11:57:52 +00:00
*/
class modGeneratePassStandard extends ModeleGenPassword
{
2018-08-22 09:24:31 +00:00
/**
* @var int ID
*/
public $id;
2018-09-01 21:12:20 +00:00
2022-01-03 11:26:57 +00:00
/**
* Minimum length (text visible by end user)
*
* @var string
*/
2018-09-01 21:12:20 +00:00
public $length;
2022-01-03 11:26:57 +00:00
/**
* Minimum length in number of characters
*
* @var integer
*/
public $length2;
2018-08-22 09:06:34 +00:00
/**
* @var DoliDB Database handler.
*/
public $db;
2018-09-01 21:12:20 +00:00
public $conf;
public $lang;
public $user;
/**
2011-09-11 18:35:38 +00:00
* Constructor
*
2011-09-24 13:44:04 +00:00
* @param DoliDB $db Database handler
* @param Conf $conf Handler de conf
* @param Translate $langs Handler de langue
* @param User $user Handler du user connecte
*/
2019-02-28 21:07:48 +00:00
public function __construct($db, $conf, $langs, $user)
{
$this->id = "standard";
$this->length = 12;
2022-01-03 11:26:57 +00:00
$this->length2 = 12;
$this->db = $db;
$this->conf = $conf;
$this->langs = $langs;
$this->user = $user;
}
/**
* Return description of module
2011-09-24 13:44:04 +00:00
*
* @return string Description of module
*/
2019-02-28 21:07:48 +00:00
public function getDescription()
{
2008-10-25 11:33:59 +00:00
global $langs;
2020-09-17 13:09:29 +00:00
return $langs->trans("PasswordGenerationStandard", $this->length);
}
/**
* Return an example of password generated by this module
2011-09-24 13:44:04 +00:00
*
* @return string Example of password
*/
2019-02-28 21:07:48 +00:00
public function getExample()
{
return $this->getNewGeneratedPassword();
}
/**
* Build new password
2011-09-24 13:44:04 +00:00
*
* @return string Return a new generated password
*/
2019-02-28 21:07:48 +00:00
public function getNewGeneratedPassword()
{
// start with a blank password
$password = "";
// define possible characters
$possible = "0123456789qwertyuiopasdfghjklzxcvbnmASDFGHJKLZXCVBNMQWERTYUIOP";
// set up a counter
$i = 0;
// add random characters to $password until $length is reached
2021-02-23 21:03:23 +00:00
while ($i < $this->length) {
// pick a random character from the possible ones
if (function_exists('random_int')) { // Cryptographic random
$char = substr($possible, random_int(0, dol_strlen($possible) - 1), 1);
} else {
$char = substr($possible, mt_rand(0, dol_strlen($possible) - 1), 1);
}
if (substr_count($password, $char) <= 6) { // we don't want this character if it's already 5 times in the password
$password .= $char;
$i++;
}
}
// done!
return $password;
}
/**
* Validate a password
* This function is called by User->setPassword() and internally to validate that the password matches the constraints.
*
* @param string $password Password to check
* @return int 0 if KO, >0 if OK
*/
public function validatePassword($password)
{
global $langs;
if (dol_strlen($password) < $this->length2) {
$langs->load("other");
$this->error = $langs->trans("YourPasswordMustHaveAtLeastXChars", $this->length2);
2021-02-23 21:03:23 +00:00
return 0;
}
return 1;
}
}