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