PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/system/application/models/auth_model.php

https://bitbucket.org/seezoo/seezoo/
PHP | 332 lines | 274 code | 36 blank | 22 comment | 33 complexity | dddafa187f00b000cb635d512a1fb5d3 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * =========================================================
  4. * ログイン用モデルクラス
  5. *
  6. * @package Seezoo Core
  7. * @author Yoshiaki Sugimoto <neo.yoshiaki.sugimoto@gmail.com>
  8. * =========================================================
  9. */
  10. class Auth_model extends Model
  11. {
  12. function __construct()
  13. {
  14. parent::Model();
  15. }
  16. function login($uname, $pass, $is_admin_login = FALSE)
  17. {
  18. $sql =
  19. 'SELECT '
  20. . 'user_id, '
  21. . 'hash, '
  22. . 'password, '
  23. . 'login_times, '
  24. . 'admin_flag, '
  25. . 'login_miss_count '
  26. .'FROM '
  27. . 'users '
  28. .'WHERE '
  29. . 'user_name = ? '
  30. .'AND '
  31. . 'login_miss_count < 3 ';
  32. if ($is_admin_login)
  33. {
  34. $sql .= 'AND is_admin_user = 1 ';
  35. }
  36. $sql .= 'LIMIT 1';
  37. $query = $this->db->query($sql, array($uname));
  38. if ($query->row())
  39. {
  40. $result = $query->row();
  41. // crypted password is match?
  42. //$password = md5($result->hash . $pass);
  43. // detect password stretching algorithm
  44. $algorithm_case = substr($result->password, 0, 3);
  45. if ( $algorithm_case === '$1$' )
  46. {
  47. // case md5 stretching
  48. $algorithm = 'md5';
  49. $password = substr($result->password, 3);
  50. }
  51. else if ( $algorithm_case === '$2$' )
  52. {
  53. // case sha1 stretching
  54. $algorithm = 'sha1';
  55. $password = substr($result->password, 3);
  56. }
  57. else if ( $algorithm_case === '$3$' )
  58. {
  59. // case sha256 stretching
  60. $algorithm = 'sha256';
  61. $password = substr($result->password, 3);
  62. }
  63. else
  64. {
  65. // not stretching
  66. $algorithm = FALSE;
  67. $password = $result->password;
  68. }
  69. $match_password = password_stretch($result->hash, $pass, $algorithm);
  70. if ($match_password === $password)
  71. {
  72. // update login user data
  73. $data = array(
  74. 'last_login' => date('Y-m-d H:i:s', time()),
  75. 'login_times' => (int)$result->login_times + 1,
  76. 'login_miss_count' => 0
  77. );
  78. $this->db->where('user_id', $result->user_id);
  79. $this->db->update('users', $data);
  80. // set login session
  81. $this->session->set_userdata('user_id', $result->user_id);
  82. // protect code
  83. if ($this->session->userdata('edit_version'))
  84. {
  85. $this->session->unset_userdata('edit_version');
  86. }
  87. // 前回までのログインデータが残っていると不整合が起きるので全削除
  88. $this->_delete_all_edit_state($result->user_id);
  89. if ($result->admin_flag == 1)
  90. {
  91. $return_path = 'dashboard/panel';
  92. }
  93. else
  94. {
  95. $return_path = '/';
  96. }
  97. return $return_path;
  98. }
  99. else
  100. {
  101. if ($result->login_miss_count < 4 && $result->user_id > 1)
  102. {
  103. $update = array(
  104. 'login_miss_count' => (int)$result->login_miss_count + 1
  105. );
  106. $this->db->where('user_id', $result->user_id);
  107. $this->db->update('users', $update);
  108. }
  109. }
  110. }
  111. return FALSE;
  112. }
  113. function logout()
  114. {
  115. if ( $this->session->userdata('user_id') )
  116. {
  117. $sql = 'UPDATE '
  118. . 'pages '
  119. . 'SET '
  120. . 'is_editting = ?, '
  121. . 'edit_user_id = ? '
  122. . 'WHERE '
  123. . 'edit_user_id = ?';
  124. $query = $this->db->query($sql, array(0, 0, $this->session->userdata('user_id')));
  125. $this->session->unset_userdata('user_id');
  126. }
  127. }
  128. function member_logout()
  129. {
  130. if ( $this->session->userdata('member_id') )
  131. {
  132. $this->session->unset_userdata('member_id');
  133. }
  134. }
  135. function relogin_with_other_user($uid)
  136. {
  137. if ((int)$this->session->userdata('user_id') !== 1)
  138. {
  139. return FALSE;
  140. }
  141. $this->session->set_userdata('user_id', $uid);
  142. return TRUE;
  143. }
  144. function _delete_all_edit_state($uid)
  145. {
  146. $data = array(
  147. 'is_editting' => 0,
  148. 'edit_user_id' => 0,
  149. 'is_arranging' => 0,
  150. 'edit_start_time' => '0000-00-00 00:00:00'
  151. );
  152. $this->db->where('edit_user_id', $uid);
  153. $this->db->update('pages', $data);
  154. if ($this->session->userdata('edit_version'))
  155. {
  156. $this->session->unset_userdata('edit_version');
  157. }
  158. if ($this->session->userdata('is_arrange'))
  159. {
  160. $this->session->unet_userdata('is_arrange');
  161. }
  162. }
  163. function set_remember_token($val)
  164. {
  165. $this->db->where('user_id', $this->session->userdata('user_id'));
  166. $this->db->update('users', array('remember_token' => $val));
  167. }
  168. function remember_login($val)
  169. {
  170. $sql = 'SELECT '
  171. . 'user_id, '
  172. . 'admin_flag '
  173. . 'FROM '
  174. . 'users '
  175. . 'WHERE '
  176. . 'remember_token = ? '
  177. . 'LIMIT 1'
  178. ;
  179. $query = $this->db->query($sql, array($val));
  180. if ($query->row())
  181. {
  182. $result = $query->row();
  183. $this->session->set_userdata('user_id', $result->user_id);
  184. if ($this->session->userdata('edit_version'))
  185. {
  186. $this->session->unset_userdata('edit_version');
  187. }
  188. // 前回までのログインデータが残っていると不整合が起きるので全削除
  189. $this->_delete_all_edit_state($result->user_id);
  190. if ($result->admin_flag > 0)
  191. {
  192. redirect('dashboard/panel');
  193. }
  194. else
  195. {
  196. redirect('/');
  197. }
  198. }
  199. return FALSE;
  200. }
  201. function is_email($mail)
  202. {
  203. $sql = 'SELECT user_id FROM users WHERE email = ? LIMIT 1';
  204. $query = $this->db->query($sql, array($mail));
  205. if ($query->row())
  206. {
  207. return TRUE;
  208. }
  209. else
  210. {
  211. return FALSE;
  212. }
  213. }
  214. function update_new_password_for_email($mail, $data)
  215. {
  216. $this->db->where('email', $mail);
  217. return $this->db->update('users', $data);
  218. }
  219. function get_master_email()
  220. {
  221. $CI =& get_instance();
  222. if ( isset($CI->site_data) && !empty($CI->site_data->system_mail_from) )
  223. {
  224. return $CI->site_data->system_mail_from;
  225. }
  226. $sql =
  227. 'SELECT '
  228. . 'email '
  229. .'FROM '
  230. . 'users '
  231. .'WHERE '
  232. . 'user_id = 1 '
  233. .'LIMIT 1';
  234. $query = $this->db->query($sql);
  235. if ($query && $query->row())
  236. {
  237. $result = $query->row();
  238. return $result->email;
  239. }
  240. return 'info@example.com';
  241. }
  242. function generate_member_activation_code($member_id, $email)
  243. {
  244. $salt = sha1(uniqid(mt_rand(), TRUE));
  245. $code = sha1($member_id . $email . $salt);
  246. $insert = array(
  247. 'activation_code' => $code,
  248. 'sz_member_id' => $member_id,
  249. 'email' => $email,
  250. 'activation_limit_time' => date('Y-m-d H:i:s', strtotime('+1 day'))
  251. );
  252. if ( $this->db->insert('sz_activation_data', $insert) )
  253. {
  254. return $code;
  255. }
  256. return FALSE;
  257. }
  258. function do_member_activation($code)
  259. {
  260. $sql =
  261. 'SELECT '
  262. . 'sz_member_id, '
  263. . 'email, '
  264. . 'activation_limit_time '
  265. .'FROM '
  266. . 'sz_activation_data '
  267. .'WHERE '
  268. . 'activation_code = ? '
  269. .'LIMIT 1'
  270. ;
  271. $query = $this->db->query($sql, array($code));
  272. if ( $query->num_rows() == 0 )
  273. {
  274. return FALSE;
  275. }
  276. $result = $query->row();
  277. if ( strtotime($result->activation_limit_time) < time() )
  278. {
  279. $ret = 'timeout';
  280. }
  281. else
  282. {
  283. $ret = TRUE;
  284. }
  285. // update email
  286. $this->db->where('sz_member_id', $result->sz_member_id);
  287. if ( ! $this->db->update('sz_members', array('email' => $result->email)) )
  288. {
  289. return FALSE;
  290. }
  291. // delete record
  292. $this->db->where('activation_code', $code);
  293. $this->db->delete('sz_activation_data');
  294. return $ret;
  295. }
  296. }