File indexing completed on 2024-12-22 05:36:19

0001 <?php
0002 
0003 // must be called POST validation
0004 
0005 /**
0006  * Transform that supplies default values for the src and alt attributes
0007  * in img tags, as well as prevents the img tag from being removed
0008  * because of a missing alt tag. This needs to be registered as both
0009  * a pre and post attribute transform.
0010  */
0011 class HTMLPurifier_AttrTransform_ImgRequired extends HTMLPurifier_AttrTransform
0012 {
0013 
0014     /**
0015      * @param array $attr
0016      * @param HTMLPurifier_Config $config
0017      * @param HTMLPurifier_Context $context
0018      * @return array
0019      */
0020     public function transform($attr, $config, $context)
0021     {
0022         $src = true;
0023         if (!isset($attr['src'])) {
0024             if ($config->get('Core.RemoveInvalidImg')) {
0025                 return $attr;
0026             }
0027             $attr['src'] = $config->get('Attr.DefaultInvalidImage');
0028             $src = false;
0029         }
0030 
0031         if (!isset($attr['alt'])) {
0032             if ($src) {
0033                 $alt = $config->get('Attr.DefaultImageAlt');
0034                 if ($alt === null) {
0035                     $attr['alt'] = basename($attr['src']);
0036                 } else {
0037                     $attr['alt'] = $alt;
0038                 }
0039             } else {
0040                 $attr['alt'] = $config->get('Attr.DefaultInvalidImageAlt');
0041             }
0042         }
0043         return $attr;
0044     }
0045 }
0046 
0047 // vim: et sw=4 sts=4