';
if ($row['mail_from'] == $user) {
$sql_update = $dbh->prepare('UPDATE messages SET mail_viewed=? WHERE msgid=?');
$sql_update->execute(array(
'true',
$row['msgid']
));
}
if ($row['rcpt_to'] == $user) {
$sql_update = $dbh->prepare('UPDATE messages SET rcpt_viewed=? WHERE msgid=?');
$sql_update->execute(array(
'true',
$row['msgid']
));
}
}
$dbh = null;
}
if (isset($_POST['sendMessage'])) {
if (isset($_POST['to']) && $_POST['to'] != '' && isset($_POST['from']) && $_POST['from'] != '' && isset($_POST['message']) && $_POST['message'] != '') {
if (($to = get_config_value('aliases.conf', strtolower($_POST['to']))) == false) {
$to = strtolower($_POST['to']);
}
$userlist = scandir($config_dir . '/users/');
$found = 0;
foreach ($userlist as $user) {
if (trim($to) == trim($user)) {
$found = 1;
}
}
// Check if target is remote. If user enters @ our own domain, strip it (it's local)
$remote_target = 0;
if (strpos($to, '@') !== false) {
$info = preg_split('/@/', $to, 2);
if ($info[1] == $rslight_gpg['domain_name']) { // domain is our domain
$to = $info[0];
foreach ($userlist as $user) {
if (($to = get_config_value('aliases.conf', strtolower($info[0]))) == false) {
$to = strtolower($info[0]);
}
if (trim($to) == trim($user)) {
$found = 1;
}
}
} else { // domain is remote
$found = 1;
$remote_target = 1;
}
}
if ($found == 0) {
echo 'User not found: ' . $to;
} else {
$database = $spooldir . '/mail.db3';
$dbh = mail_db_open($database);
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$date = time();
$message = $_POST['message'];
$msgid = '<' . md5(strtolower($to) . strtolower($from) . strtolower($subject) . strtolower($message)) . '>';
$sql = 'INSERT OR IGNORE INTO messages(msgid, mail_from, rcpt_to, rcpt_target, date, subject, message, from_hide, to_hide, mail_viewed, rcpt_viewed) VALUES(?,?,?,?,?,?,?,?,?,?,?)';
$stmt = $dbh->prepare($sql);
// For possible future use ($target is currently unused)
$target = "local";
$mail_viewed = "true";
$rcpt_viewed = null;
// $remote_target is handled here
if ($q = $stmt->execute([
$msgid,
$from,
$to,
$target,
$date,
$subject,
$message,
null,
null,
$mail_viewed,
$rcpt_viewed
])) {
if ($remote_target == 1) {
$remote_result = send_external_mail($from, $to, $date, $subject, $message);
if ($remote_result == true) {
$return_val = "Message sent.";
} else {
$return_val = "Failed to Send. No Key for Destination";
}
}
// Send internet email notification here
if (strpos('@', $to) === false) {
$user_config = unserialize(file_get_contents($config_dir . '/userconfig/' . $to . '.config'));
if ($user_config['send_mail_to_email'] == 'true') {
$email_subject = "New Mail in your Inbox from " . $from . " on " . ltrim($CONFIG['server_path'], "@");
if (get_user_config($to, 'email_verified') == 'true') {
if ($email_address = get_user_config($to, 'email')) {
$message = "\nYou have received mail from " . $from . " on " . ltrim($CONFIG['server_path'], "@") . "\n\n-----\n" . $message;
$message = rtrim($message);
$message .= "\n-----\n\nTo Reply, log into site and view Mail";
send_internet_email($email_subject, $message, $email_address);
}
}
}
}
$return_val = "Message sent.";
} else {
$return_val = "Failed to Send. Database Error";
}
// Act on return values for response to user
echo $return_val;
$dbh = null;
$user = $from;
}
}
}
if (isset($_POST['command']) && $_POST['command'] == 'Send') {
$mail_to = '';
$subject = '';
$message = '';
if (isset($_POST['id'])) {
$database = $spooldir . '/mail.db3';
$dbh = mail_db_open($database);
$query = $dbh->prepare('SELECT * FROM messages where id=:id');
$query->execute([
'id' => $_POST['id']
]);
while (($row = $query->fetch()) !== false) {
$mail_to = $row['mail_from'];
if (strpos($row['subject'], 'Re: ') !== 0) {
$subject = 'Re: ' . $row['subject'];
} else {
$subject = $row['subject'];
}
$body = explode("\n", $row['message']);
$message = $row['mail_from'] . " wrote:\n\n";
foreach ($body as $line) {
if (trim($line) !== '') {
$line = '>' . $line;
}
$message .= $line;
}
}
$dbh = null;
}
echo '
';
echo '
Send Message:
';
echo "';
echo '
';
}
view_mailbox($user);
// Show My Messages
function view_mailbox($user)
{
global $spooldir, $offset, $rslight_version;
$database = $spooldir . '/mail.db3';
$dbh = mail_db_open($database);
echo '
My Messages:
';
echo '
';
$query = $dbh->prepare('SELECT * FROM messages WHERE mail_from=:mail_from OR rcpt_to=:mail_from ORDER BY date DESC');
$query->execute([
'mail_from' => $user
]);
echo '