Skip to content

Commit

Permalink
Version 2.2.0
Browse files Browse the repository at this point in the history
Removed dependency on propaganistas package.
  • Loading branch information
luke-nehemedia committed Aug 25, 2020
1 parent f45e0f1 commit efc083e
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 29 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
- First "stable" release since there is no report of any bugs so far.
- Added a setting "includeJS" which provides the option to disable the automatic addition of the JS-file to the frontend. You can now include it manually, if you want to. Thank you, [@svale](https://github.com/svale) for wirting this code and providing it in a pull request.
- Corrected some translation errors. _Do you want to help? Feel free to add a translation to your language - it's not that much ;-)_

## 2.2.0 - 2020-08-25
- Fixed the error during installation because of the missing packagist-package
- Included Twig-Extension into plugins codebase
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "lucasbares/craft-emailobfuscator",
"description": "A simple plugin that adds a twig tag to obfuscate email addresses (by rot13) in text fields.",
"type": "craft-plugin",
"version": "2.1.0",
"version": "2.2.0",
"keywords": [
"craft",
"cms",
Expand Down Expand Up @@ -30,8 +30,7 @@
}
],
"require": {
"craftcms/cms": "^3.0.0-RC1",
"propaganistas/email-obfuscator": "~1.0"
"craftcms/cms": "^3.0.0-RC1"
},
"autoload": {
"psr-4": {
Expand Down
5 changes: 3 additions & 2 deletions src/CraftEmailobfuscator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @author Lucas Bares
* @package CraftEmailobfuscator
* @since 2.0.0
* @version 2.2.0
*
*/
class CraftEmailobfuscator extends Plugin
Expand Down Expand Up @@ -56,11 +57,11 @@ public function init()

// registering the AssetBundle with propaganistas js file
if( $this->settings->includeJS ) {
$this->view->registerAssetBundle(CraftEmailobfuscatorPropaganistasAssets::class);
$this->view->registerAssetBundle(CraftEmailobfuscatorAssets::class);
}

// registering propaganistas email obfuscator
Craft::$app->view->registerTwigExtension(new \Propaganistas\EmailObfuscator\Twig\Extension);
Craft::$app->view->registerTwigExtension(new twig\ObfuscateExtension);

// show success message with template tag information
Event::on(
Expand Down
42 changes: 42 additions & 0 deletions src/CraftEmailobfuscatorAssets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Craft Emailobfuscator plugin for Craft CMS 3.x
*
* A simple plugin that adds a twig tag to obfuscate email addresses (by rot13) in text fields.
*
* @link http://luke.nehemedia.de
* @copyright Copyright (c) 2018 Lucas Bares
*/

namespace lucasbares\craftemailobfuscator;

use craft\web\AssetBundle;
use craft\web\assets\cp\CpAsset;
use yii\web\View;

/**
* Asset-Bundle with javascript to parse email
*
*
* @author Lucas Bares
* @package CraftEmailobfuscator
* @since 2.2.0
* @version 2.2.0
*
*/
class CraftEmailobfuscatorAssets extends AssetBundle
{
public function init()
{
// path to the original javascript file of propaganistas
$this->sourcePath = '@lucasbares/craftemailobfuscator/assets';

// add the JS-File to the AssetBundle
$this->js = ['EmailObfuscator.js'];

// specify that it should be added to the head of the document
$this->jsOptions = ['position' => View::POS_HEAD];

parent::init();
}
}
24 changes: 0 additions & 24 deletions src/CraftEmailobfuscatorPropaganistasAssets.php

This file was deleted.

1 change: 1 addition & 0 deletions src/assets/EmailObfuscator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 118 additions & 0 deletions src/twig/ObfuscateExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
/**
* Craft Emailobfuscator plugin for Craft CMS 3.x
*
* A simple plugin that adds a twig tag to obfuscate email addresses (by rot13) in text fields.
*
* @link http://luke.nehemedia.de
* @copyright Copyright (c) 2018 Lucas Bares
*/
namespace lucasbares\craftemailobfuscator\twig;

use Twig_Extension;
use Twig_SimpleFilter;


/**
* Twig Extension
*
* Twig extension for "obfuscateEmail" - orgininal by propaganistas (no longer deployed on packagist)
*
* @author Lucas Bares
* @package CraftEmailobfuscator
* @since 2.1.0
*
*/
class ObfuscateExtension extends Twig_Extension
{

/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'craftemailobfuscator.emailObfuscator';
}

/**
* Returns a list of filters to add to the existing list.
*
* @return array An array of filters
*/
public function getFilters()
{
return array(
new Twig_SimpleFilter(
'obfuscateEmail',
array($this, 'parse'),
array('is_safe' => array('html'))
),
);
}

/**
* Twig filter callback.
*
* @return string Filtered content
*/
public function parse($content)
{
return $this->_obfuscateEmail($content);
}

protected function _obfuscateEmail($string){
// Casting $string to a string allows passing of objects implementing the __toString() magic method.
$string = (string) $string;

// Safeguard string.
$safeguard = '$%$!!$%$';

// Safeguard several stuff before parsing.
$prevent = array(
'|<input [^>]*@[^>]*>|is', // <input>
'|(<textarea(?:[^>]*)>)(.*?)(</textarea>)|is', // <textarea>
'|(<head(?:[^>]*)>)(.*?)(</head>)|is', // <head>
'|(<script(?:[^>]*)>)(.*?)(</script>)|is', // <script>
);
foreach ($prevent as $pattern) {
$string = preg_replace_callback($pattern, function ($matches) use ($safeguard) {
return str_replace('@', $safeguard, $matches[0]);
}, $string);
}

// Define patterns for extracting emails.
$patterns = array(
'|\<a[^>]+href\=\"mailto\:([^">?]+)(\?[^?">]+)?\"[^>]*\>(.*?)\<\/a\>|ism', // mailto anchors
'|[_a-z0-9-]+(?:\.[_a-z0-9-]+)*@[a-z0-9-]+(?:\.[a-z0-9-]+)*(?:\.[a-z]{2,3})|i', // plain emails
);

foreach ($patterns as $pattern) {
$string = preg_replace_callback($pattern, function ($parts) use ($safeguard) {
// Clean up element parts.
$parts = array_map('trim', $parts);

// ROT13 implementation for JS-enabled browsers
$js = '<script type="text/javascript">Rot13.write(' . "'" . str_rot13($parts[0]) . "'" . ');</script>';

// Reversed direction implementation for non-JS browsers
if (stripos($parts[0], '<a') === 0) {
// Mailto tag; if link content equals the email, just display the email, otherwise display a formatted string.
$nojs = ($parts[1] == $parts[3]) ? $parts[1] : (' > ' . $parts[1] . ' < ' . $parts[3]);
} else {
// Plain email; display the plain email.
$nojs = $parts[0];
}
$nojs = '<noscript><span style="unicode-bidi:bidi-override;direction:rtl;">' . strrev($nojs) . '</span></noscript>';

// Safeguard the obfuscation so it won't get picked up by the next iteration.
return str_replace('@', $safeguard, $js . $nojs);
}, $string);
}

// Revert all safeguards.
return str_replace($safeguard, '@', $string);
}

}

0 comments on commit efc083e

Please sign in to comment.