login.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * File for the login action.
  4. *
  5. * Implements an action function to be called from the {@see Controller}.
  6. *
  7. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package SWDB
  10. */
  11. /**
  12. * Logs the user in.
  13. *
  14. * Reads the POST parameters looking for the following KEYS:
  15. * uname: username or email.
  16. * password: password.
  17. * If there is a match, it generates a token and sets cookies and session variablesand
  18. * redirects to the user homepage.
  19. *
  20. * @return int 201 on success, HTTP error codes on failure.
  21. * @category Action
  22. */
  23. function action(){
  24. if (!isset($_POST['uname'], $_POST['password'])){
  25. header("Location: " . URL::BASE);
  26. //exit();
  27. return 400;
  28. }
  29. $uname = SQLite3::escapeString($_POST['uname']);
  30. if (strlen($uname) == 0){
  31. header("Location: " . URL::BASE);
  32. //exit();
  33. return 400;
  34. }
  35. $password = SQLite3::escapeString($_POST['password']);
  36. if (strlen($password) == 0){
  37. header("Location: " . URL::BASE);
  38. return 400;
  39. //exit();
  40. }
  41. $password = hash('sha256', $password);
  42. $statement = get_context()->get_db()->prepare("
  43. SELECT
  44. id,
  45. name
  46. FROM user
  47. WHERE
  48. (
  49. upper(name) = upper(:name) OR
  50. upper(mail) = upper(:mail)
  51. ) AND
  52. password = :password;
  53. ");
  54. $statement->bindValue(":name", $uname, SQLITE3_TEXT);
  55. $statement->bindValue(":mail", $uname, SQLITE3_TEXT);
  56. $statement->bindValue(":password", $password, SQLITE3_TEXT);
  57. $statement->execute();
  58. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  59. if (!$r){
  60. header("Location: " . URL::BASE);
  61. //exit();
  62. //die();
  63. return 400;
  64. }
  65. $user = $r["id"];
  66. // Generate token
  67. //$_COOKIE["user_token"]) && isset($COOKIE["user_id"])
  68. $token = bin2hex(random_bytes(32));
  69. $expiry = time() + 5 * 24 * 60 * 60; // 5 days
  70. $statement = get_context()->get_db()->prepare("
  71. INSERT INTO token (user, token, expiry) VALUES
  72. (:user, :token, :expiry);
  73. ");
  74. $statement->bindValue(":user", $user, SQLITE3_INTEGER);
  75. $statement->bindValue(":token", hash('sha256', $token), SQLITE3_TEXT);
  76. $statement->bindValue(":expiry", $expiry, SQLITE3_INTEGER);
  77. $statement->execute();
  78. setcookie("user_id", $user, $expiry, "/");
  79. setcookie("user_token", $token, $expiry, "/");
  80. session_regenerate_id();
  81. $_SESSION['session'] = true;
  82. $_SESSION['user_id'] = $user;
  83. //header("Location: " . URL::BASE . "/$user/");
  84. // TODO: Get player ID here?
  85. header("Location: " . URL::BASE);
  86. exit();
  87. die();
  88. return 201;
  89. }
  90. ?>