<?php

##############################################################################
# mailing.php3                                                               #
#                                                                            #
# Copyright ViralGames SARL 2000                                             #
##############################################################################

include("../config.php3");
include($BIB."bib_generic.php3");

include($BIB."bib_mailing.php3");

function replaceTag($tag, $value, $msg)
{
	return ereg_replace($tag, $value, $msg);
}

$fic = $ARCHIVES_DIR."mailing.log"; #fichier appartenant a viralgame et accessible par nobody uniquement en ecriture
$MAILING = $ADMIN_URL."mailing.php3"; 

$ORIGIN_KTF = 0;
$ORIGIN_LYCOS = 1;
$ORIGIN_M6 = 2;

##################################
# Administration script defaults #
##################################

# pas de timeout possible qql soit l'action effectuée
//set_time_limit($TIME_LIMIT);

# le script continue une fois qu'il est lancé qql soit l'état de la connection après
//ignore_user_abort(true); 

?>
<html>
<head></head>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>
<div align=center class=trTitle>Newsletter</div>
<br>
<?php
if (($fichier!="")&&($subject!=""))
{
	echo "</td></tr></table>";
	$subject = stripslashes($subject);
    $fd = fopen($fic,'w');
    fwrite($fd,"######### mailing.log ########\n\n");
    fwrite($fd,"MemberId | NickName | Email | localtime\n\n");
	
    #envoie a tous les membres du Kestufoot non desinscrits
    if (strcmp($cible,"tous")==0 or strcmp($cible,"plage")==0)
	{
		# pour les membres de sport24
		if ($brand==$ORIGINE_KTF or $brand==$ORIGINE_M6)
		{
			# on récupère la liste des participants
			$row_personne = main_db_query("SELECT Members.Id AS Id, Email, NickName, Origine, Name, FirstName, Password FROM Members ".
				"LEFT JOIN Players ON Members.Id=Players.MemberId WHERE IsOff='0' AND ".
				"Brand='".$brand."'");
		}
		# pour les membres de wanadoo
		elseif ($brand==$ORIGINE_LYCOS)
		{
			# on récupère la liste des participants
			$row_personne = main_db_query("SELECT Members.Id AS Id, Email2 AS Email, NickName, Origine, Name, FirstName, Password FROM Members ".
				"LEFT JOIN Players ON Members.Id=Players.MemberId WHERE IsOff='0' AND ".
				"Brand='".$ORIGINE_LYCOS."'");
		}
	}
	
	#envoie a une ou plusieurs adresses
	elseif (strcmp($cible,"particulier")==0)
	{
		$guestemail = "";
		$emails = extractemail($adresse);
		for ($i = 0; $i < count($emails); $i++)
			$guestemail = ($i < count($emails)-1 ? $guestemail."'".$emails[$i]."'," :  $guestemail."'".$emails[$i]."'");
		$row_personne = main_db_query("SELECT Members.Id AS Id,Email,NickName FROM Members LEFT JOIN Players ON Members.Id=Players.MemberId WHERE Email IN ($guestemail) AND IsOff=0");
	}
	
	else $error = $error . "Problème de radio button<br>";
	
	if ($error=="")
	{
		#on recupere le fichier a envoyer en le mettant dans une string
		$html = file($fichier);
		for($k=0;$k<sizeof($html);$k++)
		    $msg = $msg.$html[$k];
		
		#cas du mailing aux invites non membres
		if (strcmp($cible,"guests_not_member")==0)
		{
			while ($personne = db_fetch_array($row_personne))
			{
				$from = $personne['Femail'];
				$subj = ereg_replace("FATHER_NICKNAME",$personne['NickName'],$subject);
				$ms = ereg_replace("ID_REGISTRATION",$personne['GuestId'],$msg);
				
				waitMail($personne['Gemail']);
				
#				generic_mail($personne['Gemail'], $subj, $ms,
#					"From: $from\n" . 
#					"Reply-To: $from\n" . 
#					"X-Mailer: PHP/" . phpversion()."\n".
#					"MIME-Version: 1.0\n".
#					"Content-Type: text/html; charset=iso-8859-1"."\n"
#				);
				fwrite($fd,$personne['Gemail']." ".my_local_time()."\n");
			}
		}
		
		else
		{
			#cas general
		    while ($personne = db_fetch_array($row_personne))
			{
				if (isset($min) and isset($max) and ($personne['Id'] >= $min and $personne['Id'] <= $max))
				{
					unset($newMsg);
					
					# on inclue la lien pour la désinscription
					$newMsg = unregisterTag($personne['Id'],$msg);
					
					# infos sur le Nom prénom
					$newMsg = replaceTag("__FULLNAME__", $personne['FirstName']." ".$personne['Name'], $newMsg);
					
					# infos sur le login
					$newMsg = replaceTag("__NICKNAME__", $personne['NickName'], $newMsg);
	
					# infos sur le pass
					$newMsg = replaceTag("__PASSWORD__", $personne['Password'], $newMsg);
	
					switch($personne['Origine'])
					{
						case $ORIGIN_KTF:
							$newMsg = replaceTag("__HOMEURL__", "http://www.kestufoot.com", $newMsg);
						break;
						
						case $ORIGIN_M6:
							$newMsg = replaceTag("__HOMEURL__", "http://kestufoot.m6.fr", $newMsg);
						break;
					}
				
					waitMail($personne['Email']);
					
					generic_mail($personne['Email'], $subject, $newMsg,
						"From: $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"
					);
					fwrite($fd, $personne['Id']."; ".$personne['NickName']."; ".$personne['Email']."; ".date("Y-m-d H:i:s", my_local_time())."\n");
				}
			}
		}
	}
	fclose($fd);
	echo "<b>Fini !!!</b><br>";
}

