class FilterSpamspan in SpamSpan filter 8
Same name and namespace in other branches
- 8.2 src/Plugin/Filter/FilterSpamspan.php \Drupal\spamspan\Plugin\Filter\FilterSpamspan
Provides a filter to obfuscate email addresses.
Plugin annotation
@Filter(
id = "filter_spamspan",
title = @Translation("SpamSpan email address encoding filter"),
description = @Translation("Attempt to hide email addresses from spam-bots."),
type = Drupal\filter\Plugin\FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
settings = {
"spamspan_at" = " [at] ",
"spamspan_use_graphic" = 0,
"spamspan_dot_enable" = 0,
"spamspan_dot" = " [dot] ",
"spamspan_use_form" = 0,
"spamspan_form_pattern" = "<a href=""%url?goto=%email"">%displaytext</a>",
"spamspan_form_default_url" = "contact",
"spamspan_form_default_displaytext" = "contact form",
"spamspan_parse_dom" = 0
}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\filter\Plugin\FilterBase implements FilterInterface
- class \Drupal\spamspan\Plugin\Filter\FilterSpamspan implements SpamspanInterface uses SpamspanDomTrait, SpamspanSettingsFormTrait, SpamspanTrait
- class \Drupal\filter\Plugin\FilterBase implements FilterInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of FilterSpamspan
File
- src/
Plugin/ Filter/ FilterSpamspan.php, line 33
Namespace
Drupal\spamspan\Plugin\FilterView source
class FilterSpamspan extends FilterBase implements SpamspanInterface {
use SpamspanTrait;
use SpamspanDomTrait;
use SpamspanSettingsFormTrait;
/**
* Inline images.
*/
const PATTERN_IMG_INLINE = '/data\\:(?:.+?)base64(?:.+?)(?=["|\'])/';
const PATTERN_IMG_PLACEHOLDER = '__spamspan_img_placeholder__';
/**
* If text was altered by this filter, set variable below to TRUE.
*
* @var bool
*/
protected $textAltered = FALSE;
/**
* {@inheritdoc}
*/
public function tips($long = FALSE) {
return $this
->t('Each email address will be obfuscated in a human readable fashion or, if JavaScript is enabled, replaced with a spam resistent clickable link. Email addresses will get the default web form unless specified. If replacement text (a persons name) is required a webform is also required. Separate each part with the "|" pipe symbol. Replace spaces in names with "_".');
}
/**
* {@inheritdoc}
*/
public function process($text, $langcode) {
$this->textAltered = FALSE;
// HTML image tags need to be handled separately, as they may contain base64
// encoded images slowing down the email regex function.
// Therefore, remove all image contents and add them back later.
// See https://drupal.org/node/1243042 for details.
$images = [
[],
];
preg_match_all(self::PATTERN_IMG_INLINE, $text, $images);
$text = preg_replace(self::PATTERN_IMG_INLINE, self::PATTERN_IMG_PLACEHOLDER, $text);
if (!empty($this->settings['spamspan_parse_dom'])) {
$text = $this
->processAsDom($text, $this->textAltered);
}
else {
$text = $this
->processAsText($text, $this->textAltered);
}
// Revert back to the original image contents.
foreach ($images[0] as $image) {
$text = preg_replace('/' . self::PATTERN_IMG_PLACEHOLDER . '/', $image, $text, 1);
}
$result = new FilterProcessResult($text);
if ($this->textAltered) {
$result
->addAttachments([
'library' => [
'spamspan/obfuscate',
],
]);
if ($this->settings['spamspan_use_graphic']) {
$result
->addAttachments([
'library' => [
'spamspan/atsign',
],
]);
}
}
return $result;
}
/**
* Replaces email addresses using regex.
*
* @param string $text
* Input text.
* @param bool $altered
* Set to true if any replacements happen.
*
* @return string
* Output text.
*/
protected function processAsText($text, &$altered) {
$text = $this
->replaceMailtoLinks($text, $altered);
if (!empty($this->settings['spamspan_use_form'])) {
$text = $this
->replaceEmailAddressesWithOptions($text, $altered);
}
$text = $this
->replaceBareEmailAddresses($text, $altered);
return $text;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FilterBase:: |
public | property | The name of the provider that owns this filter. | |
FilterBase:: |
public | property | An associative array containing the configured settings of this filter. | |
FilterBase:: |
public | property | A Boolean indicating whether this filter is enabled. | |
FilterBase:: |
public | property | The weight of this filter compared to others in a filter collection. | |
FilterBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
1 |
FilterBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
|
FilterBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
FilterBase:: |
public | function |
Returns the administrative description for this filter plugin. Overrides FilterInterface:: |
|
FilterBase:: |
public | function |
Returns HTML allowed by this filter's configuration. Overrides FilterInterface:: |
4 |
FilterBase:: |
public | function |
Returns the administrative label for this filter plugin. Overrides FilterInterface:: |
|
FilterBase:: |
public | function |
Returns the processing type of this filter plugin. Overrides FilterInterface:: |
|
FilterBase:: |
public | function |
Prepares the text for processing. Overrides FilterInterface:: |
|
FilterBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
1 |
FilterBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
4 |
FilterInterface:: |
constant | HTML tag and attribute restricting filters to prevent XSS attacks. | ||
FilterInterface:: |
constant | Non-HTML markup language filters that generate HTML. | ||
FilterInterface:: |
constant | Irreversible transformation filters. | ||
FilterInterface:: |
constant | Reversible transformation filters. | ||
FilterSpamspan:: |
protected | property | If text was altered by this filter, set variable below to TRUE. | |
FilterSpamspan:: |
constant | Inline images. | ||
FilterSpamspan:: |
constant | |||
FilterSpamspan:: |
public | function |
Performs the filter processing. Overrides FilterInterface:: |
|
FilterSpamspan:: |
protected | function | Replaces email addresses using regex. | |
FilterSpamspan:: |
public | function |
Generates a filter's tip. Overrides FilterBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
SpamspanDomTrait:: |
protected | function | Load text as DOM Document. | |
SpamspanDomTrait:: |
protected | function | Replaces email addresses using DOM and regex. | |
SpamspanDomTrait:: |
protected | function | Replace DOM node with another one created from text. | |
SpamspanDomTrait:: |
protected | function | Convert DOM Document back to html. | |
SpamspanInterface:: |
constant | |||
SpamspanInterface:: |
constant | For cases when spamspan_use_form is checked. | ||
SpamspanInterface:: |
constant | Regex for mailto URLs. | ||
SpamspanInterface:: |
constant | Set up a regex constant to split an email into name and domain parts. | ||
SpamspanInterface:: |
constant | Special characters in the local part. | ||
SpamspanSettingsFormTrait:: |
public | function | Returns the value of a setting, or its default value if absent. | |
SpamspanSettingsFormTrait:: |
public | function | Attach our validation. | |
SpamspanSettingsFormTrait:: |
public | function | ||
SpamspanSettingsFormTrait:: |
public | function | Validate settings form. | 1 |
SpamspanTrait:: |
public | function | Callback function for preg_replace_callback. | |
SpamspanTrait:: |
public | function | Callback function for preg_replace_callback. | |
SpamspanTrait:: |
public | function | Callback function for preg_replace_callback. | |
SpamspanTrait:: |
protected | function | Clean up the contents of <a> tag. | |
SpamspanTrait:: |
protected | function | A helper function for the callbacks. | |
SpamspanTrait:: |
protected | function | A version of $this->output method. | |
SpamspanTrait:: |
public | function | ||
SpamspanTrait:: |
public | function | ||
SpamspanTrait:: |
public | function | ||
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |