register.php 3.6 KB

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