PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/fuel/modules/fuel/controllers/Login.php

http://github.com/daylightstudio/FUEL-CMS
PHP | 373 lines | 294 code | 57 blank | 22 comment | 42 complexity | 1d4255c4ada3dd1fa372c7f9de4c9630 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. class Login extends CI_Controller {
  3. public function __construct()
  4. {
  5. parent::__construct();
  6. // for flash data
  7. $this->load->library('session');
  8. if (!$this->fuel->config('admin_enabled')) show_404();
  9. $this->load->vars(array(
  10. 'js' => '',
  11. 'css' => css($this->fuel->config('xtra_css')), // use CSS function here because of the asset library path changes below
  12. 'js_controller_params' => array(),
  13. 'keyboard_shortcuts' => $this->fuel->config('keyboard_shortcuts')));
  14. // change assets path to admin
  15. $this->asset->assets_path = $this->fuel->config('fuel_assets_path');
  16. // set asset output settings
  17. $this->asset->assets_output = $this->fuel->config('fuel_assets_output');
  18. $this->lang->load('fuel');
  19. $this->load->helper('ajax');
  20. $this->load->library('form_builder');
  21. $this->load->module_model(FUEL_FOLDER, 'fuel_users_model');
  22. // set configuration paths for assets in case they are different from front end
  23. $this->asset->assets_module ='fuel';
  24. $this->asset->assets_folders = array(
  25. 'images' => 'images/',
  26. 'css' => 'css/',
  27. 'js' => 'js/',
  28. );
  29. }
  30. public function index()
  31. {
  32. // check if it's a password request and redirect'
  33. if ($this->uri->segment(3) == 'pwd_reset')
  34. {
  35. $this->pwd_reset();
  36. return;
  37. }
  38. else if ($this->uri->segment(3) == 'dev')
  39. {
  40. $this->dev();
  41. return;
  42. }
  43. else if ($this->uri->segment(3) == 'reset')
  44. {
  45. $this->reset_password();
  46. return;
  47. }
  48. $this->js_controller_params['method'] = 'add_edit';
  49. $this->load->helper('convert');
  50. $this->load->helper('cookie');
  51. $session_key = $this->fuel->auth->get_session_namespace();
  52. $user_data = $this->session->userdata($session_key);
  53. if ( ! empty($_POST))
  54. {
  55. // check if they are locked out out or not
  56. if (isset($user_data['failed_login_timer']) AND (time() - $user_data['failed_login_timer']) < (int)$this->fuel->config('seconds_to_unlock'))
  57. {
  58. $this->fuel_users_model->add_error(lang('error_max_attempts', $this->fuel->config('seconds_to_unlock')));
  59. $user_data['failed_login_timer'] = time();
  60. }
  61. else
  62. {
  63. if ($this->input->post('user_name') AND $this->input->post('password'))
  64. {
  65. if ($this->fuel->auth->login($this->input->post('user_name', TRUE), $this->input->post('password', TRUE)))
  66. {
  67. // reset failed login attempts
  68. $user_data['failed_login_timer'] = 0;
  69. // set the cookie for viewing the live site with added FUEL capabilities
  70. $config = array(
  71. 'name' => $this->fuel->auth->get_fuel_trigger_cookie_name(),
  72. 'value' => serialize(array('id' => $this->fuel->auth->user_data('id'), 'language' => $this->fuel->auth->user_data('language'))),
  73. 'expire' => 0,
  74. //'path' => WEB_PATH
  75. 'path' => $this->fuel->config('fuel_cookie_path')
  76. );
  77. set_cookie($config);
  78. $forward = $this->input->post('forward');
  79. $forward_uri = uri_safe_decode($forward);
  80. # Check URL for naughty forwarding
  81. $parsed_url = parse_url($forward_uri);
  82. $host = array_key_exists('host', $parsed_url) ? $parsed_url['host'] : null;
  83. if ($forward AND ($forward_uri != $this->fuel->config('login_redirect')) AND ($host === null))
  84. {
  85. redirect($forward_uri);
  86. }
  87. else
  88. {
  89. redirect($this->fuel->config('login_redirect'));
  90. }
  91. }
  92. else
  93. {
  94. // check if they are no longer in the locked out state and reset variables
  95. if (isset($user_data['failed_login_timer']) AND (time() - $user_data['failed_login_timer']) > (int)$this->fuel->config('seconds_to_unlock'))
  96. {
  97. $user_data['failed_login_attempts'] = 0;
  98. $this->session->unset_userdata('failed_login_timer');
  99. unset($user_data['failed_login_timer']);
  100. }
  101. else
  102. {
  103. // add to the number of attempts if it's an invalid login'
  104. $num_attempts = (!isset($user_data['failed_login_attempts'])) ? 0 : $user_data['failed_login_attempts'] + 1;
  105. $user_data['failed_login_attempts'] = $num_attempts;
  106. }
  107. // check if they should be locked out
  108. if (isset($user_data['failed_login_attempts']) AND $user_data['failed_login_attempts'] >= (int)$this->fuel->config('num_logins_before_lock') -1)
  109. {
  110. $this->fuel_users_model->add_error(lang('error_max_attempts', $this->fuel->config('seconds_to_unlock')));
  111. $user_data['failed_login_timer'] = time();
  112. $this->fuel->logs->write(lang('auth_log_account_lockout', $this->input->post('user_name', TRUE), $this->input->ip_address()), 'debug');
  113. }
  114. else
  115. {
  116. $this->fuel_users_model->add_error(lang('error_invalid_login'));
  117. $this->fuel->logs->write(lang('auth_log_failed_login', $this->input->post('user_name', TRUE), $this->input->ip_address(), ($user_data['failed_login_attempts'] + 1)), 'debug');
  118. }
  119. }
  120. }
  121. else
  122. {
  123. $this->fuel_users_model->add_error(lang('error_empty_user_pwd'));
  124. }
  125. }
  126. $this->session->set_userdata($session_key, $user_data);
  127. }
  128. // build form
  129. $this->form_builder->set_validator($this->fuel_users_model->get_validation());
  130. $fields['user_name'] = array('size' => 25, 'placeholder' => 'username', 'display_label' => FALSE);
  131. $fields['password'] = array('type' => 'password', 'size' => 25, 'placeholder' => 'password', 'display_label' => FALSE);
  132. $fields['forward'] = array('type' => 'hidden', 'value' => fuel_uri_segment(2));
  133. $this->form_builder->show_required = FALSE;
  134. $this->form_builder->submit_value = lang('login_btn');
  135. $this->form_builder->set_fields($fields);
  136. $this->form_builder->remove_js();
  137. if (!empty($_POST)) $this->form_builder->set_field_values($this->input->post(NULL, TRUE));
  138. $vars['form'] = $this->form_builder->render();
  139. // set any errors that
  140. if ($this->session->flashdata('error'))
  141. {
  142. $errors = array($this->session->flashdata('error'));
  143. }
  144. else
  145. {
  146. $errors = $this->fuel_users_model->get_errors();
  147. }
  148. $vars['error'] = $errors;
  149. // notifications template
  150. $notifications = $this->load->view('_blocks/notifications', $vars, TRUE);
  151. $vars['notifications'] = $notifications;
  152. $vars['display_forgotten_pwd'] = $this->fuel->config('allow_forgotten_password');
  153. $vars['page_title'] = lang('fuel_page_title');
  154. $this->load->module_view(FUEL_FOLDER, 'login', $vars);
  155. }
  156. // THIS IS A PASSWORD RESET TOKEN CREATION EMAIL SENDING
  157. public function pwd_reset()
  158. {
  159. if ( ! $this->fuel->config('allow_forgotten_password')) show_404();
  160. $this->js_controller_params['method'] = 'add_edit';
  161. if ( ! empty($_POST))
  162. {
  163. if ($this->input->post('email'))
  164. {
  165. $user = $this->fuel_users_model->find_one_array(array('email' => $this->input->post('email')));
  166. if ( ! empty($user['email']))
  167. {
  168. // This generates and saves a token to the user model, returns the token string.
  169. $token = $this->fuel_users_model->get_reset_password_token($user['email']);
  170. if ($token !== FALSE)
  171. {
  172. $url = 'login/reset/' . $token;
  173. $msg = lang('pwd_reset_email', fuel_url($url));
  174. $params['to'] = $this->input->post('email');
  175. $params['subject'] = lang('pwd_reset_subject');
  176. $params['message'] = $msg;
  177. $params['use_dev_mode'] = FALSE;
  178. if ($this->fuel->notification->send($params))
  179. {
  180. $this->session->set_flashdata('success', lang('pwd_reset_email_success'));
  181. $this->fuel->logs->write(lang('auth_log_pass_reset_request', $user['email'], $this->input->ip_address()), 'debug');
  182. }
  183. else
  184. {
  185. $this->session->set_flashdata('error', lang('error_pwd_reset'));
  186. $this->fuel->logs->write($this->fuel->notification->last_error(), 'debug');
  187. }
  188. redirect(fuel_uri('login'));
  189. }
  190. else
  191. {
  192. $this->fuel_users_model->add_error(lang('error_invalid_email'));
  193. }
  194. }
  195. else
  196. {
  197. $this->fuel_users_model->add_error(lang('error_invalid_email'));
  198. }
  199. }
  200. else
  201. {
  202. $this->fuel_users_model->add_error(lang('error_empty_email'));
  203. }
  204. }
  205. $this->form_builder->set_validator($this->fuel_users_model->get_validation());
  206. // build form
  207. $fields['Reset Password'] = array('type' => 'section', 'label' => lang('login_reset_pwd'));
  208. $fields['email'] = array('required' => TRUE, 'size' => 30, 'placeholder' => 'email', 'display_label' => FALSE);
  209. $this->form_builder->show_required = FALSE;
  210. $this->form_builder->set_fields($fields);
  211. $vars['form'] = $this->form_builder->render();
  212. // notifications template
  213. $vars['error'] = $this->fuel_users_model->get_errors();
  214. $vars['notifications'] = $this->load->module_view(FUEL_FOLDER, '_blocks/notifications', $vars, TRUE);
  215. $vars['page_title'] = lang('fuel_page_title');
  216. $this->load->module_view(FUEL_FOLDER, 'pwd_reset', $vars);
  217. }
  218. // THIS HANDLES A POST REQUEST FOR USER SETTING A NEW PASSWORD
  219. public function reset_password()
  220. {
  221. $token = $this->uri->segment(4);
  222. if (empty($token))
  223. {
  224. $this->session->set_flashdata('error', lang('pwd_reset_missing_token'));
  225. redirect(site_url('fuel/login'));
  226. }
  227. else
  228. {
  229. if( ! $this->fuel_users_model->validate_reset_token($token))
  230. {
  231. $this->session->set_flashdata('error', lang('pwd_reset_missing_token'));
  232. redirect(site_url('fuel/login'));
  233. }
  234. }
  235. if ( ! empty($_POST))
  236. {
  237. if ($this->input->post('email') && $this->input->post('password') && $this->input->post('password_confirm') && $this->input->post('_token'))
  238. {
  239. $this->load->library('user_agent');
  240. if ($this->input->post('password') == $this->input->post('password_confirm'))
  241. {
  242. $reset = $this->fuel_users_model->reset_password_from_token($this->input->post('email'), $this->input->post('_token'), $this->input->post('password'));
  243. if ($reset)
  244. {
  245. $this->session->set_flashdata('success', lang('pwd_reset_success'));
  246. redirect(site_url('fuel/login'));
  247. }
  248. else
  249. {
  250. if ($this->fuel_users_model->has_error())
  251. {
  252. $errors = $this->fuel_users_model->get_errors();
  253. $this->session->set_flashdata('error',$errors[0]);
  254. redirect($this->agent->referrer());
  255. }
  256. $this->session->set_flashdata('error', lang('pwd_reset_error'));
  257. redirect(site_url('fuel/login/pwd_reset'));
  258. }
  259. }
  260. else
  261. {
  262. $this->session->set_flashdata('error', lang('pwd_reset_error_not_match'));
  263. redirect($this->agent->referrer());
  264. }
  265. }
  266. }
  267. $fields['Reset Password'] = array('type' => 'section', 'label' => lang('login_reset_pwd'));
  268. $fields['Directions'] = array('type' => 'copy', 'label' => $this->fuel->users->get_password_strength_text());
  269. $fields['email'] = array('required' => TRUE, 'size' => 30, 'placeholder' => 'email', 'display_label' => FALSE);
  270. $fields['password'] = array('type' => 'password', 'required' => TRUE, 'size' => 30, 'placeholder' => 'password', 'display_label' => FALSE);
  271. $fields['password_confirm'] = array('type' => 'password', 'required' => TRUE, 'size' => 30, 'placeholder' => 'confirm password', 'display_label' => FALSE);
  272. $fields['_token'] = array('type' => 'hidden', 'value' => $token);
  273. $this->form_builder->show_required = FALSE;
  274. $this->form_builder->set_fields($fields);
  275. $vars['form'] = $this->form_builder->render();
  276. // notifications template
  277. $vars['error'] = $this->fuel_users_model->get_errors();
  278. $vars['notifications'] = $this->load->module_view(FUEL_FOLDER, '_blocks/notifications', $vars, TRUE);
  279. $vars['page_title'] = lang('fuel_page_title');
  280. $this->load->module_view(FUEL_FOLDER, 'reset', $vars);
  281. }
  282. public function dev()
  283. {
  284. $this->config->set_item('allow_forgotten_password', FALSE);
  285. if ( ! empty($_POST))
  286. {
  287. if ( ! $this->fuel->config('dev_password'))
  288. {
  289. redirect('');
  290. }
  291. else if ($this->fuel->config('dev_password') == $this->input->post('password', TRUE))
  292. {
  293. $this->load->helper('convert');
  294. $this->session->set_userdata('dev_password', TRUE);
  295. $forward = uri_safe_decode($this->input->post('forward'));
  296. redirect($forward);
  297. }
  298. else
  299. {
  300. add_error(lang('error_invalid_login'));
  301. }
  302. }
  303. $fields['password'] = array('type' => 'password', 'placeholder' => 'password', 'display_label' => FALSE, 'size' => 25);
  304. $fields['forward'] = array('type' => 'hidden', 'value' => fuel_uri_segment(2));
  305. $this->form_builder->show_required = FALSE;
  306. $this->form_builder->submit_value = 'Login';
  307. $this->form_builder->set_fields($fields);
  308. if ( ! empty($_POST)) $this->form_builder->set_field_values($this->input->post(NULL, TRUE));
  309. $vars['form'] = $this->form_builder->render();
  310. $vars['notifications'] = $this->load->module_view(FUEL_FOLDER, '_blocks/notifications', $vars, TRUE);
  311. $vars['display_forgotten_pwd'] = FALSE;
  312. $vars['instructions'] = lang('dev_pwd_instructions');
  313. $vars['page_title'] = lang('fuel_page_title');
  314. $this->load->module_view(FUEL_FOLDER, 'login', $vars);
  315. }
  316. }