From 9e005686d13023ae10009be79d3b24a99e9af44a Mon Sep 17 00:00:00 2001 From: Thomas Casteleyn Date: Wed, 17 Apr 2019 17:46:39 +0200 Subject: [PATCH 1/2] Introduce functionality to also get only the new part for HTML mails. --- classes/emailmessage.class.inc.php | 40 +++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/classes/emailmessage.class.inc.php b/classes/emailmessage.class.inc.php index d2aa469..de6b811 100644 --- a/classes/emailmessage.class.inc.php +++ b/classes/emailmessage.class.inc.php @@ -233,6 +233,10 @@ public function GetNewPartHTML($sBodyText = null) $sBodyText = ''.$sBodyText.''; } + $aGlobalDelimiterPatterns = array( + "/\RFrom: .+\RSent: .+\R/m", + "/\RDe : .+\REnvoyé : .+\R/m", + ); // default tags to remove: array of tag_name => array of class names $aTagsToRemove = array( 'blockquote' => array(), @@ -242,14 +246,15 @@ public function GetNewPartHTML($sBodyText = null) if (class_exists('MetaModel')) { - $aTagsToRemove = MetaModel::GetModuleSetting('combodo-email-synchro', 'html-tags-to-remove', $aTagsToRemove); + $aTagsToRemove = MetaModel::GetModuleSetting('combodo-email-synchro', 'html-tags-to-remove', $aTagsToRemove); + $aGlobalDelimiterPatterns = MetaModel::GetModuleSetting('combodo-email-synchro', 'multiline-delimiter-patterns', $aGlobalDelimiterPatterns); } $this->oDoc = new DOMDocument(); $this->oDoc->preserveWhitespace = true; @$this->oDoc->loadHTML(''.$sBodyText); // For loading HTML chunks where the character set is not specified - $this->CleanNode($this->oDoc, $aTagsToRemove); + $this->CleanNode($this->oDoc, $aTagsToRemove, $aGlobalDelimiterPatterns); $oXPath = new DOMXPath($this->oDoc); $sXPath = "//body"; @@ -275,11 +280,11 @@ public function GetNewPartHTML($sBodyText = null) * Clean the given node according to the list of tags / classes to remove * @param DOMNode $oElement * @param unknown $aTagsToRemove + * @param array $aGlobalDelimiterPatterns + * @param bool $bRemoveAll */ - protected function CleanNode(DOMNode $oElement, $aTagsToRemove) + protected function CleanNode(DOMNode $oElement, $aTagsToRemove, $aGlobalDelimiterPatterns, &$bRemoveAll = false) { - $aAttrToRemove = array(); - if ($oElement->hasChildNodes()) { $aChildElementsToRemove = array(); @@ -305,7 +310,7 @@ protected function CleanNode(DOMNode $oElement, $aTagsToRemove) else { // Recurse - $this->CleanNode($oNode, $aTagsToRemove); + $this->CleanNode($oNode, $aTagsToRemove, $aGlobalDelimiterPatterns, $bRemoveAll); } } } @@ -314,10 +319,15 @@ protected function CleanNode(DOMNode $oElement, $aTagsToRemove) // Remove comments $aChildElementsToRemove[] = $oNode; } - else + else if (!$bRemoveAll) { // Recurse - $this->CleanNode($oNode, $aTagsToRemove); + $this->CleanNode($oNode, $aTagsToRemove, $aGlobalDelimiterPatterns, $bRemoveAll); + } + else + { + // Remove all following nodes + $aChildElementsToRemove[] = $oNode; } } @@ -326,7 +336,19 @@ protected function CleanNode(DOMNode $oElement, $aTagsToRemove) { $oElement->removeChild($oDomElement); } - } + + // Search for delimiter patterns + foreach ($aGlobalDelimiterPatterns as $sPattern) + { + if(preg_match($sPattern, $oElement->textContent)) + { + $bRemoveAll = true; + $this->sTrace .= 'Found a match with the global pattern: "'.$sPattern.'"'; + $this->sTrace .= "\n=== ".$oElement->getNodePath()." ===\n".$oElement->textContent."\n=============\n"; + $oElement->parentNode->removeChild($oElement); + } + } + } } /** From d66d0450045e0d7afb5e6970769e8dfe7d640d2b Mon Sep 17 00:00:00 2001 From: Thomas Casteleyn Date: Mon, 14 Dec 2020 11:34:45 +0100 Subject: [PATCH 2/2] Add parameter to enable functionality --- classes/emailmessage.class.inc.php | 6 +++++- module.combodo-email-synchro.php | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/classes/emailmessage.class.inc.php b/classes/emailmessage.class.inc.php index de6b811..83e9e98 100644 --- a/classes/emailmessage.class.inc.php +++ b/classes/emailmessage.class.inc.php @@ -248,7 +248,11 @@ public function GetNewPartHTML($sBodyText = null) { $aTagsToRemove = MetaModel::GetModuleSetting('combodo-email-synchro', 'html-tags-to-remove', $aTagsToRemove); $aGlobalDelimiterPatterns = MetaModel::GetModuleSetting('combodo-email-synchro', 'multiline-delimiter-patterns', $aGlobalDelimiterPatterns); - } + + // Reset the configuration if HTML delimit is not wanted. (default old behaviour) + if (!MetaModel::GetModuleSetting('combodo-email-synchro', 'delimit_html_message', false)) + $aGlobalDelimiterPatterns = array(); + } $this->oDoc = new DOMDocument(); $this->oDoc->preserveWhitespace = true; diff --git a/module.combodo-email-synchro.php b/module.combodo-email-synchro.php index d0d8436..148c3d8 100644 --- a/module.combodo-email-synchro.php +++ b/module.combodo-email-synchro.php @@ -56,6 +56,7 @@ '/\\RDe : .+\\RDate d\'envoi : .+\\R/m', // Outlook French, plain text '/\\R-----Message d\'origine-----\\R/m', ), + 'delimit_html_message' => true, // Delimit the previous message also in the case of HTML messages 'use_message_id_as_uid' => false, // Do NOT change this unless you known what you are doing!! 'images_minimum_size' => '100x20', // Images smaller that these dimensions will be ignored (signatures...) 'images_maximum_size' => '', // Images bigger that these dimensions will be resized before uploading into iTop