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 if ($this->form_validation->run('login') === FALSE)
47 $params = array( 'title' =>
48 $this->lang->line('ui_nav_menu_login')
50 . $this->config->item('site_name'),
51 //'metas' => array('description'=>'')
53 $this->load->library('html_head_params', $params);
58 $this->load->view('html_begin', $this->html_head_params);
59 $this->load->view('header', array('selected_menu' => 'login'));
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);
66 $this->load->view('footer');
67 $this->load->view('html_end');
71 if (! $this->activated_account)
73 . site_url("user/activate/{$this->user_id}"));
74 else if (! $this->import)
76 // Redirect to last page before login.
77 header('Location: '. site_url(urldecode_segments($redirect)));
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'));
90 * Logout user and then redirect it to the last page which must be encoded
93 * @param string $redirect contains the last page URI segments encoded
94 * with helper url_encode_segments.
96 public function logout($redirect = '')
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');
103 header('Location: '. site_url(urldecode_segments($redirect)));
106 public function register($redirect = '')
108 $this->load->library('form_validation');
109 $this->load->helper('localization');
110 $this->load->helper('date');
112 $this->form_validation->set_error_delimiters('<span class="error">',
116 if ($this->form_validation->run('register'))
118 $b_validation = TRUE;
120 if ($_FILES['picture']['tmp_name'])
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);
130 $b_validation = $this->upload->do_upload('picture');
132 $this->upload->display_errors('<span class="error">',
137 $b_validation = FALSE;
141 // Edit account data if logged in, otherwise register.
142 if ($user_id = $this->session->userdata('user_id'))
144 $userdata = $this->users_model->get_userdata(intval($user_id));
145 $selected_menu = 'account';
150 $selected_menu = 'register';
153 $params = array('title' =>
154 $this->lang->line('ui_nav_menu_register')
156 . $this->config->item('site_name'),
157 //'metas' => array('description'=>'')
159 $this->load->library('html_head_params', $params);
164 $this->load->view('html_begin', $this->html_head_params);
165 $this->load->view('header',
166 array('selected_menu' => $selected_menu));
168 $main_params['content'] = $this->load->view('user/register_view',
169 array('userdata'=> $userdata, 'redirect'=> $redirect,
170 'error_upload'=> $error_upload),
172 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
173 $this->load->view('main', $main_params);
175 $this->load->view('footer');
176 $this->load->view('html_end');
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');
190 // Handle picture if one was uploaded.
191 if ($_FILES['picture']['tmp_name'])
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()
200 $this->image->save_thumbnail(
201 $upload_data['full_path'],
202 640, 480, IMAGETYPE_AUTO);
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);
210 // Update session user data.
211 $this->_update_session_userdata($data);
216 $password = $this->input->post('new-password');
218 $data['password'] = $this->input->post('new-password');
220 $this->users_model->set_userdata($user_id, $data);
222 // Redirect to last page before login.
223 header('Location: '. site_url(urldecode_segments($redirect)));
228 $data['username'] = $this->input->post('username');
229 $data['password'] = $this->input->post('password');
231 $this->users_model->register($data);
232 $user_id = $this->users_model->get_userdata($data['username'],
234 $user_id = $user_id['id'];
236 // Redirect account activation page.
237 header('Location: '. site_url("user/activate/$user_id"));
242 public function account($redirect = '')
244 $this->register($redirect);
247 public function profile($username, $videos_offset = 0)
249 // TODO handle user not found
251 $this->load->config('localization');
252 $this->load->helper('date');
253 $this->lang->load('date');
258 // Logged in user time zone
259 $time_zone = $this->session->userdata('time_zone');
262 $userdata = $this->users_model->get_userdata($username);
263 $userdata['roles'] = Users_model::roles_to_string($userdata['roles']);
264 $country_list = $this->config->item('country_list');
265 $userdata['country_name'] = $country_list[ $userdata['country'] ];
266 $userdata['last_login'] = human_gmt_to_human_local(
267 $userdata['last_login'], $time_zone);
268 $userdata['time_zone'] = $this->lang->line($userdata['time_zone']);
271 $this->load->model('videos_model');
272 $vs_data['videos'] = $this->videos_model->get_videos_summary(
273 NULL, $username, intval($videos_offset),
274 $this->config->item('videos_per_page'));
277 $this->load->library('pagination');
278 $pg_config['base_url'] = site_url("user/profile/$username/");
279 $pg_config['uri_segment'] = 4;
280 $pg_config['total_rows'] = $this->videos_model->get_videos_count(
282 $pg_config['per_page'] = $this->config->item('videos_per_page');
283 $this->pagination->initialize($pg_config);
284 $vs_data['pagination'] = $this->pagination->create_links();
285 $vs_data['title'] = NULL;
286 $vs_data['category_name'] = ''; // TODO videos_summary with AJAX
289 'title'=> $this->lang->line('user_appelation').' '.$username
291 . $this->config->item('site_name'),
292 'css'=> array('catalog.css')
293 //'metas' => array('description'=>'')
295 $this->load->library('html_head_params', $params);
297 // Current user profile tab
298 $tab = (! $videos_offset ? 0 : 1);
303 $this->load->view('html_begin', $this->html_head_params);
304 $this->load->view('header', array());
306 $vs = $this->load->view('catalog/videos_summary_view', $vs_data, TRUE);
308 $main_params['content'] = $this->load->view('user/profile_view',
309 array('userdata'=> $userdata, 'videos_summary'=> $vs, 'tab'=>$tab),
311 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
312 $this->load->view('main', $main_params);
314 $this->load->view('footer');
315 $this->load->view('html_end');
318 public function activate($user_id, $method='', $activation_code='')
320 $user_id = intval($user_id);
321 $res_form_validation = FALSE;
323 if ($method == 'code')
325 if (! $activation_code)
326 $res_form_validation = $this->form_validation->run('activate');
327 // Activation code is provided in URL.
330 if ($this->_valid_activation_code($activation_code)
331 && $this->users_model->activate_account($user_id,
334 $this->session->set_flashdata('msg', sprintf(
335 $this->lang->line('user_msg_activated_account'),
336 site_url('user/login')));
337 header('Location: '. site_url('message/info'));
342 $this->session->set_flashdata('msg',
343 $this->lang->line('user_msg_wrong_activation_code'));
344 header('Location: '. site_url('message/error'));
349 else if ($method == 'resend')
351 $res_form_validation =
352 $this->form_validation->run('resend_activation');
355 $userdata = $this->users_model->get_userdata($user_id,
356 'email, a.activation_code');
357 $email = $userdata['email'];
358 $activated_account = ($userdata['activation_code'] == NULL);
360 if ($activated_account)
362 $this->session->set_flashdata('msg', sprintf(
363 $this->lang->line('user_msg_activated_account'),
364 site_url('user/login')));
365 header('Location: '. site_url('message/info'));
369 $this->load->library('form_validation');
371 $this->form_validation->set_error_delimiters('<span class="error">',
374 if ($res_form_validation === FALSE)
377 'title'=> $this->lang->line('user_title_activation')
379 . $this->config->item('site_name'),
380 //'metas' => array('description'=>'')
382 $this->load->library('html_head_params', $params);
387 $this->load->view('html_begin', $this->html_head_params);
388 $this->load->view('header', array());
391 $main_params['content'] =
392 $this->load->view('user/activate_view',
393 array( 'user_id'=> $user_id,
394 'email'=> $userdata['email']),
397 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
398 $this->load->view('main', $main_params);
400 $this->load->view('footer');
401 $this->load->view('html_end');
405 if ($method == 'code')
407 // Redirect to a message which tells the user that the
408 // activation was successful.
409 $this->session->set_flashdata('msg', sprintf(
410 $this->lang->line('user_msg_activated_account'),
411 site_url('user/login')));
412 header('Location: '. site_url('message/info'));
415 else if ($method == 'resend')
417 // Redirect to resent message
418 $this->session->set_flashdata('msg', sprintf(
419 $this->lang->line('user_msg_activation_resent'),
420 $this->input->post('email')));
421 header('Location: '. site_url('message/info'));
427 public function recover_password()
429 $this->load->library('form_validation');
431 $this->form_validation->set_error_delimiters('<span class="error">',
434 if ($this->form_validation->run('recover_password') === FALSE)
436 $params = array( 'title' =>
438 'user_title_password_recovery')
440 . $this->config->item('site_name'),
441 //'metas' => array('description'=>'')
443 $this->load->library('html_head_params', $params);
448 $this->load->view('html_begin', $this->html_head_params);
449 $this->load->view('header', array('selected_menu' =>
450 'recover_password'));
452 $main_params['content'] = $this->load->view(
453 'user/recover_password_view', array(),
456 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
457 $this->load->view('main', $main_params);
459 $this->load->view('footer');
460 $this->load->view('html_end');
464 // Redirect to resent message
465 $this->session->set_flashdata('msg', sprintf(
466 $this->lang->line('user_msg_password_recovery_email_sent'),
467 $this->input->post('username'),
468 $this->input->post('email')));
469 header('Location: '. site_url('message/info'));
474 public function _update_session_userdata($data)
476 foreach ($data as $key=> $val)
477 $this->session->set_userdata($key, $val);
480 public function _valid_username($username)
482 return (preg_match('/^[a-z0-9\._]+$/', $username) === 1);
485 public function _valid_username_or_email($username)
487 $this->load->helper('email');
489 if (valid_email($username))
492 return $this->_valid_username($username);
495 public function _valid_date($date)
500 return (preg_match('/[\d]{4}-[\d]{2}-[\d]{2}/', $date) === 1);
503 public function _postprocess_birth_date($date)
505 // If the user entered no birth date NULL needs to be inserted into DB.
512 public function _valid_old_password($old_password, $field_username)
517 $username= $this->input->post($field_username);
519 if ($this->users_model->login($username, $old_password))
525 public function _change_password_cond($param)
527 $old = $this->input->post('old-password');
528 $new = $this->input->post('new-password');
529 $newc = $this->input->post('new-password-confirmation');
531 return (!$old && !$new && !$newc)
532 || ($old && $new && $newc);
535 public function _required_by_register($param)
537 $user_id = $this->input->post('user-id');
539 if (! $user_id && ! $param)
545 public function _valid_activation_code($activation_code)
547 return (preg_match('/^[a-fA-F0-9]{16}$/', $activation_code) == 1);
550 public function _do_login($username, $field_password)
552 $password = $this->input->post($field_password);
554 $user = $this->users_model->login($username, $password);
556 // Authentication failed.
560 // User has not activated the account.
561 if ($user['activation_code'] !== NULL)
563 $this->activated_account = FALSE;
564 $this->user_id = $user['id'];
568 // Authentication successful: set session with user data.
569 $this->session->set_userdata(array(
570 'user_id'=> $user['id'],
571 'username'=> $user['username'],
572 'auth_src'=> $user['auth_src'],
573 'time_zone'=> $user['time_zone']
575 $this->import = (isset($user['import']) ? $user['import'] : FALSE);
579 public function _do_activate($activation_code)
581 $user_id = $this->input->post('user-id');
582 if ($user_id === FALSE)
584 $user_id = intval($user_id);
586 return $this->users_model->activate_account($user_id,
590 public function _do_resend_activation($email)
592 $user_id = $this->input->post('user-id');
593 if ($user_id === FALSE)
595 $user_id = intval($user_id);
597 $this->users_model->set_userdata($user_id,
598 array('email'=> $email));
600 return $this->users_model->send_activation_email($user_id, $email);
603 public function _username_exists($username)
605 $userdata = $this->users_model->get_userdata($username);
613 public function _internal_account($username)
615 $userdata = $this->users_model->get_userdata($username, 'auth_src');
619 if ($userdata['auth_src'] != 'internal')
625 public function _do_recover_password($username)
627 $email = $this->input->post('email');
631 return $this->users_model->recover_password($username, $email);
635 /* End of file user.php */
636 /* Location: ./application/controllers/user.php */