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 ($this->users_model->get_userdata('calin.burloiu') ? 'd' : 'n');
33 * Login a user and then redirect it to the last page which must be encoded
36 * @param string $redirect contains the last page URI segments encoded
37 * with helper url_encode_segments.
39 public function login($redirect = '')
41 $this->load->library('form_validation');
42 $this->form_validation->set_error_delimiters('<span class="error">',
45 // Normal or OpenID login?
46 if ($this->input->post('openid') !== FALSE)
50 // Validate the correct form.
51 $res_form_validation = FALSE;
53 $res_form_validation = $this->form_validation->run('login');
55 $res_form_validation = $this->form_validation->run('login_openid');
57 if ($res_form_validation === FALSE)
59 $params = array( 'title' =>
60 $this->lang->line('ui_nav_menu_login')
62 . $this->config->item('site_name'),
63 //'metas' => array('description'=>'')
65 $this->load->library('html_head_params', $params);
70 $this->load->view('html_begin', $this->html_head_params);
71 $this->load->view('header', array('selected_menu' => 'login'));
73 $main_params['content'] = $this->load->view('user/login_view',
74 array('redirect'=> $redirect), TRUE);
75 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
76 $this->load->view('main', $main_params);
78 $this->load->view('footer');
79 $this->load->view('html_end');
85 $this->users_model->openid_begin_login(
86 $this->input->post('openid'));
91 if (! $this->activated_account)
93 . site_url("user/activate/{$this->user_id}"));
94 else if (! $this->import)
96 // Redirect to last page before login.
97 header('Location: '. site_url(urldecode_segments($redirect)));
101 // Redirect to account page because an user authenticates here
102 // for the first time with external authentication. The page
103 // will display imported data.
104 header('Location: '. site_url('user/account'));
109 public function check_openid_login()
111 $user = $this->users_model->openid_complete_login();
113 // Authentication failed.
114 if ($user == Auth_OpenID_CANCEL)
116 $this->load->helper('message');
117 show_error_msg_page($this, $this->lang->line('openid_cancel'));
120 else if ($user == Auth_OpenID_FAILURE)
122 $this->load->helper('message');
123 show_error_msg_page($this, $this->lang->line('openid_failure'));
127 // Authentication successful: set session with user data.
128 $this->session->set_userdata(array(
129 'user_id'=> $user['id'],
130 'username'=> $user['username'],
131 'auth_src'=> $user['auth_src'],
132 'time_zone'=> $user['time_zone']
136 header('Location: '. site_url('user/account'));
138 header('Location: '. site_url());
141 public function openid_policy()
143 $this->load->view('openid_policy_view');
147 * Logout user and then redirect it to the last page which must be encoded
150 * @param string $redirect contains the last page URI segments encoded
151 * with helper url_encode_segments.
153 public function logout($redirect = '')
155 $this->session->unset_userdata('user_id');
156 $this->session->unset_userdata('username');
157 $this->session->unset_userdata('auth_src');
158 $this->session->unset_userdata('time_zone');
160 header('Location: '. site_url(urldecode_segments($redirect)));
163 public function register($redirect = '')
165 $this->load->library('form_validation');
166 $this->load->helper('localization');
167 $this->load->helper('date');
169 $this->form_validation->set_error_delimiters('<span class="error">',
173 if ($this->form_validation->run('register'))
175 $b_validation = TRUE;
177 if ($_FILES['picture']['tmp_name'])
180 $config_upload['upload_path'] = './data/user_pictures';
181 $config_upload['file_name'] =
182 str_replace('.', '-', $this->input->post('username')) .'-';
183 $config_upload['allowed_types'] = 'gif|jpg|png';
184 $config_upload['max_size'] = '10240';
185 $this->load->library('upload', $config_upload);
187 $b_validation = $this->upload->do_upload('picture');
189 $this->upload->display_errors('<span class="error">',
194 $b_validation = FALSE;
198 // Edit account data if logged in, otherwise register.
199 if ($user_id = $this->session->userdata('user_id'))
201 $userdata = $this->users_model->get_userdata(intval($user_id));
202 $selected_menu = 'account';
207 $selected_menu = 'register';
210 $params = array('title' =>
211 $this->lang->line('ui_nav_menu_register')
213 . $this->config->item('site_name'),
214 //'metas' => array('description'=>'')
216 $this->load->library('html_head_params', $params);
221 $this->load->view('html_begin', $this->html_head_params);
222 $this->load->view('header',
223 array('selected_menu' => $selected_menu));
225 $main_params['content'] = $this->load->view('user/register_view',
226 array('userdata'=> $userdata, 'redirect'=> $redirect,
227 'error_upload'=> $error_upload),
229 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
230 $this->load->view('main', $main_params);
232 $this->load->view('footer');
233 $this->load->view('html_end');
237 $user_id = $this->input->post('user-id');
238 $data['email'] = $this->input->post('email');
239 $data['first_name'] = $this->input->post('first-name');
240 $data['last_name'] = $this->input->post('last-name');
241 $data['birth_date'] = $this->input->post('birth-date');
242 $data['country'] = $this->input->post('country');
243 $data['locality'] = $this->input->post('locality');
244 $data['ui_lang'] = $this->input->post('ui-lang');
245 $data['time_zone'] = $this->input->post('time-zone');
247 // Handle picture if one was uploaded.
248 if ($_FILES['picture']['tmp_name'])
250 $upload_data = $this->upload->data();
251 $this->load->library('image');
252 $this->image->load($upload_data['full_path']);
253 // Resize original to a maximum size.
254 if ($this->image->get_width() * $this->image->get_height()
257 $this->image->save_thumbnail(
258 $upload_data['full_path'],
259 640, 480, IMAGETYPE_AUTO);
262 $data['picture'] = $upload_data['file_name'];
263 $this->image->save_thumbnail($upload_data['file_path']
264 . $upload_data['file_name']. '-thumb.jpg', 120, 90);
267 // Update session user data.
268 $this->_update_session_userdata($data);
273 $password = $this->input->post('new-password');
275 $data['password'] = $this->input->post('new-password');
277 $this->users_model->set_userdata($user_id, $data);
279 // Redirect to last page before login.
280 header('Location: '. site_url(urldecode_segments($redirect)));
285 $data['username'] = $this->input->post('username');
286 $data['password'] = $this->input->post('password');
288 $this->users_model->register($data);
289 $user_id = $this->users_model->get_userdata($data['username'],
291 $user_id = $user_id['id'];
293 // Redirect account activation page.
294 header('Location: '. site_url("user/activate/$user_id"));
299 public function account($redirect = '')
301 $this->register($redirect);
304 public function profile($username, $videos_offset = 0)
306 // TODO handle user not found
308 $this->load->config('localization');
309 $this->load->helper('date');
310 $this->lang->load('date');
315 // Logged in user time zone
316 $time_zone = $this->session->userdata('time_zone');
319 $userdata = $this->users_model->get_userdata($username);
320 $userdata['roles'] = Users_model::roles_to_string($userdata['roles']);
321 $country_list = $this->config->item('country_list');
322 $userdata['country_name'] = $country_list[ $userdata['country'] ];
323 $userdata['last_login'] = human_gmt_to_human_local(
324 $userdata['last_login'], $time_zone);
325 $userdata['time_zone'] = $this->lang->line($userdata['time_zone']);
328 $this->load->model('videos_model');
329 $vs_data['videos'] = $this->videos_model->get_videos_summary(
330 NULL, $username, intval($videos_offset),
331 $this->config->item('videos_per_page'));
334 $this->load->library('pagination');
335 $pg_config['base_url'] = site_url("user/profile/$username/");
336 $pg_config['uri_segment'] = 4;
337 $pg_config['total_rows'] = $this->videos_model->get_videos_count(
339 $pg_config['per_page'] = $this->config->item('videos_per_page');
340 $this->pagination->initialize($pg_config);
341 $vs_data['pagination'] = $this->pagination->create_links();
342 $vs_data['title'] = NULL;
343 $vs_data['category_name'] = ''; // TODO videos_summary with AJAX
346 'title'=> $this->lang->line('user_appelation').' '.$username
348 . $this->config->item('site_name'),
349 'css'=> array('catalog.css')
350 //'metas' => array('description'=>'')
352 $this->load->library('html_head_params', $params);
354 // Current user profile tab
355 $tab = (! $videos_offset ? 0 : 1);
360 $this->load->view('html_begin', $this->html_head_params);
361 $this->load->view('header', array());
363 $vs = $this->load->view('catalog/videos_summary_view', $vs_data, TRUE);
365 $main_params['content'] = $this->load->view('user/profile_view',
366 array('userdata'=> $userdata, 'videos_summary'=> $vs, 'tab'=>$tab),
368 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
369 $this->load->view('main', $main_params);
371 $this->load->view('footer');
372 $this->load->view('html_end');
375 public function activate($user_id, $method='', $activation_code='')
377 $user_id = intval($user_id);
378 $res_form_validation = FALSE;
380 if ($method == 'code')
382 if (! $activation_code)
383 $res_form_validation = $this->form_validation->run('activate');
384 // Activation code is provided in URL.
387 if ($this->_valid_activation_code($activation_code)
388 && $this->users_model->activate_account($user_id,
391 $this->load->helper('message');
392 show_info_msg_page($this, sprintf(
393 $this->lang->line('user_msg_activated_account'),
394 site_url('user/login')));
399 $this->load->helper('message');
400 show_error_msg_page($this,
402 'user_msg_wrong_activation_code'));
407 else if ($method == 'resend')
409 $res_form_validation =
410 $this->form_validation->run('resend_activation');
413 $userdata = $this->users_model->get_userdata($user_id,
414 'email, a.activation_code');
415 $email = $userdata['email'];
416 $activated_account = ($userdata['activation_code'] == NULL);
418 if ($activated_account)
420 $this->load->helper('message');
421 show_info_msg_page($this, sprintf(
422 $this->lang->line('user_msg_activated_account'),
423 site_url('user/login')));
427 $this->load->library('form_validation');
429 $this->form_validation->set_error_delimiters('<span class="error">',
432 if ($res_form_validation === FALSE)
435 'title'=> $this->lang->line('user_title_activation')
437 . $this->config->item('site_name'),
438 //'metas' => array('description'=>'')
440 $this->load->library('html_head_params', $params);
445 $this->load->view('html_begin', $this->html_head_params);
446 $this->load->view('header', array());
449 $main_params['content'] =
450 $this->load->view('user/activate_view',
451 array( 'user_id'=> $user_id,
452 'email'=> $userdata['email']),
455 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
456 $this->load->view('main', $main_params);
458 $this->load->view('footer');
459 $this->load->view('html_end');
463 if ($method == 'code')
465 // A message which tells the user that the
466 // activation was successful.
467 $this->load->helper('message');
468 show_info_msg_page($this, sprintf(
469 $this->lang->line('user_msg_activated_account'),
470 site_url('user/login')));
473 else if ($method == 'resend')
475 // Redirect to resent message
476 $this->load->helper('message');
477 show_info_msg_page($this, sprintf(
478 $this->lang->line('user_msg_activation_resent'),
479 $this->input->post('email')));
485 public function recover_password()
487 $this->load->library('form_validation');
489 $this->form_validation->set_error_delimiters('<span class="error">',
492 if ($this->form_validation->run('recover_password') === FALSE)
494 $params = array( 'title' =>
496 'user_title_password_recovery')
498 . $this->config->item('site_name'),
499 //'metas' => array('description'=>'')
501 $this->load->library('html_head_params', $params);
506 $this->load->view('html_begin', $this->html_head_params);
507 $this->load->view('header', array('selected_menu' =>
508 'recover_password'));
510 $main_params['content'] = $this->load->view(
511 'user/recover_password_view', array(),
514 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
515 $this->load->view('main', $main_params);
517 $this->load->view('footer');
518 $this->load->view('html_end');
523 $this->load->helper('message');
524 show_info_msg_page($this, sprintf(
525 $this->lang->line('user_msg_password_recovery_email_sent'),
526 $this->input->post('username'),
527 $this->input->post('email')));
532 public function _format_message($msg, $val = '', $sub = '%s')
534 return str_replace($sub, $val, $this->lang->line($msg));
537 public function _update_session_userdata($data)
539 foreach ($data as $key=> $val)
540 $this->session->set_userdata($key, $val);
543 public function _valid_username($username)
545 return (preg_match('/^[a-z0-9\._]+$/', $username) === 1);
548 public function _valid_username_or_email($username)
550 $this->load->helper('email');
552 if (valid_email($username))
555 return $this->_valid_username($username);
558 public function _valid_date($date)
563 return (preg_match('/[\d]{4}-[\d]{2}-[\d]{2}/', $date) === 1);
566 public function _postprocess_birth_date($date)
568 // If the user entered no birth date NULL needs to be inserted into DB.
575 public function _valid_old_password($old_password, $field_username)
580 $username= $this->input->post($field_username);
582 if ($this->users_model->login($username, $old_password))
588 public function _change_password_cond($param)
590 $old = $this->input->post('old-password');
591 $new = $this->input->post('new-password');
592 $newc = $this->input->post('new-password-confirmation');
594 return (!$old && !$new && !$newc)
595 || ($old && $new && $newc);
598 public function _required_by_register($param)
600 $user_id = $this->input->post('user-id');
602 if (! $user_id && ! $param)
608 public function _valid_activation_code($activation_code)
610 return (preg_match('/^[a-fA-F0-9]{16}$/', $activation_code) == 1);
613 public function _do_login($username, $field_password)
615 $password = $this->input->post($field_password);
617 $user = $this->users_model->login($username, $password);
619 // Authentication failed.
623 // User has not activated the account.
624 if ($user['activation_code'] !== NULL)
626 $this->activated_account = FALSE;
627 $this->user_id = $user['id'];
631 // Authentication successful: set session with user data.
632 $this->session->set_userdata(array(
633 'user_id'=> $user['id'],
634 'username'=> $user['username'],
635 'auth_src'=> $user['auth_src'],
636 'time_zone'=> $user['time_zone']
638 $this->import = (isset($user['import']) ? $user['import'] : FALSE);
642 public function _do_activate($activation_code)
644 $user_id = $this->input->post('user-id');
645 if ($user_id === FALSE)
647 $user_id = intval($user_id);
649 return $this->users_model->activate_account($user_id,
653 public function _do_resend_activation($email)
655 $user_id = $this->input->post('user-id');
656 if ($user_id === FALSE)
658 $user_id = intval($user_id);
660 $this->users_model->set_userdata($user_id,
661 array('email'=> $email));
663 return $this->users_model->send_activation_email($user_id, $email);
666 public function _username_exists($username)
668 $userdata = $this->users_model->get_userdata($username);
676 public function _internal_account($username)
678 $userdata = $this->users_model->get_userdata($username, 'auth_src');
682 if ($userdata['auth_src'] != 'internal')
688 public function _do_recover_password($username)
690 $email = $this->input->post('email');
694 return $this->users_model->recover_password($username, $email);
698 /* End of file user.php */
699 /* Location: ./application/controllers/user.php */