1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3 class User extends CI_Controller {
4 public function __construct() {
5 parent::__construct();
6 $this->load->library('session');
7
8
9 if ($this->session->userdata('language')) {
10 $this->config->set_item('language', $this->session->userdata('language'));
11 $this->lang->load('translations', $this->session->userdata('language'));
12 } else {
13 $this->lang->load('translations', $this->config->item('language'));
14 }
15 }
16
17 public function index() {
18 header('Location: ' . $this->config->item('base_url') . '/user/login');
19 }
20
21 public function login() {
22 $this->load->model('settings_model');
23
24 $view['base_url'] = $this->config->item('base_url');
25 $view['dest_url'] = $this->session->userdata('dest_url');
26
27 if (!$view['dest_url']) {
28 $view['dest_url'] = $view['base_url'] . '/backend';
29 }
30
31 $view['company_name'] = $this->settings_model->get_setting('company_name');
32 $this->load->view('user/login', $view);
33 }
34
35 public function logout() {
36 $this->load->model('settings_model');
37
38 $this->session->unset_userdata('user_id');
39 $this->session->unset_userdata('user_email');
40 $this->session->unset_userdata('role_slug');
41 $this->session->unset_userdata('username');
42 $this->session->unset_userdata('dest_url');
43
44 $view['base_url'] = $this->config->item('base_url');
45 $view['company_name'] = $this->settings_model->get_setting('company_name');
46 $this->load->view('user/logout', $view);
47 }
48
49 public function forgot_password() {
50 $this->load->model('settings_model');
51 $view['base_url'] = $this->config->item('base_url');
52 $view['company_name'] = $this->settings_model->get_setting('company_name');
53 $this->load->view('user/forgot_password', $view);
54 }
55
56 public function no_privileges() {
57 $this->load->model('settings_model');
58 $view['base_url'] = $this->config->item('base_url');
59 $view['company_name'] = $this->settings_model->get_setting('company_name');
60 $this->load->view('user/no_privileges', $view);
61 }
62
63 64 65 66 67 68 69 70 71
72 public function ajax_check_login() {
73 try {
74 if (!isset($_POST['username']) || !isset($_POST['password'])) {
75 throw new Exception('Invalid credentials given!');
76 }
77
78 $this->load->model('user_model');
79 $user_data = $this->user_model->check_login($_POST['username'], $_POST['password']);
80
81 if ($user_data) {
82 $this->session->set_userdata($user_data);
83 echo json_encode(AJAX_SUCCESS);
84 } else {
85 echo json_encode(AJAX_FAILURE);
86 }
87
88 } catch(Exception $exc) {
89 echo json_encode(array(
90 'exceptions' => array(exceptionToJavaScript($exc))
91 ));
92 }
93 }
94
95 96 97 98 99 100 101
102 public function ajax_forgot_password() {
103 try {
104 if (!isset($_POST['username']) || !isset($_POST['email'])) {
105 throw new Exception('You must enter a valid username and email address in '
106 . 'order to get a new password!');
107 }
108
109 $this->load->model('user_model');
110 $this->load->model('settings_model');
111
112 $new_password = $this->user_model->regenerate_password($_POST['username'], $_POST['email']);
113
114 if ($new_password != FALSE) {
115 $this->load->library('notifications');
116 $company_settings = array(
117 'company_name' => $this->settings_model->get_setting('company_name'),
118 'company_link' => $this->settings_model->get_setting('company_link'),
119 'company_email' => $this->settings_model->get_setting('company_email')
120 );
121 $this->notifications->send_password($new_password, $_POST['email'], $company_settings);
122 }
123
124 echo ($new_password != FALSE) ? json_encode(AJAX_SUCCESS) : json_encode(AJAX_FAILURE);
125 } catch(Exception $exc) {
126 echo json_encode(array(
127 'exceptions' => array(exceptionToJavaScript($exc))
128 ));
129 }
130 }
131 }
132
133
134