PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/system/application/controllers/login.php

https://bitbucket.org/seezoo/seezoo/
PHP | 261 lines | 178 code | 29 blank | 54 comment | 12 complexity | 4bb1ce21923c559d59f6af262df81124 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * ===============================================================================
  4. *
  5. * Seezoo ログインコントローラ
  6. *
  7. * @package Seezoo Core
  8. * @author Yoshiaki Sugimoto <neo.yoshiaki.sugimoto@gmail.com>
  9. *
  10. * ===============================================================================
  11. */
  12. class Login extends SZ_Controller
  13. {
  14. public $ticket_name ='sz_login_token';
  15. public $ticket;
  16. public $dir = 'login/';
  17. public $msg = '';
  18. public static $page_title = 'ログイン';
  19. public static $description = 'ログイン画面を表示します。';
  20. /**
  21. * コンストラクタ
  22. */
  23. function __construct()
  24. {
  25. parent::SZ_Controller(FALSE);
  26. $this->load->library('form_validation');
  27. $this->load->helper(array('url', 'form', 'cookie_helper'));
  28. $this->load->model('auth_model');
  29. ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . APPPATH . 'libraries/');
  30. $this->output->set_header('Content-Type: text/html; charset=UTF-8');
  31. // no cahce
  32. $this->output->set_header('Cache-Control: no-store, no-cahce, must-revalidate, post-check=0, pre-check=0');
  33. $this->output->set_header('Pragma: no-cache');
  34. if ( defined('SEEZOO_SYSTEM_LOGIN_URI') )
  35. {
  36. if ( $this->uri->segment(1) !== SEEZOO_SYSTEM_LOGIN_URI )
  37. {
  38. show_404();
  39. }
  40. }
  41. //$this->output->enable_profiler(TRUE);
  42. }
  43. /**
  44. * デフォルトメソッド
  45. */
  46. function index()
  47. {
  48. $this->_check_remember();
  49. $this->_set_ticket();
  50. $this->_validation(FALSE);
  51. $this->load->view($this->dir . 'login');
  52. }
  53. /**
  54. * ログイン実行
  55. * @access public
  56. */
  57. function do_login()
  58. {
  59. $this->_ticket_check();
  60. $this->_validation(FALSE);
  61. $this->session->keep_flashdata($this->ticket_name);
  62. if ($this->form_validation->run() === FALSE)
  63. {
  64. $this->ticket = $this->input->post($this->ticket_name);
  65. $this->load->view($this->dir . 'login');
  66. }
  67. else
  68. {
  69. $this->ticket = $this->input->post($this->ticket_name);
  70. $this->_login_process();
  71. }
  72. }
  73. /**
  74. * パスワード再発行メソッド
  75. * @access public
  76. */
  77. function forgotten_password()
  78. {
  79. $this->_ticket_check();
  80. $this->_validation(TRUE);
  81. $this->session->keep_flashdata($this->ticket_name);
  82. $this->ticket = $this->input->post($this->ticket_name);
  83. if ($this->form_validation->run() === FALSE)
  84. {
  85. $this->load->view($this->dir . 'login', array('open' => TRUE));
  86. }
  87. else
  88. {
  89. if ($this->_send_forgotten_mail())
  90. {
  91. $this->msg = 'メールを送信しました。新しいパスワードを確認後、再度ログインして下さい。';
  92. $this->load->view($this->dir . 'login');
  93. }
  94. else
  95. {
  96. exit('メールの送信に失敗しました。');
  97. }
  98. }
  99. }
  100. /**
  101. * ログイン用ワンタイムトークン生成
  102. */
  103. function _set_ticket()
  104. {
  105. $ticket = md5(uniqid(mt_rand(), TRUE));
  106. $this->session->set_userdata($this->ticket_name, $ticket);
  107. $this->ticket = $ticket;
  108. }
  109. /**
  110. * ワンタイムトークンチェック
  111. */
  112. function _ticket_check()
  113. {
  114. $ticket = $this->input->post($this->ticket_name);
  115. if (!$ticket || $ticket !== $this->session->userdata($this->ticket_name))
  116. {
  117. exit('クッキーを許可してください。');
  118. }
  119. }
  120. /**
  121. * バリデーションセット
  122. * @access private
  123. * @param $is_forgotten
  124. */
  125. function _validation($is_forgotten = FALSE)
  126. {
  127. $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
  128. $this->form_validation->set_message('valid_email', 'メールアドレスの形式が正しくありません。');
  129. $conf = array(
  130. array(
  131. 'filed' => 'user_name',
  132. 'rules' => ($is_forgotten) ? '' : 'trim|required',
  133. 'label' => 'ユーザー名'
  134. ),
  135. array(
  136. 'field' => 'password',
  137. 'rules' => ($is_forgotten) ? '' : 'trim|required',
  138. 'label' => 'パスワード'
  139. ),
  140. array(
  141. 'field' => 'forgotten_mail',
  142. 'rules' => ($is_forgotten) ? 'trim|required|valid_email|max_length[255]|callback_already' : '',
  143. 'label' => 'メールアドレス'
  144. )
  145. );
  146. $this->form_validation->set_rules($conf);
  147. }
  148. /**
  149. * ログイン実行
  150. * @access private
  151. */
  152. function _login_process()
  153. {
  154. $uid = $this->input->post('user_name');
  155. $pass = $this->input->post('password');
  156. $res = $this->auth_model->login($uid, $pass, TRUE);
  157. if ($res)
  158. {
  159. // 継続的なログインの場はトークンを発行し、クッキーにセット
  160. if ((int)$this->input->post('remember') > 0)
  161. {
  162. $cookie = array(
  163. 'name' => 'seezoo_remembers',
  164. 'value' => sha1(microtime()),
  165. 'expire' => 60 * 60 * 24 * 7,
  166. 'domain' => '',
  167. 'path' => '/'
  168. );
  169. set_cookie($cookie);
  170. $this->auth_model->set_remember_token($cookie['value']);
  171. }
  172. redirect($res);
  173. }
  174. else
  175. {
  176. $this->msg = 'ログインに失敗しました。ユーザー名とパスワードを確認してください。';
  177. $this->load->view($this->dir . 'login');
  178. }
  179. }
  180. /**
  181. * 継続的なログインユーザーかどうかをチェック
  182. */
  183. function _check_remember()
  184. {
  185. $cookie = get_cookie('seezoo_remembers');
  186. if ($cookie)
  187. {
  188. $this->auth_model->remember_login($cookie);
  189. }
  190. }
  191. /**
  192. * 独自バリデーション:メールアドレスが存在するかどうかチェック
  193. * @param string $str
  194. */
  195. function already($str)
  196. {
  197. $is = $this->auth_model->is_email($str);
  198. if (!$is)
  199. {
  200. $this->form_validation->set_message('already', '入力されたメールアドレスは存在しません。');
  201. return FALSE;
  202. }
  203. return TRUE;
  204. }
  205. /**
  206. * パスワード再発行データをメール送信
  207. */
  208. function _send_forgotten_mail()
  209. {
  210. $to = set_value('forgotten_mail');
  211. $this->load->helper('string');
  212. $this->load->model(array('dashboard_model'));
  213. $new_password = random_string('alnum', 10);
  214. $pass = $this->dashboard_model->enc_password($new_password);
  215. $ret = $this->auth_model->update_new_password_for_email($to, $pass);
  216. if (!$ret)
  217. {
  218. return FALSE;
  219. }
  220. // load mail library
  221. require_once('qdmail.php');
  222. // set multibyte japanese
  223. mb_language('ja');
  224. $subject = '【' . SITE_TITLE . '】パスワード再発行のお知らせ';
  225. $from = $this->auth_model->get_master_email(); //sample mail
  226. $body = $this->load->view('mailbodys/forgotten_password', array('new_pass' => $new_password), TRUE);
  227. $ret = qd_send_mail('text', $to, $subject, $body, $from);
  228. $LOG =& load_class('Log');
  229. $LOG->write_mail_log($subject, $to, $body, $ret);
  230. return $ret;
  231. }
  232. }