2016-09-25 23:58:52 +02:00
|
|
|
<?php
|
2017-02-14 17:28:07 +01:00
|
|
|
function sanitize($textToSanitize,
|
|
|
|
$removedTags = array('script', 'iframe', 'input', 'form'),
|
|
|
|
$keptAttributes = array('title', 'href', 'src'),
|
|
|
|
$keptText = array()){
|
2016-09-25 23:58:52 +02:00
|
|
|
$htmlContent = str_get_html($textToSanitize);
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($htmlContent->find('*[!b38fd2b1fe7f4747d6b1c1254ccd055e]') as $element) {
|
|
|
|
if(in_array($element->tag, $keptText)) {
|
2016-09-25 23:58:52 +02:00
|
|
|
$element->outertext = $element->plaintext;
|
2017-07-29 19:28:00 +02:00
|
|
|
} elseif(in_array($element->tag, $removedTags)) {
|
2016-09-25 23:58:52 +02:00
|
|
|
$element->outertext = '';
|
|
|
|
} else {
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($element->getAllAttributes() as $attributeName => $attribute) {
|
2016-09-25 23:58:52 +02:00
|
|
|
if(!in_array($attributeName, $keptAttributes))
|
|
|
|
$element->removeAttribute($attributeName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $htmlContent;
|
|
|
|
}
|
|
|
|
|
2017-03-03 15:13:29 +01: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 13:41:45 +01:00
|
|
|
function defaultLinkTo($content, $server){
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($content->find('img') as $image) {
|
2018-08-02 06:31:56 +02:00
|
|
|
$image->src = urljoin($server, $image->src);
|
2016-09-25 23:58:52 +02:00
|
|
|
}
|
2017-02-18 13:40:58 +01:00
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($content->find('a') as $anchor) {
|
2018-08-02 06:31:56 +02:00
|
|
|
$anchor->href = urljoin($server, $anchor->href);
|
2017-02-18 13:40:58 +01:00
|
|
|
}
|
|
|
|
|
2016-09-25 23:58:52 +02:00
|
|
|
return $content;
|
|
|
|
}
|