aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Lib/MessageFilter.php
blob: e7382c0d56dc8b8113ea4f0fd767035653065045 (plain) (blame)
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
<?php

namespace Zotlabs\Lib;

require_once('include/html2plain.php');

class MessageFilter {

	public static function evaluate($item, $incl, $excl) {

		$text = prepare_text($item['body'],((isset($item['mimetype'])) ? $item['mimetype'] : 'text/bbcode'));
		$text = html2plain(($item['title']) ? $item['title'] . ' ' . $text : $text);

		$lang = null;

		if ((strpos($incl, 'lang=') !== false) || (strpos($excl, 'lang=') !== false) || (strpos($incl, 'lang!=') !== false) || (strpos($excl, 'lang!=') !== false)) {
			$lang = detect_language($text);
		}

		$tags = ((isset($item['term']) && is_array($item['term']) && count($item['term'])) ? $item['term'] : false);

		// exclude always has priority

		$exclude = (($excl) ? explode("\n", $excl) : null);

		if ($exclude) {
			foreach ($exclude as $word) {
				$word = html_entity_decode(trim($word));
				if (! $word) {
					continue;
				}
				if (isset($lang) && ((strpos($word, 'lang=') === 0) || (strpos($word, 'lang!=') === 0))) {
					if (!strlen($lang)) {
						// Result is ambiguous. As we are matching deny rules only at this time, continue tests.
						// Any matching deny rule concludes testing.
						continue;
					}
					if (strpos($word, 'lang=') === 0 && strcasecmp($lang, trim(substr($word, 5))) == 0) {
						return false;
					} elseif (strpos($word, 'lang!=') === 0 && strcasecmp($lang, trim(substr($word, 6))) != 0) {
						return false;
					}
				}
                elseif (substr($word, 0, 1) === '#' && $tags) {
					foreach ($tags as $t) {
						if ((($t['ttype'] == TERM_HASHTAG) || ($t['ttype'] == TERM_COMMUNITYTAG)) && (($t['term'] === substr($word, 1)) || (substr($word, 1) === '*'))) {
							return false;
						}
					}
				} elseif (substr($word, 0, 1) === '$' && $tags) {
					foreach ($tags as $t) {
						if (($t['ttype'] == TERM_CATEGORY) && (($t['term'] === substr($word, 1)) || (substr($word, 1) === '*'))) {
							return false;
						}
					}
				} elseif (substr($word, 0, 2) === '?+') {
					if (self::test_condition(substr($word, 2), $item['obj'])) {
						return false;
					}
				} elseif (substr($word, 0, 1) === '?') {
					if (self::test_condition(substr($word, 1), $item)) {
						return false;
					}
				} elseif ((strpos($word, '/') === 0) && preg_match($word, $text)) {
					return false;
				} elseif (stristr($text, $word) !== false) {
					return false;
				}
			}
		}

		$include = (($incl) ? explode("\n", $incl) : null);

		if ($include) {
			foreach ($include as $word) {
				$word = html_entity_decode(trim($word));
				if (! $word) {
					continue;
				}
				if (isset($lang) && ((strpos($word, 'lang=') === 0) || (strpos($word, 'lang!=') === 0))) {
					if (!strlen($lang))  {
						// Result is ambiguous. However we are checking allow rules
						// and an ambiguous language is always permitted.
						return true;
					}
					if (strpos($word, 'lang=') === 0 && strcasecmp($lang, trim(substr($word, 5))) == 0) {
						return true;
					} elseif (strpos($word, 'lang!=') === 0 && strcasecmp($lang, trim(substr($word, 6))) != 0) {
						return true;
					}
				}
                elseif (substr($word, 0, 1) === '#' && $tags) {
					foreach ($tags as $t) {
						if ((($t['ttype'] == TERM_HASHTAG) || ($t['ttype'] == TERM_COMMUNITYTAG)) && (($t['term'] === substr($word, 1)) || (substr($word, 1) === '*'))) {
							return true;
						}
					}
				} elseif (substr($word, 0, 1) === '$' && $tags) {
					foreach ($tags as $t) {
						if (($t['ttype'] == TERM_CATEGORY) && (($t['term'] === substr($word, 1)) || (substr($word, 1) === '*'))) {
							return true;
						}
					}
				} elseif (substr($word, 0, 2) === '?+') {
					if (self::test_condition(substr($word, 2), $item['obj'])) {
						return true;
					}
				} elseif (substr($word, 0, 1) === '?') {
					if (self::test_condition(substr($word, 1), $item)) {
						return true;
					}
				} elseif ((strpos($word, '/') === 0) && preg_match($word, $text)) {
					return true;
				} elseif (stristr($text, $word) !== false) {
					return true;
				}
			}
		} else {
			return true;
		}

		return false;
	}


