From 1c5be6f792510c842370a2869603c6ec7ae9a29e Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Wed, 13 May 2009 03:09:49 -0400
Subject: [PATCH] - Support UUencode content encoding (#1485839)

---
 CHANGELOG                      |    1 
 program/include/rcube_imap.php |   64 +++++----------
 program/lib/imap.inc           |  116 +++++++++++++++-------------
 3 files changed, 83 insertions(+), 98 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 31efef1..55e7001 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 CHANGELOG RoundCube Webmail
 ===========================
 
+- Support UUencode content encoding (#1485839)
 - Minimize chance of race condition in session handling (#1485659, #1484678)
 - Fix session handling on non-session SQL query error (#1485734)
 - Fix html editor mode setting when reopening draft message (#1485834)
diff --git a/program/include/rcube_imap.php b/program/include/rcube_imap.php
index 9951bda..6d44efb 100644
--- a/program/include/rcube_imap.php
+++ b/program/include/rcube_imap.php
@@ -1433,44 +1433,19 @@
 
     if (!$part) $part = 'TEXT';
 
-    if ($print)
-      {
-      $mode = $o_part->encoding == 'base64' ? 3 : ($o_part->encoding == 'quoted-printable' ? 1 : 2);
-      $body = iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id, $part, $mode);
+    $body = iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id, $part,
+        $o_part->encoding, $print, $fp);
       
-      // we have to decode the part manually before printing
-      if ($mode == 1)
-        {
-        echo $this->mime_decode($body, $o_part->encoding);
-        $body = true;
-        }
-      }
-    else
-      {
-      if ($fp && $o_part->encoding == 'base64')
-        return iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id, $part, 3, $fp);
-      else
-        $body = iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id, $part, 1);
+    if ($fp || $print)
+      return true;
 
-      // decode part body
-      if ($o_part->encoding)
-        $body = $this->mime_decode($body, $o_part->encoding);
+    // convert charset (if text or message part)
+    if ($o_part->ctype_primary=='text' || $o_part->ctype_primary=='message') {
+      // assume default if no charset specified
+      if (empty($o_part->charset))
+        $o_part->charset = $this->default_charset;
 
-      // convert charset (if text or message part)
-      if ($o_part->ctype_primary=='text' || $o_part->ctype_primary=='message')
-        {
-        // assume default if no charset specified
-        if (empty($o_part->charset))
-          $o_part->charset = $this->default_charset;
-
-        $body = rcube_charset_convert($body, $o_part->charset);
-        }
-      
-      if ($fp)
-        {
-        fwrite($fp, $body);
-        return true;
-        }
+      $body = rcube_charset_convert($body, $o_part->charset);
       }
     
     return $body;
@@ -1487,8 +1462,7 @@
   function &get_body($uid, $part=1)
     {
     $headers = $this->get_headers($uid);
-    return rcube_charset_convert(
-      $this->mime_decode($this->get_message_part($uid, $part), 'quoted-printable'),
+    return rcube_charset_convert($this->get_message_part($uid, $part, NULL),
       $headers->charset ? $headers->charset : $this->default_charset);
     }
 
@@ -1535,7 +1509,7 @@
     if (!($msg_id = $this->_uid2id($uid)))
       return FALSE;
 
-    iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id, NULL, 2);
+    iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id, NULL, NULL, true);
     }
 
 
@@ -2665,10 +2639,6 @@
     {
     switch (strtolower($encoding))
       {
-      case '7bit':
-        return $input;
-        break;
-      
       case 'quoted-printable':
         return quoted_printable_decode($input);
         break;
@@ -2676,7 +2646,15 @@
       case 'base64':
         return base64_decode($input);
         break;
-      
+
+      case 'x-uuencode':
+      case 'x-uue':
+      case 'uue':
+      case 'uuencode':
+        return convert_uudecode($input);
+        break;
+						      
+      case '7bit':
       default:
         return $input;
       }
diff --git a/program/lib/imap.inc b/program/lib/imap.inc
index 1295d5f..2427e46 100644
--- a/program/lib/imap.inc
+++ b/program/lib/imap.inc
@@ -2308,30 +2308,42 @@
 
 	$part = empty($part) ? 'HEADER' : $part.'.MIME';
 
-        return iil_C_HandlePartBody($conn, $mailbox, $id, $part, 1);
+        return iil_C_HandlePartBody($conn, $mailbox, $id, $part);
 }
 
