<?php

include("../includes/site_config.php");
include('../session_handler.inc');
include('../message.php');
include("../includes/sendemail.inc");



$Sess_UserType=$_SESSION['Sess_UserType'];

if ($Sess_UserType != "A") {
    exit;
}
restrict_demo();


if (isset($_POST['chkFile'])) $chkFile=$_POST['chkFile'];
if (isset($_POST['chkType'])) $chkType=$_POST['chkType'];
if (isset($_POST['chkAffiliates'])) $chkAffiliates=$_POST['chkAffiliates'];
$txtAddress=$_POST['txtAddress'];
$txtMessage=$_POST['txtMessage'];
$txtReply=$_POST['txtReply'];
$txtSubject=$_POST['txtSubject'];
$lstGender=$_POST['lstGender'];
$lstStatus=$_POST['lstStatus'];
$chkAllusers=$_POST['chkAllusers'];
$chkSpeeddating=$_POST['chkSpeeddating'];
$lstSendType=$_POST['lstSendType'];
$chkIntro=$_POST['chkIntro'];

#set the mail type
if (isset($chkType)) {
    $type="html";
} else {
    $type="text";
}
# check to see if it is a single address mail
############################################
if (strlen($txtAddress) > 1) {
    $recno=0;
    $recnum=1;
    $message=stripslashes($txtMessage);
    if ($type == "html")
        $message.="<p align='left'><font size='1'>".sprintf(PRGADMINMAIL_TEXT,$CONST_URL,$txtAddress);
    send_mail ("$txtAddress", "$txtReply", "$txtSubject", "$message", "$type","ON",$lstSendType);
    $recno++;
    flush();
    sleep(2);
    print("$recno) $txtAddress<br>");
# check to see if it is a file of addresses
############################################
} elseif (isset($chkFile)) {
    $recno=0;
    $recnum=1;
    $fp = fopen ($CONST_INCLUDE_ROOT."/email.txt", "r");
    while(!feof($fp)) {
        $txtAddress = fgets($fp,200);
        $txtAddress=trim($txtAddress);
        $message=stripslashes($txtMessage);
        send_mail ("$txtAddress", "$txtReply", "$txtSubject", "$message", "$type","ON",$lstSendType);
        $recno++;
        flush();
        sleep(2);
        print("$recno) $txtAddress<br>");
        if (is_int($recno/500)) {
            print("<br>".PRGADMINMAIL_SLEEP."<br><br>");
            flush();
            sleep(120);
        }
    }
    fclose($fp);
# check to see if it is a speeddating subscribers addresses
###########################################################
} elseif (isset($chkSpeeddating)) {
        switch ($lstGender) {
            case 'A':
                $condition = "";
                break;
            case 'M':
                $condition = " WHERE (sub_sex = 'M')";
                break;
            case 'F':
                $condition = " WHERE (sub_sex = 'F')";
                break;
        }
        $query="SELECT sub_email FROM sd_subscribe $condition";
        $result=mysql_query($query,$link) or die(mysql_error());
        $recnum=mysql_num_rows($result);
        $recno=0;
        print("$query<br><br>");
        # loop through selected target addresses
        while ($sql_array = mysql_fetch_object($result)) {
                if (isset($chkIntro)) {
                    $message=stripslashes($txtMessage);
                } else {
                    $message=stripslashes($txtMessage);
                    if ($type == "html")
                        $message.="<p align='left'><font size='1'>".
                        sprintf(PRGADMINMAIL_TEXT,$CONST_URL,$sql_array->sub_email);
                }
                send_mail ("$sql_array->sub_email", "$txtReply", "$txtSubject", "$message", "$type","ON", $lstSendType);
                $recno++;
                flush();
                sleep(1);
                print("$recno) $sql_array->sub_email<br>");
                if (is_int($recno/500)) {
                    print("<br>".PRGADMINMAIL_SLEEP."<br><br>");
                    flush();
                    sleep(120);
                }
        }
        mysql_close( $link );
# otherwise the source is from the database
############################################
} elseif (isset($chkAffiliates)){
		$query="SELECT * FROM affiliates";
		$result=mysql_query($query,$link) or die(mysql_error());
		$recnum=mysql_num_rows($result);
		$recno=0;
		# loop through selected target addresses
		while ($sql_array = mysql_fetch_object($result)) {
				if (isset($chkIntro)) {
					$message=GENERAL_DEAR." $sql_array->aff_username\n\n".stripslashes($txtMessage);
				} else {
					$message=stripslashes($txtMessage);
					if ($type == "html")
						$message.="<p align='left'><font size='1'>".
						sprintf(PRGADMINMAIL_TEXT,$CONST_URL,$sql_array->aff_email);
				}
				send_mail ("$sql_array->mem_email", "$txtReply", "$txtSubject", "$message", "$type","ON", $lstSendType,'ISO 8859-1','Admin');
				$recno++;
				flush();
				sleep(1);
				print("$recno) $sql_array->aff_username - $sql_array->aff_email<br>");
				if (is_int($recno/500)) {
					print("<br>".PRGADMINMAIL_SLEEP."<br><br>");
					flush();
					sleep(120);
				}
		}

} else {
    //if (isset($chkAllusers)) {$condition1=" WHERE (mem_newsletter = 1 OR mem_newsletter = 0)";} else {$condition1=" WHERE (mem_newsletter = 1)";}
	
	if (isset($_POST['newsletter'])) {$condition1=" WHERE (mem_newsletter = 1)";} else {$condition1=" WHERE (mem_newsletter = 1  OR mem_newsletter = 0)";}
	
      //$condition1=" WHERE (mem_newsletter = '$newsletter')";
	    switch ($lstGender) {
            case 'A':
                $condition2 = "";
                break;
            case 'M':
                $condition2 = " AND (mem_sex = 'M')";
                break;
            case 'F':
                $condition2 = " AND (mem_sex = 'F')";
                break;
            case 'C':
                $condition2 = " AND (mem_sex = 'C')";
                break;
        }
        switch ($lstStatus) {
            case 'All':
                $condition3 = "";
                break;
            case 'Premium':
                $condition3 = " AND (DATE_FORMAT(mem_expiredate,'%Y%m%d') > DATE_FORMAT(NOW(),'%Y%m%d'))";
                break;
            case 'Standard':
                $condition3 = " AND (DATE_FORMAT(mem_expiredate,'%Y%m%d') < DATE_FORMAT(NOW(),'%Y%m%d'))";
                break;
            case 'Inactive':
                $condition3 = " AND (DATE_FORMAT(mem_lastvisit , '%Y%m%d') < DATE_FORMAT(FROM_DAYS( TO_DAYS( curdate( ) ) - 90 ),'%Y%m%d'))";
                break;
 			case 'Rejected':
				$condition3 = " AND adv_approved = 2";
				break;
       }
		 $query="SELECT mem_username, mem_email, adv_approved FROM members LEFT JOIN adverts ON (mem_userid=adv_userid) $condition1$state1$condition2$condition3";
        $result=mysql_query($query,$link) or die(mysql_error());
        echo $recnum=mysql_num_rows($result);
        $recno=0;

        # loop through selected target addresses
        while ($sql_array = mysql_fetch_object($result)) {
                if (isset($chkIntro)) {
                    $message=GENERAL_DEAR." $sql_array->mem_username\n\n".stripslashes($txtMessage);
                } else {
                    $message=stripslashes($txtMessage);
                    if ($type == "html")
                        $message.="<p align='left'><font size='1'>".
                        sprintf(PRGADMINMAIL_TEXT,$CONST_URL,$sql_array->mem_email);
                }
                send_mail ("$sql_array->mem_email", "$txtReply", "$txtSubject", "$message", "$type","ON", $lstSendType);
                $recno++;
                flush();
                sleep(1);
                print("$recno) $sql_array->mem_username - $sql_array->mem_email<br>");
                if (is_int($recno/500)) {
                    print("<br>".PRGADMINMAIL_SLEEP."<br><br>");
                    flush();
                    sleep(120);
                }
        }
}
mysql_close( $link );
?>
<input type='button' class='button' value='<?=BUTTON_BACK?>' onClick="document.location='<?= $CONST_LINK_ROOT ?>/admin/mass_email.php?<?php print("?PHPSESSID=".session_id()); ?>';">