From c96c32f4259c6774fe17dc0a7bebfb2193bc3ea3 Mon Sep 17 00:00:00 2001 From: stekkel Date: Fri, 7 Mar 2003 22:11:22 +0000 Subject: [PATCH] improved encodeHeader function and decodeHeader handling to follow RFC's better. git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@4612 7612ce4b-ef26-0410-bec9-ea0150e637f0 --- functions/file_prefs.php | 28 ++--- functions/imap_general.php | 2 +- functions/mailbox_display.php | 10 +- functions/mime.php | 196 +++++++++++++++++----------------- 4 files changed, 117 insertions(+), 119 deletions(-) diff --git a/functions/file_prefs.php b/functions/file_prefs.php index 79852012..1cde4c88 100644 --- a/functions/file_prefs.php +++ b/functions/file_prefs.php @@ -18,7 +18,7 @@ function cachePrefValues($data_dir, $username) { global $prefs_are_cached, $prefs_cache; if ( isset($prefs_are_cached) && $prefs_are_cached) { - return; + return $prefs_cache; } sqsession_unregister('prefs_cache'); @@ -79,6 +79,7 @@ function cachePrefValues($data_dir, $username) { sqsession_register($prefs_cache, 'prefs_cache'); sqsession_register($prefs_are_cached, 'prefs_are_cached'); + return $prefs_cache; } /** @@ -89,7 +90,7 @@ function getPref($data_dir, $username, $string, $default = '') { $result = do_hook_function('get_pref_override',array($username,$string)); if (!$result) { - cachePrefValues($data_dir, $username); + $prefs_cache = cachePrefValues($data_dir, $username); if (isset($prefs_cache[$string])) { $result = $prefs_cache[$string]; } else { @@ -105,11 +106,16 @@ function getPref($data_dir, $username, $string, $default = '') { /** * Save the preferences for this user. */ -function savePrefValues($data_dir, $username) { - global $prefs_cache; +function savePrefValues($data_dir, $username, $prefs_cache) { $filename = getHashedFile($username, $data_dir, "$username.pref"); + if (!is_array($prefs_cache) || count($prefs_cache) == 0) { + include_once(SM_PATH . 'functions/display_messages.php'); + logout_error( _("Houston we have a problem, the preference cache is lost!!!.")); + exit; + } + /* Open the file for writing, or else display an error to the user. */ if(!$file = @fopen($filename.'.tmp', 'w')) { @@ -138,13 +144,11 @@ function savePrefValues($data_dir, $username) { function removePref($data_dir, $username, $string) { global $prefs_cache; - cachePrefValues($data_dir, $username); - if (isset($prefs_cache[$string])) { unset($prefs_cache[$string]); } - savePrefValues($data_dir, $username); + savePrefValues($data_dir, $username,$prefs_cache); } /** @@ -153,18 +157,16 @@ function removePref($data_dir, $username, $string) { function setPref($data_dir, $username, $string, $value) { global $prefs_cache; - cachePrefValues($data_dir, $username); if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) { return; } if ($value === '') { - removePref($data_dir, $username, $string); - return; + unset($prefs_cache[$string]); + } else { + $prefs_cache[$string] = $value; } - - $prefs_cache[$string] = $value; - savePrefValues($data_dir, $username); + savePrefValues($data_dir, $username, $prefs_cache); } /** diff --git a/functions/imap_general.php b/functions/imap_general.php index 4504ce9d..b2880f87 100755 --- a/functions/imap_general.php +++ b/functions/imap_general.php @@ -449,7 +449,7 @@ function parseAddress($address, $max=0, $addr_ar = array(), $group = '', $host=' switch ($char) { case '=': if (preg_match('/^(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(.*)/Ui',substr($address,$pos),$reg)) { - $personal = $reg[1]; + $personal .= $reg[1]; $pos += strlen($personal); } ++$pos; diff --git a/functions/mailbox_display.php b/functions/mailbox_display.php index 73b1ee60..36c64339 100644 --- a/functions/mailbox_display.php +++ b/functions/mailbox_display.php @@ -93,8 +93,8 @@ function printMessageInfo($imapConnection, $t, $not_last=true, $key, $mailbox, } } - - $subject = processSubject(decodeHeader($msg['SUBJECT']), $indent_array[$msg['ID']]); + $subject_full = decodeHeader($msg['SUBJECT']); + $subject = processSubject($subject_full, $indent_array[$msg['ID']]); echo html_tag( 'tr','','','','VALIGN="top"') . "\n"; @@ -209,11 +209,11 @@ function printMessageInfo($imapConnection, $t, $not_last=true, $key, $mailbox, $td_str .= '\n\n" . '
'; @@ -548,7 +549,7 @@ function decodeHeader ($string, $utfencode=true,$htmlsave=true) { if (is_array($string)) { $string = implode("\n", $string); } - + if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { $string = $languages[$squirrelmail_language]['XTRA_CODE']('decodeheader', $string); @@ -562,7 +563,7 @@ function decodeHeader ($string, $utfencode=true,$htmlsave=true) { $aString[$i] = ''; while (preg_match('/^(.*)=\?([^?]*)\?(Q|B)\?([^?]*)\?=(.*)$/Ui',$chunk,$res)) { //$aString[$i] .= $res[1]; - //echo "match ". $res[5] . "
"; + //echo "$chunk match ". $res[5] . "
"; $encoding = ucfirst($res[3]); switch ($encoding) { @@ -581,7 +582,7 @@ function decodeHeader ($string, $utfencode=true,$htmlsave=true) { $replace = charset_decode($res[2], $replace); } else { if ($htmlsave) { - $replace = htmlspecialchars($res[4]); + $replace = htmlspecialchars($replace); } } $aString[$i] .= $replace; @@ -599,11 +600,8 @@ function decodeHeader ($string, $utfencode=true,$htmlsave=true) { } ++$i; } - if ($htmlsave) { - return implode(' ',$aString); - } else { - return implode (' ',$aString); - } + return implode (' ',$aString); + } /* @@ -618,117 +616,115 @@ function encodeHeader ($string) { function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { return $languages[$squirrelmail_language]['XTRA_CODE']('encodeheader', $string); } + if (strtolower($default_charset) == 'iso-8859-1') { + $string = str_replace("\240",' ',$string); + } // Encode only if the string contains 8-bit characters or =? $j = strlen($string); - $l = strstr($string, '=?'); // Must be encoded ? $max_l = 75 - strlen($default_charset) - 7; $aRet = array(); $ret = ''; - $enc = false; + $iEncStart = $enc_init = false; $cur_l = $iOffset = 0; for($i = 0; $i < $j; ++$i) { - switch($string{$i}) { - case '=': - $cur_l+=3; - if ($cur_l > $max_l) { - if ($enc) { - $aRet[] = "=?$default_charset?Q?$ret?="; - $enc = false; - } else { - $aRet[] = substr($string,$iOffset,$i-$iOffset+1); - } - $iOffset = $i+1; - $cur_l = 3; - $ret = ''; - } - $ret .= '=3D'; - break; - case '?': - $cur_l+=3; - if ($cur_l > $max_l) { - if ($enc) { - $aRet[] = "=?$default_charset?Q?$ret?="; - $enc = false; - } else { - $aRet[] = substr($string,$iOffset,$i-$iOffset+1); - } - $iOffset = $i+1; - $cur_l = 3; - $ret = ''; - } - $ret .= '=3F'; - break; - case '_': - $cur_l+=3; - if ($cur_l > $max_l) { - if ($enc) { - $aRet[] = "=?$default_charset?Q?$ret?="; - $enc = false; - } else { - $aRet[] = substr($string,$iOffset,$i-$iOffset+1); - } - $iOffset = $i+1; - $cur_l = 3; - $ret = ''; - } - $ret .= '=5F'; - break; - case ' ': + switch($string{$i}) + { + case '=': + case '<': + case '>': + case ',': + case '?': + case '_': + if ($iEncStart === false) { + $iEncStart = $i; + } + $cur_l+=3; + if ($cur_l > ($max_l-2)) { + $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset); + $aRet[] = "=?$default_charset?Q?$ret?="; + $iOffset = $i; + $cur_l = 0; + $ret = ''; + $iEncStart = false; + } else { + $ret .= sprintf("=%02X",ord($string{$i})); + } + break; + case '(': + case ')': + if ($iEncStart !== false) { + $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset); + $aRet[] = "=?$default_charset?Q?$ret?="; + $iOffset = $i; + $cur_l = 0; + $ret = ''; + $iEncStart = false; + } + break; + case ' ': + if ($iEncStart !== false) { $cur_l++; if ($cur_l > $max_l) { - if ($enc) { - $aRet[] = "=?$default_charset?Q?$ret?="; - $enc = false; - } else { - $aRet[] = substr($string,$iOffset,$i-$iOffset+1); - } - $iOffset = $i+1; - $cur_l = 1; - $ret = ''; - } - $ret .= '_'; - break; - default: - $k = ord($string{$i}); - if ($k > 126) { - $enc = true; - $s = sprintf("=%02X", $k); - $cur_l += strlen($s); - if ($cur_l > $max_l) { + $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset); + $aRet[] = "=?$default_charset?Q?$ret?="; + $iOffset = $i; + $cur_l = 0; + $ret = ''; + $iEncStart = false; + } else { + $ret .= '_'; + } + } + break; + default: + $k = ord($string{$i}); + if ($k > 126) { + if ($iEncStart === false) { + $iEncStart = $i; + } + $cur_l += 3; + if ($cur_l > ($max_l-2)) { + if ($iEncStart !== false) { + $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset); $aRet[] = "=?$default_charset?Q?$ret?="; - $cur_l = strlen($s); - $ret = ''; - $l = false; - $enc = false; - $iOffset = $i+1; - } - $ret .= $s; - $l = TRUE; - } else { + } else { + $aRet[] = substr($string,$iOffset,$i-$iOffset); + } + $cur_l = 3; + $ret = ''; + $iOffset = $i; + } + $enc_init = true; + $ret .= sprintf("=%02X", $k); + } else { + if ($iEncStart !== false) { $cur_l++; if ($cur_l > $max_l) { - if ($enc) { - $aRet[] = "=?$default_charset?Q?$ret?="; - $enc = false; - } else { - $aRet[] = substr($ret,$iOffset,$i-$iOffset+1); - } - $iOffset = $i+1; - $l = false; - $cur_l = 1; + $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset); + $aRet[] = "=?$default_charset?Q?$ret?="; + $iEncStart = false; + $iOffset = $i; + $cur_l = 0; $ret = ''; - } - $ret .= $string{$i}; + } else { + $ret .= $string{$i}; + } } - break; + } + break; } } - if ($enc) { - $string = implode('',$aRet) . "=?$default_charset?Q?$ret?="; + if ($enc_init) { + if ($iEncStart !== false) { + $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset); + $aRet[] = "=?$default_charset?Q?$ret?="; + } else { + $aRet[] = substr($string,$iOffset); + } + $string = implode('',$aRet); } - return $string; } -- 2.25.1