Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Register extends CI_Controller
- {
- public function __construct()
- {
- parent::__construct();
- $this->load->model('user_model', 'user');
- }
- public function index()
- {
- // Check to see if user is allowed to register.
- if ($this->config->item('allow_registration', 'site_configs') == FALSE)
- {
- // Registration is disabled.
- show_404();
- }
- else
- {
- $this->template
- ->title('Project Manager', 'Register')
- ->set_layout('usermanagement_layout_view')
- ->set_partial('header', 'partials/header_view')
- ->set_partial('footer', 'partials/footer_view')
- ->build('register_view');
- }
- }
- public function process()
- {
- // Get defaults for output
- $output_array = $this->general_functions->get_default_output();
- // Load the validation rules from the user model to protect the backend of the system.
- $this->form_validation->set_rules($this->user->rules);
- // Check to see if validation is properly met.
- if ($this->form_validation->run() == FALSE)
- {
- // Form validation did not pass successfully. Report back to the user there was error(s) on the form.
- $output_array['message'] = 'The following form did not validate successfully. Please fix the form errors and try again.';
- $output_array['errors'] = $this->form_validation->error_array();
- }
- else
- {
- // Form validation passed successfully.
- // Set up variables from post data.
- $post_username = $this->input->post('username');
- $post_email_address = $this->input->post('email_address');
- $post_password = $this->input->post('password');
- // Develop a password array from the $post_password variable for hashing.
- $hashed_password = $this->general_functions->generate_password_hash($post_password);
- // Find out if an account already exists with the user's email address. Should return TRUE/FALSE depending on if record is found.
- if ($this->user->is_email_address_available($post_email_address) == FALSE)
- {
- // Retrieve the user data.
- $user_data = $this->user->get_user_data($post_email_address);
- // TODO: Add confirmation inside of status message 1 to ask if user wants message sent to him again.
- $status_messages = array(
- '1' => 'You have already registered for an account but have not verified your account. Do you need us to send your verification email so you can verify your account? '.anchor('send-again', 'Yes').' or '.anchor('login', 'No'),
- '2' => 'You have already registered for an account. You may proceed to the login page.'.anchor('login', 'Login'),
- '3' => 'Your account has been suspended.',
- '4' => 'You have been banned.',
- '5' => 'Your account has been deleted.'
- );
- if(array_key_exists($user_data->status_id, $status_messages))
- {
- $output_array['message'] = $status_messages[$user_data->status_id];
- }
- else
- {
- $output_array['message'] = 'Oops something happened that we are unaware of so please email the site administrator and we will look into it immediately.';
- }
- }
- else
- {
- // Email Address has not yet registered.
- // Check to see if username is available. Should return TRUE/FALSE depending on if record is found.
- if ($this->user->is_username_available($post_username) == FALSE)
- {
- // Username already exists.
- $output_array['message'] = 'The username is not available. Please choose another and try again.';
- }
- else
- {
- $registration_date = gmdate('Y-m-d H:i:s', time());
- $registration_key = sha1($registration_date.';;'.$post_email_address.';;'.$hashed_password[0]);
- // Develop the array of post data for sending to the model.
- $data = array(
- 'username' => $post_username,
- 'email_address' => $post_email_address,
- 'password' => $hashed_password[0],
- 'password_hash' => $hashed_password[1],
- 'registration_date' => $registration_date,
- 'registration_key' => $registration_key,
- );
- // Try to create the user.
- if ($this->user->create_user($data) == FALSE)
- {
- // User was not created successfully.
- // Report back to the user that was not created succesfully.
- $output_array['message'] = 'The user was not created successfully.';
- }
- else
- {
- // User was successfully created and the user needs to verify their account.
- // Add data to the data array to send in the email.
- $data['company_name'] = $this->config->item('company_name', 'site_configs');
- $data['company_email'] = $this->config->item('company_email', 'site_configs');
- $data['password'] = $post_password;
- // Send email to user who registered with registry information.
- $this->general_functions->send_email($this->config->item('company_email', 'site_configs'), $post_email_address, 'Registration for '.$this->config->item('company_name', 'site_configs'), $this->load->view('emails/registration_view', $data, TRUE));
- // TODO: Send an email to the site administrator letting them know of a new registration.
- // Report back to the user that the registration was a success.
- $output_array['status'] = 'success';
- $output_array['message'] = 'The user was created successfully. Please check your email for the link to activate your account.';
- }
- }
- }
- }
- echo json_encode($output_array);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement