register.php 4.4 KB

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