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)
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->lang->load('openid');
86 $this->load->library('openid');
87 $this->config->load('openid');
88 $request_to = site_url('user/check_openid_login');
89 $req = $this->config->item('openid_required');
90 $opt = $this->config->item('openid_optional');
91 $policy = site_url('user/openid_policy');
92 $pape_policy_uris = $this->config->item('openid_papa_policies');
94 $this->openid->set_request_to($request_to);
95 $this->openid->set_trust_root(base_url());
96 $this->openid->set_args(null);
97 $this->openid->set_sreg(true, $req, $opt, $policy);
98 if (!empty($pape_policy_uris))
99 $this->openid->set_pape(true, $pape_policy_uris);
101 // Redirection to OP site will follow.
102 $this->openid->authenticate($this->input->post('openid'));
107 if (! $this->activated_account)
109 . site_url("user/activate/{$this->user_id}"));
110 else if (! $this->import)
112 // Redirect to last page before login.
113 header('Location: '. site_url(urldecode_segments($redirect)));
117 // Redirect to account page because an user authenticates here
118 // for the first time with external authentication. The page
119 // will display imported data.
120 header('Location: '. site_url('user/account'));
125 public function check_openid_login()
127 $this->lang->load('openid');
128 $this->load->library('openid');
129 $this->config->load('openid');
130 $request_to = site_url('user/check_openid_login');
132 $this->openid->set_request_to($request_to);
133 $response = $this->openid->get_response();
135 switch ($response->status)
137 case Auth_OpenID_CANCEL:
138 $this->load->helper('message');
139 show_info_msg_page($this, $this->lang->line('openid_cancel'));
141 case Auth_OpenID_FAILURE:
142 $this->load->helper('message');
143 show_error_msg_page($this,
144 $this->_format_message('openid_failure',
145 $response->message));
147 case Auth_OpenID_SUCCESS:
148 $openid = $response->getDisplayIdentifier();
149 $esc_identity = htmlspecialchars($openid, ENT_QUOTES);
151 $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
152 $sreg = $sreg_resp->contents();
154 // Get registration informations
155 $ax = new Auth_OpenID_AX_FetchResponse();
156 $obj = $ax->fromSuccessResponse($response);
158 //echo 'nickname('. $sreg_resp->get('nickname'). ')';
159 echo var_dump($obj->data);
160 // foreach ($sreg as $key => $value)
162 // $data['success'] .= $this->_set_message('openid_content', array($key, $value), array('%s', '%t'));
169 public function openid_policy()
171 $this->load->view('openid_policy_view');
175 * Logout user and then redirect it to the last page which must be encoded
178 * @param string $redirect contains the last page URI segments encoded
179 * with helper url_encode_segments.
181 public function logout($redirect = '')
183 $this->session->unset_userdata('user_id');
184 $this->session->unset_userdata('username');
185 $this->session->unset_userdata('auth_src');
186 $this->session->unset_userdata('time_zone');
188 header('Location: '. site_url(urldecode_segments($redirect)));
191 public function register($redirect = '')
193 $this->load->library('form_validation');
194 $this->load->helper('localization');
195 $this->load->helper('date');
197 $this->form_validation->set_error_delimiters('<span class="error">',
201 if ($this->form_validation->run('register'))
203 $b_validation = TRUE;
205 if ($_FILES['picture']['tmp_name'])
208 $config_upload['upload_path'] = './data/user_pictures';
209 $config_upload['file_name'] =
210 str_replace('.', '-', $this->input->post('username')) .'-';
211 $config_upload['allowed_types'] = 'gif|jpg|png';
212 $config_upload['max_size'] = '10240';
213 $this->load->library('upload', $config_upload);
215 $b_validation = $this->upload->do_upload('picture');
217 $this->upload->display_errors('<span class="error">',
222 $b_validation = FALSE;
226 // Edit account data if logged in, otherwise register.
227 if ($user_id = $this->session->userdata('user_id'))
229 $userdata = $this->users_model->get_userdata(intval($user_id));
230 $selected_menu = 'account';
235 $selected_menu = 'register';
238 $params = array('title' =>
239 $this->lang->line('ui_nav_menu_register')
241 . $this->config->item('site_name'),
242 //'metas' => array('description'=>'')
244 $this->load->library('html_head_params', $params);
249 $this->load->view('html_begin', $this->html_head_params);
250 $this->load->view('header',
251 array('selected_menu' => $selected_menu));
253 $main_params['content'] = $this->load->view('user/register_view',
254 array('userdata'=> $userdata, 'redirect'=> $redirect,
255 'error_upload'=> $error_upload),
257 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
258 $this->load->view('main', $main_params);
260 $this->load->view('footer');
261 $this->load->view('html_end');
265 $user_id = $this->input->post('user-id');
266 $data['email'] = $this->input->post('email');
267 $data['first_name'] = $this->input->post('first-name');
268 $data['last_name'] = $this->input->post('last-name');
269 $data['birth_date'] = $this->input->post('birth-date');
270 $data['country'] = $this->input->post('country');
271 $data['locality'] = $this->input->post('locality');
272 $data['ui_lang'] = $this->input->post('ui-lang');
273 $data['time_zone'] = $this->input->post('time-zone');
275 // Handle picture if one was uploaded.
276 if ($_FILES['picture']['tmp_name'])
278 $upload_data = $this->upload->data();
279 $this->load->library('image');
280 $this->image->load($upload_data['full_path']);
281 // Resize original to a maximum size.
282 if ($this->image->get_width() * $this->image->get_height()
285 $this->image->save_thumbnail(
286 $upload_data['full_path'],
287 640, 480, IMAGETYPE_AUTO);
290 $data['picture'] = $upload_data['file_name'];
291 $this->image->save_thumbnail($upload_data['file_path']
292 . $upload_data['file_name']. '-thumb.jpg', 120, 90);
295 // Update session user data.
296 $this->_update_session_userdata($data);
301 $password = $this->input->post('new-password');
303 $data['password'] = $this->input->post('new-password');
305 $this->users_model->set_userdata($user_id, $data);
307 // Redirect to last page before login.
308 header('Location: '. site_url(urldecode_segments($redirect)));
313 $data['username'] = $this->input->post('username');
314 $data['password'] = $this->input->post('password');
316 $this->users_model->register($data);
317 $user_id = $this->users_model->get_userdata($data['username'],
319 $user_id = $user_id['id'];
321 // Redirect account activation page.
322 header('Location: '. site_url("user/activate/$user_id"));
327 public function account($redirect = '')
329 $this->register($redirect);
332 public function profile($username, $videos_offset = 0)
334 // TODO handle user not found
336 $this->load->config('localization');
337 $this->load->helper('date');
338 $this->lang->load('date');
343 // Logged in user time zone
344 $time_zone = $this->session->userdata('time_zone');
347 $userdata = $this->users_model->get_userdata($username);
348 $userdata['roles'] = Users_model::roles_to_string($userdata['roles']);
349 $country_list = $this->config->item('country_list');
350 $userdata['country_name'] = $country_list[ $userdata['country'] ];
351 $userdata['last_login'] = human_gmt_to_human_local(
352 $userdata['last_login'], $time_zone);
353 $userdata['time_zone'] = $this->lang->line($userdata['time_zone']);
356 $this->load->model('videos_model');
357 $vs_data['videos'] = $this->videos_model->get_videos_summary(
358 NULL, $username, intval($videos_offset),
359 $this->config->item('videos_per_page'));
362 $this->load->library('pagination');
363 $pg_config['base_url'] = site_url("user/profile/$username/");
364 $pg_config['uri_segment'] = 4;
365 $pg_config['total_rows'] = $this->videos_model->get_videos_count(
367 $pg_config['per_page'] = $this->config->item('videos_per_page');
368 $this->pagination->initialize($pg_config);
369 $vs_data['pagination'] = $this->pagination->create_links();
370 $vs_data['title'] = NULL;
371 $vs_data['category_name'] = ''; // TODO videos_summary with AJAX
374 'title'=> $this->lang->line('user_appelation').' '.$username
376 . $this->config->item('site_name'),
377 'css'=> array('catalog.css')
378 //'metas' => array('description'=>'')
380 $this->load->library('html_head_params', $params);
382 // Current user profile tab
383 $tab = (! $videos_offset ? 0 : 1);
388 $this->load->view('html_begin', $this->html_head_params);
389 $this->load->view('header', array());
391 $vs = $this->load->view('catalog/videos_summary_view', $vs_data, TRUE);
393 $main_params['content'] = $this->load->view('user/profile_view',
394 array('userdata'=> $userdata, 'videos_summary'=> $vs, 'tab'=>$tab),
396 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
397 $this->load->view('main', $main_params);
399 $this->load->view('footer');
400 $this->load->view('html_end');
403 public function activate($user_id, $method='', $activation_code='')
405 $user_id = intval($user_id);
406 $res_form_validation = FALSE;
408 if ($method == 'code')
410 if (! $activation_code)
411 $res_form_validation = $this->form_validation->run('activate');
412 // Activation code is provided in URL.
415 if ($this->_valid_activation_code($activation_code)
416 && $this->users_model->activate_account($user_id,
419 $this->load->helper('message');
420 show_info_msg_page($this, sprintf(
421 $this->lang->line('user_msg_activated_account'),
422 site_url('user/login')));
427 $this->load->helper('message');
428 show_error_msg_page($this,
430 'user_msg_wrong_activation_code'));
435 else if ($method == 'resend')
437 $res_form_validation =
438 $this->form_validation->run('resend_activation');
441 $userdata = $this->users_model->get_userdata($user_id,
442 'email, a.activation_code');
443 $email = $userdata['email'];
444 $activated_account = ($userdata['activation_code'] == NULL);
446 if ($activated_account)
448 $this->load->helper('message');
449 show_info_msg_page($this, sprintf(
450 $this->lang->line('user_msg_activated_account'),
451 site_url('user/login')));
455 $this->load->library('form_validation');
457 $this->form_validation->set_error_delimiters('<span class="error">',
460 if ($res_form_validation === FALSE)
463 'title'=> $this->lang->line('user_title_activation')
465 . $this->config->item('site_name'),
466 //'metas' => array('description'=>'')
468 $this->load->library('html_head_params', $params);
473 $this->load->view('html_begin', $this->html_head_params);
474 $this->load->view('header', array());
477 $main_params['content'] =
478 $this->load->view('user/activate_view',
479 array( 'user_id'=> $user_id,
480 'email'=> $userdata['email']),
483 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
484 $this->load->view('main', $main_params);
486 $this->load->view('footer');
487 $this->load->view('html_end');
491 if ($method == 'code')
493 // A message which tells the user that the
494 // activation was successful.
495 $this->load->helper('message');
496 show_info_msg_page($this, sprintf(
497 $this->lang->line('user_msg_activated_account'),
498 site_url('user/login')));
501 else if ($method == 'resend')
503 // Redirect to resent message
504 $this->load->helper('message');
505 show_info_msg_page($this, sprintf(
506 $this->lang->line('user_msg_activation_resent'),
507 $this->input->post('email')));
513 public function recover_password()
515 $this->load->library('form_validation');
517 $this->form_validation->set_error_delimiters('<span class="error">',
520 if ($this->form_validation->run('recover_password') === FALSE)
522 $params = array( 'title' =>
524 'user_title_password_recovery')
526 . $this->config->item('site_name'),
527 //'metas' => array('description'=>'')
529 $this->load->library('html_head_params', $params);
534 $this->load->view('html_begin', $this->html_head_params);
535 $this->load->view('header', array('selected_menu' =>
536 'recover_password'));
538 $main_params['content'] = $this->load->view(
539 'user/recover_password_view', array(),
542 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
543 $this->load->view('main', $main_params);
545 $this->load->view('footer');
546 $this->load->view('html_end');
551 $this->load->helper('message');
552 show_info_msg_page($this, sprintf(
553 $this->lang->line('user_msg_password_recovery_email_sent'),
554 $this->input->post('username'),
555 $this->input->post('email')));
560 public function _format_message($msg, $val = '', $sub = '%s')
562 return str_replace($sub, $val, $this->lang->line($msg));
565 public function _update_session_userdata($data)
567 foreach ($data as $key=> $val)
568 $this->session->set_userdata($key, $val);
571 public function _valid_username($username)
573 return (preg_match('/^[a-z0-9\._]+$/', $username) === 1);
576 public function _valid_username_or_email($username)
578 $this->load->helper('email');
580 if (valid_email($username))
583 return $this->_valid_username($username);
586 public function _valid_date($date)
591 return (preg_match('/[\d]{4}-[\d]{2}-[\d]{2}/', $date) === 1);
594 public function _postprocess_birth_date($date)
596 // If the user entered no birth date NULL needs to be inserted into DB.
603 public function _valid_old_password($old_password, $field_username)
608 $username= $this->input->post($field_username);
610 if ($this->users_model->login($username, $old_password))
616 public function _change_password_cond($param)
618 $old = $this->input->post('old-password');
619 $new = $this->input->post('new-password');
620 $newc = $this->input->post('new-password-confirmation');
622 return (!$old && !$new && !$newc)
623 || ($old && $new && $newc);
626 public function _required_by_register($param)
628 $user_id = $this->input->post('user-id');
630 if (! $user_id && ! $param)
636 public function _valid_activation_code($activation_code)
638 return (preg_match('/^[a-fA-F0-9]{16}$/', $activation_code) == 1);
641 public function _do_login($username, $field_password)
643 $password = $this->input->post($field_password);
645 $user = $this->users_model->login($username, $password);
647 // Authentication failed.
651 // User has not activated the account.
652 if ($user['activation_code'] !== NULL)
654 $this->activated_account = FALSE;
655 $this->user_id = $user['id'];
659 // Authentication successful: set session with user data.
660 $this->session->set_userdata(array(
661 'user_id'=> $user['id'],
662 'username'=> $user['username'],
663 'auth_src'=> $user['auth_src'],
664 'time_zone'=> $user['time_zone']
666 $this->import = (isset($user['import']) ? $user['import'] : FALSE);
670 public function _do_activate($activation_code)
672 $user_id = $this->input->post('user-id');
673 if ($user_id === FALSE)
675 $user_id = intval($user_id);
677 return $this->users_model->activate_account($user_id,
681 public function _do_resend_activation($email)
683 $user_id = $this->input->post('user-id');
684 if ($user_id === FALSE)
686 $user_id = intval($user_id);
688 $this->users_model->set_userdata($user_id,
689 array('email'=> $email));
691 return $this->users_model->send_activation_email($user_id, $email);
694 public function _username_exists($username)
696 $userdata = $this->users_model->get_userdata($username);
704 public function _internal_account($username)
706 $userdata = $this->users_model->get_userdata($username, 'auth_src');
710 if ($userdata['auth_src'] != 'internal')
716 public function _do_recover_password($username)
718 $email = $this->input->post('email');
722 return $this->users_model->recover_password($username, $email);
726 /* End of file user.php */
727 /* Location: ./application/controllers/user.php */