if (($fichier=="")||($subject=="")||($error!=""))
{
	echo $error;
	$form_action = $MAILING;
	$form_name = "mailing";
?>
Envoyer à :<br>
<form method="post" ENCTYPE="multipart/form-data" action="<?php echo $form_action ?>" name="main">
<input type="hidden" name="<?php echo $form_name ?>" value="x">
Les membres de
<select name="brand">
	<option value="<?php echo $ORIGINE_KTF?>">Kestufoot</option>
	<option value="<?php echo $ORIGINE_LYCOS?>">Lycos Foot</option>
	<option value="<?php echo $ORIGIN_M6 ?>">M6</option>
</select>

<input type="radio" name="cible" value="tous"> Tous les membres (
<?php
$allMembers = main_db_query("SELECT COUNT(*) AS tot, IsOff FROM Members GROUP BY IsOff");
$allMembersOn = db_fetch_array($allMembers);
$allMembersOff = db_fetch_array($allMembers);
echo $allMembersOn['tot']." sur ".($allMembersOff['tot']+$allMembersOn['tot']);
?>
 )<br>
<input type="radio" name="cible" value="actifs"> Tous les membres actifs depuis...<br>
<input type="radio" name="cible" value="inactifs"> Tous les membres n'ayant pas joué depuis...<br>...le <input type="text" name="dateRelance" value="<?php echo $dateRelance ?>"><br>
<br>
<input type="radio" name="cible" value="plage"> Les membres d'Id compris entre <input type="text" name="min" value="1"> et <input type="text" name="max" value="<?php $nb = db_fetch_array(main_db_query('SELECT count(*) AS NB FROM Members')); echo $nb[NB]; ?>" > inclus<br>
<input type="radio" name="cible" value="particulier" checked> Les membres d'adresses suivantes<br><textarea name="adresse" rows=10 cols=50></textarea><br>
<br>
Sélectionnez le nom du fichier de mailing <input type="file" name="fichier" size="50"><br>
<br>Entrez le Sujet du message<br><input size=45 type="text" name="subject"><br><br>
<input type="submit">
</form>
<?php
}
?>
</body>
</html>
