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. function addUser($username,$password,$email){
  6. $salt = get_sql()->escape_string(salt());
  7. $email = get_sql()->escape_string($email);
  8. $username = get_sql()->escape_string($username);
  9. $hash = get_sql()->escape_string(saltedHash($password,$salt));
  10. $res = query("INSERT INTO `users` (email,name,password,salt) VALUES ('%s','%s','%s','%s')",Array($email,$username,$hash,$salt));
  11. alog('u',mysqli_insert_id(get_sql()),'User Created');
  12. return $res;
  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. ?>