Login_Action.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. require_once(PATH::ACTION . "Action.php");
  12. /**
  13. * Logs the user in.
  14. *
  15. * Reads the POST parameters looking for the following KEYS:
  16. * uname: username or email.
  17. * password: password.
  18. * If there is a match, it generates a token and sets cookies and session variablesand
  19. * redirects to the user homepage.
  20. *
  21. * @category Action
  22. */
  23. class Login_Action extends Action{
  24. /**
  25. * Executes the action.
  26. */
  27. function execute(){
  28. if (!isset($_POST['uname'], $_POST['password'])){
  29. $this->code = 401;
  30. $this->message = "Invalid username / password.";
  31. http_response_code(401);
  32. header("Location: " . URL::BASE . "login/?status=401");
  33. die(); // So the erorr is not handled by the controller.
  34. return 401;
  35. }
  36. $uname = SQLite3::escapeString($_POST['uname']);
  37. if (strlen($uname) == 0){
  38. $this->code = 401;
  39. $this->message = "Invalid username / password.";
  40. http_response_code(401);
  41. header("Location: " . URL::BASE . "login/?status=401");
  42. die(); // So the erorr is not handled by the controller.
  43. return 401;
  44. }
  45. $password = SQLite3::escapeString($_POST['password']);
  46. if (strlen($password) == 0){
  47. $this->code = 401;
  48. $this->message = "Invalid username / password.";
  49. http_response_code(401);
  50. header("Location: " . URL::BASE . "login/?status=401");
  51. die(); // So the erorr is not handled by the controller.
  52. return 401;
  53. }
  54. $password = hash('sha256', $password);
  55. $statement = get_context()->get_db()->prepare("
  56. SELECT
  57. id,
  58. name
  59. FROM user
  60. WHERE
  61. (
  62. upper(name) = upper(:name) OR
  63. upper(mail) = upper(:mail)
  64. ) AND
  65. password = :password;
  66. ");
  67. $statement->bindValue(":name", $uname, SQLITE3_TEXT);
  68. $statement->bindValue(":mail", $uname, SQLITE3_TEXT);
  69. $statement->bindValue(":password", $password, SQLITE3_TEXT);
  70. $statement->execute();
  71. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  72. if (!$r){
  73. $this->code = 401;
  74. $this->message = "Invalid username / password.";
  75. header("Location: " . URL::BASE . "login/?status=401");
  76. die(); // So the erorr is not handled by the controller.
  77. return 401;
  78. }
  79. $user = $r["id"];
  80. // Generate token
  81. //$_COOKIE["user_token"]) && isset($COOKIE["user_id"])
  82. $token = bin2hex(random_bytes(32));
  83. $expiry = time() + 5 * 24 * 60 * 60; // 5 days
  84. $statement = get_context()->get_db()->prepare("
  85. INSERT INTO token (user, token, expiry) VALUES
  86. (:user, :token, :expiry);
  87. ");
  88. $statement->bindValue(":user", $user, SQLITE3_INTEGER);
  89. $statement->bindValue(":token", hash('sha256', $token), SQLITE3_TEXT);
  90. $statement->bindValue(":expiry", $expiry, SQLITE3_INTEGER);
  91. $statement->execute();
  92. setcookie("user_id", $user, $expiry, "/");
  93. setcookie("user_token", $token, $expiry, "/");
  94. session_regenerate_id();
  95. $_SESSION['session'] = true;
  96. $_SESSION['user_id'] = $user;
  97. //header("Location: " . URL::BASE . "/$user/");
  98. // TODO: Get player ID here?
  99. $this->code = 204;
  100. $this->message = "No content.";
  101. header("Location: " . URL::BASE);
  102. die();
  103. return 204;
  104. }
  105. }