ImgRequired.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. // must be called POST validation
  3. /**
  4. * Transform that supplies default values for the src and alt attributes
  5. * in img tags, as well as prevents the img tag from being removed
  6. * because of a missing alt tag. This needs to be registered as both
  7. * a pre and post attribute transform.
  8. */
  9. class HTMLPurifier_AttrTransform_ImgRequired extends HTMLPurifier_AttrTransform
  10. {
  11. public function transform($attr, $config, $context) {
  12. $src = true;
  13. if (!isset($attr['src'])) {
  14. if ($config->get('Core.RemoveInvalidImg')) return $attr;
  15. $attr['src'] = $config->get('Attr.DefaultInvalidImage');
  16. $src = false;
  17. }
  18. if (!isset($attr['alt'])) {
  19. if ($src) {
  20. $alt = $config->get('Attr.DefaultImageAlt');
  21. if ($alt === null) {
  22. // truncate if the alt is too long
  23. $attr['alt'] = substr(basename($attr['src']),0,40);
  24. } else {
  25. $attr['alt'] = $alt;
  26. }
  27. } else {
  28. $attr['alt'] = $config->get('Attr.DefaultInvalidImageAlt');
  29. }
  30. }
  31. return $attr;
  32. }
  33. }
  34. // vim: et sw=4 sts=4