-function iil_C_HandlePartBody(&$conn, $mailbox, $id, $part='', $mode=1, $file=NULL) {
-	/* modes:
-        1: return string (or write to $file pointer)
-        2: print
-        3: base64 and print (or write to $file pointer)
-	*/
+function iil_C_HandlePartBody(&$conn, $mailbox, $id, $part='', $encoding=NULL, $print=NULL, $file=NULL) {
 	
 	$fp     = $conn->fp;
 	$result = false;
+	
+	switch ($encoding) {
+		case 'base64':
+			$mode = 1;
+		break;
+		case 'quoted-printable':
+			$mode = 2;
+		break;
+		case 'x-uuencode':
+		case 'x-uue':
+		case 'uue':
+		case 'uuencode':
+			$mode = 3;
+		break;
+		default:
+			$mode = 0;
+	}
 	
 	if (iil_C_Select($conn, $mailbox)) {
     		$reply_key = '* ' . $id;
 
     		// format request
-		$key     = 'ftch' . ($c++) . ' ';
-		$request = $key . "FETCH $id (BODY.PEEK[$part])";
+		$key     = 'ftch0';
+		$request = $key . " FETCH $id (BODY.PEEK[$part])";
     		// send request
 		if (!iil_PutLine($fp, $request)) {
 		    return false;
     		}
-        
+
     		// receive reply line
     		do {
         		$line = chop(iil_ReadLine($fp, 1000));
@@ -2352,14 +2364,13 @@
 				$result = substr($line, $from, $len);
 			}
 	    
-	                if ($mode == 2) {
-        		        echo $result;
-	                } else if ($mode == 3) {
-				if ($file)
-					fwrite($file, base64_decode($result));
-        		    	else
-					echo base64_decode($result);
-			}			     
+        		if ($mode == 1)
+				$result = base64_decode($result);
+			else if ($mode == 2)
+				$result = quoted_printable_decode($result);
+			else if ($mode == 3)
+				$result = convert_uudecode($result);
+
     		} else if ($line[$len-1] == '}') {
 	                //multi-line request, find sizes of content and receive that many bytes
         		$from     = strpos($line, '{') + 1;
@@ -2381,13 +2392,6 @@
 				$line = rtrim($line, "\t\r\n\0\x0B");
 
 		                if ($mode == 1) {
-					if ($file)
-						fwrite($file, $line . "\n");
-            			        else
-						$result .= $line . "\n";
-		                } else if ($mode == 2) {
-            			        echo $line . "\n";
-		                } else if ($mode == 3) {
 					// create chunks with proper length for base64 decoding
 					$line = $prev.$line;
 					$length = strlen($line);
@@ -2401,8 +2405,32 @@
 
 					if ($file)
 						fwrite($file, base64_decode($line));
-            				else
+            				else if ($print)
 						echo base64_decode($line);
+					else
+						$result .= base64_decode($line);
+				} else if ($mode == 2) {
+					$line .= $line[sizeof($line)-1] != '=' ? "\n" : '';
+					if ($file)
+						fwrite($file, quoted_printable_decode($line));
+            				else if ($print)
+						echo quoted_printable_decode($line);
+					else
+						$result .= quoted_printable_decode($line);
+				} else if ($mode == 3) {
+					if ($file)
+						fwrite($file, convert_uudecode($line));
+            				else if ($print)
+						echo convert_uudecode($line);
+					else
+						$result .= convert_uudecode($line);
+				} else {
+					if ($file)
+						fwrite($file, $line . "\n");
+            			        else if ($print)
+						echo $line . "\n";
+					else
+						$result .= $line . "\n";
 				}
         		}
     		}
@@ -2412,43 +2440,28 @@
         			$line = iil_ReadLine($fp, 1024);
 			} while (!iil_StartsWith($line, $key, true));
         
-		if ($mode == 3 && $file) {
-			return true;
-		}
-	
     		if ($result) {
 	    		$result = rtrim($result, "\t\r\n\0\x0B");
 			if ($file) {
 				fwrite($file, $result);
-				return true;
-			}	
-			return $result; // substr($result, 0, strlen($result)-1);
+			} else if ($print) {
+				echo $result;
+			} else
+				return $result; // substr($result, 0, strlen($result)-1);
     		}
     		
-		return false;
+		return true;
 	}
     
-	if ($mode==1) {
-		if ($file && $result) {
-			fwrite($file, $result);
-			return true;
-		}
-    		return $result;
-	}
-	
 	return false;
 }
 
 function iil_C_FetchPartBody(&$conn, $mailbox, $id, $part, $file=NULL) {
-	return iil_C_HandlePartBody($conn, $mailbox, $id, $part, 1, $file);
+	return iil_C_HandlePartBody($conn, $mailbox, $id, $part, NULL, NULL, $file);
 }
 
 function iil_C_PrintPartBody(&$conn, $mailbox, $id, $part) {
-	iil_C_HandlePartBody($conn, $mailbox, $id, $part, 2);
-}
-
-function iil_C_PrintBase64Body(&$conn, $mailbox, $id, $part) {
-	iil_C_HandlePartBody($conn, $mailbox, $id, $part, 3);
+	iil_C_HandlePartBody($conn, $mailbox, $id, $part, NULL, true, NULL);
 }
 
 function iil_C_CreateFolder(&$conn, $folder) {
@@ -2594,13 +2607,6 @@
 		}
 	}
 	return $result;
-}
-
-function iil_C_PrintSource(&$conn, $folder, $id, $part) {
-	$header = iil_C_FetchPartHeader($conn, $folder, $id, $part);
-	//echo str_replace("\r", '', $header);
-	echo $header;
-	echo iil_C_PrintPartBody($conn, $folder, $id, $part);
 }
 
 function iil_C_GetQuota(&$conn) {

--
Gitblit v1.9.1