From adc2c33c37c781444c4234c6fed38e95d084ed8d Mon Sep 17 00:00:00 2001 From: Calin Burloiu Date: Thu, 15 Sep 2011 18:13:59 +0300 Subject: [PATCH] working at user login --- application/config/autoload.php | 2 +- application/config/config.php | 8 +- application/config/hooks.php | 8 +- application/controllers/catalog.php | 23 +---- application/controllers/user.php | 94 +++++++++++++++---- application/hooks/hooks.php | 15 +++ .../language/english/form_validation_lang.php | 10 ++ application/models/users_model.php | 54 +++++++++++ application/models/videos_model.php | 2 + application/views/header.php | 5 + application/views/user/login_view.php | 19 +++- css/default.css | 11 +++ 12 files changed, 207 insertions(+), 44 deletions(-) create mode 100644 application/hooks/hooks.php create mode 100644 application/language/english/form_validation_lang.php create mode 100644 application/models/users_model.php diff --git a/application/config/autoload.php b/application/config/autoload.php index c3d8699..c7ea0f0 100644 --- a/application/config/autoload.php +++ b/application/config/autoload.php @@ -52,7 +52,7 @@ $autoload['packages'] = array(APPPATH.'third_party'); | $autoload['libraries'] = array('database', 'session', 'xmlrpc'); */ -$autoload['libraries'] = array('form_validation'); +$autoload['libraries'] = array('form_validation', 'session'); /* diff --git a/application/config/config.php b/application/config/config.php index 755fc91..f707636 100644 --- a/application/config/config.php +++ b/application/config/config.php @@ -224,7 +224,7 @@ $config['cache_path'] = ''; | MUST set an encryption key. See the user guide for info. | */ -$config['encryption_key'] = ''; +$config['encryption_key'] = '328pgYhL6Fc87WC46UHIaaxHBYeutR5u'; /* |-------------------------------------------------------------------------- @@ -244,13 +244,13 @@ $config['encryption_key'] = ''; | 'sess_time_to_update' = how many seconds between CI refreshing Session Information | */ -$config['sess_cookie_name'] = 'ci_session'; +$config['sess_cookie_name'] = 'p2ptube_session'; $config['sess_expiration'] = 7200; $config['sess_expire_on_close'] = FALSE; $config['sess_encrypt_cookie'] = FALSE; $config['sess_use_database'] = FALSE; -$config['sess_table_name'] = 'ci_sessions'; -$config['sess_match_ip'] = FALSE; +$config['sess_table_name'] = 'sessions'; +$config['sess_match_ip'] = TRUE; $config['sess_match_useragent'] = TRUE; $config['sess_time_to_update'] = 300; diff --git a/application/config/hooks.php b/application/config/hooks.php index a4ad2be..b758599 100644 --- a/application/config/hooks.php +++ b/application/config/hooks.php @@ -10,7 +10,13 @@ | */ - +$hook['pre_controller'][] = array( + 'class' => '', + 'function' => 'select_language', + 'filename' => 'hooks.php', + 'filepath' => 'hooks', + 'params' => array() +); /* End of file hooks.php */ /* Location: ./application/config/hooks.php */ \ No newline at end of file diff --git a/application/controllers/catalog.php b/application/controllers/catalog.php index 79d0f96..743342e 100644 --- a/application/controllers/catalog.php +++ b/application/controllers/catalog.php @@ -55,7 +55,10 @@ class Catalog extends CI_Controller { // ** LOADING VIEWS // ** $this->load->view('html_begin', $this->html_head_params); - $this->load->view('header', array('selected_menu' => 'home')); + $this->load->view('header', array( + 'selected_menu'=> 'home', + 'username'=> $this->session->userdata('username') + )); $main_params['content'] = $this->load->view('catalog/index_view', $data, TRUE); $main_params['side'] = $this->load->view('side_default', NULL, TRUE); @@ -67,23 +70,7 @@ class Catalog extends CI_Controller { public function test($page = 0) { - $params = array( 'title' => 'Test - '. $this->config->item('site_name'), - //'css' => array(), - //'js' => array(), - //'metas' => array('description'=>'','keywords'=>'') - ); - $this->load->library('html_head_params', $params); - - // ** - // ** LOADING VIEWS - // ** - $this->load->view('html_begin', $this->html_head_params); - $this->load->view('header', array('selected_menu' => 'home')); - - $this->load->view('echo', array('output'=>'Test Page', 'clear'=>TRUE)); - - $this->load->view('footer'); - $this->load->view('html_end'); + echo $this->uri->segment(1); } public function category($category_name, $ordering = 'hottest', $offset = 0) diff --git a/application/controllers/user.php b/application/controllers/user.php index 33c50d9..33dd727 100644 --- a/application/controllers/user.php +++ b/application/controllers/user.php @@ -7,53 +7,115 @@ * @author Călin-Andrei Burloiu */ class User extends CI_Controller { - + + private $username = NULL; + private $email = NULL; + private $user_id = NULL; + public function __construct() { parent::__construct(); - + $this->lang->load('user'); } - + public function index() { } - + public function login() - { + { $this->load->library('form_validation'); - - if ($this->form_validation->run() == FALSE) + $this->load->model('users_model'); + + $username = $this->input->post('username'); + $password = $this->input->post('password'); + + $form_validation_config = array( + array( + 'field'=>'username', + 'label'=>'lang:user_username_or_email', + 'rules'=>'trim|required|min_length[5]|max_length[32]' + . '|strtolower|callback__valid_username' + . '|callback__do_login[password]' + ), + array( + 'field'=>'password', + 'label'=>'lang:user_password', + 'rules'=>'required|min_length[5]|max_length[32]' + ) + ); + $this->form_validation->set_rules($form_validation_config); + $this->form_validation->set_error_delimiters('', + ''); + + if ($this->form_validation->run() === FALSE) { $params = array( 'title' => $this->config->item('site_name'), 'css' => array( 'catalog.css' - ), - //'js' => array(), - //'metas' => array('description'=>'') + ), + //'js' => array(), + //'metas' => array('description'=>'') ); $this->load->library('html_head_params', $params); - + // ** // ** LOADING VIEWS // ** $this->load->view('html_begin', $this->html_head_params); $this->load->view('header', array('selected_menu' => 'login')); - + $this->load->view('user/login_view', array()); - + $this->load->view('footer'); $this->load->view('html_end'); } else { - header('Location: ' . site_url()); + if ($this->user_id !== NULL) + { + $this->session->set_userdata(array( + 'user_id'=> $this->user_id, + 'username'=> $this->username + )); + } + + header('Location: '. site_url()); return; } } - - public function _check_login($username, $password) + + public function _valid_username($username) + { + $this->load->helper('email'); + + if (valid_email($username)) + return TRUE; + else + return (preg_match('/^[a-z0-9\._]+$/', $username) == 1); + } + + public function _do_login($username, $field_password) { + $password = $this->input->post('password'); + + $this->load->model('users_model'); + $res_login = $this->users_model->login($username, $password); + + // First authentication of a user with LDAP, i.e. the user does not + // have an user_id in `users` DB table yet. + if ($res_login === TRUE) + return TRUE; + // Authentication failed + else if ($res_login === FALSE) + return FALSE; + + // Authentication when the user has an user_id in the DB. + $this->username = $res_login['username']; + $this->email = $res_login['email']; + $this->user_id = $res_login['id']; + return TRUE; } } diff --git a/application/hooks/hooks.php b/application/hooks/hooks.php new file mode 100644 index 0000000..91aab4c --- /dev/null +++ b/application/hooks/hooks.php @@ -0,0 +1,15 @@ +set_item('language', 'romanian'); + + $vars = get_defined_vars(); + print_r($vars); +} + +/* End of file catalog.php */ +/* Location: ./application/controllers/catalog.php */ diff --git a/application/language/english/form_validation_lang.php b/application/language/english/form_validation_lang.php new file mode 100644 index 0000000..2961a22 --- /dev/null +++ b/application/language/english/form_validation_lang.php @@ -0,0 +1,10 @@ +db === NULL) + { + $this->load->library('singleton_db'); + $this->db = $this->singleton_db->connect(); + } + } + + /** + * Check authentication credentials. $username can be username or e-mail. + * + * @param string $username + * @param string $password + */ + public function login($username, $password) + { + $this->load->helper('email'); + + // User logs with e-mail address. + if (! valid_email($username)) + $cond_user = "username = '$username'"; + else + $cond_user = "email = '$username'"; + + $enc_password = sha1($password); + + // TODO select only required fields. + $query = $this->db->query("SELECT * FROM `users` + WHERE $cond_user AND password = '$enc_password'"); + + if ($query->num_rows() !== 1) + return FALSE; + + return $query->row_array(); + } +} + +/* End of file users_model.php */ +/* Location: ./application/models/users_model.php */ diff --git a/application/models/videos_model.php b/application/models/videos_model.php index 5ba77c9..f92b07e 100644 --- a/application/models/videos_model.php +++ b/application/models/videos_model.php @@ -11,6 +11,8 @@ class Videos_model extends CI_Model { public function __construct() { + parent::__construct(); + if ($this->db === NULL) { $this->load->library('singleton_db'); diff --git a/application/views/header.php b/application/views/header.php index 242c710..53d68e7 100644 --- a/application/views/header.php +++ b/application/views/header.php @@ -46,11 +46,16 @@ >lang->line('ui_nav_menu_contact') ?> + + + +