From 599e29dee14454f349a815e8518ae508dc6cfd4c Mon Sep 17 00:00:00 2001 From: Klaus Leithoff Date: Thu, 24 Sep 2015 10:53:02 +0000 Subject: [PATCH] fix problem with possible json_encode problem (removing all initial data from mailbody) on reply/forward --- mail/inc/class.mail_compose.inc.php | 32 +++--------------- mail/inc/class.mail_ui.inc.php | 46 ++------------------------ phpgwapi/inc/class.translation.inc.php | 36 ++++++++++++++++++++ 3 files changed, 43 insertions(+), 71 deletions(-) diff --git a/mail/inc/class.mail_compose.inc.php b/mail/inc/class.mail_compose.inc.php index e69f407da1..4645b994ea 100644 --- a/mail/inc/class.mail_compose.inc.php +++ b/mail/inc/class.mail_compose.inc.php @@ -1109,28 +1109,7 @@ class mail_compose // prepare body // in a way, this tests if we are having real utf-8 (the displayCharset) by now; we should if charsets reported (or detected) are correct - if (strtoupper($this->displayCharset) == 'UTF-8') - { - $test = @json_encode($content['body']); - //error_log(__METHOD__.__LINE__.' ->'.strlen($singleBodyPart['body']).' Error:'.json_last_error().'<- BodyPart:#'.$test.'#'); - //if (json_last_error() != JSON_ERROR_NONE && strlen($singleBodyPart['body'])>0) - if ($test=="null" && strlen($content['body'])>0) - { - // try to fix broken utf8 - $x = (function_exists('mb_convert_encoding')?mb_convert_encoding($content['body'],'UTF-8','UTF-8'):(function_exists('iconv')?@iconv("UTF-8","UTF-8//IGNORE",$content['body']):$content['body'])); - $test = @json_encode($x); - if ($test=="null" && strlen($content['body'])>0) - { - // this should not be needed, unless something fails with charset detection/ wrong charset passed - error_log(__METHOD__.__LINE__.' Charset problem detected; Charset Detected:'.mail_bo::detect_encoding($content['body'])); - $content['body'] = utf8_encode($content['body']); - } - else - { - $content['body'] = $x; - } - } - } + $content['body'] = translation::convert_jsonsafe($content['body'],'utf-8'); //error_log(__METHOD__.__LINE__.array2string($content)); // get identities of all accounts as "$acc_id:$ident_id" => $identity @@ -1621,7 +1600,7 @@ class mail_compose $bodyParts[$i]['body'] = "
".$bodyParts[$i]['body']."
"; } if ($bodyParts[$i]['charSet']===false) $bodyParts[$i]['charSet'] = mail_bo::detect_encoding($bodyParts[$i]['body']); - $bodyParts[$i]['body'] = translation::convert($bodyParts[$i]['body'], $bodyParts[$i]['charSet']); + $bodyParts[$i]['body'] = translation::convert_jsonsafe($bodyParts[$i]['body'], $bodyParts[$i]['charSet']); #error_log( "GetDraftData (HTML) CharSet:".mb_detect_encoding($bodyParts[$i]['body'] . 'a' , strtoupper($bodyParts[$i]['charSet']).','.strtoupper($this->displayCharset).',UTF-8, ISO-8859-1')); $this->sessionData['body'] .= ($i>0?"
":""). $bodyParts[$i]['body'] ; } @@ -1635,7 +1614,7 @@ class mail_compose $this->sessionData['body'] .= "
"; } if ($bodyParts[$i]['charSet']===false) $bodyParts[$i]['charSet'] = mail_bo::detect_encoding($bodyParts[$i]['body']); - $bodyParts[$i]['body'] = translation::convert($bodyParts[$i]['body'], $bodyParts[$i]['charSet']); + $bodyParts[$i]['body'] = translation::convert_jsonsafe($bodyParts[$i]['body'], $bodyParts[$i]['charSet']); #error_log( "GetDraftData (Plain) CharSet".mb_detect_encoding($bodyParts[$i]['body'] . 'a' , strtoupper($bodyParts[$i]['charSet']).','.strtoupper($this->displayCharset).',UTF-8, ISO-8859-1')); $this->sessionData['body'] .= ($i>0?"\r\n":""). $bodyParts[$i]['body'] ; } @@ -2085,7 +2064,7 @@ class mail_compose $_htmlConfig = mail_bo::$htmLawed_config; mail_bo::$htmLawed_config['comment'] = 2; mail_bo::$htmLawed_config['transform_anchor'] = false; - $this->sessionData['body'] .= "
".self::_getCleanHTML(translation::convert($bodyParts[$i]['body'], $bodyParts[$i]['charSet'])); + $this->sessionData['body'] .= "
".self::_getCleanHTML(translation::convert_jsonsafe($bodyParts[$i]['body'], $bodyParts[$i]['charSet'])); mail_bo::$htmLawed_config = $_htmlConfig; #error_log( "GetReplyData (HTML) CharSet:".mb_detect_encoding($bodyParts[$i]['body'] . 'a' , strtoupper($bodyParts[$i]['charSet']).','.strtoupper($this->displayCharset).',UTF-8, ISO-8859-1')); } @@ -2108,8 +2087,7 @@ class mail_compose } // add line breaks to $bodyParts - if ($bodyParts[$i]['charSet']===false) $bodyParts[$i]['charSet'] = mail_bo::detect_encoding($bodyParts[$i]['body']); - $newBody = translation::convert($bodyParts[$i]['body'], $bodyParts[$i]['charSet']); + $newBody = translation::convert_jsonsafe($bodyParts[$i]['body'],$bodyParts[$i]['charSet']); #error_log( "GetReplyData (Plain) CharSet:".mb_detect_encoding($bodyParts[$i]['body'] . 'a' , strtoupper($bodyParts[$i]['charSet']).','.strtoupper($this->displayCharset).',UTF-8, ISO-8859-1')); $newBody = mail_ui::resolve_inline_images($newBody, $_folder, $_uid, $_partID, 'plain'); $this->sessionData['body'] .= "\r\n"; diff --git a/mail/inc/class.mail_ui.inc.php b/mail/inc/class.mail_ui.inc.php index a9745f56d5..c44da59b2d 100644 --- a/mail/inc/class.mail_ui.inc.php +++ b/mail/inc/class.mail_ui.inc.php @@ -1961,23 +1961,7 @@ class mail_ui foreach ($attachments as $key => $value) { $attachmentHTML[$key]['filename']= ($value['name'] ? ( $value['filename'] ? $value['filename'] : $value['name'] ) : lang('(no subject)')); - $test = @json_encode($attachmentHTML[$key]['filename']); - //error_log(__METHOD__.__LINE__.' ->'.strlen($singleBodyPart['body']).' Error:'.json_last_error().'<- BodyPart:#'.$test.'#'); - if (($test=="null" || $test === false || is_null($test)) && strlen($attachmentHTML[$key]['filename'])>0) - { - // try to fix broken utf8 - $x = utf8_encode($attachmentHTML[$key]['filename']); - $test = @json_encode($x); - if (($test=="null" || $test === false || is_null($test)) && strlen($attachmentHTML[$key]['filename'])>0) - { - // this should not be needed, unless something fails with charset detection/ wrong charset passed - $attachmentHTML[$key]['filename'] = (function_exists('mb_convert_encoding')?mb_convert_encoding($attachmentHTML[$key]['filename'],'UTF-8','UTF-8'):(function_exists('iconv')?@iconv("UTF-8","UTF-8//IGNORE",$attachmentHTML[$key]['filename']):$attachmentHTML[$key]['filename'])); - } - else - { - $attachmentHTML[$key]['filename'] = $x; - } - } + $attachmentHTML[$key]['filename'] = translation::convert_jsonsafe($attachmentHTML[$key]['filename'],'utf-8'); //error_log(array2string($value)); //error_log(strtoupper($value['mimeType']) .'<->'. mime_magic::filename2mime($attachmentHTML[$key]['filename'])); if (strtoupper($value['mimeType']=='APPLICATION/OCTET-STREAM')) $value['mimeType'] = mime_magic::filename2mime($attachmentHTML[$key]['filename']); @@ -2749,33 +2733,7 @@ class mail_ui { $singleBodyPart['body'] = preg_replace($sar,$rar,$singleBodyPart['body']); } - if ($singleBodyPart['charSet']===false) $singleBodyPart['charSet'] = translation::detect_encoding($singleBodyPart['body']); - $singleBodyPart['body'] = $GLOBALS['egw']->translation->convert( - $singleBodyPart['body'], - strtolower($singleBodyPart['charSet']) - ); - // in a way, this tests if we are having real utf-8 (the displayCharset) by now; we should if charsets reported (or detected) are correct - if (strtoupper(mail_bo::$displayCharset) == 'UTF-8') - { - $test = @json_encode($singleBodyPart['body']); - //error_log(__METHOD__.__LINE__.' ->'.strlen($singleBodyPart['body']).' Error:'.json_last_error().'<- BodyPart:#'.$test.'#'); - if (($test=="null" || $test === false || is_null($test)) && strlen($singleBodyPart['body'])>0) - { - // try to fix broken utf8 - $x = (function_exists('mb_convert_encoding')?mb_convert_encoding($singleBodyPart['body'],'UTF-8','UTF-8'):(function_exists('iconv')?@iconv("UTF-8","UTF-8//IGNORE",$singleBodyPart['body']):$singleBodyPart['body'])); - $test = @json_encode($x); - if (($test=="null" || $test === false || is_null($test)) && strlen($singleBodyPart['body'])>0) - { - // this should not be needed, unless something fails with charset detection/ wrong charset passed - error_log(__METHOD__.__LINE__.' Charset Reported:'.$singleBodyPart['charSet'].' Charset Detected:'.translation::detect_encoding($singleBodyPart['body'])); - $singleBodyPart['body'] = utf8_encode($singleBodyPart['body']); - } - else - { - $singleBodyPart['body'] = $x; - } - } - } + $singleBodyPart['body'] = translation::convert_jsonsafe($singleBodyPart['body'],$singleBodyPart['charSet']); //error_log(__METHOD__.__LINE__.array2string($singleBodyPart)); if($singleBodyPart['mimeType'] == 'text/plain') { diff --git a/phpgwapi/inc/class.translation.inc.php b/phpgwapi/inc/class.translation.inc.php index 27b4ee50df..16fe0a1b15 100644 --- a/phpgwapi/inc/class.translation.inc.php +++ b/phpgwapi/inc/class.translation.inc.php @@ -921,6 +921,42 @@ class translation return $data; } + /** + * converts a string $data from charset $from to something that is json_encode tested + * + * @param string/array $data string(s) to convert + * @param string/boolean $from charset $data is in or False if it should be detected + * @return string/array converted string(s) from $data + */ + static function convert_jsonsafe($data,$from=False) + { + if ($from===false) $from = self::detect_encoding($data); + $data = self::convert($data,strtolower($from)); + // in a way, this tests if we are having real utf-8 (the displayCharset) by now; we should if charsets reported (or detected) are correct + if (strtoupper(self::charset()) == 'UTF-8') + { + $test = @json_encode($data); + //error_log(__METHOD__.__LINE__.' ->'.strlen($data).' Error:'.json_last_error().'<- data:#'.$test.'#'); + if (($test=="null" || $test === false || is_null($test)) && strlen($data)>0) + { + // try to fix broken utf8 + $x = (function_exists('mb_convert_encoding')?mb_convert_encoding($data,'UTF-8','UTF-8'):(function_exists('iconv')?@iconv("UTF-8","UTF-8//IGNORE",$data):$data)); + $test = @json_encode($x); + if (($test=="null" || $test === false || is_null($test)) && strlen($data)>0) + { + // this should not be needed, unless something fails with charset detection/ wrong charset passed + error_log(__METHOD__.__LINE__.' Charset Reported:'.$from.' Charset Detected:'.translation::detect_encoding($data)); + $data = utf8_encode($data); + } + else + { + $data = $x; + } + } + } + return $data; + } + /** * insert/update/delete one phrase in the lang-table *