aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Lib/MessageFilter.php
blob: 21e6ca26a1c04b2628abbdd65ba1150d6655def9 (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
<?php

namespace Zotlabs\Lib;



class MessageFilter {


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

		require_once('include/html2plain.php');

		$text = prepare_text($item['body'],$item['mimetype']);
		$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 = ((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 = trim($word);
				if(! $word)
					continue;
				if(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((strpos($word,'/') === 0) && preg_match($word,$text))
					return false;
				elseif((strpos($word,'lang=') === 0) && ($lang) && (strcasecmp($lang,trim(substr($word,5))) == 0))
					return false;
				elseif((strpos($word,'lang!=') === 0) && ($lang) && (strcasecmp($lang,trim(substr($word,6))) != 0))
					return false;
				elseif(stristr($text,$word) !== false)
					return false;
			}
		}

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

		if($include) {
			foreach($include as $word) {
				$word = trim($word);
				if(! $word)
					continue;
				if(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((strpos($word,'/') === 0) && preg_match($word,$text))
					return true;
				elseif((strpos($word,'lang=') === 0) && ($lang) && (strcasecmp($lang,trim(substr($word,5))) == 0))
					return true;
				elseif((strpos($word,'lang!=') === 0) && ($lang) && (strcasecmp($lang,trim(substr($word,6))) != 0))
					return true;
				elseif(stristr($text,$word) !== false)
					return true;
			}
		}
		else {
			return true;
		}

		return false;
	}


}