Advertisement
Guest User

Untitled

a guest
Nov 21st, 2013
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.85 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.      
  3. class Register extends CI_Controller
  4. {  
  5.     public function __construct()
  6.     {
  7.          parent::__construct();
  8.          $this->load->model('user_model', 'user');
  9.     }
  10.    
  11.     public function index()
  12.     {  
  13.         // Check to see if user is allowed to register.
  14.         if ($this->config->item('allow_registration', 'site_configs') == FALSE)
  15.         {
  16.             // Registration is disabled.
  17.             show_404();
  18.         }
  19.         else
  20.         {  
  21.             $this->template
  22.                 ->title('Project Manager', 'Register')
  23.                 ->set_layout('usermanagement_layout_view')
  24.                 ->set_partial('header', 'partials/header_view')
  25.                 ->set_partial('footer', 'partials/footer_view')
  26.                 ->build('register_view');
  27.         }
  28.     }
  29.    
  30.     public function process()
  31.     {
  32.         // Get defaults for output
  33.         $output_array = $this->general_functions->get_default_output();
  34.  
  35.         // Load the validation rules from the user model to protect the backend of the system.
  36.         $this->form_validation->set_rules($this->user->rules);
  37.  
  38.         // Check to see if validation is properly met.
  39.         if ($this->form_validation->run() == FALSE)
  40.     {
  41.             // Form validation did not pass successfully. Report back to the user there was error(s) on the form.
  42.             $output_array['message'] = 'The following form did not validate successfully. Please fix the form errors and try again.';
  43.             $output_array['errors'] = $this->form_validation->error_array();
  44.     }
  45.     else
  46.     {
  47.             // Form validation passed successfully.
  48.             // Set up variables from post data.
  49.             $post_username = $this->input->post('username');
  50.             $post_email_address = $this->input->post('email_address');
  51.             $post_password = $this->input->post('password');
  52.            
  53.             // Develop a password array from the $post_password variable for hashing.
  54.             $hashed_password = $this->general_functions->generate_password_hash($post_password);
  55.            
  56.             // Find out if an account already exists with the user's email address. Should return TRUE/FALSE depending on if record is found.
  57.             if ($this->user->is_email_address_available($post_email_address) == FALSE)
  58.             {
  59.                 // Retrieve the user data.
  60.                 $user_data = $this->user->get_user_data($post_email_address);
  61.            
  62.                    
  63.                 // TODO: Add confirmation inside of status message 1 to ask if user wants message sent to him again.
  64.                 $status_messages = array(
  65.                     '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'),
  66.                     '2' => 'You have already registered for an account. You may proceed to the login page.'.anchor('login', 'Login'),
  67.                     '3' => 'Your account has been suspended.',
  68.                     '4' => 'You have been banned.',
  69.                     '5' => 'Your account has been deleted.'
  70.                 );
  71.                
  72.                 if(array_key_exists($user_data->status_id, $status_messages))
  73.                 {
  74.                     $output_array['message'] = $status_messages[$user_data->status_id];
  75.                 }
  76.                 else
  77.                 {
  78.                     $output_array['message'] = 'Oops something happened that we are unaware of so please email the site administrator and we will look into it immediately.';
  79.                 }
  80.             }
  81.             else
  82.             {
  83.                 // Email Address has not yet registered.
  84.                
  85.                 // Check to see if username is available. Should return TRUE/FALSE depending on if record is found.
  86.                 if ($this->user->is_username_available($post_username) == FALSE)
  87.                 {
  88.                     // Username already exists.
  89.                     $output_array['message'] = 'The username is not available. Please choose another and try again.';
  90.                 }
  91.                 else
  92.                 {
  93.                     $registration_date = gmdate('Y-m-d H:i:s', time());
  94.                     $registration_key = sha1($registration_date.';;'.$post_email_address.';;'.$hashed_password[0]);
  95.        
  96.                     // Develop the array of post data for sending to the model.
  97.                     $data = array(
  98.                         'username' => $post_username,
  99.                         'email_address' => $post_email_address,
  100.                         'password' => $hashed_password[0],
  101.                         'password_hash' => $hashed_password[1],
  102.                         'registration_date' => $registration_date,
  103.                         'registration_key' => $registration_key,
  104.                     );
  105.                    
  106.                     // Try to create the user.
  107.                     if ($this->user->create_user($data) == FALSE)
  108.                     {
  109.                         // User was not created successfully.
  110.                         // Report back to the user that was not created succesfully.
  111.                         $output_array['message'] = 'The user was not created successfully.';
  112.                     }
  113.                     else
  114.                     {
  115.                         // User was successfully created and the user needs to verify their account.
  116.                        
  117.                         // Add data to the data array to send in the email.              
  118.                         $data['company_name'] = $this->config->item('company_name', 'site_configs');
  119.                         $data['company_email'] = $this->config->item('company_email', 'site_configs');
  120.                         $data['password'] = $post_password;
  121.                        
  122.                         // Send email to user who registered with registry information.
  123.                         $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));
  124.                        
  125.                         // TODO: Send an email to the site administrator letting them know of a new registration.
  126.                        
  127.                         // Report back to the user that the registration was a success.
  128.                         $output_array['status'] = 'success';
  129.                         $output_array['message'] = 'The user was created successfully. Please check your email for the link to activate your account.';
  130.                     }
  131.                 }
  132.             }
  133.     }
  134.         echo json_encode($output_array);
  135.     }  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement