html.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. function sanitize($textToSanitize,
  3. $removedTags = array('script', 'iframe', 'input', 'form'),
  4. $keptAttributes = array('title', 'href', 'src'),
  5. $keptText = array()){
  6. $htmlContent = str_get_html($textToSanitize);
  7. foreach($htmlContent->find('*[!b38fd2b1fe7f4747d6b1c1254ccd055e]') as $element) {
  8. if(in_array($element->tag, $keptText)) {
  9. $element->outertext = $element->plaintext;
  10. } elseif(in_array($element->tag, $removedTags)) {
  11. $element->outertext = '';
  12. } else {
  13. foreach($element->getAllAttributes() as $attributeName => $attribute) {
  14. if(!in_array($attributeName, $keptAttributes))
  15. $element->removeAttribute($attributeName);
  16. }
  17. }
  18. }
  19. return $htmlContent;
  20. }
  21. function backgroundToImg($htmlContent) {
  22. $regex = '/background-image[ ]{0,}:[ ]{0,}url\([\'"]{0,}(.*?)[\'"]{0,}\)/';
  23. $htmlContent = str_get_html($htmlContent);
  24. foreach($htmlContent->find('*[!b38fd2b1fe7f4747d6b1c1254ccd055e]') as $element) {
  25. if(preg_match($regex, $element->style, $matches) > 0) {
  26. $element->outertext = '<img style="display:block;" src="' . $matches[1] . '" />';
  27. }
  28. }
  29. return $htmlContent;
  30. }
  31. function defaultLinkTo($content, $server){
  32. foreach($content->find('img') as $image) {
  33. $image->src = urljoin($server, $image->src);
  34. }
  35. foreach($content->find('a') as $anchor) {
  36. $anchor->href = urljoin($server, $anchor->href);
  37. }
  38. return $content;
  39. }