Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce functionality to also get only the new part for HTML mails. #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions classes/emailmessage.class.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ public function GetNewPartHTML($sBodyText = null)
$sBodyText = '<html><body>'.$sBodyText.'</body></html>';
}

$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(),
Expand All @@ -242,14 +246,19 @@ 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);

// 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;
@$this->oDoc->loadHTML('<?xml encoding="UTF-8"?>'.$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";
Expand All @@ -275,11 +284,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();
Expand All @@ -305,7 +314,7 @@ protected function CleanNode(DOMNode $oElement, $aTagsToRemove)
else
{
// Recurse
$this->CleanNode($oNode, $aTagsToRemove);
$this->CleanNode($oNode, $aTagsToRemove, $aGlobalDelimiterPatterns, $bRemoveAll);
}
}
}
Expand All @@ -314,10 +323,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;
}
}

Expand All @@ -326,7 +340,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);
}
}
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions module.combodo-email-synchro.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down