Guest User

Untitled

a guest
Nov 1st, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. class DbOperations{
  2.  
  3. private $con;
  4.  
  5. function __construct(){
  6.  
  7. require_once dirname(__FILE__).'/DbConnect.php';
  8.  
  9. $db = new DbConnect();
  10.  
  11. $this->con = $db->connect();
  12.  
  13. }
  14. function wp_hash_password($password) {
  15. global $wp_hasher;
  16.  
  17. if ( empty($wp_hasher) ) {
  18. require_once( ABSPATH . WPINC . '/class-phpass.php');
  19. // By default, use the portable hash from phpass
  20. $wp_hasher = new PasswordHash(8, true);
  21. }
  22.  
  23. return $wp_hasher->HashPassword( trim( $password ) );
  24. }
  25. function wp_check_password($password, $hash, $user_id = '') {
  26. global $wp_hasher;
  27.  
  28. // If the hash is still md5...
  29. if ( strlen($hash) <= 32 ) {
  30. $check = hash_equals( $hash, md5( $password ) );
  31. if ( $check && $user_id ) {
  32. // Rehash using new hash.
  33. wp_set_password($password, $user_id);
  34. $hash = wp_hash_password($password);
  35. }
  36.  
  37. /**
  38. * Filters whether the plaintext password matches the encrypted password.
  39. *
  40. * @since 2.5.0
  41. *
  42. * @param bool $check Whether the passwords match.
  43. * @param string $password The plaintext password.
  44. * @param string $hash The hashed password.
  45. * @param string|int $user_id User ID. Can be empty.
  46. */
  47. return apply_filters( 'check_password', $check, $password, $hash, $user_id );
  48. }
  49.  
  50. // If the stored hash is longer than an MD5, presume the
  51. // new style phpass portable hash.
  52. if ( empty($wp_hasher) ) {
  53. require_once( ABSPATH . WPINC . '/class-phpass.php');
  54. // By default, use the portable hash from phpass
  55. $wp_hasher = new PasswordHash(8, true);
  56. }
  57.  
  58. $check = $wp_hasher->CheckPassword($password, $hash);
  59.  
  60. /** This filter is documented in wp-includes/pluggable.php */
  61. return apply_filters( 'check_password', $check, $password, $hash, $user_id );
  62. }
  63.  
  64. /*CRUD -> C -> CREATE */
  65.  
  66. public function createUser($username, $pass, $email){
  67. if($this->isUserExist($username,$email)){
  68. return 0;
  69. }else{
  70. $password = wp_hash_password($pass);
  71. $stmt = $this->con->prepare("INSERT INTO `users` (`id`, `username`, `password`, `email`) VALUES (NULL, ?, ?, ?);");
  72. $stmt->bind_param("sss",$username,$password,$email);
  73.  
  74. if($stmt->execute()){
  75. return 1;
  76. }else{
  77. return 2;
  78. }
  79. }
  80. }
  81.  
  82. public function userLogin($username, $pass){
  83. $password = wp_check_password($pass);
  84. $stmt = $this->con->prepare("SELECT id FROM users WHERE username = ? AND password = ?");
  85. $stmt->bind_param("ss",$username,$password);
  86. $stmt->execute();
  87. $stmt->store_result();
  88. return $stmt->num_rows > 0;
  89. }
  90.  
  91. public function getUserByUsername($username){
  92. $stmt = $this->con->prepare("SELECT * FROM users WHERE username = ?");
  93. $stmt->bind_param("s",$username);
  94. $stmt->execute();
  95. return $stmt->get_result()->fetch_assoc();
  96. }
  97.  
  98.  
  99. private function isUserExist($username, $email){
  100. $stmt = $this->con->prepare("SELECT id FROM users WHERE username = ? OR email = ?");
  101. $stmt->bind_param("ss", $username, $email);
  102. $stmt->execute();
  103. $stmt->store_result();
  104. return $stmt->num_rows > 0;
  105. }
  106.  
  107. }
Add Comment
Please, Sign In to add comment