/** * convert blockquotes to quotes ("> ") and strip tags * * this function uses tidy or DOM to recursivly walk the dom tree of the html mail * @see http://php.net/manual/de/tidy.root.php * @see http://php.net/manual/en/book.dom.php * * @param tidyNode|DOMNode $_node * @param integer $_quoteIndent * @param string $_eol * @return string * * @todo we can transform more tags here, i.e. the <strong>BOLDTEXT</strong> tag could be replaced with *BOLDTEXT* * @todo think about removing the tidy code * @todo reduce complexity */ public static function addQuotesAndStripTags($_node, $_quoteIndent = 0, $_eol = "\n") { $result = ''; $hasChildren = $_node instanceof DOMNode ? $_node->hasChildNodes() : $_node->hasChildren(); $nameProperty = $_node instanceof DOMNode ? 'nodeName' : 'name'; $valueProperty = $_node instanceof DOMNode ? 'nodeValue' : 'value'; $divNewline = FALSE; if ($hasChildren) { $lastChild = NULL; $children = $_node instanceof DOMNode ? $_node->childNodes : $_node->child; if ($_node->{$nameProperty} == 'div') { $divNewline = TRUE; } foreach ($children as $child) { $isTextLeaf = $child instanceof DOMNode ? $child->{$nameProperty} == '#text' : !$child->{$nameProperty}; if ($isTextLeaf) { // leaf -> add quotes and append to content string if ($_quoteIndent > 0) { $result .= str_repeat(self::QUOTE, $_quoteIndent) . $child->{$valueProperty}; } else { if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . "value: " . $child->{$valueProperty} . " / name: " . $_node->{$nameProperty} . "\n"); } if ($divNewline) { $result .= $_eol . str_repeat(self::QUOTE, $_quoteIndent); $divNewline = FALSE; } $result .= $child->{$valueProperty}; } } else { if ($child->{$nameProperty} == 'blockquote') { // opening blockquote $_quoteIndent++; } else { if ($child->{$nameProperty} == 'br') { if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . "value: " . $child->{$valueProperty} . " / name: " . $_node->{$nameProperty} . "\n"); } // reset quoted state on newline if ($lastChild !== NULL && $lastChild->{$nameProperty} == 'br') { // add quotes to repeating newlines $result .= str_repeat(self::QUOTE, $_quoteIndent); } $result .= $_eol; $divNewline = FALSE; } } } $result .= self::addQuotesAndStripTags($child, $_quoteIndent, $_eol); if ($child->{$nameProperty} == 'blockquote') { // closing blockquote $_quoteIndent--; // add newline after last closing blockquote if ($_quoteIndent == 0) { $result .= $_eol; } } $lastChild = $child; } // add newline if closing div if ($divNewline) { $result .= $_eol . str_repeat(self::QUOTE, $_quoteIndent); } } return $result; }