]> git.mxchange.org Git - friendica.git/blob - src/Protocol/Relay.php
Merge pull request #13724 from Raroun/Fix-for-Issue-#13637---Photo-caption-prevents...
[friendica.git] / src / Protocol / Relay.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
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\Smilies;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Core\Cache\Enum\Duration;
27 use Friendica\Core\Logger;
28 use Friendica\Core\Protocol;
29 use Friendica\Database\DBA;
30 use Friendica\DI;
31 use Friendica\Model\APContact;
32 use Friendica\Model\Contact;
33 use Friendica\Model\GServer;
34 use Friendica\Model\Item;
35 use Friendica\Model\Post;
36 use Friendica\Model\Search;
37 use Friendica\Model\Tag;
38 use Friendica\Model\User;
39 use Friendica\Util\DateTimeFormat;
40 use Friendica\Util\Strings;
41
42 /**
43  * Base class for relay handling
44  * @see https://github.com/jaywink/social-relay
45  * @see https://wiki.diasporafoundation.org/Relay_servers_for_public_posts
46  */
47 class Relay
48 {
49         const SCOPE_NONE = '';
50         const SCOPE_ALL = 'all';
51         const SCOPE_TAGS = 'tags';
52
53         /**
54          * Check if a post is wanted
55          *
56          * @param array  $tags
57          * @param string $body
58          * @param int    $authorid
59          * @param string $url
60          * @param string $network
61          * @param int    $causerid
62          * @param array  $languages
63          * @return boolean "true" is the post is wanted by the system
64          */
65         public static function isSolicitedPost(array $tags, string $body, int $authorid, string $url, string $network = '', int $causerid = 0, array $languages = []): bool
66         {
67                 $config = DI::config();
68
69                 if (Contact::hasFollowers($authorid)) {
70                         Logger::info('Author has got followers on this server - accepted', ['network' => $network, 'url' => $url, 'author' => $authorid, 'tags' => $tags]);
71                         return true;
72                 }
73
74                 $scope = $config->get('system', 'relay_scope');
75
76                 if ($scope == self::SCOPE_NONE) {
77                         Logger::info('Server does not accept relay posts - rejected', ['network' => $network, 'url' => $url]);
78                         return false;
79                 }
80
81                 if (Contact::isBlocked($authorid)) {
82                         Logger::info('Author is blocked - rejected', ['author' => $authorid, 'network' => $network, 'url' => $url]);
83                         return false;
84                 }
85
86                 if (Contact::isHidden($authorid)) {
87                         Logger::info('Author is hidden - rejected', ['author' => $authorid, 'network' => $network, 'url' => $url]);
88                         return false;
89                 }
90
91                 if (!empty($causerid)) {
92                         $contact = Contact::getById($causerid, ['url']);
93                         $causer = $contact['url'] ?? '';
94                 } else {
95                         $causer = '';
96                 }
97
98                 $body = ActivityPub\Processor::normalizeMentionLinks($body);
99
100                 $denyTags = [];
101
102                 if ($scope == self::SCOPE_TAGS) {
103                         $tagList = self::getSubscribedTags();
104                 } else {
105                         $tagList  = [];
106                 }
107
108                 $deny_tags = $config->get('system', 'relay_deny_tags');
109                 $tagitems = explode(',', mb_strtolower($deny_tags));
110                 foreach ($tagitems as $tag) {
111                         $tag = trim($tag, '# ');
112                         $denyTags[] = $tag;
113                 }
114
115                 if (!empty($tagList) || !empty($denyTags)) {
116                         $content = mb_strtolower(BBCode::toPlaintext($body, false));
117
118                         foreach ($tags as $tag) {
119                                 $tag = mb_strtolower($tag);
120                                 if (in_array($tag, $denyTags)) {
121                                         Logger::info('Unwanted hashtag found - rejected', ['hashtag' => $tag, 'network' => $network, 'url' => $url, 'causer' => $causer]);
122                                         return false;
123                                 }
124
125                                 if (in_array($tag, $tagList)) {
126                                         Logger::info('Subscribed hashtag found - accepted', ['hashtag' => $tag, 'network' => $network, 'url' => $url, 'causer' => $causer]);
127                                         return true;
128                                 }
129
130                                 // We check with "strpos" for performance issues. Only when this is true, the regular expression check is used
131                                 // RegExp is taken from here: https://medium.com/@shiba1014/regex-word-boundaries-with-unicode-207794f6e7ed
132                                 if ((strpos($content, $tag) !== false) && preg_match('/(?<=[\s,.:;"\']|^)' . preg_quote($tag, '/') . '(?=[\s,.:;"\']|$)/', $content)) {
133                                         Logger::info('Subscribed hashtag found in content - accepted', ['hashtag' => $tag, 'network' => $network, 'url' => $url, 'causer' => $causer]);
134                                         return true;
135                                 }
136                         }
137                 }
138
139                 if (!self::isWantedLanguage($body, 0, $authorid, $languages)) {
140                         Logger::info('Unwanted or Undetected language found - rejected', ['network' => $network, 'url' => $url, 'causer' => $causer, 'tags' => $tags]);
141                         return false;
142                 }
143
144                 if ($scope == self::SCOPE_ALL) {
145                         Logger::info('Server accept all posts - accepted', ['network' => $network, 'url' => $url, 'causer' => $causer, 'tags' => $tags]);
146                         return true;
147                 }
148
149                 Logger::info('No matching hashtags found - rejected', ['network' => $network, 'url' => $url, 'causer' => $causer, 'tags' => $tags]);
150                 return false;
151         }
152
153         /**
154          * Get a list of subscribed tags by both the users and the tags that are defined by the admin
155          *
156          * @return array
157          */
158         public static function getSubscribedTags(): array
159         {
160                 $systemTags  = [];
161                 $server_tags = DI::config()->get('system', 'relay_server_tags');
162
163                 foreach (explode(',', mb_strtolower($server_tags)) as $tag) {
164                         $systemTags[] = trim($tag, '# ');
165                 }
166
167                 if (DI::config()->get('system', 'relay_user_tags')) {
168                         $userTags = Search::getUserTags();
169                 } else {
170                         $userTags = [];
171                 }
172
173                 return array_unique(array_merge($systemTags, $userTags));
174         }
175
176         /**
177          * Detect the language of a post and decide if the post should be accepted
178          *
179          * @param string $body
180          * @param int    $uri_id
181          * @param int    $author_id
182          * @param array  $languages
183          * @return boolean
184          */
185         public static function isWantedLanguage(string $body, int $uri_id = 0, int $author_id = 0, array $languages = [])
186         {
187                 $detected = [];
188                 $quality  = DI::config()->get('system', 'relay_language_quality');
189                 foreach (Item::getLanguageArray($body, DI::config()->get('system', 'relay_languages'), $uri_id, $author_id) as $language => $reliability) {
190                         if (($reliability >= $quality) && ($quality > 0)) {
191                                 $detected[] = $language;
192                         }
193                 }
194
195                 if (empty($languages) && empty($detected) && (empty($body) || Smilies::isEmojiPost($body))) {
196                         Logger::debug('Empty body or only emojis', ['body' => $body]);
197                         return true;
198                 }
199
200                 if (!empty($languages) || !empty($detected)) {
201                         $user_languages = User::getLanguages();
202
203                         foreach ($detected as $language) {
204                                 if (in_array($language, $user_languages)) {
205                                         Logger::debug('Wanted language found in detected languages', ['language' => $language, 'detected' => $detected, 'userlang' => $user_languages, 'body' => $body]);
206                                         return true;
207                                 }
208                         }
209                         foreach ($languages as $language) {
210                                 if (in_array($language, $user_languages)) {
211                                         Logger::debug('Wanted language found in defined languages', ['language' => $language, 'languages' => $languages, 'detected' => $detected, 'userlang' => $user_languages, 'body' => $body]);
212                                         return true;
213                                 }
214                         }
215                         Logger::debug('No wanted language found', ['languages' => $languages, 'detected' => $detected, 'userlang' => $user_languages, 'body' => $body]);
216                         return false;
217                 } elseif (DI::config()->get('system', 'relay_deny_undetected_language')) {
218                         Logger::info('Undetected language found', ['body' => $body]);
219                         return false;
220                 }
221
222                 return true;
223         }
224
225         /**
226          * Update or insert a relay contact
227          *
228          * @param array $gserver Global server record
229          * @param array $fields  Optional network specific fields
230          * @return void
231          * @throws \Exception
232          */
233         public static function updateContact(array $gserver, array $fields = [])
234         {
235                 if (in_array($gserver['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
236                         $system = APContact::getByURL($gserver['url'] . '/friendica');
237                         if (!empty($system['sharedinbox'])) {
238                                 Logger::info('Successfully probed for relay contact', ['server' => $gserver['url']]);
239                                 $id = Contact::updateFromProbeByURL($system['url']);
240                                 Logger::info('Updated relay contact', ['server' => $gserver['url'], 'id' => $id]);
241                                 return;
242                         }
243                 }
244
245                 $condition = ['uid' => 0, 'gsid' => $gserver['id'], 'contact-type' => Contact::TYPE_RELAY];
246                 $old = DBA::selectFirst('contact', [], $condition);
247                 if (!DBA::isResult($old)) {
248                         $condition = ['uid' => 0, 'nurl' => Strings::normaliseLink($gserver['url'])];
249                         $old = DBA::selectFirst('contact', [], $condition);
250                         if (DBA::isResult($old)) {
251                                 $fields['gsid'] = $gserver['id'];
252                                 $fields['contact-type'] = Contact::TYPE_RELAY;
253                                 Logger::info('Assigning missing data for relay contact', ['server' => $gserver['url'], 'id' => $old['id']]);
254                         }
255                 } elseif (empty($fields)) {
256                         Logger::info('No content to update, quitting', ['server' => $gserver['url']]);
257                         return;
258                 }
259
260                 if (DBA::isResult($old)) {
261                         $fields['updated'] = DateTimeFormat::utcNow();
262
263                         Logger::info('Update relay contact', ['server' => $gserver['url'], 'id' => $old['id'], 'fields' => $fields]);
264                         Contact::update($fields, ['id' => $old['id']], $old);
265                 } else {
266                         $default = ['created' => DateTimeFormat::utcNow(),
267                                 'name' => 'relay', 'nick' => 'relay', 'url' => $gserver['url'],
268                                 'nurl' => Strings::normaliseLink($gserver['url']),
269                                 'network' => Protocol::DIASPORA, 'uid' => 0,
270                                 'batch' => $gserver['url'] . '/receive/public',
271                                 'rel' => Contact::FOLLOWER, 'blocked' => false,
272                                 'pending' => false, 'writable' => true,
273                                 'gsid' => $gserver['id'],
274                                 'baseurl' => $gserver['url'], 'contact-type' => Contact::TYPE_RELAY];
275
276                         $fields = array_merge($default, $fields);
277
278                         Logger::info('Create relay contact', ['server' => $gserver['url'], 'fields' => $fields]);
279                         Contact::insert($fields);
280                 }
281         }
282
283         /**
284          * Mark the relay contact of the given contact for archival
285          * This is called whenever there is a communication issue with the server.
286          * It avoids sending stuff to servers who don't exist anymore.
287          * The relay contact is a technical contact entry that exists once per server.
288          *
289          * @param array $contact of the relay contact
290          * @return void
291          */
292         public static function markForArchival(array $contact)
293         {
294                 if (!empty($contact['contact-type']) && ($contact['contact-type'] == Contact::TYPE_RELAY)) {
295                         // This is already the relay contact, we don't need to fetch it
296                         $relay_contact = $contact;
297                 } elseif (empty($contact['baseurl'])) {
298                         if (!empty($contact['batch'])) {
299                                 $condition = ['uid' => 0, 'network' => Protocol::FEDERATED, 'batch' => $contact['batch'], 'contact-type' => Contact::TYPE_RELAY];
300                                 $relay_contact = DBA::selectFirst('contact', [], $condition);
301                         } else {
302                                 return;
303                         }
304                 } else {
305                         $gserver = ['id' => $contact['gsid'] ?: GServer::getID($contact['baseurl'], true),
306                                 'url' => $contact['baseurl'], 'network' => $contact['network']];
307                         $relay_contact = self::getContact($gserver, []);
308                 }
309
310                 if (!empty($relay_contact)) {
311                         Logger::info('Relay contact will be marked for archival', ['id' => $relay_contact['id'], 'url' => $relay_contact['url']]);
312                         Contact::markForArchival($relay_contact);
313                 }
314         }
315
316         /**
317          * Return a list of servers that we serve via the direct relay
318          *
319          * @param integer $item_id  id of the item that is sent
320          * @param array   $contacts Previously fetched contacts
321          * @param array   $networks Networks of the relay servers
322          * @return array of relay servers
323          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
324          */
325         public static function getDirectRelayList(int $item_id): array
326         {
327                 $serverlist = [];
328
329                 if (!DI::config()->get('system', 'relay_directly', false)) {
330                         return [];
331                 }
332
333                 // We distribute our stuff based on the parent to ensure that the thread will be complete
334                 $parent = Post::selectFirst(['uri-id'], ['id' => $item_id]);
335                 if (!DBA::isResult($parent)) {
336                         return [];
337                 }
338
339                 // Servers that want to get all content
340                 $servers = DBA::select('gserver', ['id', 'url', 'network'], ['relay-subscribe' => true, 'relay-scope' => 'all']);
341                 while ($server = DBA::fetch($servers)) {
342                         $serverlist[$server['id']] = $server;
343                 }
344                 DBA::close($servers);
345
346                 // All tags of the current post
347                 $tags = DBA::select('tag-view', ['name'], ['uri-id' => $parent['uri-id'], 'type' => Tag::HASHTAG]);
348                 $taglist = [];
349                 while ($tag = DBA::fetch($tags)) {
350                         $taglist[] = $tag['name'];
351                 }
352                 DBA::close($tags);
353
354                 // All servers who wants content with this tag
355                 $tagserverlist = [];
356                 if (!empty($taglist)) {
357                         $tagserver = DBA::select('gserver-tag', ['gserver-id'], ['tag' => $taglist]);
358                         while ($server = DBA::fetch($tagserver)) {
359                                 $tagserverlist[] = $server['gserver-id'];
360                         }
361                         DBA::close($tagserver);
362                 }
363
364                 // All addresses with the given id
365                 if (!empty($tagserverlist)) {
366                         $servers = DBA::select('gserver', ['id', 'url', 'network'], ['relay-subscribe' => true, 'relay-scope' => 'tags', 'id' => $tagserverlist]);
367                         while ($server = DBA::fetch($servers)) {
368                                 $serverlist[$server['id']] = $server;
369                         }
370                         DBA::close($servers);
371                 }
372
373                 $contacts = [];
374
375                 // Now we are collecting all relay contacts
376                 foreach ($serverlist as $gserver) {
377                         // We don't send messages to ourselves
378                         if (Strings::compareLink($gserver['url'], DI::baseUrl())) {
379                                 continue;
380                         }
381                         $contact = self::getContact($gserver);
382                         if (empty($contact)) {
383                                 continue;
384                         }
385                 }
386
387                 return $contacts;
388         }
389
390         /**
391          * Return a list of relay servers
392          *
393          * @param array $fields Field list
394          * @return array List of relay servers
395          * @throws Exception
396          */
397         public static function getList(array $fields = []): array
398         {
399                 return DBA::selectToArray('apcontact', $fields,
400                         ["`type` IN (?, ?) AND `url` IN (SELECT `url` FROM `contact` WHERE `uid` = ? AND `rel` = ?)", 'Application', 'Service', 0, Contact::FRIEND]);
401         }
402
403         /**
404          * Return a contact for a given server address or creates a dummy entry
405          *
406          * @param array $gserver Global server record
407          * @param array $fields  Fieldlist
408          * @return array|bool Array with the contact or false on error
409          * @throws \Exception
410          */
411         private static function getContact(array $gserver, array $fields = ['batch', 'id', 'url', 'name', 'network', 'protocol', 'archive', 'blocked'])
412         {
413                 // Fetch the relay contact
414                 $condition = ['uid' => 0, 'gsid' => $gserver['id'], 'contact-type' => Contact::TYPE_RELAY];
415                 $contact = DBA::selectFirst('contact', $fields, $condition);
416                 if (DBA::isResult($contact)) {
417                         if ($contact['archive'] || $contact['blocked']) {
418                                 return false;
419                         }
420                         return $contact;
421                 } else {
422                         self::updateContact($gserver);
423
424                         $contact = DBA::selectFirst('contact', $fields, $condition);
425                         if (DBA::isResult($contact)) {
426                                 return $contact;
427                         }
428                 }
429
430                 // It should never happen that we arrive here
431                 return [];
432         }
433
434         /**
435          * Resubscribe to all relay servers
436          *
437          * @return void
438          */
439         public static function reSubscribe()
440         {
441                 foreach (self::getList() as $server) {
442                         $success = ActivityPub\Transmitter::sendRelayFollow($server['url']);
443                         Logger::debug('Resubscribed', ['profile' => $server['url'], 'success' => $success]);
444                 }
445         }
446 }