html.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. if(strpos($image->src, 'http') === false
  34. && strpos($image->src, '//') === false
  35. && strpos($image->src, 'data:') === false)
  36. $image->src = $server . $image->src;
  37. }
  38. foreach($content->find('a') as $anchor) {
  39. if(strpos($anchor->href, 'http') === false
  40. && strpos($anchor->href, '//') === false
  41. && strpos($anchor->href, '#') !== 0
  42. && strpos($anchor->href, '?') !== 0)
  43. $anchor->href = $server . $anchor->href;
  44. }
  45. return $content;
  46. }