<?php
include ('logue.php'); 
include ('entete2.php');
include ('entete.php');

// en heures
$delais_avant_push = 48;
$coef_or = 1.33;
$coef_cristal = 2;
$coef_naquadah = 4;
$coef_malps = 3900;
$coef_prs = 8000;
$coef_grs = 40000;
$coef_eppzs = 10000;

if ( $_GET['f'] == "supprimer" && $_POST['id_destinataire'] != NULL && $_POST['id_envoyeur'] != NULL) {
	$id_envoyeur = $_POST['id_envoyeur'];
	$id_destinataire = $_POST['id_destinataire'];
	$sql = "DELETE FROM `Pushing_Systeme` WHERE `id_envoyeur` = $id_envoyeur AND `id_destinataire` = $id_destinataire";
	$res = db_mysql_Query($sql) or die('Erreur SQL: <br>$sql<br>'.mysql_error());
	echo "<script language=\"javascript\" type=\"text/javascript\"> window.location.replace(\"sgu_systeme_push_print.php\"); </script>";
}

elseif ( $_GET['f'] == "bannir" && $_POST['id_envoyeur'] != NULL && $_POST['id_destinataire'] != NULL && $_POST['nbr_jours'] != NULL && $_POST['motif'] != NULL) {
	$id_envoyeur = $_POST['id_envoyeur'];
	$id_destinataire = $_POST['id_destinataire'];
	$motif = $_POST['motif'];
	$nbr_jours = $_POST['nbr_jours'];
	$finduban = strftime("%Y-%m-%d %H:%M:%S", time()+ (round(60*60*6*$nbr_jours)));
	echo $finduban;

	// login du joueur fort
	$req_dest = "SELECT login FROM matgraph_users WHERE id = '$id_destinataire'";
	$res_dest = db_mysql_Query($req_dest) or die('Erreur SQL: <br>$req_dest<br>'.mysql_error());
	$row_dest = mysql_fetch_array($res_dest);
	// login du joueur faible
	$req_env = "SELECT login FROM matgraph_users WHERE id = '$id_envoyeur'";
	$res_env = db_mysql_Query($req_env) or die('Erreur SQL: <br>$req_env<br>'.mysql_error());
	$row_env = mysql_fetch_array($res_env);
	
	// verification de si le joueur fort est pas déjà bannis
	$sql_vb_dest = "SELECT * FROM `matgraph_banni` WHERE `login` = '$row_dest[login]'";
	$res_vb_dest = db_mysql_Query($sql_vb_dest) or die('Erreur SQL: <br>$sql_vb_dest<br>'.mysql_error());
	$num_vb_dest = mysql_num_rows($res_vb_dest);
	
	if ( $num_vb_dest == NULL ) {
		
		// déactivation du mode vacance
		$sql_vac = "UPDATE matgraph_users SET vacance = '0000-00-00 00:00:00' WHERE login = '$row_dest[login]'";
		$res_vac = db_mysql_Query($sql_vac)or die('Erreur SQL:<br>$sql_vac<br>'.mysql_error());

		// kick
		$sql_kick = "UPDATE matgraph_connexion SET secu = 'ban' WHERE idjoueur = '$id_destinataire'";
		$res_kick = db_mysql_Query($sql_kick)or die('Erreur SQL: <br>$sql_kick<br>'.mysql_error());

		// ban
		$sql_ban = "INSERT INTO `matgraph_banni` (`id`, `login`, `jour`, `heure`, `minute`, `date`, `motif`, `Intervenant`) VALUES (NULL, '$row_dest[login]', '$nbr_jours', '0', '0', '$finduban', '$motif', '');";
		$res_ban = db_mysql_Query($sql_ban) or die('Erreur SQL: <br>$sql_ban<br>'.mysql_error());

	}

	// verification de si le joueur fort est pas déjà bannis
	$sql_vb_env = "SELECT * FROM `matgraph_banni` WHERE `login` = '$row_env[login]'";
	$res_vb_env = db_mysql_Query($sql_vb_env) or die('Erreur SQL: <br>$sql_vb_env<br>'.mysql_error());
	$num_vb_env = mysql_num_rows($res_vb_env);
	
	if ( $num_vb_env == NULL ) {
	
		// déactivation de mode vacance
		$sql_vac = "UPDATE matgraph_users SET vacance = '0000-00-00 00:00:00' WHERE login = '$row_env[login]'";
		$res_vac = db_mysql_Query($sql_vac)or die('Erreur SQL:<br>$sql_vac<br>'.mysql_error());
		
		// kick
		$sql_kick = "UPDATE matgraph_connexion SET secu = 'ban' WHERE idjoueur = '$id_envoyeur'";
		$res_kick = db_mysql_Query($sql_kick)or die('Erreur SQL: <br>$sql_kick<br>'.mysql_error());
		
		// ban
		$sql_ban = "INSERT INTO `matgraph_banni` (`id`, `login`, `jour`, `heure`, `minute`, `date`, `motif`, `Intervenant`) VALUES (NULL, '$row_env[login]', '$nbr_jours', '0', '0', '$finduban', '$motif', '');";
		$res_ban = db_mysql_Query($sql_ban) or die('Erreur SQL: <br>$sql_ban<br>'.mysql_error());
		
	}

	// suppression de l'entrée
	$sql = "DELETE FROM `Pushing_Systeme` WHERE `id_envoyeur` = $id_envoyeur AND `id_destinataire` = $id_destinataire";
	$res = db_mysql_Query($sql) or die('Erreur SQL: <br>$sql<br>'.mysql_error());

	// echo "<script language=\"javascript\" type=\"text/javascript\"> window.location.replace(\"sgu_systeme_push_print.php\");<//script>";
}

