modGeneratePassStandard.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /**
  19. * \file htdocs/core/modules/security/generate/modGeneratePassStandard.class.php
  20. * \ingroup core
  21. * \brief File to manage password generation according to standard rule
  22. */
  23. require_once DOL_DOCUMENT_ROOT.'/core/modules/security/generate/modules_genpassword.php';
  24. /**
  25. * Class to generate a password according to a dolibarr standard rule (12 random chars)
  26. */
  27. class modGeneratePassStandard extends ModeleGenPassword
  28. {
  29. /**
  30. * @var int ID
  31. */
  32. public $id;
  33. public $picto = 'fa-shield-alt';
  34. /**
  35. * Minimum length (text visible by end user)
  36. *
  37. * @var string
  38. */
  39. public $length;
  40. /**
  41. * Minimum length in number of characters
  42. *
  43. * @var integer
  44. */
  45. public $length2;
  46. /**
  47. * @var DoliDB Database handler.
  48. */
  49. public $db;
  50. public $conf;
  51. public $lang;
  52. public $user;
  53. /**
  54. * Constructor
  55. *
  56. * @param DoliDB $db Database handler
  57. * @param Conf $conf Handler de conf
  58. * @param Translate $langs Handler de langue
  59. * @param User $user Handler du user connecte
  60. */
  61. public function __construct($db, $conf, $langs, $user)
  62. {
  63. $this->id = "standard";
  64. $this->length = 12;
  65. $this->length2 = 12;
  66. $this->db = $db;
  67. $this->conf = $conf;
  68. $this->langs = $langs;
  69. $this->user = $user;
  70. }
  71. /**
  72. * Return description of module
  73. *
  74. * @return string Description of module
  75. */
  76. public function getDescription()
  77. {
  78. global $langs;
  79. return $langs->trans("PasswordGenerationStandard", $this->length);
  80. }
  81. /**
  82. * Return an example of password generated by this module
  83. *
  84. * @return string Example of password
  85. */
  86. public function getExample()
  87. {
  88. return $this->getNewGeneratedPassword();
  89. }
  90. /**
  91. * Build new password
  92. *
  93. * @return string Return a new generated password
  94. */
  95. public function getNewGeneratedPassword()
  96. {
  97. // start with a blank password
  98. $password = "";
  99. // define possible characters
  100. $possible = "0123456789qwertyuiopasdfghjklzxcvbnmASDFGHJKLZXCVBNMQWERTYUIOP";
  101. // set up a counter
  102. $i = 0;
  103. // add random characters to $password until $length is reached
  104. while ($i < $this->length) {
  105. // pick a random character from the possible ones
  106. if (function_exists('random_int')) { // Cryptographic random
  107. $char = substr($possible, random_int(0, dol_strlen($possible) - 1), 1);
  108. } else {
  109. $char = substr($possible, mt_rand(0, dol_strlen($possible) - 1), 1);
  110. }
  111. if (substr_count($password, $char) <= 6) { // we don't want this character if it's already 5 times in the password
  112. $password .= $char;
  113. $i++;
  114. }
  115. }
  116. // done!
  117. return $password;
  118. }
  119. /**
  120. * Validate a password
  121. * This function is called by User->setPassword() and internally to validate that the password matches the constraints.
  122. *
  123. * @param string $password Password to check
  124. * @return int 0 if KO, >0 if OK
  125. */
  126. public function validatePassword($password)
  127. {
  128. global $langs;
  129. if (dol_strlen($password) < $this->length2) {
  130. $langs->load("other");
  131. $this->error = $langs->trans("YourPasswordMustHaveAtLeastXChars", $this->length2);
  132. return 0;
  133. }
  134. return 1;
  135. }
  136. }