	/**
	 * @brief Test for Conditional Execution conditions. Shamelessly ripped off from Code/Render/Comanche
	 *
	 * This is extensible. The first version of variable testing supports tests of the forms:
	 *
	 * - ?foo ~= baz which will check if item.foo contains the string 'baz';
	 * - ?foo == baz which will check if item.foo is the string 'baz';
	 * - ?foo != baz which will check if item.foo is not the string 'baz';
	 * - ?foo >= 3 which will check if item.foo is greater than or equal to 3;
	 * - ?foo > 3 which will check if item.foo is greater than 3;
	 * - ?foo <= 3 which will check if item.foo is less than or equal to 3;
	 * - ?foo < 3 which will check if item.foo is less than 3;
	 *
	 * - ?foo {} baz which will check if 'baz' is an array element in item.foo
	 * - ?foo {*} baz which will check if 'baz' is an array key in item.foo
	 * - ?foo which will check for a return of a true condition for item.foo;
     * - ?!foo which will check for a return of a false condition for item.foo;
     *
	 * The values 0, '', an empty array, and an unset value will all evaluate to false.
	 *
	 * @param string $s
	 * @param array $item
	 * @return bool
	 */

	public static function test_condition($s,$item) {

		if (preg_match('/(.*?)\s\~\=\s(.*?)$/', $s, $matches)) {
			$x = ((array_key_exists(trim($matches[1]),$item)) ? $item[trim($matches[1])] : EMPTY_STR);
			if (stripos($x, trim($matches[2])) !== false) {
				return true;
			}
			return false;
		}

		if (preg_match('/(.*?)\s\=\=\s(.*?)$/', $s, $matches)) {
			$x = ((array_key_exists(trim($matches[1]),$item)) ? $item[trim($matches[1])] : EMPTY_STR);
			if ($x == trim($matches[2])) {
				return true;
			}
			return false;
		}

		if (preg_match('/(.*?)\s\!\=\s(.*?)$/', $s, $matches)) {
			$x = ((array_key_exists(trim($matches[1]),$item)) ? $item[trim($matches[1])] : EMPTY_STR);
			if ($x != trim($matches[2])) {
				return true;
			}
			return false;
		}

		if (preg_match('/(.*?)\s\>\=\s(.*?)$/', $s, $matches)) {
			$x = ((array_key_exists(trim($matches[1]),$item)) ? $item[trim($matches[1])] : EMPTY_STR);
			if ($x >= trim($matches[2])) {
				return true;
			}
			return false;
		}

		if (preg_match('/(.*?)\s\<\=\s(.*?)$/', $s, $matches)) {
			$x = ((array_key_exists(trim($matches[1]),$item)) ? $item[trim($matches[1])] : EMPTY_STR);
			if ($x <= trim($matches[2])) {
				return true;
			}
			return false;
		}

		if (preg_match('/(.*?)\s\>\s(.*?)$/', $s, $matches)) {
			$x = ((array_key_exists(trim($matches[1]),$item)) ? $item[trim($matches[1])] : EMPTY_STR);
			if ($x > trim($matches[2])) {
				return true;
			}
			return false;
		}

		if (preg_match('/(.*?)\s\>\s(.*?)$/', $s, $matches)) {
			$x = ((array_key_exists(trim($matches[1]),$item)) ? $item[trim($matches[1])] : EMPTY_STR);
			if ($x < trim($matches[2])) {
				return true;
			}
			return false;
		}

		if (preg_match('/[\$](.*?)\s\{\}\s(.*?)$/', $s, $matches)) {
			$x = ((array_key_exists(trim($matches[1]),$item)) ? $item[trim($matches[1])] : EMPTY_STR);
			if (is_array($x) && in_array(trim($matches[2]), $x)) {
				return true;
			}
			return false;
		}

		if (preg_match('/(.*?)\s\{\*\}\s(.*?)$/', $s, $matches)) {
			$x = ((array_key_exists(trim($matches[1]),$item)) ? $item[trim($matches[1])] : EMPTY_STR);
			if (is_array($x) && array_key_exists(trim($matches[2]), $x)) {
				return true;
			}
			return false;
		}

		// Ordering of this check (for falsiness) with relation to the following one (check for truthiness) is important.
		if (preg_match('/\!(.*?)$/', $s, $matches)) {
			$x = ((array_key_exists(trim($matches[1]),$item)) ? $item[trim($matches[1])] : EMPTY_STR);
			if (!$x) {
				return true;
			}
			return false;
		}

		if (preg_match('/(.*?)$/', $s, $matches)) {
			$x = ((array_key_exists(trim($matches[1]),$item)) ? $item[trim($matches[1])] : EMPTY_STR);
			if ($x) {
				return true;
			}
			return false;
		}

		return false;
	}

}