register.php 4.0 KB

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