| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | namespace Aurora\Modules\Calendar; |
| 9: | |
| 10: | class TextUtils |
| 11: | { |
| 12: | public static function clearHtml($sText) |
| 13: | { |
| 14: | $oDom = \MailSo\Base\HtmlUtils::GetDomFromText($sText); |
| 15: | $aNodes = $oDom->getElementsByTagName('*'); |
| 16: | |
| 17: | $bHasExternals = false; |
| 18: | $aFoundCIDs = []; |
| 19: | $aContentLocationUrls = []; |
| 20: | $aFoundedContentLocationUrls = []; |
| 21: | |
| 22: | foreach ($aNodes as $oElement) { |
| 23: | $sTagNameLower = \strtolower($oElement->tagName); |
| 24: | if ('img' === $sTagNameLower) { |
| 25: | $oElement->parentNode->removeChild($oElement); |
| 26: | } |
| 27: | |
| 28: | $sBackground = $oElement->hasAttribute('background') ? \trim($oElement->getAttribute('background')) : ''; |
| 29: | $sBackgroundColor = $oElement->hasAttribute('bgcolor') ? \trim($oElement->getAttribute('bgcolor')) : ''; |
| 30: | |
| 31: | if (!empty($sBackground) || !empty($sBackgroundColor)) { |
| 32: | if (!empty($sBackground)) { |
| 33: | $oElement->removeAttribute('background'); |
| 34: | } |
| 35: | |
| 36: | if (!empty($sBackgroundColor)) { |
| 37: | $oElement->removeAttribute('bgcolor'); |
| 38: | } |
| 39: | } |
| 40: | |
| 41: | $aForbiddenAttributes = array( |
| 42: | 'id', 'class', 'contenteditable', 'designmode', 'formaction', 'data-bind', 'xmlns', 'srcset' |
| 43: | ); |
| 44: | |
| 45: | foreach ($aForbiddenAttributes as $sAttributeName) { |
| 46: | @$oElement->removeAttribute($sAttributeName); |
| 47: | } |
| 48: | |
| 49: | |
| 50: | if (isset($oElement->attributes)) { |
| 51: | foreach ($oElement->attributes as $sAttributeName => $oAttributeNode) { |
| 52: | $sAttributeNameLower = \strtolower($sAttributeName); |
| 53: | if (!!preg_match('/^\s*on.+$/m', $sAttributeNameLower)) { |
| 54: | @$oElement->removeAttribute($sAttributeNameLower); |
| 55: | } |
| 56: | } |
| 57: | } |
| 58: | |
| 59: | if ($oElement->hasAttribute('style')) { |
| 60: | $oElement->setAttribute( |
| 61: | 'style', |
| 62: | \MailSo\Base\HtmlUtils::ClearStyle( |
| 63: | $oElement->getAttribute('style'), |
| 64: | $oElement, |
| 65: | $bHasExternals, |
| 66: | $aFoundCIDs, |
| 67: | $aContentLocationUrls, |
| 68: | $aFoundedContentLocationUrls |
| 69: | ) |
| 70: | ); |
| 71: | } |
| 72: | } |
| 73: | $sText = $oDom->saveHTML(); |
| 74: | unset($oDom); |
| 75: | |
| 76: | $sText = \MailSo\Base\HtmlUtils::ClearTags($sText); |
| 77: | $sText = \MailSo\Base\HtmlUtils::ClearBodyAndHtmlTag($sText); |
| 78: | |
| 79: | return $sText; |
| 80: | } |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | public static function isHtml($sText) |
| 86: | { |
| 87: | preg_match_all('/<\/?[a-zA-Z-]+(?:\s|\s[^>]+|\S)?>/', $sText, $matches, PREG_SET_ORDER); |
| 88: | |
| 89: | return count($matches) > 0; |
| 90: | } |
| 91: | } |
| 92: | |