PageRenderTime 24ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/validator/sfPasswordStrengthValidator.class.php

http://piwam.googlecode.com/
PHP | 136 lines | 100 code | 25 blank | 11 comment | 33 complexity | d34788615f3bf16eecf84c993d7a42bb MD5 | raw file
Possible License(s): ISC, LGPL-2.1, AGPL-3.0, BSD-3-Clause, LGPL-3.0, GPL-2.0
  1. <?php
  2. /**
  3. * Check strength of password
  4. *
  5. * @see http://snippets.symfony-project.org/snippets/from/mysyfy/order_by/date
  6. * @since r20
  7. */
  8. class sfPasswordStrengthValidator extends sfValidator
  9. {
  10. public function execute (&$value, &$error)
  11. {
  12. $weakness = $this->Password_Strength($value);
  13. if($weakness==1) {
  14. $error = $this->getParameter('strength_error');
  15. return false;
  16. }
  17. return $weakness;
  18. }
  19. public function initialize ($context, $parameters = null)
  20. {
  21. parent::initialize($context);
  22. $this->setParameter('strength_error', 'Mot de passe trop simple');
  23. $this->getParameterHolder()->add($parameters);
  24. return true;
  25. }
  26. /**
  27. * Thanks for: Alix Axel Weblog
  28. *
  29. * @see http://www.alixaxel.com/wordpress/wp-content/2007/06/Password_Strength.phps
  30. */
  31. function Password_Strength($password, $username = null)
  32. {
  33. if (!empty($username)) {
  34. $password = str_replace($username, '', $password);
  35. }
  36. $strength = 0;
  37. $password_length = strlen($password);
  38. if ($password_length < 5) {
  39. return $strength;
  40. }
  41. else {
  42. $strength = $password_length * 4;
  43. }
  44. for ($i = 2; $i <= 4; $i++)
  45. {
  46. $temp = str_split($password, $i);
  47. $strength -= (ceil($password_length / $i) - count(array_unique($temp)));
  48. }
  49. preg_match_all('/[0-9]/', $password, $numbers);
  50. if (!empty($numbers)) {
  51. $numbers = count($numbers[0]);
  52. if ($numbers >= 3) {
  53. $strength += 5;
  54. }
  55. }
  56. else {
  57. $numbers = 0;
  58. }
  59. preg_match_all('/[|!@#$%&*\/=?,;.:\-_+~^¨\\\]/', $password, $symbols);
  60. if (!empty($symbols)) {
  61. $symbols = count($symbols[0]);
  62. if ($symbols >= 2) {
  63. $strength += 5;
  64. }
  65. }
  66. else {
  67. $symbols = 0;
  68. }
  69. preg_match_all('/[a-z]/', $password, $lowercase_characters);
  70. preg_match_all('/[A-Z]/', $password, $uppercase_characters);
  71. if (!empty($lowercase_characters)) {
  72. $lowercase_characters = count($lowercase_characters[0]);
  73. }
  74. else {
  75. $lowercase_characters = 0;
  76. }
  77. if (!empty($uppercase_characters)) {
  78. $uppercase_characters = count($uppercase_characters[0]);
  79. }
  80. else {
  81. $uppercase_characters = 0;
  82. }
  83. if (($lowercase_characters > 0) && ($uppercase_characters > 0)) {
  84. $strength += 10;
  85. }
  86. $characters = $lowercase_characters + $uppercase_characters;
  87. if (($numbers > 0) && ($symbols > 0)) {
  88. $strength += 15;
  89. }
  90. if (($numbers > 0) && ($characters > 0)) {
  91. $strength += 15;
  92. }
  93. if (($symbols > 0) && ($characters > 0)) {
  94. $strength += 15;
  95. }
  96. if (($numbers == 0) && ($symbols == 0)) {
  97. $strength -= 10;
  98. }
  99. if (($symbols == 0) && ($characters == 0)) {
  100. $strength -= 10;
  101. }
  102. if ($strength < 0) {
  103. $strength = 0;
  104. }
  105. if ($strength > 100) {
  106. $strength = 100;
  107. }
  108. return $strength;
  109. }
  110. }
  111. ?>