Advertisement
Guest User

Untitled

a guest
Jan 28th, 2016
203
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.  
  3. /**
  4. * User Controller
  5. *
  6. * @package Controllers
  7. */
  8.  
  9. $allowed_functions = array('ajax_check_login');
  10. $ru = $_SERVER['REQUEST_URI']
  11. $func = preg_replace('/.*\//', '', $ru);
  12. if (isset($func) && in_array($func, $allowed_functions)) {
  13. $user = new User();
  14. $user->$func();
  15. }
  16.  
  17. class User extends CI_Controller {
  18. /**
  19. * Class Constructor
  20. */
  21. public function __construct() {
  22. parent::__construct();
  23. $this->load->library('session');
  24. // Set user's selected language.
  25. if ($this->session->userdata('language')) {
  26. $this->config->set_item('language', $this->session->userdata('language'));
  27. $this->lang->load('translations', $this->session->userdata('language'));
  28. } else {
  29. $this->lang->load('translations', $this->config->item('language')); // default
  30. }
  31. }
  32. /**
  33. * Default Method
  34. *
  35. * The default method will redirect the browser to the user/login URL.
  36. */
  37. public function index() {
  38. header('Location: ' . $this->config->item('base_url') . '/index.php/user/login');
  39. }
  40. /**
  41. * Display the login page.
  42. */
  43. public function login() {
  44. $this->load->model('settings_model');
  45. $view['base_url'] = $this->config->item('base_url');
  46. $view['dest_url'] = $this->session->userdata('dest_url');
  47. if (!$view['dest_url']) {
  48. $view['dest_url'] = $view['base_url'] . '/index.php/backend';
  49. }
  50. $view['company_name'] = $this->settings_model->get_setting('company_name');
  51. $this->load->view('user/login', $view);
  52. }
  53. /**
  54. * Display the logout page.
  55. */
  56. public function logout() {
  57. $this->load->model('settings_model');
  58. $this->session->unset_userdata('user_id');
  59. $this->session->unset_userdata('user_email');
  60. $this->session->unset_userdata('role_slug');
  61. $this->session->unset_userdata('username');
  62. $this->session->unset_userdata('dest_url');
  63. $view['base_url'] = $this->config->item('base_url');
  64. $view['company_name'] = $this->settings_model->get_setting('company_name');
  65. $this->load->view('user/logout', $view);
  66. }
  67. /**
  68. * Display the forgot password page.
  69. */
  70. public function forgot_password() {
  71. $this->load->model('settings_model');
  72. $view['base_url'] = $this->config->item('base_url');
  73. $view['company_name'] = $this->settings_model->get_setting('company_name');
  74. $this->load->view('user/forgot_password', $view);
  75. }
  76. public function no_privileges() {
  77. $this->load->model('settings_model');
  78. $view['base_url'] = $this->config->item('base_url');
  79. $view['company_name'] = $this->settings_model->get_setting('company_name');
  80. $this->load->view('user/no_privileges', $view);
  81. }
  82. /**
  83. * [AJAX] Check whether the user has entered the correct login credentials.
  84. *
  85. * The session data of a logged in user are the following:
  86. * - 'user_id'
  87. * - 'user_email'
  88. * - 'role_slug'
  89. * - 'dest_url'
  90. */
  91. public function ajax_check_login() {
  92. try {
  93. if (!isset($_POST['username']) || !isset($_POST['password'])) {
  94. throw new Exception('Invalid credentials given!');
  95. }
  96. $this->load->model('user_model');
  97. $user_data = $this->user_model->check_login($_POST['username'], $_POST['password']);
  98. if ($user_data) {
  99. $this->session->set_userdata($user_data); // Save data on user's session.
  100. echo json_encode(AJAX_SUCCESS);
  101. } else {
  102. echo json_encode(AJAX_FAILURE);
  103. }
  104. } catch(Exception $exc) {
  105. echo json_encode(array(
  106. 'exceptions' => array(exceptionToJavaScript($exc))
  107. ));
  108. }
  109. }
  110. /**
  111. * Regenerate a new password for the current user, only if the username and
  112. * email address given corresond to an existing user in db.
  113. *
  114. * @param string $_POST['username']
  115. * @param string $_POST['email']
  116. */
  117. public function ajax_forgot_password() {
  118. try {
  119. if (!isset($_POST['username']) || !isset($_POST['email'])) {
  120. throw new Exception('You must enter a valid username and email address in '
  121. . 'order to get a new password!');
  122. }
  123. $this->load->model('user_model');
  124. $this->load->model('settings_model');
  125. $new_password = $this->user_model->regenerate_password($_POST['username'], $_POST['email']);
  126. if ($new_password != FALSE) {
  127. $this->load->library('notifications');
  128. $company_settings = array(
  129. 'company_name' => $this->settings_model->get_setting('company_name'),
  130. 'company_link' => $this->settings_model->get_setting('company_link'),
  131. 'company_email' => $this->settings_model->get_setting('company_email')
  132. );
  133. $this->notifications->send_password($new_password, $_POST['email'], $company_settings);
  134. }
  135. echo ($new_password != FALSE) ? json_encode(AJAX_SUCCESS) : json_encode(AJAX_FAILURE);
  136. } catch(Exception $exc) {
  137. echo json_encode(array(
  138. 'exceptions' => array(exceptionToJavaScript($exc))
  139. ));
  140. }
  141. }
  142. }
  143. /* End of file user.php */
  144. /* Location: ./application/controllers/user.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement