<?php

##############################################################################
# cartons_jaunes.php3                                                                #
#                                                                            #
# Copyright ViralGames SARL 2000                                             #
##############################################################################


include("config.php3");
include($BIB."bib_generic.php3");
include($BIB."bib_tables.php3");
include($BIB."bib_webmaster_forums.php3");
include($BIB."bib_cartons.php3");
include($BIB."bib_mailing.php3");


$current_european_date = date("Y-m-d H:i:s", my_local_time());

$SCORE_MIN = 100;

$sess = open_page($sessid, 1);

if ($sess->memberid == -1) $sess->reinit("Error: invalid access to member page");

$member = getmember($sess->memberid);
$player = getplayer($sess->memberid);


/*---------------------------------------------------------------*/
# Envois d'un message
/**------------------------------------------------------------*/

if($submit=="envoyer")
{
	
	$error="";
	//debug("SELECT * FROM Players WHERE NickName='".trim($ToNickName)."'");
	$toPlayerRow = main_db_query("SELECT * FROM Players WHERE NickName='".trim($ToNickName)."'");
	if (db_num_rows($toPlayerRow)>0)
	{
		//NickName existe
		$toPlayer = db_fetch_array($toPlayerRow);
		$toMember = getmember($toPlayer[MemberId]);
		$ToPId = $toPlayer[Id];		
	}
	else
	{
		//NickName n'existe pas
		$error .= C_LEPSEUDO." - ".$ToNickName." - ".C_NEXISTEPAS." !<br>";
	}
		
	if(!isset($TYPE_CARTON[$Color]))
	{
		$error .= "Vous n'avez pas sélectionné de couleur de cartons<br>";
	}
	
	if(strlen(trim($Objet))< 1)
	{
		$error .= "Vous n'avez pas saisi d'objet<br>";
	}
	
	if (empty($error))
	{
		# on formatte le texte pour les caractères dur genre ',",\ et NULL (à voir pour la virgule)
		$Text = nl2br(htmlentities($Text));
		
		# nl2br pas fiable
		$Text = ereg_replace("\n", "", $Text); 
		$Text = ereg_replace("\r", "", $Text); 
		


		# on cherche si l expediteur n est pas bloquer par le destinataire
		$squery = " SELECT COUNT(*) AS Number FROM Messagerie_BlackList WHERE Black=".$player[Id]." AND Blacker=".$ToPId;
		$rowsBlacks = main_db_query($squery);
		//debug($squery);
		$rowBlack = db_fetch_assoc($rowsBlacks);
		//echo "number : ".$rowBlack['Number']."<br>";
		
		# on ne peut pas envoyer de cartons jaunes aux modos
		/*for($r=0; $r < count($TAB_AUTHORS); $r++)
		{
			if ($TAB_AUTHORS[$r]["NickName"] == $ToNickName)
				$rowBlack['Number'] = 1;
		}	*/	
		
		if($rowBlack['Number'] == 0)
		{
			
			//debug("INSERT INTO Messagerie (DateEnvoi,Objet,FromPId,ToPId,Text,Color) VALUES ('".$current_european_date."','".$Objet."','".$player[Id]."','".$ToPId."','".$Text."','".$TYPE_CARTON[$Color]."')");
			# on met le postit dans la base
			main_db_query("INSERT INTO Messagerie (DateEnvoi,Objet,FromPId,ToPId,Text,Color) VALUES ('".$current_european_date."','".$Objet."','".$player[Id]."','".$ToPId."','".$Text."','".$TYPE_CARTON[$Color]."')");
	
			# on incremente le nb de cartons non lus
			//main_db_query("UPDATE Players SET NbNewCartons=".get_messages_non_lus($player[Id])." WHERE Id=".$ToPId);
			
			# On envoit un mail si l'optin Carton est activée
			if( $toMember['OptinCarton'] != 0 )
			{
				#ici on envoie un mail de confirmation d'inscription
				$subject="Vous avez reçu un carton sur ".$GAME_NAME." !";
				$msg = loadTemplate("Mailing/template_optinCarton.html");
								
				$msg = replaceTag("__LOGIN__", $toPlayer[NickName], $msg);
				$msg = replaceTag("__EXPEDITEUR__", $player[NickName], $msg);
				$msg = replaceTag("__GAMEURL__", $SERVER_URL, $msg);
			
				generic_mail($toMember[Email], $subject, $msg,
						"From: ".$GAME_NAME." <".$FROM_FIELD.">\n" . 
						"Reply-To: ".$REPLY_TO_FIELD."\n" . 
						"X-Mailer: PHP/" . phpversion()."\n".
						"MIME-Version: 1.0\n".
						"Content-Type: text/html; charset=iso-8859-1"."\n"
				);

					}
		}
		
		# on remet le text à rien
		$ToNickName = $Objet = $Text="";
		
		# info
		$info=C_MESSAGEENVOYE." ".$toPlayer[NickName].".";
	}
}



/*---------------------------------------------------------------*/
# insertion dans la black liste
/**------------------------------------------------------------*/
if($toblackList == 1)
{
	$squery = "SELECT Id FROM Players WHERE NickName='".$pseudo."'";
	$rowsPlayers = main_db_query($squery);
	$rowPlayer = db_fetch_assoc($rowsPlayers);
	
	if($rowPlayer['Id'] != "")
	{
		# je verifie s il existe pas deja dans la black liste
		$squeryVerif = "SELECT COUNT(*) AS NB FROM Messagerie_BlackList WHERE Black=".$rowPlayer['Id']." AND Blacker=".$player['Id'];
		$rowExist = db_fetch_assoc(main_db_query($squeryVerif));
		if($rowExist['NB'] == 0)
		{
			$squeryInsert = "INSERT INTO Messagerie_BlackList (Black,Blacker) VALUES ('".$rowPlayer['Id']."', '".$player['Id']."')";
			//debug($squeryInsert);
			main_db_query($squeryInsert);
			
			$info = $pseudo ." a été rajouté à votre blacklist.";
		}
	}
	else
	{
		$errorPseudo = $pseudo." n'existe pas !";
	}
	$pseudo = "";
	$toblackList = 0;
}

/*---------------------------------------------------------------*/
# Mise a jour de l'optin mail
/**------------------------------------------------------------*/
if($formOptionCarton == 1)
{
	$squery = "UPDATE Members SET OptinCarton = $OptinCarton WHERE Id='".$member[Id]."'";
	$rowsMembers = main_db_query($squery);

	$info = "Option mail mise à jour.";
	$member[OptinCarton] = $OptinCarton;
}


if($delId > 0)
{
	delete_message($player[Id],$delId);
}

if($ansId > 0)
{
	list($ToNickName, $Objet,$msgId) = get_exp_message_nickname($ansId);
}

if( empty($OptinCarton) )
{
	$OptinCarton = $member[OptinCarton];
}



/*------------ TAG HEADER ----*/
$header_title = "Kestufoot: jeu de pronostics gratuits sur le football, mes cartons.";
$header_description = "Cette page vous permet de lire vos messages et d’envoyer des messages sous forme de cartons (jaune, rouge, rose) à vos amis du Kestufoot.";
$header_keywords = "amis, cartons jaunes, cartons rouges, lire mes cartons, envoyer un carton, matchs de foot, pronostics gratuits, jeu de pronostics, jeu de foot.";
$header_keywords = "amis, cartons jaunes, cartons rouges, inviter des amis, relancer des amis, matchs de foot, pronostics gratuits, jeu de pronostics, jeu de foot.";
/*------------ /TAG HEADER ----*/
	
$page_courante = "Mes cartons";

if($sess->memberid != -1)
{
	$sub_header='subh_connecte.inc';
}


include("header.inc");



include("mes-cartons.inc");
	


include("footer.inc");

close_page($sess);
?> 

