2016-09-26 00:58:52 +03:00
|
|
|
<?php
|
2017-02-14 19:28:07 +03:00
|
|
|
function sanitize($textToSanitize,
|
|
|
|
$removedTags = array('script', 'iframe', 'input', 'form'),
|
|
|
|
$keptAttributes = array('title', 'href', 'src'),
|
|
|
|
$keptText = array()){
|
2016-09-26 00:58:52 +03:00
|
|
|
$htmlContent = str_get_html($textToSanitize);
|
|
|
|
|
2017-07-29 20:28:00 +03:00
|
|
|
foreach($htmlContent->find('*[!b38fd2b1fe7f4747d6b1c1254ccd055e]') as $element) {
|
|
|
|
if(in_array($element->tag, $keptText)) {
|
2016-09-26 00:58:52 +03:00
|
|
|
$element->outertext = $element->plaintext;
|
2017-07-29 20:28:00 +03:00
|
|
|
} elseif(in_array($element->tag, $removedTags)) {
|
2016-09-26 00:58:52 +03:00
|
|
|
$element->outertext = '';
|
|
|
|
} else {
|
2017-07-29 20:28:00 +03:00
|
|
|
foreach($element->getAllAttributes() as $attributeName => $attribute) {
|
2016-09-26 00:58:52 +03:00
|
|
|
if(!in_array($attributeName, $keptAttributes))
|
|
|
|
$element->removeAttribute($attributeName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $htmlContent;
|
|
|
|
}
|
|
|
|
|
2017-03-03 17:13:29 +03:00
|
|
|
function backgroundToImg($htmlContent) {
|
|
|
|
|
|
|
|
$regex = '/background-image[ ]{0,}:[ ]{0,}url\([\'"]{0,}(.*?)[\'"]{0,}\)/';
|
|
|
|
$htmlContent = str_get_html($htmlContent);
|
|
|
|
|
|
|
|
foreach($htmlContent->find('*[!b38fd2b1fe7f4747d6b1c1254ccd055e]') as $element) {
|
|
|
|
|
|
|
|
if(preg_match($regex, $element->style, $matches) > 0) {
|
|
|
|
|
|
|
|
$element->outertext = '<img style="display:block;" src="' . $matches[1] . '" />';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $htmlContent;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-18 15:41:45 +03:00
|
|
|
function defaultLinkTo($content, $server){
|
2017-07-29 20:28:00 +03:00
|
|
|
foreach($content->find('img') as $image) {
|
2018-08-02 07:31:56 +03:00
|
|
|
$image->src = urljoin($server, $image->src);
|
2016-09-26 00:58:52 +03:00
|
|
|
}
|
2017-02-18 15:40:58 +03:00
|
|
|
|
2017-07-29 20:28:00 +03:00
|
|
|
foreach($content->find('a') as $anchor) {
|
2018-08-02 07:31:56 +03:00
|
|
|
$anchor->href = urljoin($server, $anchor->href);
|
2017-02-18 15:40:58 +03:00
|
|
|
}
|
|
|
|
|
2016-09-26 00:58:52 +03:00
|
|
|
return $content;
|
|
|
|
}
|