<?php 

class App_extends extends CI_Model {

	public function AccessLogger(){

		date_default_timezone_set('America/Sao_Paulo');
		$country_info = $this->getCountryInfo();
        
		$data = array(
			'origin' => $this->agent->referrer(),
			'ip' => $this->input->ip_address(),
			'browser' => $this->agent->browser(),
			'platform' => $this->agent->platform(),
			'created_at' => date('Y-m-d H:i:s'),
			'player' => $this->session->userdata('id'),
			'player_name' => $this->session->userdata('nome'),
			'country' => $country_info
		);

		$this->db->insert('access_log', $data);
	}
	private function getCountryInfo() {
		$ip = $this->input->ip_address();
		$api_url = "https://api.ipgeolocation.io/ipgeo?apiKey=d4f74157d06d4540b0d7c4242d20af3b&ip=" . $ip;
		$curl_options = array(
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_URL => $api_url
		);
		$curl = curl_init();
		curl_setopt_array($curl, $curl_options);
		$response = curl_exec($curl);
		curl_close($curl);
		$response_object = json_decode($response);
		if (isset($response_object->country_name))
		{
			return $response_object->country_name;
		}
	}
}