2006-08-14 23:11:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// must be called POST validation
|
|
|
|
|
2006-08-20 21:55:28 +00:00
|
|
|
/**
|
2007-06-20 21:39:28 +00:00
|
|
|
* Transform that supplies default values for the src and alt attributes
|
|
|
|
* in img tags, as well as prevents the img tag from being removed
|
|
|
|
* because of a missing alt tag. This needs to be registered as both
|
|
|
|
* a pre and post attribute transform.
|
2006-08-20 21:55:28 +00:00
|
|
|
*/
|
2006-08-14 23:11:28 +00:00
|
|
|
class HTMLPurifier_AttrTransform_ImgRequired extends HTMLPurifier_AttrTransform
|
|
|
|
{
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
/**
|
|
|
|
* @param array $attr
|
|
|
|
* @param HTMLPurifier_Config $config
|
|
|
|
* @param HTMLPurifier_Context $context
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function transform($attr, $config, $context)
|
|
|
|
{
|
2006-08-14 23:11:28 +00:00
|
|
|
$src = true;
|
2006-08-17 20:29:34 +00:00
|
|
|
if (!isset($attr['src'])) {
|
2013-07-16 11:56:14 +00:00
|
|
|
if ($config->get('Core.RemoveInvalidImg')) {
|
|
|
|
return $attr;
|
|
|
|
}
|
2009-02-20 00:17:49 +00:00
|
|
|
$attr['src'] = $config->get('Attr.DefaultInvalidImage');
|
2006-08-14 23:11:28 +00:00
|
|
|
$src = false;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-17 20:29:34 +00:00
|
|
|
if (!isset($attr['alt'])) {
|
2006-08-14 23:11:28 +00:00
|
|
|
if ($src) {
|
2009-02-20 00:17:49 +00:00
|
|
|
$alt = $config->get('Attr.DefaultImageAlt');
|
2008-10-06 18:51:03 +00:00
|
|
|
if ($alt === null) {
|
2010-03-08 06:22:21 +00:00
|
|
|
// truncate if the alt is too long
|
2013-07-16 11:56:14 +00:00
|
|
|
$attr['alt'] = substr(basename($attr['src']), 0, 40);
|
2008-10-06 18:51:03 +00:00
|
|
|
} else {
|
|
|
|
$attr['alt'] = $alt;
|
|
|
|
}
|
2006-08-14 23:11:28 +00:00
|
|
|
} else {
|
2009-02-20 00:17:49 +00:00
|
|
|
$attr['alt'] = $config->get('Attr.DefaultInvalidImageAlt');
|
2006-08-14 23:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
2006-08-17 20:29:34 +00:00
|
|
|
return $attr;
|
2006-08-14 23:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-06 09:24:59 +00:00
|
|
|
// vim: et sw=4 sts=4
|