register.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. if(Core::$user) HTTP::redirect("/account");
  3. $page_title = "Register";
  4. Core::addBreadcrumb("/register", $page_title);
  5. $user = new User;
  6. $user_address = new UserAddress();
  7. try {
  8. $submit = http::value("submit");
  9. if($submit) {
  10. Core::validateCSRFToken($submit);
  11. $form_exception = new FormException;
  12. $password = http::value("password");
  13. $password_confirm = http::value("password_confirm");
  14. if($password != $password_confirm) $form_exception->addError("Passwords do not match.");
  15. $user->email = http::value("email", "email");
  16. $user->password = password_hash($password, PASSWORD_DEFAULT, ["cost" => 14]);
  17. $user->title = http::value("title", "trim");
  18. $user->first_name = http::value("first_name", "trim");
  19. $user->last_name = http::value("last_name", "trim");
  20. $user->date_of_birth = http::value("date_of_birth", "trim");
  21. $user->phone = http::value("phone", "trim");
  22. $user->dts_registered = gmdate("Y-m-d H:i:s");
  23. $user_address->postal_code = http::value("postal_code");
  24. if(empty($user->email)) $form_exception->addError("Email address is invalid.");
  25. if(empty($user->title)) $form_exception->addError("Title is required.");
  26. if(empty($user->first_name)) $form_exception->addError("First name is required.");
  27. if(empty($user->last_name)) $form_exception->addError("Last name is required.");
  28. if(empty($user->date_of_birth)) $form_exception->addError("Date of birth is required.");
  29. if(empty($user->phone)) $form_exception->addError("Mobile number is required.");
  30. if(empty($user_address->postal_code)) $form_exception->addError("Postal code is required.");
  31. $email_exists = User::query(["email" => $user->email]);
  32. if($email_exists) $form_exception->addError("Email address is already registered.");
  33. // Geocode the postal code
  34. try {
  35. $results = Geolocation::geocode($user_address->postal_code);
  36. $result = $results[0];
  37. foreach($result->address_components as $component) {
  38. $types = $component->types;
  39. if(in_array("postal_code", $types)) $user_address->postal_code = $component->long_name;
  40. if(in_array("route", $types)) $user_address->address_1 = $component->long_name;
  41. if(in_array("postal_town", $types)) $user_address->city = $component->long_name;
  42. if(in_array("administrative_area_level_1", $types)) $user_address->state = $component->long_name;
  43. if(in_array("country", $types)) $user_address->country = $component->long_name;
  44. }
  45. $user_address->latitude = $result->geometry->location->lat;
  46. $user_address->longitude = $result->geometry->location->lng;
  47. }
  48. catch(Exception $e) {
  49. $form_exception->addError("Failed to get location from postal code.");
  50. }
  51. if($form_exception->hasErrors()) {
  52. throw $form_exception;
  53. }
  54. // OK
  55. $success = $user->commit();
  56. if(!$success) throw new Exception("Registration failed. Please try again later.");
  57. $user_address->user_id = $user->id;
  58. $success = $user_address->commit();
  59. // TODO: Send out registration email
  60. Core::addAlert("success", "Registration successful. You can now log in.");
  61. HTTP::redirect("/log-in");
  62. }
  63. }
  64. catch(FormException $e) {
  65. $e->generateAlerts();
  66. }
  67. catch(Exception $e) {
  68. Core::addAlert("danger", $e->getMessage());
  69. }
  70. View::includeOnce("header", compact("page_title"));
  71. View::include("register", compact("user", "user_address"));
  72. View::includeOnce("footer");
  73. ?>