<?php
if(isset($_REQUEST['galaxi']) && $_REQUEST['galaxi']!='' && isset($_REQUEST['system']) && $_REQUEST['system']!='' && isset($_REQUEST['position']) && $_REQUEST['position']!='' && isset($_REQUEST['contenu'])){
	include('config.php');
	$query3="SELECT login FROM matgraph_users WHERE galaxi='".$_REQUEST['galaxi']."' AND system='".$_REQUEST['system']."' AND position='".$_REQUEST['position']."'";
	$sql3=mysql_query($query3) or die('Erreur MySQL : '.mysql_error());
	list($login_fr)=mysql_fetch_array($sql3);
	if($login_fr==''){
		echo "Cette plan&egrave;te n'existe pas";
	}else{
		echo htmlentities($login_fr);
	}
	exit;
}elseif(isset($_REQUEST['contenu'])){
	exit;
}
include ('logue.php');
include ('entete2.php');
include ('entete.php');
include ('ressource.php');
echo "<script type='text/javascript'>
function verif_champs(){
	if(document.forms[0].elements['galaxi']!='' && document.forms[0].elements['system']!='' && document.forms[0].elements['position']!=''){
		AfficheForm('add_friend','ami.php','reponse');
		return false;
	}
}
</script>";
echo"<div align=\"center\"><form name=\"colo\" method=\"post\" action=\"colonie.php\" target=\"_parent\">
<table align=\"center\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
   <tr>
     <td width=\"100\" rowspan=\"2\" valign=\"middle\">
       <div align=\"center\">
         <input name=\"page\" value=\"" . $PHP_SELF . "\" type=\"hidden\" id=\"page\">
         <select name=\"idcolo\" onchange=submit()>";
