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\Contact;
28 use Friendica\Model\Search;
31 * Base class for relay handling
36 * Check if a post is wanted
40 * @param int $authorid
42 * @return boolean "true" is the post is wanted by the system
44 public static function isSolicitedPost(array $tags, string $body, int $authorid, string $url, string $network = '')
46 $config = DI::config();
48 $subscribe = $config->get('system', 'relay_subscribe', false);
50 $scope = $config->get('system', 'relay_scope', SR_SCOPE_ALL);
52 $scope = SR_SCOPE_NONE;
55 if ($scope == SR_SCOPE_NONE) {
56 Logger::info('Server does not accept relay posts - rejected', ['network' => $network, 'url' => $url]);
60 if (Contact::isBlocked($authorid)) {
61 Logger::info('Author is blocked - rejected', ['author' => $authorid, 'network' => $network, 'url' => $url]);
65 if (Contact::isHidden($authorid)) {
66 Logger::info('Author is hidden - rejected', ['author' => $authorid, 'network' => $network, 'url' => $url]);
74 if ($scope == SR_SCOPE_TAGS) {
75 $server_tags = $config->get('system', 'relay_server_tags');
76 $tagitems = explode(',', mb_strtolower($server_tags));
77 foreach ($tagitems AS $tag) {
78 $systemTags[] = trim($tag, '# ');
81 if ($config->get('system', 'relay_user_tags')) {
82 $userTags = Search::getUserTags();
86 $tagList = array_unique(array_merge($systemTags, $userTags));
88 $deny_tags = $config->get('system', 'relay_deny_tags');
89 $tagitems = explode(',', mb_strtolower($deny_tags));
90 foreach ($tagitems AS $tag) {
91 $tag = trim($tag, '# ');
95 if (!empty($tagList) || !empty($denyTags)) {
96 $content = mb_strtolower(BBCode::toPlaintext($body, false));
98 foreach ($tags as $tag) {
99 $tag = mb_strtolower($tag);
100 if (in_array($tag, $denyTags)) {
101 Logger::info('Unwanted hashtag found - rejected', ['hashtag' => $tag, 'network' => $network, 'url' => $url]);
105 if (in_array($tag, $tagList)) {
106 Logger::info('Subscribed hashtag found - accepted', ['hashtag' => $tag, 'network' => $network, 'url' => $url]);
110 // We check with "strpos" for performance issues. Only when this is true, the regular expression check is used
111 // RegExp is taken from here: https://medium.com/@shiba1014/regex-word-boundaries-with-unicode-207794f6e7ed
112 if ((strpos($content, $tag) !== false) && preg_match('/(?<=[\s,.:;"\']|^)' . preg_quote($tag, '/') . '(?=[\s,.:;"\']|$)/', $content)) {
113 Logger::info('Subscribed hashtag found in content - accepted', ['hashtag' => $tag, 'network' => $network, 'url' => $url]);
119 if ($scope == SR_SCOPE_ALL) {
120 Logger::info('Server accept all posts - accepted', ['network' => $network, 'url' => $url]);
124 Logger::info('No matching hashtags found - rejected', ['network' => $network, 'url' => $url]);