From f1574678888fe8ccc636730d20193f4c6d4866e3 Mon Sep 17 00:00:00 2001 From: nathan Date: Thu, 16 Jan 2025 10:44:10 -0700 Subject: [PATCH] Fix merge newline issues discovered in tracker notifications - {{all_comments}} was all in one line --- api/src/Storage/Merge.php | 138 +++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 63 deletions(-) diff --git a/api/src/Storage/Merge.php b/api/src/Storage/Merge.php index 5d350f7629..e20909456b 100644 --- a/api/src/Storage/Merge.php +++ b/api/src/Storage/Merge.php @@ -832,65 +832,7 @@ abstract class Merge return $ret; } - // fix application/msword mimetype for rtf files - if($mimetype == 'application/msword' && strtolower(substr($document, -4)) == '.rtf') - { - $mimetype = 'application/rtf'; - } - - switch($mimetype) - { - case 'application/vnd.oasis.opendocument.text': // open office - case 'application/vnd.oasis.opendocument.spreadsheet': - case 'application/vnd.oasis.opendocument.presentation': - case 'application/vnd.oasis.opendocument.text-template': - case 'application/vnd.oasis.opendocument.spreadsheet-template': - case 'application/vnd.oasis.opendocument.presentation-template': - case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': // ms office 2007 - case 'application/vnd.ms-word.document.macroenabled.12': - case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': - case 'application/vnd.ms-excel.sheet.macroenabled.12': - case 'application/xml': - case 'text/xml': - case 'text/html': - $this->is_xml = true; - break; - } - - switch($mimetype) - { - case 'application/rtf': - case 'text/rtf': - $this->line_feed = '}\par \pard\plain{'; - break; - case 'application/vnd.oasis.opendocument.text': - case 'application/vnd.oasis.opendocument.presentation': - case 'application/vnd.oasis.opendocument.text-template': - case 'application/vnd.oasis.opendocument.presentation-template': - $this->line_feed = ''; - break; - case 'application/vnd.oasis.opendocument.spreadsheet': // open office calc - case 'application/vnd.oasis.opendocument.spreadsheet-template': - $this->line_feed = ''; - break; - case 'application/xmlExcel.Sheet': // Excel 2003 - $this->line_feed = ' '; - break; - case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': - case 'application/vnd.ms-word.document.macroenabled.12': - case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': - case 'application/vnd.ms-excel.sheet.macroenabled.12': - $this->line_feed = ''; - break; - case 'application/xml'; - $this->line_feed = ''; - break; - case 'text/html': - $this->line_feed = "
"; - break; - default: - $this->line_feed = "\n"; - } + $this->apply_mimetype($mimetype, $document); try { @@ -1000,6 +942,73 @@ abstract class Merge } } + /** + * Use a different mimetype. Sets this.is_xml & this.line_feed + */ + private function apply_mimetype($mimetype, $document = '') + { + // fix application/msword mimetype for rtf files + if($mimetype == 'application/msword' && strtolower(substr($document, -4)) == '.rtf') + { + $mimetype = 'application/rtf'; + } + + switch($mimetype) + { + case 'application/vnd.oasis.opendocument.text': // open office + case 'application/vnd.oasis.opendocument.spreadsheet': + case 'application/vnd.oasis.opendocument.presentation': + case 'application/vnd.oasis.opendocument.text-template': + case 'application/vnd.oasis.opendocument.spreadsheet-template': + case 'application/vnd.oasis.opendocument.presentation-template': + case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': // ms office 2007 + case 'application/vnd.ms-word.document.macroenabled.12': + case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': + case 'application/vnd.ms-excel.sheet.macroenabled.12': + case 'application/xml': + case 'text/xml': + case 'text/html': + $this->is_xml = true; + break; + } + + switch($mimetype) + { + case 'application/rtf': + case 'text/rtf': + $this->line_feed = '}\par \pard\plain{'; + break; + case 'application/vnd.oasis.opendocument.text': + case 'application/vnd.oasis.opendocument.presentation': + case 'application/vnd.oasis.opendocument.text-template': + case 'application/vnd.oasis.opendocument.presentation-template': + $this->line_feed = ''; + break; + case 'application/vnd.oasis.opendocument.spreadsheet': // open office calc + case 'application/vnd.oasis.opendocument.spreadsheet-template': + $this->line_feed = '
'; + break; + case 'application/xmlExcel.Sheet': // Excel 2003 + $this->line_feed = ' '; + break; + case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': + case 'application/vnd.ms-word.document.macroenabled.12': + case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': + case 'application/vnd.ms-excel.sheet.macroenabled.12': + $this->line_feed = ''; + break; + case 'application/xml'; + $this->line_feed = ''; + break; + case 'text/html': + $this->line_feed = "
"; + break; + default: + $this->line_feed = "\n"; + } + return $mimetype; + } + /** * Merges a given document with contact data * @@ -1043,7 +1052,7 @@ abstract class Merge } // make currently processed mimetype available to class methods; - $this->mimetype = $mimetype; + $this->mimetype = $this->apply_mimetype($mimetype); // fix garbled placeholders if($fix && is_array($fix)) @@ -1481,12 +1490,15 @@ abstract class Merge } else { - $value = $cleaned; + // Strip some specific stuff to avoid the extra new lines + $value = str_replace(["\n", "\n\n\n", "\n", + "\n", "\n"], '', $cleaned); } } // replace

and
with CRLF (remove

and CRLF) - $value = strip_tags(str_replace(array("\r", "\n", '

', '

', '
', '
', '
'), - array('', '', '', "\r\n", '', "\r\n", "\r\n"), $value + $value = strip_tags(str_replace(array("\r", '

', "

\n", '

', '
', '
', + '
'), + array('', '', "\n", "\n", '', "\n", "\n"), $value ), implode('', $replace_tags) );