1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\AntiSpamBundle\Twig\Extension; |
13
|
|
|
|
14
|
|
|
use Twig\Extension\AbstractExtension; |
15
|
|
|
use Twig\TwigFilter; |
16
|
|
|
|
17
|
|
|
final class StringTwigExtension extends AbstractExtension |
18
|
|
|
{ |
19
|
|
|
private const MAIL_HTML_PATTERN = '/\<a(?:[^>]+)href\=\"mailto\:([^">]+)\"(?:[^>]*)\>(.*?)\<\/a\>/ism'; |
20
|
|
|
private const MAIL_TEXT_PATTERN = '/(([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\.([A-Z]{2,4})(\((.+?)\))?)/i'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string|null |
24
|
|
|
*/ |
25
|
|
|
private $mailCssClass; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string[] |
29
|
|
|
*/ |
30
|
|
|
private $mailAtText; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string[] |
34
|
|
|
*/ |
35
|
|
|
private $mailDotText; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string[] $mailAtText |
39
|
|
|
* @param string[] $mailDotText |
40
|
|
|
*/ |
41
|
|
|
public function __construct(?string $mailCssClass, array $mailAtText, array $mailDotText) |
42
|
|
|
{ |
43
|
|
|
$this->mailCssClass = $mailCssClass; |
44
|
|
|
$this->mailAtText = $mailAtText; |
45
|
|
|
$this->mailDotText = $mailDotText; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getFilters() |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
|
|
new TwigFilter('antispam', [$this, 'antispam'], [ |
52
|
|
|
'is_safe' => ['html'], |
53
|
|
|
]), |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Replaces E-Mail addresses with an alternative text representation. |
59
|
|
|
* |
60
|
|
|
* @param string $string input string |
61
|
|
|
* @param bool $html Secure html or text |
62
|
|
|
* |
63
|
|
|
* @return string with replaced links |
64
|
|
|
*/ |
65
|
|
|
public function antispam(string $string, bool $html = true): string |
66
|
|
|
{ |
67
|
|
|
if ($html) { |
68
|
|
|
return preg_replace_callback(self::MAIL_HTML_PATTERN, [$this, 'encryptMail'], $string) ?: ''; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return preg_replace_callback(self::MAIL_TEXT_PATTERN, [$this, 'encryptMailText'], $string) ?: ''; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string[] $matches |
76
|
|
|
*/ |
77
|
|
|
private function encryptMailText(array $matches): string |
78
|
|
|
{ |
79
|
|
|
$email = $matches[1]; |
80
|
|
|
|
81
|
|
|
return $this->getSecuredName($email). |
82
|
|
|
$this->mailAtText[array_rand($this->mailAtText)]. |
83
|
|
|
$this->getSecuredName($email, true); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string[] $matches |
88
|
|
|
*/ |
89
|
|
|
private function encryptMail(array $matches): string |
90
|
|
|
{ |
91
|
|
|
[, $email, $text] = $matches; |
|
|
|
|
92
|
|
|
|
93
|
|
|
if ($text === $email) { |
|
|
|
|
94
|
|
|
$text = ''; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return |
98
|
|
|
'<span'.(null !== $this->mailCssClass ? ' class="'.$this->mailCssClass.'"' : '').'>'. |
99
|
|
|
'<span>'.$this->getSecuredName($email).'</span>'. |
100
|
|
|
$this->mailAtText[array_rand($this->mailAtText)]. |
101
|
|
|
'<span>'.$this->getSecuredName($email, true).'</span>'. |
102
|
|
|
('' !== $text ? ' (<span>'.$text.'</span>)' : ''). |
|
|
|
|
103
|
|
|
'</span>'; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function getSecuredName(string $name, bool $isDomain = false): string |
107
|
|
|
{ |
108
|
|
|
$index = strpos($name, '@'); |
109
|
|
|
|
110
|
|
|
\assert(false !== $index && -1 !== $index); |
111
|
|
|
|
112
|
|
|
if ($isDomain) { |
113
|
|
|
$name = substr($name, $index + 1); |
114
|
|
|
} else { |
115
|
|
|
$name = substr($name, 0, $index); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return str_replace('.', $this->mailDotText[array_rand($this->mailDotText)], $name ?: ''); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.