1
0

setup.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App;
  3. use Exception;
  4. use ParagonIE\AntiCSRF\AntiCSRF;
  5. use RuntimeException;
  6. $users = User::getAll();
  7. if($users) {
  8. Http::redirect("/home");
  9. }
  10. $request = Request::fromPost();
  11. $email = $request->string("email");
  12. $password = $request->raw("password");
  13. $confirm_password = $request->raw("confirm_password");
  14. if($email) {
  15. $csrf = new AntiCSRF();
  16. try {
  17. if(!$csrf->validateRequest()) {
  18. throw new RuntimeException(__("CSRF validation failed."));
  19. }
  20. if($password !== $confirm_password) {
  21. throw new RuntimeException(__("Passwords do not match."));
  22. }
  23. $user = new User();
  24. $user->title = $request->string("title");
  25. $user->first_name = $request->string("first_name");
  26. $user->last_name = $request->string("last_name");
  27. $user->email = $request->email("email");
  28. $user->password = password_hash($password, PASSWORD_DEFAULT);
  29. $user->admin = 1;
  30. $user->dts_created = Database::dts();
  31. $user->commit();
  32. Session::addAlert("success", __("User created successfully."));
  33. Http::redirect("/home");
  34. }
  35. catch(Exception $e) {
  36. Session::addAlert("danger", $e->getMessage());
  37. }
  38. }
  39. View::include("header");
  40. View::include("setup");
  41. View::include("footer");