<?	
include('config.php');
require_once ('include/general.php');

$login = $_POST["login"];
$pass = $password = $_POST["pass"];
$mail = $_POST["mail"];
$permmail = 'NON';
$planete = $_POST["planete"];

$login = strtolower($login);
$password = strtolower($password);
$mail = strtolower($mail);
$pass = md5($password.$key);



$testlogin = db_mysql_Query("SELECT * FROM matgraph_users WHERE login ='$login'");
$testmail = db_mysql_Query("SELECT * FROM matgraph_users WHERE mail ='$mail'"); 
 
if ( mysql_num_rows( $testlogin ) != 0 ) {

	echo '<script language=javascript> alert ("Le login '.$login.' est déjà pris.");</script>';

	include('http://www.sg-univers.fr/inscription_form.php');

	exit;

}

$sql = ('SELECT * FROM matgraph_users ORDER BY galaxi, system, position');
$res = db_mysql_Query($sql);
while ($row = mysql_fetch_array($res))
{
// echo "<p>" . $row['galaxi'] . " - " . $row['system'] . " - " . $row['position'] . "</p>";
$galaxi[] = $row['galaxi'];
$system[] = $row['system'];
$position[] = $row['position'];
}

// echo ' Soit le dernier du tableau après tri : ';
// echo '<p>' . $galaxi[sizeof($galaxi)-1] . ' - ' . $system[sizeof($system)-1] . ' - ' . $position[sizeof($position)-1] . '</p>';

$galaxi = $galaxi[sizeof($galaxi)-1];
$system = $system[sizeof($system)-1];
$position = $position[sizeof($position)-1];

if ($position == 1) {
$New_position = 3;
$New_system = $system;
$New_galaxi = $galaxi; }

if ($position == 2) {
$New_position = 3;
$New_system = $system;
$New_galaxi = $galaxi; }

if ($position == 3) {
$New_position = 5;
$New_system = $system;
$New_galaxi = $galaxi; }

if ($position == 4) {
$New_position = 6;
$New_system = $system;
$New_galaxi = $galaxi; }

if ($position == 5) {
$New_position = 7;
$New_system = $system;
$New_galaxi = $galaxi; }

if ($position == 6) {	
$New_position = 7;
$New_system = $system;
$New_galaxi = $galaxi; }

if ($position == 7) {
$New_position = 3;
$New_system = $system + 1;
$New_galaxi = $galaxi; }

if ($position == 8) {
$New_position = 3;
$New_system = $system + 1;
$New_galaxi = $galaxi; }

if ($position == 9) {
$New_position = 3;
$New_system = $system + 1;
$New_galaxi = $galaxi; }

if ($position == 10) {
$New_position = 3;
$New_system = $system + 1;
$New_galaxi = $galaxi; }

$final_position = $New_position ;
if($New_system > 100) {
$final_system = 1;
$final_galaxi = $New_galaxi + 1; }
else {
$final_system = $New_system;
$final_galaxi = $New_galaxi; }

if(strftime("%Y-%m-%d %H:%M:%S", time()) < '2006-05-06 20:00:00')
{
	$date_commencement = '2006-05-06 20:00:00';
}
else
{
	$date_commencement = strftime("%Y-%m-%d %H:%M:%S", time());
}

$img = rand(1, 20);
$requete = "INSERT INTO matgraph_users VALUES ('', '', '$_POST[login]', '$pass', '$_POST[mail]', '$_POST[permmail]', '$_POST[planete]', '1', 'no', '0','25', '0000-00-00 00:00:00', '$img', '0', '0', '0', '$final_galaxi', '$final_system', '$final_position', '180', '0', '0', '0', '0', '0', '0', '$date_commencement', '0', '0', '0000-00-00 00:00:00', '600', '0', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', '400', '0', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', '0', '0', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', '0', '0', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', '0', '0', '0000-00-00 00:00:00', '0', '0', '0000-00-00 00:00:00', '0', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', '0', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0')";
$resultat = db_mysql_Query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());

$sql = "SELECT id FROM matgraph_users WHERE mail='$_POST[mail]' AND password ='$pass'";
$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
$row = array( 'id' => mysql_insert_id() );

$req2 = "UPDATE matgraph_users SET idjoueur = '$row[id]' WHERE id='$row[id]'";
$ress2 = db_mysql_Query($req2);

$requete = "INSERT INTO matgraph_newsletter VALUES('','$_POST[mail]')";
$resultat = db_mysql_Query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());

$req = "INSERT INTO matgraph_idjoueur VALUES ('', '$row[id]', '$row[id]')";
$ress = db_mysql_Query($req);

	if (!$resultat) 
		die('Erreur SQL! <br>'.$requete.'<br>'.mysql_error());
	else
		echo '<script language="javascript" type="text/javascript">
<!--
window.location.replace("http://www.sg-univers.fr/inscription_merci.php");
-->
</script>';




$subject = "Bienvenue sur StarGate Univer";
$message = "<html>
<head>
 <title>Bienvenue sur StarGate Univer</title>
</head>
<body>
Bonjours $login,<br>
Bienvenue sur StarGate Univer.<br>

Vous êtes priés de conserver cet e-mail dans vos archives. Voici les informations concernant votre compte:<br><br>

----------------------------<br>
Nom d'utilisateur: $login<br>
Mot de passe: $password<br>
----------------------------<br>

http://www.sg-univers.fr/<br>

Cordialement<br>
M@tttt34
</body>
</html>";

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: <ns21309.ovh.net>\r\n";
$headers .= "Reply-To: contact@sg-univers.info\r\n";


if( mail($mail, $subject, $message, $headers) ) {

	echo "Merci.<br/>Un e-mail a été envoyer a $email. contenant les informations de votre compte";

}
else {

	echo "<script language=javascript> alert ('Il est impossible d\'envoyer l\'email de confirmation. Contactez le webmaster.');</script>";

	include("http://www.sg-univers.fr/inscription_form.php");

	exit;
} 

?>

