From cb2bc809ef29f349d38c89e202d821e67bb4c947 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Tue, 21 Sep 2010 14:47:55 -0400
Subject: [PATCH] Fix db_mode check in insert_id()

---
 program/steps/mail/func.inc |  154 ++++++++++++++++++++++++++++++++------------------
 1 files changed, 98 insertions(+), 56 deletions(-)

diff --git a/program/steps/mail/func.inc b/program/steps/mail/func.inc
index 68b790c..91fd05e 100644
--- a/program/steps/mail/func.inc
+++ b/program/steps/mail/func.inc
@@ -26,8 +26,8 @@
 
 $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('addcontact', 'autocomplete', 'upload', 'display-attachment', 'remove-attachment');
+// actions that do not require imap connection here
+$NOIMAP_ACTIONS = array('addcontact', 'autocomplete', 'upload', 'display-attachment', 'remove-attachment', 'get');
 
 // always instantiate imap object (but not yet connect to server)
 $RCMAIL->imap_init();
@@ -171,7 +171,9 @@
       && (($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 
+  // make sure 'threads' and 'subject' columns are present
+  if (!in_array('subject', $a_show_cols))
+    array_unshift($a_show_cols, 'subject');
   if (!in_array('threads', $a_show_cols))
     array_unshift($a_show_cols, 'threads');
 
@@ -223,28 +225,41 @@
 
 /**
  * return javascript commands to add rows to the message list
- * or to replace the whole list (IE only)
  */
-function rcmail_js_message_list($a_headers, $insert_top=FALSE, $head_replace=FALSE)
+function rcmail_js_message_list($a_headers, $insert_top=FALSE, $a_show_cols=null)
 {
   global $CONFIG, $IMAP, $RCMAIL, $OUTPUT;
 
-  if (!empty($_SESSION['list_attrib']['columns']))
-    $a_show_cols = $_SESSION['list_attrib']['columns'];
-  else
-    $a_show_cols = is_array($CONFIG['list_cols']) ? $CONFIG['list_cols'] : array('subject');
+  if (empty($a_show_cols)) {
+    if (!empty($_SESSION['list_attrib']['columns']))
+      $a_show_cols = $_SESSION['list_attrib']['columns'];
+    else
+      $a_show_cols = is_array($CONFIG['list_cols']) ? $CONFIG['list_cols'] : array('subject');
+  }
+  else {
+    if (!is_array($a_show_cols))
+      $a_show_cols = preg_split('/[\s,;]+/', strip_quotes($a_show_cols));
+    $head_replace = true;
+  }
 
   $mbox = $IMAP->get_mailbox_name();
   $delim = $IMAP->get_hierarchy_delimiter();
+
+  // make sure 'threads' and 'subject' columns are present
+  if (!in_array('subject', $a_show_cols))
+    array_unshift($a_show_cols, 'subject');
+  if (!in_array('threads', $a_show_cols))
+    array_unshift($a_show_cols, 'threads');
+
+  $_SESSION['list_attrib']['columns'] = $a_show_cols;
 
   // show 'to' instead of 'from' in sent/draft messages
   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');
+  // Make sure there are no duplicated columns (#1486999)
+  $a_show_cols = array_unique($a_show_cols);
 
   // Plugins may set header's list_cols/list_flags and other rcube_mail_header variables
   // and list columns
@@ -735,7 +750,7 @@
 
   // plaintext postprocessing
   if ($part->ctype_secondary == 'plain')
-    $body = rcmail_plain_body($body);
+    $body = rcmail_plain_body($body, $part->ctype_parameters['format'] == 'flowed');
 
   // allow post-processing of the message body
   $data = $RCMAIL->plugins->exec_hook('message_part_after', array('type' => $part->ctype_secondary, 'body' => $body) + $data);
@@ -747,75 +762,106 @@
 /**
  * Handle links and citation marks in plain text message
  *
- * @param string  Plain text string 
+ * @param string  Plain text string
+ * @param boolean Text uses format=flowed
+ *
  * @return string Formatted HTML string
  */
-function rcmail_plain_body($body)
+function rcmail_plain_body($body, $flowed=false)
 {
+  global $RCMAIL;
+
   // make links and email-addresses clickable
-  $replacements = new rcube_string_replacer;
+  $replacer = new rcube_string_replacer;
 
   // search for patterns like links and e-mail addresses
-  $body = preg_replace_callback($replacements->link_pattern, array($replacements, 'link_callback'), $body);
-  $body = preg_replace_callback($replacements->mailto_pattern, array($replacements, 'mailto_callback'), $body);
+  $body = preg_replace_callback($replacer->link_pattern, array($replacer, 'link_callback'), $body);
+  $body = preg_replace_callback($replacer->mailto_pattern, array($replacer, 'mailto_callback'), $body);
 
   // split body into single lines
   $a_lines = preg_split('/\r?\n/', $body);
-  $q_lines = array();
   $quote_level = 0;
+  $last = -1;
 
   // find/mark quoted lines...
   for ($n=0, $cnt=count($a_lines); $n < $cnt; $n++) {
-    $q = 0;
-
     if ($a_lines[$n][0] == '>' && preg_match('/^(>+\s*)+/', $a_lines[$n], $regs)) {
       $q = strlen(preg_replace('/\s/', '', $regs[0]));
-        $a_lines[$n] = substr($a_lines[$n], strlen($regs[0]));
+      $a_lines[$n] = substr($a_lines[$n], strlen($regs[0]));
 
       if ($q > $quote_level)
-        $q_lines[$n]['quote'] = $q - $quote_level;
+        $a_lines[$n] = $replacer->get_replacement($replacer->add(
+          str_repeat('<blockquote>', $q - $quote_level))) . $a_lines[$n];
       else if ($q < $quote_level)
-        $q_lines[$n]['endquote'] = $quote_level - $q;
+        $a_lines[$n] = $replacer->get_replacement($replacer->add(
+          str_repeat('</blockquote>', $quote_level - $q))) . $a_lines[$n];
+      else if ($flowed) {
+        // previous line is flowed
+        if (isset($a_lines[$last]) && $a_lines[$n]
+          && $a_lines[$last][strlen($a_lines[$last])-1] == ' ') {
+          // merge lines
+          $a_lines[$last] .= $a_lines[$n];
+          unset($a_lines[$n]);
+        }
+        else
+          $last = $n;
+      }
     }
-    else if ($quote_level > 0)
-      $q_lines[$n]['endquote'] = $quote_level;
+    else {
+      $q = 0;
+      if ($flowed) {
+        // sig separator - line is fixed
+        if ($a_lines[$n] == '-- ') {
+          $last = $n;
+        }
+        else {
+          // remove space-stuffing
+          if ($a_lines[$n][0] == ' ')
+            $a_lines[$n] = substr($a_lines[$n], 1);
+
+          // previous line is flowed?
+          if (isset($a_lines[$last]) && $a_lines[$n]
+            && $a_lines[$last] != '-- '
+            && $a_lines[$last][strlen($a_lines[$last])-1] == ' '
+          ) {
+            $a_lines[$last] .= $a_lines[$n];
+            unset($a_lines[$n]);
+          }
+          else {
+            $last = $n;
+          }
+        }
+        if ($quote_level > 0)
+          $a_lines[$last] = $replacer->get_replacement($replacer->add(
+            str_repeat('</blockquote>', $quote_level))) . $a_lines[$last];
+      }
+      else if ($quote_level > 0)
+        $a_lines[$n] = $replacer->get_replacement($replacer->add(
+          str_repeat('</blockquote>', $quote_level))) . $a_lines[$n];
+    }
 
     $quote_level = $q;
   }
 
   // quote plain text
-  $body = Q(join("\n", $a_lines), 'replace', false);
+  $body = Q(join("\n", $a_lines), '', false);
 
   // colorize signature
-  if (($sp = strrpos($body, '-- ')) !== false)
-    if (($sp == 0 || $body[$sp-1] == "\n") && $body[$sp+3] == "\n") {
-      $body = substr($body, 0, max(0, $sp))
-	.'<span class="sig">'.substr($body, $sp).'</span>';
+  $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))
+          .'<span class="sig">'.substr($body, $sp).'</span>';
+      break;
     }
+  }
 
-  // colorize quoted lines
-  $a_lines = preg_split('/\n/', $body);
-  foreach ($q_lines as $i => $q)
-    if ($q['quote'])
-      $a_lines[$i] = str_repeat('<blockquote>', $q['quote']) . $a_lines[$i];
-    else if ($q['endquote'])
-      $a_lines[$i] = str_repeat('</blockquote>', $q['endquote']) . $a_lines[$i];
+  // insert url/mailto links and citation tags
+  $body = $replacer->resolve($body);
 
-  // insert the links for urls and mailtos
-  $body = $replacements->resolve(join("\n", $a_lines));
-    
   return $body;
-}
-
-
-/**
- * add a string to the replacement array and return a replacement string
- */
-function rcmail_str_replacement($str, &$rep)
-{
-  static $count = 0;
-  $rep[$count] = stripslashes($str);
-  return "##string_replacement{".($count++)."}##";
 }
 
 
@@ -982,10 +1028,6 @@
 
         $plugin = $RCMAIL->plugins->exec_hook('message_body_prefix', array(
           'part' => $part, 'prefix' => ''));
-
-        // 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']));
 

--
Gitblit v1.9.1