users can add pictures to their profiles; users can like and dislike videos
[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         public function test($user_id = 1)
28         {
29
30         }
31
32         /**
33         * Login a user and then redirect it to the last page which must be encoded
34         * in $redirect.
35         *
36         * @param string $redirect       contains the last page URI segments encoded
37         * with helper url_encode_segments.
38         */
39         public function login($redirect = '')
40         {
41                 $this->load->library('form_validation');
42                         
43                 $this->form_validation->set_error_delimiters('<span class="error">',
44                         '</span>');
45
46                 if ($this->form_validation->run('login') === FALSE)
47                 {
48                         $params = array(        'title' =>
49                                                                         $this->lang->line('ui_nav_menu_login')
50                                                                                 .' &ndash; '
51                                                                                 . $this->config->item('site_name'),
52                                                                 //'metas' => array('description'=>'')
53                         );
54                         $this->load->library('html_head_params', $params);
55                                 
56                         // **
57                         // ** LOADING VIEWS
58                         // **
59                         $this->load->view('html_begin', $this->html_head_params);
60                         $this->load->view('header', array('selected_menu' => 'login'));
61
62                         $main_params['content'] = $this->load->view('user/login_view',
63                                 array('redirect'=> $redirect), TRUE);
64                         $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
65                         $this->load->view('main', $main_params);
66                                 
67                         $this->load->view('footer');
68                         $this->load->view('html_end');
69                 }
70                 else
71                 {
72                         if (! $this->activated_account)
73                                 header('Location: '
74                                         . site_url("user/activate/{$this->user_id}"));
75                         else if (! $this->import)
76                         {
77                                 // Redirect to last page before login. 
78                                 header('Location: '. site_url(urldecode_segments($redirect)));
79                         }
80                         else
81                         {
82                                 // Redirect to account page because an user authenticates here
83                                 // for the first time with external authentication. The page
84                                 // will display imported data.
85                                 header('Location: '. site_url('user/account'));
86                         }
87                 }
88         }
89         
90         /**
91          * Logout user and then redirect it to the last page which must be encoded
92          * in $redirect.
93          * 
94          * @param string $redirect      contains the last page URI segments encoded
95          * with helper url_encode_segments.
96          */
97         public function logout($redirect = '')
98         {
99                 $this->session->unset_userdata('user_id');
100                 $this->session->unset_userdata('username');
101                 $this->session->unset_userdata('auth_src');
102                 $this->session->unset_userdata('time_zone');
103                 
104                 header('Location: '. site_url(urldecode_segments($redirect)));
105         }
106         
107         public function register($redirect = '')
108         {
109                 $this->load->library('form_validation');
110                 $this->load->helper('localization');
111                 $this->load->helper('date');
112                         
113                 $this->form_validation->set_error_delimiters('<span class="error">',
114                                         '</span>');
115                 $error_upload = '';
116
117                 if ($this->form_validation->run('register'))
118                 {
119                         $b_validation = TRUE;
120                         
121                         if ($_FILES['picture']['tmp_name'])
122                         {
123                                 // Upload library
124                                 $config_upload['upload_path'] = './data/user_pictures';
125                                 $config_upload['file_name'] = 
126                                         str_replace('.', '-', $this->input->post('username')) .'-';
127                                 $config_upload['allowed_types'] = 'gif|jpg|png';
128                                 $config_upload['max_size'] = '10240';
129                                 $this->load->library('upload', $config_upload);
130                                 
131                                 $b_validation = $this->upload->do_upload('picture');
132                                 $error_upload = 
133                                         $this->upload->display_errors('<span class="error">',
134                                                         '</span>');
135                         }
136                 }
137                 else
138                         $b_validation = FALSE;
139                 
140                 if (! $b_validation)
141                 {
142                         // Edit account data if logged in, otherwise register.
143                         if ($user_id = $this->session->userdata('user_id'))
144                         {
145                                 $userdata = $this->users_model->get_userdata(intval($user_id));
146                                 $selected_menu = 'account';
147                         }
148                         else
149                         {
150                                 $userdata = FALSE;
151                                 $selected_menu = 'register';
152                         }
153                         
154                         $params = array('title' =>
155                                                                 $this->lang->line('ui_nav_menu_register')
156                                                                         .' &ndash; '
157                                                                         . $this->config->item('site_name'),
158                                                         //'metas' => array('description'=>'')
159                         );
160                         $this->load->library('html_head_params', $params);
161                 
162                         // **
163                         // ** LOADING VIEWS
164                         // **
165                         $this->load->view('html_begin', $this->html_head_params);
166                         $this->load->view('header', 
167                                 array('selected_menu' => $selected_menu));
168                         
169                         $main_params['content'] = $this->load->view('user/register_view', 
170                                 array('userdata'=> $userdata, 'redirect'=> $redirect,
171                                         'error_upload'=> $error_upload),
172                                 TRUE);
173                         $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
174                         $this->load->view('main', $main_params);
175                 
176                         $this->load->view('footer');
177                         $this->load->view('html_end');
178                 }
179                 else
180                 {
181                         $user_id = $this->input->post('user-id');
182                         $data['email'] = $this->input->post('email');
183                         $data['first_name'] = $this->input->post('first-name');
184                         $data['last_name'] = $this->input->post('last-name');
185                         $data['birth_date'] = $this->input->post('birth-date');
186                         $data['country'] = $this->input->post('country');
187                         $data['locality'] = $this->input->post('locality');
188                         $data['ui_lang'] = $this->input->post('ui-lang');
189                         $data['time_zone'] = $this->input->post('time-zone');
190                         
191                         // Handle picture if one was uploaded.
192                         if ($_FILES['picture']['tmp_name'])
193                         {
194                                 $upload_data = $this->upload->data();
195                                 $this->load->library('image');
196                                 $this->image->load($upload_data['full_path']);
197                                 // Resize original to a maximum size.
198                                 if ($this->image->get_width() * $this->image->get_height()
199                                                 > 640*480)
200                                 {
201                                         $this->image->save_thumbnail(
202                                                 $upload_data['full_path'],
203                                                 640, 480, IMAGETYPE_AUTO);
204                                 }
205                                 // Create thumbnail.
206                                 $data['picture'] = $upload_data['file_name']. '-thumb.jpg';
207                                 $this->image->save_thumbnail($upload_data['file_path']
208                                                 . $upload_data['file_name']. '-thumb.jpg', 120, 90);
209                         }
210                         
211                         // Update session user data.
212                         $this->_update_session_userdata($data);
213                         
214                         // Edit account data
215                         if ($user_id)
216                         {
217                                 $password = $this->input->post('new-password');
218                                 if ($password)
219                                         $data['password'] = $this->input->post('new-password');
220                                 
221                                 $this->users_model->set_userdata($user_id, $data);
222                                 
223                                 // Redirect to last page before login.
224                                 header('Location: '. site_url(urldecode_segments($redirect)));
225                         }
226                         // Registration
227                         else
228                         {
229                                 $data['username'] = $this->input->post('username');
230                                 $data['password'] = $this->input->post('password');
231                                 
232                                 $this->users_model->register($data);
233                                 $user_id = $this->users_model->get_userdata($data['username'],
234                                                 "id");
235                                 $user_id = $user_id['id'];
236                                 
237                                 // Redirect account activation page.
238                                 header('Location: '. site_url("user/activate/$user_id"));
239                         }
240                 }
241         }
242         
243         public function account($redirect = '')
244         {
245                 $this->register($redirect);
246         }
247         
248         public function profile($username, $videos_offset = 0)
249         {
250                 // TODO handle user not found
251                 
252                 $this->load->config('localization');
253                 $this->load->helper('date');
254                 $this->lang->load('date');
255                 
256                 // **
257                 // ** LOADING MODEL
258                 // **
259                 // Logged in user time zone
260                 $time_zone = $this->session->userdata('time_zone');
261                 if (! $time_zone)
262                         $time_zone = 'UTC';
263                 
264                 // User data
265                 $userdata = $this->users_model->get_userdata($username);
266                 $userdata['roles'] = Users_model::roles_to_string($userdata['roles']);
267                 $country_list = $this->config->item('country_list');
268                 $userdata['country_name'] = $country_list[ $userdata['country'] ];
269                 $userdata['last_login'] = date('Y-m-d H:i:s',  
270                         gmt_to_local(
271                                 strtotime($userdata['last_login']), 
272                                 $time_zone, 
273                                 TRUE)) . ($time_zone == 'UTC' ? ' (UTC)' : '');
274                 $userdata['time_zone'] = $this->lang->line($userdata['time_zone']);
275                 
276                 // User's videos
277                 $this->load->model('videos_model');
278                 $vs_data['videos'] = $this->videos_model->get_videos_summary(
279                         NULL, $username, intval($videos_offset),
280                         $this->config->item('videos_per_page'));
281                 
282                 // Pagination
283                 $this->load->library('pagination');
284                 $pg_config['base_url'] = site_url("user/profile/$username/");
285                 $pg_config['uri_segment'] = 4;
286                 $pg_config['total_rows'] = $this->videos_model->get_videos_count(
287                         NULL, $username);
288                 $pg_config['per_page'] = $this->config->item('videos_per_page');
289                 $this->pagination->initialize($pg_config);
290                 $vs_data['pagination'] = $this->pagination->create_links();
291                 $vs_data['title'] = NULL;
292                 $vs_data['category_name'] = ''; // TODO videos_summary with AJAX
293                 
294                 $params = array(
295                         'title'=> $this->lang->line('user_appelation').' '.$username
296                                 .' &ndash; '
297                                 . $this->config->item('site_name'),
298                         'css'=> array('catalog.css')
299                         //'metas' => array('description'=>'')
300                 );
301                 $this->load->library('html_head_params', $params);
302                 
303                 // Current user profile tab
304                 $tab = (! $videos_offset ? 0 : 1);
305                 
306                 // **
307                 // ** LOADING VIEWS
308                 // **
309                 $this->load->view('html_begin', $this->html_head_params);
310                 $this->load->view('header', array());
311                 
312                 $vs = $this->load->view('catalog/videos_summary_view', $vs_data, TRUE);
313                 
314                 $main_params['content'] = $this->load->view('user/profile_view',
315                         array('userdata'=> $userdata, 'videos_summary'=> $vs, 'tab'=>$tab),
316                         TRUE);
317                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
318                 $this->load->view('main', $main_params);
319                 
320                 $this->load->view('footer');
321                 $this->load->view('html_end');
322         }
323         
324         public function activate($user_id, $method='', $activation_code='')
325         {
326                 $user_id = intval($user_id);            
327                 $res_form_validation = FALSE;
328                 
329                 if ($method == 'code')
330                 {
331                         if (! $activation_code)
332                                 $res_form_validation = $this->form_validation->run('activate');
333                         // Activation code is provided in URL.
334                         else
335                         {
336                                 if ($this->_valid_activation_code($activation_code)
337                                                 && $this->users_model->activate_account($user_id,
338                                                         $activation_code))
339                                 {
340                                         $this->session->set_flashdata('msg', sprintf(
341                                                 $this->lang->line('user_msg_activated_account'), 
342                                                 site_url('user/login')));
343                                         header('Location: '. site_url('message/info'));
344                                         return;
345                                 }
346                                 else
347                                 {
348                                         $this->session->set_flashdata('msg',
349                                                 $this->lang->line('user_msg_wrong_activation_code'));
350                                         header('Location: '. site_url('message/error'));
351                                         return;
352                                 }
353                         }
354                 }
355                 else if ($method == 'resend')
356                 {
357                         $res_form_validation =
358                                 $this->form_validation->run('resend_activation');
359                 }
360                 
361                 $userdata = $this->users_model->get_userdata($user_id,
362                                 'email, a.activation_code');
363                 $email = $userdata['email'];
364                 $activated_account = ($userdata['activation_code'] == NULL);
365                 
366                 if ($activated_account)
367                 {
368                         $this->session->set_flashdata('msg', sprintf(
369                                                 $this->lang->line('user_msg_activated_account'), 
370                                                 site_url('user/login')));
371                         header('Location: '. site_url('message/info'));
372                         return;
373                 }
374                 
375                 $this->load->library('form_validation');
376                         
377                 $this->form_validation->set_error_delimiters('<span class="error">',
378                                         '</span>');
379                 
380                 if ($res_form_validation === FALSE)
381                 {
382                         $params = array(
383                                 'title'=> $this->lang->line('user_title_activation')
384                                         .' &ndash; '
385                                         . $this->config->item('site_name'),
386                                 //'metas' => array('description'=>'')
387                         );
388                         $this->load->library('html_head_params', $params);
389                 
390                         // **
391                         // ** LOADING VIEWS
392                         // **
393                         $this->load->view('html_begin', $this->html_head_params);
394                         $this->load->view('header', array());
395
396                         // Show form
397                         $main_params['content'] = 
398                                 $this->load->view('user/activate_view',
399                                 array(  'user_id'=> $user_id,
400                                                 'email'=> $userdata['email']),
401                                 TRUE);
402                         
403                         $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
404                         $this->load->view('main', $main_params);
405                 
406                         $this->load->view('footer');
407                         $this->load->view('html_end');
408                 }
409                 else
410                 {
411                         if ($method == 'code')
412                         {
413                                 // Redirect to a message which tells the user that the
414                                 // activation was successful.
415                                 $this->session->set_flashdata('msg', sprintf(
416                                                 $this->lang->line('user_msg_activated_account'), 
417                                                 site_url('user/login')));
418                                 header('Location: '. site_url('message/info'));
419                                 return;
420                         }
421                         else if ($method == 'resend')
422                         {
423                                 // Redirect to resent message
424                                 $this->session->set_flashdata('msg', sprintf(
425                                                 $this->lang->line('user_msg_activation_resent'),
426                                                 $this->input->post('email')));
427                                 header('Location: '. site_url('message/info'));
428                                 return;
429                         }
430                 }
431         }
432         
433         public function recover_password()
434         {
435                 $this->load->library('form_validation');
436                         
437                 $this->form_validation->set_error_delimiters('<span class="error">',
438                         '</span>');
439
440                 if ($this->form_validation->run('recover_password') === FALSE)
441                 {
442                         $params = array(        'title' =>
443                                                                         $this->lang->line(
444                                                                                 'user_title_password_recovery')
445                                                                                 .' &ndash; '
446                                                                                 . $this->config->item('site_name'),
447                                                                 //'metas' => array('description'=>'')
448                         );
449                         $this->load->library('html_head_params', $params);
450                                 
451                         // **
452                         // ** LOADING VIEWS
453                         // **
454                         $this->load->view('html_begin', $this->html_head_params);
455                         $this->load->view('header', array('selected_menu' => 
456                                         'recover_password'));
457
458                         $main_params['content'] = $this->load->view(
459                                 'user/recover_password_view', array(),
460                                 TRUE);
461                         
462                         $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
463                         $this->load->view('main', $main_params);
464                                 
465                         $this->load->view('footer');
466                         $this->load->view('html_end');
467                 }
468                 else
469                 {
470                         // Redirect to resent message
471                         $this->session->set_flashdata('msg', sprintf(
472                                         $this->lang->line('user_msg_password_recovery_email_sent'),
473                                         $this->input->post('username'),
474                                         $this->input->post('email')));
475                         header('Location: '. site_url('message/info'));
476                         return;
477                 }
478         }
479         
480         public function _update_session_userdata($data)
481         {
482                 foreach ($data as $key=> $val)
483                         $this->session->set_userdata($key, $val);
484         }
485         
486         public function _valid_username($username)
487         {
488                 return (preg_match('/^[a-z0-9\._]+$/', $username) === 1);
489         }
490
491         public function _valid_username_or_email($username)
492         {
493                 $this->load->helper('email');
494
495                 if (valid_email($username))
496                         return TRUE;
497                 else
498                         return $this->_valid_username($username);
499         }
500         
501         public function _valid_date($date)
502         {
503                 if (! $date)
504                         return TRUE;
505                 
506                 return (preg_match('/[\d]{4}-[\d]{2}-[\d]{2}/', $date) === 1);
507         }
508         
509         public function _valid_old_password($old_password, $field_username)
510         {
511                 if (! $old_password)
512                         return TRUE;
513                 
514                 $username= $this->input->post($field_username);
515                 
516                 if ($this->users_model->login($username, $old_password))
517                         return TRUE;
518                 
519                 return FALSE;
520         }
521         
522         public function _change_password_cond($param)
523         {
524                 $old = $this->input->post('old-password');
525                 $new = $this->input->post('new-password');
526                 $newc = $this->input->post('new-password-confirmation');
527                 
528                 return (!$old && !$new && !$newc)
529                         || ($old && $new && $newc);
530         }
531         
532         public function _required_by_register($param)
533         {
534                 $user_id = $this->input->post('user-id');
535                 
536                 if (! $user_id && ! $param)
537                         return FALSE;
538                 
539                 return TRUE;
540         }
541         
542         public function _valid_activation_code($activation_code)
543         {
544                 return (preg_match('/^[a-fA-F0-9]{16}$/', $activation_code) == 1);
545         }
546
547         public function _do_login($username, $field_password)
548         {
549                 $password = $this->input->post($field_password);
550
551                 $user = $this->users_model->login($username, $password);
552
553                 // Authentication failed.
554                 if ($user === FALSE)
555                         return FALSE;
556                 
557                 // User has not activated the account.
558                 if ($user['activation_code'] !== NULL)
559                 {
560                         $this->activated_account = FALSE;
561                         $this->user_id = $user['id'];
562                         return TRUE;
563                 }
564                 
565                 // Authentication successful: set session with user data.
566                 $this->session->set_userdata(array(
567                         'user_id'=> $user['id'],
568                         'username'=> $user['username'],
569                         'auth_src'=> $user['auth_src'],
570                         'time_zone'=> $user['time_zone']
571                 ));
572                 $this->import = (isset($user['import']) ? $user['import'] : FALSE);
573                 return TRUE;
574         }
575         
576         public function _do_activate($activation_code)
577         {
578                 $user_id = $this->input->post('user-id');
579                 if ($user_id === FALSE)
580                         return FALSE;
581                 $user_id = intval($user_id);
582                 
583                 return $this->users_model->activate_account($user_id,
584                                 $activation_code);
585         }
586         
587         public function _do_resend_activation($email)
588         {
589                 $user_id = $this->input->post('user-id');
590                 if ($user_id === FALSE)
591                         return FALSE;
592                 $user_id = intval($user_id);
593                 
594                 $this->users_model->set_userdata($user_id,
595                         array('email'=> $email));
596                 
597                 return $this->users_model->send_activation_email($user_id, $email);
598         }
599         
600         public function _username_exists($username)
601         {
602                 $userdata = $this->users_model->get_userdata($username);
603                 
604                 if (! $userdata)
605                         return FALSE;
606                 
607                 return TRUE;
608         }
609         
610         public function _internal_account($username)
611         {
612                 $userdata = $this->users_model->get_userdata($username, 'auth_src');
613                 if (! $userdata)
614                         return FALSE;
615
616                 if ($userdata['auth_src'] != 'internal')
617                         return FALSE;
618                 
619                 return TRUE;
620         }
621         
622         public function _do_recover_password($username)
623         {
624                 $email = $this->input->post('email');
625                 if (! $email)
626                         return FALSE;
627                 
628                 return $this->users_model->recover_password($username, $email);
629         }
630 }
631
632 /* End of file user.php */
633 /* Location: ./application/controllers/user.php */