<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

	public function index()
	{
		save_current_url(base_url() . 'login/');
		
		if( ! $this->session->userdata('user_type')) {
			$this->lang->load('account_general', get_language());
				
			$data['page_title'] = $this->lang->line('website_name') . ' - ' . $this->lang->line('hf_login');
			
			$this->load->view('login', $data);
		}
		else {
			redirect('home', 'refresh');
		}		
	}
	
	public function submit()
	{
		if( ! $this->session->userdata('user_type')) {
			$this->lang->load('account_general', get_language());
			
			$username = trim($this->input->post('username', TRUE));
			$password = trim($this->input->post('password', TRUE));
			$remember_password = (isset($_POST['remember_password']) ? 1 : 0);

			if ($username == '') {
				echo $this->lang->line('login_enter_username_message');
				exit();
			} else if ($password == '') {
				echo $this->lang->line('login_enter_password_message');
				exit();
			}
			
			#Check for username availbility
			$query = $this->db->query("SELECT member_id, full_name, username, password_origin, password, package_type, member_type, allowed_withdraw, unlimited_withdraw, allowed_transfer, allowed_convert, allowed_topup, active, is_locked, last_login_date FROM sw_members WHERE username = ? LIMIT 1", array($username));
			if($query->num_rows() > 0) {
				$row = $query->row();
				
				$verify_password = sha1(md5(sha1(md5($password)) . sha1(md5($this->config->item('encryption_key')))));
				
				if($row->password == $verify_password) {
					if($row->is_locked == 0) {
					    if($row->active == 1) {
    						$this->session->set_userdata(array('member_id' => $row->member_id, 'member_name' => $row->full_name, 'member_username' => $row->username, 'member_package_type' => $row->package_type, 'member_member_type' => $row->member_type, 'member_allowed_withdraw' => $row->allowed_withdraw, 'member_unlimited_withdraw' => $row->unlimited_withdraw, 'member_allowed_transfer' => $row->allowed_transfer, 'member_allowed_convert' => $row->allowed_convert, 'member_allowed_topup' => $row->allowed_topup, 'member_active' => $row->active, 'member_login_datetime' => date('Y-m-d H:i'), 'user_type' => MEMBER));
    						
    						if($remember_password == 1) {
    							$this->session->set_userdata('remember_username', $row->username);
    							$this->session->set_userdata('remember_password', $row->password_origin);
    						}
    						else {
    							$this->session->set_userdata('remember_username', '');
    							$this->session->set_userdata('remember_password', '');
    						}
    						
    						$this->db->trans_start();
    						
    						$this->db->query("UPDATE sw_members SET last_login_date = ? WHERE member_id = ? AND username = ? LIMIT 1", array(time(), $row->member_id, $row->username));
    						
    						$this->load->model('member_model');
    						$this->member_model->insert_member_log($row->member_id, LOG_LOGIN);
    						
    						$this->db->trans_complete();
    						
    						if ($this->db->trans_status() === TRUE) {
    							echo STATUS_200;
    						}
    						else {
    							echo $this->lang->line('login_failed');
    						}
					    }else{
							echo $this->lang->line('login_failed');
						}
					}
					else {
						echo $this->lang->line('login_failed');
					}	
				}
				else {
					echo $this->lang->line('login_invalid_username_or_password');
				}
			}
			else {
				echo $this->lang->line('login_invalid_username_or_password');
			}
		}
		else {
			redirect('home', 'refresh');
		}		
	}
}

/* End of file login.php */
/* Location: ./application/controllers/login.php */