user.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. if($user = $res->fetch_assoc()){
  38. unset($user['password']);
  39. unset($user['salt']);
  40. unset($user['key']);
  41. return $user;
  42. }
  43. }
  44. }
  45. return false;
  46. }
  47. ?>