Example #1
0
 public function registerUser()
 {
     $session = new SessionHelper();
     $mail = new MailHelper();
     $usermodel = new UserModel();
     // Form validation
     $formval = new FormHelper();
     // Username
     $username = $formval->testInput($_POST['username']);
     // Password
     $password = $formval->testInput($_POST['password']);
     // Email
     $email = $formval->testInput($_POST['email']);
     // Captcha
     include_once BASE_URI . 'app/vendor/securimage/securimage.php';
     $securimage = new Securimage();
     if ($securimage->check($formval->testInput($_POST['captcha_code'])) == false) {
         $session->setMessage('Verification code was incorrect, please try again', 3);
         redirectTo(BASE_URL . 'index.php?c=user&a=viewregisterpage');
     }
     // Check if the username is taken
     $users = $usermodel->getUserNames();
     // If we have a match the username is already in use
     if (in_array(strtolower($username), $users, true)) {
         $session->setMessage('Username is taken', 3);
         redirectTo(BASE_URL . 'index.php?c=user&a=viewregisterpage');
     }
     // Check if the email address is taken
     $emailaddresses = $usermodel->getEmailAddresses();
     if (in_array(strtolower($email), $emailaddresses, true)) {
         $session->setMessage('Email address is in use, if you forgot your password go to the login page and click lost password', 3);
         redirectTo(BASE_URL . 'index.php?c=user&a=viewregisterpage');
     } else {
         // Create the new user
         $user = new User();
         // Hash password
         $user->hashed_password = password_hash($password, PASSWORD_DEFAULT);
         $user->username = strtolower($username);
         $user->email = $email;
         // The default role is user
         $user->role = 3;
         // The user is disabled by default untill an admin actives the account
         $user->is_active = 0;
         $newusermodel = new UserModel();
         if ($newusermodel->createUser($user)) {
             $session->setMessage('Account creation successfull, please login (after admin has actived the account)', 4);
             $mail->sendWelcomeMail($username, $email, $username);
             $this->viewLoginPage();
         } else {
             redirectTo(BASE_URL . 'index.php?c=user&a=viewregisterpage');
         }
     }
 }