Update positioning in contact page
[living-lab-site.git] / application / controllers / user.php
index 2e4b4ee..169d5d2 100644 (file)
@@ -26,7 +26,31 @@ class User extends CI_Controller {
        
        public function test($user_id = 1)
        {
-
+//             echo extension_loaded('gd') ? 'gd' : 'nu';
+       }
+       
+       // DEBUG
+       public function show_session()
+       {
+               if (ENVIRONMENT == 'production')
+                       die();
+                       
+               var_dump($this->session->all_userdata());
+       }
+       // DEBUG
+       public function destroy_session()
+       {
+               if (ENVIRONMENT == 'production')
+                       die();
+                       
+               $this->session->sess_destroy();
+       }
+       
+       public function ajax_get_captcha()
+       {
+               $this->load->library('captcha');
+               $captcha = $this->captcha->get_captcha();
+               echo $captcha['image'];
        }
 
        /**
@@ -82,24 +106,8 @@ class User extends CI_Controller {
                {
                        if ($b_openid)
                        {
-                               $this->lang->load('openid');
-                               $this->load->library('openid');
-                               $this->config->load('openid');
-                               $request_to = site_url('user/check_openid_login');
-                               $req = $this->config->item('openid_required');
-                               $opt = $this->config->item('openid_optional');
-                               $policy = site_url('user/openid_policy');
-                               $pape_policy_uris = $this->config->item('openid_papa_policies');
-                               
-                               $this->openid->set_request_to($request_to);
-                               $this->openid->set_trust_root(base_url());
-                               $this->openid->set_args(null);
-                               $this->openid->set_sreg(true, $req, $opt, $policy);
-                               if (!empty($pape_policy_uris))
-                                       $this->openid->set_pape(true, $pape_policy_uris);
-                               
-                               // Redirection to OP site will follow.
-                               $this->openid->authenticate($this->input->post('openid'));
+                               $this->users_model->openid_begin_login(
+                                               $this->input->post('openid'));
                                return;
                        }
                        
@@ -124,48 +132,34 @@ class User extends CI_Controller {
        
        public function check_openid_login()
        {
-               $this->lang->load('openid');
-               $this->load->library('openid');
-               $this->config->load('openid');
-               $request_to = site_url('user/check_openid_login');
-
-               $this->openid->set_request_to($request_to);
-               $response = $this->openid->get_response();
-
-               switch ($response->status)
+               $user = $this->users_model->openid_complete_login();
+               
+               // Authentication failed.
+               if ($user == Auth_OpenID_CANCEL)
                {
-               case Auth_OpenID_CANCEL:
                        $this->load->helper('message');
-                       show_info_msg_page($this, $this->lang->line('openid_cancel'));
-                       break;
-               case Auth_OpenID_FAILURE:
+                       show_error_msg_page($this, $this->lang->line('openid_cancel'));
+                       return;
+               }               
+               else if ($user == Auth_OpenID_FAILURE)
+               {
                        $this->load->helper('message');
-                       show_error_msg_page($this,
-                                       $this->_format_message('openid_failure',
-                                                       $response->message));
-                       break;
-               case Auth_OpenID_SUCCESS:
-                       $openid = $response->getDisplayIdentifier();
-                       $esc_identity = htmlspecialchars($openid, ENT_QUOTES);
-
-                       $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
-                       $sreg = $sreg_resp->contents();
-                       
-                       // Get registration informations
-                   $ax = new Auth_OpenID_AX_FetchResponse();
-                       $obj = $ax->fromSuccessResponse($response);
-
-                       //echo 'nickname('. $sreg_resp->get('nickname'). ')';
-                       echo var_dump($obj->data);
-                       echo '<br />';
-                       echo var_dump($sreg);
-//                     foreach ($sreg as $key => $value)
-//                     {
-//                             $data['success'] .= $this->_set_message('openid_content', array($key, $value), array('%s', '%t'));
-//                     }
-
-                       break;
+                       show_error_msg_page($this, $this->lang->line('openid_failure'));
+                       return;
                }
+
+               // Authentication successful: set session with user data.
+               $this->session->set_userdata(array(
+                       'user_id'=> $user['id'],
+                       'username'=> $user['username'],
+                       'auth_src'=> $user['auth_src'],
+                       'time_zone'=> $user['time_zone']
+               ));
+               
+               if ($user['import'])
+                       header('Location: '. site_url('user/account'));
+               else
+                       header('Location: '. site_url());
        }
        
        public function openid_policy()
@@ -185,6 +179,7 @@ class User extends CI_Controller {
                $this->session->unset_userdata('user_id');
                $this->session->unset_userdata('username');
                $this->session->unset_userdata('auth_src');
+               $this->session->unset_userdata('roles');
                $this->session->unset_userdata('time_zone');
                
                header('Location: '. site_url(urldecode_segments($redirect)));
@@ -195,6 +190,8 @@ class User extends CI_Controller {
                $this->load->library('form_validation');
                $this->load->helper('localization');
                $this->load->helper('date');
+               
+               $user_id = $this->session->userdata('user_id');
                        
                $this->form_validation->set_error_delimiters('<span class="error">',
                                        '</span>');
@@ -222,19 +219,30 @@ class User extends CI_Controller {
                }
                else
                        $b_validation = FALSE;
-               
+
                if (! $b_validation)
                {
                        // Edit account data if logged in, otherwise register.
-                       if ($user_id = $this->session->userdata('user_id'))
+                       // ** ACCOUNT
+                       if ($user_id)
                        {
                                $userdata = $this->users_model->get_userdata(intval($user_id));
+                               if (substr($userdata['username'], 0, 8) == 'autogen_')
+                                       $userdata['autogen_username'] =
+                                               substr($userdata['username'], 8);
                                $selected_menu = 'account';
+                               $captcha = FALSE;
                        }
+                       // ** REGISTER
                        else
                        {
                                $userdata = FALSE;
                                $selected_menu = 'register';
+                               
+                               // CAPTCHA
+                               $this->load->library('captcha');
+                               $captcha = $this->captcha->get_captcha();
+                               $captcha = $captcha['image'];
                        }
                        
                        $params = array('title' =>
@@ -254,7 +262,7 @@ class User extends CI_Controller {
                        
                        $main_params['content'] = $this->load->view('user/register_view', 
                                array('userdata'=> $userdata, 'redirect'=> $redirect,
-                                       'error_upload'=> $error_upload),
+                                       'error_upload'=> $error_upload, 'captcha'=> $captcha),
                                TRUE);
                        $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
                        $this->load->view('main', $main_params);
@@ -264,10 +272,14 @@ class User extends CI_Controller {
                }
                else
                {
-                       $user_id = $this->input->post('user-id');
+                       // TODO: Security problem!
+                       //$user_id = $this->input->post('user-id');
+                       if ($this->input->post('username'))
+                               $data['username'] = $this->input->post('username');
                        $data['email'] = $this->input->post('email');
                        $data['first_name'] = $this->input->post('first-name');
                        $data['last_name'] = $this->input->post('last-name');
+                       $data['sex'] = intval($this->input->post('sex'));
                        $data['birth_date'] = $this->input->post('birth-date');
                        $data['country'] = $this->input->post('country');
                        $data['locality'] = $this->input->post('locality');
@@ -294,6 +306,7 @@ class User extends CI_Controller {
                                                . $upload_data['file_name']. '-thumb.jpg', 120, 90);
                        }
                        
+                       // TODO: To much info as session data?
                        // Update session user data.
                        $this->_update_session_userdata($data);
                        
@@ -302,7 +315,7 @@ class User extends CI_Controller {
                        {
                                $password = $this->input->post('new-password');
                                if ($password)
-                                       $data['password'] = $this->input->post('new-password');
+                                       $data['password'] = $password;
                                
                                $this->users_model->set_userdata($user_id, $data);
                                
@@ -314,6 +327,7 @@ class User extends CI_Controller {
                        {
                                $data['username'] = $this->input->post('username');
                                $data['password'] = $this->input->post('password');
+                               $data['auth_src'] = 'internal';
                                
                                $this->users_model->register($data);
                                $user_id = $this->users_model->get_userdata($data['username'],
@@ -335,6 +349,17 @@ class User extends CI_Controller {
        {
                // TODO handle user not found
                
+               $user_id = $this->session->userdata('user_id');
+               if ($user_id)
+               {
+                       if (intval($user_id) & USER_ROLE_ADMIN)
+                               $allow_unactivated = TRUE;
+                       else
+                               $allow_unactivated = FALSE;
+               }
+               else
+                       $allow_unactivated = FALSE;
+               
                $this->load->config('localization');
                $this->load->helper('date');
                $this->lang->load('date');
@@ -357,15 +382,16 @@ class User extends CI_Controller {
                // User's videos
                $this->load->model('videos_model');
                $vs_data['videos'] = $this->videos_model->get_videos_summary(
-                       NULL, $username, intval($videos_offset),
-                       $this->config->item('videos_per_page'));
+                               NULL, $username, intval($videos_offset),
+                               $this->config->item('videos_per_page'), 'hottest',
+                               $allow_unactivated);
                
                // Pagination
                $this->load->library('pagination');
                $pg_config['base_url'] = site_url("user/profile/$username/");
                $pg_config['uri_segment'] = 4;
                $pg_config['total_rows'] = $this->videos_model->get_videos_count(
-                       NULL, $username);
+                       NULL, $username, $allow_unactivated);
                $pg_config['per_page'] = $this->config->item('videos_per_page');
                $this->pagination->initialize($pg_config);
                $vs_data['pagination'] = $this->pagination->create_links();
@@ -567,7 +593,18 @@ class User extends CI_Controller {
        public function _update_session_userdata($data)
        {
                foreach ($data as $key=> $val)
-                       $this->session->set_userdata($key, $val);
+               {
+                       if ($this->session->userdata($key))
+                               $this->session->set_userdata($key, $val);
+               }
+       }
+       
+       public function _is_username_unique($username)
+       {
+               if ($this->users_model->get_userdata($username))
+                       return FALSE;
+               
+               return TRUE;
        }
        
        public function _valid_username($username)
@@ -602,12 +639,12 @@ class User extends CI_Controller {
                return $date;
        }
        
-       public function _valid_old_password($old_password, $field_username)
+       public function _valid_old_password($old_password)
        {
                if (! $old_password)
                        return TRUE;
                
-               $username= $this->input->post($field_username);
+               $username= $this->session->userdata('username');
                
                if ($this->users_model->login($username, $old_password))
                        return TRUE;
@@ -627,7 +664,7 @@ class User extends CI_Controller {
        
        public function _required_by_register($param)
        {
-               $user_id = $this->input->post('user-id');
+               $user_id = $this->session->userdata('user_id');
                
                if (! $user_id && ! $param)
                        return FALSE;
@@ -663,6 +700,7 @@ class User extends CI_Controller {
                        'user_id'=> $user['id'],
                        'username'=> $user['username'],
                        'auth_src'=> $user['auth_src'],
+                       'roles'=> $user['roles'],
                        'time_zone'=> $user['time_zone']
                ));
                $this->import = (isset($user['import']) ? $user['import'] : FALSE);
@@ -703,6 +741,13 @@ class User extends CI_Controller {
                return TRUE;
        }
        
+       public function _check_captcha($word)
+       {
+               $this->load->library('captcha');
+               
+               return $this->captcha->check_captcha($word);
+       }
+       
        public function _internal_account($username)
        {
                $userdata = $this->users_model->get_userdata($username, 'auth_src');