Current File : /home/inlingua/public_html/decay_sym/root/var/softaculous/apps/exim/boxtrapper_func.php
<?php

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

 function check_email_exists($user, $email){
	
	$emailuser = explode('@', $email);
	
	$userDir = '/etc/exim/users/'.$user.'/'.trim($emailuser[1]).'/passwd';
	
	if(!file_exists($userDir)){
		return false;
	}
	
	// Email user list of domain
	$lines = file($userDir);
	
	foreach($lines as $k => $v){
		
		// Email Username !!!
		$data = explode(':', $lines[$k]);
		if(trim($data[0]) == trim($emailuser[0])){
			return true;
		}
	}
	return false;
}

function add_mail_in_queue($user, $msg, $msg_id, $mail){

	$email = explode("@", $mail);
	$queueDIR = '/etc/exim/users/'.$user.'/'.$email[1].'/boxtrapper/'.$email[0].'/queue';
	@mkdir($queueDIR);
	exim_perms($user, $queueDIR);
	$msgpath = $queueDIR.'/'.$msg_id.'.msg';

	// Adding msg
	file_put_contents($msgpath, $msg);
	exim_perms($user, $msgpath);

}

function add_mail_in_whilelist($user, $tomail, $from){
	
	$email = explode('@', $tomail);
	
	$box_path = '/etc/exim/users/'.$user.'/'.$email[1].'/boxtrapper/'.$email[0];
	$box_list_path = $box_path.'/lists/';
	$whitelist = $box_list_path.'whitelist';
	$log_path = $box_path.'/log';
	
	if(!is_dir($box_list_path)){
		mkdir($box_list_path);
	}
	
	exim_perms($user, $box_list_path);
	
	file_put_contents($whitelist, "\nfrom ".$from, FILE_APPEND);
	exim_perms($user, $whitelist);
	
	boxtrapper_log($user, $tomail, 'Whitelisting and delivering all mails from '.$from."\n");
	
	
}

function send_verify_mail($user, $to, $from, $msg_id, $header){

	$email = explode('@', $to);
	$mdata = get_boxtrapper_automessages($user, $to, 'verify');
	$url = 'http://'.$email[1].':2002/boxtrapper.php?email='.$to.'&msgid='.$msg_id;

	// Update in subject and msg
	$subject = str_replace('{{msgid}}', $msg_id, $mdata['subject']);
	$msg = str_replace('{{url}}', $url, $mdata['body']);
	$msg = str_replace('{{headers}}', $header, $msg);

	// Now we had the subject and body ready to send 
	sendboxtrappermail($from, $to, $subject, $msg);
}

function send_verifyreleased_mail($user, $to, $from, $header){
	$mdata = get_boxtrapper_automessages($user, $to, 'verifyreleased');

	// Update in subject and msg
	$subject = str_replace('{{fromname}}', $to, $mdata['subject']);
	$msg = str_replace('{{fromname}}', $to, $mdata['body']);
	$msg = str_replace('{{headers}}', $header, $msg);

	// Now we had the subject and body ready to send 
	sendboxtrappermail($from, $to, $subject, $msg);
}

function deliver_to_mail_address($user, $to, $msg = '', $msgid = ''){

	$email = explode('@', $to);
	$boxpath = '/etc/exim/users/'.$user.'/'.$email[1].'/boxtrapper/'.$email[0];

	// First if MSG is empty then we need to find the MSG
	if(empty($msg)){
		$msg = file_get_contents($boxpath.'/queue/'.$msgid.'.msg');
	}
	
	$mailpath = $boxpath.'/mail/';
	@mkdir($mailpath);
	exim_perms($user, $mailpath);
	
	$msgfile = $mailpath.time().'M'.rand(0000, 9999).$email[1];
	file_put_contents($msgfile, $msg);
	
}

function forward_to_forwarders($user, $to, $headers, $subject, $message){

	$email = explode('@', $to);

	$forwardlist = file('/etc/exim/users/'.$user.'/'.$email[1].'/boxtrapper/'.$email[0].'/lists/forwards');

	if(!empty($forwardlist)){
		foreach($forwardlist as $fk => $fv){
			sendboxtrappermail($fv, $to, $subject, $message, $headers);
		}
		boxtrapper_log($user, $to, 'The mail with subject:'.$subject.' has been delivered and forwarded to '.implode(', ', $forwardlist));
	}

	return true;
	
}

function send_returnverify_mail($user, $to, $from, $msgid, $subject, $header){
	$mdata = get_boxtrapper_automessages($user, $to, 'returnverify');

	// Update in subject and msg
	$subject = str_replace('{{subject}}', $subject, $mdata['subject']);
	$msg = str_replace('{{headers}}', $header, $mdata['body']);
	$msg = str_replace('{{msgid}}', $msgid, $msg);

	// Now we had the subject and body ready to send 
	sendboxtrappermail($from, $to, $subject, $msg);
}

