-
Notifications
You must be signed in to change notification settings - Fork 0
/
MailObfuscator.php
65 lines (52 loc) · 1.55 KB
/
MailObfuscator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
* @copyright Copyright (c) 2015 Stefan Linke
* @license https://github.com/particleflux/yii2-mail-obfuscator/blob/master/LICENSE
* @link https://github.com/particleflux/yii2-mail-obfuscator#readme
*/
namespace particleflux\MailObfuscator;
use yii\base\Widget;
use yii\helpers\Html;
/**
* Class MailObfuscator
* @package particleflux\MailObfuscator
* @author Stefan Linke <mail@particleflux.de>
*/
class MailObfuscator extends Widget
{
/** @var string email to obfuscate */
public $email;
/** @var string text to display as link, defaults to email */
public $text;
/**
* @var array html options for the anchor tag
*/
public $options = [];
public function run()
{
if ($this->text === null) {
$this->text = $this->email;
}
$encodedMail = base64_encode('mailto:' . $this->email);
$encodedText = base64_encode($this->text);
$this->view->registerJs(<<<JS
function d(s) {
var e={},i,b=0,c,x,l=0,a,r='',w=String.fromCharCode,L=s.length;
var A="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
for(i=0;i<64;i++){e[A.charAt(i)]=i;}
for(x=0;x<L;x++){
c=e[s.charAt(x)];b=(b<<6)+c;l+=6;
while(l>=8){((a=(b>>>(l-=8))&0xff)||(x<(L-2)))&&(r+=w(a));}
}
return r;
}
var a = document.getElementById('{$this->id}');
var m = d('{$encodedMail}');
a.href = m;
a.innerHTML = d('{$encodedText}');
JS
);
$this->options = array_merge(['id' => $this->id], $this->options);
echo Html::a('', null, $this->options);
}
}