register.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  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. $user_address->country = DEFAULT_COUNTRY;
  9. $secondary_user_address = new UserAddress;
  10. $secondary_user_address->name = __("Secondary Residence");
  11. $secondary_user_address->country = DEFAULT_COUNTRY;
  12. try {
  13. $submit = http::value("submit");
  14. if($submit) {
  15. Core::validateCSRFToken($submit);
  16. $form_exception = new FormException;
  17. $password = http::value("password");
  18. $password_confirm = http::value("password_confirm");
  19. if($password != $password_confirm) $form_exception->addError(__("Passwords do not match."));
  20. $user->email = http::value("email", "email");
  21. $user->password = password_hash($password, PASSWORD_DEFAULT, ["cost" => 14]);
  22. $user->title = http::value("title", "trim");
  23. $user->first_name = http::value("first_name", "trim");
  24. $user->last_name = http::value("last_name", "trim");
  25. $user->gender = http::value("gender", "trim");
  26. $user->date_of_birth = http::getDateFromFields("date_of_birth");
  27. $user->phone = http::value("phone", "trim");
  28. $user->terms_accepted = http::value("terms_accepted", "int") ? 1 : 0;
  29. $user->newsletter_consent = http::value("newsletter_content", "int") ? 1 : 0;
  30. $user->dts_registered = gmdate("Y-m-d H:i:s");
  31. $user_address->postal_code = http::value("postal_code");
  32. $user_address->country = http::value("country");
  33. $secondary_user_address->postal_code = http::value("secondary_postal_code");
  34. $secondary_user_address->country = http::value("country");
  35. // Referrer
  36. $referrer_id = http::value("r", "int");
  37. $referrer = User::get($referrer_id);
  38. if($referrer) $user->referrer_id = $referrer_id;
  39. if(!Form::checkRecaptcha()) $form_exception->addError(__("reCAPTCHA check failed."));
  40. 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."));
  41. if(empty($user->email)) $form_exception->addError(__("Email address is invalid."));
  42. if(empty($user->title)) $form_exception->addError(__("Title is required."));
  43. if(empty($user->first_name)) $form_exception->addError(__("First name is required."));
  44. if(empty($user->last_name)) $form_exception->addError(__("Last name is required."));
  45. if(empty($user->date_of_birth)) $form_exception->addError(__("Date of birth is required."));
  46. if(empty($user->phone)) $form_exception->addError(__("Mobile number is required."));
  47. if(empty($user->terms_accepted)) $form_exception->addError(__("Acceptance of Terms and Conditions is required."));
  48. if(empty($user_address->postal_code)) $form_exception->addError(__("Postal code is required."));
  49. if(empty($user_address->country)) $form_exception->addError(__("Country is required."));
  50. $email_exists = User::query(["email" => $user->email]);
  51. if($email_exists) $form_exception->addError(__("Email address is already registered."));
  52. // Geocode the postal code
  53. $success = $user_address->geocode();
  54. if(!$success) $form_exception->addError(__("Failed to get map location of current residence's postal code."));
  55. if($secondary_user_address->postal_code) {
  56. $success = $secondary_user_address->geocode();
  57. if(!$success) $form_exception->addError(__("Failed to get map location of secondary residence's postal code."));
  58. }
  59. if($form_exception->hasErrors()) {
  60. throw $form_exception;
  61. }
  62. // OK
  63. $success = $user->commit();
  64. if(!$success) throw new Exception(__("Registration failed. Please try again later."));
  65. $user_address->user_id = $user->id;
  66. $success = $user_address->commit();
  67. if($secondary_user_address->postal_code) {
  68. $secondary_user_address->user_id = $user->id;
  69. $secondary_user_address->commit();
  70. }
  71. $user->sendActivationEmail();
  72. Core::addAlert("success", __("Registration successful. An email has been sent to verify your email address. Please follow the instructions in the email before logging in."));
  73. HTTP::redirect("/log-in");
  74. }
  75. }
  76. catch(FormException $e) {
  77. $e->generateAlerts();
  78. }
  79. catch(Exception $e) {
  80. Core::addAlert("danger", $e->getMessage());
  81. }
  82. View::includeOnce("header", compact("page_title"));
  83. View::include("register", compact("user", "user_address", "secondary_user_address"));
  84. View::includeOnce("footer");