]> git.mxchange.org Git - friendica.git/blob - src/Protocol/Relay.php
Configuration for the number of languages
[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                 $scope = $config->get('system', 'relay_scope');
70
71                 if ($scope == self::SCOPE_NONE) {
72                         Logger::info('Server does not accept relay posts - rejected', ['network' => $network, 'url' => $url]);
73                         return false;
74                 }
75
76                 if (Contact::isBlocked($authorid)) {
77                         Logger::info('Author is blocked - rejected', ['author' => $authorid, 'network' => $network, 'url' => $url]);
78                         return false;
79                 }
80
81                 if (Contact::isHidden($authorid)) {
82                         Logger::info('Author is hidden - rejected', ['author' => $authorid, 'network' => $network, 'url' => $url]);
83                         return false;
84                 }
85
86                 if (!empty($causerid)) {
87                         $contact = Contact::getById($causerid, ['url']);
88                         $causer = $contact['url'] ?? '';
89                 } else {
90                         $causer = '';
91                 }
92
93                 $body = ActivityPub\Processor::normalizeMentionLinks($body);
94
95                 $denyTags = [];
96
97                 if ($scope == self::SCOPE_TAGS) {
98                         $tagList = self::getSubscribedTags();
99                 } else {
100                         $tagList  = [];
101                 }
102
103                 $deny_tags = $config->get('system', 'relay_deny_tags');
104                 $tagitems = explode(',', mb_strtolower($deny_tags));
105                 foreach ($tagitems as $tag) {
106                         $tag = trim($tag, '# ');
107                         $denyTags[] = $tag;
108                 }
109
110                 if (!empty($tagList) || !empty($denyTags)) {
111                         $content = mb_strtolower(BBCode::toPlaintext($body, false));
112
113                         foreach ($tags as $tag) {
114                                 $tag = mb_strtolower($tag);
115                                 if (in_array($tag, $denyTags)) {
116                                         Logger::info('Unwanted hashtag found - rejected', ['hashtag' => $tag, 'network' => $network, 'url' => $url, 'causer' => $causer]);
117                                         return false;
118                                 }
119
120                                 if (in_array($tag, $tagList)) {
121                                         Logger::info('Subscribed hashtag found - accepted', ['hashtag' => $tag, 'network' => $network, 'url' => $url, 'causer' => $causer]);
122                                         return true;
123                                 }
124
125                                 // We check with "strpos" for performance issues. Only when this is true, the regular expression check is used
126                                 // RegExp is taken from here: https://medium.com/@shiba1014/regex-word-boundaries-with-unicode-207794f6e7ed
127                                 if ((strpos($content, $tag) !== false) && preg_match('/(?<=[\s,.:;"\']|^)' . preg_quote($tag, '/') . '(?=[\s,.:;"\']|$)/', $content)) {
128                                         Logger::info('Subscribed hashtag found in content - accepted', ['hashtag' => $tag, 'network' => $network, 'url' => $url, 'causer' => $causer]);
129                                         return true;
130                                 }
131                         }
132                 }
133
134                 if (!self::isWantedLanguage($body, 0, $authorid, $languages)) {
135                         Logger::info('Unwanted or Undetected language found - rejected', ['network' => $network, 'url' => $url, 'causer' => $causer, 'tags' => $tags]);
136                         return false;
137                 }
138
139                 if ($scope == self::SCOPE_ALL) {
140                         Logger::info('Server accept all posts - accepted', ['network' => $network, 'url' => $url, 'causer' => $causer, 'tags' => $tags]);
141                         return true;
142                 }
143
144                 Logger::info('No matching hashtags found - rejected', ['network' => $network, 'url' => $url, 'causer' => $causer, 'tags' => $tags]);
145                 return false;
146         }
147
148         /**
149          * Get a list of subscribed tags by both the users and the tags that are defined by the admin
150          *
151          * @return array
152          */
153         public static function getSubscribedTags(): array
154         {
155                 $systemTags  = [];
156                 $server_tags = DI::config()->get('system', 'relay_server_tags');
157
158                 foreach (explode(',', mb_strtolower($server_tags)) as $tag) {
159                         $systemTags[] = trim($tag, '# ');
160                 }
161
162                 if (DI::config()->get('system', 'relay_user_tags')) {
163                         $userTags = Search::getUserTags();
164                 } else {
165                         $userTags = [];
166                 }
167
168                 return array_unique(array_merge($systemTags, $userTags));
169         }
170
171         /**
172          * Detect the language of a post and decide if the post should be accepted
173          *
174          * @param string $body
175          * @param int    $uri_id
176          * @param int    $author_id
177          * @param array  $languages
178          * @return boolean
179          */
180         public static function isWantedLanguage(string $body, int $uri_id = 0, int $author_id = 0, array $languages = [])
181         {
182                 if (empty($languages) && (empty($body) || Smilies::isEmojiPost($body))) {
183                         Logger::debug('Empty body or only emojis', ['body' => $body]);
184                         return true;
185                 }
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)) {
196                         $cachekey = 'relay:isWantedLanguage';
197                         $user_languages = DI::cache()->get($cachekey);
198                         if (is_null($user_languages)) {
199                                 $user_languages = User::getLanguages();
200                                 DI::cache()->set($cachekey, $user_languages);
201                         }
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 }