summaryrefslogtreecommitdiff
blob: 503fe797c884348be05a7a6d9911964cb26b233d (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/**
 * Rule compiler for Jetpack Waf.
 *
 * @package automattic/jetpack-waf
 */

namespace Automattic\Jetpack\Waf;

/**
 * Waf_Operators class
 */
class Waf_Operators {
	/**
	 * Returns true if the test string is found at the beginning of the input.
	 *
	 * @param string $input Input.
	 * @param string $test Test.
	 * @return string|false
	 */
	public function begins_with( $input, $test ) {
		if ( '' === $input && '' === $test ) {
			return '';
		}

		return substr( $input, 0, strlen( $test ) ) === $test
		? $test
		: false;
	}

	/**
	 * Returns true if the test string is found anywhere in the input.
	 *
	 * @param string $input Input.
	 * @param string $test Test.
	 * @return string|false
	 */
	public function contains( $input, $test ) {
		if ( empty( $input ) || empty( $test ) ) {
			return false;
		}

		return strpos( $input, $test ) !== false
		? $test
		: false;
	}

	/**
	 * Returns true if the test string with word boundaries is found anywhere in the input.
	 *
	 * @param string $input Input.
	 * @param string $test Test.
	 * @return string|false
	 */
	public function contains_word( $input, $test ) {
		return ( $input === $test || 1 === preg_match( '/\b' . preg_quote( $test, '/' ) . '\b/Ds', $input ) )
		? $test
		: false;
	}

	/**
	 * Returns true if the test string is found at the end of the input.
	 *
	 * @param string $input Input.
	 * @param string $test Test.
	 * @return string|false
	 */
	public function ends_with( $input, $test ) {
		return ( '' === $test || substr( $input, -1 * strlen( $test ) ) === $test )
		? $test
		: false;
	}

	/**
	 * Returns true if the input value is equal to the test value.
	 * If either value cannot be converted to an int it will be treated as 0.
	 *
	 * @param mixed $input Input.
	 * @param mixed $test Test.
	 * @return int|false
	 */
	public function eq( $input, $test ) {
		return intval( $input ) === intval( $test )
		? $input
		: false;
	}

	/**
	 * Returns true if the input value is greater than or equal to the test value.
	 * If either value cannot be converted to an int it will be treated as 0.
	 *
	 * @param mixed $input Input.
	 * @param mixed $test Test.
	 * @return int|false
	 */
	public function ge( $input, $test ) {
		return intval( $input ) >= intval( $test )
		? $input
		: false;
	}

	/**
	 * Returns true if the input value is greater than the test value.
	 * If either value cannot be converted to an int it will be treated as 0.
	 *
	 * @param mixed $input Input.
	 * @param mixed $test Test.
	 * @return int|false
	 */
	public function gt( $input, $test ) {
		return intval( $input ) > intval( $test )
		? $input
		: false;
	}

	/**
	 * Returns true if the input value is less than or equal to the test value.
	 * If either value cannot be converted to an int it will be treated as 0.
	 *
	 * @param mixed $input Input.
	 * @param mixed $test Test.
	 * @return int|false
	 */
	public function le( $input, $test ) {
		return intval( $input ) <= intval( $test )
		? $input
		: false;
	}

	/**
	 * Returns true if the input value is less than the test value.
	 * If either value cannot be converted to an int it will be treated as 0.
	 *
	 * @param mixed $input Input.
	 * @param mixed $test Test.
	 * @return int|false
	 */
	public function lt( $input, $test ) {
		return intval( $input ) < intval( $test )
		? $input
		: false;
	}

	/**
	 * Returns false.
	 *
	 * @return false
	 */
	public function no_match() {
		return false;
	}

	/**
	 * Uses a multi-string matching algorithm to search through $input for a number of given $words.
	 *
	 * @param string   $input Input.
	 * @param string[] $words \AhoCorasick\MultiStringMatcher $matcher.
	 * @return string[]|false Returns the words that were found in $input, or FALSE if no words were found.
	 */
	public function pm( $input, $words ) {
		$results = $this->get_multi_string_matcher( $words )->searchIn( $input );

		return isset( $results[0] )
		? array_map(
			function ( $r ) {
				return $r[1]; },
			$results
		)
		: false;
	}

	/**
	 * The last-used pattern-matching algorithm.
	 *
	 * @var array
	 */
	private $last_multi_string_matcher = array( null, null );

	/**
	 * Creates a matcher that uses the Aho-Corasick algorithm to efficiently find a number of words in an input string.
	 * Caches the last-used matcher so that the same word list doesn't have to be compiled multiple times.
	 *
	 * @param string[] $words Words.
	 * @return \AhoCorasick\MultiStringMatcher
	 */
	private function get_multi_string_matcher( $words ) {
		// only create a new matcher entity if we don't have one already for this word list.
		if ( $this->last_multi_string_matcher[0] !== $words ) {
			$this->last_multi_string_matcher = array( $words, new \AhoCorasick\MultiStringMatcher( $words ) );
		}

		return $this->last_multi_string_matcher[1];
	}

	/**
	 * Performs a regular expression match on the input subject using the given pattern.
	 * Returns false if the pattern does not match, or the substring(s) of the input
	 * that were matched by the pattern.
	 *
	 * @param string $subject Subject.
	 * @param string $pattern Pattern.
	 * @return string[]|false
	 */
	public function rx( $subject, $pattern ) {
		$matched = preg_match( $pattern, $subject, $matches );
		return 1 === $matched
			? $matches
			: false;
	}

	/**
	 * Returns true if the given input string matches the test string.
	 *
	 * @param string $input Input.
	 * @param string $test Test.
	 * @return string|false
	 */
	public function streq( $input, $test ) {
		return $input === $test
		? $test
		: false;
	}

	/**
	 * Returns true.
	 *
	 * @param string $input Input.
	 * @return bool
	 */
	public function unconditional_match( $input ) {
		return $input;
	}

	/**
	 * Checks to see if the input string only contains characters within the given byte range
	 *
	 * @param string $input Input.
	 * @param array  $valid_range Valid range.
	 * @return string
	 */
	public function validate_byte_range( $input, $valid_range ) {
		if ( '' === $input ) {
			// an empty string is considered "valid".
			return false;
		}
		$i = 0;
		while ( isset( $input[ $i ] ) ) {
			$n = ord( $input[ $i ] );
			if ( $n < $valid_range['min'] || $n > $valid_range['max'] ) {
				return $input[ $i ];
			}
			$valid = false;
			foreach ( $valid_range['range'] as $b ) {
				if ( $n === $b || is_array( $b ) && $n >= $b[0] && $n <= $b[1] ) {
					$valid = true;
					break;
				}
			}
			if ( ! $valid ) {
				return $input[ $i ];
			}
			$i++;
		}

		// if there weren't any invalid bytes, return false.
		return false;
	}

	/**
	 * Returns true if the input value is found anywhere inside the test value
	 * (i.e. the inverse of @contains)
	 *
	 * @param mixed $input Input.
	 * @param mixed $test Test.
	 * @return string|false
	 */
	public function within( $input, $test ) {
		if ( '' === $input || '' === $test ) {
			return false;
		}

		return stripos( $test, $input ) !== false
		? $input
		: false;
	}
}