<?php 
require_once(dirname(__file__)."/class.phpmailer.php");
require_once(dirname(__file__)."/extras/htmlfilter.php");
require_once(dirname(__file__)."/extras/class.html2text.php");

class mailer{

	var $SMTPAuth = true;
	var $Host		= "mydesitickets.com";		//Host
	var $Port		= 465;						//Set the SMTP port number - likely to be 25, 2626 or 587
	var $Username	= "mailer@mydesitickets.com";	//Username
	var $Password	= '8(vF$~[@h$S#';			//Password
	var $SMTPSecure	= "TLS";	

	var $SpamReplaceText = "*content removed*";
	var $SpamErrorMessage;
	
	function __construct(){
		$this->SpamErrorMessage = "<p align=\"center\"><font color=\"red\">Malicious code content detected.</font><br><b>Your IP Number of <b>".getenv("REMOTE_ADDR")."</b> has been logged.</b></p>";
		
		$this->mail = new PHPMailer(); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
	}

	function attachment($path)
	{
		$this->mail->AddAttachment($path  );
	}


	function Send(){
		$Subject 	= $this->escape($this->Subject);
		
		$this->body = file_get_contents($this->template);
		
		if (get_magic_quotes_runtime()) {
			$this->body = stripslashes($this->body);
		}
		if (get_magic_quotes_gpc()) {
			$this->body = stripslashes($this->body);
		}
		
		foreach($this->data as $key => $val) 
		$this->body = str_replace("|@|".$key."|@|",$val,$this->body);
		
		$this->body = HTMLFilter($this->body,"images/");
		$html2text = new html2text($this->body);
		$altbody = $html2text->get_text();

		// smtp authentication
		if($this->SMTPAuth){
			$this->mail->SMTPAuth = $this->SMTPAuth;
			$this->mail->SMTPSecure = $SMTPSecure;
			$this->mail->Host = ($SMTPSecure=="SSL"?"ssl://":"").$this->Host;
			$this->mail->Port = $this->Port;
			$this->mail->Username = $this->Username;
			$this->mail->Password = $this->Password;
		}
		else{
			$this->mail->IsSendmail();
		}
		
		$this->mail->SMTPDebug  = $this->SMTPDebug;
		$this->mail->Debugoutput = 'html';

		$this->mail->Subject = $Subject;
		$this->mail->AltBody = $altbody; // optional - MsgHTML will create an alternate automatically
		$this->mail->MsgHTML($this->body);
		return $this->mail->Send();

	}
	
	function setTo($to, $to_name){
		$this->to = $to;
		$this->to_name = $to_name;
		$this->mail->AddAddress($this->escape($to), $this->escape($to_name));
	}

	function setFrom($from, $from_name){
		$this->from = $from;
		$this->from_name = $from_name;
		$this->mail->AddReplyTo($this->escape($from), $this->escape($from_name));
		$this->mail->SetFrom($this->escape($from), $this->escape($from_name));
		$this->mail->AddReplyTo($this->escape($from), $this->escape($from_name));
	}
	
	function setCC($from, $from_name){
		$this->mail->AddCC($this->escape($from), $this->escape($from_name));
	}
	
	function setBCC($from, $from_name){
		$this->mail->AddBCC($this->escape($from), $this->escape($from_name));
	}

	function clear(){
		$this->mail->ClearAllRecipients(); 
		$this->mail->ClearReplyTos();
		$this->mail->ClearCustomHeaders();
	}
	
	function escape($str){		
		// Check for Website URL's in the form input boxes as if we block website URLs from the form,
		// then this will stop the spammers wastignt ime sending emails
		if (preg_match("/http/i", "$str")) {echo "$this->SpamErrorMessage"; exit();}
		
		// Patterm match search to strip out the invalid charcaters, this prevents the mail injection spammer 
		$pattern = '/(;|\||`|>|<|&|^|"|'."\n|\r|'".'|{|}|[|]|\)|\()/i'; // build the pattern match string 
		
		$str = preg_replace($pattern, "", $str); 
		
		// Check for the injected headers from the spammer attempt 
		// This will replace the injection attempt text with the string you have set in the above config section
		$find = array("/bcc\:/i","/Content\-Type\:/i","/cc\:/i","/to\:/i"); 
		$str = preg_replace($find, "$this->SpamReplaceText", $str);
		
		return $str;
	}
	
}
global $mailer;
$mailer = new mailer();
?>