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

echo"<script type=\"text/javascript\">
<!--
function smiley(codedusmiley)
{
window.document.rep.presentation.value += codedusmiley+' ';
}
function smiley2(codedusmiley)
{
window.document.rep.txtinterne.value += codedusmiley+' ';
}
// -->
</script>
<style type=\"text/css\">
button,input {
border:0px solid #7E7E7E;
background-color:#000000;
font-family:'trebuchet ms',sans-serif;
color:white;
}
textarea {
border:1px solid #7E7E7E;
background-color:#000000;
font-family:'trebuchet ms',sans-serif;
color:white;
}
select, option {
background-color:#000000;
color:white;
}
A.menulink2 {
width: 150px;
text-decoration: none;
font-family:Verdana;
font-size:11px;
color: #FF3300;
BORDER: none;
border: solid 1px #000000;
}

A.menulink2:hover {
border: solid 1px #000000;
background-color:#000000;
}
</style>
<STYLE TYPE=text/css>
.Style1 {color: #0000FF}
.Style2 {color: #FF0000}
</STYLE>
</head>
<BODY TEXT=\"#ffffff\" LINK=\"#ffffff\" VLINK=\"#ffffff\" ALINK=\"#ffffff\" bgcolor=\"#000000\">";


$id = $_REQUEST["id"];

$row2[ally] = $id ;

echo '
<div align="center"><a href="'.$_SERVER['PHP_SELF'].'?id='.$id.'">L\'Alliance</a> || <a href="'.$_SERVER['PHP_SELF'].'?id='.$id.'&amp;editer=defcon">Modifier DEFCON</a> ||
<a href="'.$_SERVER['PHP_SELF'].'?id='.$id.'&amp;editer=presentation">Modifier Présentation</a></div><br /><br /><br />';
if($_REQUEST['editer']=='defcon'){
	if($_POST['niveau']!=''){
		$sql = "UPDATE matgraph_ally SET defcon='".$_POST['niveau']."' WHERE id='".$row2['ally']."'";
		$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
	}
	$sql = "SELECT * FROM matgraph_ally WHERE id='".$row2['ally']."'";
	$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
	$row = mysql_fetch_array($res);
	
	echo '
	<div align="center">Choississez le niveau de DEFCON a attribué à cette alliance.
	<br><font color="#009900">DEFCON 5 : Préparation normale en temps de paix.</font>
	<br><font color="#999933">DEFCON 4 : Renseignement accru et mesures de sécurité renforcées.</font>
	<br><font color="#FFFF00">DEFCON 3 : Accroissement de la préparation des forces.</font>
	<br><font color="#FF9900">DEFCON 2 : Accroissement supplémentaire dans la préparation des forces, mais inférieure à la préparation maximale.</font>
	<br><font color="#FF0000">DEFCON 1 : Etat de guerre !! Préparation maximale des forces.</font><br><br>

	<form action="index.php?id='.$id.'&editer=defcon" method="post" name="form1" target="_parent">
	  <select name="niveau" id="niveau">
	    <option value="5"'; if($row[defcon] == "5") { echo" selected";} echo'>Niveau 5</option>
	    <option value="4"'; if($row[defcon] == "4") { echo" selected";} echo'>Niveau 4</option>
	    <option value="3"'; if($row[defcon] == "3") { echo" selected";} echo'>Niveau 3</option>
	    <option value="2"'; if($row[defcon] == "2") { echo" selected";} echo'>Niveau 2</option>
	    <option value="1"'; if($row[defcon] == "1") { echo" selected";} echo'>Niveau 1</option>
	  </select>
	<input type="submit" name="Submit" value="Modifier"></form>
	</div>';
}elseif($_REQUEST['editer']=='presentation'){

	if($_REQUEST['nom']!='' && $_REQUEST['tag']!=''){
	
		$nom = htmlentities ($_POST["nom"]);
		$tag = htmlentities ($_POST["tag"]);
		$url = htmlentities ($_POST["url"]);
		$img = htmlentities ($_POST["img"]);
		$presentation = htmlentities ($_POST["presentation"]);
		$txtinterne = htmlentities ($_POST["txtinterne"]);
		
		if ($nom !== $row[nom]) {
			$req = "SELECT id FROM matgraph_ally WHERE nom='$nom' AND id!='".$id."' ORDER by id ASC";
			$res = db_mysql_Query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
			$num = mysql_num_rows($res);

			if ( $num > 0 ) {
				while ( $row = mysql_fetch_array($res) ) {
					echo"<br>Ce nom est déja pris.<br><br>";
					exit;
				}
			}
		}
		if ($tag !== $row[tag]) {
			$req = "SELECT id FROM matgraph_ally WHERE tag='$tag' AND id!='".$id."' ORDER by id ASC";
			$res = db_mysql_Query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
			$num = mysql_num_rows($res);

			if ( $num > 0 ) {
				while ( $row = mysql_fetch_array($res) ) {
					echo"<br>Ce tag est déja pris.<br><br>";
					exit;
				}
			}
		}

		$sql = 'UPDATE matgraph_ally SET nom="'.$nom.'",tag="'.$tag.'",url="'.$url.'",img="'.$img.'",presentation="'.$presentation.'",txtinterne="'.$txtinterne.'" WHERE id="'.$id.'" ';
		$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
		echo '<div align="center">Modification de la présentation de l\'Alliance effectuée avec succés.</div>';
		sleep(1);
	}

	$sql = "SELECT * FROM matgraph_ally WHERE id='$row2[ally]'";
	$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
	$row = mysql_fetch_array($res); 

	echo"<form name=\"rep\" method=\"post\" action=\"index.php?id=".$id."&amp;editer=presentation\" target=\"_parent\">
	  <table width=\"618\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\" bordercolor=\"#000000\" align=\"center\" style=\"font-family:trebuchet ms,sans-serif; font-size: 9pt ;\">
	    <tr style =\"background-color:#222222;\">
	      <td width=\"159\"><strong>Nom : </strong></td>
	      <td width=\"453\"><div align=\"center\">
	        <input name=\"nom\" type=\"text\" id=\"nom\" value=\"$row[nom]\" maxlength=\"35\">
	      </div></td>
	    </tr>
	    <tr>
	      <td><strong>Tag : </strong></td>
	      <td width=\"453\"><div align=\"center\">
	        <input name=\"tag\" type=\"text\" id=\"tag\" value=\"$row[tag]\" maxlength=\"6\">
	      </div></td>
	    </tr>
	    <tr style =\"background-color:#222222;\">
	      <td><strong>Url Page d'accueil : </strong></td>
	      <td width=\"453\"><div align=\"center\">
	        <input name=\"url\" type=\"text\" id=\"url\" value=\"$row[url]\" maxlength=\"300\">
	      </div></td>
	    </tr>
	    <tr>
	      <td><strong>Url Logo : </strong></td>
	      <td width=\"453\"><div align=\"center\">
	        <input name=\"img\" type=\"text\" id=\"img\" value=\"$row[img]\" maxlength=\"300\">
	      </div></td>
	    </tr>
	    <tr style =\"background-color:#222222;\">
	      <td colspan=\"2\"> <strong>Pr&eacute;sentation : </strong><br><div align=\"center\"><a href=\"javascript:smiley('[i][/i]');\"><em>Mettre en Italic </em> </a>	
		<a href=\"javascript:smiley('[b][/b]');\"><b>Mettre en Gras </b> </a>
		<a href=\"javascript:smiley('[u][/u]');\"><u>Souligner </u> </a>
		<a href=\"javascript:smiley('[img][/img]');\">Image </a>
		<a href=\"javascript:smiley('[URL=l\'url de le page]Visitez ce site[/URL]');\">Lien</a>
		<select>
	<option onclick=\"javascript:smiley('[align=left][/align]');\">Gauche </option>
	<option onclick=\"javascript:smiley('[align=center][/align]');\">Centrer </option>
	<option onclick=\"javascript:smiley('[align=right][/align]');\">Droite </option>
	</select>
	<select>
	<option onclick=\"javascript:smiley('[color=red][/color]');\">Rouge </option>
	<option onclick=\"javascript:smiley('[color=green][/color]');\">Green </option>
	<option onclick=\"javascript:smiley('[color=blue][/color]');\">Blue </option>
	</select>
		  </div>
		 </td>
	    </tr>
	    <tr>
	      <td colspan=\"2\"><div align=\"center\">
	        <textarea name=\"presentation\" cols=\"70\" id=\"presentation\" rows=\"8\">$row[presentation]</textarea>
	      </div></td>
	    </tr>
	    <tr style =\"background-color:#222222;\">
	     <td colspan=\"2\"><strong>Texte interne : </strong> <br><div align=\"center\"><a href=\"javascript:smiley('[i][/i]');\"><em>Mettre en Italic </em> </a>	
		<a href=\"javascript:smiley2('[b][/b]');\"><b>Mettre en Gras </b></a> 
		<a href=\"javascript:smiley2('[u][/u]');\"><u>Souligner </u></a> 
		<a href=\"javascript:smiley2('[img][/img]');\">Image </a>
		<a href=\"javascript:smiley2('[URL=l\'url de le page]Visitez ce site[/URL]');\">Lien</a>
		<select>
	<option onclick=\"javascript:smiley2('[align=left][/align]');\">Gauche </option>
	<option onclick=\"javascript:smiley2('[align=center][/align]');\">Centrer </option>
	<option onclick=\"javascript:smiley2('[align=right][/align]');\">Droite </option>
	</select>
	<select>
	<option onclick=\"javascript:smiley2('[color=red][/color]');\">Rouge </option>
	<option onclick=\"javascript:smiley2('[color=green][/color]');\">Green </option>
	<option onclick=\"javascript:smiley2('[color=blue][/color]');\">Blue </option>
	</select>
	</div>
	</td>
	    </tr>
	    <tr>
	      <td colspan=\"2\"><div align=\"center\">
	        <textarea name=\"txtinterne\" cols=\"70\" id=\"txtinterne\" rows=\"8\">$row[txtinterne]</textarea>
	      </div></td>
	    </tr>
	    <tr style =\"background-color:#222222;\">
	      <td colspan=\"2\" height=\"45\"><div align=\"center\">
	        <input type=\"submit\" name=\"Submit\" value=\"Envoyer\">
	      </div></td>
	    </tr>";
	    
	echo"</table>
	</form>";

}elseif($_REQUEST['voir']=="membre"){
	$req = "SELECT id,login,pts,galaxi,system,position,date,statut FROM matgraph_users WHERE ally='".$id."' ORDER by pts DESC";
	$res = db_mysql_Query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
	$num = mysql_num_rows($res);
	if ( $num > 0 ) {
	$i = 1;
	echo"<table width=\"618\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\" bordercolor=\"#000000\" align=\"center\" style=\"font-family:trebuchet ms,sans-serif; font-size: 9pt ;\">
  <tr style =\"background-color:#111111;\">
    <td align=\"center\" colspan=\"9\"> Liste des membres (Nombre: $num) </td>
  </tr>
  <tr style =\"background-color:#222222;\">
  	<td align=\"center\" width=\"25\">Del</td>
	<td align=\"center\" width=\"30\">Edit</td>
	<td align=\"center\">N&deg;</td>
    <td align=\"center\">Pseudo</td>
    <td align=\"center\">Coordonn&eacute;es</td>
    <td align=\"center\">Statut</td>
    <td align=\"center\">Points</td>
    <td align=\"center\">Online</td>
    <td align=\"center\">MP</td>
  </tr>";
  	while ( $row = mysql_fetch_array($res) ) {
 		$sql2 = "SELECT ally,statut FROM matgraph_users WHERE id='$idlogin' LIMIT 0,1";
		$res2 = db_mysql_Query($sql2)or die('erreur sql<br>'.$sql2.'<br>'.mysql_error());
		$row2 = mysql_fetch_array($res2);
  
  		if($row2[statut] !== "0" AND $row2[statut] !=="-1") {
			$sql = "SELECT online FROM matgraph_ally_statut WHERE id='$row2[statut]' LIMIT 0,1";
			$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
			$row = mysql_fetch_array($res);
			if($row[online] == "1"){
				$online2 = "ok";
			}
		}else{
			$online2 = "ok";
		}
  
		if($row[statut] == "-1") {
			$statut = "Fondateur"; 
		}else{
			$statut = $row4[nom] ; 
		}

		$sql4 = "SELECT nom FROM matgraph_ally_statut WHERE id='$row[statut]' LIMIT 0,1";
		$res4 = db_mysql_Query($sql4)or die('erreur sql<br>'.$sql4.'<br>'.mysql_error());
		$row4 = mysql_fetch_array($res4);

		// On recupere la date la plus eleve, plutot que d'utiliser row[date], qui est la date de la PM
		$req_online = "SELECT date FROM matgraph_users WHERE idjoueur = $row[id] ORDER BY date DESC LIMIT 0,1";
		$res_online = db_mysql_Query($req_online) or die('Erreur SQL! <br>'.$req_online.'<br>'.mysql_error());
		$row_online = mysql_fetch_array($res_online);

		// Donc on parse row_online[date], et plus 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})", $row_online[date], $thetime);
		$time = mktime($thetime[4],$thetime[5],$thetime[6],$thetime[2],$thetime[3],$thetime[1]);

		// on arrondi a la minute
		$diftemps = time() - $time;
		//$difheure = round($diftemps / 3600,1);
		//$difmin = $difheure * 60;
		$difmin = round($diftemps / 60,1);

		if ($difmin < 10 ) {
			$online = "<font color=\"#00CC00\">Online</font> ($row_online[date])"; 
		} else {
			$online = "<font color=\"#FF0000\">Offline</font> ($row_online[date])";
		}

		$pts = number_format($row[pts] / 1000, 0, ',', ' ');
		echo"<tr";
		if (($i/2) == ceil($i/2)){
			echo' style ="background-color:#111111;"' ;
		}
		echo">
	<td width=\"25\" align=\"center\">
		<form name=\"supprimer".$row['id']."\" method=\"post\" action=\"ally_membre_del.php\" target=\"_parent\">
			<input type=\"hidden\" name=\"id\" value=\"$row[id]\">
			<input type=\"hidden\" name=\"idally\" value=\"$idally\">
    		<input type=\"image\" src=\"../../imgs/del.gif\" style=\"width:25;\">
		</form>
    </td>
	<td width=\"30\" align=\"center\">
		<form name=\"edit".$row['id']."\" method=\"post\" action=\"ally_membre_edit.php\" target=\"_parent\">
			<input type=\"hidden\" name=\"id\" value=\"$row[id]\">
			<input type=\"hidden\" name=\"idally\" value=\"$idally\">
    		<input type=\"image\" src=\"../../imgs/edit.gif\" style=\"width:30;\">
		</form>
    </td>
	<td>$i</td>
    <td align=\"center\">$row[login]</td>
    <td align=\"center\">[$row[galaxi]:$row[system]:$row[position]]</td>
    <td align=\"center\">".htmlentities($statut)."</td>
    <td align=\"center\">$pts</td>
    <td align=\"center\">";if($online2 == "ok") { echo"$online"; } else { echo"-";}echo"</td>
    <td align=\"center\">
		<form name=\"galaxi1\" method=\"post\" action=\"mp_envoi.php\" target=\"_parent\">
			<input name=\"galaxi\" type=\"hidden\" value=\"$row[galaxi]\">
			<input name=\"system\" type=\"hidden\" value=\"$row[system]\">
			<input name=\"position\" type=\"hidden\" value=\"$row[position]\">
			<input name=\"idreceveur\" type=\"hidden\" value=\"$row[id]\">
			<input type=\"image\" src=\"../../imgs/msg.gif\">
		</form>
	</td>
  </tr>";
 		 $i++;
	}
	echo "</table>";
	}else{
		echo "<div align=\"center\">Pas de membres...</div>";
	}
}else{

$reponse  = db_mysql_Query("SELECT pts_ally FROM matgraph_ally WHERE id='$row2[ally]'");
$reponse2 = db_mysql_Query("SELECT COUNT(id) FROM matgraph_ally WHERE pts_ally > ".mysql_result($reponse,0));
$classement = mysql_result($reponse2,0)+1;

$reqtotal = "SELECT * FROM matgraph_ally";
$restotal = db_mysql_Query($reqtotal) or die('Erreur SQL! <br>'.$reqtotal.'<br>'.mysql_error());
$numtotal = mysql_num_rows($restotal);

$sql = "SELECT * FROM matgraph_ally WHERE id='$row2[ally]'";
$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
$row = mysql_fetch_array($res); 

$req = "SELECT id FROM matgraph_users WHERE ally='$row[id]' ORDER by id ASC";
$res = db_mysql_Query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
$num = mysql_num_rows($res);

$present = htmlentities (stripslashes($row[presentation]));

$bbcode= array (
"/\[b\](.+?)\[\/b\]/si",
"/\[i\](.+?)\[\/i\]/si",
"/\[u\](.+?)\[\/u\]/si",
"/\[url=(.+?)\](.+?)\[\/url\]/si",
"/\[img\](.+?)\[\/img\]/si",

);
$htmlcode= array (
"<b>$1</b>",
"<i>$1</i>",
"<u>$1</u>",
"<a href='$1'>$2</a>",
"<img src='$1'>"
);
$present=preg_replace($bbcode,$htmlcode,$present);

$present=preg_replace('!\[color=(red|green|blue)\](.+)\[/color\]!isU', "<span style='color:$1'>$2</span>", $present); //couleur
$present=preg_replace('!\[align=(left|center|right)\](.+)\[/align\]!isU', "<div align='$1'>$2</div>", $present); //aligner

$present=nl2br($present);

echo"<div align=\"center\"><table width=\"618\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\" bordercolor=\"#000000\" style=\"font-family:trebuchet ms,sans-serif; font-size: 9pt ;\" align=\"center\">";
if(!empty($row[img])) { echo"
  <tr>
    <td colspan=\"2\"><div align=\"center\"><img src=\"$row[img]\"></div></td>
  </tr>";} 
echo"
  <tr style =\"background-color:#222222;\">
    <td width=\"119\"><strong>Nom : </strong></td>
    <td width=\"493\" align=\"center\">".html_entity_decode($row[nom])."</td>
  </tr>
  <tr>
    <td><strong>Tag : </strong></td>
    <td align=\"center\">$row[tag]</td>
  </tr>
  <tr style =\"background-color:#222222;\">
    <td><strong>Classement : </strong></td>
    <td align=\"center\">$classement / $numtotal</td>
  </tr>
  <tr>
    <td><strong>Membres : </strong></td>
    <td align=\"center\">$num (<a href=\"index.php?id=".$id."&amp;voir=membre\">Liste des membres</a>)</td>
  </tr>
  <tr style =\"background-color:#222222;\">
  	<td><strong>DEFCON :</strong></td>
	<td align=\"center\">";
	if($row['defcon']=='5'){
		echo "<font color=\"#009900\">Niveau 5</font>";
	}elseif($row['defcon']=='4'){
		echo "<font color=\"#999933\">Niveau 4</font>";
	}elseif($row['defcon']=='3'){
		echo "<font color=\"#FFFF00\">Niveau 3</font>";
	}elseif($row['defcon']=='2'){
		echo "<font color=\"#FF9900\">Niveau 2</font>";
	}elseif($row['defcon']=='1'){
		echo "<font color=\"#FF0000\">Niveau 1</font>";
	}
echo"
  <tr>
    <td height=\"15\" colspan=\"2\" align=\"center\" ><strong>Pr&eacute;sentation : </strong></td>
  </tr>
  <tr  style =\"background-color:#222222;\">
    <td colspan=\"2\">".html_entity_decode($present)."</td>
  </tr>
  <tr>
    <td><strong>Page d'accueil :</strong></td>
    <td align=\"center\">";
	if($row['url']!==''){echo '<a href=\"$row[url]\" target=\"_blank\">$row[url]</a>';}
	echo "</td>
  </tr>
  </table></div>";
 }
  
echo"<br/><br/><div align=\"center\">
  	<button onclick=\"window.close()\" Style=\"border:1px solid #7E7E7E;\">Fermer</button>
	</div>
</body>
</html>";
?>