<?php
defined('BASEPATH') OR exit('No direct script access allowed');
defined('DIR_SEPARATOR') || define('DIR_SEPARATOR', '/');
class Tracker extends CI_Controller {
	
	public function __construct() {
		parent::__construct();
		//$this->load->model('setup/Setupsystem','setup');
		//$this->load->model('contact/contactmain', 'cmain');
	}
	
	public function save() {
		$this->load->model('tracker/Trackermain','trk');
		
		$trk['ip'] = (isset($_POST['ip'])) ? $_POST['ip'] : NULL;
		$trk['full'] = (isset($_POST['full'])) ? $_POST['full'] : NULL;
		$trk['route'] = (isset($_POST['route'])) ? $_POST['route'] : NULL;
		$trk['city'] = (isset($_POST['city'])) ? $_POST['city'] : NULL;
		$trk['state'] = (isset($_POST['state'])) ? $_POST['state'] : NULL;
		$trk['postcode'] = (isset($_POST['postcode'])) ? $_POST['postcode'] : NULL;
		$trk['country_long'] = (isset($_POST['country_long'])) ? $_POST['country_long'] : NULL;
		$trk['country_short'] = (isset($_POST['country_short'])) ? $_POST['country_short'] : NULL;
		$trk['lat'] = (isset($_POST['lat'])) ? $_POST['lat'] : NULL;
		$trk['long'] = (isset($_POST['long'])) ? $_POST['long'] : NULL;
		$trk['org'] = (isset($_POST['org'])) ? $_POST['org'] : NULL;
		$trk['href'] = (isset($_POST['href'])) ? $_POST['href'] : NULL;
		
		if($io = (isset($_POST['io'])) ? $_POST['io'] : NULL) {
			if(!$trk['city']) $trk['city'] = (isset($io['city'])) ? $io['city'] : NULL;
			if(!$trk['state']) $trk['state'] = (isset($io['region'])) ? $io['region'] : NULL;
			if(!$trk['postcode']) $trk['postcode'] = (isset($io['postal'])) ? $io['postal'] : NULL;
			if(!$trk['country_long']) $trk['country_long'] = (isset($io['country'])) ? $this->trk->getCountryName($io['country']) : NULL;
			if(!$trk['country_short']) $trk['country_short'] = (isset($io['country'])) ? $io['country'] : NULL;
			
			if(isset($io['loc']) && $io['loc']) {
				$loc = explode(",",$io['loc']);
				if(!$trk['lat']) $trk['lat'] = trim($loc[0]);
				if(!$trk['long']) $trk['long'] = trim($loc[1]);
			}
		}
		
		// GET CURRENT URL
		$c = (isset($_POST['c']) && trim($_POST['c'])) ? trim($_POST['c']) : "home";
		$a = (isset($_POST['a']) && trim($_POST['a'])) ? trim($_POST['a']) : "index";
		$u = (isset($_POST['u']) && trim($_POST['u'])) ? trim($_POST['u']) : NULL;
		$trk['path'] = $c.DIR_SEPARATOR.$a.DIR_SEPARATOR;
		$trk['param'] = ltrim(rtrim(str_replace($trk['path'],"",$u), "/"),"/");		
		$trk['param'] = (ltrim(rtrim($trk['param'],'/'),'/') == ltrim(rtrim($trk['path'],'/'),'/')) ? NULL : $trk['param'];
		$trk['href'] = ltrim(rtrim(str_replace(base_url(),"",$trk['href']), "/"),"/");	
		
		// GET REMOTE IP OF CLIENT (IF FOUND) INSTEAD OF PUBLIC IP
		$trk['ip'] = (isset($_SERVER['REMOTE_ADDR']) && trim($_SERVER['REMOTE_ADDR']) && trim($_SERVER['REMOTE_ADDR']) != '::1') ? trim($_SERVER['REMOTE_ADDR']) : $trk['ip'];
		
		// SAVE TRACKER
		$this->trk->saveTracker($trk);		
		
		exit();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
?>




