function sendblacklistmail($user, $from, $to, $subject, $headers, $fromname){
	$mdata = get_boxtrapper_automessages($user, $to, 'blacklist');

	// Update in subject and msg
	$subject = str_replace('{{subject}}', $subject, $mdata['subject']);
	$msg = str_replace('{{headers}}', $headers, $mdata['body']);
	$msg = str_replace('{{acct}}', $from, $msg);

	// Now we had the subject and body ready to send 
	sendboxtrappermail($from, $fromname, $subject, $msg);
}

// Send mail using 
function sendboxtrappermail($to, $from = '', $subject, $msg, $header = ''){
	
	if(empty($header)){
		if(!empty($from)){
			$header .= "From:$from \r\n";
		}
		$header .= "MIME-Version: 1.0\r\n";
		$header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
	}

	mail($to, $subject, nl2br($msg), $header);
	return true;

}

// Make boxtrapper log
function boxtrapper_log($user, $mail, $msg) {

	$email = explode("@", $mail);

	$emailpath = '/etc/exim/users/'.$user.'/'.$email[1].'/boxtrapper/'.$email[0];
	$logpath = $emailpath.'/log';

	if(!file_exists($emailpath)){
		@mkdir($emailpath);
		exim_perms($user, $emailpath);
	}

	if(!file_exists($logpath)){
		@mkdir($logpath);
		exim_perms($user, $logpath);
	}

	$logfile = $logpath.'/'.date('Y-m-d').'.log';
	file_put_contents($logfile, date('Y-m-d h:i:s').' '.$msg, FILE_APPEND);
	exim_perms($user, $logfile);
	
}

function exim_perms($user, $path){
	global $group;
	
	chown($path, $group);
	chgrp($path, $user);
	
	if(is_dir($path)){
		chmod($path, 0750);
	}elseif(is_file($path)){
		chmod($path, 0740);
	}
}

function exim_check_boxtrapper_list($user, $from, $to, $subject, $list = 'whitelist'){

	$listarr = ['whitelist', 'ignorelist', 'blacklist'];

	// Check if list called are valid list
	if(!in_array($list, $listarr)){
		return false;
	}

	$email = explode('@', $to);

	$lists = file('/etc/exim/users/'.$user.'/'.$email[1].'/boxtrapper/'.$email[0].'/lists/'.$list);

	foreach($lists as $key => $val){
		
		// Here we just check the values available in the list with there preg
		if((preg_match('/'.$val.'/', 'from '.$from)) || (preg_match('/'.$val.'/', 'to '.$to)) || (preg_match('/'.$val.'/', 'subject '.$subject)) || (preg_match('/'.$val.'/', 'cc '.$to))){
			return true;
		}

		// Now with our Preg
		if((preg_match('/^from+\s'.$from.'/', $val)) || (preg_match('/^to+\s'.$to.'/', $val)) || (preg_match('/^subject+\s'.$subject.'/', $val)) || (preg_match('/^cc+\s'.$to.'/', $val))){
			return true;
		}
	}
	return false;

}

function exim_check_boxtrapper_enabled($user, $mail){
	global $softpanel;
	
	$email = explode('@', $mail);
	
	$vaild_email = filter_var($mail, FILTER_VALIDATE_EMAIL);
	
	if(empty($vaild_email)){
		return false;
	}
	
	if(class_exists('softpanel')){
		$file_exist = $softpanel->root_func_exec('file_exists', ['/etc/exim/users/'.$user.'/'.$email[1].'/boxtrapper/'.$email[0].'/.enabled']);
	}else{
		$file_exist = file_exists('/etc/exim/users/'.$user.'/'.$email[1].'/boxtrapper/'.$email[0].'/.enabled');
	}

	if(($file_exist)){
		return true;
	}

	return false;

}

function exim_get_boxtrapper_conf($user, $mail){

	$email = explode('@', $mail);

	$confpath = '/etc/exim/users/'.$user.'/'.$email[1].'/boxtrapper/'.$email[0].'/boxconf.json';

	// Check if the conf exist
	if(!file_exists($confpath)){
		return false;
	}

	$conf = file_get_contents($confpath);
	$conf = json_decode($conf, true);

	return $conf;

}

function get_boxtrapper_automessages($user, $mail, $message){

	$email = explode('@', $mail);

	$msgpath = '/etc/exim/users/'.$user.'/'.$email[1].'/boxtrapper/'.$email[0].'/message/'.$message;

	// Check if the message exist
	if(!file_exists($msgpath)){
		return false;
	}

	$mdata = file_get_contents($msgpath);
	$mdata = json_decode($mdata, true);

	return $mdata;

}

function generateRandString($length, $special = 0){
	
	$randstack = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '');
	
	$specialchars = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');

	$randstr = '';

	while(strlen($randstr) < $length){
		
		$randstr .= $randstack[array_rand($randstack)];
		
		if(!empty($special) && strlen($randstr) < $length && (strlen($randstr)%2 == 0)){
			$randstr .= $specialchars[array_rand($specialchars)];
		}
		
	}
	
	return str_shuffle($randstr);

}

?>