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');
264 $userdata = $this->users_model->get_userdata($username);
265 $userdata['roles'] = Users_model::roles_to_string($userdata['roles']);
266 $country_list = $this->config->item('country_list');
267 $userdata['country_name'] = $country_list[ $userdata['country'] ];
268 $userdata['last_login'] = date('Y-m-d H:i:s',
270 strtotime($userdata['last_login']),
272 TRUE)) . ($time_zone == 'UTC' ? ' (UTC)' : '');
273 $userdata['time_zone'] = $this->lang->line($userdata['time_zone']);
276 $this->load->model('videos_model');
277 $vs_data['videos'] = $this->videos_model->get_videos_summary(
278 NULL, $username, intval($videos_offset),
279 $this->config->item('videos_per_page'));
282 $this->load->library('pagination');
283 $pg_config['base_url'] = site_url("user/profile/$username/");
284 $pg_config['uri_segment'] = 4;
285 $pg_config['total_rows'] = $this->videos_model->get_videos_count(
287 $pg_config['per_page'] = $this->config->item('videos_per_page');
288 $this->pagination->initialize($pg_config);
289 $vs_data['pagination'] = $this->pagination->create_links();
290 $vs_data['title'] = NULL;
291 $vs_data['category_name'] = ''; // TODO videos_summary with AJAX
294 'title'=> $this->lang->line('user_appelation').' '.$username
296 . $this->config->item('site_name'),
297 'css'=> array('catalog.css')
298 //'metas' => array('description'=>'')
300 $this->load->library('html_head_params', $params);
302 // Current user profile tab
303 $tab = (! $videos_offset ? 0 : 1);
308 $this->load->view('html_begin', $this->html_head_params);
309 $this->load->view('header', array());
311 $vs = $this->load->view('catalog/videos_summary_view', $vs_data, TRUE);
313 $main_params['content'] = $this->load->view('user/profile_view',
314 array('userdata'=> $userdata, 'videos_summary'=> $vs, 'tab'=>$tab),
316 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
317 $this->load->view('main', $main_params);
319 $this->load->view('footer');
320 $this->load->view('html_end');
323 public function activate($user_id, $method='', $activation_code='')
325 $user_id = intval($user_id);
326 $res_form_validation = FALSE;
328 if ($method == 'code')
330 if (! $activation_code)
331 $res_form_validation = $this->form_validation->run('activate');
332 // Activation code is provided in URL.
335 if ($this->_valid_activation_code($activation_code)
336 && $this->users_model->activate_account($user_id,
339 $this->session->set_flashdata('msg', sprintf(
340 $this->lang->line('user_msg_activated_account'),
341 site_url('user/login')));
342 header('Location: '. site_url('message/info'));
347 $this->session->set_flashdata('msg',
348 $this->lang->line('user_msg_wrong_activation_code'));
349 header('Location: '. site_url('message/error'));
354 else if ($method == 'resend')
356 $res_form_validation =
357 $this->form_validation->run('resend_activation');
360 $userdata = $this->users_model->get_userdata($user_id,
361 'email, a.activation_code');
362 $email = $userdata['email'];
363 $activated_account = ($userdata['activation_code'] == NULL);
365 if ($activated_account)
367 $this->session->set_flashdata('msg', sprintf(
368 $this->lang->line('user_msg_activated_account'),
369 site_url('user/login')));
370 header('Location: '. site_url('message/info'));
374 $this->load->library('form_validation');
376 $this->form_validation->set_error_delimiters('<span class="error">',
379 if ($res_form_validation === FALSE)
382 'title'=> $this->lang->line('user_title_activation')
384 . $this->config->item('site_name'),
385 //'metas' => array('description'=>'')
387 $this->load->library('html_head_params', $params);
392 $this->load->view('html_begin', $this->html_head_params);
393 $this->load->view('header', array());
396 $main_params['content'] =
397 $this->load->view('user/activate_view',
398 array( 'user_id'=> $user_id,
399 'email'=> $userdata['email']),
402 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
403 $this->load->view('main', $main_params);
405 $this->load->view('footer');
406 $this->load->view('html_end');
410 if ($method == 'code')
412 // Redirect to a message which tells the user that the
413 // activation was successful.
414 $this->session->set_flashdata('msg', sprintf(
415 $this->lang->line('user_msg_activated_account'),
416 site_url('user/login')));
417 header('Location: '. site_url('message/info'));
420 else if ($method == 'resend')
422 // Redirect to resent message
423 $this->session->set_flashdata('msg', sprintf(
424 $this->lang->line('user_msg_activation_resent'),
425 $this->input->post('email')));
426 header('Location: '. site_url('message/info'));
432 public function recover_password()
434 $this->load->library('form_validation');
436 $this->form_validation->set_error_delimiters('<span class="error">',
439 if ($this->form_validation->run('recover_password') === FALSE)
441 $params = array( 'title' =>
443 'user_title_password_recovery')
445 . $this->config->item('site_name'),
446 //'metas' => array('description'=>'')
448 $this->load->library('html_head_params', $params);
453 $this->load->view('html_begin', $this->html_head_params);
454 $this->load->view('header', array('selected_menu' =>
455 'recover_password'));
457 $main_params['content'] = $this->load->view(
458 'user/recover_password_view', array(),
461 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
462 $this->load->view('main', $main_params);
464 $this->load->view('footer');
465 $this->load->view('html_end');
469 // Redirect to resent message
470 $this->session->set_flashdata('msg', sprintf(
471 $this->lang->line('user_msg_password_recovery_email_sent'),
472 $this->input->post('username'),
473 $this->input->post('email')));
474 header('Location: '. site_url('message/info'));
479 public function _update_session_userdata($data)
481 foreach ($data as $key=> $val)
482 $this->session->set_userdata($key, $val);
485 public function _valid_username($username)
487 return (preg_match('/^[a-z0-9\._]+$/', $username) === 1);
490 public function _valid_username_or_email($username)
492 $this->load->helper('email');
494 if (valid_email($username))
497 return $this->_valid_username($username);
500 public function _valid_date($date)
505 return (preg_match('/[\d]{4}-[\d]{2}-[\d]{2}/', $date) === 1);
508 public function _postprocess_birth_date($date)
510 // If the user entered no birth date NULL needs to be inserted into DB.
517 public function _valid_old_password($old_password, $field_username)
522 $username= $this->input->post($field_username);
524 if ($this->users_model->login($username, $old_password))
530 public function _change_password_cond($param)
532 $old = $this->input->post('old-password');
533 $new = $this->input->post('new-password');
534 $newc = $this->input->post('new-password-confirmation');
536 return (!$old && !$new && !$newc)
537 || ($old && $new && $newc);
540 public function _required_by_register($param)
542 $user_id = $this->input->post('user-id');
544 if (! $user_id && ! $param)
550 public function _valid_activation_code($activation_code)
552 return (preg_match('/^[a-fA-F0-9]{16}$/', $activation_code) == 1);
555 public function _do_login($username, $field_password)
557 $password = $this->input->post($field_password);
559 $user = $this->users_model->login($username, $password);
561 // Authentication failed.
565 // User has not activated the account.
566 if ($user['activation_code'] !== NULL)
568 $this->activated_account = FALSE;
569 $this->user_id = $user['id'];
573 // Authentication successful: set session with user data.
574 $this->session->set_userdata(array(
575 'user_id'=> $user['id'],
576 'username'=> $user['username'],
577 'auth_src'=> $user['auth_src'],
578 'time_zone'=> $user['time_zone']
580 $this->import = (isset($user['import']) ? $user['import'] : FALSE);
584 public function _do_activate($activation_code)
586 $user_id = $this->input->post('user-id');
587 if ($user_id === FALSE)
589 $user_id = intval($user_id);
591 return $this->users_model->activate_account($user_id,
595 public function _do_resend_activation($email)
597 $user_id = $this->input->post('user-id');
598 if ($user_id === FALSE)
600 $user_id = intval($user_id);
602 $this->users_model->set_userdata($user_id,
603 array('email'=> $email));
605 return $this->users_model->send_activation_email($user_id, $email);
608 public function _username_exists($username)
610 $userdata = $this->users_model->get_userdata($username);
618 public function _internal_account($username)
620 $userdata = $this->users_model->get_userdata($username, 'auth_src');
624 if ($userdata['auth_src'] != 'internal')
630 public function _do_recover_password($username)
632 $email = $this->input->post('email');
636 return $this->users_model->recover_password($username, $email);
640 /* End of file user.php */
641 /* Location: ./application/controllers/user.php */