<?php



/*	Comments here */



chdir(dirname(__FILE__));



require_once("./config.inc.php");



error_reporting(E_ALL ^ E_NOTICE);



//-------------------------------------------------------------------

function strip_array($ar)

{

	foreach ($ar as $_k=>$_v) 

		$ar[$_k] = (is_array($_v)) ? strip_array($_v): stripslashes($_v);

	return $ar;

}

//-------------------------------------------------------------------

function validate_admin($username, $password)

{

	if ($username != ADMIN_USERNAME)

		return false;

	if ($password != ADMIN_PASSWORD)

		return false;

	return true;

}

//-------------------------------------------------------------------

function msg($type, $message)

{

	global $files;

	$f = fopen($files['logs'],'a+');

	fputs($f,"$type\t".date("m/d/y\tH:i:s\t").trim($message).DELIMITER_LINES);

	fclose($f);

}

//-------------------------------------------------------------------

function rules($new_rules = null)

{

	global $files;

	$ss = @file($files['rules']);

	$rules = array();

	foreach ($ss as $s)

	{

		$s = trim($s);

		if (!$s) continue;

		list ($incoming, $optin, $forward) = explode(DELIMITER_FIELDS,$s);

		$rules[] = array(

			'incoming'	=> trim($incoming),

			'optin'		=> explode(DELIMITER_EMAILS,trim($optin)),

			'forward'	=> explode(DELIMITER_EMAILS,trim($forward)));

	}	



	return $rules;

}

//-------------------------------------------------------------------

function add_parsed_header(&$where,$header)

{

	$x = strpos($header,":");

	$where[substr($header,0,$x)] = trim(substr($header,$x+1));

}

//-------------------------------------------------------------------

function there_is_a_match($header, $email)

{

	if ($header == $email) 

		return true;

	if (strpos($header,"<$email>")!==false) 

		return true;

	return false;

}

//-------------------------------------------------------------------

function is_valid_email($email) 

{

	//checks if $email address is correct

	return (preg_match("#^[_\.0-9a-z-]+@([0-9a-z][-0-9a-z\.]+)\.([a-z]{2,}$)#i", $email, $check));

}

//-------------------------------------------------------------------

function send_optins($incoming, $headers, $body, $recipients)

{

	$replyto = get_reply_to($headers);

	$name = extract_name($header);



	unset($headers['To']);

	unset($headers['Subject']);

	$headers['From'] = ($name) ? "\"".addcslashes($name,'"')."\" <$replyto>" : $replyto;



	foreach ($recipients as $optin)

	{

		$optin = trim($optin);

		if (!is_valid_email($optin)) continue;

		$headers['Envelope-to'] = $optin;

		$headers['Reply-To'] = $replyto;



		send_mail($optin,$incoming,$body,$headers);

	}



}

//-------------------------------------------------------------------

function send_forwards($incoming, $headers, $body, $recipients)

{

	$subject = $headers['Subject']." [$incoming]";

	unset($headers['Subject']);

	unset($headers['To']);



	foreach ($recipients as $forward)

	{

		$forward = trim($forward);

		if (!is_valid_email($forward)) continue;



		send_mail($forward,$subject,$body,$headers);

	}

}

//-------------------------------------------------------------------

function extract_address($s)

{

	preg_match("#[_\.0-9a-z-]+@([0-9a-z][-0-9a-z\.]+)\.([a-z]{2,})#i",$s,$matches);	

	return $matches[0];

}

//-------------------------------------------------------------------

function get_reply_to($headers)

{

	$rt = extract_address($headers['Reply-To']);

	$fr = extract_address($headers['From']);



	return ($rt) ? $rt : $fr;

}

//-------------------------------------------------------------------

function extract_name($s)

{

	$email = extract_address($s);

	$s = str_replace($email,'',$s);

	$name = trim($s," \n\t<>\"");

	return $name;

}

//-------------------------------------------------------------------

function send_mail($to, $subject, $body, $headers = null, $extra = "")

