SubmitRegisterController.inc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. class SubmitRegisterController implements IController {
  3. private $form;
  4. private $database;
  5. private $authenticator;
  6. private $header;
  7. public function __construct() {
  8. $this->form = new Form();
  9. $this->database = new Database();
  10. $this->authenticator = new Authenticator();
  11. $this->header = new Header();
  12. }
  13. public function execute() {
  14. $newDisplayName = $this->form->postCleanDefaulted('newUsername', "");
  15. $newUsername = strtolower($newDisplayName);
  16. $newEmail = strtolower($this->form->postCleanDefaulted('newEmail', ""));
  17. $newPassword1 = $this->form->postCleanDefaulted('newPassword1', "");
  18. $newPassword2 = $this->form->postCleanDefaulted('newPassword2', "");
  19. $sql = sprintf("SELECT * FROM users WHERE username = '%s' OR email = '%s'", $newUsername, $newEmail);
  20. $uniqueCheck = $this->database->query($sql);
  21. if (count($uniqueCheck) > 0) {
  22. // already in use
  23. return;
  24. }
  25. if ($newPassword1 != $newPassword2) {
  26. // passwords don't match
  27. return;
  28. }
  29. $sql = sprintf("INSERT INTO users (email, username, display_name)
  30. VALUES ('%s', '%s', '%s')", $newEmail, $newUsername, $newDisplayName);
  31. $userId = $this->database->write($sql);
  32. if ($userId == 0) {
  33. // something went wrong with inserting the user
  34. return;
  35. }
  36. $sql = sprintf("INSERT INTO users_auth (user_id, passwordhash)
  37. VALUES ('%s', SHA1('%s'))", $userId, $newPassword1);
  38. $this->database->write($sql);
  39. $sql = "SELECT * FROM default_topics";
  40. $defaultTopics = $this->database->query($sql);
  41. $sql = "INSERT INTO user_topic_subscription (user_id, topic_id) VALUES ";
  42. $subscribedTopics = array();
  43. foreach ($defaultTopics as $topicData) {
  44. $subscribedTopics[] = sprintf("('%s', '%s')", $userId, $topicData['topic_id']);
  45. }
  46. $sql .= implode(',', $subscribedTopics);
  47. $this->database->query($sql);
  48. $this->authenticator->authenticate($newUsername, $newPassword1);
  49. $this->header->redirect('/');
  50. }
  51. }