<?php
include("includes/site_info.php");
include("session_handler.inc");
include('lib/lib.Functions.php');

$db = new db();

$WUserID = $_SESSION['Sess_UserId'];
$WUserType = $_SESSION['Sess_UserType'];

$userid=$_REQUEST['userid'];

$myhandle=$_REQUEST['myhandle'];

$txtSubject=trim($_REQUEST['txtSubject']);

$txtMessage=trim($_REQUEST['txtMessage']);
if (strlen($txtSubject) == '') {
                $error_message=PRGSENDMAIL_TEXT1;
                echo "Without Subject???<br><br>";
				die;
}

if (strlen($txtMessage) == '') {
                $error_message=PRGSENDMAIL_TEXT2;
                echo "Without Message???<br><br>";
				die;
}

if (!$userid) {
                $error_message=PRGSENDMAIL_TEXT6;
                echo "Without Receipent???<br><br>";
				die;
}

$query = "SELECT count(*) FROM blockmail WHERE blk_receiverid = '$userid' AND blk_senderid =  '$WUserID'";

if ($db->get_var($query)) {
        $error_message=PRGSENDMAIL_TEXT9;
                echo $error_message;
				die;
}

####################
# ANTI SPAM CHECK
####################

if ($CONST_SPAM_ON=='Y'){
        $spam=false;
        $spam_result=$db->get_var("select count(*) from messages where msg_title='$txtSubject' and msg_text='$txtMessage' and msg_senderid=$WUserID");
        if ($spam_result >= $CONST_SPAM_TOLERANCE) {
                $spam=true;
        } else {
            $spam_array=array("president"," late husband","assassination","transaction","security company",
                                            "sealed trunk","BANK ACCOUNT","MILLION US DOLLARS","your commission","US$",
                                            "million", "deposit", "$10,000", "West Africa", "Nigeria", "Ivory Coast",
                                            "ivory coast", "shipping cost", "USD", "your assitance", "urgent business",
                                            "Nigerian telecommunication Plc","SUCCESSFUL TRANSFER","bank","assitance",
											"transfer","money","assist","deposit", "SUCCESSFUL TRANSFER", "nigeria");
            $count=0;
            foreach($spam_array as $value) {
                if (stristr ( $txtMessage, $value)) $count++;
            }
            if ($count >= 3) $spam=true;
        }
        if ($spam==true) {
            $mem_array = $db->get_row("select * from members where mem_userid=$WUserID");
			send_mail ("$CONST_MAIL", "$CONST_MAIL", "Spammer detected", "$mem_array->mem_username ($mem_array->mem_userid) has been suspended for possible spamming.<br><br>The members' original expire date was $mem_array->mem_expiredate<br><br>If this was <u>not</u> spam you can reinstate the user through Admin->Member Administration and update the expire date as noted above.","html", "ON");
            $db->query("update members set mem_expiredate='2002-01-01' where mem_userid=$WUserID");
            session_destroy();
            header("Location: $CONST_LINK_ROOT/spam.php");
            exit;
        }
}
####################
# END OF SPAM CHECK
####################

# Add the message to the database and mail the user
$query="INSERT INTO messages (msg_senderid, msg_receiverid, msg_senderhandle, msg_title, msg_text, msg_dateadded, msg_read) VALUES ('$WUserID', '$userid','$myhandle' ,'$txtSubject', '$txtMessage', NOW(), 'U' )";
$db->query($query);

//echo "SELECT mem_username, mem_email FROM members LEFT JOIN adverts on mem_userid=adv_userid WHERE mem_userid = '$userid'";
$sql_array = $db->get_row("SELECT mem_username, mem_email FROM members LEFT JOIN adverts on mem_userid=adv_userid WHERE mem_userid = '$userid'");

$data['ReceiverName'] =$sql_array->mem_username;
$data['SenderName'] = $myhandle;
$data['CompanyName'] = $CONST_COMPANY;
$data['Url'] = $CONST_URL;
$data['SupportEmail'] = $CONST_SUPPMAIL;
$CONST_MAIL = $option_manager->GetValue('mail'); 
$smsEmail=$sql_array->adv_phone.$sql_array->phn_provider;
list($type,$message) = getTemplateByName("You_Have_Got_A_Mail",$data,getDefaultLanguage($userid));
send_mail ("$sql_array->mem_email", "$CONST_MAIL", PRGSENDMAIL_TEXT4, $message ,$type,"ON");
echo "Opps! Your Message Done!!!<br><br>";
echo "<script type='text/javascript'>setTimeout(function() { parent.jQuery.facebox.close(); }, 1000);</script>";
?>