{

	if (!is_array($headers))

		$headers = array();



	$Delim = "\r\n";



	$AllowedHeaders = array(

		'Date', 'From', 'Reply-To', 'Organization',

		'X-Priority','Content-Transfer-Encoding',

		'Content-Type', 'MIME-Version', 'Importance',

		'In-Reply-To','X-Confirm-Reading-To',

		'Disposition-Notification-To','Return-Receipt-To', 'Delivery-date', 'Envelope-to');



	$passheaders = "";

	$from_log = "";



	foreach ($AllowedHeaders as $name)

	{

		if ($headers[$name])

			$passheaders .= "$name: {$headers[$name]}$Delim";

		if ($name == "From")

			$from_log = $headers[$name];

	}



	if ($extra)

		$passheaders .= "$Delim$extra";



	msg('MAIL',"To: $to ||| Subject: $subject ||| From: $from_log");



	if (MAILER_METHOD == 'mail')

		return mail ($to,$subject,$body,ltrim($passheaders));



	if (MAILER_METHOD == 'smtp')

		return smtp_mail(extract_address($to),$subject,$body,ltrim($passheaders));



	return false; //unknown MAILER_METHOD

}

//-------------------------------------------------------------------

function parse_bounce($s,$email)

{

	return str_replace("%email%",$email,$s);

}

//-------------------------------------------------------------------

function smtp_mail($to, $subject, $message, $headers = '')

{

	global $smtp;



    $recipients = explode(',', $to);

    $user = $smtp['username'];

    $pass = $smtp['password'];

    // The server details that worked for you in the above step

    $smtp_host = $smtp['hostname'];

    //The port that worked for you in the above step

    $smtp_port = $smtp['port_num'];

 

    if (!($socket = @fsockopen($smtp_host, $smtp_port, $errno, $errstr, 15)))

    {

      msg('SMTP',"Error connecting to '$smtp_host' ($errno) ($errstr)");

	  return false;

    }

 

    if (!server_parse($socket, '220')) return false;



	$host = parse_url($smtp_host,PHP_URL_HOST);

 

    fwrite($socket, 'EHLO '.$host."\r\n");

    if (!server_parse($socket, '250')) return false;

 

    fwrite($socket, 'AUTH LOGIN'."\r\n");

    if (!server_parse($socket, '334')) return false;

 

    fwrite($socket, base64_encode($user)."\r\n");

    if (!server_parse($socket, '334')) return false;

 

    fwrite($socket, base64_encode($pass)."\r\n");

    if (!server_parse($socket, '235')) return false;

 

    fwrite($socket, 'MAIL FROM: <'.$user.'>'."\r\n");

    if (!server_parse($socket, '250')) return false;

 

    foreach ($recipients as $email)

    {

        fwrite($socket, 'RCPT TO: <'.$email.'>'."\r\n");

        if (!server_parse($socket, '250')) return false;

    }

 

    fwrite($socket, 'DATA'."\r\n");

    if (!server_parse($socket, '354')) return false;

 

    fwrite($socket, 'Subject: '

      .$subject."\r\n".'To: <'.implode('>, <', $recipients).'>'

      ."\r\n".$headers."\r\n\r\n".$message."\r\n");

 

    fwrite($socket, '.'."\r\n");

    if (!server_parse($socket, '250')) return false;

 

    fwrite($socket, 'QUIT'."\r\n");

    fclose($socket);

 

    return true;

}

//-------------------------------------------------------------------

//Function to Processes Server Response Codes

function server_parse($socket, $expected_response)

{

    $server_response = '';

    while (substr($server_response, 3, 1) != ' ')

    {

        if (!($server_response = fgets($socket, 256)))

        {

          msg('SMTP','Error while fetching server response codes.');

		  return false;

        }            

    }

 

    if (!(substr($server_response, 0, 3) == $expected_response))

    {

      msg('SMTP','Unable to send e-mail: '.$server_response);

	  return false;

    }



	return true;

}

//-------------------------------------------------------------------



if (get_magic_quotes_gpc()==1) $_POST = strip_array($_POST);



//-------------------------------------------------------------------



