]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-5229 and MDL-6196 adding gb18030 mime header encoding; merged from MOODLE_18_STABLE
authorskodak <skodak>
Mon, 12 Mar 2007 08:41:28 +0000 (08:41 +0000)
committerskodak <skodak>
Mon, 12 Mar 2007 08:41:28 +0000 (08:41 +0000)
admin/settings/server.php
lib/moodlelib.php
lib/phpmailer/README_MOODLE.txt
lib/phpmailer/class.phpmailer.php
lib/textlib.class.php

index 30d04ac915dba10c944d7e5d7004eb172737ad28..cefd3e3bc24828afbcc224e7d75a2996505989bf 100644 (file)
@@ -48,6 +48,7 @@ $temp->add(new admin_setting_configselect('digestmailtime', get_string('digestma
                                                                                                                                                           '22' => '22',
                                                                                                                                                           '23' => '23')));
 $charsets = get_list_of_charsets();
+unset($charsets['UTF-8']); // not needed here
 $options = array();
 $options['0'] = get_string('none');
 $options = array_merge($options, $charsets);
index 66d2d2955c84c58fd45a30fa167c21081fbf3974..fb14196d5fc78b278107661096561ea7ac4364f7 100644 (file)
@@ -3296,7 +3296,7 @@ function email_to_user($user, $from, $subject, $messagetext, $messagehtml='', $a
     $mail->Version = 'Moodle '. $CFG->version;           // mailer version
     $mail->PluginDir = $CFG->libdir .'/phpmailer/';      // plugin directory (eg smtp plugin)
 
-    $mail->CharSet = 'utf-8';
+    $mail->CharSet = 'UTF-8';
 
     if ($CFG->smtphosts == 'qmail') {
         $mail->IsQmail();                              // use Qmail system
@@ -3403,10 +3403,8 @@ function email_to_user($user, $from, $subject, $messagetext, $messagehtml='', $a
             }
         }
     /// If it has changed, convert all the necessary strings
-        $charset = strtolower($charset);
         $charsets = get_list_of_charsets();
         unset($charsets['UTF-8']);
-        $charsets = array_map('strtolower', $charsets);
         if (in_array($charset, $charsets)) {
         /// Save the new mail charset
             $mail->CharSet = $charset;
index 3f57d592c6e38d7ea435b2dc2aa1f7ebad18cd2d..c60713f4f05eeb3c7c1f28d41820d11e04b29073 100644 (file)
@@ -7,3 +7,8 @@ Moodle-specific changes to phpmailer are tracked here.
   Date: 14 Feb 2007
   Reason: http://tracker.moodle.org/browse/MDL-3681
 
+  lib/phpmailer/class.phpmailer.php
+
+  Changed by skodak
+  Date 12 Mar 2007
+  Reason: support for gb18030
\ No newline at end of file
index 2cd5cfc7258231c5c7fe251029f8b2eec8dbc119..6b2603563fa5147274a4b006a07f96ba32e33f5d 100644 (file)
@@ -1192,7 +1192,11 @@ class PHPMailer
 
     /// Start Moodle Hack - do our own multibyte-safe header encoding
         $textlib = textlib_get_instance();
-        return $textlib->encode_mimeheader($str, $this->CharSet);
+        $result = $textlib->encode_mimeheader($str, $this->CharSet);
+        if ($result !== false) {
+            return $result;
+        }
+        // try the old way that does not handle binary-safe line splitting in mime header
     /// End Moodle Hack
       $x = 0;
       
index 61234e8573e084b6696f979a72c17034a4d587d2..b8a570c619d19a0602002b44f78f151885dc9314 100644 (file)
@@ -222,6 +222,9 @@ class textlib {
      * paravoid (http://www.php.net/manual/en/function.mb-encode-mimeheader.php#60283).
      */
     function encode_mimeheader($text, $charset='utf-8') {
+        if (empty($text)) {
+            return (string)$text;
+        }
     /// Normalize charset
         $charset = $this->typo3cs->parse_charset($charset);
     /// If the text is pure ASCII, we don't need to encode it
@@ -239,16 +242,43 @@ class textlib {
     /// Max line length is 75 (including start and end)
         $length = 75 - strlen($start) - strlen($end);
     /// Multi-byte ratio
-        $ratio = $this->strlen($text, $charset) / strlen($text);
+        $multilength = $this->strlen($text, $charset);
+    /// Detect if strlen and friends supported
+        if ($multilength === false) {
+            if ($charset == 'GB18030' or $charset == 'gb18030') {
+                while (strlen($text)) {
+                    // try to encode first 22 chars - we expect most chars are two bytes long
+                    if (preg_match('/^(([\x00-\x7f])|([\x81-\xfe][\x40-\x7e])|([\x81-\xfe][\x80-\xfe])|([\x81-\xfe][\x30-\x39]..)){1,22}/m', $text, $matches)) {
+                        $chunk = $matches[0];
+                        $encchunk = base64_encode($chunk);
+                        if (strlen($encchunk) > $length) {
+                            // find first 11 chars - each char in 4 bytes - worst case scenario
+                            preg_match('/^(([\x00-\x7f])|([\x81-\xfe][\x40-\x7e])|([\x81-\xfe][\x80-\xfe])|([\x81-\xfe][\x30-\x39]..)){1,11}/m', $text, $matches);                            
+                            $chunk = $matches[0];
+                            $encchunk = base64_encode($chunk);
+                        }
+                        $text = substr($text, strlen($chunk));
+                        $encoded .= ' '.$start.$encchunk.$end.$linefeed;
+                    } else {
+                        break;
+                    }
+                }
+                $encoded = trim($encoded);
+                return $encoded; 
+            } else {
+                return false;
+            }
+        }
+        $ratio = $multilength / strlen($text);
     /// Base64 ratio
         $magic = $avglength = floor(3 * $length * $ratio / 4);
     /// basic infinite loop protection
         $maxiterations = strlen($text)*2;
         $iteration = 0; 
     /// Iterate over the string in magic chunks
-        for ($i=0; $i <= $this->strlen($text, $charset); $i+=$magic) {
+        for ($i=0; $i <= $multilength; $i+=$magic) {
             if ($iteration++ > $maxiterations) {
-                return str_repeat('?', strlen($text)); // probably infinite loop, safer to use raw char length here 
+                return false; // probably infinite loop 
             } 
             $magic = $avglength;
             $offset = 0;