<?php
/****************************************************************************************
SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV

Version 0.9.9

Copyright (C) 2006-2009  Sylvain FATOME
This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; 
either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program
(in the directory docs); 
if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
Boston, MA  02110-1301, USA.

Contact : galawa@free.fr

Web : http://smitst.homelinux.com
****************************************************************************************/


// Vérification des tentatives de hack
$pos=strpos($_SERVER['PHP_SELF'],"inc.php");
if ($pos===FALSE)
	$ok_inc=TRUE;
else
	$ok_inc=FALSE;
if ($ok_inc==FALSE) header("Location: ../index.php");


/***********************/
/*   Géolocalisation   */
/***********************/

// Retoune le pays du connecté
function pays_connect($no_ip)
{
	global
		$root;
		
	$chem_geoip=$root.REP."/inc/GeoIP.dat";
	$pays= "";
	$code_pays = "";

	// read GeoIP database
	$handle = geoip_open($chem_geoip, GEOIP_STANDARD);

	// map IP to country
	$code_pays = geoip_country_code_by_addr($handle, $no_ip);

	// close database handler
	geoip_close($handle);
	
	if ($code_pays <> "")
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rnom = "SELECT * FROM " .TBPAYS. " WHERE pays_code='".$code_pays."'";
		$rsnom = mysql_query($rnom) or die(mysql_error());
		if (mysql_num_rows($rsnom) == 1) // Uniquement UN SEUL enregistrement !
		{
			if ($rowpays = mysql_fetch_array($rsnom)) 
			{
				extract ($rowpays);
				$pays =  $pays_nom;
			}
		}
	
		mysql_free_result($rsnom);
		mysql_close($conn2);
	}
	
	return $pays;
}

function pays_connect_code($no_ip)
{
	global
		$root;
		
	$chem_geoip=$root.REP."/inc/GeoIP.dat";
	
	$pays= "";

	$exist_file=@is_file($chem_geoip);
	if ($exist_file)
	{
		$code_pays = "";

		// read GeoIP database
		$handle = geoip_open($chem_geoip, GEOIP_STANDARD);
	
		// map IP to country
		$code_pays = geoip_country_code_by_addr($handle, $no_ip);
	
		// close database handler
		geoip_close($handle);
		
		if ($code_pays <> "")
		{
			$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			$rnom = "SELECT * FROM " .TBPAYS. " WHERE pays_code='".$code_pays."'";
			$rsnom = mysql_query($rnom) or die(mysql_error());
			if (mysql_num_rows($rsnom) == 1) // Uniquement UN SEUL enregistrement !
			{
				if ($rowpays = mysql_fetch_array($rsnom)) 
				{
					extract ($rowpays);
					$pays =  $pays_code;
				}
			}
		
			mysql_free_result($rsnom);
			mysql_close($conn2);
		}
	}	
	return $pays;
}
?> 
