working at video comments
[living-lab-site.git] / application / controllers / user.php
index 793d7f4..43b26a3 100644 (file)
@@ -10,6 +10,7 @@ class User extends CI_Controller {
 
        private $import = FALSE;
        private $activated_account = TRUE;
+       private $user_id = NULL;
 
        public function __construct()
        {
@@ -22,6 +23,11 @@ class User extends CI_Controller {
        public function index()
        {
        }
+       
+       public function test($user_id = 1)
+       {
+
+       }
 
        /**
        * Login a user and then redirect it to the last page which must be encoded
@@ -33,11 +39,10 @@ class User extends CI_Controller {
        public function login($redirect = '')
        {
                $this->load->library('form_validation');
-                       
                $this->form_validation->set_error_delimiters('<span class="error">',
                        '</span>');
 
-               if ($this->form_validation->run('signin') === FALSE)
+               if ($this->form_validation->run('login') === FALSE)
                {
                        $params = array(        'title' =>
                                                                        $this->lang->line('ui_nav_menu_login')
@@ -64,7 +69,8 @@ class User extends CI_Controller {
                else
                {
                        if (! $this->activated_account)
-                               header('Location: '. site_url('catalog/test'));
+                               header('Location: '
+                                       . site_url("user/activate/{$this->user_id}"));
                        else if (! $this->import)
                        {
                                // Redirect to last page before login. 
@@ -105,8 +111,32 @@ class User extends CI_Controller {
                        
                $this->form_validation->set_error_delimiters('<span class="error">',
                                        '</span>');
+               $error_upload = '';
+
+               if ($this->form_validation->run('register'))
+               {
+                       $b_validation = TRUE;
+                       
+                       if ($_FILES['picture']['tmp_name'])
+                       {
+                               // Upload library
+                               $config_upload['upload_path'] = './data/user_pictures';
+                               $config_upload['file_name'] = 
+                                       str_replace('.', '-', $this->input->post('username')) .'-';
+                               $config_upload['allowed_types'] = 'gif|jpg|png';
+                               $config_upload['max_size'] = '10240';
+                               $this->load->library('upload', $config_upload);
+                               
+                               $b_validation = $this->upload->do_upload('picture');
+                               $error_upload = 
+                                       $this->upload->display_errors('<span class="error">',
+                                                       '</span>');
+                       }
+               }
+               else
+                       $b_validation = FALSE;
                
-               if ($this->form_validation->run('register') === FALSE)
+               if (! $b_validation)
                {
                        // Edit account data if logged in, otherwise register.
                        if ($user_id = $this->session->userdata('user_id'))
@@ -136,7 +166,8 @@ class User extends CI_Controller {
                                array('selected_menu' => $selected_menu));
                        
                        $main_params['content'] = $this->load->view('user/register_view', 
-                               array('userdata'=> $userdata, 'redirect'=> $redirect),
+                               array('userdata'=> $userdata, 'redirect'=> $redirect,
+                                       'error_upload'=> $error_upload),
                                TRUE);
                        $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
                        $this->load->view('main', $main_params);
@@ -156,6 +187,26 @@ class User extends CI_Controller {
                        $data['ui_lang'] = $this->input->post('ui-lang');
                        $data['time_zone'] = $this->input->post('time-zone');
                        
+                       // Handle picture if one was uploaded.
+                       if ($_FILES['picture']['tmp_name'])
+                       {
+                               $upload_data = $this->upload->data();
+                               $this->load->library('image');
+                               $this->image->load($upload_data['full_path']);
+                               // Resize original to a maximum size.
+                               if ($this->image->get_width() * $this->image->get_height()
+                                               > 640*480)
+                               {
+                                       $this->image->save_thumbnail(
+                                               $upload_data['full_path'],
+                                               640, 480, IMAGETYPE_AUTO);
+                               }
+                               // Create thumbnail.
+                               $data['picture'] = $upload_data['file_name'];
+                               $this->image->save_thumbnail($upload_data['file_path']
+                                               . $upload_data['file_name']. '-thumb.jpg', 120, 90);
+                       }
+                       
                        // Update session user data.
                        $this->_update_session_userdata($data);
                        
@@ -167,6 +218,9 @@ class User extends CI_Controller {
                                        $data['password'] = $this->input->post('new-password');
                                
                                $this->users_model->set_userdata($user_id, $data);
+                               
+                               // Redirect to last page before login.
+                               header('Location: '. site_url(urldecode_segments($redirect)));
                        }
                        // Registration
                        else
@@ -175,10 +229,13 @@ class User extends CI_Controller {
                                $data['password'] = $this->input->post('password');
                                
                                $this->users_model->register($data);
+                               $user_id = $this->users_model->get_userdata($data['username'],
+                                               "id");
+                               $user_id = $user_id['id'];
+                               
+                               // Redirect account activation page.
+                               header('Location: '. site_url("user/activate/$user_id"));
                        }
-                       
-                       // Redirect to last page before login.
-                       header('Location: '. site_url(urldecode_segments($redirect)));
                }
        }
        
@@ -263,10 +320,160 @@ class User extends CI_Controller {
                $this->load->view('html_end');
        }
        
-       public function activate($user_id, $activation_code)
+       public function activate($user_id, $method='', $activation_code='')
        {
-               $user_id = intval($user_id);
-               echo ''. $this->users_model->activate_account($user_id, $activation_code);
+               $user_id = intval($user_id);            
+               $res_form_validation = FALSE;
+               
+               if ($method == 'code')
+               {
+                       if (! $activation_code)
+                               $res_form_validation = $this->form_validation->run('activate');
+                       // Activation code is provided in URL.
+                       else
+                       {
+                               if ($this->_valid_activation_code($activation_code)
+                                               && $this->users_model->activate_account($user_id,
+                                                       $activation_code))
+                               {
+                                       $this->session->set_flashdata('msg', sprintf(
+                                               $this->lang->line('user_msg_activated_account'), 
+                                               site_url('user/login')));
+                                       header('Location: '. site_url('message/info'));
+                                       return;
+                               }
+                               else
+                               {
+                                       $this->session->set_flashdata('msg',
+                                               $this->lang->line('user_msg_wrong_activation_code'));
+                                       header('Location: '. site_url('message/error'));
+                                       return;
+                               }
+                       }
+               }
+               else if ($method == 'resend')
+               {
+                       $res_form_validation =
+                               $this->form_validation->run('resend_activation');
+               }
+               
+               $userdata = $this->users_model->get_userdata($user_id,
+                               'email, a.activation_code');
+               $email = $userdata['email'];
+               $activated_account = ($userdata['activation_code'] == NULL);
+               
+               if ($activated_account)
+               {
+                       $this->session->set_flashdata('msg', sprintf(
+                                               $this->lang->line('user_msg_activated_account'), 
+                                               site_url('user/login')));
+                       header('Location: '. site_url('message/info'));
+                       return;
+               }
+               
+               $this->load->library('form_validation');
+                       
+               $this->form_validation->set_error_delimiters('<span class="error">',
+                                       '</span>');
+               
+               if ($res_form_validation === FALSE)
+               {
+                       $params = array(
+                               'title'=> $this->lang->line('user_title_activation')
+                                       .' &ndash; '
+                                       . $this->config->item('site_name'),
+                               //'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());
+
+                       // Show form
+                       $main_params['content'] = 
+                               $this->load->view('user/activate_view',
+                               array(  'user_id'=> $user_id,
+                                               'email'=> $userdata['email']),
+                               TRUE);
+                       
+                       $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
+                       $this->load->view('main', $main_params);
+               
+                       $this->load->view('footer');
+                       $this->load->view('html_end');
+               }
+               else
+               {
+                       if ($method == 'code')
+                       {
+                               // Redirect to a message which tells the user that the
+                               // activation was successful.
+                               $this->session->set_flashdata('msg', sprintf(
+                                               $this->lang->line('user_msg_activated_account'), 
+                                               site_url('user/login')));
+                               header('Location: '. site_url('message/info'));
+                               return;
+                       }
+                       else if ($method == 'resend')
+                       {
+                               // Redirect to resent message
+                               $this->session->set_flashdata('msg', sprintf(
+                                               $this->lang->line('user_msg_activation_resent'),
+                                               $this->input->post('email')));
+                               header('Location: '. site_url('message/info'));
+                               return;
+                       }
+               }
+       }
+       
+       public function recover_password()
+       {
+               $this->load->library('form_validation');
+                       
+               $this->form_validation->set_error_delimiters('<span class="error">',
+                       '</span>');
+
+               if ($this->form_validation->run('recover_password') === FALSE)
+               {
+                       $params = array(        'title' =>
+                                                                       $this->lang->line(
+                                                                               'user_title_password_recovery')
+                                                                               .' &ndash; '
+                                                                               . $this->config->item('site_name'),
+                                                               //'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' => 
+                                       'recover_password'));
+
+                       $main_params['content'] = $this->load->view(
+                               'user/recover_password_view', array(),
+                               TRUE);
+                       
+                       $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
+                       $this->load->view('main', $main_params);
+                               
+                       $this->load->view('footer');
+                       $this->load->view('html_end');
+               }
+               else
+               {
+                       // Redirect to resent message
+                       $this->session->set_flashdata('msg', sprintf(
+                                       $this->lang->line('user_msg_password_recovery_email_sent'),
+                                       $this->input->post('username'),
+                                       $this->input->post('email')));
+                       header('Location: '. site_url('message/info'));
+                       return;
+               }
        }
        
        public function _update_session_userdata($data)
@@ -298,6 +505,15 @@ class User extends CI_Controller {
                return (preg_match('/[\d]{4}-[\d]{2}-[\d]{2}/', $date) === 1);
        }
        
+       public function _postprocess_birth_date($date)
+       {
+               // If the user entered no birth date NULL needs to be inserted into DB.
+               if (! $date)
+                       return NULL;
+               
+               return $date;
+       }
+       
        public function _valid_old_password($old_password, $field_username)
        {
                if (! $old_password)
@@ -330,6 +546,11 @@ class User extends CI_Controller {
                
                return TRUE;
        }
+       
+       public function _valid_activation_code($activation_code)
+       {
+               return (preg_match('/^[a-fA-F0-9]{16}$/', $activation_code) == 1);
+       }
 
        public function _do_login($username, $field_password)
        {
@@ -345,6 +566,7 @@ class User extends CI_Controller {
                if ($user['activation_code'] !== NULL)
                {
                        $this->activated_account = FALSE;
+                       $this->user_id = $user['id'];
                        return TRUE;
                }
                
@@ -358,6 +580,61 @@ class User extends CI_Controller {
                $this->import = (isset($user['import']) ? $user['import'] : FALSE);
                return TRUE;
        }
+       
+       public function _do_activate($activation_code)
+       {
+               $user_id = $this->input->post('user-id');
+               if ($user_id === FALSE)
+                       return FALSE;
+               $user_id = intval($user_id);
+               
+               return $this->users_model->activate_account($user_id,
+                               $activation_code);
+       }
+       
+       public function _do_resend_activation($email)
+       {
+               $user_id = $this->input->post('user-id');
+               if ($user_id === FALSE)
+                       return FALSE;
+               $user_id = intval($user_id);
+               
+               $this->users_model->set_userdata($user_id,
+                       array('email'=> $email));
+               
+               return $this->users_model->send_activation_email($user_id, $email);
+       }
+       
+       public function _username_exists($username)
+       {
+               $userdata = $this->users_model->get_userdata($username);
+               
+               if (! $userdata)
+                       return FALSE;
+               
+               return TRUE;
+       }
+       
+       public function _internal_account($username)
+       {
+               $userdata = $this->users_model->get_userdata($username, 'auth_src');
+               if (! $userdata)
+                       return FALSE;
+
+               if ($userdata['auth_src'] != 'internal')
+                       return FALSE;
+               
+               return TRUE;
+       }
+       
+       public function _do_recover_password($username)
+       {
+               $email = $this->input->post('email');
+               if (! $email)
+                       return FALSE;
+               
+               return $this->users_model->recover_password($username, $email);
+       }
 }
 
 /* End of file user.php */