]> git.mxchange.org Git - friendica.git/blob - src/Protocol/Relay.php
fb838f91761b7b2f0f1cbe46da5aa20178a54dfa
[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\Contact;
28 use Friendica\Model\Search;
29
30 /**
31  * Base class for relay handling
32  */
33 class Relay
34 {
35         /**
36          * Check if a post is wanted
37          *
38          * @param array $tags
39          * @param string $body
40          * @param int $authorid
41          * @param string $url
42          * @return boolean "true" is the post is wanted by the system
43          */
44         public static function isSolicitedPost(array $tags, string $body, int $authorid, string $url, string $network = '')
45         {
46                 $config = DI::config();
47
48                 $subscribe = $config->get('system', 'relay_subscribe', false);
49                 if ($subscribe) {
50                         $scope = $config->get('system', 'relay_scope', SR_SCOPE_ALL);
51                 } else {
52                         $scope = SR_SCOPE_NONE;
53                 }
54
55                 if ($scope == SR_SCOPE_NONE) {
56                         Logger::info('Server does not accept relay posts - rejected', ['network' => $network, 'url' => $url]);
57                         return false;
58                 }
59
60                 if (Contact::isBlocked($authorid)) {
61                         Logger::info('Author is blocked - rejected', ['author' => $authorid, 'network' => $network, 'url' => $url]);
62                         return false;
63                 }
64
65                 if (Contact::isHidden($authorid)) {
66                         Logger::info('Author is hidden - rejected', ['author' => $authorid, 'network' => $network, 'url' => $url]);
67                         return false;
68                 }
69
70                 $systemTags = [];
71                 $userTags = [];
72                 $denyTags = [];
73
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, '# ');
79                         }
80
81                         if ($config->get('system', 'relay_user_tags')) {
82                                 $userTags = Search::getUserTags();
83                         }
84                 }
85
86                 $tagList = array_unique(array_merge($systemTags, $userTags));
87
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, '# ');
92                         $denyTags[] = $tag;
93                 }
94
95                 if (!empty($tagList) || !empty($denyTags)) {
96                         $content = mb_strtolower(BBCode::toPlaintext($body, false));
97
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]);
102                                         return false;
103                                 }
104
105                                 if (in_array($tag, $tagList)) {
106                                         Logger::info('Subscribed hashtag found - accepted', ['hashtag' => $tag, 'network' => $network, 'url' => $url]);
107                                         return true;
108                                 }
109
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]);
114                                         return true;
115                                 }
116                         }
117                 }
118
119                 if ($scope == SR_SCOPE_ALL) {
120                         Logger::info('Server accept all posts - accepted', ['network' => $network, 'url' => $url]);
121                         return true;
122                 }
123
124                 Logger::info('No matching hashtags found - rejected', ['network' => $network, 'url' => $url]);
125                 return false;
126         }
127 }