ImgRequired.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. /**
  12. * @param array $attr
  13. * @param HTMLPurifier_Config $config
  14. * @param HTMLPurifier_Context $context
  15. * @return array
  16. */
  17. public function transform($attr, $config, $context)
  18. {
  19. $src = true;
  20. if (!isset($attr['src'])) {
  21. if ($config->get('Core.RemoveInvalidImg')) {
  22. return $attr;
  23. }
  24. $attr['src'] = $config->get('Attr.DefaultInvalidImage');
  25. $src = false;
  26. }
  27. if (!isset($attr['alt'])) {
  28. if ($src) {
  29. $alt = $config->get('Attr.DefaultImageAlt');
  30. if ($alt === null) {
  31. $attr['alt'] = basename($attr['src']);
  32. } else {
  33. $attr['alt'] = $alt;
  34. }
  35. } else {
  36. $attr['alt'] = $config->get('Attr.DefaultInvalidImageAlt');
  37. }
  38. }
  39. return $attr;
  40. }
  41. }
  42. // vim: et sw=4 sts=4