-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed dependency on propaganistas package.
- Loading branch information
1 parent
f45e0f1
commit efc083e
Showing
7 changed files
with
170 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |