]> git.mxchange.org Git - friendica.git/blob - src/Protocol/Relay.php
Default values for settings / use lowercase
[friendica.git] / src / Protocol / Relay.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Protocol;
23
24 use Friendica\Content\Text\BBCode;
25 use Friendica\Core\Logger;
26 use Friendica\DI;
27 use Friendica\Model\Search;
28
29 /**
30  * Base class for relay handling
31  */
32 class Relay
33 {
34         /**
35          * Check if a post is wanted
36          *
37          * @param array $tags
38          * @param string $body
39          * @param string $url
40          * @return boolean "true" is the post is wanted by the system
41          */
42         public static function isSolicitedPost(array $tags, string $body, string $url, string $network = '')
43         {
44                 $config = DI::config();
45
46                 $subscribe = $config->get('system', 'relay_subscribe', false);
47                 if ($subscribe) {
48                         $scope = $config->get('system', 'relay_scope', SR_SCOPE_ALL);
49                 } else {
50                         $scope = SR_SCOPE_NONE;
51                 }
52
53                 if ($scope == SR_SCOPE_NONE) {
54                         Logger::info('Server does not accept relay posts - rejected', ['network' => $network, 'url' => $url]);
55                         return false;
56                 }
57
58                 $systemTags = [];
59                 $userTags = [];
60                 $denyTags = [];
61
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, '# ');
67                         }
68
69                         if ($config->get('system', 'relay_user_tags')) {
70                                 $userTags = Search::getUserTags();
71                         }
72                 }
73
74                 $tagList = array_unique(array_merge($systemTags, $userTags));
75
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, '# ');
80                         $denyTags[] = $tag;
81                 }
82
83                 if (!empty($tagList) || !empty($denyTags)) {
84                         $content = mb_strtolower(BBCode::toPlaintext($body, false));
85
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]);
90                                         return false;
91                                 }
92
93                                 if (in_array($tag, $tagList)) {
94                                         Logger::info('Subscribed hashtag found - accepted', ['hashtag' => $tag, 'network' => $network, 'url' => $url]);
95                                         return true;
96                                 }
97
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]);
102                                         return true;
103                                 }
104                         }
105                 }
106
107                 if ($scope == SR_SCOPE_ALL) {
108                         Logger::info('Server accept all posts - accepted', ['network' => $network, 'url' => $url]);
109                         return true;
110                 }
111
112                 Logger::info('No matching hashtags found - rejected', ['network' => $network, 'url' => $url]);
113                 return false;
114         }
115 }