| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- if(Core::$user) HTTP::redirect("/account");
- $page_title = _("Register");
- Core::addBreadcrumb("/register", $page_title);
- $user = new User;
- $user_address = new UserAddress();
- $user_address->name = _("Current Residence");
- $user_address->country = DEFAULT_COUNTRY;
- try {
- $submit = http::value("submit");
- if($submit) {
- Core::validateCSRFToken($submit);
- $form_exception = new FormException;
- $password = http::value("password");
- $password_confirm = http::value("password_confirm");
- if($password != $password_confirm) $form_exception->addError(_("Passwords do not match."));
- $user->email = http::value("email", "email");
- $user->password = password_hash($password, PASSWORD_DEFAULT, ["cost" => 14]);
- $user->title = http::value("title", "trim");
- $user->first_name = http::value("first_name", "trim");
- $user->last_name = http::value("last_name", "trim");
- $user->gender = http::value("gender", "trim");
- $user->date_of_birth = http::getDateFromFields("date_of_birth");
- $user->phone = http::value("phone", "trim");
- $user->terms_accepted = http::value("terms_accepted", "int");
- $user->newsletter_consent = http::value("newsletter_content", "int");
- $user->dts_registered = gmdate("Y-m-d H:i:s");
- $user_address->postal_code = http::value("postal_code");
- $user_address->country = http::value("country");
- // Referrer
- $referrer_id = http::value("r", "int");
- $referrer = User::get($referrer_id);
- if($referrer) $user->referrer_id = $referrer_id;
- if(!Form::checkRecaptcha()) $form_exception->addError(_("reCAPTCHA check failed."));
- 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."));
- if(empty($user->email)) $form_exception->addError(_("Email address is invalid."));
- if(empty($user->title)) $form_exception->addError(_("Title is required."));
- if(empty($user->first_name)) $form_exception->addError(_("First name is required."));
- if(empty($user->last_name)) $form_exception->addError(_("Last name is required."));
- if(empty($user->date_of_birth)) $form_exception->addError(_("Date of birth is required."));
- if(empty($user->phone)) $form_exception->addError(_("Mobile number is required."));
- if(empty($user->terms_accepted)) $form_exception->addError(_("Acceptance of Terms and Conditions is required."));
- if(empty($user_address->postal_code)) $form_exception->addError(_("Postal code is required."));
- if(empty($user_address->country)) $form_exception->addError(_("Country is required."));
- $email_exists = User::query(["email" => $user->email]);
- if($email_exists) $form_exception->addError(_("Email address is already registered."));
- // Geocode the postal code
- try {
- $results = Geolocation::geocode($user_address->postal_code . ", " . $user_address->country);
- $result = $results[0];
- foreach($result->address_components as $component) {
- $types = $component->types;
- if(in_array("postal_code", $types)) $user_address->postal_code = $component->long_name;
- if(in_array("route", $types)) $user_address->address_1 = $component->long_name;
- if(in_array("postal_town", $types)) $user_address->city = $component->long_name;
- if(in_array("administrative_area_level_1", $types)) $user_address->state = $component->long_name;
- if(in_array("country", $types)) $user_address->country = $component->long_name;
- }
- $user_address->latitude = $result->geometry->location->lat;
- $user_address->longitude = $result->geometry->location->lng;
- }
- catch(Exception $e) {
- $form_exception->addError(_("Failed to get location from postal code and country."));
- }
- if($form_exception->hasErrors()) {
- throw $form_exception;
- }
- // OK
- $success = $user->commit();
- if(!$success) throw new Exception(_("Registration failed. Please try again later."));
- $user_address->user_id = $user->id;
- $success = $user_address->commit();
- $user->sendActivationEmail();
- 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."));
- HTTP::redirect("/log-in");
- }
- }
- catch(FormException $e) {
- $e->generateAlerts();
- }
- catch(Exception $e) {
- Core::addAlert("danger", $e->getMessage());
- }
- View::includeOnce("header", compact("page_title"));
- View::include("register", compact("user", "user_address"));
- View::includeOnce("footer");
- ?>
|