alecpl
2010-06-23 b25dfd0913f2ca5666124740c30156fa0a6abaef
program/steps/mail/func.inc
@@ -27,7 +27,7 @@
$EMAIL_ADDRESS_PATTERN = '([a-z0-9][a-z0-9\-\.\+\_]*@[a-z0-9][a-z0-9\-\.]*\\.[a-z]{2,5})';
// actions that do not require imap connection
$NOIMAP_ACTIONS = array('spell', 'addcontact', 'autocomplete', 'upload', 'display-attachment', 'remove-attachment');
$NOIMAP_ACTIONS = array('addcontact', 'autocomplete', 'upload', 'display-attachment', 'remove-attachment');
// always instantiate imap object (but not yet connect to server)
$RCMAIL->imap_init();
@@ -130,7 +130,7 @@
  if (!$OUTPUT->ajax_call)
    $OUTPUT->add_label('checkingmail', 'deletemessage', 'movemessagetotrash',
      'movingmessage', 'copyingmessage', 'copy', 'move');
      'movingmessage', 'copyingmessage', 'copy', 'move', 'quota');
  $OUTPUT->set_pagetitle(rcmail_localize_foldername($mbox_name));
  }
@@ -170,6 +170,10 @@
  if ((strpos($mbox.$delim, $CONFIG['sent_mbox'].$delim)===0 || strpos($mbox.$delim, $CONFIG['drafts_mbox'].$delim)===0)
      && (($f = array_search('from', $a_show_cols)) !== false) && array_search('to', $a_show_cols) === false)
    $a_show_cols[$f] = 'to';
  // make sure 'threads' column is present
  if (!in_array('threads', $a_show_cols))
    array_unshift($a_show_cols, 'threads');
  $skin_path = $_SESSION['skin_path'] = $CONFIG['skin_path'];
@@ -223,7 +227,7 @@
 */
function rcmail_js_message_list($a_headers, $insert_top=FALSE, $head_replace=FALSE)
{
  global $CONFIG, $IMAP, $OUTPUT;
  global $CONFIG, $IMAP, $RCMAIL, $OUTPUT;
  if (!empty($_SESSION['list_attrib']['columns']))
    $a_show_cols = $_SESSION['list_attrib']['columns'];
@@ -238,6 +242,18 @@
      && (($f = array_search('from', $a_show_cols)) !== false) && array_search('to', $a_show_cols) === false)
    $a_show_cols[$f] = 'to';
  // make sure 'threads' column is present
  if (!in_array('threads', $a_show_cols))
    array_unshift($a_show_cols, 'threads');
  // Plugins may set header's list_cols/list_flags and other rcube_mail_header variables
  // and list columns
  $plugin = $RCMAIL->plugins->exec_hook('messages_list',
    array('messages' => $a_headers, 'cols' => $a_show_cols));
  $a_show_cols = $plugin['cols'];
  $a_headers   = $plugin['messages'];
  $thead = $head_replace ? rcmail_message_list_head($_SESSION['list_attrib'], $a_show_cols) : NULL;
  $OUTPUT->command('set_message_coltypes', $a_show_cols, $thead);
