class FilterHtmlImageSecure
Same name and namespace in other branches
- 11.x core/modules/filter/src/Plugin/Filter/FilterHtmlImageSecure.php \Drupal\filter\Plugin\Filter\FilterHtmlImageSecure
- 10 core/modules/filter/src/Plugin/Filter/FilterHtmlImageSecure.php \Drupal\filter\Plugin\Filter\FilterHtmlImageSecure
- 9 core/modules/filter/src/Plugin/Filter/FilterHtmlImageSecure.php \Drupal\filter\Plugin\Filter\FilterHtmlImageSecure
- 8.9.x core/modules/filter/src/Plugin/Filter/FilterHtmlImageSecure.php \Drupal\filter\Plugin\Filter\FilterHtmlImageSecure
Provides a filter to restrict images to site.
Attributes
#[Filter(id: "filter_html_image_secure", title: new TranslatableMarkup("Restrict images to this site"), description: new TranslatableMarkup("Disallows usage of <img> tag sources that are not hosted on this site by replacing them with a placeholder image."), type: FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE, weight: 9)]
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements \Drupal\Component\Plugin\PluginInspectionInterface, \Drupal\Component\Plugin\DerivativeInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses \Drupal\Core\DependencyInjection\AutowiredInstanceTrait, \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait extends \Drupal\Component\Plugin\PluginBase
- class \Drupal\filter\Plugin\FilterBase implements \Drupal\filter\Plugin\FilterInterface extends \Drupal\Core\Plugin\PluginBase
- class \Drupal\filter\Plugin\Filter\FilterHtmlImageSecure implements \Drupal\Core\Plugin\ContainerFactoryPluginInterface extends \Drupal\filter\Plugin\FilterBase
- class \Drupal\filter\Plugin\FilterBase implements \Drupal\filter\Plugin\FilterInterface extends \Drupal\Core\Plugin\PluginBase
- class \Drupal\Core\Plugin\PluginBase uses \Drupal\Core\DependencyInjection\AutowiredInstanceTrait, \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait extends \Drupal\Component\Plugin\PluginBase
Expanded class hierarchy of FilterHtmlImageSecure
File
-
core/
modules/ filter/ src/ Plugin/ Filter/ FilterHtmlImageSecure.php, line 19
Namespace
Drupal\filter\Plugin\FilterView source
class FilterHtmlImageSecure extends FilterBase implements ContainerFactoryPluginInterface {
/**
* The file URL generator service.
*/
protected FileUrlGeneratorInterface $fileUrlGenerator;
/**
* The module handler service.
*/
protected ModuleHandlerInterface $moduleHandler;
/**
* The app root directory path.
*/
protected string $root;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ?FileUrlGeneratorInterface $file_url_generator = NULL, ?ModuleHandlerInterface $module_handler = NULL, #[Autowire(param: 'app.root')] ?string $root = NULL) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
if (!$file_url_generator) {
@trigger_error('Calling ' . __METHOD__ . '() without the $file_url_generator argument is deprecated in drupal:11.4.0 and will be required in drupal:12.0.0. See https://www.drupal.org/node/3566774', E_USER_DEPRECATED);
$file_url_generator = \Drupal::service('file_url_generator');
}
$this->fileUrlGenerator = $file_url_generator;
if (!$module_handler) {
@trigger_error('Calling ' . __METHOD__ . '() without the $module_handler argument is deprecated in drupal:11.4.0 and will be required in drupal:12.0.0. See https://www.drupal.org/node/3566774', E_USER_DEPRECATED);
$module_handler = \Drupal::moduleHandler();
}
$this->moduleHandler = $module_handler;
if (!$root) {
@trigger_error('Calling ' . __METHOD__ . '() without the $root argument is deprecated in drupal:11.4.0 and will be required in drupal:12.0.0. See https://www.drupal.org/node/3566774', E_USER_DEPRECATED);
$root = \Drupal::root();
}
$this->root = $root;
}
/**
* {@inheritdoc}
*/
public function process($text, $langcode) {
// Find the path (e.g. '/') to Drupal root.
$base_path = base_path();
$base_path_length = mb_strlen($base_path);
// Find the directory on the server where index.php resides.
$local_dir = $this->root . '/';
$html_dom = Html::load($text);
$images = $html_dom->getElementsByTagName('img');
foreach ($images as $image) {
$src = $image->getAttribute('src');
// Transform absolute image URLs to relative image URLs: prevent problems
// on multisite set-ups and prevent mixed content errors.
$image->setAttribute('src', $this->fileUrlGenerator
->transformRelative($src));
// Verify that $src starts with $base_path.
// This also ensures that external images cannot be referenced.
$src = $image->getAttribute('src');
if (mb_substr($src, 0, $base_path_length) === $base_path) {
// Remove the $base_path to get the path relative to the Drupal root.
// Ensure the path refers to an actual image by prefixing the image
// source with the Drupal root and running getimagesize() on it.
$local_image_path = $local_dir . mb_substr($src, $base_path_length);
$local_image_path = rawurldecode($local_image_path);
if (@getimagesize($local_image_path)) {
// The image has the right path. Invalid images are handled below.
continue;
}
}
// Allow modules and themes to replace an invalid image with an error
// indicator.
// @see \Drupal\filter\Hook\FilterHooks::filterSecureImageAlter()
$this->moduleHandler
->alter('filter_secure_image', $image);
}
return new FilterProcessResult(Html::serialize($html_dom));
}
/**
* {@inheritdoc}
*/
public function tips($long = FALSE) {
return $this->t('Only images hosted on this site may be used in <img> tags.');
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
|---|---|---|---|---|---|
| AutowiredInstanceTrait::createInstanceAutowired | public static | function | Instantiates a new instance of the implementing class using autowiring. | ||
| AutowiredInstanceTrait::getAutowireArguments | private static | function | Resolves arguments for a method using autowiring. | ||
| DependencySerializationTrait::$_entityStorages | protected | property | An array of entity type IDs keyed by the property name of their storages. | ||
| DependencySerializationTrait::$_serviceIds | protected | property | An array of service IDs keyed by property name used for serialization. | ||
| DependencySerializationTrait::__sleep | public | function | 2 | ||
| DependencySerializationTrait::__wakeup | public | function | 2 | ||
| FilterBase::$provider | public | property | The name of the provider that owns this filter. | ||
| FilterBase::$settings | public | property | An associative array containing the configured settings of this filter. | ||
| FilterBase::$status | public | property | A Boolean indicating whether this filter is enabled. | ||
| FilterBase::$weight | public | property | The weight of this filter compared to others in a filter collection. | ||
| FilterBase::calculateDependencies | public | function | Calculates dependencies for the configured plugin. | Overrides DependentPluginInterface::calculateDependencies | 1 |
| FilterBase::defaultConfiguration | public | function | Gets default configuration for this plugin. | Overrides ConfigurableInterface::defaultConfiguration | |
| FilterBase::getConfiguration | public | function | Gets this plugin's configuration. | Overrides ConfigurableInterface::getConfiguration | |
| FilterBase::getDescription | public | function | Returns the administrative description for this filter plugin. | Overrides FilterInterface::getDescription | |
| FilterBase::getHTMLRestrictions | public | function | Returns HTML allowed by this filter's configuration. | Overrides FilterInterface::getHTMLRestrictions | 4 |
| FilterBase::getLabel | public | function | Returns the administrative label for this filter plugin. | Overrides FilterInterface::getLabel | |
| FilterBase::getType | public | function | Returns the processing type of this filter plugin. | Overrides FilterInterface::getType | |
| FilterBase::prepare | public | function | Prepares the text for processing. | Overrides FilterInterface::prepare | |
| FilterBase::setConfiguration | public | function | Sets the configuration for this plugin instance. | Overrides ConfigurableInterface::setConfiguration | 1 |
| FilterBase::settingsForm | public | function | Generates a filter's settings form. | Overrides FilterInterface::settingsForm | 3 |
| FilterHtmlImageSecure::$fileUrlGenerator | protected | property | The file URL generator service. | ||
| FilterHtmlImageSecure::$moduleHandler | protected | property | The module handler service. | ||
| FilterHtmlImageSecure::$root | protected | property | The app root directory path. | ||
| FilterHtmlImageSecure::process | public | function | Performs the filter processing. | Overrides FilterInterface::process | |
| FilterHtmlImageSecure::tips | public | function | Generates a filter's tip. | Overrides FilterBase::tips | |
| FilterHtmlImageSecure::__construct | public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | Overrides FilterBase::__construct | |
| FilterInterface::TYPE_HTML_RESTRICTOR | constant | HTML tag and attribute restricting filters to prevent XSS attacks. | |||
| FilterInterface::TYPE_MARKUP_LANGUAGE | constant | Non-HTML markup language filters that generate HTML. | |||
| FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE | constant | Irreversible transformation filters. | |||
| FilterInterface::TYPE_TRANSFORM_REVERSIBLE | constant | Reversible transformation filters. | |||
| MessengerTrait::$messenger | protected | property | The messenger. | 27 | |
| MessengerTrait::messenger | public | function | Gets the messenger. | 27 | |
| MessengerTrait::setMessenger | public | function | Sets the messenger. | ||
| PluginBase::$configuration | protected | property | Configuration information passed into the plugin. | 1 | |
| PluginBase::$pluginDefinition | protected | property | The plugin implementation definition. | 1 | |
| PluginBase::$pluginId | protected | property | The plugin ID. | ||
| PluginBase::create | public static | function | Instantiates a new instance of the implementing class using autowiring. | 70 | |
| PluginBase::DERIVATIVE_SEPARATOR | constant | A string which is used to separate base plugin IDs from the derivative ID. | |||
| PluginBase::getBaseId | public | function | Gets the base_plugin_id of the plugin instance. | Overrides DerivativeInspectionInterface::getBaseId | |
| PluginBase::getDerivativeId | public | function | Gets the derivative_id of the plugin instance. | Overrides DerivativeInspectionInterface::getDerivativeId | |
| PluginBase::getPluginDefinition | public | function | Gets the definition of the plugin implementation. | Overrides PluginInspectionInterface::getPluginDefinition | 2 |
| PluginBase::getPluginId | public | function | Gets the plugin ID of the plugin instance. | Overrides PluginInspectionInterface::getPluginId | |
| StringTranslationTrait::$stringTranslation | protected | property | The string translation service. | 3 | |
| StringTranslationTrait::formatPlural | protected | function | Formats a string containing a count of items. | ||
| StringTranslationTrait::getNumberOfPlurals | protected | function | Returns the number of plurals supported by a given language. | ||
| StringTranslationTrait::getStringTranslation | protected | function | Gets the string translation service. | ||
| StringTranslationTrait::setStringTranslation | public | function | Sets the string translation service to use. | 2 | |
| StringTranslationTrait::t | protected | function | Translates a string to the current language or to a given language. | 1 |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.