<?php

error_reporting(E_ALL);

require '../smarty/libs/Smarty.class.php';
//require_once 'DB.php';

include ("./function.glob.php");

//$dsn = "mysql://ta0022:qdfpdf@localhost/ta0022_1";

//$database = DB::connect($dsn);

//if (DB::isError($database)) {
//        die ($database->getMessage());
//}

$smarty = new Smarty;

// This code demonstrates how to lookup the country by IP Address

include("../geoIP/geoip.inc");

// Uncomment if querying against GeoIP/Lite City.
//include("geoipcity.inc");

$gi = geoip_open("../geoIP/GeoIP.dat",GEOIP_STANDARD);

// No IP found (will be overwritten by for
// if any IP is found behind a firewall)
$ip = FALSE;

// If HTTP_CLIENT_IP is set, then give it priority
if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
  $ip = $_SERVER["HTTP_CLIENT_IP"];
}

// User is behind a proxy and check that we discard RFC1918 IP addresses
// if they are behind a proxy then only figure out which IP belongs to the
// user.  Might not need any more hackin if there is a squid reverse proxy
// infront of apache.

if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  // Put the IP's into an array which we shall work with shortly.
  $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
  if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
  
  for ($i = 0; $i < count($ips); $i++) {
    // Skip RFC 1918 IP's 10.0.0.0/8, 172.16.0.0/12 and
    // 192.168.0.0/16
    if (!preg_match('/^(?:10|172\.(?:1[6-9]|2\d|3[01])|192\.168)\./', $ips[$i])) {
      if (version_compare(phpversion(), "5.0.0", ">=")) {
        if (ip2long($ips[$i]) != false) {
          $ip = $ips[$i];
          break;
        }
      } else {
        if (ip2long($ips[$i]) != -1) {
          $ip = $ips[$i];
          break;
        }
      }
    }
  }
}

// print $ip ? $ip : $_SERVER['REMOTE_ADDR'];

//print geoip_country_code_by_addr($gi, $ip ? $ip : $_SERVER['REMOTE_ADDR']);

$geo_lang = geoip_country_code_by_addr($gi, $ip ? $ip : $_SERVER['REMOTE_ADDR']);

#print geoip_country_code_by_addr($gi, "217.129.252.214") . "\t" .
#     geoip_country_name_by_addr($gi, "217.129.252.214") . "\n";

geoip_close($gi);



$contact = 0;





$url_array=explode("/",$_SERVER['REQUEST_URI']);

if (isset($_SERVER['HTTPS'])) {
        $site='https://'.$_SERVER['HTTP_HOST'];
} else {
        $site='http://'.$_SERVER['HTTP_HOST'];
}

if($url_array[1]) {
	//$language = strtolower(mysql_escape_string($url_array[1]));
	$language = strtolower(htmlspecialchars($url_array[1]));
}

if(!empty($url_array[2])) {
	if($url_array[2]) {
//		$content_name = strtolower(mysql_escape_string($url_array[2]));
//		$content_name = mysql_escape_string($url_array[2]);
		$content_name = htmlspecialchars($url_array[2]);
		if($content_name == "Contact_Us")
			$contact=1;
	}
} else {
	$contact=1;
}

if(!empty($url_array[3])) {
        if($url_array[3]) {
                //$content_name = mysql_escape_string($url_array[3]);
                $content_name = htmlspecialchars($url_array[3]);
        }
}

//$smarty->assign('llll', $language);

/* check here if language */

if(!isset($language)) {
	$language="en";
}

if(!isset($content_name)) {
	$content_name="main";
}

//$main_content = $database->getRow("SELECT data_file, html_title, header_image FROM `content` WHERE language='$language' AND content_name='$content_name'");

//$menu = $database->getAll("SELECT name, link, type FROM `menu_data` WHERE language='$language' ORDER BY pos ASC");

//$smarty->assign('menu', $menu);

$smarty->assign('blog_index', '/var/www/marsensing/www.marsensing.com/htdocs/blog/index.html');

$smarty->assign('bla', $_SERVER['REMOTE_ADDR']);
$smarty->assign('lang', $geo_lang);
$smarty->assign('language', $language);
$smarty->assign('site', $site);
$smarty->assign('content_name' ,$content_name);
$smarty->assign('contact', $contact);

//$smarty->assign('main_content', $main_content);

$smarty->compile_check = true;
$smarty->debugging = false;

$smarty->display('main.tpl');

?>
