user.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. require_once(realpath(dirname(__FILE__)).'/config.php');
  3. require_once(PATH_PHP.'database.php');
  4. require_once(PATH_PHP.'security.php');
  5. global $mysqli;
  6. function addUser($username,$password,$email){
  7. global $mysqli;
  8. $salt = $mysqli->escape_string(salt());
  9. $email = $mysqli->escape_string($email);
  10. $username = $mysqli->escape_string($username);
  11. $hash = $mysqli->escape_string(saltedHash($password,$salt));
  12. return query("INSERT INTO `users` (email,name,password,salt) VALUES ('%s','%s','%s','%s')",Array($email,$username,$hash,$salt));
  13. }
  14. function isUser($name){
  15. $res = query("SELECT id FROM `users` WHERE name='%s'",Array($name));
  16. if($res->num_rows == 1){
  17. return true;
  18. }else{
  19. return false;
  20. }
  21. }
  22. function userId($name){
  23. if($user = query("SELECT id FROM `users` WHERE name='%s'",Array($name))){
  24. if($user->num_rows == 1){
  25. $user = $user->fetch_assoc();
  26. return $user['id'];
  27. }
  28. }
  29. return false;
  30. }
  31. function userObj($id){
  32. if(is_string($id)){
  33. $id = userId($id);
  34. }
  35. if($res = query("SELECT * FROM `users` WHERE id='%d'",Array($id))){
  36. if($res->num_rows == 1){
  37. return $res->fetch_assoc();
  38. }
  39. }
  40. return false;
  41. }
  42. ?>