else {
	$datetime_now = strftime("%Y-%m-%d %H:%M:%S", time());

	$sql2 = "SELECT * FROM `Pushing_Systeme` WHERE date < '$datetime_now'";
	$res2 = db_mysql_Query($sql2) or die('Erreur SQL: <br>$sql2<br>'.mysql_error());
	$num = mysql_num_rows($res2);

	if ( $num > 0 ) {
		while ( $row = mysql_fetch_array($res2) ) {
			// calcul des valeurs en fer de "produit"
			$grs = $row[grs] / $coef_grs;
			$eppzs = $row[eppzs] / $coef_eppzs;
			$prs = $row[prs] / $coef_prs;
			$malps = $row[malps] / $coef_malps;
			$naquadah = $row[naquadah] / $coef_naquadah;
			$cristal = $row[cristal] / $coef_cristal;
			$or = round( $row[orr] / $coef_or );
			//calcul des points
			$pts_destinataire = number_format(floor(($row[pts_destinataire] / 1000)), 0, ',', ' ');
			$pts_envoyeur = number_format(floor(($row[pts_envoyeur] / 1000)), 0, ',', ' ');
			//calcul de l'heure du push
			ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $row[date], $thetime);
			$time = mktime($thetime[4],$thetime[5],$thetime[6],$thetime[2],$thetime[3],$thetime[1]);
			$datetime =  strftime("%Y-%m-%d %H:%M:%S", $time - ( 60*60*$delais_avant_push ));
			// login joueur fort
			$req_dest = "SELECT login FROM matgraph_users WHERE id = $row[id_destinataire]";
			$res_dest = db_mysql_Query($req_dest) or die('Erreur SQL: <br>$req_dest<br>'.mysql_error());
			$row_dest = mysql_fetch_array($res_dest);
			// login joueur faible
			$req_env = "SELECT login FROM matgraph_users WHERE id= $row[id_envoyeur]";
			$res_env = db_mysql_Query($req_env) or die('Erreur SQL: <br>$req_env<br>'.mysql_error());
			$row_env = mysql_fetch_array($res_env);
			echo " ";
			echo "
			<div style=\"text-align: center;\">
				<table style=\"background-color: rgb(17, 17, 17); width: 720px; height: 20px;\">
					<tr>
						<td>
							$datetime :: <span style=\"color: rgb(255, 0, 0);\">$row_dest[login] ($pts_destinataire pts)</span> :: <span style=\"color: rgb(51, 204, 0);\">$row_env[login] ($pts_envoyeur pts)</span>
						</td>
						<td>
							<div style=\"text-align: right;\">
								<form name=\"supprimer\" method=\"post\" action=\"sgu_systeme_push_print.php?f=supprimer\" target=\"_parent\">
									<input type=\"hidden\" name=\"id_destinataire\" value=\"$row[id_destinataire]\">
									<input type=\"hidden\" name=\"id_envoyeur\" value=\"$row[id_envoyeur]\">
									<input type=\"image\"style=\"border: 0px solid rgb(126, 126, 126);\" alt=\"del\" src=\"imgs/del.gif\" border=\"0\" height=\"15\" width=\"20\">

								</form>
							</div>
						</td>
					</tr>
				</table>
				<table style=\"text-align: center; width: 720px; height: 50px;\">
					<tr>
						<td style=\"background-color: rgb(34, 34, 34); width: 80px;\">
							Fer: <b>$row[fer]</b>
						</td>
						<td style=\"background-color: rgb(17, 17, 17); width: 80px;\">
							Or: <b>$or</b>
						</td>
						<td style=\"background-color: rgb(34, 34, 34); width: 80px;\">
							Cristal: <b>$cristal</b>
						</td>
						<td style=\"background-color: rgb(17, 17, 17); width: 80px;\">
							Naquadah: <b>$naquadah</b>
						</td>
						<td style=\"background-color: rgb(34, 34, 34); width: 80px;\">
							Militaires: <b>$row[militaires]</b>
						</td>
						<td style=\"background-color: rgb(17, 17, 17); width: 80px;\">
							Eppzs: <b>$eppzs</b>
						</td>
						<td style=\"background-color: rgb(34, 34, 34); width: 80px;\">
							Malps: <b>$malps</b>
						</td>
						<td style=\"background-color: rgb(17, 17, 17); width: 80px;\">
							PRs: <b>$prs</b>
						</td>
						<td style=\"background-color: rgb(34, 34, 34); width: 80px;\">
							GRs: <b>$grs</b>
						</td>
					</tr>
				</table>
				<table style=\"text-align: right; background-color: rgb(17, 17, 17); width: 720px; height: 25px;\">
					<tr>
						<td>
							<form name=\"bannir\" method=\"post\" action=\"sgu_systeme_push_print.php?f=bannir\" target=\"_parent\">
								<input type=\"hidden\" name=\"id_destinataire\" value=\"$row[id_destinataire]\">
								<input type=\"hidden\" name=\"id_envoyeur\" value=\"$row[id_envoyeur]\">
								Nbr de jours: <input type=\"text\" size=\"5\" name=\"nbr_jours\" value=\"0\" maxlength=\"4\">
								Motif: <input type=\"text\" size=\"25\" name=\"motif\" value=\"Push\">
								<input type=\"submit\" name=\"Submit\" value=\"Bannir\" Style=\"border:1px solid #7E7E7E;\">
							</form>
						</td>
					</tr>
				</table>
			<br>
			</div>
			";
		}
	}
	else {
		echo "<div style=\"text-align: center;\">Aucun push d&eacute;tect&eacute;.</div><br>";
	}
}
?>
