2018-05-11 00:11:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Zotlabs\Lib;
|
|
|
|
|
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
class MessageFilter
|
|
|
|
{
|
2018-05-11 00:11:03 +00:00
|
|
|
|
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
public static function evaluate($item, $incl, $excl)
|
|
|
|
{
|
2018-05-11 00:11:03 +00:00
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
require_once('include/html2plain.php');
|
2018-05-11 00:11:03 +00:00
|
|
|
|
2021-09-04 00:17:46 +00:00
|
|
|
$text = prepare_text($item['body'],((isset($item['mimetype'])) ? $item['mimetype'] : 'text/x-multicode'));
|
2021-12-03 03:01:39 +00:00
|
|
|
$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 = 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;
|
|
|
|
}
|
2018-05-12 11:28:04 +00:00
|
|
|
}
|