working at user login
authorCalin Burloiu <calin.burloiu@gmail.com>
Thu, 15 Sep 2011 15:13:59 +0000 (18:13 +0300)
committerCalin Burloiu <calin.burloiu@gmail.com>
Thu, 15 Sep 2011 15:13:59 +0000 (18:13 +0300)
12 files changed:
application/config/autoload.php
application/config/config.php
application/config/hooks.php
application/controllers/catalog.php
application/controllers/user.php
application/hooks/hooks.php [new file with mode: 0644]
application/language/english/form_validation_lang.php [new file with mode: 0644]
application/models/users_model.php [new file with mode: 0644]
application/models/videos_model.php
application/views/header.php
application/views/user/login_view.php
css/default.css

index c3d8699..c7ea0f0 100644 (file)
@@ -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');
 
 
 /*
index 755fc91..f707636 100644 (file)
@@ -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;
 
index a4ad2be..b758599 100644 (file)
 |
 */
 
-
+$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
index 79d0f96..743342e 100644 (file)
@@ -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)
index 33c50d9..33dd727 100644 (file)
  * @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('<span class="error">',
+                       '</span>');
+
+               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 (file)
index 0000000..91aab4c
--- /dev/null
@@ -0,0 +1,15 @@
+<?php 
+
+// TODO delete this if not used
+function select_language()
+{
+       $CI =& get_instance();
+       
+       //$config->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 (file)
index 0000000..2961a22
--- /dev/null
@@ -0,0 +1,10 @@
+<?php
+
+include('system/language/english/form_validation_lang.php');
+
+$lang['_valid_username']       = "You must enter an e-mail address or a valid username.";
+$lang['_do_login']                     = "Wrong %s, or wrong %s.";
+
+
+/* End of file form_validation_lang.php */
+/* Location: ./system/language/english/form_validation_lang.php */
\ No newline at end of file
diff --git a/application/models/users_model.php b/application/models/users_model.php
new file mode 100644 (file)
index 0000000..ad4caf0
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+
+/**
+ * Class Users_model models user information from DB
+ * 
+ * @category   Model
+ * @author             calinburloiu
+ *
+ */
+class Users_model extends CI_Model {
+       public $db = NULL;
+
+       public function __construct()
+       {
+               parent::__construct();
+
+               if ($this->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 */
index 5ba77c9..f92b07e 100644 (file)
@@ -11,6 +11,8 @@ class Videos_model extends CI_Model {
        
        public function __construct()
        {
+               parent::__construct();
+               
                if ($this->db === NULL)
                {
                        $this->load->library('singleton_db');
index 242c710..53d68e7 100644 (file)
        <?php echo ($selected_menu == 'contact' ? 'class="selected"' : '') ?>><?php echo $this->lang->line('ui_nav_menu_contact') ?>
        </a></li>
 
+       <?php if (isset($username) && $username): ?>
+       <li class="menu-right"><a href="<?php echo '#'//site_url('register') ?>"
+               <?php echo ($selected_menu == 'account' ? 'class="selected"' : '') ?>><?php echo $username ?></a></li>
+       <?php else: ?>
        <li class="menu-right"><a href="<?php echo '#'//site_url('register') ?>"
                <?php echo ($selected_menu == 'register' ? 'class="selected"' : '') ?>><?php echo $this->lang->line('ui_nav_menu_register') ?></a></li>
                
        <li class="menu-right"><a href="<?php echo site_url('user/login') ?>" 
                <?php echo ($selected_menu == 'login' ? 'class="selected"' : '') ?>><?php echo $this->lang->line('ui_nav_menu_log_in') ?></a></li>
+       <?php endif; ?>
 </ul>
 
 <div id="header">
index db1b9bc..4389cdb 100644 (file)
@@ -1,20 +1,31 @@
-<table>
+
+<?php echo form_open('user/login') ?>
+<table class="form">
        <tr>
                <th><?php echo $this->lang->line('user_username_or_email'). ': ' ?></th>
                <td>
-                       <input type="text" name="username" size="32" />
+                       <input type="text" name="username" size="16" value="<?php echo set_value('username') ?>" />
                </td>
        </tr>
+       <tr>
+               <td></td>
+               <td><?php echo form_error('username') ?></td>
+       </tr>
        <tr>
                <th><?php echo $this->lang->line('user_password'). ': ' ?></th>
                <td>
-                       <input type="password" name="password" size="32" />
+                       <input type="password" name="password" size="16" value="" />
                </td>
        </tr>
+       <tr>
+               <td></td>
+               <td><?php echo form_error('password') ?></td>
+       </tr>
        <tr>
                <td></td>
                <td>
                        <input type="submit" value="<?php echo $this->lang->line('ui_nav_menu_log_in') ?>" />
                </td>
        </tr>
-</table>
\ No newline at end of file
+</table>
+</form>
\ No newline at end of file
index 2c0b2fe..852928c 100644 (file)
@@ -26,6 +26,17 @@ h1
        margin-bottom: 8px;
 }
 
+table.form th
+{
+       text-align: right;
+}
+
+.error
+{
+       color: red;
+       font-weight:bold;
+}
+
 .inline
 {
        vertical-align: middle;