b9feb1030fd4a591f19445ca151fbd18e5afec5e
[living-lab-site.git] / application / controllers / user.php
1 <?php
2
3 /**
4  * Class User controls video hierarchy and searching
5  *
6  * @category    Controller
7  * @author              Călin-Andrei Burloiu
8  */
9 class User extends CI_Controller {
10
11         private $import = FALSE;
12
13         public function __construct()
14         {
15                 parent::__construct();
16
17                 $this->lang->load('user');
18                 $this->load->model('users_model');
19         }
20
21         public function index()
22         {
23         }
24
25         /**
26         * Login a user and then redirect it to the last page which must be encoded
27         * in $redirect.
28         *
29         * @param string $redirect       contains the last page URI segments encoded
30         * with helper url_encode_segments.
31         */
32         public function login($redirect = '')
33         {
34                 $this->load->library('form_validation');
35                         
36                 $this->form_validation->set_error_delimiters('<span class="error">',
37                         '</span>');
38
39                 if ($this->form_validation->run('signin') === FALSE)
40                 {
41                         $params = array(        'title' =>
42                                                                         $this->lang->line('ui_nav_menu_login')
43                                                                                 .' &ndash; '
44                                                                                 . $this->config->item('site_name'),
45                                                                 //'metas' => array('description'=>'')
46                         );
47                         $this->load->library('html_head_params', $params);
48                                 
49                         // **
50                         // ** LOADING VIEWS
51                         // **
52                         $this->load->view('html_begin', $this->html_head_params);
53                         $this->load->view('header', array('selected_menu' => 'login'));
54
55                         $main_params['content'] = $this->load->view('user/login_view',
56                                 array('redirect'=> $redirect), TRUE);
57                         $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
58                         $this->load->view('main', $main_params);
59                                 
60                         $this->load->view('footer');
61                         $this->load->view('html_end');
62                 }
63                 else
64                 {
65                         if (! $this->import)
66                         {
67                                 // Redirect to last page before login. 
68                                 header('Location: '. site_url(urldecode_segments($redirect)));
69                         }
70                         else
71                         {
72                                 // Redirect to account page because an user authenticates here
73                                 // for the first time with external authentication. The page
74                                 // will display imported data.
75                                 header('Location: '. site_url('user/account'));
76                         }
77                 }
78         }
79         
80         /**
81          * Logout user and then redirect it to the last page which must be encoded
82          * in $redirect.
83          * 
84          * @param string $redirect      contains the last page URI segments encoded
85          * with helper url_encode_segments.
86          */
87         public function logout($redirect = '')
88         {
89                 $this->session->unset_userdata('user_id');
90                 $this->session->unset_userdata('username');
91                 
92                 header('Location: '. site_url(urldecode_segments($redirect)));
93         }
94         
95         public function register($redirect = '')
96         {
97                 $this->load->library('form_validation');
98                 $this->load->helper('localization');
99                 $this->load->helper('date');
100                         
101                 $this->form_validation->set_error_delimiters('<span class="error">',
102                                         '</span>');
103                 
104                 if ($this->form_validation->run('register') === FALSE)
105                 {
106                         // Edit account data if logged in, otherwise register.
107                         if ($user_id = $this->session->userdata('user_id'))
108                         {
109                                 $userdata = $this->users_model->get_userdata($user_id);
110                         }
111                         else
112                         {
113                                 $userdata = FALSE;
114                         }
115                         
116                         $params = array('title' =>
117                                                                 $this->lang->line('ui_nav_menu_register')
118                                                                         .' &ndash; '
119                                                                         . $this->config->item('site_name'),
120                                                         //'metas' => array('description'=>'')
121                         );
122                         $this->load->library('html_head_params', $params);
123                 
124                         // **
125                         // ** LOADING VIEWS
126                         // **
127                         $this->load->view('html_begin', $this->html_head_params);
128                         $this->load->view('header', array('selected_menu' => 'register'));
129                         
130                         $main_params['content'] = $this->load->view('user/register_view', 
131                                 array('userdata'=> $userdata, 'redirect'=> $redirect),
132                                 TRUE);
133                         $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
134                         $this->load->view('main', $main_params);
135                 
136                         $this->load->view('footer');
137                         $this->load->view('html_end');
138                 }
139                 else
140                 {
141                         $user_id = $this->input->post('user-id');
142                         $data['email'] = $this->input->post('email');
143                         $data['first_name'] = $this->input->post('first-name');
144                         $data['last_name'] = $this->input->post('last-name');
145                         $data['birth_date'] = $this->input->post('birth-date');
146                         $data['country'] = $this->input->post('country');
147                         $data['locality'] = $this->input->post('locality');
148                         $data['ui_lang'] = $this->input->post('ui-lang');
149                         $data['time_zone'] = $this->input->post('time-zone');
150                         
151                         // Edit account data
152                         if ($user_id)
153                         {
154                                 $password = $this->input->post('new-password');
155                                 if ($password)
156                                         $data['password'] = $this->input->post('new-password');
157                                 
158                                 $this->users_model->set_userdata($user_id, $data);
159                         }
160                         // Registration
161                         else
162                         {
163                                 $data['username'] = $this->input->post('username');
164                                 $data['password'] = $this->input->post('password');
165                                 
166                                 $this->users_model->register($data);
167                         }
168                         
169                         // Redirect to last page before login.
170                         header('Location: '. site_url(urldecode_segments($redirect)));
171                 }
172         }
173         
174         public function account($redirect = '')
175         {
176                 $this->register($redirect);
177         }
178         
179         public function _valid_username($username)
180         {
181                 return (preg_match('/^[a-z0-9\._]+$/', $username) === 1);
182         }
183
184         public function _valid_username_or_email($username)
185         {
186                 $this->load->helper('email');
187
188                 if (valid_email($username))
189                         return TRUE;
190                 else
191                         return $this->_valid_username($username);
192         }
193         
194         public function _valid_date($date)
195         {
196                 if (! $date)
197                         return TRUE;
198                 
199                 return (preg_match('/[\d]{4}-[\d]{2}-[\d]{2}/', $date) === 1);
200         }
201         
202         public function _valid_old_password($old_password, $field_username)
203         {
204                 if (! $old_password)
205                         return TRUE;
206                 
207                 $username= $this->input->post($field_username);
208                 
209                 if ($this->users_model->login($username, $old_password))
210                         return TRUE;
211                 
212                 return FALSE;
213         }
214         
215         public function _change_password_cond($param)
216         {
217                 $old = $this->input->post('old-password');
218                 $new = $this->input->post('new-password');
219                 $newc = $this->input->post('new-password-confirmation');
220                 
221                 return (!$old && !$new && !$newc)
222                         || ($old && $new && $newc);
223         }
224         
225         public function _required_by_register($param)
226         {
227                 $user_id = $this->input->post('user-id');
228                 
229                 if (! $user_id && ! $param)
230                         return FALSE;
231                 
232                 return TRUE;
233         }
234
235         public function _do_login($username, $field_password)
236         {
237                 $password = $this->input->post($field_password);
238
239                 $user = $this->users_model->login($username, $password);
240
241                 // Authentication failed.
242                 if ($user === FALSE)
243                         return FALSE;
244                 
245                 // Authentication successful: set session with user data.
246                 $this->session->set_userdata(array(
247                         'user_id'=> $user['id'],
248                         'username'=> $user['username'],
249                         'auth_src'=> $user['auth_src']
250                 ));
251                 $this->import = $user['import'];
252                 return TRUE;
253         }
254 }
255
256 /* End of file user.php */
257 /* Location: ./application/controllers/user.php */