Skip to content

Instantly share code, notes, and snippets.

@breadlesscode
Last active January 16, 2020 16:49
Show Gist options
  • Save breadlesscode/2c31fd668c95f5d78f35a1917449b5a9 to your computer and use it in GitHub Desktop.
Save breadlesscode/2c31fd668c95f5d78f35a1917449b5a9 to your computer and use it in GitHub Desktop.
NEOS CMS E-Mail obfuscation fusion implentation
<?php
namespace My\Package\Fusion;
class EmailObfuscationImplementation extends \Neos\Fusion\FusionObjects\DataStructureImplementation
{
const MAILTO_REGEX = '/<a(.*?)href="mailto:(.+?)"(.*?)>(.*?)<\/a>/i';
/**
* Get the glue to insert between items
*
* @return string
*/
public function getValue()
{
return $this->fusionValue('value') ?? '';
}
/**
* Get the glue to insert between items
*
* @return string
*/
public function getRot13Encrypter()
{
return function($hit) {
list(
$wholeString,
$tagContentBeforeHref,
$mailtoEmail,
$tagContentAfterHref,
$linkContent
) = $hit;
if (filter_var($linkContent, FILTER_VALIDATE_EMAIL)) {
$emailLength = strlen($linkContent);
$atPos = strpos($linkContent, '@') + 1;
$linkContent = substr($hit[4], 0, $atPos);
$linkContent.= '<span class="d-none" aria-hidden="true">no-bots</span>';
$linkContent.= substr($hit[4], $atPos, $emailLength);
}
return sprintf(
'<a%shref="mailto:%s"%s>%s</a>',
$tagContentBeforeHref,
str_rot13($mailtoEmail),
$tagContentAfterHref,
$linkContent
);
};
}
/**
* {@inheritdoc}
*
* @return string
*/
public function evaluate()
{
return preg_replace_callback(
self::MAILTO_REGEX,
$this->getRot13Encrypter(),
$this->getValue()
);
}
}
prototype(My.Package:Helper.EmailObfusication) {
@class = "Wysiwyg\\OGE\\CorporateSite\\Fusion\\EmailObfuscationImplementation"
value = ${value}
}
String.prototype.rot13 = function () {
return this.replace(/[a-zA-Z]/g, function(a){
return String.fromCharCode(((a=a.charCodeAt())<91?78:110)>a?a+13:a-13);
});
};
$(document).ready(function(){
$('body').on('click', '[href^="mailto:"]:not(.email-obf__decoded)', function(){
let mailtoHref = $(this).attr('href');
let rotEmail = mailtoHref.substring('mailto:'.length);
$(this).attr('href', 'mailto:' + rotEmail.rot13()).addClass('email-obf__decoded');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment