|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the class {\Mailcode\Mailcode_Parser_StringPreProcessor}. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Mailcode |
|
6
|
|
|
* @subpackage Parser |
|
7
|
|
|
* @see \Mailcode\Mailcode_Parser_StringPreProcessor |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Mailcode; |
|
13
|
|
|
|
|
14
|
|
|
use AppUtils\ConvertHelper; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Prepares a string to be parsed by the parser, by rendering |
|
18
|
|
|
* it compatible through a number of adjustments. This includes |
|
19
|
|
|
* stripping out `style` tags in HTML strings, since CSS syntax |
|
20
|
|
|
* conflicts with Mailcode. |
|
21
|
|
|
* |
|
22
|
|
|
* @package Mailcode |
|
23
|
|
|
* @subpackage Parser |
|
24
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class Mailcode_Parser_StringPreProcessor |
|
27
|
|
|
{ |
|
28
|
|
|
public const LITERAL_BRACKET_RIGHT_REPLACEMENT = '﴿'; |
|
29
|
|
|
public const LITERAL_BRACKET_LEFT_REPLACEMENT = '﴾'; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $subject; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct(string $subject) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->subject = $subject; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function process() : string |
|
41
|
|
|
{ |
|
42
|
|
|
$this->stripStyleTags(); |
|
43
|
|
|
$this->escapeRegexBrackets(); |
|
44
|
|
|
|
|
45
|
|
|
return $this->subject; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function escapeRegexBrackets() : void |
|
49
|
|
|
{ |
|
50
|
|
|
preg_match_all('/{[0-9]+,[0-9]+}|{[0-9]+}/xU', $this->subject, $result, PREG_PATTERN_ORDER); |
|
51
|
|
|
|
|
52
|
|
|
$matches = array_unique($result[0]); |
|
53
|
|
|
|
|
54
|
|
|
foreach ($matches as $match) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->subject = $this->replaceBrackets($this->subject, $match); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Removes all <style> tags to avoid conflicts with CSS code. |
|
62
|
|
|
*/ |
|
63
|
|
|
private function stripStyleTags() : void |
|
64
|
|
|
{ |
|
65
|
|
|
if(!ConvertHelper::isStringHTML($this->subject)) |
|
66
|
|
|
{ |
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->subject = preg_replace( |
|
71
|
|
|
'%<style\b[^>]*>(.*?)</style>%six', |
|
72
|
|
|
'', |
|
73
|
|
|
$this->subject |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function replaceBrackets(string $subject, string $needle) : string |
|
78
|
|
|
{ |
|
79
|
|
|
$replacement = str_replace( |
|
80
|
|
|
array('{', '}'), |
|
81
|
|
|
array( |
|
82
|
|
|
self::LITERAL_BRACKET_LEFT_REPLACEMENT, |
|
83
|
|
|
self::LITERAL_BRACKET_RIGHT_REPLACEMENT |
|
84
|
|
|
), |
|
85
|
|
|
$needle |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
return str_replace($needle, $replacement, $subject); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|