From b79cc545ec020f7dd4bd83dcd06af3cf2b1fcaff Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Tue, 28 Aug 2012 05:20:20 -0400
Subject: [PATCH] Improvements/fixes for Larry skin

---
 program/include/rcube_mime.php |  192 +++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 174 insertions(+), 18 deletions(-)

diff --git a/program/include/rcube_mime.php b/program/include/rcube_mime.php
index bc0b2aa..d8e04a9 100644
--- a/program/include/rcube_mime.php
+++ b/program/include/rcube_mime.php
@@ -19,9 +19,6 @@
  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
  | Author: Aleksander Machniak <alec@alec.pl>                            |
  +-----------------------------------------------------------------------+
-
- $Id$
-
 */
 
 
@@ -34,7 +31,7 @@
  */
 class rcube_mime
 {
-    private static $default_charset = RCMAIL_CHARSET;
+    private static $default_charset;
 
 
     /**
@@ -42,12 +39,26 @@
      */
     function __construct($default_charset = null)
     {
-        if ($default_charset) {
-            self::$default_charset = $default_charset;
+        self::$default_charset = $default_charset;
+    }
+
+
+    /**
+     * Returns message/object character set name
+     *
+     * @return string Characted set name
+     */
+    public static function get_charset()
+    {
+        if (self::$default_charset) {
+            return self::$default_charset;
         }
-        else {
-            self::$default_charset = rcmail::get_instance()->config->get('default_charset', RCMAIL_CHARSET);
+
+        if ($charset = rcube::get_instance()->config->get('default_charset')) {
+            return $charset;
         }
+
+        return RCMAIL_CHARSET;
     }
 
 
@@ -92,15 +103,15 @@
         if ($part->ctype_parameters['charset'])
             $struct->charset = $part->ctype_parameters['charset'];
 
-        $part_charset = $struct->charset ? $struct->charset : self::$default_charset;
+        $part_charset = $struct->charset ? $struct->charset : self::get_charset();
 
-        // TODO: determine filename
+        // determine filename
         if (($filename = $part->d_parameters['filename']) || ($filename = $part->ctype_parameters['name'])) {
             $struct->filename = rcube_mime::decode_mime_string($filename, $part_charset);
         }
 
         // copy part body and convert it to UTF-8 if necessary
-        $struct->body = $part->ctype_primary == 'text' ? rcube_charset::convert($part->body, $part_charset) : $part->body;
+        $struct->body = $part->ctype_primary == 'text' || !$part->ctype_parameters['charset'] ? rcube_charset::convert($part->body, $part_charset) : $part->body;
         $struct->size = strlen($part->body);
         $struct->disposition = $part->disposition;
 
@@ -186,7 +197,7 @@
      */
     public static function decode_mime_string($input, $fallback = null)
     {
-        $default_charset = !empty($fallback) ? $fallback : self::$default_charset;
+        $default_charset = !empty($fallback) ? $fallback : self::get_charset();
 
         // rfc: all line breaks or other characters not found
         // in the Base64 Alphabet must be ignored by decoding software
@@ -214,7 +225,7 @@
                 // Append everything that is before the text to be decoded
                 if ($start != $pos) {
                     $substr = substr($input, $start, $pos-$start);
-                    $out   .= rcube_charset_convert($substr, $default_charset);
+                    $out   .= rcube_charset::convert($substr, $default_charset);
                     $start  = $pos;
                 }
                 $start += $length;
@@ -255,13 +266,13 @@
                     $text = quoted_printable_decode($text);
                 }
 
-                $out .= rcube_charset_convert($text, $charset);
+                $out .= rcube_charset::convert($text, $charset);
                 $tmp = array();
             }
 
             // add the last part of the input string
             if ($start != strlen($input)) {
-                $out .= rcube_charset_convert(substr($input, $start), $default_charset);
+                $out .= rcube_charset::convert(substr($input, $start), $default_charset);
             }
 
             // return the results
@@ -269,7 +280,7 @@
         }
 
         // no encoding information, use fallback
-        return rcube_charset_convert($input, $default_charset);
+        return rcube_charset::convert($input, $default_charset);
     }
 
 
@@ -530,10 +541,10 @@
                     $prefix = $regs[0];
                     $level = strlen($prefix);
                     $line  = rtrim(substr($line, $level));
-                    $line  = $prefix . rc_wordwrap($line, $length - $level - 2, " \r\n$prefix ");
+                    $line  = $prefix . self::wordwrap($line, $length - $level - 2, " \r\n$prefix ");
                 }
                 else if ($line) {
-                    $line = rc_wordwrap(rtrim($line), $length - 2, " \r\n");
+                    $line = self::wordwrap(rtrim($line), $length - 2, " \r\n");
                     // space-stuffing
                     $line = preg_replace('/(^|\r\n)(From| |>)/', '\\1 \\2', $line);
                 }
