working at video comments
[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                 if (! $time_zone)
261                         $time_zone = 'UTC';
262                 
263                 // User data
264                 $userdata = $this->users_model->get_userdata($username);
265                 $userdata['roles'] = Users_model::roles_to_string($userdata['roles']);
266                 $country_list = $this->config->item('country_list');
267                 $userdata['country_name'] = $country_list[ $userdata['country'] ];
268                 $userdata['last_login'] = date('Y-m-d H:i:s',  
269                         gmt_to_local(
270                                 strtotime($userdata['last_login']), 
271                                 $time_zone, 
272                                 TRUE)) . ($time_zone == 'UTC' ? ' (UTC)' : '');
273                 $userdata['time_zone'] = $this->lang->line($userdata['time_zone']);
274                 
275                 // User's videos
276                 $this->load->model('videos_model');
277                 $vs_data['videos'] = $this->videos_model->get_videos_summary(
278                         NULL, $username, intval($videos_offset),
279                         $this->config->item('videos_per_page'));
280                 
281                 // Pagination
282                 $this->load->library('pagination');
283                 $pg_config['base_url'] = site_url("user/profile/$username/");
284                 $pg_config['uri_segment'] = 4;
285                 $pg_config['total_rows'] = $this->videos_model->get_videos_count(
286                         NULL, $username);
287                 $pg_config['per_page'] = $this->config->item('videos_per_page');
288                 $this->pagination->initialize($pg_config);
289                 $vs_data['pagination'] = $this->pagination->create_links();
290                 $vs_data['title'] = NULL;
291                 $vs_data['category_name'] = ''; // TODO videos_summary with AJAX
292                 
293                 $params = array(
294                         'title'=> $this->lang->line('user_appelation').' '.$username
295                                 .' &ndash; '
296                                 . $this->config->item('site_name'),
297                         'css'=> array('catalog.css')
298                         //'metas' => array('description'=>'')
299                 );
300                 $this->load->library('html_head_params', $params);
301                 
302                 // Current user profile tab
303                 $tab = (! $videos_offset ? 0 : 1);
304                 
305                 // **
306                 // ** LOADING VIEWS
307                 // **
308                 $this->load->view('html_begin', $this->html_head_params);
309                 $this->load->view('header', array());
310                 
311                 $vs = $this->load->view('catalog/videos_summary_view', $vs_data, TRUE);
312                 
313                 $main_params['content'] = $this->load->view('user/profile_view',
314                         array('userdata'=> $userdata, 'videos_summary'=> $vs, 'tab'=>$tab),
315                         TRUE);
316                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
317                 $this->load->view('main', $main_params);
318                 
319                 $this->load->view('footer');
320                 $this->load->view('html_end');
321         }
322         
323         public function activate($user_id, $method='', $activation_code='')
324         {
325                 $user_id = intval($user_id);            
326                 $res_form_validation = FALSE;
327                 
328                 if ($method == 'code')
329                 {
330                         if (! $activation_code)
331                                 $res_form_validation = $this->form_validation->run('activate');
332                         // Activation code is provided in URL.
333                         else
334                         {
335                                 if ($this->_valid_activation_code($activation_code)
336                                                 && $this->users_model->activate_account($user_id,
337                                                         $activation_code))
338                                 {
339                                         $this->session->set_flashdata('msg', sprintf(
340                                                 $this->lang->line('user_msg_activated_account'), 
341                                                 site_url('user/login')));
342                                         header('Location: '. site_url('message/info'));
343                                         return;
344                                 }
345                                 else
346                                 {
347                                         $this->session->set_flashdata('msg',
348                                                 $this->lang->line('user_msg_wrong_activation_code'));
349                                         header('Location: '. site_url('message/error'));
350                                         return;
351                                 }
352                         }
353                 }
354                 else if ($method == 'resend')
355                 {
356                         $res_form_validation =
357                                 $this->form_validation->run('resend_activation');
358                 }
359                 
360                 $userdata = $this->users_model->get_userdata($user_id,
361                                 'email, a.activation_code');
362                 $email = $userdata['email'];
363                 $activated_account = ($userdata['activation_code'] == NULL);
364                 
365                 if ($activated_account)
366                 {
367                         $this->session->set_flashdata('msg', sprintf(
368                                                 $this->lang->line('user_msg_activated_account'), 
369                                                 site_url('user/login')));
370                         header('Location: '. site_url('message/info'));
371                         return;
372                 }
373                 
374                 $this->load->library('form_validation');
375                         
376                 $this->form_validation->set_error_delimiters('<span class="error">',
377                                         '</span>');
378                 
379                 if ($res_form_validation === FALSE)
380                 {
381                         $params = array(
382                                 'title'=> $this->lang->line('user_title_activation')
383                                         .' &ndash; '
384                                         . $this->config->item('site_name'),
385                                 //'metas' => array('description'=>'')
386                         );
387                         $this->load->library('html_head_params', $params);
388                 
389                         // **
390                         // ** LOADING VIEWS
391                         // **
392                         $this->load->view('html_begin', $this->html_head_params);
393                         $this->load->view('header', array());
394
395                         // Show form
396                         $main_params['content'] = 
397                                 $this->load->view('user/activate_view',
398                                 array(  'user_id'=> $user_id,
399                                                 'email'=> $userdata['email']),
400                                 TRUE);
401                         
402                         $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
403                         $this->load->view('main', $main_params);
404                 
405                         $this->load->view('footer');
406                         $this->load->view('html_end');
407                 }
408                 else
409                 {
410                         if ($method == 'code')
411                         {
412                                 // Redirect to a message which tells the user that the
413                                 // activation was successful.
414                                 $this->session->set_flashdata('msg', sprintf(
415                                                 $this->lang->line('user_msg_activated_account'), 
416                                                 site_url('user/login')));
417                                 header('Location: '. site_url('message/info'));
418                                 return;
419                         }
420                         else if ($method == 'resend')
421                         {
422                                 // Redirect to resent message
423                                 $this->session->set_flashdata('msg', sprintf(
424                                                 $this->lang->line('user_msg_activation_resent'),
425                                                 $this->input->post('email')));
426                                 header('Location: '. site_url('message/info'));
427                                 return;
428                         }
429                 }
430         }
431         
432         public function recover_password()
433         {
434                 $this->load->library('form_validation');
435                         
436                 $this->form_validation->set_error_delimiters('<span class="error">',
437                         '</span>');
438
439                 if ($this->form_validation->run('recover_password') === FALSE)
440                 {
441                         $params = array(        'title' =>
442                                                                         $this->lang->line(
443                                                                                 'user_title_password_recovery')
444                                                                                 .' &ndash; '
445                                                                                 . $this->config->item('site_name'),
446                                                                 //'metas' => array('description'=>'')
447                         );
448                         $this->load->library('html_head_params', $params);
449                                 
450                         // **
451                         // ** LOADING VIEWS
452                         // **
453                         $this->load->view('html_begin', $this->html_head_params);
454                         $this->load->view('header', array('selected_menu' => 
455                                         'recover_password'));
456
457                         $main_params['content'] = $this->load->view(
458                                 'user/recover_password_view', array(),
459                                 TRUE);
460                         
461                         $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
462                         $this->load->view('main', $main_params);
463                                 
464                         $this->load->view('footer');
465                         $this->load->view('html_end');
466                 }
467                 else
468                 {
469                         // Redirect to resent message
470                         $this->session->set_flashdata('msg', sprintf(
471                                         $this->lang->line('user_msg_password_recovery_email_sent'),
472                                         $this->input->post('username'),
473                                         $this->input->post('email')));
474                         header('Location: '. site_url('message/info'));
475                         return;
476                 }
477         }
478         
479         public function _update_session_userdata($data)
480         {
481                 foreach ($data as $key=> $val)
482                         $this->session->set_userdata($key, $val);
483         }
484         
485         public function _valid_username($username)
486         {
487                 return (preg_match('/^[a-z0-9\._]+$/', $username) === 1);
488         }
489
490         public function _valid_username_or_email($username)
491         {
492                 $this->load->helper('email');
493
494                 if (valid_email($username))
495                         return TRUE;
496                 else
497                         return $this->_valid_username($username);
498         }
499         
500         public function _valid_date($date)
501         {
502                 if (! $date)
503                         return TRUE;
504                 
505                 return (preg_match('/[\d]{4}-[\d]{2}-[\d]{2}/', $date) === 1);
506         }
507         
508         public function _postprocess_birth_date($date)
509         {
510                 // If the user entered no birth date NULL needs to be inserted into DB.
511                 if (! $date)
512                         return NULL;
513                 
514                 return $date;
515         }
516         
517         public function _valid_old_password($old_password, $field_username)
518         {
519                 if (! $old_password)
520                         return TRUE;
521                 
522                 $username= $this->input->post($field_username);
523                 
524                 if ($this->users_model->login($username, $old_password))
525                         return TRUE;
526                 
527                 return FALSE;
528         }
529         
530         public function _change_password_cond($param)
531         {
532                 $old = $this->input->post('old-password');
533                 $new = $this->input->post('new-password');
534                 $newc = $this->input->post('new-password-confirmation');
535                 
536                 return (!$old && !$new && !$newc)
537                         || ($old && $new && $newc);
538         }
539         
540         public function _required_by_register($param)
541         {
542                 $user_id = $this->input->post('user-id');
543                 
544                 if (! $user_id && ! $param)
545                         return FALSE;
546                 
547                 return TRUE;
548         }
549         
550         public function _valid_activation_code($activation_code)
551         {
552                 return (preg_match('/^[a-fA-F0-9]{16}$/', $activation_code) == 1);
553         }
554
555         public function _do_login($username, $field_password)
556         {
557                 $password = $this->input->post($field_password);
558
559                 $user = $this->users_model->login($username, $password);
560
561                 // Authentication failed.
562                 if ($user === FALSE)
563                         return FALSE;
564                 
565                 // User has not activated the account.
566                 if ($user['activation_code'] !== NULL)
567                 {
568                         $this->activated_account = FALSE;
569                         $this->user_id = $user['id'];
570                         return TRUE;
571                 }
572                 
573                 // Authentication successful: set session with user data.
574                 $this->session->set_userdata(array(
575                         'user_id'=> $user['id'],
576                         'username'=> $user['username'],
577                         'auth_src'=> $user['auth_src'],
578                         'time_zone'=> $user['time_zone']
579                 ));
580                 $this->import = (isset($user['import']) ? $user['import'] : FALSE);
581                 return TRUE;
582         }
583         
584         public function _do_activate($activation_code)
585         {
586                 $user_id = $this->input->post('user-id');
587                 if ($user_id === FALSE)
588                         return FALSE;
589                 $user_id = intval($user_id);
590                 
591                 return $this->users_model->activate_account($user_id,
592                                 $activation_code);
593         }
594         
595         public function _do_resend_activation($email)
596         {
597                 $user_id = $this->input->post('user-id');
598                 if ($user_id === FALSE)
599                         return FALSE;
600                 $user_id = intval($user_id);
601                 
602                 $this->users_model->set_userdata($user_id,
603                         array('email'=> $email));
604                 
605                 return $this->users_model->send_activation_email($user_id, $email);
606         }
607         
608         public function _username_exists($username)
609         {
610                 $userdata = $this->users_model->get_userdata($username);
611                 
612                 if (! $userdata)
613                         return FALSE;
614                 
615                 return TRUE;
616         }
617         
618         public function _internal_account($username)
619         {
620                 $userdata = $this->users_model->get_userdata($username, 'auth_src');
621                 if (! $userdata)
622                         return FALSE;
623
624                 if ($userdata['auth_src'] != 'internal')
625                         return FALSE;
626                 
627                 return TRUE;
628         }
629         
630         public function _do_recover_password($username)
631         {
632                 $email = $this->input->post('email');
633                 if (! $email)
634                         return FALSE;
635                 
636                 return $this->users_model->recover_password($username, $email);
637         }
638 }
639
640 /* End of file user.php */
641 /* Location: ./application/controllers/user.php */