<?php
/*
 ########################################################################
#                                                                        #
#           Version 4       /                        /   /               #
#          -----------__---/__---__------__----__---/---/-               #
#           | /| /  /___) /   ) (_ `   /   ) /___) /   /                 #
#          _|/_|/__(___ _(___/_(__)___/___/_(___ _/___/___               #
#                       Free Content / Management System                 #
#                                   /                                    #
#                                                                        #
#                                                                        #
#   Copyright 2005-2006 by webspell.org                                  #
#                                                                        #
#   visit webSPELL.org, webspell.info to get webSPELL for free           #
#   - Script runs under the GNU GENERAL PUBLIC LICENSE                   #
#   - It's NOT allowed to remove this copyright-tag                      #
#   -- http://www.fsf.org/licensing/licenses/gpl.html                    #
#                                                                        #
#   Code based on WebSPELL Clanpackage (Michael Gruber - webspell.at),   #
#   Far Development by Development Team - webspell.org                   #
#                                                                        #
#   visit webspell.org                                                   #
#                                                                        #
 ########################################################################
*/

echo'<h2>newsletter</h2>';

if(!isuseradmin($userID) OR substr(basename($_SERVER[REQUEST_URI]),0,15) != "admincenter.php") die('Access denied.');

if($_POST['send'] || $_POST['testen']) {

 $message = $_POST['message'];
 $title = $_POST['title'];
 $testmail = $_POST['testmail'];

 $date=date("d.m.Y", time());
 $message=nl2br($message);
 $emailbody = '<!--
Hello!
Your system doesnt support HTML mails.
Your received following message:
$message
 --> 
<html>
<head>
<style type="text/css">
<!--
.border {
	border: 1px solid #000000;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
	color: #000000;
	background-color: ABAFBA;
}
table {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 12px;
	color: #000000;
}
-->
</style>
</head>

<body bgcolor="#464A51">
<table width="628" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr> 
    <td><table width="596" border="0" align="center" cellpadding="5" cellspacing="0" class="border">
        <tr> 
          <td align="center"><a href="http://'.$hp_url.'"><img src="http://'.$hp_url.'/images/banner.gif" border="0"></a><br>
            <table width="96%" border="0" align="center" cellpadding="10" cellspacing="0">
              <tr>
                <td><b>'.$title.'</b><br>
                  <br>
                  '.$message.'<br>
                  <br>
                  <hr size="1" noshade color="#000000">
                  <font size="1">You can remove yourself from the mailing-list 
                  at your <a href="http://'.$hp_url.'/index.php?site=myprofile">Profile</a>
                  </font> </td>
              </tr>
            </table> </td>
        </tr>
      </table></td>
  </tr>
</table>
</body>
</html>'; 
  $emailbody=stripslashes($emailbody);

	if($_POST['testen']) $bcc[] = $testmail;
	else {
		
		//clanmember
		
		if($_POST['sendto_clanmembers']) {

    	$ergebnis=safe_query("SELECT userID FROM ".PREFIX."squads_members GROUP BY userID");
			$anz=mysql_num_rows($ergebnis);
			if($anz) {
		    while($ds=mysql_fetch_array($ergebnis)) {
			    $emails[] = getemail($ds[userID]);
		    }
	    }
	  }
	  
		if($_POST['sendto_registered']) {

	    $ergebnis=safe_query("SELECT * FROM ".PREFIX."user WHERE newsletter='1'");
			$anz=mysql_num_rows($ergebnis);
			if($anz) {
		    while($ds=mysql_fetch_array($ergebnis)) {
			    $emails[] = $ds[email];
		    }
    	}
	  
	  }

		if($_POST['sendto_newsletter']) {
	  				
    $ergebnis=safe_query("SELECT * FROM ".PREFIX."newsletter");
		$anz=mysql_num_rows($ergebnis);
		if($anz) {
		    while($ds=mysql_fetch_array($ergebnis)) {
       $emails[] = $ds[email];
		    }
	    }
	  }
	  
		$bcc=$emails;
	}

	$header = "From:$admin_name<$admin_email>\n";
	$header .= "Reply-To: $admin_email\n";
	$header .= "Content-Type: text/html";

  $success = true;
  $bcc=array_unique($bcc);
	foreach($bcc as $mailto) {
 	 if(!mail($mailto,PAGETITLE." Newsletter",$emailbody,$header)) $succces = false;
	}
  if($success) echo'newsletter was sent <br><br>Receptionist(s):<br><br> '.implode(", ",$bcc);
  else echo'ERROR: Newsletter could not be sent (mailserver available?).';
}
else { 
    ?>
    <form action="admincenter.php?site=newsletter" method="post"> 
    <table width="100%" border="0" cellspacing="2" cellpadding="0">
    <tr>
      <td>Title:</td>
	  <td><input type="text" name="title" size="65" class="form_off" onFocus="this.className='form_on'" onBlur="this.className='form_off'"></td>
    </tr>
    <tr>
      <td valign="top">HTML-Mail:</td>
      <td><textarea cols="65" rows="25" name="message" class="form_off" onFocus="this.className='form_on'" onBlur="this.className='form_off'"></textarea></td>
    </tr>
    <tr>
      <td>test Newsletter:</td>
      <td><input type="text" name="testmail" class="form_off" onFocus="this.className='form_on'" onBlur="this.className='form_off'" value="user@inter.net"> <input type="submit" value="testen" name="testen"></td>
    </tr>	
    <tr>
      <td>Send to:</td>
      <td><input type="checkbox" name="sendto_clanmembers" value="1" checked> Clanmembers [<?php echo mysql_num_rows(safe_query("SELECT userID FROM ".PREFIX."squads_members GROUP BY userID")); ?> users]
      	<br><input type="checkbox" name="sendto_registered" value="1" checked> Registered (including clanmembers) [<?php echo mysql_num_rows(safe_query("SELECT * FROM ".PREFIX."user WHERE newsletter='1'")); ?> users]
      	<br><input type="checkbox" name="sendto_newsletter" value="1" checked> Newsletter-User [<?php echo mysql_num_rows(safe_query("SELECT * FROM ".PREFIX."newsletter")); ?> users]
      	<br> </td>
    </tr>	
		<tr>
      <td>&nbsp;</td>
      <td><input type="submit" value="send Newsletter" name="send"></td>
    </tr>
    </table>
    </form> 
    <?php 
} 

?>
