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);
161 echo var_dump($sreg);
162 // foreach ($sreg as $key => $value)
164 // $data['success'] .= $this->_set_message('openid_content', array($key, $value), array('%s', '%t'));
171 public function openid_policy()
173 $this->load->view('openid_policy_view');
177 * Logout user and then redirect it to the last page which must be encoded
180 * @param string $redirect contains the last page URI segments encoded
181 * with helper url_encode_segments.
183 public function logout($redirect = '')
185 $this->session->unset_userdata('user_id');
186 $this->session->unset_userdata('username');
187 $this->session->unset_userdata('auth_src');
188 $this->session->unset_userdata('time_zone');
190 header('Location: '. site_url(urldecode_segments($redirect)));
193 public function register($redirect = '')
195 $this->load->library('form_validation');
196 $this->load->helper('localization');
197 $this->load->helper('date');
199 $this->form_validation->set_error_delimiters('<span class="error">',
203 if ($this->form_validation->run('register'))
205 $b_validation = TRUE;
207 if ($_FILES['picture']['tmp_name'])
210 $config_upload['upload_path'] = './data/user_pictures';
211 $config_upload['file_name'] =
212 str_replace('.', '-', $this->input->post('username')) .'-';
213 $config_upload['allowed_types'] = 'gif|jpg|png';
214 $config_upload['max_size'] = '10240';
215 $this->load->library('upload', $config_upload);
217 $b_validation = $this->upload->do_upload('picture');
219 $this->upload->display_errors('<span class="error">',
224 $b_validation = FALSE;
228 // Edit account data if logged in, otherwise register.
229 if ($user_id = $this->session->userdata('user_id'))
231 $userdata = $this->users_model->get_userdata(intval($user_id));
232 $selected_menu = 'account';
237 $selected_menu = 'register';
240 $params = array('title' =>
241 $this->lang->line('ui_nav_menu_register')
243 . $this->config->item('site_name'),
244 //'metas' => array('description'=>'')
246 $this->load->library('html_head_params', $params);
251 $this->load->view('html_begin', $this->html_head_params);
252 $this->load->view('header',
253 array('selected_menu' => $selected_menu));
255 $main_params['content'] = $this->load->view('user/register_view',
256 array('userdata'=> $userdata, 'redirect'=> $redirect,
257 'error_upload'=> $error_upload),
259 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
260 $this->load->view('main', $main_params);
262 $this->load->view('footer');
263 $this->load->view('html_end');
267 $user_id = $this->input->post('user-id');
268 $data['email'] = $this->input->post('email');
269 $data['first_name'] = $this->input->post('first-name');
270 $data['last_name'] = $this->input->post('last-name');
271 $data['birth_date'] = $this->input->post('birth-date');
272 $data['country'] = $this->input->post('country');
273 $data['locality'] = $this->input->post('locality');
274 $data['ui_lang'] = $this->input->post('ui-lang');
275 $data['time_zone'] = $this->input->post('time-zone');
277 // Handle picture if one was uploaded.
278 if ($_FILES['picture']['tmp_name'])
280 $upload_data = $this->upload->data();
281 $this->load->library('image');
282 $this->image->load($upload_data['full_path']);
283 // Resize original to a maximum size.
284 if ($this->image->get_width() * $this->image->get_height()
287 $this->image->save_thumbnail(
288 $upload_data['full_path'],
289 640, 480, IMAGETYPE_AUTO);
292 $data['picture'] = $upload_data['file_name'];
293 $this->image->save_thumbnail($upload_data['file_path']
294 . $upload_data['file_name']. '-thumb.jpg', 120, 90);
297 // Update session user data.
298 $this->_update_session_userdata($data);
303 $password = $this->input->post('new-password');
305 $data['password'] = $this->input->post('new-password');
307 $this->users_model->set_userdata($user_id, $data);
309 // Redirect to last page before login.
310 header('Location: '. site_url(urldecode_segments($redirect)));
315 $data['username'] = $this->input->post('username');
316 $data['password'] = $this->input->post('password');
318 $this->users_model->register($data);
319 $user_id = $this->users_model->get_userdata($data['username'],
321 $user_id = $user_id['id'];
323 // Redirect account activation page.
324 header('Location: '. site_url("user/activate/$user_id"));
329 public function account($redirect = '')
331 $this->register($redirect);
334 public function profile($username, $videos_offset = 0)
336 // TODO handle user not found
338 $this->load->config('localization');
339 $this->load->helper('date');
340 $this->lang->load('date');
345 // Logged in user time zone
346 $time_zone = $this->session->userdata('time_zone');
349 $userdata = $this->users_model->get_userdata($username);
350 $userdata['roles'] = Users_model::roles_to_string($userdata['roles']);
351 $country_list = $this->config->item('country_list');
352 $userdata['country_name'] = $country_list[ $userdata['country'] ];
353 $userdata['last_login'] = human_gmt_to_human_local(
354 $userdata['last_login'], $time_zone);
355 $userdata['time_zone'] = $this->lang->line($userdata['time_zone']);
358 $this->load->model('videos_model');
359 $vs_data['videos'] = $this->videos_model->get_videos_summary(
360 NULL, $username, intval($videos_offset),
361 $this->config->item('videos_per_page'));
364 $this->load->library('pagination');
365 $pg_config['base_url'] = site_url("user/profile/$username/");
366 $pg_config['uri_segment'] = 4;
367 $pg_config['total_rows'] = $this->videos_model->get_videos_count(
369 $pg_config['per_page'] = $this->config->item('videos_per_page');
370 $this->pagination->initialize($pg_config);
371 $vs_data['pagination'] = $this->pagination->create_links();
372 $vs_data['title'] = NULL;
373 $vs_data['category_name'] = ''; // TODO videos_summary with AJAX
376 'title'=> $this->lang->line('user_appelation').' '.$username
378 . $this->config->item('site_name'),
379 'css'=> array('catalog.css')
380 //'metas' => array('description'=>'')
382 $this->load->library('html_head_params', $params);
384 // Current user profile tab
385 $tab = (! $videos_offset ? 0 : 1);
390 $this->load->view('html_begin', $this->html_head_params);
391 $this->load->view('header', array());
393 $vs = $this->load->view('catalog/videos_summary_view', $vs_data, TRUE);
395 $main_params['content'] = $this->load->view('user/profile_view',
396 array('userdata'=> $userdata, 'videos_summary'=> $vs, 'tab'=>$tab),
398 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
399 $this->load->view('main', $main_params);
401 $this->load->view('footer');
402 $this->load->view('html_end');
405 public function activate($user_id, $method='', $activation_code='')
407 $user_id = intval($user_id);
408 $res_form_validation = FALSE;
410 if ($method == 'code')
412 if (! $activation_code)
413 $res_form_validation = $this->form_validation->run('activate');
414 // Activation code is provided in URL.
417 if ($this->_valid_activation_code($activation_code)
418 && $this->users_model->activate_account($user_id,
421 $this->load->helper('message');
422 show_info_msg_page($this, sprintf(
423 $this->lang->line('user_msg_activated_account'),
424 site_url('user/login')));
429 $this->load->helper('message');
430 show_error_msg_page($this,
432 'user_msg_wrong_activation_code'));
437 else if ($method == 'resend')
439 $res_form_validation =
440 $this->form_validation->run('resend_activation');
443 $userdata = $this->users_model->get_userdata($user_id,
444 'email, a.activation_code');
445 $email = $userdata['email'];
446 $activated_account = ($userdata['activation_code'] == NULL);
448 if ($activated_account)
450 $this->load->helper('message');
451 show_info_msg_page($this, sprintf(
452 $this->lang->line('user_msg_activated_account'),
453 site_url('user/login')));
457 $this->load->library('form_validation');
459 $this->form_validation->set_error_delimiters('<span class="error">',
462 if ($res_form_validation === FALSE)
465 'title'=> $this->lang->line('user_title_activation')
467 . $this->config->item('site_name'),
468 //'metas' => array('description'=>'')
470 $this->load->library('html_head_params', $params);
475 $this->load->view('html_begin', $this->html_head_params);
476 $this->load->view('header', array());
479 $main_params['content'] =
480 $this->load->view('user/activate_view',
481 array( 'user_id'=> $user_id,
482 'email'=> $userdata['email']),
485 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
486 $this->load->view('main', $main_params);
488 $this->load->view('footer');
489 $this->load->view('html_end');
493 if ($method == 'code')
495 // A message which tells the user that the
496 // activation was successful.
497 $this->load->helper('message');
498 show_info_msg_page($this, sprintf(
499 $this->lang->line('user_msg_activated_account'),
500 site_url('user/login')));
503 else if ($method == 'resend')
505 // Redirect to resent message
506 $this->load->helper('message');
507 show_info_msg_page($this, sprintf(
508 $this->lang->line('user_msg_activation_resent'),
509 $this->input->post('email')));
515 public function recover_password()
517 $this->load->library('form_validation');
519 $this->form_validation->set_error_delimiters('<span class="error">',
522 if ($this->form_validation->run('recover_password') === FALSE)
524 $params = array( 'title' =>
526 'user_title_password_recovery')
528 . $this->config->item('site_name'),
529 //'metas' => array('description'=>'')
531 $this->load->library('html_head_params', $params);
536 $this->load->view('html_begin', $this->html_head_params);
537 $this->load->view('header', array('selected_menu' =>
538 'recover_password'));
540 $main_params['content'] = $this->load->view(
541 'user/recover_password_view', array(),
544 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
545 $this->load->view('main', $main_params);
547 $this->load->view('footer');
548 $this->load->view('html_end');
553 $this->load->helper('message');
554 show_info_msg_page($this, sprintf(
555 $this->lang->line('user_msg_password_recovery_email_sent'),
556 $this->input->post('username'),
557 $this->input->post('email')));
562 public function _format_message($msg, $val = '', $sub = '%s')
564 return str_replace($sub, $val, $this->lang->line($msg));
567 public function _update_session_userdata($data)
569 foreach ($data as $key=> $val)
570 $this->session->set_userdata($key, $val);
573 public function _valid_username($username)
575 return (preg_match('/^[a-z0-9\._]+$/', $username) === 1);
578 public function _valid_username_or_email($username)
580 $this->load->helper('email');
582 if (valid_email($username))
585 return $this->_valid_username($username);
588 public function _valid_date($date)
593 return (preg_match('/[\d]{4}-[\d]{2}-[\d]{2}/', $date) === 1);
596 public function _postprocess_birth_date($date)
598 // If the user entered no birth date NULL needs to be inserted into DB.
605 public function _valid_old_password($old_password, $field_username)
610 $username= $this->input->post($field_username);
612 if ($this->users_model->login($username, $old_password))
618 public function _change_password_cond($param)
620 $old = $this->input->post('old-password');
621 $new = $this->input->post('new-password');
622 $newc = $this->input->post('new-password-confirmation');
624 return (!$old && !$new && !$newc)
625 || ($old && $new && $newc);
628 public function _required_by_register($param)
630 $user_id = $this->input->post('user-id');
632 if (! $user_id && ! $param)
638 public function _valid_activation_code($activation_code)
640 return (preg_match('/^[a-fA-F0-9]{16}$/', $activation_code) == 1);
643 public function _do_login($username, $field_password)
645 $password = $this->input->post($field_password);
647 $user = $this->users_model->login($username, $password);
649 // Authentication failed.
653 // User has not activated the account.
654 if ($user['activation_code'] !== NULL)
656 $this->activated_account = FALSE;
657 $this->user_id = $user['id'];
661 // Authentication successful: set session with user data.
662 $this->session->set_userdata(array(
663 'user_id'=> $user['id'],
664 'username'=> $user['username'],
665 'auth_src'=> $user['auth_src'],
666 'time_zone'=> $user['time_zone']
668 $this->import = (isset($user['import']) ? $user['import'] : FALSE);
672 public function _do_activate($activation_code)
674 $user_id = $this->input->post('user-id');
675 if ($user_id === FALSE)
677 $user_id = intval($user_id);
679 return $this->users_model->activate_account($user_id,
683 public function _do_resend_activation($email)
685 $user_id = $this->input->post('user-id');
686 if ($user_id === FALSE)
688 $user_id = intval($user_id);
690 $this->users_model->set_userdata($user_id,
691 array('email'=> $email));
693 return $this->users_model->send_activation_email($user_id, $email);
696 public function _username_exists($username)
698 $userdata = $this->users_model->get_userdata($username);
706 public function _internal_account($username)
708 $userdata = $this->users_model->get_userdata($username, 'auth_src');
712 if ($userdata['auth_src'] != 'internal')
718 public function _do_recover_password($username)
720 $email = $this->input->post('email');
724 return $this->users_model->recover_password($username, $email);
728 /* End of file user.php */
729 /* Location: ./application/controllers/user.php */