]> git.mxchange.org Git - friendica.git/blob - src/Protocol/Relay.php
Merge pull request #10116 from mexon/mat/addon-console-command
[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\Core\Protocol;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\APContact;
30 use Friendica\Model\Contact;
31 use Friendica\Model\GServer;
32 use Friendica\Model\Item;
33 use Friendica\Model\Post;
34 use Friendica\Model\Search;
35 use Friendica\Model\Tag;
36 use Friendica\Util\DateTimeFormat;
37 use Friendica\Util\Strings;
38
39 /**
40  * Base class for relay handling
41  */
42 class Relay
43 {
44         /**
45          * Check if a post is wanted
46          *
47          * @param array $tags
48          * @param string $body
49          * @param int $authorid
50          * @param string $url
51          * @return boolean "true" is the post is wanted by the system
52          */
53         public static function isSolicitedPost(array $tags, string $body, int $authorid, string $url, string $network = '')
54         {
55                 $config = DI::config();
56
57                 $subscribe = $config->get('system', 'relay_subscribe', false);
58                 if ($subscribe) {
59                         $scope = $config->get('system', 'relay_scope', SR_SCOPE_ALL);
60                 } else {
61                         $scope = SR_SCOPE_NONE;
62                 }
63
64                 if ($scope == SR_SCOPE_NONE) {
65                         Logger::info('Server does not accept relay posts - rejected', ['network' => $network, 'url' => $url]);
66                         return false;
67                 }
68
69                 if (Contact::isBlocked($authorid)) {
70                         Logger::info('Author is blocked - rejected', ['author' => $authorid, 'network' => $network, 'url' => $url]);
71                         return false;
72                 }
73
74                 if (Contact::isHidden($authorid)) {
75                         Logger::info('Author is hidden - rejected', ['author' => $authorid, 'network' => $network, 'url' => $url]);
76                         return false;
77                 }
78
79                 $systemTags = [];
80                 $userTags = [];
81                 $denyTags = [];
82
83                 if ($scope == SR_SCOPE_TAGS) {
84                         $server_tags = $config->get('system', 'relay_server_tags');
85                         $tagitems = explode(',', mb_strtolower($server_tags));
86                         foreach ($tagitems AS $tag) {
87                                 $systemTags[] = trim($tag, '# ');
88                         }
89
90                         if ($config->get('system', 'relay_user_tags')) {
91                                 $userTags = Search::getUserTags();
92                         }
93                 }
94
95                 $tagList = array_unique(array_merge($systemTags, $userTags));
96
97                 $deny_tags = $config->get('system', 'relay_deny_tags');
98                 $tagitems = explode(',', mb_strtolower($deny_tags));
99                 foreach ($tagitems AS $tag) {
100                         $tag = trim($tag, '# ');
101                         $denyTags[] = $tag;
102                 }
103
104                 if (!empty($tagList) || !empty($denyTags)) {
105                         $content = mb_strtolower(BBCode::toPlaintext($body, false));
106
107                         foreach ($tags as $tag) {
108                                 $tag = mb_strtolower($tag);
109                                 if (in_array($tag, $denyTags)) {
110                                         Logger::info('Unwanted hashtag found - rejected', ['hashtag' => $tag, 'network' => $network, 'url' => $url]);
111                                         return false;
112                                 }
113
114                                 if (in_array($tag, $tagList)) {
115                                         Logger::info('Subscribed hashtag found - accepted', ['hashtag' => $tag, 'network' => $network, 'url' => $url]);
116                                         return true;
117                                 }
118
119                                 // We check with "strpos" for performance issues. Only when this is true, the regular expression check is used
120                                 // RegExp is taken from here: https://medium.com/@shiba1014/regex-word-boundaries-with-unicode-207794f6e7ed
121                                 if ((strpos($content, $tag) !== false) && preg_match('/(?<=[\s,.:;"\']|^)' . preg_quote($tag, '/') . '(?=[\s,.:;"\']|$)/', $content)) {
122                                         Logger::info('Subscribed hashtag found in content - accepted', ['hashtag' => $tag, 'network' => $network, 'url' => $url]);
123                                         return true;
124                                 }
125                         }
126                 }
127
128                 if ($scope == SR_SCOPE_ALL) {
129                         Logger::info('Server accept all posts - accepted', ['network' => $network, 'url' => $url]);
130                         return true;
131                 }
132
133                 Logger::info('No matching hashtags found - rejected', ['network' => $network, 'url' => $url]);
134                 return false;
135         }
136
137         /**
138          * Update or insert a relay contact
139          *
140          * @param array $gserver Global server record
141          * @param array $fields  Optional network specific fields
142          * @throws \Exception
143          */
144         public static function updateContact(array $gserver, array $fields = [])
145         {
146                 if (in_array($gserver['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
147                         $system = APContact::getByURL($gserver['url'] . '/friendica');
148                         if (!empty($system['sharedinbox'])) {
149                                 Logger::info('Sucessfully probed for relay contact', ['server' => $gserver['url']]);
150                                 $id = Contact::updateFromProbeByURL($system['url']);
151                                 Logger::info('Updated relay contact', ['server' => $gserver['url'], 'id' => $id]);
152                                 return;
153                         }
154                 }
155
156                 $condition = ['uid' => 0, 'gsid' => $gserver['id'], 'contact-type' => Contact::TYPE_RELAY];
157                 $old = DBA::selectFirst('contact', [], $condition);
158                 if (!DBA::isResult($old)) {
159                         $condition = ['uid' => 0, 'nurl' => Strings::normaliseLink($gserver['url'])];
160                         $old = DBA::selectFirst('contact', [], $condition);
161                         if (DBA::isResult($old)) {
162                                 $fields['gsid'] = $gserver['id'];
163                                 $fields['contact-type'] = Contact::TYPE_RELAY;
164                                 Logger::info('Assigning missing data for relay contact', ['server' => $gserver['url'], 'id' => $old['id']]);
165                         }
166                 } elseif (empty($fields)) {
167                         Logger::info('No content to update, quitting', ['server' => $gserver['url']]);
168                         return;
169                 }
170
171                 if (DBA::isResult($old)) {      
172                         $fields['updated'] = DateTimeFormat::utcNow();
173
174                         Logger::info('Update relay contact', ['server' => $gserver['url'], 'id' => $old['id'], 'fields' => $fields]);
175                         DBA::update('contact', $fields, ['id' => $old['id']], $old);
176                 } else {
177                         $default = ['created' => DateTimeFormat::utcNow(),
178                                 'name' => 'relay', 'nick' => 'relay', 'url' => $gserver['url'],
179                                 'nurl' => Strings::normaliseLink($gserver['url']),
180                                 'network' => Protocol::DIASPORA, 'uid' => 0,
181                                 'batch' => $gserver['url'] . '/receive/public',
182                                 'rel' => Contact::FOLLOWER, 'blocked' => false,
183                                 'pending' => false, 'writable' => true,
184                                 'gsid' => $gserver['id'],
185                                 'baseurl' => $gserver['url'], 'contact-type' => Contact::TYPE_RELAY];
186
187                         $fields = array_merge($default, $fields);
188
189                         Logger::info('Create relay contact', ['server' => $gserver['url'], 'fields' => $fields]);
190                         Contact::insert($fields);
191                 }
192         }
193
194         /**
195          * Mark the relay contact of the given contact for archival
196          * This is called whenever there is a communication issue with the server.
197          * It avoids sending stuff to servers who don't exist anymore.
198          * The relay contact is a technical contact entry that exists once per server.
199          *
200          * @param array $contact of the relay contact
201          */
202         public static function markForArchival(array $contact)
203         {
204                 if (!empty($contact['contact-type']) && ($contact['contact-type'] == Contact::TYPE_RELAY)) {
205                         // This is already the relay contact, we don't need to fetch it
206                         $relay_contact = $contact;
207                 } elseif (empty($contact['baseurl'])) {
208                         if (!empty($contact['batch'])) {
209                                 $condition = ['uid' => 0, 'network' => Protocol::FEDERATED, 'batch' => $contact['batch'], 'contact-type' => Contact::TYPE_RELAY];
210                                 $relay_contact = DBA::selectFirst('contact', [], $condition);
211                         } else {
212                                 return;
213                         }
214                 } else {
215                         $gserver = ['id' => $contact['gsid'] ?: GServer::getID($contact['baseurl'], true),
216                                 'url' => $contact['baseurl'], 'network' => $contact['network']];
217                         $relay_contact = self::getContact($gserver, []);
218                 }
219
220                 if (!empty($relay_contact)) {
221                         Logger::info('Relay contact will be marked for archival', ['id' => $relay_contact['id'], 'url' => $relay_contact['url']]);
222                         Contact::markForArchival($relay_contact);
223                 }
224         }
225
226         /**
227          * Return a list of relay servers
228          *
229          * The list contains not only the official relays but also servers that we serve directly
230          *
231          * @param integer $item_id  id of the item that is sent
232          * @param array   $contacts Previously fetched contacts
233          * @param array   $networks Networks of the relay servers 
234          *
235          * @return array of relay servers
236          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
237          */
238         public static function getList(int $item_id, array $contacts, array $networks)
239         {
240                 $serverlist = [];
241
242                 // Fetching relay servers
243                 $serverdata = DI::config()->get("system", "relay_server");
244
245                 if (!empty($serverdata)) {
246                         $servers = explode(",", $serverdata);
247                         foreach ($servers as $server) {
248                                 $gserver = DBA::selectFirst('gserver', ['id', 'url', 'network'], ['nurl' => Strings::normaliseLink($server)]);
249                                 if (DBA::isResult($gserver)) {
250                                         $serverlist[$gserver['id']] = $gserver;
251                                 }
252                         }
253                 }
254
255                 if (DI::config()->get("system", "relay_directly", false)) {
256                         // We distribute our stuff based on the parent to ensure that the thread will be complete
257                         $parent = Post::selectFirst(['uri-id'], ['id' => $item_id]);
258                         if (!DBA::isResult($parent)) {
259                                 return;
260                         }
261
262                         // Servers that want to get all content
263                         $servers = DBA::select('gserver', ['id', 'url', 'network'], ['relay-subscribe' => true, 'relay-scope' => 'all']);
264                         while ($server = DBA::fetch($servers)) {
265                                 $serverlist[$server['id']] = $server;
266                         }
267                         DBA::close($servers);
268
269                         // All tags of the current post
270                         $tags = DBA::select('tag-view', ['name'], ['uri-id' => $parent['uri-id'], 'type' => Tag::HASHTAG]);
271                         $taglist = [];
272                         while ($tag = DBA::fetch($tags)) {
273                                 $taglist[] = $tag['name'];
274                         }
275                         DBA::close($tags);
276
277                         // All servers who wants content with this tag
278                         $tagserverlist = [];
279                         if (!empty($taglist)) {
280                                 $tagserver = DBA::select('gserver-tag', ['gserver-id'], ['tag' => $taglist]);
281                                 while ($server = DBA::fetch($tagserver)) {
282                                         $tagserverlist[] = $server['gserver-id'];
283                                 }
284                                 DBA::close($tagserver);
285                         }
286
287                         // All adresses with the given id
288                         if (!empty($tagserverlist)) {
289                                 $servers = DBA::select('gserver', ['id', 'url', 'network'], ['relay-subscribe' => true, 'relay-scope' => 'tags', 'id' => $tagserverlist]);
290                                 while ($server = DBA::fetch($servers)) {
291                                         $serverlist[$server['id']] = $server;
292                                 }
293                                 DBA::close($servers);
294                         }
295                 }
296
297                 // Now we are collecting all relay contacts
298                 foreach ($serverlist as $gserver) {
299                         // We don't send messages to ourselves
300                         if (Strings::compareLink($gserver['url'], DI::baseUrl())) {
301                                 continue;
302                         }
303                         $contact = self::getContact($gserver);
304                         if (empty($contact)) {
305                                 continue;
306                         }
307
308                         if (in_array($contact['network'], $networks) && !in_array($contact['batch'], array_column($contacts, 'batch'))) {
309                                 $contacts[] = $contact;
310                         }
311                 }
312
313                 return $contacts;
314         }
315
316         /**
317          * Return a contact for a given server address or creates a dummy entry
318          *
319          * @param array $gserver Global server record
320          * @param array $fields  Fieldlist
321          * @return array with the contact
322          * @throws \Exception
323          */
324         private static function getContact(array $gserver, array $fields = ['batch', 'id', 'url', 'name', 'network', 'protocol', 'archive', 'blocked'])
325         {
326                 // Fetch the relay contact
327                 $condition = ['uid' => 0, 'gsid' => $gserver['id'], 'contact-type' => Contact::TYPE_RELAY];
328                 $contact = DBA::selectFirst('contact', $fields, $condition);
329                 if (DBA::isResult($contact)) {
330                         if ($contact['archive'] || $contact['blocked']) {
331                                 return false;
332                         }
333                         return $contact;
334                 } else {
335                         self::updateContact($gserver);
336
337                         $contact = DBA::selectFirst('contact', $fields, $condition);
338                         if (DBA::isResult($contact)) {
339                                 return $contact;
340                         }
341                 }
342
343                 // It should never happen that we arrive here
344                 return [];
345         }
346 }