<?
if(VALID_DOCUMENT != 1) die('what?');
$text = get_var('text');
$mailbox = get_var('mailbox');
$mailkey = get_var('mailkey');
if(!isset($action) || $action == ''){
$action = 'compose';
}
if($action == 'compose'){
$to = get_var('to');
$cc = get_var('cc');
$bcc = get_var('bcc');
$subject = get_var('subject');
$text = get_var('text');
$init_html = false;
// special case for failed mails
if(isset($_SESSION['failed_mail_params'])){
$to = $_SESSION['failed_mail_params']['to'];
$cc = $_SESSION['failed_mail_params']['cc'];
$bcc = $_SESSION['failed_mail_params']['bcc'];
$subject = $_SESSION['failed_mail_params']['subject'];
$text = $_SESSION['failed_mail_params']['text'];
if($_SESSION['failed_mail_params']['mime-type'] == 'text/html')
$init_html = true;
unset($_SESSION['failed_mail_params']);
}
else{
$_SESSION['last_mailbox'] = $_SESSION['last_mailbox'] = '';
if(!$GLOBALS['MOBILE'])
$text .= "\n\n".$_SESSION['preferences']->getSignature();
}
include(TMPLPATH.'compose_form.php');
}
else if($action == 'compose-spell-check'){
$spell = new SpellChecker;
$old_text = get_var('text');
$spell = $spell->check_text(get_var('text'));
include(TMPLPATH."spell_check.php");
}
else if($action == 'compose-execute-spell'){
$res = '';
$spell = new SpellChecker;
$checked = $spell->replace_checked(get_var('old_text'),
$_REQUEST['words'],$_REQUEST['from'],$_REQUEST['length']);
$for_js = js_string($checked);
print '
<script type="text/javascript">
opener.document.getElementById("raw_text").value = '.$for_js.';
window.close();
</script>
';
}
else if($action == 'compose-reply' || $action == 'compose-reply-all' ||
$action == 'compose-forward' || $action == 'compose-continue'){
$mailbox = get_var('mailbox');
$mailkey = get_var('mailkey');
if($mailbox != '' && $mailkey != ''){
$mail = Cache::getReadMail($mailbox,$mailkey);
}
else
$mail = false;
if($mail){
if($action == 'compose-reply' || $action == 'compose-reply-all'){
$_SESSION['last_mailbox'] = $mailbox;
$_SESSION['last_mailkey'] = $mailkey;
if($mail->header->from_personal)
$to = $mail->header->from_personal.' <'.$mail->header->from_mail_address.'>';
else
$to = $mail->header->from_mail_address;
// for all in cc
if($action == 'compose-reply-all'){
if($mail->ifnntp)
$to = $mail->header->newsgroups;
else{
$cc = '';
if($mail->header->_header['toaddress'])
$cc .= $mail->header->_header['toaddress'].',';
if($mail->header->_header['ccaddress'])
$cc .= $mail->header->_header['ccaddress'];
$cc_arr = array();
foreach(explode(',',$cc) as $a){
$a = trim($a);
if($a !== '' &&
strpos($a,get_current_user_email())===false &&
strpos($a,$mail->header->from_mail_address)=== false)
$cc_arr[] = $a;
}
$cc = implode(', ',$cc_arr);
}
}
if(preg_match('/^Re:/i',$mail->header->subject))
$subject = $mail->header->subject;
else
$subject = 'Re: '.$mail->header->subject;
$reply_message_id = $mail->header->_header['message-id'];
}
else if($action == 'compose-forward'){
$_SESSION['last_mailbox'] = $mailbox;
$_SESSION['last_mailkey'] = $mailkey;
if(eregi('^Fwd:',$mail->header->subject))
$subject = $mail->header->subject;
else
$subject = 'Fwd: '.$mail->header->subject;
}
else if($action == 'compose-continue'){
$_SESSION['last_mailbox'] = $_SESSION['last_mailbox'] = '';
$subject = $mail->header->subject;
$to = $mail->header->_header['toaddress'];
$cc = $mail->header->_header['ccaddress'];
$bcc = $mail->header->_header['bccaddress'];
}
// construct message text
$init_html = false;
$init_text = '';
// final text for compose textarea
$text = '';
// choose text and html mode
// first part html
if(isset($mail->parts[0]) &&
$mail->parts[0]->if_printable() &&
$mail->parts[0]->mime_type === 'text/html'){
$init_text = $mail->parts[0]->get_safe_body();
$init_html = true;
}
// second part html
else if(isset($mail->parts[1]) &&
$mail->parts[1]->if_printable() &&
$mail->parts[1]->mime_type == 'text/html'){
$init_text = $mail->parts[1]->get_safe_body();
$init_html = true;
}
// take first if printable
else if(isset($mail->parts[0]) && $mail->parts[0]->if_printable()){
$init_text = $mail->parts[0]->get_body();
}
if($action == 'compose-reply' || $action == 'compose-reply-all'){
$reply_header = $_SESSION['preferences']->getReplyHeader();
if($init_html){
$reply_header = str_replace('%d','<i>'.date('D d M G:i Y',$mail->header->_header['date']).'</i>',$reply_header);
if($mail->header->from_personal != '')
$reply_header = str_replace('%a','<b>'.trim($mail->header->from_personal).'</b>',$reply_header);
else
$reply_header = str_replace('%a','<b>'.$mail->header->from_mail_address.'</b>',$reply_header);
$reply_header = str_replace('%s','<u>'.$mail->header->subject_print(true).'</u>',$reply_header);
$text = '<br/><div style="font-size:85%;color:#555555;">'.nl2br($reply_header).'</div>'.
'<blockquote style="'.$GLOBALS['QUOTE_STYLE'].'">'.$init_text.'</blockquote>'.
'<pre style="font-size:85%;">'.h($_SESSION['preferences']->getSignature()).'</pre>';
}
else{
$reply_header = str_replace('%d',date('D d M G:i Y',$mail->header->_header['date']),$reply_header);
if($mail->header->from_personal != '')
$reply_header = str_replace('%a',trim($mail->header->from_personal),$reply_header);
else
$reply_header = str_replace('%a',$mail->header->from_mail_address,$reply_header);
$reply_header = str_replace('%s',$mail->header->subject_print(true),$reply_header);
$text = "\n\n".$reply_header."\n";
$lines = explode("\n",$init_text);
// add reply char
foreach($lines as $line){
$text .= "> $line\n";
}
$text .= "\n".$_SESSION['preferences']->getSignature();
}
}
else if($action == 'compose-forward'){
// always attach mail parts for mobile
if($GLOBALS['MOBILE']){
$attachmailbox = $mailbox;
$attachmailkey = $mailkey;
}
else if($init_html){
$text = '<br/><pre style="font-size:85%;">'.h($_SESSION['preferences']->getSignature()).'</pre>';
$text .= '<div style="font-size:85%;color:#555555;">';
$text .= '-- Forwarded message --<br/>';
$text .= '<b>Date:</b> '.$mail->header->date_print(true)."<br/>";
$text .= '<b>From:</b> '.$mail->header->from_print(true,false)."<br/>";
if($mail->header->to_count > 0){
$tmp='';
for($i=0;$i<$mail->header->to_count;$i++)
$tmp .= $mail->header->to_print($i,true,false).", ";
$text .= '<b>To:</b> '.$tmp."<br/>";
}
if($mail->header->cc_count > 0){
$tmp='';
for($i=0;$i<$mail->header->cc_count;$i++)
$tmp .= $mail->header->cc_print($i,true,false).", ";
$text .= '<b>Cc:</b> '.$tmp."<br/>";
}
$text .= '<b>Subject:</b> '.$mail->header->subject."<br/><br/>";
$text .= '</div>';
$text .= $init_text;
}
else{
$text = "\n\n".$_SESSION['preferences']->getSignature()."\n";
$text .= "\n---------- Forwarded message ----------\n";
$text .= 'Date: '.$mail->header->date_print(true)."\n";
$text .= 'From: '.htmlentitydecode($mail->header->from_print(true,false))."\n";
if($mail->header->to_count > 0){
$tmp='';
for($i=0;$i<$mail->header->to_count;$i++){
$tmp .= htmlentitydecode($mail->header->to_print($i,true,false));
$tmp .= ($i<$mail->header->to_count-1?"\n ":'');
}
$text .= 'To: '.$tmp."\n";
}
if($mail->header->cc_count > 0){
$tmp='';
for($i=0;$i<$mail->header->cc_count;$i++){
$tmp .= htmlentitydecode($mail->header->cc_print($i,true,false));
$tmp .= ($i<$mail->header->cc_count-1?"\n ":'');
}
$text .= 'Cc: '.$tmp."\n";
}
$text .= 'Subject: '.$mail->header->subject."\n\n";
$text .= $init_text;
}
if($mail->parts_count > 1 || $mail->parts[0]->if_attachment()){
$attachmailbox = $mailbox;
$attachmailkey = $mailkey;
}
}
else if($action == 'compose-continue'){
$text = $init_text;
if($GLOBALS['MOBILE'])
$text = html2text($text,20);
}
// textarea empty for mobile
if($GLOBALS['MOBILE'] && $action != 'compose-continue'){
$text = '';
}
include(TMPLPATH.'compose_form.php');
}
}
else if($action == 'compose-postpone'){
// headers
$envelope = create_envelope(get_var('subject'),
get_var('to'),get_var('cc'),get_var('bcc'));
// mime type
$subtype = (get_var('mime-type') == 'text/html')?'html':'plain';
// text
$text = get_var('text');
// Charset definition
$charset = $GLOBALS['DEFAULT_CHARSET'];
if($charset && preg_match('/[\x7f-\xff]/',$envelope['subject'])) // charset encoded subject
$envelope['subject'] = '=?'.$charset.'?B?'.base64_encode($envelope['subject']).'?=';
$part = array();
$part['type']='text/'.$subtype;
$part['charset'] = $charset;
$part['description']=$envelope['subject'];
$part['contents.data']="$text\n";
$body[] = $part;
// msg
$msg = build_mail($envelope,$body,true);
// move to draft
$draft = $_SESSION['mboxes']->find($_SESSION['preferences']->getDraftMailbox());
if(!is_object($draft)){
$_SESSION['mboxes']->create($_SESSION['preferences']->getDraftMailbox());
$draft = $_SESSION['mboxes']->find($_SESSION['preferences']->getDraftMailbox());
}
$msg = 'Date:'.date('r')."\n".$msg;
$draft->append($msg);
Notice::set('Mail postponed');
if(!$_SESSION['preferences']->isShowSendInfo() && $_SESSION['last_mailbox'] && $_SESSION['last_mailkey']){
$mailbox = $_SESSION['last_mailbox'];
$mailkey = $_SESSION['last_mailkey'];
$_SESSION['last_mailbox'] = $_SESSION['last_mailbox'] = '';
// show email
location_header($_SERVER['PHP_SELF'].'?action=read-mail&mailbox='.$mailbox.'&mailkey='.$mailkey);
}
else
location_header($_SERVER['PHP_SELF'].'?action=notice');
}
else if($action == 'compose-send'){
$standalone = $_REQUEST['standalone'] == 'true';
$encrypt = $_REQUEST['encrypt']=='true';
$sign = $_REQUEST['sign']=='true';
$res = '';
// headers
$envelope = create_envelope(get_var('subject'),
get_var('to'),
get_var('cc'),
get_var('bcc'));
if(get_var('reply-message-id')!=''){
$envelope['custom_headers'][] = 'In-Reply-To: <'.get_var('reply-message-id').'>';
}
// add receipt notification
if($_REQUEST['notify']=='true' && $_SESSION['preferences']->getUseDispositionNotification()){
$envelope['custom_headers'][] = 'Disposition-Notification-To: '.$envelope['from'];
}
// add priority
if($_REQUEST['priority']!='' && isset($GLOBALS['EMAIL_PRIORITIES'][$_REQUEST['priority']])){
$priority = $GLOBALS['EMAIL_PRIORITIES'][$_REQUEST['priority']];
foreach($priority['headers'] as $h){
$envelope['custom_headers'][] = $h;
}
}
// mime type
$subtype = (get_var('mime-type') == 'text/html')?'html':'plain';
// text
$text = get_var('text');
// Charset definition
$charset = $GLOBALS['DEFAULT_CHARSET'];
// body
$body = array();
// attachments
$attachments = array();
$count = get_var('attachments_num');
for($i=0;$i<$count;$i++){
$attach = new Attachment('attachment',$i);
if($attach->if_uploaded)
$attachments[] = $attach;
}
reset($attachments);
$attachmailbox = get_var('attachmailbox');
$attachmailkey = get_var('attachmailkey');
$attachment_parts = array();
if(($attachmailbox != "" && $attachmailkey != "") || count($attachments) > 0){
// forwards
if($attachmailbox != "" && $attachmailkey != ""){
$mail = Cache::getReadMail($attachmailbox,$attachmailkey);
$attachparts = get_var('attach-parts');
foreach($attachparts as $i){
$p = &$mail->parts[$i];
$part = array();
$part['type']=$p->mime_type;
$part['encoding']=ENCBINARY;
if($p->if_attachment()){
$part['description']=$p->filename;
$part['disposition'] = 'attachment; filename="'.$p->filename.'"';
}
else{
$part['charset']=$p->charset;
}
$part['contents.data']=$p->body;
$attachment_parts[] = $part;
$res .= "<b>".$p->filename."</b> forwarded to attachment\n";
}
}
// Attachments
reset($attachments);
foreach($attachments as $attach){
$part = array();
$part['type']=$attach->type;
$part['encoding']=ENCBINARY;
$part['description']=$attach->name;
$part['disposition'] = 'attachment; filename="'.$attach->name.'"';
$part['contents.data']=$attach->data;
$attachment_parts[] = $part;
$res .= "Added <b>".$attach->name."</b> [".bytes2string(strlen($attach->data))."] to attachment\n";
}
}
$has_attachments = array_not_empty($attachment_parts);
// message text
if($subtype == 'html'){
$alternate_body = array();
// text part
$part = array();
$part['type']='text/plain';
$part['charset'] = $charset;
$part['contents.data']=html2text($text);
$alternate_body[] = $part;
// html part
$part = array();
$part['type']='text/'.$subtype;
$part['charset'] = $charset;
$part['contents.data']=$text."\n";
$part['multipart']=true;
$alternate_body[] = $part;
// composite alternative multipart
if(array_not_empty($attachment_parts)){
$body[] = array('type'=>'multipart/alternative',
'body'=>$alternate_body,
'composite'=>true);
}
else{
$body = $alternate_body;
}
}
else{
$part = array();
$part['type']='text/'.$subtype;
$part['charset'] = $charset;
$part['contents.data']=$text."\n";
$body[] = $part;
}
if(array_not_empty($attachment_parts)){
$body = array_merge($body,$attachment_parts);
}
// encryption/signature body changes
if(($sign || $encrypt) &&
$_SESSION['preferences']->getUseGnuPG()){
require_once('simple_gpg.php');
$gpg = new SimpleGPG();
if($encrypt){
// hook for sign encrypted data
if($sign){
// try to load
if(!$gpg->loadPassphraseFromRequest()){
Error::set("Wrong passphrase.");
$_SESSION['failed_mail_params'] = $_REQUEST;
location_header($_SERVER['PHP_SELF'].'?action=compose');
}
$gpg->sign_encrypted = true;
}
$ret = $gpg->makeEncryptedEnvelopeBody($envelope,$body);
if($ret['returnval'] === 0){
if(isset($ret['info'])) Notice::set($ret['info']);
$envelope = $ret['envelope'];
$body = $ret['body'];
}
else{
Error::set("Encrypted mail sending failed.\n".$ret['error']);
$_SESSION['failed_mail_params'] = $_REQUEST;
location_header($_SERVER['PHP_SELF'].'?action=compose');
}
}
else if($sign){
// try to load
if(!$gpg->loadPassphraseFromRequest()){
Error::set("Wrong passphrase.");
$_SESSION['failed_mail_params'] = $_REQUEST;
location_header($_SERVER['PHP_SELF'].'?action=compose');
}
$ret = $gpg->makeSignedEnvelopeBody($envelope,$body);
if($ret['returnval'] == 0){
if(isset($ret['info'])) Notice::set($ret['info']);
$envelope = $ret['envelope'];
$body = $ret['body'];
}
else{
Error::set("Signed mail sending failed.\n".$ret['error']);
$_SESSION['failed_mail_params'] = $_REQUEST;
location_header($_SERVER['PHP_SELF'].'?action=compose');
}
}
}
// build message
$msg = build_mail($envelope,$body);
// send
$sender = new SMTPSender;
// Sending for all recipients from to,cc and bcc.
$send_out = $sender->send($GLOBALS['MAIL_USER_NAME'].'@'.MAILSUFFIX,
$envelope['all-recipients'],$msg);
if($sender->if_successful){
if($_REQUEST['remember-addresses']=='true' && $_SESSION['preferences']->getRememberAddresses()){
$_SESSION['addressbook']->update($envelope['all-recipients-full']);
}
if($_SESSION['preferences']->isFccEnabled()){
$fcc = $_SESSION['mboxes']->find($_SESSION['preferences']->getFccMailbox());
if(!is_object($fcc)){
Notice::set($_SESSION['mboxes']->create($_SESSION['preferences']->getFccMailbox())."\n");
$fcc = $_SESSION['mboxes']->find($_SESSION['preferences']->getFccMailbox());
}
if(is_object($fcc)){
// $envelope = create_envelope(get_var('subject'),
// get_var('to'),get_var('cc'),get_var('bcc'));
// body for sent-mail
$fcc_body = array();
$fcc_body[] = array('type'=>'text/'.$subtype,
'charset'=>$charset,
'description'=>$envelope['subject'],
'contents.data'=>$text."\n");
if(count($attachments) > 0){
$attach_text = "Attached files :\n";
$index=1;
foreach($attachments as $attach){
$attach_text .= ' '.($index++).''.$attach->name.' ['.bytes2string(strlen($attach->data))."]\n";
}
$fcc_body[] = array('type'=>'text/plain',
'description'=>'Attachments list and description',
'contents.data'=>$attach_text);
}
// remove all custom headers
unset($envelope['custom_headers']);
// sent msg
$sent_msg = '';
// try to encrypt
if($encrypt && $_SESSION['preferences']->getUseGnuPG()){
$gpg = new SimpleGPG();
$ret = $gpg->makeEncryptedEnvelopeBody($envelope,$fcc_body,false);
if($ret['returnval'] == 0){
$sent_msg = build_mail($ret['envelope'],$ret['body'],true);
Notice::set("Sent mail copied encrypted.\n");
}
}
// if not encrypted, do it as usual
if($sent_msg === ''){
$envelope['encrypted'] = false;
$sent_msg = build_mail($envelope,$fcc_body,true);
Notice::set("Sent mail copied.\n");
}
// charset adding
// if(eregi("Content-Type:[ ]?text/plain([^\n]*)",$sent_msg,$match))
// $sent_msg = str_replace($match[1],"; charset=".$charset,$sent_msg);
// append msg with date header
$sent_msg = 'Date: '.date('r')."\n".$sent_msg;
// appending to fcc folder
$fcc->append($sent_msg);
}
}
if($_SESSION['last_mailbox'] && $_SESSION['last_mailkey']){
$mbox = $_SESSION['mboxes']->find($_SESSION['last_mailbox']);
$mp = $mbox->getMP();
$mp->set_flag($_SESSION['last_mailbox'],$_SESSION['last_mailkey'],ANSWERED_FLAG);
}
if(!$_SESSION['preferences']->isShowSendInfo() &&
$_SESSION['last_mailbox'] &&
$_SESSION['last_mailkey']){
$mailbox = $_SESSION['last_mailbox'];
$mailkey = $_SESSION['last_mailkey'];
$_SESSION['last_mailbox'] = $_SESSION['last_mailbox'] = '';
Notice::set(wordwrap('Mail to '.implode(", ",$envelope['all-recipients'])."\nwas sent successfully.",45));
// show email
if($standalone)
location_header($_SERVER['PHP_SELF'].'?action=preview-mail&mailbox='.$mailbox.'&mailkey='.$mailkey);
else
location_header($_SERVER['PHP_SELF'].'?action=read-mail&mailbox='.$mailbox.'&mailkey='.$mailkey);
}
else{
$res .= "<b>Mail has been sent successfully:</b>\n".htmlspecialchars($send_out);
}
}
else{
Error::set("Error when mail sending:</b>\n".htmlspecialchars($send_out));
$_SESSION['failed_mail_params'] = $_REQUEST;
location_header($_SERVER['PHP_SELF'].'?action=compose');
}
if($_SESSION['last_mailbox'] && $_SESSION['last_mailkey']){
$lmailbox = $_SESSION['last_mailbox'];
$lmailkey = $_SESSION['last_mailkey'];
$_SESSION['last_mailbox'] = $_SESSION['last_mailbox'] = '';
if($standalone)
$res .=
'<a href="'.$_SERVER['PHP_SELF'].'?action=preview-mail&mailbox='.$lmailbox.'&mailkey='.$lmailkey.'">'.
'back to last visited mail</a>';
else
$res .=
'<a href="'.$_SERVER['PHP_SELF'].'?action=read-mail&mailbox='.$lmailbox.'&mailkey='.$lmailkey.'">'.
'back to last visited mail</a><br/>'.
'<a href="'.$_SERVER['PHP_SELF'].'?action=show-folder&mailbox='.$lmailbox.'">'.
'back to '.$lmailbox.'</a><br/>';
}
// message debug printing
// if(is_test_user()){
// print "<pre>\n". htmlspecialchars($msg)."</pre>\n";
// exit;
// }
Notice::set($res);
if($standalone)
location_header($_SERVER['PHP_SELF'].'?action=notice&standalone=true');
else
location_header($_SERVER['PHP_SELF'].'?action=notice');
}
// AJAX action for notification about emial reading
else if($action == 'compose-notify'){
$mailbox = get_var('mailbox');
$mailkey = get_var('mailkey');
$mail = false;
if($mailbox != '' && $mailkey != ''){
$mail = Cache::getReadMail($mailbox,$mailkey);
}
if(is_object($mail)){
$envelope = create_envelope('Read: '.$mail->header->subject,
$mail->header->get_from_address());
// body
$body = array(array('type'=>'text/plain',
'charset'=>$GLOBALS['DEFAULT_CHARSET'],
'contents.data'=>
"This is receipt for the mail you sent to\n".
$envelope['from']." at ".date("d/m/Y H:i",$mail->header->date)."\n\n".
"This receipt verifies that the message has been displayed on the recipient's ".
"computer at ".date('d/m/Y H:i')."\n"),
array('type'=>'message/disposition-notification',
'contents.data'=>
"Final-Recipient: rfc822;".$mail->header->from_mail_address."\n".
"Original-Message-ID: <".$mail->header->message_id.">\n".
"Disposition: manual-action/MDN-sent-manually; displayed\n".
"\noriginal message not included.\n"));
// message
$msg = build_mail($envelope,$body);
$sender = new SMTPSender;
// Sending for all recipients from to,cc and bcc.
$send_out = $sender->send($GLOBALS['MAIL_USER_NAME'].'@'.MAILSUFFIX,
$envelope['all-recipients'],
$msg);
if($sender->if_successful){
print 'Receipt notification sended successfully';
}
else{
print 'Error with mail sending:<pre>'.htmlspecialchars($send_out).'</pre>';
}
}
else
print '<i>'.htmlspecialchars($email).'</i> doesn\'t look like email address.';
}
?>