4 * Class User controls video hierarchy and searching
7 * @author Călin-Andrei Burloiu
9 class User extends CI_Controller {
11 private $import = FALSE;
12 private $activated_account = TRUE;
13 private $user_id = NULL;
15 public function __construct()
17 parent::__construct();
19 $this->lang->load('user');
20 $this->load->model('users_model');
23 public function index()
27 public function test($user_id = 1)
29 echo extension_loaded('gd') ? 'gd' : 'nu';
33 public function show_session()
35 if (ENVIRONMENT == 'production')
38 var_dump($this->session->all_userdata());
41 public function destroy_session()
43 if (ENVIRONMENT == 'production')
46 $this->session->sess_destroy();
49 public function ajax_get_captcha()
51 $this->load->library('captcha');
52 $captcha = $this->captcha->get_captcha();
53 echo $captcha['image'];
57 * Login a user and then redirect it to the last page which must be encoded
60 * @param string $redirect contains the last page URI segments encoded
61 * with helper url_encode_segments.
63 public function login($redirect = '')
65 $this->load->library('form_validation');
66 $this->form_validation->set_error_delimiters('<span class="error">',
69 // Normal or OpenID login?
70 if ($this->input->post('openid') !== FALSE)
74 // Validate the correct form.
75 $res_form_validation = FALSE;
77 $res_form_validation = $this->form_validation->run('login');
79 $res_form_validation = $this->form_validation->run('login_openid');
81 if ($res_form_validation === FALSE)
83 $params = array( 'title' =>
84 $this->lang->line('ui_nav_menu_login')
86 . $this->config->item('site_name'),
87 //'metas' => array('description'=>'')
89 $this->load->library('html_head_params', $params);
94 $this->load->view('html_begin', $this->html_head_params);
95 $this->load->view('header', array('selected_menu' => 'login'));
97 $main_params['content'] = $this->load->view('user/login_view',
98 array('redirect'=> $redirect), TRUE);
99 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
100 $this->load->view('main', $main_params);
102 $this->load->view('footer');
103 $this->load->view('html_end');
109 $this->users_model->openid_begin_login(
110 $this->input->post('openid'));
115 if (! $this->activated_account)
117 . site_url("user/activate/{$this->user_id}"));
118 else if (! $this->import)
120 // Redirect to last page before login.
121 header('Location: '. site_url(urldecode_segments($redirect)));
125 // Redirect to account page because an user authenticates here
126 // for the first time with external authentication. The page
127 // will display imported data.
128 header('Location: '. site_url('user/account'));
133 public function check_openid_login()
135 $user = $this->users_model->openid_complete_login();
137 // Authentication failed.
138 if ($user == Auth_OpenID_CANCEL)
140 $this->load->helper('message');
141 show_error_msg_page($this, $this->lang->line('openid_cancel'));
144 else if ($user == Auth_OpenID_FAILURE)
146 $this->load->helper('message');
147 show_error_msg_page($this, $this->lang->line('openid_failure'));
151 // Authentication successful: set session with user data.
152 $this->session->set_userdata(array(
153 'user_id'=> $user['id'],
154 'username'=> $user['username'],
155 'auth_src'=> $user['auth_src'],
156 'time_zone'=> $user['time_zone']
160 header('Location: '. site_url('user/account'));
162 header('Location: '. site_url());
165 public function openid_policy()
167 $this->load->view('openid_policy_view');
171 * Logout user and then redirect it to the last page which must be encoded
174 * @param string $redirect contains the last page URI segments encoded
175 * with helper url_encode_segments.
177 public function logout($redirect = '')
179 $this->session->unset_userdata('user_id');
180 $this->session->unset_userdata('username');
181 $this->session->unset_userdata('auth_src');
182 $this->session->unset_userdata('time_zone');
184 header('Location: '. site_url(urldecode_segments($redirect)));
187 public function register($redirect = '')
189 $this->load->library('form_validation');
190 $this->load->helper('localization');
191 $this->load->helper('date');
193 $user_id = $this->session->userdata('user_id');
195 $this->form_validation->set_error_delimiters('<span class="error">',
199 if ($this->form_validation->run('register'))
201 $b_validation = TRUE;
203 if ($_FILES['picture']['tmp_name'])
206 $config_upload['upload_path'] = './data/user_pictures';
207 $config_upload['file_name'] =
208 str_replace('.', '-', $this->input->post('username')) .'-';
209 $config_upload['allowed_types'] = 'gif|jpg|png';
210 $config_upload['max_size'] = '10240';
211 $this->load->library('upload', $config_upload);
213 $b_validation = $this->upload->do_upload('picture');
215 $this->upload->display_errors('<span class="error">',
220 $b_validation = FALSE;
224 // Edit account data if logged in, otherwise register.
228 $userdata = $this->users_model->get_userdata(intval($user_id));
229 if (substr($userdata['username'], 0, 8) == 'autogen_')
230 $userdata['autogen_username'] =
231 substr($userdata['username'], 8);
232 $selected_menu = 'account';
239 $selected_menu = 'register';
242 $this->load->library('captcha');
243 $captcha = $this->captcha->get_captcha();
244 $captcha = $captcha['image'];
247 $params = array('title' =>
248 $this->lang->line('ui_nav_menu_register')
250 . $this->config->item('site_name'),
251 //'metas' => array('description'=>'')
253 $this->load->library('html_head_params', $params);
258 $this->load->view('html_begin', $this->html_head_params);
259 $this->load->view('header',
260 array('selected_menu' => $selected_menu));
262 $main_params['content'] = $this->load->view('user/register_view',
263 array('userdata'=> $userdata, 'redirect'=> $redirect,
264 'error_upload'=> $error_upload, 'captcha'=> $captcha),
266 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
267 $this->load->view('main', $main_params);
269 $this->load->view('footer');
270 $this->load->view('html_end');
274 // TODO: Security problem!
275 //$user_id = $this->input->post('user-id');
276 if ($this->input->post('username'))
277 $data['username'] = $this->input->post('username');
278 $data['email'] = $this->input->post('email');
279 $data['first_name'] = $this->input->post('first-name');
280 $data['last_name'] = $this->input->post('last-name');
281 $data['sex'] = intval($this->input->post('sex'));
282 $data['birth_date'] = $this->input->post('birth-date');
283 $data['country'] = $this->input->post('country');
284 $data['locality'] = $this->input->post('locality');
285 $data['ui_lang'] = $this->input->post('ui-lang');
286 $data['time_zone'] = $this->input->post('time-zone');
288 // Handle picture if one was uploaded.
289 if ($_FILES['picture']['tmp_name'])
291 $upload_data = $this->upload->data();
292 $this->load->library('image');
293 $this->image->load($upload_data['full_path']);
294 // Resize original to a maximum size.
295 if ($this->image->get_width() * $this->image->get_height()
298 $this->image->save_thumbnail(
299 $upload_data['full_path'],
300 640, 480, IMAGETYPE_AUTO);
303 $data['picture'] = $upload_data['file_name'];
304 $this->image->save_thumbnail($upload_data['file_path']
305 . $upload_data['file_name']. '-thumb.jpg', 120, 90);
308 // TODO: To much info as session data?
309 // Update session user data.
310 $this->_update_session_userdata($data);
315 $password = $this->input->post('new-password');
317 $data['password'] = $password;
319 $this->users_model->set_userdata($user_id, $data);
321 // Redirect to last page before login.
322 header('Location: '. site_url(urldecode_segments($redirect)));
327 $data['username'] = $this->input->post('username');
328 $data['password'] = $this->input->post('password');
329 $data['auth_src'] = 'internal';
331 $this->users_model->register($data);
332 $user_id = $this->users_model->get_userdata($data['username'],
334 $user_id = $user_id['id'];
336 // Redirect account activation page.
337 header('Location: '. site_url("user/activate/$user_id"));
342 public function account($redirect = '')
344 $this->register($redirect);
347 public function profile($username, $videos_offset = 0)
349 // TODO handle user not found
351 $this->load->config('localization');
352 $this->load->helper('date');
353 $this->lang->load('date');
358 // Logged in user time zone
359 $time_zone = $this->session->userdata('time_zone');
362 $userdata = $this->users_model->get_userdata($username);
363 $userdata['roles'] = Users_model::roles_to_string($userdata['roles']);
364 $country_list = $this->config->item('country_list');
365 $userdata['country_name'] = $country_list[ $userdata['country'] ];
366 $userdata['last_login'] = human_gmt_to_human_local(
367 $userdata['last_login'], $time_zone);
368 $userdata['time_zone'] = $this->lang->line($userdata['time_zone']);
371 $this->load->model('videos_model');
372 $vs_data['videos'] = $this->videos_model->get_videos_summary(
373 NULL, $username, intval($videos_offset),
374 $this->config->item('videos_per_page'));
377 $this->load->library('pagination');
378 $pg_config['base_url'] = site_url("user/profile/$username/");
379 $pg_config['uri_segment'] = 4;
380 $pg_config['total_rows'] = $this->videos_model->get_videos_count(
382 $pg_config['per_page'] = $this->config->item('videos_per_page');
383 $this->pagination->initialize($pg_config);
384 $vs_data['pagination'] = $this->pagination->create_links();
385 $vs_data['title'] = NULL;
386 $vs_data['category_name'] = ''; // TODO videos_summary with AJAX
389 'title'=> $this->lang->line('user_appelation').' '.$username
391 . $this->config->item('site_name'),
392 'css'=> array('catalog.css')
393 //'metas' => array('description'=>'')
395 $this->load->library('html_head_params', $params);
397 // Current user profile tab
398 $tab = (! $videos_offset ? 0 : 1);
403 $this->load->view('html_begin', $this->html_head_params);
404 $this->load->view('header', array());
406 $vs = $this->load->view('catalog/videos_summary_view', $vs_data, TRUE);
408 $main_params['content'] = $this->load->view('user/profile_view',
409 array('userdata'=> $userdata, 'videos_summary'=> $vs, 'tab'=>$tab),
411 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
412 $this->load->view('main', $main_params);
414 $this->load->view('footer');
415 $this->load->view('html_end');
418 public function activate($user_id, $method='', $activation_code='')
420 $user_id = intval($user_id);
421 $res_form_validation = FALSE;
423 if ($method == 'code')
425 if (! $activation_code)
426 $res_form_validation = $this->form_validation->run('activate');
427 // Activation code is provided in URL.
430 if ($this->_valid_activation_code($activation_code)
431 && $this->users_model->activate_account($user_id,
434 $this->load->helper('message');
435 show_info_msg_page($this, sprintf(
436 $this->lang->line('user_msg_activated_account'),
437 site_url('user/login')));
442 $this->load->helper('message');
443 show_error_msg_page($this,
445 'user_msg_wrong_activation_code'));
450 else if ($method == 'resend')
452 $res_form_validation =
453 $this->form_validation->run('resend_activation');
456 $userdata = $this->users_model->get_userdata($user_id,
457 'email, a.activation_code');
458 $email = $userdata['email'];
459 $activated_account = ($userdata['activation_code'] == NULL);
461 if ($activated_account)
463 $this->load->helper('message');
464 show_info_msg_page($this, sprintf(
465 $this->lang->line('user_msg_activated_account'),
466 site_url('user/login')));
470 $this->load->library('form_validation');
472 $this->form_validation->set_error_delimiters('<span class="error">',
475 if ($res_form_validation === FALSE)
478 'title'=> $this->lang->line('user_title_activation')
480 . $this->config->item('site_name'),
481 //'metas' => array('description'=>'')
483 $this->load->library('html_head_params', $params);
488 $this->load->view('html_begin', $this->html_head_params);
489 $this->load->view('header', array());
492 $main_params['content'] =
493 $this->load->view('user/activate_view',
494 array( 'user_id'=> $user_id,
495 'email'=> $userdata['email']),
498 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
499 $this->load->view('main', $main_params);
501 $this->load->view('footer');
502 $this->load->view('html_end');
506 if ($method == 'code')
508 // A message which tells the user that the
509 // activation was successful.
510 $this->load->helper('message');
511 show_info_msg_page($this, sprintf(
512 $this->lang->line('user_msg_activated_account'),
513 site_url('user/login')));
516 else if ($method == 'resend')
518 // Redirect to resent message
519 $this->load->helper('message');
520 show_info_msg_page($this, sprintf(
521 $this->lang->line('user_msg_activation_resent'),
522 $this->input->post('email')));
528 public function recover_password()
530 $this->load->library('form_validation');
532 $this->form_validation->set_error_delimiters('<span class="error">',
535 if ($this->form_validation->run('recover_password') === FALSE)
537 $params = array( 'title' =>
539 'user_title_password_recovery')
541 . $this->config->item('site_name'),
542 //'metas' => array('description'=>'')
544 $this->load->library('html_head_params', $params);
549 $this->load->view('html_begin', $this->html_head_params);
550 $this->load->view('header', array('selected_menu' =>
551 'recover_password'));
553 $main_params['content'] = $this->load->view(
554 'user/recover_password_view', array(),
557 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
558 $this->load->view('main', $main_params);
560 $this->load->view('footer');
561 $this->load->view('html_end');
566 $this->load->helper('message');
567 show_info_msg_page($this, sprintf(
568 $this->lang->line('user_msg_password_recovery_email_sent'),
569 $this->input->post('username'),
570 $this->input->post('email')));
575 public function _format_message($msg, $val = '', $sub = '%s')
577 return str_replace($sub, $val, $this->lang->line($msg));
580 public function _update_session_userdata($data)
582 foreach ($data as $key=> $val)
584 if ($this->session->userdata($key))
585 $this->session->set_userdata($key, $val);
589 public function _is_username_unique($username)
591 if ($this->users_model->get_userdata($username))
597 public function _valid_username($username)
599 return (preg_match('/^[a-z0-9\._]+$/', $username) === 1);
602 public function _valid_username_or_email($username)
604 $this->load->helper('email');
606 if (valid_email($username))
609 return $this->_valid_username($username);
612 public function _valid_date($date)
617 return (preg_match('/[\d]{4}-[\d]{2}-[\d]{2}/', $date) === 1);
620 public function _postprocess_birth_date($date)
622 // If the user entered no birth date NULL needs to be inserted into DB.
629 public function _valid_old_password($old_password)
634 $username= $this->session->userdata('username');
636 if ($this->users_model->login($username, $old_password))
642 public function _change_password_cond($param)
644 $old = $this->input->post('old-password');
645 $new = $this->input->post('new-password');
646 $newc = $this->input->post('new-password-confirmation');
648 return (!$old && !$new && !$newc)
649 || ($old && $new && $newc);
652 public function _required_by_register($param)
654 $user_id = $this->session->userdata('user_id');
656 if (! $user_id && ! $param)
662 public function _valid_activation_code($activation_code)
664 return (preg_match('/^[a-fA-F0-9]{16}$/', $activation_code) == 1);
667 public function _do_login($username, $field_password)
669 $password = $this->input->post($field_password);
671 $user = $this->users_model->login($username, $password);
673 // Authentication failed.
677 // User has not activated the account.
678 if ($user['activation_code'] !== NULL)
680 $this->activated_account = FALSE;
681 $this->user_id = $user['id'];
685 // Authentication successful: set session with user data.
686 $this->session->set_userdata(array(
687 'user_id'=> $user['id'],
688 'username'=> $user['username'],
689 'auth_src'=> $user['auth_src'],
690 'time_zone'=> $user['time_zone']
692 $this->import = (isset($user['import']) ? $user['import'] : FALSE);
696 public function _do_activate($activation_code)
698 $user_id = $this->input->post('user-id');
699 if ($user_id === FALSE)
701 $user_id = intval($user_id);
703 return $this->users_model->activate_account($user_id,
707 public function _do_resend_activation($email)
709 $user_id = $this->input->post('user-id');
710 if ($user_id === FALSE)
712 $user_id = intval($user_id);
714 $this->users_model->set_userdata($user_id,
715 array('email'=> $email));
717 return $this->users_model->send_activation_email($user_id, $email);
720 public function _username_exists($username)
722 $userdata = $this->users_model->get_userdata($username);
730 public function _check_captcha($word)
732 $this->load->library('captcha');
734 return $this->captcha->check_captcha($word);
737 public function _internal_account($username)
739 $userdata = $this->users_model->get_userdata($username, 'auth_src');
743 if ($userdata['auth_src'] != 'internal')
749 public function _do_recover_password($username)
751 $email = $this->input->post('email');
755 return $this->users_model->recover_password($username, $email);
759 /* End of file user.php */
760 /* Location: ./application/controllers/user.php */