2006-03-12 14:15:29 +01:00
|
|
|
<?php
|
2011-05-01 12:48:43 +02:00
|
|
|
/* Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
|
2006-03-12 14:15:29 +01:00
|
|
|
*
|
|
|
|
|
* 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 2 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
|
2011-08-01 01:24:38 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2006-03-12 14:15:29 +01:00
|
|
|
* or see http://www.gnu.org/
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
2008-10-25 13:33:59 +02:00
|
|
|
* \file htdocs/includes/modules/security/generate/modGeneratePassStandard.class.php
|
|
|
|
|
* \ingroup core
|
|
|
|
|
* \brief File to manage password generation according to standard rule
|
|
|
|
|
*/
|
2006-03-12 14:15:29 +01:00
|
|
|
|
2007-05-05 02:06:07 +02:00
|
|
|
require_once(DOL_DOCUMENT_ROOT ."/includes/modules/security/generate/modules_genpassword.php");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2010-07-21 14:16:25 +02:00
|
|
|
* \class modGeneratePassStandard
|
2010-07-21 13:57:52 +02:00
|
|
|
* \brief Class to generate a password according to a dolibarr standard rule (8 random chars)
|
|
|
|
|
*/
|
2007-05-05 02:06:07 +02:00
|
|
|
class modGeneratePassStandard extends ModeleGenPassword
|
2006-03-12 14:15:29 +01:00
|
|
|
{
|
|
|
|
|
var $id;
|
|
|
|
|
var $length;
|
|
|
|
|
|
|
|
|
|
var $db;
|
|
|
|
|
var $conf;
|
|
|
|
|
var $lang;
|
|
|
|
|
var $user;
|
|
|
|
|
|
2009-08-21 19:08:15 +02:00
|
|
|
|
2006-03-12 14:15:29 +01:00
|
|
|
/**
|
2011-09-11 20:35:38 +02:00
|
|
|
* Constructor
|
|
|
|
|
*
|
2011-09-24 15:44:04 +02: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
|
2006-03-12 14:15:29 +01:00
|
|
|
*/
|
2006-04-01 14:03:31 +02:00
|
|
|
function modGeneratePassStandard($db, $conf, $langs, $user)
|
2006-03-12 14:15:29 +01:00
|
|
|
{
|
2006-04-01 14:03:31 +02:00
|
|
|
$this->id = "standard";
|
2006-03-12 14:15:29 +01:00
|
|
|
$this->length = 8;
|
|
|
|
|
|
|
|
|
|
$this->db=$db;
|
|
|
|
|
$this->conf=$conf;
|
|
|
|
|
$this->langs=$langs;
|
|
|
|
|
$this->user=$user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-05-01 12:48:43 +02:00
|
|
|
* Return description of module
|
2011-09-24 15:44:04 +02:00
|
|
|
*
|
2011-05-01 12:48:43 +02:00
|
|
|
* @return string Description of module
|
2006-03-12 14:15:29 +01:00
|
|
|
*/
|
|
|
|
|
function getDescription()
|
|
|
|
|
{
|
2008-10-25 13:33:59 +02:00
|
|
|
global $langs;
|
|
|
|
|
return $langs->trans("PasswordGenerationStandard");
|
2006-03-12 14:15:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-05-01 12:48:43 +02:00
|
|
|
* Return an example of password generated by this module
|
2011-09-24 15:44:04 +02:00
|
|
|
*
|
2011-05-01 12:48:43 +02:00
|
|
|
* @return string Example of password
|
2006-03-12 14:15:29 +01:00
|
|
|
*/
|
|
|
|
|
function getExample()
|
|
|
|
|
{
|
|
|
|
|
return $this->getNewGeneratedPassword();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-05-01 12:48:43 +02:00
|
|
|
* Build new password
|
2011-09-24 15:44:04 +02:00
|
|
|
*
|
2011-05-01 12:48:43 +02:00
|
|
|
* @return string Return a new generated password
|
2006-03-12 14:15:29 +01:00
|
|
|
*/
|
|
|
|
|
function getNewGeneratedPassword()
|
|
|
|
|
{
|
|
|
|
|
// start with a blank password
|
|
|
|
|
$password = "";
|
2009-08-21 19:08:15 +02:00
|
|
|
|
2006-03-12 14:15:29 +01:00
|
|
|
// define possible characters
|
|
|
|
|
$possible = "0123456789bcdfghjkmnpqrstvwxyz";
|
2009-08-21 19:08:15 +02:00
|
|
|
|
2006-03-12 14:15:29 +01:00
|
|
|
// set up a counter
|
|
|
|
|
$i = 0;
|
|
|
|
|
|
|
|
|
|
// add random characters to $password until $length is reached
|
|
|
|
|
while ($i < $this->length)
|
|
|
|
|
{
|
2009-08-21 19:08:15 +02:00
|
|
|
|
2006-03-12 14:15:29 +01:00
|
|
|
// pick a random character from the possible ones
|
2010-08-24 16:42:18 +02:00
|
|
|
$char = substr($possible, mt_rand(0, dol_strlen($possible)-1), 1);
|
2009-08-21 19:08:15 +02:00
|
|
|
|
2006-03-12 14:15:29 +01:00
|
|
|
// we don't want this character if it's already in the password
|
|
|
|
|
if (!strstr($password, $char))
|
|
|
|
|
{
|
|
|
|
|
$password .= $char;
|
|
|
|
|
$i++;
|
|
|
|
|
}
|
2009-08-21 19:08:15 +02:00
|
|
|
|
2006-03-12 14:15:29 +01:00
|
|
|
}
|
2009-08-21 19:08:15 +02:00
|
|
|
|
2006-03-12 14:15:29 +01:00
|
|
|
// done!
|
|
|
|
|
return $password;
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-21 19:08:15 +02:00
|
|
|
/**
|
2011-05-01 12:48:43 +02:00
|
|
|
* Validate a password
|
2011-09-24 15:44:04 +02:00
|
|
|
*
|
|
|
|
|
* @param string $password Password to check
|
|
|
|
|
* @return int 0 if KO, >0 if OK
|
2009-08-21 19:08:15 +02:00
|
|
|
*/
|
|
|
|
|
function validatePassword($password)
|
|
|
|
|
{
|
2010-08-24 16:42:18 +02:00
|
|
|
if (dol_strlen($password) < $this->length) return 0;
|
2009-08-21 19:08:15 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
2006-03-12 14:15:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
?>
|