<?
/*
echo '<script language=javascript> alert ("MAJ Serveur, revenez dans quelques minutes.");</script>';
echo '<script language="javascript" type="text/javascript">
<!--
window.location.replace("index.php");
-->
</script>';
exit;*/


////////////////
// test champ vide
session_start();
$_SESSION['login']=$_POST['login'];
include ('config.php');
include ('include/general.php');
$login=$_POST['login'];
//$_SESSION['login']=$login;
$password=$_POST['password'];
$mail=$_POST['mail'];
if($login=='' || $password=='' || $mail=='')
{
echo '<script language=javascript> alert ("Vous avez oublié de remplir un champ.");</script>';
echo '<script language="javascript" type="text/javascript">
<!--
window.location.replace("index.php");
-->
</script>';
unset($_SESSION['login']);
session_unregister('login');
session_destroy();
exit;
}
//
////////////////

////////////////
// test pass ou login dans la bdd /// fonction de login
$sql = "SELECT password FROM matgraph_users WHERE login= '$login' AND password != 'no' ";
$req = db_mysql_Query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
$data = mysql_fetch_array($req);
$passs = md5($password.$key);
if($data['password'] != $passs)
{
echo '<script language=javascript> alert ("Mauvais login / password. Merci de recommencer :  '.$login.' , '.$password.' , '.$sql.' ");</script>';
echo '<script language="javascript" type="text/javascript">
<!--
window.location.replace("index.php");
-->
</script>';
session_destroy();
exit;
}
else
{
	// Rien que pour voir, voyons si il n aurait pas un cookie avec un pseudo différent
	if( isset($_COOKIE['wtf_powa'] ) ) {
		$ancien_login = $_COOKIE['wtf_powa'];
	}
	SetCookie("wtf_powa", md5($login), mktime(0, 0, 0, 1, 1, 2038));
	// On test si il est banni
	$sql = "SELECT id,date, jour, heure, minute  FROM matgraph_banni WHERE login = '$login' ORDER by date DESC LIMIT 1";
	$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
	$num = mysql_num_rows($res);
	if( $num > 0 ) {
		$row = mysql_fetch_array($res);

		// On sauve les jours heures minutes de ban
		$jour = $row['jour'];
		$heure = $row['heure'];
		$minute = $row['minute'];
		$datetime = $row['date'];

		ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $datetime, $thetime);
		$time = mktime($thetime[4],$thetime[5],$thetime[6],$thetime[2],$thetime[3],$thetime[1]);

		$diftemps = time() - $time;

		if( $diftemps < 0 ) {
			// Il est toujours ban
			echo '<script language=javascript> alert ("Votre peine de ban n est pas expiré");</script>';
			echo '<script language="javascript" type="text/javascript">
			<!--
			window.location.replace("index.php");
			-->
			</script>';
			session_destroy();
			exit;
		} else {
			// Duree de ban expiré
			$sql = "DELETE FROM matgraph_banni WHERE id = $row[id] LIMIT 1";
			$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());

			// On met à jour également la date, pour qu il ne produise rien durant son ban
			$sql = "SELECT id, date FROM matgraph_users WHERE login = '$login'";
			$req = db_mysql_Query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
			while( $row = mysql_fetch_array($req) ) {
				$datetime = $row['date'];

				//echo "<br>$datetime";
				ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $datetime, $thetime);
				$new_date = date("Y-m-d H:i:s",mktime($thetime[4]+$heure,$thetime[5]+$minute,$thetime[6],$thetime[2],$thetime[3]+$jour,$thetime[1]));

				//echo "<br>$new_date";

				$sql2 = "UPDATE matgraph_users SET date = '".$new_date."' WHERE id = $row[id]";
				$res2 = db_mysql_Query($sql2)or die('erreur sql<br>'.$sql2.'<br>'.mysql_error());
			}
			$sql = "UPDATE matgraph_users SET vacance = '0000-00-00 00:00:00' WHERE login= '$login' AND password != 'no'";
			$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
			//$row = mysql_fetch_array($res);
		}
	}

//session_start();
session_register('login');
$_SESSION['login']="$login";
SetCookie("matgraph","1",mktime(0,0,0,1,1,2010),"$login","$passs","$mail");
//session_start ();
if ( !session_is_registered("login"))
{
header('location:apercu.php') ;
}
/*echo '<script language=javascript> alert ("Salut '.$login.' , Tu en as marre que le jeux ram et plante ? alors cliques sur les pubs !!");</script>';*/

$sql2 = "SELECT * FROM matgraph_users WHERE login='$login' AND id = idjoueur ORDER BY ID ASC limit 1";
$res2 = db_mysql_Query($sql2)or die('erreur sql<br>'.$sql2.'<br>'.mysql_error());
$row2 = mysql_fetch_array($res2);
$dataid['id'] = $row2['id'] ;
$idlogin = $dataid['id'] ;

// On loggue l ip

function get_ip() {
    if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    elseif(isset($_SERVER['HTTP_CLIENT_IP'])) {
        $ip  = $_SERVER['HTTP_CLIENT_IP'];
    }
    else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

$sql_ip = "SELECT * FROM matgraph_ip WHERE idlogin = $idlogin";
$res_ip = db_mysql_Query($sql_ip)or die('erreur sql<br>'.$sql_ip.'<br>'.mysql_error());
$num_ip = mysql_num_rows($res_ip);
if( $num_ip == 0 ) {
	$sql_ip2 = "INSERT INTO matgraph_ip
		(idlogin, ip, date_connexion, vientde, browser, hote, port)
		VALUES (
			$idlogin, '".get_ip()."', now(), '".$_SERVER['HTTP_REFERER']."', '".$_SERVER['HTTP_USER_AGENT']."', '".gethostbyaddr(get_ip())."', ".$_SERVER['REMOTE_PORT']."
		)";
} else {
	$sql_ip2 = "UPDATE matgraph_ip
	SET ip = '".get_ip()."', date_connexion = now(),
	vientde = '".$_SERVER['HTTP_REFERER']."', browser = '".$_SERVER['HTTP_USER_AGENT']."', hote = '".gethostbyaddr(get_ip())."', port = ".$_SERVER['REMOTE_PORT']."
	WHERE idlogin = $idlogin";
}
$res_ip2 = db_mysql_Query($sql_ip2)or die('erreur sql<br>'.$sql_ip2.'<br>'.mysql_error());
session_register('ip');
$_SESSION['ip']=get_ip();
if( isset($ancien_login) ) {

	if( $ancien_login != md5($login) ) {

		$sql_alog = "UPDATE matgraph_ip SET alogin = '".$ancien_login."' WHERE idlogin = $idlogin";
		$res_alog = db_mysql_Query($sql_alog)or die('erreur sql<br>'.$sql_alog.'<br>'.mysql_error());

	}

}

include ('recompte_pts_include.php');

echo '<script language="javascript" type="text/javascript">
<!--
window.location.replace("apercu.php");
-->
</script>';

}
//
////////////////

?>
<style type="text/css">
<!--
body {
	background-color: #000000;
}
-->
</style>

