Guest User

Untitled

a guest
Jul 15th, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Nm_auth
  3. {
  4. var $email;
  5. var $password;
  6. var $message;
  7. private $_logged_in = FALSE;
  8.  
  9. function __construct()
  10. {
  11. $this->_CI =& get_instance();
  12. $this->_CI->load->library('session');
  13. $this->_CI->load->model('auth');
  14. $this->_CI->config->load('NM_auth');
  15. log_message('debug', "NMauth Class Initialized");
  16.  
  17. if($this->_CI->session->userdata('email')){
  18. $this->_logged_in = TRUE;
  19. }
  20. }
  21.  
  22. function check_email($email)
  23. {
  24. $query = $this->_CI->db->get_where("users", array ('LOWER(email)'=>strtolower($email)));
  25. if($query->num_rows() == 1){
  26. return TRUE;
  27. }
  28. return FALSE;
  29. }
  30.  
  31. function check_user($email, $password)
  32. {
  33. $query = $this->_CI->db->get_where("users", array ('LOWER(email)'=>strtolower($email)));
  34. if($query->num_rows() == 1){
  35. if(md5($password) == $query->row()->password) {
  36. return TRUE;
  37. } else {
  38. return FALSE;
  39. }
  40. }
  41. }
  42.  
  43. function is_logged_in()
  44. {
  45. return $this->_logged_in ? TRUE : FALSE;
  46. }
  47.  
  48. function redirect_logged_in()
  49. {
  50. if ($this->is_logged_in() != FALSE)
  51. {
  52. $this->_CI->session->set_flashdata('error', 'You are already logged in.');
  53. redirect((($this->_CI->session->userdata('last-page') == '/login') || ($this->_CI->session->userdata('last-page') == '/register')) ? '' : $this->_CI->session->userdata('last-page'));
  54. }
  55. }
  56.  
  57. function redirect_back($message)
  58. {
  59. if ($this->is_logged_in() != FALSE)
  60. {
  61. $this->_CI->session->set_flashdata('info', $message);
  62. redirect(($this->_CI->session->userdata('last-page') != 'login') && ($this->_CI->session->userdata('last-page') != 'register') ? $this->_CI->session->userdata('last-page') : '');
  63. }
  64. }
  65.  
  66. function get_user_data($email)
  67. {
  68. $this->_CI->db->select('users.*, profiles.*');
  69. $this->_CI->db->join('profiles', 'profiles.user_id = users.id', 'left');
  70. $this->_CI->db->group_by('users.id');
  71. $this->_CI->db->where('LOWER(users.email)', strtolower($email));
  72. $query = $this->_CI->db->get('users');
  73. return $query->row();
  74. }
  75.  
  76. function login($email, $password)
  77. {
  78. if($this->check_user($email, $password))
  79. {
  80. $user = $this->get_user_data($email);
  81. if($user->is_active == '1')
  82. {
  83. $session_data = array (
  84. 'email'=>$user->email,
  85. 'user_id'=>$user->user_id
  86. );
  87. if($user->level == '1') {
  88. $session_data['admin'] = 'TRUE';
  89. }
  90. if($user->level == '2') {
  91. $session_data['cust'] = 'TRUE';
  92. }
  93. $this->_CI->session->set_userdata($session_data);
  94. $this->_CI->db->where('id', $user->user_id);
  95. $this->_CI->db->set('last_active', date('Y-m-d H:i:s'));
  96. if($this->_CI->db->update('users')){
  97. $this->_logged_in = TRUE;
  98. return TRUE;
  99. }
  100. }
  101. } else {
  102. $this->_logged_in = FALSE;
  103. return FALSE;
  104. }
  105. }
  106.  
  107. function logout($email)
  108. {
  109. $user = $this->get_user_data($email);
  110. $data['last_active'] = date("Y-m-d H:i:s");
  111. $this->_CI->db->where('id', $this->_CI->session->userdata('user_id'));
  112. if($this->_CI->db->update('users', $data)){
  113. $unset_params = array('regenerated'=>'', 'last-page'=>'logout', 'email'=>'', 'user_id'=>'', 'admin'=>'FALSE', 'cust'=>'FALSE');
  114. $this->_CI->session->unset_userdata($unset_params);
  115. $this->_logged_in = FALSE;
  116. return TRUE;
  117. }
  118. return FALSE;
  119. }
  120.  
  121. function register($reg_data){
  122. if(is_array($reg_data)){
  123. $data = array(
  124. 'email' => $reg_data['email'],
  125. 'password' => md5($reg_data['password']),
  126. 'level' => '2',
  127. 'is_active' => '1'
  128. );
  129.  
  130. if($this->_CI->db->insert('users', $data)){
  131. $this->_CI->email->from(EMAIL_FROM, SITE_NAME.' Management');
  132. $this->_CI->email->to($reg_data['email']);
  133.  
  134. $this->_CI->email->subject('Your New Account Registration Confirmation');
  135.  
  136. $msg = "Thanks for registering at ".SITE_NAME."! Your account details are below.\r\n\r\n";
  137. $msg .= "Email Address: ".$reg_data['email']."\r\nPassword: ".$reg_data['password']."\r\n\r\n";
  138. $msg .= "You may login at ".base_url().'login'." anytime to update your profile and place new orders.\r\n\r\n";
  139. $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";
  140.  
  141. $this->_CI->email->message($msg);
  142.  
  143. $this->_CI->email->send();
  144.  
  145. $data = array();
  146. $data = array(
  147. 'user_id' => $this->_CI->db->insert_id(),
  148. 'name' => $reg_data['name'],
  149. 'phone' => $reg_data['phone'],
  150. 'cell' => $reg_data['cell'],
  151. 'receive_newsletters' => $reg_data['receive_newsletters'],
  152. 'company_name' => $reg_data['company_name'],
  153. 'company_id' => $reg_data['company_id'],
  154. 'company_comment' => $reg_data['company_comment'],
  155. 'b_address' => $reg_data['b_address'],
  156. 'b_address2' => $reg_data['b_address2'],
  157. 'b_postcode' => $reg_data['b_postcode'],
  158. 'b_postarea' => $reg_data['b_postarea']
  159. );
  160.  
  161. $data['which_address'] = $reg_data['which_address'] == '1' ? $reg_data['which_address'] : '2';
  162.  
  163. if($data['which_address'] == '1'){
  164. $data['s_address'] = $reg_data['b_address'];
  165. $data['s_address2'] = $reg_data['b_address2'];
  166. $data['s_postcode'] = $reg_data['b_postcode'];
  167. $data['s_postarea'] = $reg_data['b_postarea'];
  168. }
  169.  
  170. return $this->_CI->db->insert('profiles', $data) ? TRUE : FALSE;
  171. }
  172. }
  173. return FALSE;
  174. }
  175.  
  176. }
  177. /* End of Nm_auth.php */
Add Comment
Please, Sign In to add comment