@@ -245,20 +261,22 @@
  if (empty($a_headers))
    return;
  // remove 'attachment' and 'flag' columns, we don't need them here
  if(($key = array_search('attachment', $a_show_cols)) !== FALSE)
  // remove 'threads', 'attachment', 'flag' columns, we don't need them here
  if (($key = array_search('attachment', $a_show_cols)) !== FALSE)
    unset($a_show_cols[$key]);
  if(($key = array_search('flag', $a_show_cols)) !== FALSE)
  if (($key = array_search('flag', $a_show_cols)) !== FALSE)
    unset($a_show_cols[$key]);
  if (($key = array_search('threads', $a_show_cols)) !== FALSE)
    unset($a_show_cols[$key]);
  // loop through message headers
  foreach ($a_headers as $n => $header)
    {
    $a_msg_cols = array();
    $a_msg_flags = array();
    if (empty($header))
      continue;
    $a_msg_cols = array();
    $a_msg_flags = array();
    $IMAP->set_charset(!empty($header->charset) ? $header->charset : $CONFIG['default_charset']);
@@ -285,6 +303,8 @@
    if ($header->depth)
      $a_msg_flags['depth'] = $header->depth;
    else if ($header->has_children)
      $roots[] = $header->uid;
    if ($header->parent_uid)
      $a_msg_flags['parent_uid'] = $header->parent_uid;
    if ($header->has_children)
@@ -305,11 +325,21 @@
      $a_msg_flags['attachment'] = 1;
    $a_msg_flags['mbox'] = $mbox;
    // merge with plugin result
    if (!empty($header->list_flags) && is_array($header->list_flags))
      $a_msg_flags = array_merge($a_msg_flags, $header->list_flags);
    if (!empty($header->list_cols) && is_array($header->list_cols))
      $a_msg_cols = array_merge($a_msg_cols, $header->list_cols);
    $OUTPUT->command('add_message_row',
      $header->uid,
      $a_msg_cols,
      $a_msg_flags,
      $insert_top);
    }
    if ($IMAP->threading) {
      $OUTPUT->command('init_threads', (array) $roots);
    }
}
@@ -339,7 +369,7 @@
  else
    $list_menu = '';
  $cells = array(array('className' => 'threads', 'html' => $list_menu));
  $cells = array();
  foreach ($a_show_cols as $col) {
    // get column name
@@ -349,6 +379,9 @@
        break;
      case 'attachment':
        $col_name = sprintf($image_tag, $skin_path, $attrib['attachmenticon'], '');
        break;
      case 'threads':
        $col_name = $list_menu;
        break;
      default:
        $col_name = Q(rcube_label($col));
@@ -374,14 +407,15 @@
 */
function rcmail_messagecontent_frame($attrib)
  {
  global $OUTPUT;
  global $OUTPUT, $RCMAIL;
  if (empty($attrib['id']))
    $attrib['id'] = 'rcmailcontentwindow';
  $attrib['name'] = $attrib['id'];
  $OUTPUT->set_env('contentframe', $attrib['id']);
  if ($RCMAIL->config->get('preview_pane'))
    $OUTPUT->set_env('contentframe', $attrib['id']);
  $OUTPUT->set_env('blankpage', $attrib['src'] ? $OUTPUT->abs_url($attrib['src']) : 'program/blank.gif');
  return html::iframe($attrib);
@@ -415,13 +449,10 @@
  $quota = rcmail_quota_content($attrib);
  if (is_array($quota)) {
    $OUTPUT->add_script('$(document).ready(function(){
  $OUTPUT->add_script('$(document).ready(function(){
   rcmail.set_quota('.json_serialize($quota).')});', 'foot');
    $quota = '';
    }
  return html::span($attrib, $quota);
  return html::span($attrib, '');
  }
@@ -429,39 +460,37 @@
  {
  global $COMM_PATH, $RCMAIL;
  $display = isset($_SESSION['quota_display']) ? $_SESSION['quota_display'] : '';
  $quota = $RCMAIL->imap->get_quota();
  $quota = $RCMAIL->plugins->exec_hook('quota', $quota);
  if (!$quota['total'] && $RCMAIL->config->get('quota_zero_as_unlimited'))
    return rcube_label('unlimited');
  $quota_result = (array) $quota;
  $quota_result['type'] = isset($_SESSION['quota_display']) ? $_SESSION['quota_display'] : '';
  if ($quota['total'])
    {
  if (!$quota['total'] && $RCMAIL->config->get('quota_zero_as_unlimited')) {
    $quota_result['title'] = rcube_label('unlimited');
    $quota_result['percent'] = 0;
    }
  else if ($quota['total']) {
    if (!isset($quota['percent']))
      $quota['percent'] = min(100, round(($quota['used']/max(1,$quota['total']))*100));
      $quota_result['percent'] = min(100, round(($quota['used']/max(1,$quota['total']))*100));
    $quota_result = sprintf('%s / %s (%.0f%%)',
    $title = sprintf('%s / %s (%.0f%%)',
        show_bytes($quota['used'] * 1024), show_bytes($quota['total'] * 1024),
        $quota['percent']);
        $quota_result['percent']);
    if ($display == 'image') {
      $quota_result = array(
       'percent'    => $quota['percent'],
        'title'      => $quota_result,
     );
    $quota_result['title'] = $title;
      if ($attrib['width'])
        $quota_result['width'] = $attrib['width'];
      if ($attrib['height'])
        $quota_result['height']   = $attrib['height'];
      }
      return $quota_result;
    if ($attrib['width'])
      $quota_result['width'] = $attrib['width'];
    if ($attrib['height'])
      $quota_result['height']   = $attrib['height'];
    }
  else {
    $quota_result['title'] = rcube_label('unknown');
    $quota_result['percent'] = 0;
    }
  return rcube_label('unknown');
  return $quota_result;
  }
@@ -518,12 +547,16 @@
}
function rcmail_send_unread_count($mbox_name, $force=false)
function rcmail_send_unread_count($mbox_name, $force=false, $count=null)
{
  global $RCMAIL;
  $old_unseen = $_SESSION['unseen_count'][$mbox_name];
  $unseen = $RCMAIL->imap->messagecount($mbox_name, 'UNSEEN', $force);
  if ($count === null)
    $unseen = $RCMAIL->imap->messagecount($mbox_name, 'UNSEEN', $force);
  else
    $unseen = $count;
  if ($unseen != $old_unseen || ($mbox_name == 'INBOX'))
    $RCMAIL->output->command('set_unread_count', $mbox_name, $unseen, ($mbox_name == 'INBOX'));
@@ -927,6 +960,15 @@
        if (!isset($part->body))
          $part->body = $MESSAGE->get_part_content($part->mime_id);
        // message is cached but not exists (#1485443), or other error
        if ($part->body === false) {
          rcmail_message_error($MESSAGE->uid);
        }
        // re-format format=flowed content
        if ($part->ctype_secondary == "plain" && $part->ctype_parameters['format'] == "flowed")
          $part->body = rcube_message::unfold_flowed($part->body);
        $body = rcmail_print_body($part, array('safe' => $safe_mode, 'plain' => !$CONFIG['prefer_html']));
        if ($part->ctype_secondary == 'html')
@@ -1062,7 +1104,8 @@
  $end = '>';
  if ($tag == 'link' && preg_match('/^https?:\/\//i', $attrib['href'])) {
    $attrib['href'] = "./bin/modcss.php?u=" . urlencode($attrib['href']) . "&c=" . urlencode($GLOBALS['rcmail_html_container_id']);
    $attrib['href'] = "?_task=utils&_action=modcss&u=" . urlencode($attrib['href'])
        . "&c=" . urlencode($GLOBALS['rcmail_html_container_id']);
    $end = ' />';
  }
  else if (preg_match('/^mailto:'.$EMAIL_ADDRESS_PATTERN.'(\?[^"\'>]+)?/i', $attrib['href'], $mailto)) {
@@ -1154,40 +1197,37 @@
/**
 * Wrap text to a given number of characters per line
 * but respect the mail quotation of replies messages (>)
 * but respect the mail quotation of replies messages (>).
 * Finally add another quotation level by prpending the lines
 * with >
 *
 * @param string Text to wrap
 * @param int The line width
 * @return string The wrapped text
 */
function rcmail_wrap_quoted($text, $max = 76)
function rcmail_wrap_and_quote($text, $length = 72)
{
  // Rebuild the message body with a maximum of $max chars, while keeping quoted message.
  $max = min(77, $length + 8);
  $lines = preg_split('/\r?\n/', trim($text));
  $out = '';
  foreach ($lines as $line) {
    if (strlen($line) > $max) {
      if (preg_match('/^([>\s]+)/', $line, $regs)) {
        $length = strlen($regs[0]);
        $prefix = substr($line, 0, $length);
        // Remove '> ' from the line, then wordwrap() the line
        $line = rc_wordwrap(substr($line, $length), $max - $length);
        // Rebuild the line with '> ' at the beginning of each 'subline'
        $newline = '';
        foreach (explode("\n", $line) as $l) {
          $newline .= $prefix . $l . "\n";
        }
        // Remove the righest newline char
        $line = rtrim($newline);
    // don't wrap already quoted lines
    if ($line[0] == '>')
      $line = '>' . rtrim($line);
    else if (mb_strlen($line) > $max) {
      $newline = '';
      foreach(explode("\n", rc_wordwrap($line, $length - 2)) as $l) {
        if (strlen($l))
          $newline .= '> ' . $l . "\n";
        else
          $newline .= ">\n";
      }
      else {
        $line = rc_wordwrap($line, $max);
      }
      $line = rtrim($newline);
    }
    else
      $line = '> ' . $line;
    // Append the line
    $out .= $line . "\n";
@@ -1358,8 +1398,8 @@
    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
      if (preg_match_all('/<([^@]+@[^>]+)>/', $headers_enc['To'], $m)) {
        $headers_enc['To'] = implode(', ', $m[1]);
        }
      }
    }
    $msg_body = $message->get();
@@ -1368,10 +1408,23 @@
            'file' => __FILE__, 'line' => __LINE__,
            'message' => "Could not create message: ".$msg_body->getMessage()),
            TRUE, FALSE);
    else if (ini_get('safe_mode'))
      $sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str);
    else
      $sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str, "-f$from");
    else {
      $delim   = $RCMAIL->config->header_delimiter();
      $to      = $headers_enc['To'];
      $subject = $headers_enc['Subject'];
      if ($delim != "\r\n") {
        $header_str = str_replace("\r\n", $delim, $header_str);
        $msg_body   = str_replace("\r\n", $delim, $msg_body);
        $to         = str_replace("\r\n", $delim, $to);
        $subject    = str_replace("\r\n", $delim, $subject);
      }
      if (ini_get('safe_mode'))
        $sent = mail($to, $subject, $msg_body, $header_str);
      else
        $sent = mail($to, $subject, $msg_body, $header_str, "-f$from");
    }
  }
  if ($sent) {
@@ -1410,7 +1463,7 @@
    $recipient = array_shift($IMAP->decode_address_list($message->headers->mdn_to));
    $mailto = $recipient['mailto'];
    $compose = new Mail_mime($RCMAIL->config->header_delimiter());
    $compose = new Mail_mime("\r\n");
    $compose->setParam('text_encoding', 'quoted-printable');
    $compose->setParam('html_encoding', 'quoted-printable');
@@ -1421,7 +1474,7 @@
    // compose headers array
    $headers = array(
      'Date' => date('r'),
      'Date' => rcmail_user_date(),
      'From' => $sender,
      'To'   => $message->headers->mdn_to,
      'Subject' => rcube_label('receiptread') . ': ' . $message->subject,
@@ -1466,6 +1519,29 @@
  return false;
}
// Returns RFC2822 formatted current date in user's timezone
function rcmail_user_date()
{
  global $CONFIG;
  // get user's timezone
  if ($CONFIG['timezone'] === 'auto') {
    $tz = isset($_SESSION['timezone']) ? $_SESSION['timezone'] : date('Z')/3600;
  }
  else {
    $tz = $CONFIG['timezone'];
    if ($CONFIG['dst_active'])
      $tz++;
  }
  $date = time() + $tz * 60 * 60;
  $date = gmdate('r', $date);
  $date = preg_replace('/[+-][0-9]{4}$/', sprintf('%+05d', $tz * 100), $date);
  return $date;
}
function rcmail_search_filter($attrib)
{
  global $OUTPUT, $CONFIG;
@@ -1498,6 +1574,21 @@
  return $out;
}
function rcmail_message_error($uid=null)
{
  global $RCMAIL;
  // Set env variables for messageerror.html template
  if ($RCMAIL->action == 'show') {
    $mbox_name = $RCMAIL->imap->get_mailbox_name();
    $RCMAIL->output->set_env('mailbox', $mbox_name);
    $RCMAIL->output->set_env('uid', null);
  }
  // display error message
  $RCMAIL->output->show_message('messageopenerror', 'error');
  // ... display message error page
  $RCMAIL->output->send('messageerror');
}
// register UI objects
$OUTPUT->add_handlers(array(
@@ -1515,5 +1606,4 @@
  'searchform' => array($OUTPUT, 'search_form'),
));
?>