@@ -545,4 +556,149 @@
         return implode("\r\n", $text);
     }
 
+
+    /**
+     * Improved wordwrap function.
+     *
+     * @param string $string  Text to wrap
+     * @param int    $width   Line width
+     * @param string $break   Line separator
+     * @param bool   $cut     Enable to cut word
+     *
+     * @return string Text
+     */
+    public static function wordwrap($string, $width=75, $break="\n", $cut=false)
+    {
+        $para   = explode($break, $string);
+        $string = '';
+
+        while (count($para)) {
+            $line = array_shift($para);
+            if ($line[0] == '>') {
+                $string .= $line.$break;
+                continue;
+            }
+
+            $list = explode(' ', $line);
+            $len = 0;
+            while (count($list)) {
+                $line   = array_shift($list);
+                $l      = mb_strlen($line);
+                $newlen = $len + $l + ($len ? 1 : 0);
+
+                if ($newlen <= $width) {
+                    $string .= ($len ? ' ' : '').$line;
+                    $len += (1 + $l);
+                }
+                else {
+                    if ($l > $width) {
+                        if ($cut) {
+                            $start = 0;
+                            while ($l) {
+                                $str = mb_substr($line, $start, $width);
+                                $strlen = mb_strlen($str);
+                                $string .= ($len ? $break : '').$str;
+                                $start += $strlen;
+                                $l -= $strlen;
+                                $len = $strlen;
+                            }
+                        }
+                        else {
+                            $string .= ($len ? $break : '').$line;
+                            if (count($list)) {
+                                $string .= $break;
+                            }
+                            $len = 0;
+                        }
+                    }
+                    else {
+                        $string .= $break.$line;
+                        $len = $l;
+                    }
+                }
+            }
+
+            if (count($para)) {
+                $string .= $break;
+            }
+        }
+
+        return $string;
+    }
+
+
+    /**
+     * A method to guess the mime_type of an attachment.
+     *
+     * @param string $path      Path to the file.
+     * @param string $name      File name (with suffix)
+     * @param string $failover  Mime type supplied for failover.
+     * @param string $is_stream Set to True if $path contains file body
+     *
+     * @return string
+     * @author Till Klampaeckel <till@php.net>
+     * @see    http://de2.php.net/manual/en/ref.fileinfo.php
+     * @see    http://de2.php.net/mime_content_type
+     */
+    public static function file_content_type($path, $name, $failover = 'application/octet-stream', $is_stream = false)
+    {
+        $mime_type = null;
+        $mime_magic = rcube::get_instance()->config->get('mime_magic');
+        $mime_ext = @include RCMAIL_CONFIG_DIR . '/mimetypes.php';
+
+        // use file name suffix with hard-coded mime-type map
+        if (is_array($mime_ext) && $name) {
+            if ($suffix = substr($name, strrpos($name, '.')+1)) {
+                $mime_type = $mime_ext[strtolower($suffix)];
+            }
+        }
+
+        // try fileinfo extension if available
+        if (!$mime_type && function_exists('finfo_open')) {
+            if ($finfo = finfo_open(FILEINFO_MIME, $mime_magic)) {
+                if ($is_stream)
+                    $mime_type = finfo_buffer($finfo, $path);
+                else
+                    $mime_type = finfo_file($finfo, $path);
+                finfo_close($finfo);
+            }
+        }
+
+        // try PHP's mime_content_type
+        if (!$mime_type && !$is_stream && function_exists('mime_content_type')) {
+            $mime_type = @mime_content_type($path);
+        }
+
+        // fall back to user-submitted string
+        if (!$mime_type) {
+            $mime_type = $failover;
+        }
+        else {
+            // Sometimes (PHP-5.3?) content-type contains charset definition,
+            // Remove it (#1487122) also "charset=binary" is useless
+            $mime_type = array_shift(preg_split('/[; ]/', $mime_type));
+        }
+
+        return $mime_type;
+    }
+
+
+    /**
+     * Detect image type of the given binary data by checking magic numbers.
+     *
+     * @param string $data  Binary file content
+     *
+     * @return string Detected mime-type or jpeg as fallback
+     */
+    public static function image_content_type($data)
+    {
+        $type = 'jpeg';
+        if      (preg_match('/^\x89\x50\x4E\x47/', $data)) $type = 'png';
+        else if (preg_match('/^\x47\x49\x46\x38/', $data)) $type = 'gif';
+        else if (preg_match('/^\x00\x00\x01\x00/', $data)) $type = 'ico';
+    //  else if (preg_match('/^\xFF\xD8\xFF\xE0/', $data)) $type = 'jpeg';
+
+        return 'image/' . $type;
+    }
+
 }

--
Gitblit v1.9.1