account activation almost ready
[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         private $activated_account = TRUE;
13         private $user_id = NULL;
14
15         public function __construct()
16         {
17                 parent::__construct();
18
19                 $this->lang->load('user');
20                 $this->load->model('users_model');
21         }
22
23         public function index()
24         {
25         }
26
27         /**
28         * Login a user and then redirect it to the last page which must be encoded
29         * in $redirect.
30         *
31         * @param string $redirect       contains the last page URI segments encoded
32         * with helper url_encode_segments.
33         */
34         public function login($redirect = '')
35         {
36                 $this->load->library('form_validation');
37                         
38                 $this->form_validation->set_error_delimiters('<span class="error">',
39                         '</span>');
40
41                 if ($this->form_validation->run('signin') === FALSE)
42                 {
43                         $params = array(        'title' =>
44                                                                         $this->lang->line('ui_nav_menu_login')
45                                                                                 .' &ndash; '
46                                                                                 . $this->config->item('site_name'),
47                                                                 //'metas' => array('description'=>'')
48                         );
49                         $this->load->library('html_head_params', $params);
50                                 
51                         // **
52                         // ** LOADING VIEWS
53                         // **
54                         $this->load->view('html_begin', $this->html_head_params);
55                         $this->load->view('header', array('selected_menu' => 'login'));
56
57                         $main_params['content'] = $this->load->view('user/login_view',
58                                 array('redirect'=> $redirect), TRUE);
59                         $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
60                         $this->load->view('main', $main_params);
61                                 
62                         $this->load->view('footer');
63                         $this->load->view('html_end');
64                 }
65                 else
66                 {
67                         if (! $this->activated_account)
68                                 header('Location: '
69                                         . site_url("user/activate/{$this->user_id}"));
70                         else if (! $this->import)
71                         {
72                                 // Redirect to last page before login. 
73                                 header('Location: '. site_url(urldecode_segments($redirect)));
74                         }
75                         else
76                         {
77                                 // Redirect to account page because an user authenticates here
78                                 // for the first time with external authentication. The page
79                                 // will display imported data.
80                                 header('Location: '. site_url('user/account'));
81                         }
82                 }
83         }
84         
85         /**
86          * Logout user and then redirect it to the last page which must be encoded
87          * in $redirect.
88          * 
89          * @param string $redirect      contains the last page URI segments encoded
90          * with helper url_encode_segments.
91          */
92         public function logout($redirect = '')
93         {
94                 $this->session->unset_userdata('user_id');
95                 $this->session->unset_userdata('username');
96                 $this->session->unset_userdata('auth_src');
97                 $this->session->unset_userdata('time_zone');
98                 
99                 header('Location: '. site_url(urldecode_segments($redirect)));
100         }
101         
102         public function register($redirect = '')
103         {
104                 $this->load->library('form_validation');
105                 $this->load->helper('localization');
106                 $this->load->helper('date');
107                         
108                 $this->form_validation->set_error_delimiters('<span class="error">',
109                                         '</span>');
110                 
111                 if ($this->form_validation->run('register') === FALSE)
112                 {
113                         // Edit account data if logged in, otherwise register.
114                         if ($user_id = $this->session->userdata('user_id'))
115                         {
116                                 $userdata = $this->users_model->get_userdata(intval($user_id));
117                                 $selected_menu = 'account';
118                         }
119                         else
120                         {
121                                 $userdata = FALSE;
122                                 $selected_menu = 'register';
123                         }
124                         
125                         $params = array('title' =>
126                                                                 $this->lang->line('ui_nav_menu_register')
127                                                                         .' &ndash; '
128                                                                         . $this->config->item('site_name'),
129                                                         //'metas' => array('description'=>'')
130                         );
131                         $this->load->library('html_head_params', $params);
132                 
133                         // **
134                         // ** LOADING VIEWS
135                         // **
136                         $this->load->view('html_begin', $this->html_head_params);
137                         $this->load->view('header', 
138                                 array('selected_menu' => $selected_menu));
139                         
140                         $main_params['content'] = $this->load->view('user/register_view', 
141                                 array('userdata'=> $userdata, 'redirect'=> $redirect),
142                                 TRUE);
143                         $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
144                         $this->load->view('main', $main_params);
145                 
146                         $this->load->view('footer');
147                         $this->load->view('html_end');
148                 }
149                 else
150                 {
151                         $user_id = $this->input->post('user-id');
152                         $data['email'] = $this->input->post('email');
153                         $data['first_name'] = $this->input->post('first-name');
154                         $data['last_name'] = $this->input->post('last-name');
155                         $data['birth_date'] = $this->input->post('birth-date');
156                         $data['country'] = $this->input->post('country');
157                         $data['locality'] = $this->input->post('locality');
158                         $data['ui_lang'] = $this->input->post('ui-lang');
159                         $data['time_zone'] = $this->input->post('time-zone');
160                         
161                         // Update session user data.
162                         $this->_update_session_userdata($data);
163                         
164                         // Edit account data
165                         if ($user_id)
166                         {
167                                 $password = $this->input->post('new-password');
168                                 if ($password)
169                                         $data['password'] = $this->input->post('new-password');
170                                 
171                                 $this->users_model->set_userdata($user_id, $data);
172                                 
173                                 // Redirect to last page before login.
174                                 header('Location: '. site_url(urldecode_segments($redirect)));
175                         }
176                         // Registration
177                         else
178                         {
179                                 $data['username'] = $this->input->post('username');
180                                 $data['password'] = $this->input->post('password');
181                                 
182                                 $this->users_model->register($data);
183                                 $user_id = $this->users_model->get_userdata($data['username'],
184                                                 "id");
185                                 $user_id = $user_id['id'];
186                                 
187                                 // Redirect account activation page.
188                                 header('Location: '. site_url("user/activate/$user_id"));
189                         }
190                 }
191         }
192         
193         public function account($redirect = '')
194         {
195                 $this->register($redirect);
196         }
197         
198         public function profile($username, $videos_offset = 0)
199         {
200                 // TODO handle user not found
201                 
202                 $this->load->config('localization');
203                 $this->load->helper('date');
204                 $this->lang->load('date');
205                 
206                 // **
207                 // ** LOADING MODEL
208                 // **
209                 // Logged in user time zone
210                 $time_zone = $this->session->userdata('time_zone');
211                 if (! $time_zone)
212                         $time_zone = 'UTC';
213                 
214                 // User data
215                 $userdata = $this->users_model->get_userdata($username);
216                 $userdata['roles'] = Users_model::roles_to_string($userdata['roles']);
217                 $country_list = $this->config->item('country_list');
218                 $userdata['country_name'] = $country_list[ $userdata['country'] ];
219                 $userdata['last_login'] = date('Y-m-d H:i:s',  
220                         gmt_to_local(
221                                 strtotime($userdata['last_login']), 
222                                 $time_zone, 
223                                 TRUE)) . ($time_zone == 'UTC' ? ' (UTC)' : '');
224                 $userdata['time_zone'] = $this->lang->line($userdata['time_zone']);
225                 
226                 // User's videos
227                 $this->load->model('videos_model');
228                 $vs_data['videos'] = $this->videos_model->get_videos_summary(
229                         NULL, $username, intval($videos_offset),
230                         $this->config->item('videos_per_page'));
231                 
232                 // Pagination
233                 $this->load->library('pagination');
234                 $pg_config['base_url'] = site_url("user/profile/$username/");
235                 $pg_config['uri_segment'] = 4;
236                 $pg_config['total_rows'] = $this->videos_model->get_videos_count(
237                         NULL, $username);
238                 $pg_config['per_page'] = $this->config->item('videos_per_page');
239                 $this->pagination->initialize($pg_config);
240                 $vs_data['pagination'] = $this->pagination->create_links();
241                 $vs_data['title'] = NULL;
242                 $vs_data['category_name'] = ''; // TODO videos_summary with AJAX
243                 
244                 $params = array(
245                         'title'=> $this->lang->line('user_appelation').' '.$username
246                                 .' &ndash; '
247                                 . $this->config->item('site_name'),
248                         'css'=> array('catalog.css')
249                         //'metas' => array('description'=>'')
250                 );
251                 $this->load->library('html_head_params', $params);
252                 
253                 // Current user profile tab
254                 $tab = (! $videos_offset ? 0 : 1);
255                 
256                 // **
257                 // ** LOADING VIEWS
258                 // **
259                 $this->load->view('html_begin', $this->html_head_params);
260                 $this->load->view('header', array());
261                 
262                 $vs = $this->load->view('catalog/videos_summary_view', $vs_data, TRUE);
263                 
264                 $main_params['content'] = $this->load->view('user/profile_view',
265                         array('userdata'=> $userdata, 'videos_summary'=> $vs, 'tab'=>$tab),
266                         TRUE);
267                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
268                 $this->load->view('main', $main_params);
269                 
270                 $this->load->view('footer');
271                 $this->load->view('html_end');
272         }
273         
274         public function activate($user_id, $method='', $activation_code='')
275         {
276                 $user_id = intval($user_id);
277                 $userdata = $this->users_model->get_userdata($user_id,
278                                 'email, a.activation_code');
279                 $email = $userdata['email'];
280                 //print_r($userdata['activation_code']);
281                 $activated_account = ($userdata['activation_code'] == NULL);
282                 
283                 $this->load->library('form_validation');
284                         
285                 $this->form_validation->set_error_delimiters('<span class="error">',
286                                         '</span>');
287                 
288                 $res_form_validation = FALSE;
289                 if ($method == 'code')
290                 {
291                         $res_form_validation = $this->form_validation->run('activate');
292                 }
293                 else if ($method == 'resend')
294                 {
295                         $res_form_validation = 
296                                         $this->form_validation->run('resend_activation');
297                 }
298                 
299                 if ($res_form_validation === FALSE)
300                 {
301                         $params = array(
302                                 'title'=> $this->lang->line('user_title_activation')
303                                         .' &ndash; '
304                                         . $this->config->item('site_name'),
305                                 //'metas' => array('description'=>'')
306                         );
307                         $this->load->library('html_head_params', $params);
308                 
309                         // **
310                         // ** LOADING VIEWS
311                         // **
312                         $this->load->view('html_begin', $this->html_head_params);
313                         $this->load->view('header', array());
314
315                         if (! $activated_account)
316                         {
317                                 $main_params['content'] = 
318                                         $this->load->view('user/activate_view',
319                                         array('user_id'=> $user_id, 'email'=> $userdata['email']),
320                                         TRUE);
321                         }
322                         else
323                         {
324                                 $main_params['content'] =
325                                         $this->load->view('user/activated_account_view',
326                                         NULL, TRUE);
327                         }
328                         
329                         $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
330                         $this->load->view('main', $main_params);
331                 
332                         $this->load->view('footer');
333                         $this->load->view('html_end');
334                 }
335                 else
336                 {
337                         if ($method == 'code')
338                         {
339                                 // Redirect to a message which tells the user that the
340                                 // activation was successful.
341                                 header('Location: '. site_url("user/activate/$user_id"));
342                         }
343                         else if ($method == 'resend')
344                         {
345                                 // Redirect to home page
346                                 header('Location: '. site_url());
347                         }
348                 }
349         }
350         
351         public function _update_session_userdata($data)
352         {
353                 foreach ($data as $key=> $val)
354                         $this->session->set_userdata($key, $val);
355         }
356         
357         public function _valid_username($username)
358         {
359                 return (preg_match('/^[a-z0-9\._]+$/', $username) === 1);
360         }
361
362         public function _valid_username_or_email($username)
363         {
364                 $this->load->helper('email');
365
366                 if (valid_email($username))
367                         return TRUE;
368                 else
369                         return $this->_valid_username($username);
370         }
371         
372         public function _valid_date($date)
373         {
374                 if (! $date)
375                         return TRUE;
376                 
377                 return (preg_match('/[\d]{4}-[\d]{2}-[\d]{2}/', $date) === 1);
378         }
379         
380         public function _valid_old_password($old_password, $field_username)
381         {
382                 if (! $old_password)
383                         return TRUE;
384                 
385                 $username= $this->input->post($field_username);
386                 
387                 if ($this->users_model->login($username, $old_password))
388                         return TRUE;
389                 
390                 return FALSE;
391         }
392         
393         public function _change_password_cond($param)
394         {
395                 $old = $this->input->post('old-password');
396                 $new = $this->input->post('new-password');
397                 $newc = $this->input->post('new-password-confirmation');
398                 
399                 return (!$old && !$new && !$newc)
400                         || ($old && $new && $newc);
401         }
402         
403         public function _required_by_register($param)
404         {
405                 $user_id = $this->input->post('user-id');
406                 
407                 if (! $user_id && ! $param)
408                         return FALSE;
409                 
410                 return TRUE;
411         }
412         
413         public function _valid_activation_code($activation_code)
414         {
415                 return (preg_match('/^[a-fA-F0-9]{16}$/', $activation_code) == 1);
416         }
417
418         public function _do_login($username, $field_password)
419         {
420                 $password = $this->input->post($field_password);
421
422                 $user = $this->users_model->login($username, $password);
423
424                 // Authentication failed.
425                 if ($user === FALSE)
426                         return FALSE;
427                 
428                 // User has not activated the account.
429                 if ($user['activation_code'] !== NULL)
430                 {
431                         $this->activated_account = FALSE;
432                         $this->user_id = $user['id'];
433                         return TRUE;
434                 }
435                 
436                 // Authentication successful: set session with user data.
437                 $this->session->set_userdata(array(
438                         'user_id'=> $user['id'],
439                         'username'=> $user['username'],
440                         'auth_src'=> $user['auth_src'],
441                         'time_zone'=> $user['time_zone']
442                 ));
443                 $this->import = (isset($user['import']) ? $user['import'] : FALSE);
444                 return TRUE;
445         }
446         
447         public function _do_activate($activation_code)
448         {
449                 $user_id = $this->input->post('user-id');
450                 if ($user_id === FALSE)
451                         return FALSE;
452                 $user_id = intval($user_id);
453                 
454                 return $this->users_model->activate_account($user_id,
455                                 $activation_code);
456         }
457         
458         public function _do_resend_activation($email)
459         {
460                 return FALSE;
461         }
462 }
463
464 /* End of file user.php */
465 /* Location: ./application/controllers/user.php */