<?php
// +-------------------------------------------------------------+
// | HiveMail™ version 1.3.1
// +-------------------------------------------------------------+
// | HIVEMAIL™ IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than Hivemail or if you have otherwise received
// | this software from someone who is not a representative of
// | this organization you are involved in an illegal activity.
// +-------------------------------------------------------------+
// | $RCSfile: compose.send.php,v $ - $Revision: 1.80.4.1 $
// | $Date: 2004/08/15 02:27:03 $ - $Author: tubedogg $
// +-------------------------------------------------------------+

error_reporting(E_ALL & ~E_NOTICE);
if ($_REQUEST['cmd'] != 'redirect') {
	define('LOAD_MINI_TEMPLATES', true);
}
$templatesused = 'redirect_mailsent';
require_once('./global.php');

// ############################################################################
// Are we just redirecting or showing a template?
if ($cmd == 'redirect') {
	eval(makeredirect('redirect_mailsent', INDEX_FILE.iif($hiveuser['returnsent'], '?folderid=-2')));
}

// ############################################################################
// Require all needed functions
require_once('./includes/functions_pop.php');
require_once('./includes/functions_smtp.php');
require_once('./includes/functions_mime.php');

// ############################################################################
// The while() loop allows us to break out of the main code easily
$errortemplate = '';
do {

// ############################################################################
// Get draft information
$draft = getinfo('draft', $draftid);
if (isset($popid) and is_numeric($popid)) {
	$data['sendby'] = $popid;
}
$tempdata = unserialize_base64($draft['data']);
$data['attach'] = $tempdata['attach'];
if (!isset($data['message']) and isset($message)) {
	$data['message'] = $message;
}
unset($tempdata);

// ############################################################################
// Update the new data in the database, just in case we go back sometime
$DB_site->query("
	UPDATE hive_draft
	SET data = '".addslashes(base64_encode(serialize($data)))."'
	WHERE draftid = $draftid
");

// ############################################################################
// Check sending rate limit
if ($hiveuser['msgpersec']['every'] > 0 and $hiveuser['lastsent'] > (TIMENOW - ($hiveuser['msgpersec']['every'] * $hiveuser['msgpersec']['unit']))) {
	$value = $hiveuser['msgpersec']['every'];
	switch ($hiveuser['msgpersec']['unit']) {
		case 1:
			$unit = 'second(s)';
			break;
		case 60:
			$unit = 'minute(s)';
			break;
		case 3600:
			$unit = 'hour(s)';
			break;
		case 86400:
			$unit = 'day(s)';
			break;
	}
	$lastsent = ceil(($hiveuser['msgpersec']['every'] * $hiveuser['msgpersec']['unit'] - TIMENOW + $hiveuser['lastsent']) / $hiveuser['msgpersec']['unit']);
	$errortemplate = 'error_oversendingrate';
	break;
}

// ############################################################################
// Add group signature
if ($data['html'] and !empty($hiveuser['groupsig_html'])) {
	if (substr($data['message'], -strlen('</DIV>') == '</DIV>')) {
		$data['message'] = substr($data['message'], 0, -strlen('</DIV>'))."\n\n<br />\n<br />\n$hiveuser[groupsig_html]</DIV>";
	} else {
		$data['message'] .= "\n\n<br />\n<br />\n$hiveuser[groupsig_html]</DIV>";
	}
} elseif (!$data['html'] and !empty($hiveuser['groupsig_text'])) {
	$data['message'] .= CRLF.CRLF.$hiveuser['groupsig_text'];
}

// ############################################################################
// Send as HTML
if ($data['html']) {
	$data['message'] = preg_replace("#<BR>(\r?\n){0}#i", "<BR>\n", str_replace('<BR><BR>', "<BR><BR>", $data['message']));
	$data['plainmessage'] = strtr(strip_tags($data['message']), array_flip(get_html_translation_table(HTML_ENTITIES)));
	$data['htmlmessage'] = $data['message'];

	// Add background color if one was selected
	if (!empty($data['bgcolor'])) {
		$data['htmlmessage'] = "<body bgcolor=\"$data[bgcolor]\">$data[htmlmessage]</body>";
	}
} else {
	$data['plainmessage'] = $data['message'];
}
$data['plainmessage'] = preg_replace("#\r?\n#", CRLF, $data['plainmessage']);
$data['htmlmessage'] = preg_replace("#\r?\n#", CRLF, $data['htmlmessage']);
unset($data['message']);

// ############################################################################
// Fix priority
if ($data['priority'] != '1' and $data['priority'] != '5') {
	$data['priority'] = 3;
}

// ############################################################################
// Are we sending through an external account?
$smtp_info = null;
if (is_numeric($data['sendby']) and $data['sendby'] > 0 and ($pop = getinfo('pop', $data['sendby'], false, false))) {
	$smtp_info = array('host' => $pop['smtp_server'], 'port' => $pop['smtp_port'], 'helo' => $_SERVER['SERVER_NAME'], 'auth' => (!empty($pop['smtp_password']) and !empty($pop['smtp_username'])), 'user' => $pop['smtp_username'], 'pass' => pop_decrypt($pop['smtp_password']));
	if (smtp_validate($smtp_info)) {
		$hiveuser['realname'] = $pop['displayname'];
		$hiveuser['fromemail'] = $pop['displayemail'];
		$hiveuser['replyto'] = $pop['replyto'];
	} else {
		$smtp_info = null;
	}
} else {
	$hiveuser['fromemail'] = $data['sendby'];
	$hiveuser['replyto'] = iif($hiveuser['overridereplyto'], $data['sendby'], $hiveuser['replyto']);
}

// ############################################################################
// Create the boundary
$semi_rand = md5(TIMENOW);
$mime_boundary = '==Multipart_Boundary_x'.$semi_rand.'x';
$mime_boundary_inner = '==Inner_Boundary_x'.$semi_rand.'x';
$data['plainmessage'] = str_replace($mime_boundary, '', $data['plainmessage']);
$data['htmlmessage'] = str_replace($mime_boundary, '', $data['htmlmessage']);

// ############################################################################
// The recipient lists
$to = encodelist($data['to']);
$cc = encodelist($data['cc']);
$bcc = encodelist($data['bcc']);

// Too many recipients?
$allRecipients = extract_email("$data[to] $data[cc] $data[bcc]", true);
if (($numRecipients = count($allRecipients)) > $hiveuser['maxrecips'] and $hiveuser['maxrecips'] > 0) {
	$errortemplate = 'error_maxrecipients';
	break;
}

// ############################################################################
// The email subject
$subject = trim($data['subject']);

// ############################################################################
// Make sure all fields are there
if (empty($to) or !extract_email($to)) {
	$errortemplate = 'error_noto';
	break;
} elseif (empty($subject)) {
	$errortemplate = 'error_nosubject';
	break;
} elseif (empty($data['plainmessage']) and !is_array($data['attach'])) {
	$errortemplate = 'error_nomessage';
	break;
}

// ############################################################################
// Set the right Content-Type
if (!$data['html'] and !is_array($data['attach'])) {
	$content_type = 'text/plain';
} elseif ($data['html'] and !is_array($data['attach'])) {
	$content_type = 'multipart/alternative';
} else {
	$content_type = 'multipart/mixed';
}

// ############################################################################
// Create the headers
if (!is_numeric($data['sendby']) and $hiveuser['overridereplyto'] and $data['sendby'] != $hiveuser['username'].getop('domainname')) {
	$data['replyto'] = $data['sendby'];
} elseif (!($data['replyto'] = extract_email($data['replyto'])) and !($data['replyto'] = extract_email($hiveuser['replyto']))) {
	$data['replyto'] = $hiveuser['username'].$hiveuser['domain'];
}
		
$headers  =	'Reply-To: <'.$data['replyto'].'>'.CRLF.							// Reply-To
			'Return-Path: <'.$data['replyto'].'>'.CRLF.							// Return-Path
			'From: "'.$hiveuser['realname'].
			'" <'.$hiveuser['fromemail'].'>'.CRLF.								// From who
			//'To: '.$to.CRLF.													// To email
			iif(!empty($cc), 'CC: '.$cc.CRLF).									// CC emails
			iif(!empty($bcc), 'Bcc: '.$bcc.CRLF).								// Bcc emails
			'MIME-Version: 1.0'.CRLF.											// Mime version
			'X-Mailer: '.getop('mailer_name').CRLF.								// The mailer
			'X-Mailer-Version: '.HIVEVERSION.CRLF.								// Mailer version
			'Date: '.date('r').CRLF.											// The date
			'X-Priority: '.$data['priority'].CRLF.								// Priority
			iif($hiveuser['sendip'], 'X-Originating-IP: '.IPADDRESS.CRLF).		// IP address
			'Content-Type: '.$content_type;										// Content type
if ($content_type != 'text/plain') {
	$headers .= ';'.CRLF.'              boundary="'.$mime_boundary.'"';			// Boundary / encoding
} else {
	$headers .= CRLF.'Content-Transfer-Encoding: '.iif(SEND_WITH_BASE64, 'base64', '7bit');
}

		
// ############################################################################
// Add In-Reply-To and References headers if applicable	
if (substr($data['special'], 0, 2) == 're') {
	$messageid = substr($data['special'], 3);
	$message = getinfo('message', $messageid);
	if (!empty($message['emailid'])) {
		get_source($message);
		$headers .= CRLF.'In-Reply-To: '.$message['emailid'];
		$headers .= CRLF.'References: ';
		$references = trim(get_references($message['source']));
		if (!empty($references)) {
			$headers .= $references.' ';
		}
		$headers .= $message['emailid'];
	}
}

// ############################################################################
// Request Read Receipt
if ($data['requestread']) {
	$headers .= CRLF.'Disposition-Notification-To: "'.$hiveuser['realname'].'" <'.$data['replyto'].'>';	
																				// Read Receipt
}

// ############################################################################
// Let's get this show going
$body = '';
if ($content_type != 'text/plain') {
	$body  .= 'This is a multi-part message in MIME format.';					// For older clients
}

// ############################################################################
// If we have two versions of the message start a sub-section
if ($data['html']) {
	$body .= CRLF.CRLF.'--'.$mime_boundary.CRLF.
			'Content-Type: multipart/alternative;'.CRLF.
			'	boundary="'.$mime_boundary_inner.'"'.CRLF;
	exchange($mime_boundary, $mime_boundary_inner);
}

// ############################################################################
// Add the text to the message
if ($content_type != 'text/plain') {
	$body .= CRLF.CRLF.'--'.$mime_boundary.CRLF.
			'Content-Type: text/plain; charset="'.$lang['emailcharset'].'"'.CRLF.
			'Content-Transfer-Encoding: '.iif(SEND_WITH_BASE64, 'base64', '7bit').CRLF.CRLF.
			(SEND_WITH_BASE64 ? base64_encode($data['plainmessage']) : $data['plainmessage']);
} else {
	$body .= CRLF.CRLF.(SEND_WITH_BASE64 ? base64_encode($data['plainmessage']) : $data['plainmessage']);
}

// ############################################################################
// If we have an HTML message put it too
if ($data['html']) {
	// Add background color if one was selected
	if (!empty($data['bgcolor'])) {
		$data['htmlmessage'] = "<body bgcolor=\"$data[bgcolor]\">$data[htmlmessage]</body>";
	}
	$body .= CRLF.CRLF.'--'.$mime_boundary.CRLF.
			'Content-Type: text/html; charset="'.$lang['emailcharset'].'"'.CRLF.
			'Content-Transfer-Encoding: '.iif(SEND_WITH_BASE64, 'base64', '7bit').CRLF.CRLF.
			(SEND_WITH_BASE64 ? base64_encode($data['htmlmessage']) : $data['htmlmessage']).CRLF.CRLF.
			'--'.$mime_boundary.'--';
	exchange($mime_boundary, $mime_boundary_inner);
}

// ############################################################################
// Add the attachments
if (is_array($data['attach'])) {
	foreach ($data['attach'] as $attachdata) {
		$encdata = chunk_split(base64_encode($attachdata['data']));

		$body .= CRLF.CRLF.'--'.$mime_boundary.CRLF.
				'Content-Type: '.$attachdata['type'].';'.CRLF.
				'              name="'.$attachdata['filename'].'"'.CRLF.
				'Content-Disposition: attachment;'.CRLF.
				'                     filename="'.$attachdata['filename'].'"'.CRLF.
				'Content-Transfer-Encoding: base64'.CRLF.CRLF.
				$encdata;
	}
}

// ############################################################################
// Finish the text with last boundary
if ($content_type != 'text/plain') {
	$body .= CRLF.'--'.$mime_boundary.'--'.CRLF;
}

// ############################################################################
// Send it!
$success = smtp_mail($to, $subject, $body, $headers, true, $smtp_info);
if ($success !== true) { // Something went wrong
	log_event(EVENT_WARNING, 502, array('from' => $hiveuser['username'], 'to' => $to, 'smtp_errors' => '- '.@implode('<br />- ', $_smtp_connection->errors)));
	$goback = stripslashes($goback);
	$errortemplate = 'error_couldntsend';
	break;
} else {
	log_event(EVENT_NOTICE, 501, array('from' => $hiveuser['username'], 'to' => $to, 'ip' => IPADDRESS));
	$DB_site->query("
		UPDATE hive_user
		SET lastsent = ".TIMENOW."
		WHERE userid = $hiveuser[userid]
	");
}

// ############################################################################
// Save a copy if needed
if ($data['savecopy']) {
	// Our special subject
	encode_subject($subject);

	// Add the To: line
	if (!preg_match("#\r?\nTo:#i", $headers)) {
		$headers = str_replace('From: "'.$hiveuser['realname'].
			'" <'.$hiveuser['fromemail'].'>'.CRLF, 'From: "'.$hiveuser['realname'].
			'" <'.$hiveuser['fromemail'].'>'.CRLF.'To: '.$to.CRLF, $headers);
	}

	// Add the BCC: line if we had one and lost it somehow
	if (!empty($bcc) and !preg_match("#\r?\nBcc: #i", $headers)) {
		$headers = str_replace('MIME-Version: 1.0', 'Bcc: '.$bcc.CRLF.'MIME-Version: 1.0', $headers);
	}

	// In case we don't use SMTP
	if (empty($smtp_recipients)) {
		$smtp_recipients = implode(' ', array_unique(extract_email("$to $cc $bcc", true)));
	}

	$status = MAIL_READ + MAIL_OUTGOING + iif($data['requestread'], MAIL_SENTRECEIPT, 0);

	$dirname = get_dirname();
	$filename = make_filename($dirname);
	$DB_site->query("
		INSERT INTO hive_message
		(messageid, userid, folderid, dateline, email, name, subject, message, recipients, attach, status, emailid, source, priority, size)
		VALUES
		(NULL, $hiveuser[userid], -2, ".TIMENOW.", '".addslashes($hiveuser['fromemail'])."', '".addslashes($hiveuser['realname'])."', '".addslashes($subject)."', '".addslashes($data['plainmessage'])."', '".addslashes($smtp_recipients)."', ".iif(is_array($data['attach']), sizeof($data['attach']), 0).", $status, '', '".(getop('flat_use') ? ($dirname.'/'.$filename) : addslashes($headers."\n\n".$body))."', $data[priority], ".strlen($headers."\n\n".$body).")
	");
	storeinfile($dirname, $filename, $headers, $body);
}

// ############################################################################
// Mark as replied or forwarded
if (!empty($data['special'])) {
	$messageid = intval(substr($data['special'], 3));
	if ($data['deleteorig']) {
		$DB_site->query("
			DELETE FROM hive_message
			WHERE userid = $hiveuser[userid] AND messageid = $messageid
		");
	} else {
		$which = substr($data['special'], 0, 2);
		$bit = iif($which == 're', MAIL_REPLIED, MAIL_FORWARDED);
		$message = getinfo('message', $messageid);
		if (!($message['status'] & $bit)) {
			$DB_site->query("
				UPDATE hive_message
				SET status = status + $bit
				WHERE messageid = $messageid
			");
		}
	}
}

// ############################################################################
// Clear temp mail
$DB_site->query("
	DELETE FROM hive_draft
	WHERE draftid = ".intval($draftid)."
	AND userid = $hiveuser[userid]
");
rebuild_drafts_cache();

// ############################################################################
// Add recipients to address book
$insertvalues = '';
$addedalladdresses = true;
if ($data['addtobook']) {
	if ($hiveuser['maxcontacts'] > 0) {
		$contacts = $DB_site->query("SELECT contactid FROM hive_contact WHERE userid = $hiveuser[userid]");
		$numcontacts = $DB_site->num_rows($contacts);
	}
	foreach ($allRecipients as $recip) {
		if (!$DB_site->query_first("SELECT contactid FROM hive_contact WHERE userid IN (0, $hiveuser[userid]) AND email = '".addslashes($recip)."'")) {
			if ($hiveuser['maxcontacts'] == 0 or ($hiveuser['maxcontacts'] > 0 and ++$numcontacts <= $hiveuser['maxcontacts'])) {
				$insertvalues .= ",(NULL, $hiveuser[userid], '".addslashes($recip)."', '".addslashes($recip)."', -13, 'a:0:{}', 'a:0:{}', 'a:0:{}', 'a:0:{}')";
			} elseif ($hiveuser['maxcontacts']) {
				$addedalladdresses = false;
			}
		}
	}
	$insertvalues = substr($insertvalues, 1); // first comma

	// Add 'em
	if (!empty($insertvalues)) {
		$DB_site->query("
			INSERT INTO hive_contact (contactid, userid, email, name, timezone, emailinfo, nameinfo, addressinfo, phoneinfo) VALUES
			$insertvalues
		");
	}
}

} while(false);

// ############################################################################
// Use Javascript to return either the new URL or the error message
if (!empty($errortemplate)) {
	eval(makeeval('returnerror', $errortemplate));
} else {
	$returnerror = '';
}

?><script language="JavaScript" type="text/javascript">
<!--
window.parent.doneSend('<?php echo addslashes($returnerror); ?>', <?php echo ((int) $addedalladdresses); ?>);
//-->
</script><?php

?>