Register_Action.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.
  17. * e-mail: e-mail.
  18. * password: password.
  19. * password_repeat: password.
  20. * If there is a match, it generates a token and sets cookies and session variablesand
  21. * redirects to the user homepage.
  22. *
  23. * @category Action
  24. */
  25. class Register_Action extends Action{
  26. /**
  27. * Executes the action.
  28. */
  29. function execute(){
  30. if (!isset($_POST['uname'])){
  31. $this->code = 400;
  32. $this->message = "Username is required.";
  33. header("Location: " . URL::BASE . "register/");
  34. return;
  35. }
  36. if (!isset($_POST['password'])){
  37. $this->code = 400;
  38. $this->message = "Password is required.";
  39. header("Location: " . URL::BASE . "register/");
  40. return;
  41. }
  42. if (!isset($_POST['password_repeat'])){
  43. $this->code = 400;
  44. $this->message = "Passwords do not match.";
  45. header("Location: " . URL::BASE . "register/");
  46. return;
  47. }
  48. if (!isset($_POST['mail'])){
  49. $this->code = 400;
  50. $this->message = "eMail is required.";
  51. header("Location: " . URL::BASE . "register/");
  52. return;
  53. }
  54. $uname = SQLite3::escapeString($_POST['uname']);
  55. if (strlen($uname) == 0){
  56. $this->code = 400;
  57. $this->message = "Username is required.";
  58. header("Location: " . URL::BASE . "register/");
  59. return;
  60. }
  61. $mail = SQLite3::escapeString($_POST['mail']);
  62. // TODO: Validate email
  63. if (strlen($mail) == 0){
  64. $this->code = 400;
  65. $this->message = "eMail is required.";
  66. header("Location: " . URL::BASE . "register/");
  67. return;
  68. }
  69. $password = SQLite3::escapeString($_POST['password']);
  70. if (strlen($password) == 0){
  71. $this->code = 400;
  72. $this->message = "Password is required.";
  73. header("Location: " . URL::BASE . "register/");
  74. return;
  75. }
  76. $password_repeat = SQLite3::escapeString($_POST['password_repeat']);
  77. if ($password_repeat != $password){
  78. $this->code = 400;
  79. $this->message = "Passwords do not match.";
  80. header("Location: " . URL::BASE . "register/");
  81. return;
  82. }
  83. $statement = get_context()->get_db()->prepare("
  84. SELECT count(id) AS c
  85. FROM user WHERE name = :name;
  86. ");
  87. $statement->bindValue(":name", $uname, SQLITE3_TEXT);
  88. if ($statement->execute()->fetchArray(SQLITE3_ASSOC)["c"] != 0){
  89. $this->code = 400;
  90. $this->message = "Username already in use.";
  91. header("Location: " . URL::BASE . "register/");
  92. return;
  93. }
  94. $statement = get_context()->get_db()->prepare("
  95. SELECT count(id) AS c
  96. FROM user WHERE mail = :mail;
  97. ");
  98. $statement->bindValue(":mail", $mail, SQLITE3_TEXT);
  99. if ($statement->execute()->fetchArray(SQLITE3_ASSOC)["c"] != 0){
  100. $this->code = 400;
  101. $this->message = "Mail address already in use.";
  102. header("Location: " . URL::BASE . "register/");
  103. return;
  104. }
  105. // All data is correct
  106. // Generate an API key
  107. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  108. $charactersLength = 16;
  109. $api = '';
  110. for ($i = 0; $i < $charactersLength; $i++) {
  111. $api .= $characters[rand(0, $charactersLength - 1)];
  112. }
  113. // Encode password
  114. $password = hash('sha256', $password);
  115. // Insert
  116. $statement = get_context()->get_db()->prepare("
  117. INSERT INTO user (id, name, mail, password, api_key, admin)
  118. VALUES(
  119. (SELECT max(id) + 1 FROM user),
  120. :name,
  121. :mail,
  122. :password,
  123. :api_key,
  124. :admin
  125. );
  126. ");
  127. $statement->bindValue(":name", $uname, SQLITE3_TEXT);
  128. $statement->bindValue(":mail", $uname, SQLITE3_TEXT);
  129. $statement->bindValue(":password", $password, SQLITE3_TEXT);
  130. $statement->bindValue(":api_key", $api, SQLITE3_TEXT);
  131. $statement->bindValue(":admin", 0, SQLITE3_INTEGER);
  132. $statement->execute();
  133. $statement->execute();
  134. // TODO: Redirect to login?
  135. $this->code = 204;
  136. $this->message = "No content.";
  137. header("Location: " . URL::BASE);
  138. return;
  139. }
  140. }