include ('colo2.php');
if(isset($_REQUEST['galaxi']) && $_REQUEST['galaxi']!='' && isset($_REQUEST['system']) && $_REQUEST['system']!='' && isset($_REQUEST['position']) && $_REQUEST['position']!=''){
	$query="SELECT idjoueur FROM matgraph_users WHERE galaxi='".$_REQUEST['galaxi']."' AND system='".$_REQUEST['system']."' AND position='".$_REQUEST['position']."'";
	$sql=mysql_query($query) or die('Erreur MySQL : '.mysql_error());
	list($id_fr)=mysql_fetch_array($sql);
	$query="SELECT COUNT(id) FROM matgraph_users WHERE id='".$idlogin."' AND ami_d LIKE '%".$id_fer."%'";
	$sql=mysql_query($query) or die('Erreur MySQL : '.mysql_error());
	if(mysql_num_rows($sql) > 1){
		echo "Demande déjà envoyée auparavant...";
	}else{
		$query="UPDATE matgraph_users SET ami_d='".$row2['ami_d'].$id_fr.";' WHERE id='".$idlogin."'";
		$sql=mysql_query($query) or die('Erreur MySQL : '.mysql_error());
		echo "Demande envoyée...";
	}
	echo "<script type='text/javascript'>
	function reload(){
		window.location.replace('ami.php');
	}
	setTimeout('reload',500);
	</script>";
}
if(isset($_REQUEST['friend_dem_id']) && $_REQUEST['friend_dem_id']!=''){
	$ami_d=preg_replace('#'.$_REQUEST['friend_dem_id'].';#','',$row2['ami_d']);
	$query="UPDATE matgraph_users SET ami_d='".$ami_d."' WHERE id='".$idlogin."'";
	$sql=mysql_query($query) or die('Erreur MySQL : '.mysql_error());
	echo "Demande retiré...";
}
if(isset($_REQUEST['friend_accepter_id']) && isset($_REQUEST['option']) && $_REQUEST['friend_accepter_id']!='' && $_REQUEST['option']!=''){
	if($_REQUEST['option']=='1'){
		$query="UPDATE matgraph_users SET ami=CONCAT(ami,'".$_REQUEST['friend_accepter_id']."',';') WHERE id='".$idlogin."'";
		$sql=mysql_query($query) or die('Erreur MySQL : '.mysql_error());
		$query="SELECT ami,ami_d FROM matgraph_users WHERE id='".$_REQUEST['friend_accepter_id']."'";
		$sql=mysql_query($query) or die('Erreur MySQL : '.mysql_error());
		list($ami,$ami_d)=mysql_fetch_array($sql);
		$ami_d=preg_replace('#'.$idlogin.';#','',$ami_d);
		$ami.=$idlogin.";";
		$query="UPDATE matgraph_users SET ami='".$ami."',ami_d='".$ami_d."' WHERE id='".$_REQUEST['friend_accepter_id']."'";
		$sql=mysql_query($query) or die('Erreur MySQL : '.mysql_error());
		echo "Ami ajouté avec succés...";
	}elseif($_REQUEST['option']=='2'){
		$query="SELECT ami_d FROM matgraph_users WHERE id='".$_REQUEST['friend_accepter_id']."'";
		$sql=mysql_query($query) or die('Erreur MySQL : '.mysql_error());
		list($ami_d)=mysql_fetch_array($sql);
		$ami_d=preg_replace('#'.$idlogin.';#','',$ami_d);
		$query="UPDATE matgraph_users SET ami_d='".$ami_d."' WHERE id='".$_REQUEST['friend_accepter_id']."'";
		$sql=mysql_query($query) or die('Erreur MySQL : '.mysql_error());
		echo "Ami refusé avec succés...(il est possible qu'il vous redemande une invitation)";
	}
}
$query="SELECT ami,ami_d FROM matgraph_users WHERE id='".$idlogin."'";
$sql=db_mysql_Query($query);
list($ami,$ami_d)=mysql_fetch_array($sql);
$ami=explode(';',$ami);$ami_d=explode(';',$ami_d);
array_pop($ami);array_pop($ami_d);
echo "<table align='center' width='80%' cellspacing='0' cellpadding='0'><tr style='background-color:#222222;'><td colspan='4'><b>L</b>iste des Amis</td></tr>
<tr align='center'><td>Pseudo</td><td>Coordonnées</td><td>Alliance</td><td>Statut</td></tr>";
if(count($ami)>=1){
	$i=1;
	foreach($ami as $ami_id){
		$query1="SELECT login,galaxi,system,position,ally,date FROM matgraph_users WHERE id='".$ami_id."'";
		$sql1=db_mysql_Query($query1);
		$row=mysql_fetch_array($sql1);
		if ( ($i % 2) == 0 ){
    		echo "<tr align='center'>";
    	}else{
    		echo "<tr align='center' style =\"background-color:#222222;\">";
		}
		$query2="SELECT nom FROM matgraph_ally WHERE id='".$row[4]."'";
		$sql2=db_mysql_Query($query2);
		list($ally)=mysql_fetch_array($sql2);
		if($ally==''){$ally='-';}
		echo "<td>".$row[0]."</td><td>".$row[1].":".$row[2].":".$row[3]."</td><td>".$ally."</td>";
		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]);
		if($time < (time()-120) ){
			echo '<td><font color="#FF0000">Offline</font></td></tr>'; 
		}else{
			echo '<td><font color="#33FF33">Online</font></td></tr>';
		}
	}
}else{
	echo "<tr align='center' style =\"background-color:#222222;\"><td colspan='4'>Auncun Ami</td></tr>";
}
echo "<tr align='center'><td colspan='4'><div id='reponse' style='width:200px;'></div><form name='add_friend' method=post action='ami.php'><input type='text' size='3' maxlength='3' name='galaxi' id='galaxi' onchange='verif_champs();'>:<input type='text' size='3' maxlength='3' name='system' id='system' onchange='verif_champs();'>:<input type='text' size='2' maxlength='2' name='position' id='position' onchange='verif_champs();'><input type='submit' value='Ajouter un Ami'></form></td></tr>
</table>
<br/><br/>
<table width='80%' align='center' cellspacing='0' cellpadding='0'><tr style='background-color:#222222;'><td colspan='2'><b>L</b>iste des Proposition</td></tr>";
$query="SELECT id,login FROM matgraph_users WHERE ami_d LIKE '%".$idlogin."%'";
$sql=mysql_query($query) or die ('Erreur MySQL : '.mysql_error());
$num=mysql_num_rows($sql);
if($num>0){
	echo "<tr align='center'><td>Pseudo</td><td>Action</td>";
	$i=1;
	while($ami_p=mysql_fetch_array($sql)){
		if ( ($i % 2) == 0 ){
    		echo "<tr align='center'>";
    	}else{
    		echo "<tr align='center' style =\"background-color:#222222;\">";
		}
		echo "<td>".$ami_p[1]."</td><td><form method=post action='ami.php'><input type='hidden' name='friend_accepter_id' value='".$ami_p[0]."' /><input type='hidden' name='option'/><input type='button' value='Accepter' onclick='this.form.elements[1].value=1;this.form.submit()' /><input type='button' onclick='this.form.elements[1].value=2;this.form.submit()' value='Refuser' /></form></td></tr>";
	}
}else{
	echo "<tr align='center'><td colspan='2'>Liste Vide</td></tr>";
}
echo "</table>
<br/><br/>
<table width='80%' align='center' cellspacing='0' cellpadding='0'><tr style='background-color:#222222;'><td colspan='2'><b>L</b>iste des Demandes D'amis</td></tr>
<tr align='center'><td>Pseudo</td><td>Action</td>";
if(count($ami_d)>=1){
	$i=1;
	foreach($ami_d as $ami_id){
		$query1="SELECT login FROM matgraph_users WHERE idjoueur='".$ami_id."' LIMIT 0,1";
		$sql1=mysql_query($query1) or die('Erreur MySQL :'.mysql_error());
		$row4=mysql_fetch_array($sql1);
		if ( ($i % 2) == 0 ){
    		echo "<tr align='center'>";
    	}else{
    		echo "<tr style =\"background-color:#222222;vertical-align:center\" align='center'>";
		}
		echo "<td>".$row4[0]."</td><td><form method=post action='ami.php'><input type='hidden' name='friend_dem_id' value='".$ami_id."' /><input type='submit' value='Annuler' /></form></td></tr>";
	}
}else{
	echo "<tr align='center' style =\"background-color:#222222;\"><td colspan='2'>Liste Vide</td></tr>";
}
echo "</table><br/>";
?>