From fddc68b16d3b2586c11cda2cda862430e1795dde Mon Sep 17 00:00:00 2001 From: RainLoop Team Date: Thu, 9 Jun 2016 00:41:05 +0300 Subject: [PATCH] Fix html parsing (for PHP <5.3.6) --- package.json | 4 +- .../app/libraries/MailSo/Base/HtmlUtils.php | 159 ++++++++++++------ 2 files changed, 105 insertions(+), 58 deletions(-) diff --git a/package.json b/package.json index 694a23d49..5885ef77c 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,9 @@ "name": "RainLoop", "title": "RainLoop Webmail", "version": "1.10.1", - "release": "123", + "release": "125", "private": true, - "ownCloudPackageVersion": "4.17", + "ownCloudPackageVersion": "4.18", "description": "Simple, modern & fast web-based email client", "homepage": "http://rainloop.net", "main": "gulpfile.js", diff --git a/rainloop/v/0.0.0/app/libraries/MailSo/Base/HtmlUtils.php b/rainloop/v/0.0.0/app/libraries/MailSo/Base/HtmlUtils.php index dca9fe18e..851014b0b 100644 --- a/rainloop/v/0.0.0/app/libraries/MailSo/Base/HtmlUtils.php +++ b/rainloop/v/0.0.0/app/libraries/MailSo/Base/HtmlUtils.php @@ -26,51 +26,6 @@ class HtmlUtils { } - /** - * @param string $sText - * - * @return \DOMDocument|bool - */ - public static function GetDomFromText($sText) - { - $bState = true; - if (\MailSo\Base\Utils::FunctionExistsAndEnabled('libxml_use_internal_errors')) - { - $bState = \libxml_use_internal_errors(true); - } - - $oDom = new \DOMDocument('1.0', 'utf-8'); - $oDom->encoding = 'UTF-8'; - $oDom->strictErrorChecking = false; - $oDom->formatOutput = false; - $oDom->preserveWhiteSpace = false; - - $sHtmlAttrs = $sBodyAttrs = ''; - - $sText = \MailSo\Base\HtmlUtils::FixSchemas($sText); - $sText = \MailSo\Base\HtmlUtils::ClearFastTags($sText); - $sText = \MailSo\Base\HtmlUtils::ClearBodyAndHtmlTag($sText, $sHtmlAttrs, $sBodyAttrs); - - @$oDom->loadHTML('<'.'?xml version="1.0" encoding="utf-8"?'.'>'. - ''. - ''. - '
'.$sText.'
'); - - @$oDom->normalizeDocument(); - - if (\MailSo\Base\Utils::FunctionExistsAndEnabled('libxml_clear_errors')) - { - @\libxml_clear_errors(); - } - - if (\MailSo\Base\Utils::FunctionExistsAndEnabled('libxml_use_internal_errors')) - { - \libxml_use_internal_errors($bState); - } - - return $oDom; - } - /** * @param \DOMElement $oElement * @@ -97,6 +52,104 @@ class HtmlUtils return $aResult; } + /** + * @param string $sText + * + * @return \DOMDocument|bool + */ + public static function GetDomFromText($sText) + { + $bState = true; + if (\MailSo\Base\Utils::FunctionExistsAndEnabled('libxml_use_internal_errors')) + { + $bState = \libxml_use_internal_errors(true); + } + + $sHtmlAttrs = $sBodyAttrs = ''; + + $sText = \MailSo\Base\HtmlUtils::FixSchemas($sText); + $sText = \MailSo\Base\HtmlUtils::ClearFastTags($sText); + $sText = \MailSo\Base\HtmlUtils::ClearBodyAndHtmlTag($sText, $sHtmlAttrs, $sBodyAttrs); + + $oDom = self::createDOMDocument(); + @$oDom->loadHTML('<'.'?xml version="1.0" encoding="utf-8"?'.'>'. + ''. + ''. + '
'.$sText.'
'); + + @$oDom->normalizeDocument(); + + if (\MailSo\Base\Utils::FunctionExistsAndEnabled('libxml_clear_errors')) + { + @\libxml_clear_errors(); + } + + if (\MailSo\Base\Utils::FunctionExistsAndEnabled('libxml_use_internal_errors')) + { + \libxml_use_internal_errors($bState); + } + + return $oDom; + } + + /** + * @return \DOMDocument + */ + private static function createDOMDocument() + { + $oDoc = new \DOMDocument('1.0', 'UTF-8'); + $oDoc->encoding = 'UTF-8'; + $oDoc->strictErrorChecking = false; + $oDoc->formatOutput = false; + $oDoc->preserveWhiteSpace = false; + + return $oDoc; + } + + /** + * @return boolean + */ + private static function comparedVersion() + { + return \version_compare(PHP_VERSION, '5.3.6') >= 0; + } + + /** + * @param \DOMDocument|\DOMElement $oElem + * + * @return type + */ + private static function domToString($oElem, $oDom = null) + { + $sResult = ''; + if ($oElem instanceof \DOMDocument) + { + if (isset($oElem->documentElement) && self::comparedVersion()) + { + $sResult = $oElem->saveHTML($oElem->documentElement); + } + else + { + $sResult = $oElem->saveHTML(); + } + } + else if ($oElem) + { + if ($oDom && self::comparedVersion()) + { + $sResult = $oDom->saveHTML($oElem); + } + else + { + $oTempDoc = self::createDOMDocument(); + $oTempDoc->appendChild($oTempDoc->importNode($oElem->cloneNode(true), true)); + $sResult = $oTempDoc->saveHTML(); + } + } + + return \trim($sResult); + } + /** * @param \DOMDocument $oDom * @param bool $bWrapByFakeHtmlAndBodyDiv = true @@ -138,28 +191,22 @@ class HtmlUtils } $oWrap->appendChild($oDiv); - $sResult = \trim($oDom->saveHTML($oWrap)); + $sResult = self::domToString($oWrap, $oDom); } else { - $sResult = \trim($oDom->saveHTML($oDiv)); - - if (0 === strpos($sResult, '
')) + $sResult = self::domToString($oDiv, $oDom); + if (0 === strpos($sResult, '
') && '
' === substr($sResult, -6)) { $sResult = substr($sResult, 5); - - if ('
' === substr($sResult, -6)) - { - $sResult = substr($sResult, 0, -6); - } - + $sResult = substr($sResult, 0, -6); $sResult = \trim($sResult); } } } else { - $sResult = \trim($oDom->saveHTML()); + $sResult = self::domToString($oDom); } $sResult = \str_replace(\MailSo\Base\HtmlUtils::$KOS, ':', $sResult);