|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the class {@see \Mailcode\Parser\Statement\Tokenizer\SpecialChars}. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Mailcode |
|
6
|
|
|
* @subpackage Parser |
|
7
|
|
|
* @see \Mailcode\Parser\Statement\Tokenizer\SpecialChars |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Mailcode\Parser\Statement\Tokenizer; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Handles the encoding, decoding and escaping of |
|
16
|
|
|
* Mailcode special characters in a string. |
|
17
|
|
|
* |
|
18
|
|
|
* @package Mailcode |
|
19
|
|
|
* @subpackage Parser |
|
20
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class SpecialChars |
|
23
|
|
|
{ |
|
24
|
|
|
public const ESCAPE_CHAR = '\\'; |
|
25
|
|
|
public const PLACEHOLDER_QUOTE = '__QUOTE__'; |
|
26
|
|
|
public const PLACEHOLDER_BRACKET_OPEN = '__BRACKET_OPEN__'; |
|
27
|
|
|
public const PLACEHOLDER_BRACKET_CLOSE = '__BRACKET_CLOSE__'; |
|
28
|
|
|
|
|
29
|
|
|
private static array $charsEncoded = array( |
|
30
|
|
|
'"' => self::PLACEHOLDER_QUOTE, |
|
31
|
|
|
'{' => self::PLACEHOLDER_BRACKET_OPEN, |
|
32
|
|
|
'}' => self::PLACEHOLDER_BRACKET_CLOSE |
|
33
|
|
|
); |
|
34
|
|
|
|
|
35
|
|
|
private static array $charsEscaped = array( |
|
36
|
|
|
'"' => self::ESCAPE_CHAR.'"', |
|
37
|
|
|
'{' => self::ESCAPE_CHAR.'{', |
|
38
|
|
|
'}' => self::ESCAPE_CHAR.'}' |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
|
|
public static function getChars() : array |
|
42
|
|
|
{ |
|
43
|
|
|
return self::$charsEncoded; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Encodes a string for the internal string format, |
|
48
|
|
|
* which uses placeholders for special characters |
|
49
|
|
|
* like quotes and brackets. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $subject |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function encodeAll(string $subject) : string |
|
55
|
|
|
{ |
|
56
|
|
|
$subject = self::decode($subject); |
|
57
|
|
|
|
|
58
|
|
|
return str_replace( |
|
59
|
|
|
array_keys(self::$charsEncoded), |
|
60
|
|
|
array_values(self::$charsEncoded), |
|
61
|
|
|
$subject |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public static function encodeEscaped(string $subject) : string |
|
66
|
|
|
{ |
|
67
|
|
|
return str_replace( |
|
68
|
|
|
array_values(self::$charsEscaped), |
|
69
|
|
|
array_values(self::$charsEncoded), |
|
70
|
|
|
$subject |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Escapes all Mailcode special characters in the |
|
76
|
|
|
* specified string. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $subject |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
public static function escape(string $subject) : string |
|
82
|
|
|
{ |
|
83
|
|
|
// Avoid double-encoding special characters |
|
84
|
|
|
$subject = self::decode($subject); |
|
85
|
|
|
|
|
86
|
|
|
return str_replace( |
|
87
|
|
|
array_keys(self::$charsEscaped), |
|
88
|
|
|
array_values(self::$charsEscaped), |
|
89
|
|
|
$subject |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Decodes a string (including escaped characters) into |
|
95
|
|
|
* its raw form without escaped or encoded characters. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $subject |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
|
|
public static function decode(string $subject) : string |
|
101
|
|
|
{ |
|
102
|
|
|
$replaces = array(); |
|
103
|
|
|
|
|
104
|
|
|
foreach(self::$charsEncoded as $char => $placeholder) |
|
105
|
|
|
{ |
|
106
|
|
|
$escaped = self::$charsEscaped[$char]; |
|
107
|
|
|
|
|
108
|
|
|
$replaces[$escaped] = $char; |
|
109
|
|
|
$replaces[$placeholder] = $char; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return str_replace( |
|
113
|
|
|
array_keys($replaces), |
|
114
|
|
|
array_values($replaces), |
|
115
|
|
|
$subject |
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|