Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Nm_auth
- {
- var $email;
- var $password;
- var $message;
- private $_logged_in = FALSE;
- function __construct()
- {
- $this->_CI =& get_instance();
- $this->_CI->load->library('session');
- $this->_CI->load->model('auth');
- $this->_CI->config->load('NM_auth');
- log_message('debug', "NMauth Class Initialized");
- if($this->_CI->session->userdata('email')){
- $this->_logged_in = TRUE;
- }
- }
- function check_email($email)
- {
- $query = $this->_CI->db->get_where("users", array ('LOWER(email)'=>strtolower($email)));
- if($query->num_rows() == 1){
- return TRUE;
- }
- return FALSE;
- }
- function check_user($email, $password)
- {
- $query = $this->_CI->db->get_where("users", array ('LOWER(email)'=>strtolower($email)));
- if($query->num_rows() == 1){
- if(md5($password) == $query->row()->password) {
- return TRUE;
- } else {
- return FALSE;
- }
- }
- }
- function is_logged_in()
- {
- return $this->_logged_in ? TRUE : FALSE;
- }
- function redirect_logged_in()
- {
- if ($this->is_logged_in() != FALSE)
- {
- $this->_CI->session->set_flashdata('error', 'You are already logged in.');
- redirect((($this->_CI->session->userdata('last-page') == '/login') || ($this->_CI->session->userdata('last-page') == '/register')) ? '' : $this->_CI->session->userdata('last-page'));
- }
- }
- function redirect_back($message)
- {
- if ($this->is_logged_in() != FALSE)
- {
- $this->_CI->session->set_flashdata('info', $message);
- redirect(($this->_CI->session->userdata('last-page') != 'login') && ($this->_CI->session->userdata('last-page') != 'register') ? $this->_CI->session->userdata('last-page') : '');
- }
- }
- function get_user_data($email)
- {
- $this->_CI->db->select('users.*, profiles.*');
- $this->_CI->db->join('profiles', 'profiles.user_id = users.id', 'left');
- $this->_CI->db->group_by('users.id');
- $this->_CI->db->where('LOWER(users.email)', strtolower($email));
- $query = $this->_CI->db->get('users');
- return $query->row();
- }
- function login($email, $password)
- {
- if($this->check_user($email, $password))
- {
- $user = $this->get_user_data($email);
- if($user->is_active == '1')
- {
- $session_data = array (
- 'email'=>$user->email,
- 'user_id'=>$user->user_id
- );
- if($user->level == '1') {
- $session_data['admin'] = 'TRUE';
- }
- if($user->level == '2') {
- $session_data['cust'] = 'TRUE';
- }
- $this->_CI->session->set_userdata($session_data);
- $this->_CI->db->where('id', $user->user_id);
- $this->_CI->db->set('last_active', date('Y-m-d H:i:s'));
- if($this->_CI->db->update('users')){
- $this->_logged_in = TRUE;
- return TRUE;
- }
- }
- } else {
- $this->_logged_in = FALSE;
- return FALSE;
- }
- }
- function logout($email)
- {
- $user = $this->get_user_data($email);
- $data['last_active'] = date("Y-m-d H:i:s");
- $this->_CI->db->where('id', $this->_CI->session->userdata('user_id'));
- if($this->_CI->db->update('users', $data)){
- $unset_params = array('regenerated'=>'', 'last-page'=>'logout', 'email'=>'', 'user_id'=>'', 'admin'=>'FALSE', 'cust'=>'FALSE');
- $this->_CI->session->unset_userdata($unset_params);
- $this->_logged_in = FALSE;
- return TRUE;
- }
- return FALSE;
- }
- function register($reg_data){
- if(is_array($reg_data)){
- $data = array(
- 'email' => $reg_data['email'],
- 'password' => md5($reg_data['password']),
- 'level' => '2',
- 'is_active' => '1'
- );
- if($this->_CI->db->insert('users', $data)){
- $this->_CI->email->from(EMAIL_FROM, SITE_NAME.' Management');
- $this->_CI->email->to($reg_data['email']);
- $this->_CI->email->subject('Your New Account Registration Confirmation');
- $msg = "Thanks for registering at ".SITE_NAME."! Your account details are below.\r\n\r\n";
- $msg .= "Email Address: ".$reg_data['email']."\r\nPassword: ".$reg_data['password']."\r\n\r\n";
- $msg .= "You may login at ".base_url().'login'." anytime to update your profile and place new orders.\r\n\r\n";
- $msg .= "Once again, thank you for registering at ".SITE_NAME."! We look forward to serving your needs.\r\n\r\n -- ".SITE_NAME." Management\r\n";
- $this->_CI->email->message($msg);
- $this->_CI->email->send();
- $data = array();
- $data = array(
- 'user_id' => $this->_CI->db->insert_id(),
- 'name' => $reg_data['name'],
- 'phone' => $reg_data['phone'],
- 'cell' => $reg_data['cell'],
- 'receive_newsletters' => $reg_data['receive_newsletters'],
- 'company_name' => $reg_data['company_name'],
- 'company_id' => $reg_data['company_id'],
- 'company_comment' => $reg_data['company_comment'],
- 'b_address' => $reg_data['b_address'],
- 'b_address2' => $reg_data['b_address2'],
- 'b_postcode' => $reg_data['b_postcode'],
- 'b_postarea' => $reg_data['b_postarea']
- );
- $data['which_address'] = $reg_data['which_address'] == '1' ? $reg_data['which_address'] : '2';
- if($data['which_address'] == '1'){
- $data['s_address'] = $reg_data['b_address'];
- $data['s_address2'] = $reg_data['b_address2'];
- $data['s_postcode'] = $reg_data['b_postcode'];
- $data['s_postarea'] = $reg_data['b_postarea'];
- }
- return $this->_CI->db->insert('profiles', $data) ? TRUE : FALSE;
- }
- }
- return FALSE;
- }
- }
- /* End of Nm_auth.php */
Add Comment
Please, Sign In to add comment