<?php
include ('logue.php'); 
include ('entete2.php');
include ('ressource.php');
include ('entete.php');
echo"
<STYLE TYPE=text/css>
.Style1 {color: #0000FF}
.Style2 {color: #FF0000}
input {
border:1px 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;
}
</STYLE>";
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'); 

$sql2 = "SELECT login,ally,statut FROM matgraph_users WHERE id='$idlogin'";
$res2 = db_mysql_Query($sql2)or die('erreur sql<br>'.$sql2.'<br>'.mysql_error());
$row2 = mysql_fetch_array($res2);

$sql_options = "SELECT mailcollectif FROM matgraph_ally_statut WHERE id='$row2[statut]'";
$res_options = db_mysql_Query($sql_options)or die('erreur sql<br>'.$sql_options.'<br>'.mysql_error());
$row_options = mysql_fetch_array($res_options); 

if($row2[ally] == "no") {
	echo '<script language="javascript" type="text/javascript">
	<!--
	window.location.replace("ally_index.php");
	-->
	</script>';
	exit;
}

if($row2[statut] !== "0" AND $row_options[mailcollectif] == "1" OR $row2[statut] == "-1") {

	$sujet = "";
	$sujet.= "$row2[login] :";
	$sujet.= htmlentities ($_POST["sujet"]);
	$texte = htmlentities ($_POST["msg"]);

	if(empty($sujet)) {
	$sujet = "Sans titre";}

	$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'>"
	);
	$texte=preg_replace($bbcode,$htmlcode,$texte);

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

	$texte=nl2br($texte);

	$req = "SELECT idjoueur FROM matgraph_users WHERE ally='$row2[ally]' 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) ) {
			$requete = 'INSERT INTO matgraph_msgally VALUES ("", now(), "non", "'.$row[idjoueur].'", "'.$sujet.'", "'.$texte.'")';
			$resultat = db_mysql_Query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
		}
	}
	echo"<br><br>Votre message a bien été envoyer.";
}
else {
	echo '<script language="javascript" type="text/javascript">
	<!--
	window.location.replace("ally_index.php");
	-->
	</script>';
}
?>
