Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class User extends Controller {
- function User()
- {
- parent::Controller();
- $this->output->enable_profiler(TRUE);
- // Load email library
- $this->load->library('email');
- }
- /*
- ******************
- * PAGE FUNCTIONS *
- ******************
- */
- // Login
- function login()
- {
- // Validation Rules
- $rules = array(
- 'email' => 'required|callback_check_login',
- 'password' => 'required');
- // Set validation rules
- $this->validation->set_rules($rules);
- // Validation fields
- $fields = array(
- 'email'=>'Email Address',
- 'password'=>'Password');
- // Set error delimiters
- $this->validation->set_error_delimiters('<li>', '</li>');
- // Set validation fields
- $this->validation->set_fields($fields);
- if ($this->validation->run())
- {
- // Redirect logged in users to home.
- redirect("my/home");
- }
- else
- {
- // Write form view to template engine
- $this->template->write_view('content', 'forms/login');
- // render page
- $this->template->render();
- }
- }
- // Register
- function register()
- {
- // Validation Rules
- $rules = array(
- 'first_name' => 'required',
- 'last_name' => 'required',
- 'email' => 'required|valid_email|callback_email_check',
- 'password' => 'required|matches[confirm_password]',
- 'confirm_password' => 'required',
- 'gender' => 'required',
- 'day' => 'required',
- 'month' => 'required',
- 'year' => 'required');
- // Set validation rules
- $this->validation->set_rules($rules);
- // Validation fields
- $fields = array(
- 'first_name'=>'First Name',
- 'last_name'=>'Last Name',
- 'email'=>'Email Address',
- 'password'=>'Password',
- 'confirm_password'=>'Confirm Password',
- 'gender'=>'Gender',
- 'day'=>'Day',
- 'month'=>'Month',
- 'year'=>'Year');
- // Set error delimiters
- $this->validation->set_error_delimiters('<li>', '</li>');
- // Set validation fields
- $this->validation->set_fields($fields);
- if ($this->validation->run())
- {
- // Create birthday stamp.
- $birthday = mktime(0, 0, 0, $this->input->post('month'), $this->input->post('day'), $this->input->post('year'));
- // Database fields
- $data = array(
- 'First_Name' => $this->input->post('first_name'),
- 'Last_Name' => $this->input->post('last_name'),
- 'Email' => $this->input->post('email'),
- 'Password' => md5($this->input->post('password')),
- 'Birth' => $birthday,
- 'Gender' => $this->input->post('gender'),
- 'Stamp' => time()
- );
- // Create user.
- $this->User_auth->create_user($data);
- // Send out Activation email
- $this->email->from('auto@domain.com', 'Site');
- $this->email->to($this->input->post('email'));
- $this->email->subject('Site Account Activation');
- // Email template.
- $message = 'You need to activate your account before you are able to login.'."\n\n";
- $message .= 'Click or paste the link below into your browser to activate your account.'."\n";
- $message .= $this->config->site_url().'/activate/'.md5($this->input->post('email')).''."\n\n";
- $message .= '*******************'."\n";
- $message .= '* ACCOUNT DETAILS *'."\n";
- $message .= '*******************'."\n";
- $message .= 'username: '.$this->input->post('email').''."\n";
- $message .= 'password: '.$this->input->post('password').''."\n\n";
- $message .= 'The [Site] Team';
- $this->email->message($message);
- $this->email->send();
- $content_body = '<p>Your account has been successfully created.</p>';
- $content_body .= '<p>You will be sent out an acivation e-mail in the next 24/48 hours.</p>';
- $content_body .= '<p>If you don\'t recieve your acivation e-mail please contact the support team at help@domain.com</p>';
- $content = array('title'=>'Congratulations','content'=>$content_body);
- // Write form view to template engine
- $this->template->write_view('body', 'content/message', $content);
- // render page
- $this->template->render();
- }
- else
- {
- // Write form view to template engine
- $this->template->write_view('body', 'forms/register');
- // render page
- $this->template->render();
- }
- }
- // Activation
- function activate()
- {
- // Grab segment 3 from URL
- $email = $this->uri->segment(3);
- // Check to see if email is not empty
- if (!empty($email))
- {
- // Query
- $query = $this->db->get_where("users", array('MD5(Email)' => $email));
- // Row count from results
- if ($query->num_rows() > 0)
- {
- // Get results
- $row = $query->row();
- // Check if user account has been activated
- if ($row->Activate == 0)
- {
- // update activation details for this user account
- $data = array('Activate' => 1);
- $where = "MD5(Email)='".$email."'";
- $str = $this->db->update_string('users', $data, $where);
- $this->db->query($str);
- // dump complete message
- $content_body = '<p>Your account has been successfully activated.</p>';
- $content_body .= '<p><a href="#" title="">Login to your account</a></p>';
- $content = array('title'=>'Activated','content'=>$content_body);
- // Write form view to template engine
- $this->template->write_view('body', 'content/message', $content);
- // render page
- $this->template->render();
- }
- else
- {
- // dump error message (account is allready active)
- $content_body = '<p>Your account has allready been activated.</p>';
- $content = array('title'=>'Activation Error (ACCA)','content'=>$content_body);
- // Write form view to template engine
- $this->template->write_view('body', 'content/message', $content);
- // render page
- $this->template->render();
- }
- }
- else
- {
- // dump error message (no user found)
- $content_body = '<p>There were problems when trying to activate your account.</p>';
- $content_body .= '<p>if the problem persists please contact the support team at help@domain.com</p>';
- $content = array('title'=>'Activation Error (NOUSR)','content'=>$content_body);
- // Write form view to template engine
- $this->template->write_view('body', 'content/message', $content);
- // render page
- $this->template->render();
- }
- }
- else
- {
- // dump error message (no segments found)
- $content_body = '<p>There were problems when trying to activate your account.</p>';
- $content_body .= '<p>if the problem persists please contact the support team at help@domain.com</p>';
- $content = array('title'=>'Activation Error (NOSEG)','content'=>$content_body);
- // Write form view to template engine
- $this->template->write_view('body', 'content/message', $content);
- // render page
- $this->template->render();
- }
- }
- /*
- ***************************
- * FORM CALLBACK FUNCTIONS *
- ***************************
- */
- // Check to see if username is in our database.
- function email_check($str)
- {
- // Query
- $query = $this->db->get_where("users", array('Email' => $str));
- // Row count from results
- if ($query->num_rows() > 0)
- {
- // Add error and return false
- $this->validation->set_message('email_check', 'E-mail address you have entered is allready in our database.');
- return FALSE;
- }
- else
- {
- // Return true with no error
- return TRUE;
- }
- }
- // Check to see if the user exists and attempt a login.
- function check_login($email)
- {
- $password = md5($this->input->post('password'));
- if ($this->user_auth->try_login(array('Email'=>$email, 'Password'=>$password)))
- {
- return TRUE;
- }
- else
- {
- $this->validation->set_message('check_login', 'Your Email/Password do not match our records.');
- return FALSE;
- }
- }
- }
- /* End of file user.php */
- /* Location: ./system/application/controllers/user.php */
Add Comment
Please, Sign In to add comment