Guest User

Untitled

a guest
Jul 29th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. <?php
  2.  
  3. class Auth
  4. {
  5. var $_obj;
  6. var $_user;
  7.  
  8. function Auth()
  9. {
  10. $this->_obj =& get_instance();
  11. $this->_obj->load->library('session');
  12. $this->_get_session();
  13.  
  14. $this->_obj->load->helper('cookie');
  15. }
  16.  
  17. ##############
  18. ##Шифрование##
  19. ##############
  20.  
  21. function _hash($pass, $email, $salt = NULL) //функция шифрования пароля.
  22. {
  23. $string = $pass.$email;
  24. //'password'
  25.  
  26. $encryption_key = $this->_obj->config->item('encryption_key');
  27.  
  28. $string = $string.$encryption_key ;
  29.  
  30. if ($salt === NULL)
  31. {
  32. $salt = sha1(uniqid(rand(), true));
  33. }
  34.  
  35. $string = $string.$salt;
  36.  
  37. $hashed = sha1($string);
  38.  
  39. return array(
  40. 'hashed' => $hashed,
  41. 'salt' => $salt
  42. );
  43. }
  44.  
  45.  
  46. #############
  47. ###SESSION###
  48. #############
  49.  
  50. function _set_session($user)
  51. {
  52. $data['id'] = $user['id'];
  53. $data['email'] = $user['email'];
  54. $data['first_name'] = $user['first_name'];
  55. $data['last_name'] = $user['last_name'];
  56. $data['access'] = $user['access'];
  57. $data['active'] = $user['active'];
  58. $data['logged_in'] = 1;
  59.  
  60. $this->_obj->session->set_userdata('auth', $data);
  61.  
  62. $this->_get_session();
  63. }
  64.  
  65. function set_access()
  66. {
  67. return $this->_user['access'];
  68. }
  69.  
  70.  
  71. function _get_session() // получение сессии
  72. {
  73. $user = $this->_obj->session->userdata('auth');
  74.  
  75. if ($user === FALSE || (isset($user['logged_in']) && $user['logged_in'] !== '1'))
  76. {
  77. $user = array(
  78. 'id' => 0,
  79. 'logged_in' => FALSE
  80. );
  81.  
  82. $this->_user = $user;
  83.  
  84. return FALSE;
  85. }
  86. else
  87. {
  88. $this->_user = $user;
  89.  
  90. return TRUE;
  91. }
  92. }
  93.  
  94. function _clear_session()
  95. {
  96. $this->_obj->session->unset_userdata('auth');
  97.  
  98. $this->_get_session();
  99. }
  100.  
  101.  
  102. function logged_in()
  103. {
  104. return $this->_user['logged_in'];
  105. }
  106.  
  107. function get_user()
  108. {
  109. return $this->_user;
  110. }
  111.  
  112. function login($email, $pass) //функция авторизации
  113. {
  114. $this->_obj->db->where('email', $email);
  115.  
  116. $query = $this->_obj->db->get('log', 1);
  117.  
  118. if ($query->num_rows() == 0)
  119. {
  120. return FALSE;
  121. }
  122.  
  123. $user = $query->row_array();
  124.  
  125. $hashed = $this->_hash($pass, $email, $user['salt']);
  126.  
  127. if ($user['pass'] === $hashed['hashed'])
  128. {
  129. $this->_set_session($user);
  130. //set_cookie($cookie);
  131. return TRUE;
  132.  
  133. }
  134. return FALSE;
  135. }
  136.  
  137. function signup($user = NULL) //регистрация
  138. {
  139. if (empty($user)) return false;
  140.  
  141. // array(
  142. // 'email_reg'
  143. // 'pass_reg'
  144. // 'first_name'
  145. // 'last_name'
  146. // );
  147.  
  148. $encrypted = $this->_hash($user['pass'], $user['email']);
  149.  
  150. $clean_pass = $user['pass'];
  151.  
  152. $user['pass'] = $encrypted['hashed'];
  153. $user['salt'] = $encrypted['salt'];
  154.  
  155. $this->_obj->db->insert('log', $user);
  156.  
  157. $this->_obj->db->set('email', $user['email']);
  158. $this->_obj->db->insert('ocenka');
  159.  
  160. $this->login($user['email'], $clean_pass);
  161.  
  162. return $user;
  163.  
  164. }
  165.  
  166. function logout() //разрыв сессии
  167. {
  168. $this->_clear_session();
  169. }
  170.  
  171. function reset_password($email) //восстановление пароля, отправка ссылки на почту для восстановление пароля.
  172. {
  173. $this->_obj->db->where('email', $email);
  174. $q = $this->_obj->db->get('log', 1);
  175.  
  176. if ($q->num_rows() == 0)
  177. {
  178. return FALSE;
  179. }
  180.  
  181. $user = $q->row_array();
  182.  
  183. $hash = substr(sha1($email.rand()), 0, 10);
  184.  
  185. $data = array(
  186. 'reset_password_hash' => $hash
  187. );
  188. $this->_obj->db->where('id', $user['id']);
  189. $q = $this->_obj->db->update('log', $data);
  190.  
  191. //$config = array(
  192. // 'protocol' => 'smtp',
  193. // 'smtp_host' => 'smtp.gmail.com',
  194. // 'smtp_user' => 'Runisov@gmail.com',
  195. // 'smtp_pass' => 'lop89Fn2',
  196. // 'smtp_port' => 576,
  197. // 'smtp_timeout' => 5
  198. // );
  199. //
  200. //$this->_obj->email->initialize($config);
  201.  
  202. $this->_obj->load->library('email');
  203. $this->_obj->email->to($user['email']);
  204. $this->_obj->email->from('no-reply@localhost.ru', 'root');
  205. $this->_obj->email->subject('Восстановление пароля');
  206.  
  207. $reset_url = site_url('account/reset/'.$hash);
  208.  
  209. $message = "hi {$user['first_name']} {$user['last_name']}, \n\n {$reset_url}";
  210.  
  211. $this->_obj->email->message($message);
  212. $this->_obj->email->send();
  213.  
  214. return TRUE;
  215. }
  216.  
  217. function reset_pass($user_id, $pass) // после того когда пользователь перешел по ссылкке для восстановление пароля, и ввел новый, запускается эта функция, для изменения в БД
  218. {
  219. $this->_obj->db->where('id', $user_id);
  220. $q = $this->_obj->db->get('log', 1);
  221.  
  222. if ($q->num_rows() == 0)
  223. {
  224. return FALSE;
  225. }
  226. $user = $q->row_array();
  227.  
  228. $new_pass = $this->_hash($pass, $user['email']);
  229.  
  230. $data = array(
  231. 'pass' => $new_pass['hashed'],
  232. 'salt' => $new_pass['salt']
  233. );
  234.  
  235. $this->_obj->db->where('id', $user['id']);
  236. $this->_obj->db->update('log', $data);
  237.  
  238. return TRUE;
  239.  
  240. //die('New password is '.var_dump($new_pass));
  241. }
  242.  
  243.  
  244. ############
  245. ####AJAX####
  246. ############
  247.  
  248. function updb($foo) //запись в БД после изменения права доступа пользователя.
  249. {
  250. $ac = array(
  251. 'user' => '0',
  252. 'admin' => '1',
  253. 'moder' => '2'
  254. );
  255. $data = array('access' => $ac[$foo['access']]);
  256. $this->_obj->db->where('id', $foo['id']);
  257. $this->_obj->db->update('log', $data);
  258. }
  259.  
  260. function upstat($st) //запись в БД после изменения статуса пользователя.
  261. {
  262. $arstat = array(
  263. 'block' => '0',
  264. 'unlock' => '1'
  265. );
  266. $data = array('active' => $arstat[$st['status']]);
  267. $this->_obj->db->where('id', $st['id']);
  268. $this->_obj->db->update('log', $data);
  269.  
  270. }
  271.  
  272. }
Add Comment
Please, Sign In to add comment