<?
/*
    ANISEED, NISEED System and Database
    Copyright (C) 2007 Olivier Tassy, Fabrice Daian, Patrick Lemaire

    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 3 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.  If not, see <http://www.gnu.org/licenses/>.

*/
include ("connection.php");

//Calcul du code suivant ip
function numByIP($IP)
{
$tab=explode(".",$IP);
$A=$tab[0];$B=$tab[1];$C=$tab[2];$D=$tab[3];
$num=$A*(256*256*256)+$B*(256*256)+$C*(256)+$D;
return $num;
}

//Calcul du pays suivant le code
function numToCountry($num)
{
$handle = fopen("./GeoIP.csv", "r");
if ($handle) {
   while (!feof($handle)) {
       $buffer = fgets($handle, 4096);
       
	$tab=explode(",",$buffer);
	
	$cherche=$tab[3];
	$cherche=str_replace("\"","",$cherche);

	//print "NUM=$num\tACTU=$cherche<br>";
	if ($num<$cherche) 
		{	
		//fclose($handle);
		break;
		}
   }
   fclose($handle);
}
return trim(str_replace("\"","",$tab[5]));
}


function ipToCountry($IP)
{
return numToCountry(numByIP($IP));
}


$IP=$_SERVER["REMOTE_ADDR"]; 

if ($IP==$instituteAddress) return;
if ($IP=="139.124.62.148") return;

$jour=date("d");
$nommois=date("m");
$annee=date("Y"); 
$heure = date("H:i");

$country=ipToCountry($IP);

$requete="INSERT INTO \"LOG\" (\"IP\",\"DATE\",\"TIME\",\"COUNTRY\",\"VERSION\") values ('$IP','$annee/$nommois/$jour','$heure','$country','3');";

pg_query($c,"begin");
pg_query($c,$requete);
pg_query($c,"commit");





?>
