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