PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/buddypress/bp-forums/bbpress/bb-includes/backpress/class.wp-pass.php

https://bitbucket.org/codemen_iftekhar/codemen
PHP | 141 lines | 55 code | 20 blank | 66 comment | 12 complexity | d9364329333e7a3a8b5b778374586cbd MD5 | raw file
  1. <?php
  2. // Last sync [WP10712] - Refactored into a class from wp-incldues/pluggable.php
  3. class WP_Pass {
  4. /**
  5. * Create a hash (encrypt) of a plain text password.
  6. *
  7. * For integration with other applications, this function can be overwritten to
  8. * instead use the other package password checking algorithm.
  9. *
  10. * @since WP 2.5
  11. * @global object $wp_hasher PHPass object
  12. * @uses PasswordHash::HashPassword
  13. *
  14. * @param string $password Plain text user password to hash
  15. * @return string The hash string of the password
  16. */
  17. function hash_password($password) {
  18. global $wp_hasher;
  19. if ( empty($wp_hasher) ) {
  20. require_once( BACKPRESS_PATH . 'class.passwordhash.php');
  21. // By default, use the portable hash from phpass
  22. $wp_hasher = new PasswordHash(8, TRUE);
  23. }
  24. return $wp_hasher->HashPassword($password);
  25. }
  26. /**
  27. * Checks the plaintext password against the encrypted Password.
  28. *
  29. * Maintains compatibility between old version and the new cookie authentication
  30. * protocol using PHPass library. The $hash parameter is the encrypted password
  31. * and the function compares the plain text password when encypted similarly
  32. * against the already encrypted password to see if they match.
  33. *
  34. * For integration with other applications, this function can be overwritten to
  35. * instead use the other package password checking algorithm.
  36. *
  37. * @since WP 2.5
  38. * @global object $wp_hasher PHPass object used for checking the password
  39. * against the $hash + $password
  40. * @uses PasswordHash::CheckPassword
  41. *
  42. * @param string $password Plaintext user's password
  43. * @param string $hash Hash of the user's password to check against.
  44. * @return bool False, if the $password does not match the hashed password
  45. */
  46. function check_password($password, $hash, $user_id = '') {
  47. global $wp_hasher, $wp_users_object;
  48. list($hash, $broken) = array_pad( explode( '---', $hash ), 2, '' );
  49. // If the hash is still md5...
  50. if ( strlen($hash) <= 32 ) {
  51. $check = ( $hash == md5($password) );
  52. if ( $check && $user_id && !$broken ) {
  53. // Rehash using new hash.
  54. $wp_users_object->set_password($password, $user_id);
  55. $hash = WP_Pass::hash_password($password);
  56. }
  57. return apply_filters('check_password', $check, $password, $hash, $user_id);
  58. }
  59. // If the stored hash is longer than an MD5, presume the
  60. // new style phpass portable hash.
  61. if ( empty($wp_hasher) ) {
  62. require_once( BACKPRESS_PATH . 'class.passwordhash.php');
  63. // By default, use the portable hash from phpass
  64. $wp_hasher = new PasswordHash(8, TRUE);
  65. }
  66. $check = $wp_hasher->CheckPassword($password, $hash);
  67. return apply_filters('check_password', $check, $password, $hash, $user_id);
  68. }
  69. /**
  70. * Generates a random password drawn from the defined set of characters
  71. *
  72. * @since WP 2.5
  73. *
  74. * @param int $length The length of password to generate
  75. * @param bool $special_chars Whether to include standard special characters
  76. * @return string The random password
  77. */
  78. function generate_password($length = 12, $special_chars = true) {
  79. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  80. if ( $special_chars )
  81. $chars .= '!@#$%^&*()';
  82. $password = '';
  83. for ( $i = 0; $i < $length; $i++ )
  84. $password .= substr($chars, WP_Pass::rand(0, strlen($chars) - 1), 1);
  85. return $password;
  86. }
  87. /**
  88. * Generates a random number
  89. *
  90. * Not verbatim WordPress, keeps seed value in backpress options.
  91. *
  92. * @since WP 2.6.2
  93. *
  94. * @param int $min Lower limit for the generated number (optional, default is 0)
  95. * @param int $max Upper limit for the generated number (optional, default is 4294967295)
  96. * @return int A random number between min and max
  97. */
  98. function rand( $min = 0, $max = 0 ) {
  99. global $rnd_value;
  100. $seed = backpress_get_transient('random_seed');
  101. // Reset $rnd_value after 14 uses
  102. // 32(md5) + 40(sha1) + 40(sha1) / 8 = 14 random numbers from $rnd_value
  103. if ( strlen($rnd_value) < 8 ) {
  104. $rnd_value = md5( uniqid(microtime() . mt_rand(), true ) . $seed );
  105. $rnd_value .= sha1($rnd_value);
  106. $rnd_value .= sha1($rnd_value . $seed);
  107. $seed = md5($seed . $rnd_value);
  108. backpress_set_transient('random_seed', $seed);
  109. }
  110. // Take the first 8 digits for our value
  111. $value = substr($rnd_value, 0, 8);
  112. // Strip the first eight, leaving the remainder for the next call to wp_rand().
  113. $rnd_value = substr($rnd_value, 8);
  114. $value = abs(hexdec($value));
  115. // Reduce the value to be within the min - max range
  116. // 4294967295 = 0xffffffff = max random number
  117. if ( $max != 0 )
  118. $value = $min + (($max - $min + 1) * ($value / (4294967295 + 1)));
  119. return abs(intval($value));
  120. }
  121. }