Current File : /home/inlingua/public_html/decay_sym/root/var/softaculous/apps/exim/boxtrapper.php
#!/usr/local/emps/bin/php
<?php

/**
 * Boxtrapper PHP Pipe program
 *
 * @author Julfekar Shaikh
 */

// Define SOFTACULOUS
@define('SOFTACULOUS', 1);

// Include functions file
include_once(__DIR__.'/boxtrapper_func.php'); 

global $user, $group;

// Setting User and Group
$userInfo = posix_getgrgid(posix_getgid());
$user = $userInfo['name'];
$groupInfo = posix_getpwuid(posix_getuid());
$group = $groupInfo['name'];

if(empty($user)){
	exit(0);
}

// Now get the email content
$fh = fopen("php://stdin", "r");

$email_msg = '';
// read through the email until the end
while (!feof($fh)){
	$email_msg .= fread($fh, 1024);
}
fclose($fh);

// Handle email
$lines = explode("\n", $email_msg);

// Empty vars
$from = $subject = $headers = $message = $cc = $spamscore = "";
$splittingheaders = true;
$whitelist = $exist = false;

for ($i=0; $i < count($lines); $i++) {
	if ($splittingheaders) {
		// This is a header
		$headers .= $lines[$i]."\n";

		// look out for special headers
		if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
			$subject = $matches[1];
		}
		if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
			$from = $matches[1];
		}
		if (preg_match("/^To: (.*)/", $lines[$i], $matches)) {
			$to = $matches[1];
		}
		if (preg_match("/^Cc: (.*)/", $lines[$i], $matches)) {
			$cc = $matches[1];
		}
		if (preg_match("/^X-Spam-Score: (.*)/", $lines[$i], $matches)) {
			$spamscore = $matches[1];
		}
	} else {
		// content/main message body information
		$message .= $lines[$i]."\n";
	}

	if (trim($lines[$i])=="") {
		// empty line, header section has ended
		$splittingheaders = false;
	}
}

// Check if having multiple mails
if(!empty($to)){
	$to = explode(',', $to);
}
if(!empty($cc)){
	$cc = explode(',', $cc);
}

// Merge both
if(!empty($to) && !empty($cc)){
	$to = array_merge($to, $cc);
	$to = array_map('trim', $to); // To remove spacing in between
}

// Making only mail from 'FROM' mail
if (preg_match('/ <(.*)>/', $from, $matches)){
	$from = $matches[1];
}

// Loop to and check remaining things and have a action
foreach ($to as $tk => $tv){

	// Make it email only
	if (preg_match('/ <(.*)>/', $tv, $matches)){
		$tv = $matches[1];
	}

	// Now check if this exists
	if(check_email_exists($user, $tv)){
		$exist = true;
	}

	// If not exist then continue
	if(empty($exist)){
		continue;
	}

	// Check if boxtrapper is enabled or not
	if(exim_check_boxtrapper_enabled($user, $tv)){
		$enabled = true;
	}

	// If not enabled then continue
	if(empty($enabled)){
		// $whitelist = true;
		continue;
	}

	// Now get the boxtrapper configuration
	$conf = exim_get_boxtrapper_conf($user, $tv); // Output will be in array
	
	// Now we have to check is spamscore has the ability to bypass and have our from name
	$sa_exist = file_exists('/etc/mail/spamassassin');
	$spcore = (!empty($conf['bypassscore']) && !empty($sa_exist)) ? $conf['bypassscore'] : -2.5;
	$ftomail = !empty($conf['fromname']) ? $conf['fromname'].' <'.$tv.'>' : $tv;

	// Get the bypass
	if($spamscore <= $spcore){
		$bypass = true;
	}
	
	$temail = explode('@', $tv);
	
	$list_path = '/etc/exim/users/'.$user.'/'.$temail[1].'/boxtrapper/'.$temail[0].'/lists/';
	
	// Now we have to check whether from address is whitelisted or not
	if(file_exists($list_path.'whitelist')){
		if(exim_check_boxtrapper_list($user, $from, $tv, $subject, 'whitelist') && !empty($conf['whitelisting_by_assoc'])){
			$whitelist = true;
		}
	}

	// Now we just check either its not in the ignore list
	if(file_exists($list_path.'ignorelist')){
		if(exim_check_boxtrapper_list($user, $from, $tv, $subject, 'ignorelist')){
			boxtrapper_log($user, $tv, 'Ignoring mail from '.$from.'/'.$subject);
			exit(0); // Exit because we dont have to take any action
		}
	}

	// Now we have to check whether the mail contain from blacklist
	if(file_exists($list_path.'blacklist')){
		if(exim_check_boxtrapper_list($user, $from, $tv, $subject, 'blacklist')){
			boxtrapper_log($user, $tv, 'Rejecting mail from '.$from.'/'.$subject.' as it is blacklisted');
			sendblacklistmail($user, $from, $tv, $subject, $headers, $ftomail);
			exit(0); // Exit because we dont have to take any action
		}
	}

	// Now if either the mail whitelisted or able to bypass then accept the mail
	if(!empty($bypass) || !empty($whitelist)){
		// Copy the mail file to address
		deliver_to_mail_address($user, $tv, $email_msg, '');

		if(file_exists($list_path.'forwards') && !empty($whitelist)){
			forward_to_forwarders($user, $tv, $headers, $subject, $message);
		}
		exit(0); // As we had finished with our task
	}

	// Connect with Queue DB 
	$con = new SQLite3('/etc/exim/users/'.$user.'/'.$temail[1].'/boxtrapper/'.$temail[0].'/queue.db');
	
	preg_match("/verify#(.*)/", $subject, $output);

	if(!empty($output)){
		
		$out = explode(' ', $output[1]);

		// Get the list of Queue ID's from the DB
		$results = $con->query('SELECT * FROM `queue` WHERE msgid = "'.trim($out[0]).'"');
		if(empty($results->fetchArray())){
			// Means the authentication Key is wrong So we have to reply with wrong Msg id message
			send_returnverify_mail($user, $tv, $from, trim($out[0]), $subject, $headers);
			exit(0);
		}

		// If valid, Deliver Message Message
		$res = $con->query('SELECT * FROM `queue` WHERE `from` = "'.trim($from).'"');

		while ($row = $res->fetchArray()){
			$ch[] = $row['msgid'];
			deliver_to_mail_address($user, $tv, '', $row['msgid']);
		}
		
		// After delivery the mails remove entry from the DB
		$con->query('DELETE FROM `queue` WHERE `from` = "'.trim($from).'"');

		// Make it whitelist if conf allowed us and send verified Message
		if(!empty($conf['whitelisting'])){
			add_mail_in_whilelist($user, $tv, $from);
		}
		while ($row = $results->fetchArray()) {
			$user = $row['user'];
		}
		send_verifyreleased_mail($user, $tv, $from, $headers);
		
		exit(0);

	}

	// Now it is only remain for new mails 
	$msg_id = generateRandString(7);

	add_mail_in_queue($user, $email_msg, $msg_id, $tv);
	send_verify_mail($user, $tv, $from, $msg_id, $headers);

	// Add in DB
	$results = $con->query('INSERT INTO `queue` (`msgid`, `from`, `subject`, `body`, `time`) VALUES ("'.$msg_id.'", "'.$from.'", "'.htmlentities($subject).'", "'.htmlentities($message).'", "'.time().'")');

	boxtrapper_log($user, $tv, 'A mail has been added to queue from '.$from."\n");

	exit(0); // End of the script
}

// End the script
exit(0);