Login_Action.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. header("Location: " . URL::BASE . "login/");
  32. return;
  33. }
  34. $uname = SQLite3::escapeString($_POST['uname']);
  35. if (strlen($uname) == 0){
  36. $this->code = 401;
  37. $this->message = "Invalid username / password.";
  38. header("Location: " . URL::BASE . "login/");
  39. return;
  40. }
  41. $password = SQLite3::escapeString($_POST['password']);
  42. if (strlen($password) == 0){
  43. $this->code = 401;
  44. $this->message = "Invalid username / password.";
  45. header("Location: " . URL::BASE . "login/");
  46. return;
  47. }
  48. $password = hash('sha256', $password);
  49. $statement = get_context()->get_db()->prepare("
  50. SELECT
  51. id,
  52. name
  53. FROM user
  54. WHERE
  55. (
  56. upper(name) = upper(:name) OR
  57. upper(mail) = upper(:mail)
  58. ) AND
  59. password = :password;
  60. ");
  61. $statement->bindValue(":name", $uname, SQLITE3_TEXT);
  62. $statement->bindValue(":mail", $uname, SQLITE3_TEXT);
  63. $statement->bindValue(":password", $password, SQLITE3_TEXT);
  64. $statement->execute();
  65. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  66. if (!$r){
  67. $this->code = 401;
  68. $this->message = "Invalid username / password.";
  69. header("Location: " . URL::BASE . "login/");
  70. return;
  71. }
  72. $user = $r["id"];
  73. // Generate token
  74. //$_COOKIE["user_token"]) && isset($COOKIE["user_id"])
  75. $token = bin2hex(random_bytes(32));
  76. $expiry = time() + 5 * 24 * 60 * 60; // 5 days
  77. $statement = get_context()->get_db()->prepare("
  78. INSERT INTO token (user, token, expiry) VALUES
  79. (:user, :token, :expiry);
  80. ");
  81. $statement->bindValue(":user", $user, SQLITE3_INTEGER);
  82. $statement->bindValue(":token", hash('sha256', $token), SQLITE3_TEXT);
  83. $statement->bindValue(":expiry", $expiry, SQLITE3_INTEGER);
  84. $statement->execute();
  85. setcookie("user_id", $user, $expiry, "/");
  86. setcookie("user_token", $token, $expiry, "/");
  87. session_regenerate_id();
  88. $_SESSION['session'] = true;
  89. $_SESSION['user_id'] = $user;
  90. //header("Location: " . URL::BASE . "/$user/");
  91. // TODO: Get player ID here?
  92. $this->code = 204;
  93. $this->message = "No content.";
  94. header("Location: " . URL::BASE);
  95. return;
  96. }
  97. }