3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Protocol;
24 use Friendica\Content\Text\BBCode;
25 use Friendica\Core\Logger;
27 use Friendica\Model\Search;
30 * Base class for relay handling
35 * Check if a post is wanted
40 * @return boolean "true" is the post is wanted by the system
42 public static function isSolicitedPost(array $tags, string $body, string $url, string $network = '')
44 $config = DI::config();
46 $subscribe = $config->get('system', 'relay_subscribe', false);
48 $scope = $config->get('system', 'relay_scope', SR_SCOPE_ALL);
50 $scope = SR_SCOPE_NONE;
53 if ($scope == SR_SCOPE_NONE) {
54 Logger::info('Server does not accept relay posts - rejected', ['network' => $network, 'url' => $url]);
62 if ($scope == SR_SCOPE_TAGS) {
63 $server_tags = $config->get('system', 'relay_server_tags');
64 $tagitems = explode(',', mb_strtolower($server_tags));
65 foreach ($tagitems AS $tag) {
66 $systemTags[] = trim($tag, '# ');
69 if ($config->get('system', 'relay_user_tags')) {
70 $userTags = Search::getUserTags();
74 $tagList = array_unique(array_merge($systemTags, $userTags));
76 $deny_tags = $config->get('system', 'relay_deny_tags');
77 $tagitems = explode(',', mb_strtolower($deny_tags));
78 foreach ($tagitems AS $tag) {
79 $tag = trim($tag, '# ');
83 if (!empty($tagList) || !empty($denyTags)) {
84 $content = mb_strtolower(BBCode::toPlaintext($body, false));
86 foreach ($tags as $tag) {
87 $tag = mb_strtolower($tag);
88 if (in_array($tag, $denyTags)) {
89 Logger::info('Unwanted hashtag found - rejected', ['hashtag' => $tag, 'network' => $network, 'url' => $url]);
93 if (in_array($tag, $tagList)) {
94 Logger::info('Subscribed hashtag found - accepted', ['hashtag' => $tag, 'network' => $network, 'url' => $url]);
98 // We check with "strpos" for performance issues. Only when this is true, the regular expression check is used
99 // RegExp is taken from here: https://medium.com/@shiba1014/regex-word-boundaries-with-unicode-207794f6e7ed
100 if ((strpos($content, $tag) !== false) && preg_match('/(?<=[\s,.:;"\']|^)' . preg_quote($tag, '/') . '(?=[\s,.:;"\']|$)/', $content)) {
101 Logger::info('Subscribed hashtag found in content - accepted', ['hashtag' => $tag, 'network' => $network, 'url' => $url]);
107 if ($scope == SR_SCOPE_ALL) {
108 Logger::info('Server accept all posts - accepted', ['network' => $network, 'url' => $url]);
112 Logger::info('No matching hashtags found - rejected', ['network' => $network, 'url' => $url]);