1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Matecat\SubFiltering\Filters\Sprintf; |
4
|
|
|
|
5
|
|
|
class SprintfLocker { |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Protection tags |
9
|
|
|
*/ |
10
|
|
|
const PRE_LOCK_TAG = '_____########'; |
11
|
|
|
const POST_LOCK_TAG = '########_____'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var string|null |
15
|
|
|
*/ |
16
|
|
|
private ?string $source; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string|null |
20
|
|
|
*/ |
21
|
|
|
private ?string $target; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private array $notAllowedMap; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private array $replacementMap = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* SprintfLocker constructor. |
35
|
|
|
* |
36
|
|
|
* @param string|null $source |
37
|
|
|
* @param string|null $target |
38
|
|
|
*/ |
39
|
88 |
|
public function __construct( ?string $source = null, ?string $target = null ) { |
40
|
88 |
|
$this->source = $source; |
41
|
88 |
|
$this->target = $target; |
42
|
88 |
|
$this->notAllowedMap = $this->createNotAllowedMap(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
88 |
|
private function createNotAllowedMap(): array { |
49
|
88 |
|
$map = []; |
50
|
|
|
|
51
|
88 |
|
$all = include __DIR__ . "/language/all/not_allowed.php"; |
52
|
88 |
|
$map = array_merge( $map, $all ); |
53
|
|
|
|
54
|
88 |
|
if ( $this->source and file_exists( __DIR__ . "/language/" . $this->source . "/not_allowed.php" ) ) { |
55
|
1 |
|
$source = include __DIR__ . "/language/" . $this->source . "/not_allowed.php"; |
56
|
1 |
|
$map = array_merge( $map, $source ); |
57
|
|
|
} |
58
|
|
|
|
59
|
88 |
|
if ( $this->target and file_exists( __DIR__ . "/language/" . $this->target . "/not_allowed.php" ) ) { |
60
|
4 |
|
$target = include __DIR__ . "/language/" . $this->target . "/not_allowed.php"; |
61
|
4 |
|
$map = array_merge( $map, $target ); |
62
|
|
|
} |
63
|
|
|
|
64
|
88 |
|
return $map; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $segment |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
88 |
|
public function lock( string $segment ): string { |
73
|
88 |
|
$replacementMap = $this->createReplacementMap( $segment ); |
74
|
88 |
|
$this->replacementMap = $replacementMap; |
75
|
|
|
|
76
|
88 |
|
return str_replace( array_keys( $replacementMap ), array_values( $replacementMap ), $segment ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $segment |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
88 |
|
public function unlock(string $segment ): string { |
85
|
88 |
|
$replacementMap = $this->replacementMap; |
86
|
|
|
|
87
|
88 |
|
return str_replace( array_values( $replacementMap ), array_keys( $replacementMap ), $segment ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Create the replacement map |
92
|
|
|
* |
93
|
|
|
* @param string $segment |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
88 |
|
private function createReplacementMap( string $segment ): array { |
98
|
88 |
|
$replacementMap = []; |
99
|
|
|
|
100
|
88 |
|
foreach ( $this->notAllowedMap as $item => $details ) { |
101
|
|
|
|
102
|
4 |
|
$type = $details[ 'type' ]; |
103
|
|
|
|
104
|
|
|
switch ( $type ) { |
105
|
4 |
|
case 'exact': |
106
|
2 |
|
$replacementMap[ $item ] = self::PRE_LOCK_TAG . $this->maskString( $item ) . self::POST_LOCK_TAG; |
107
|
2 |
|
break; |
108
|
|
|
|
109
|
2 |
|
case 'regex': |
110
|
2 |
|
preg_match_all( '/' . $item . '/', $segment, $matches ); |
111
|
|
|
|
112
|
2 |
|
foreach ( $matches[ 0 ] as $match ) { |
113
|
2 |
|
$replacementMap[ $match ] = self::PRE_LOCK_TAG . $this->maskString( $match ) . self::POST_LOCK_TAG; |
114
|
|
|
} |
115
|
2 |
|
break; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
88 |
|
return $replacementMap; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $string |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
4 |
|
private function maskString( string $string ): string { |
128
|
4 |
|
return str_replace( [ '%', '-', '_' ], '', $string ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
} |