thomascube
2010-12-17 db1a87cd6c506f2afbd1a37c64cb56ae11120b49
program/steps/mail/compose.inc
@@ -33,9 +33,17 @@
// Since there are many ways to leave the compose page improperly, it seems necessary to clean-up an old
// compose when a "new/forward/reply/draft" is called - otherwise the old session attachments will appear
if (!is_array($_SESSION['compose']) || $_SESSION['compose']['id'] != get_input_value('_id', RCUBE_INPUT_GET))
$MESSAGE_ID = get_input_value('_id', RCUBE_INPUT_GET);
if (!is_array($_SESSION['compose']) || $_SESSION['compose']['id'] != $MESSAGE_ID)
{
  rcmail_compose_cleanup();
  // Infinite redirect prevention in case of broken session (#1487028)
  if ($MESSAGE_ID)
    raise_error(array('code' => 500, 'type' => 'php',
      'file' => __FILE__, 'line' => __LINE__,
      'message' => "Invalid session"), true, true);
  $_SESSION['compose'] = array(
    'id' => uniqid(mt_rand()),
    'param' => request2param(RCUBE_INPUT_GET),
@@ -92,12 +100,8 @@
  }
  // check if folder for saving sent messages exists and is subscribed (#1486802)
  if (($sent_folder = $_SESSION['compose']['param']['sent_mbox']) && !$IMAP->mailbox_exists($sent_folder, true)) {
    // folder may exist but isn't subscribed (#1485241)
    if (!$IMAP->mailbox_exists($sent_folder))
      $IMAP->create_mailbox($sent_folder, true);
    else
      $IMAP->subscribe($sent_folder);
  if ($sent_folder = $_SESSION['compose']['param']['sent_mbox']) {
    rcmail_check_sent_folder($sent_folder, true);
  }
  // redirect to a unique URL with all parameters stored in session
@@ -108,7 +112,8 @@
// add some labels to client
$OUTPUT->add_label('nosubject', 'nosenderwarning', 'norecipientwarning', 'nosubjectwarning', 'cancel',
    'nobodywarning', 'notsentwarning', 'notuploadedwarning', 'savingmessage', 'sendingmessage', 
    'messagesaved', 'converting', 'editorwarning', 'searching', 'uploading', 'fileuploaderror');
    'messagesaved', 'converting', 'editorwarning', 'searching', 'uploading', 'fileuploaderror',
    'autocompletechars');
// add config parameters to client script
if (!empty($CONFIG['drafts_mbox'])) {
@@ -119,6 +124,7 @@
$OUTPUT->set_env('mailbox', $IMAP->get_mailbox_name());
$OUTPUT->set_env('sig_above', $CONFIG['sig_above']);
$OUTPUT->set_env('top_posting', $CONFIG['top_posting']);
$OUTPUT->set_env('autocomplete_min_length', $CONFIG['autocomplete_min_length']);
// get reference message and set compose mode
if ($msg_uid = $_SESSION['compose']['param']['reply_uid'])
@@ -169,6 +175,13 @@
      $MESSAGE->reply_all = $_SESSION['compose']['param']['all'];
    $OUTPUT->set_env('compose_mode', 'reply');
    // Save the sent message in the same folder of the message being replied to
    if ($RCMAIL->config->get('reply_same_folder') && ($sent_folder = $_SESSION['compose']['mailbox'])
      && rcmail_check_sent_folder($sent_folder, false)
    ) {
      $_SESSION['compose']['param']['sent_mbox'] = $sent_folder;
    }
  }
  else if ($compose_mode == RCUBE_COMPOSE_DRAFT)
  {
@@ -183,8 +196,15 @@
        $_SESSION['compose']['forward_uid'] = $info['uid'];
      $_SESSION['compose']['mailbox'] = $info['folder'];
      // Save the sent message in the same folder of the message being replied to
      if ($RCMAIL->config->get('reply_same_folder') && ($sent_folder = $info['folder'])
        && rcmail_check_sent_folder($sent_folder, false)
      ) {
        $_SESSION['compose']['param']['sent_mbox'] = $sent_folder;
      }
    }
    if ($MESSAGE->headers->in_reply_to)
      $_SESSION['compose']['reply_msgid'] = '<'.$MESSAGE->headers->in_reply_to.'>';
@@ -247,20 +267,12 @@
      $param = 'replyto';
      $header = 'reply-to';
    case 'mailreplyto':
    case 'mailreply-to':
    case 'followupto':
    case 'followup-to':
      if (!$fname) {
        $fname = '_mailreplyto';
        $param = 'mailreplyto';
        $header = 'mailreply-to';
      }
    case 'mailfollowupto':
    case 'mailfollowup-to':
      if (!$fname) {
        $fname = '_mailfollowupto';
        $param = 'mailfollowupto';
        $header = 'mailfollowup-to';
        $fname = '_followupto';
        $param = 'followupto';
        $header = 'mail-followup-to';
      }
      $allow_attrib = array('id', 'class', 'style', 'size', 'tabindex');
@@ -334,10 +346,10 @@
      $fvalue = $MESSAGE->get_header('cc');
    else if ($header=='bcc' && !empty($MESSAGE->headers->bcc))
      $fvalue = $MESSAGE->get_header('bcc');
    else if ($header=='reply-to' && !empty($MESSAGE->headers->others['mail-reply-to']))
      $fvalue = $MESSAGE->get_header('mail-reply-to');
    else if ($header=='reply-to' && !empty($MESSAGE->headers->replyto))
      $fvalue = $MESSAGE->get_header('reply-to');
    else if ($header=='mail-reply-to' && !empty($MESSAGE->headers->others['mail-reply-to']))
      $fvalue = $MESSAGE->get_header('followup-to');
    else if ($header=='mail-followup-to' && !empty($MESSAGE->headers->others['mail-followup-to']))
      $fvalue = $MESSAGE->get_header('mail-followup-to');
@@ -357,7 +369,6 @@
      $fvalue .= (strlen($fvalue) ? ', ':'') . $string;
    }
  }
  if ($fname && $field_type)
  {
@@ -459,10 +470,11 @@
      // Set identity
      foreach ($user_identities as $sql_arr) {
        // set draft's identity
        if ($compose_mode == RCUBE_COMPOSE_DRAFT) {
          if (strstr($MESSAGE->headers->from, $sql_arr['email']))
        if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT) {
          if ($MESSAGE->headers->from == format_email_recipient($sql_arr['email'], $sql_arr['name'])) {
            $from_id = $sql_arr['identity_id'];
            break;
          }
        }
        // set identity if it's one of the reply-message recipients (with prio for default identity)
        else if (in_array($sql_arr['email'], $a_recipients) && (empty($from_id) || $sql_arr['standard']))
@@ -481,8 +493,9 @@
    // add signatures to client
    $OUTPUT->set_env('signatures', $a_signatures);
  }
  else
  {
  // no identities, display text input field
  else {
    $field_attrib['class'] = 'from_address';
    $input_from = new html_inputfield($field_attrib);
    $out = $input_from->show($_POST['_from']);
  }
@@ -491,48 +504,79 @@
}
function rcmail_compose_editor_mode()
{
  global $RCMAIL, $MESSAGE, $compose_mode;
  static $useHtml;
  if ($useHtml !== null)
    return $useHtml;
  $html_editor = intval($RCMAIL->config->get('htmleditor'));
  if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT) {
    $useHtml = $MESSAGE->has_html_part();
  }
  else if ($compose_mode == RCUBE_COMPOSE_REPLY) {
    $useHtml = ($html_editor == 1 || ($html_editor == 2 && $MESSAGE->has_html_part()));
  }
  else { // RCUBE_COMPOSE_FORWARD or NEW
    $useHtml = ($html_editor == 1);
  }
  return $useHtml;
}
function rcmail_prepare_message_body()
{
  global $RCMAIL, $CONFIG, $MESSAGE, $compose_mode, $LINE_LENGTH, $HTML_MODE;
  if ($CONFIG['htmleditor'] || (($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT) && $MESSAGE->has_html_part()))
    $isHtml = true;
  else
    $isHtml = false;
  $body = '';
  global $RCMAIL, $MESSAGE, $compose_mode, $LINE_LENGTH, $HTML_MODE;
  // use posted message body
  if (!empty($_POST['_message']))
  {
  if (!empty($_POST['_message'])) {
    $body = get_input_value('_message', RCUBE_INPUT_POST, true);
    $isHtml = (bool) get_input_value('_is_html', RCUBE_INPUT_POST);
  }
  else if ($_SESSION['compose']['param']['body'])
  {
  else if ($_SESSION['compose']['param']['body']) {
    $body = $_SESSION['compose']['param']['body'];
    $isHtml = false;
  }
  else if ($compose_mode)
  {
  // reply/edit/draft/forward
  else if ($compose_mode) {
    $has_html_part = $MESSAGE->has_html_part();
    if (($isHtml || $compose_mode == RCUBE_COMPOSE_DRAFT) && $has_html_part)
    {
      $body = $MESSAGE->first_html_part();
      $isHtml = true;
    $isHtml = rcmail_compose_editor_mode();
    if ($isHtml) {
      if ($has_html_part) {
        $body = $MESSAGE->first_html_part();
      }
      else {
        $body = $MESSAGE->first_text_part();
        // try to remove the signature
        if ($RCMAIL->config->get('strip_existing_sig', true))
          $body = rcmail_remove_signature($body);
        // add HTML formatting
        $body = rcmail_plain_body($body);
        if ($body)
          $body = '<pre>' . $body . '</pre>';
      }
    }
    else if ($has_html_part)
    {
      // use html part if it has been used for message (pre)viewing
      // decrease line length for quoting
      $len = $compose_mode == RCUBE_COMPOSE_REPLY ? $LINE_LENGTH-2 : $LINE_LENGTH;
      $txt = new html2text($MESSAGE->first_html_part(), false, true, $len);
      $body = $txt->get_text();
      $isHtml = false;
    }
    else
    {
      $body = $MESSAGE->first_text_part();
      $isHtml = false;
    else {
      if ($has_html_part) {
        // use html part if it has been used for message (pre)viewing
        // decrease line length for quoting
        $len = $compose_mode == RCUBE_COMPOSE_REPLY ? $LINE_LENGTH-2 : $LINE_LENGTH;
        $txt = new html2text($MESSAGE->first_html_part(), false, true, $len);
        $body = $txt->get_text();
      }
      else {
        $body = $MESSAGE->first_text_part($part);
        if ($body && $part && $part->ctype_secondary == 'plain'
            && $part->ctype_parameters['format'] == 'flowed'
        ) {
          $body = rcube_message::unfold_flowed($body);
        }
      }
    }
    // compose reply-body
@@ -544,6 +588,9 @@
    // load draft message body
    else if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT)
      $body = rcmail_create_draft_body($body, $isHtml);
  }
  else { // new message
    $isHtml = rcmail_compose_editor_mode();
  }
  $plugin = $RCMAIL->plugins->exec_hook('message_compose_body',
@@ -592,9 +639,20 @@
  $out .= $msgtype->show();
  // If desired, set this textarea to be editable by TinyMCE
  if ($isHtml) $attrib['class'] = 'mce_editor';
  $textarea = new html_textarea($attrib);
  $out .= $textarea->show($MESSAGE_BODY);
  if ($isHtml) {
    $attrib['class'] = 'mce_editor';
    $textarea = new html_textarea($attrib);
    $out .= $textarea->show($MESSAGE_BODY);
  }
  else {
    $textarea = new html_textarea($attrib);
    $out .= $textarea->show('');
    // quote plain text, inject into textarea
    $table = get_html_translation_table(HTML_SPECIALCHARS);
    $MESSAGE_BODY = strtr($MESSAGE_BODY, $table);
    $out = substr($out, 0, -11) . $MESSAGE_BODY . '</textarea>';
  }
  $out .= $form_end ? "\n$form_end" : '';
  $OUTPUT->set_env('composebody', $attrib['id']);
@@ -676,24 +734,14 @@
  // build reply prefix
  $from = array_pop($RCMAIL->imap->decode_address_list($MESSAGE->get_header('from')));
  $prefix = sprintf("On %s, %s wrote:",
    $MESSAGE->headers->date, $from['name'] ? $from['name'] : idn_to_utf8($from['email']));
    $MESSAGE->headers->date, $from['name'] ? $from['name'] : idn_to_utf8($from['mailto']));
  if (!$bodyIsHtml) {
    $body = preg_replace('/\r?\n/', "\n", $body);
    // try to remove the signature
    if ($RCMAIL->config->get('strip_existing_sig', true)) {
      $len = strlen($body);
      while (($sp = strrpos($body, "-- \n", $sp ? -$len+$sp-1 : 0)) !== false) {
        if ($sp == 0 || $body[$sp-1] == "\n") {
          // do not touch blocks with more that X lines
          if (substr_count($body, "\n", $sp) < $RCMAIL->config->get('sig_max_lines', 15)) {
            $body = substr($body, 0, max(0, $sp-1));
          }
          break;
        }
      }
    }
    if ($RCMAIL->config->get('strip_existing_sig', true))
      $body = rcmail_remove_signature($body);
    // soft-wrap and quote message text
    $body = rcmail_wrap_and_quote(rtrim($body, "\n"), $LINE_LENGTH);
@@ -807,11 +855,32 @@
  
  return $body;
}
function rcmail_remove_signature($body)
{
  global $RCMAIL;
  $len = strlen($body);
  $sig_max_lines = $RCMAIL->config->get('sig_max_lines', 15);
  while (($sp = strrpos($body, "-- \n", $sp ? -$len+$sp-1 : 0)) !== false) {
    if ($sp == 0 || $body[$sp-1] == "\n") {
      // do not touch blocks with more that X lines
      if (substr_count($body, "\n", $sp) < $sig_max_lines) {
        $body = substr($body, 0, max(0, $sp-1));
      }
      break;
    }
  }
  return $body;
}
function rcmail_write_compose_attachments(&$message, $bodyIsHtml)
{
  global $OUTPUT;
  global $RCMAIL;
  $cid_map = $messages = array();
  foreach ((array)$message->mime_parts as $pid => $part)
@@ -835,7 +904,7 @@
      if (!$skip && ($attachment = rcmail_save_attachment($message, $pid))) {
        $_SESSION['compose']['attachments'][$attachment['id']] = $attachment;
        if ($bodyIsHtml && ($part->content_id || $part->content_location)) {
          $url = $OUTPUT->app->comm_path.'&_action=display-attachment&_file=rcmfile'.$attachment['id'];
          $url = $RCMAIL->comm_path.'&_action=display-attachment&_file=rcmfile'.$attachment['id'];
          if ($part->content_id)
            $cid_map['cid:'.$part->content_id] = $url;
          else
@@ -853,14 +922,14 @@
function rcmail_write_inline_attachments(&$message)
{
  global $OUTPUT;
  global $RCMAIL;
  $cid_map = array();
  foreach ((array)$message->mime_parts as $pid => $part) {
    if (($part->content_id || $part->content_location) && $part->filename) {
      if ($attachment = rcmail_save_attachment($message, $pid)) {
        $_SESSION['compose']['attachments'][$attachment['id']] = $attachment;
        $url = $OUTPUT->app->comm_path.'&_action=display-attachment&_file=rcmfile'.$attachment['id'];
        $url = $RCMAIL->comm_path.'&_action=display-attachment&_file=rcmfile'.$attachment['id'];
        if ($part->content_id)
          $cid_map['cid:'.$part->content_id] = $url;
        else
@@ -1146,16 +1215,34 @@
}
function rcmail_dsn_checkbox($attrib)
{
  global $RCMAIL;
  list($form_start, $form_end) = get_form_tags($attrib);
  unset($attrib['form']);
  if (!isset($attrib['id']))
    $attrib['id'] = 'dsn';
  $attrib['name'] = '_dsn';
  $attrib['value'] = '1';
  $checkbox = new html_checkbox($attrib);
  $out = $form_start ? "$form_start\n" : '';
  $out .= $checkbox->show($RCMAIL->config->get('dsn_default'));
  $out .= $form_end ? "\n$form_end" : '';
  return $out;
}
function rcmail_editor_selector($attrib)
{
  global $CONFIG, $MESSAGE, $compose_mode;
  // determine whether HTML or plain text should be checked
  if ($compose_mode)
    $useHtml = (($CONFIG['htmleditor'] || $compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT)
   && $MESSAGE->has_html_part());
  else
    $useHtml = $CONFIG['htmleditor'] ? true : false;
  $useHtml = rcmail_compose_editor_mode();
  if (empty($attrib['editorid']))
    $attrib['editorid'] = 'rcmComposeBody';
@@ -1172,8 +1259,7 @@
  return $select->show($useHtml ? 'html' : 'plain');
  foreach ($choices as $value => $text)
  {
  foreach ($choices as $value => $text) {
    $attrib['id'] = '_' . $value;
    $attrib['value'] = $value;
    $selector .= $radio->show($chosenvalue, $attrib) . html::label($attrib['id'], Q(rcube_label($text)));
@@ -1191,6 +1277,26 @@
}
function rcmail_check_sent_folder($folder, $create=false)
{
  global $IMAP;
  if ($IMAP->mailbox_exists($folder, true)) {
    return true;
  }
  // folder may exist but isn't subscribed (#1485241)
  if ($create) {
    if (!$IMAP->mailbox_exists($folder))
      return $IMAP->create_mailbox($folder, true);
    else
      return $IMAP->subscribe($folder);
  }
  return false;
}
function get_form_tags($attrib)
{
  global $RCMAIL, $MESSAGE_FORM;
@@ -1204,13 +1310,13 @@
    $form_start = empty($attrib['form']) ? $RCMAIL->output->form_tag(array('name' => "form", 'method' => "post")) : '';
    $form_start .= $hiddenfields->show();
  }
  $form_end = ($MESSAGE_FORM && !strlen($attrib['form'])) ? '</form>' : '';
  $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
  if (!$MESSAGE_FORM)
    $RCMAIL->output->add_gui_object('messageform', $form_name);
  $MESSAGE_FORM = $form_name;
  return array($form_start, $form_end);
@@ -1228,6 +1334,7 @@
  'priorityselector' => 'rcmail_priority_selector',
  'editorselector' => 'rcmail_editor_selector',
  'receiptcheckbox' => 'rcmail_receipt_checkbox',
  'dsncheckbox' => 'rcmail_dsn_checkbox',
  'storetarget' => 'rcmail_store_target_selection',
));