login.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * File for the login action.
  4. *
  5. * Implements an action function to be called from the {@see Controller}.
  6. *
  7. * @category Action
  8. */
  9. /**
  10. * Logs the user in.
  11. *
  12. * Reads the POST parameters looking for the following KEYS:
  13. * uname
  14. * password
  15. * If there is a match, it sets cookies and session variables, and
  16. * redirects to the user homepage.
  17. *
  18. * @param resource $db Database connection.
  19. * @return int 0 on success, negative values on error.
  20. * @category Action
  21. */
  22. function action($db = null){
  23. if (!isset($_POST['uname'], $_POST['password'])){
  24. return -1;
  25. }
  26. $uname = SQLite3::escapeString($_POST['uname']);
  27. if (strlen($uname) == 0){
  28. return -2;
  29. }
  30. $password = SQLite3::escapeString($_POST['password']);
  31. if (strlen($password) == 0){
  32. return -3;
  33. }
  34. $password = hash('sha256', $password);
  35. $q = $db->query("SELECT uid FROM player WHERE upper(name) = upper('$uname') AND password = '$password';");
  36. $r = $q->fetchArray(SQLITE3_ASSOC);
  37. if (!$r){
  38. return -4;
  39. }
  40. $uid = $r["uid"];
  41. setcookie("uid", $uid, time() + 5 * 24 * 60 * 60); // 5 Days
  42. session_regenerate_id();
  43. $_SESSION['session'] = true;
  44. $_SESSION['name'] = $uname;
  45. $_SESSION['uid'] = $uid;
  46. header("Location: /$uid/");
  47. die();
  48. return 0;
  49. }
  50. ?>