register.php 3.9 KB

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