]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub/Transmitter.php
Merge
[friendica.git] / src / Protocol / ActivityPub / Transmitter.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\ActivityPub;
23
24 use Friendica\Content\Feature;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Content\Text\Plaintext;
27 use Friendica\Core\Cache\Duration;
28 use Friendica\Core\Logger;
29 use Friendica\Core\Protocol;
30 use Friendica\Core\System;
31 use Friendica\Database\DBA;
32 use Friendica\DI;
33 use Friendica\Model\APContact;
34 use Friendica\Model\Contact;
35 use Friendica\Model\Conversation;
36 use Friendica\Model\Item;
37 use Friendica\Model\Profile;
38 use Friendica\Model\Photo;
39 use Friendica\Model\Tag;
40 use Friendica\Model\User;
41 use Friendica\Protocol\Activity;
42 use Friendica\Protocol\ActivityPub;
43 use Friendica\Util\DateTimeFormat;
44 use Friendica\Util\HTTPSignature;
45 use Friendica\Util\Images;
46 use Friendica\Util\JsonLD;
47 use Friendica\Util\LDSignature;
48 use Friendica\Util\Map;
49 use Friendica\Util\Network;
50 use Friendica\Util\XML;
51
52 require_once 'include/api.php';
53 require_once 'mod/share.php';
54
55 /**
56  * ActivityPub Transmitter Protocol class
57  *
58  * To-Do:
59  * @todo Undo Announce
60  */
61 class Transmitter
62 {
63         /**
64          * collects the lost of followers of the given owner
65          *
66          * @param array   $owner Owner array
67          * @param integer $page  Page number
68          *
69          * @return array of owners
70          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
71          */
72         public static function getFollowers($owner, $page = null)
73         {
74                 $condition = ['rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::FEDERATED, 'uid' => $owner['uid'],
75                         'self' => false, 'deleted' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
76                 $count = DBA::count('contact', $condition);
77
78                 $data = ['@context' => ActivityPub::CONTEXT];
79                 $data['id'] = DI::baseUrl() . '/followers/' . $owner['nickname'];
80                 $data['type'] = 'OrderedCollection';
81                 $data['totalItems'] = $count;
82
83                 // When we hide our friends we will only show the pure number but don't allow more.
84                 $profile = Profile::getByUID($owner['uid']);
85                 if (!empty($profile['hide-friends'])) {
86                         return $data;
87                 }
88
89                 if (empty($page)) {
90                         $data['first'] = DI::baseUrl() . '/followers/' . $owner['nickname'] . '?page=1';
91                 } else {
92                         $data['type'] = 'OrderedCollectionPage';
93                         $list = [];
94
95                         $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
96                         while ($contact = DBA::fetch($contacts)) {
97                                 $list[] = $contact['url'];
98                         }
99                         DBA::close($contacts);
100
101                         if (!empty($list)) {
102                                 $data['next'] = DI::baseUrl() . '/followers/' . $owner['nickname'] . '?page=' . ($page + 1);
103                         }
104
105                         $data['partOf'] = DI::baseUrl() . '/followers/' . $owner['nickname'];
106
107                         $data['orderedItems'] = $list;
108                 }
109
110                 return $data;
111         }
112
113         /**
114          * Create list of following contacts
115          *
116          * @param array   $owner Owner array
117          * @param integer $page  Page numbe
118          *
119          * @return array of following contacts
120          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
121          */
122         public static function getFollowing($owner, $page = null)
123         {
124                 $condition = ['rel' => [Contact::SHARING, Contact::FRIEND], 'network' => Protocol::FEDERATED, 'uid' => $owner['uid'],
125                         'self' => false, 'deleted' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
126                 $count = DBA::count('contact', $condition);
127
128                 $data = ['@context' => ActivityPub::CONTEXT];
129                 $data['id'] = DI::baseUrl() . '/following/' . $owner['nickname'];
130                 $data['type'] = 'OrderedCollection';
131                 $data['totalItems'] = $count;
132
133                 // When we hide our friends we will only show the pure number but don't allow more.
134                 $profile = Profile::getByUID($owner['uid']);
135                 if (!empty($profile['hide-friends'])) {
136                         return $data;
137                 }
138
139                 if (empty($page)) {
140                         $data['first'] = DI::baseUrl() . '/following/' . $owner['nickname'] . '?page=1';
141                 } else {
142                         $data['type'] = 'OrderedCollectionPage';
143                         $list = [];
144
145                         $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
146                         while ($contact = DBA::fetch($contacts)) {
147                                 $list[] = $contact['url'];
148                         }
149                         DBA::close($contacts);
150
151                         if (!empty($list)) {
152                                 $data['next'] = DI::baseUrl() . '/following/' . $owner['nickname'] . '?page=' . ($page + 1);
153                         }
154
155                         $data['partOf'] = DI::baseUrl() . '/following/' . $owner['nickname'];
156
157                         $data['orderedItems'] = $list;
158                 }
159
160                 return $data;
161         }
162
163         /**
164          * Public posts for the given owner
165          *
166          * @param array   $owner Owner array
167          * @param integer $page  Page numbe
168          *
169          * @return array of posts
170          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
171          * @throws \ImagickException
172          */
173         public static function getOutbox($owner, $page = null)
174         {
175                 $public_contact = Contact::getIdForURL($owner['url'], 0, true);
176
177                 $condition = ['uid' => 0, 'contact-id' => $public_contact, 'author-id' => $public_contact,
178                         'private' => [Item::PUBLIC, Item::UNLISTED], 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
179                         'deleted' => false, 'visible' => true, 'moderated' => false];
180                 $count = DBA::count('item', $condition);
181
182                 $data = ['@context' => ActivityPub::CONTEXT];
183                 $data['id'] = DI::baseUrl() . '/outbox/' . $owner['nickname'];
184                 $data['type'] = 'OrderedCollection';
185                 $data['totalItems'] = $count;
186
187                 if (empty($page)) {
188                         $data['first'] = DI::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=1';
189                 } else {
190                         $data['type'] = 'OrderedCollectionPage';
191                         $list = [];
192
193                         $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
194
195                         $items = Item::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
196                         while ($item = Item::fetch($items)) {
197                                 $activity = self::createActivityFromItem($item['id'], true);
198                                 $activity['type'] = $activity['type'] == 'Update' ? 'Create' : $activity['type'];
199
200                                 // Only list "Create" activity objects here, no reshares
201                                 if (!empty($activity['object']) && ($activity['type'] == 'Create')) {
202                                         $list[] = $activity['object'];
203                                 }
204                         }
205
206                         if (!empty($list)) {
207                                 $data['next'] = DI::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=' . ($page + 1);
208                         }
209
210                         $data['partOf'] = DI::baseUrl() . '/outbox/' . $owner['nickname'];
211
212                         $data['orderedItems'] = $list;
213                 }
214
215                 return $data;
216         }
217
218         /**
219          * Return the service array containing information the used software and it's url
220          *
221          * @return array with service data
222          */
223         private static function getService()
224         {
225                 return ['type' => 'Service',
226                         'name' =>  FRIENDICA_PLATFORM . " '" . FRIENDICA_CODENAME . "' " . FRIENDICA_VERSION . '-' . DB_UPDATE_VERSION,
227                         'url' => DI::baseUrl()->get()];
228         }
229
230         /**
231          * Return the ActivityPub profile of the given user
232          *
233          * @param integer $uid User ID
234          * @return array with profile data
235          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
236          */
237         public static function getProfile($uid)
238         {
239                 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
240                         'account_removed' => false, 'verified' => true];
241                 $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
242                 $user = DBA::selectFirst('user', $fields, $condition);
243                 if (!DBA::isResult($user)) {
244                         return [];
245                 }
246
247                 $fields = ['locality', 'region', 'country-name'];
248                 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid]);
249                 if (!DBA::isResult($profile)) {
250                         return [];
251                 }
252
253                 $fields = ['name', 'url', 'location', 'about', 'avatar', 'photo'];
254                 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
255                 if (!DBA::isResult($contact)) {
256                         return [];
257                 }
258
259                 $data = ['@context' => ActivityPub::CONTEXT];
260                 $data['id'] = $contact['url'];
261                 $data['diaspora:guid'] = $user['guid'];
262                 $data['type'] = ActivityPub::ACCOUNT_TYPES[$user['account-type']];
263                 $data['following'] = DI::baseUrl() . '/following/' . $user['nickname'];
264                 $data['followers'] = DI::baseUrl() . '/followers/' . $user['nickname'];
265                 $data['inbox'] = DI::baseUrl() . '/inbox/' . $user['nickname'];
266                 $data['outbox'] = DI::baseUrl() . '/outbox/' . $user['nickname'];
267                 $data['preferredUsername'] = $user['nickname'];
268                 $data['name'] = $contact['name'];
269                 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
270                         'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
271                 $data['summary'] = BBCode::convert($contact['about'], false);
272                 $data['url'] = $contact['url'];
273                 $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
274                 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
275                         'owner' => $contact['url'],
276                         'publicKeyPem' => $user['pubkey']];
277                 $data['endpoints'] = ['sharedInbox' => DI::baseUrl() . '/inbox'];
278                 $data['icon'] = ['type' => 'Image',
279                         'url' => $contact['photo']];
280
281                 $data['generator'] = self::getService();
282
283                 // tags: https://kitty.town/@inmysocks/100656097926961126.json
284                 return $data;
285         }
286
287         /**
288          * @param string $username
289          * @return array
290          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
291          */
292         public static function getDeletedUser($username)
293         {
294                 return [
295                         '@context' => ActivityPub::CONTEXT,
296                         'id' => DI::baseUrl() . '/profile/' . $username,
297                         'type' => 'Tombstone',
298                         'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
299                         'updated' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
300                         'deleted' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
301                 ];
302         }
303
304         /**
305          * Returns an array with permissions of a given item array
306          *
307          * @param array $item
308          *
309          * @return array with permissions
310          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
311          * @throws \ImagickException
312          */
313         private static function fetchPermissionBlockFromConversation($item)
314         {
315                 if (empty($item['thr-parent'])) {
316                         return [];
317                 }
318
319                 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
320                 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
321                 if (!DBA::isResult($conversation)) {
322                         return [];
323                 }
324
325                 $activity = json_decode($conversation['source'], true);
326
327                 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
328                 $profile = APContact::getByURL($actor);
329
330                 $item_profile = APContact::getByURL($item['author-link']);
331                 $exclude[] = $item['author-link'];
332
333                 if ($item['gravity'] == GRAVITY_PARENT) {
334                         $exclude[] = $item['owner-link'];
335                 }
336
337                 $permissions['to'][] = $actor;
338
339                 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
340                         if (empty($activity[$element])) {
341                                 continue;
342                         }
343                         if (is_string($activity[$element])) {
344                                 $activity[$element] = [$activity[$element]];
345                         }
346
347                         foreach ($activity[$element] as $receiver) {
348                                 if (empty($receiver)) {
349                                         continue;
350                                 }
351
352                                 if (!empty($profile['followers']) && $receiver == $profile['followers'] && !empty($item_profile['followers'])) {
353                                         $permissions[$element][] = $item_profile['followers'];
354                                 } elseif (!in_array($receiver, $exclude)) {
355                                         $permissions[$element][] = $receiver;
356                                 }
357                         }
358                 }
359                 return $permissions;
360         }
361
362         /**
363          * Creates an array of permissions from an item thread
364          *
365          * @param array   $item       Item array
366          * @param boolean $blindcopy  addressing via "bcc" or "cc"?
367          * @param integer $last_id    Last item id for adding receivers
368          * @param boolean $forum_mode "true" means that we are sending content to a forum
369          *
370          * @return array with permission data
371          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
372          * @throws \ImagickException
373          */
374         private static function createPermissionBlockForItem($item, $blindcopy, $last_id = 0, $forum_mode = false)
375         {
376                 if ($last_id == 0) {
377                         $last_id = $item['id'];
378                 }
379
380                 $always_bcc = false;
381
382                 // Check if we should always deliver our stuff via BCC
383                 if (!empty($item['uid'])) {
384                         $profile = Profile::getByUID($item['uid']);
385                         if (!empty($profile)) {
386                                 $always_bcc = $profile['hide-friends'];
387                         }
388                 }
389
390                 if (DI::config()->get('system', 'ap_always_bcc')) {
391                         $always_bcc = true;
392                 }
393
394                 if (self::isAnnounce($item) || DI::config()->get('debug', 'total_ap_delivery')) {
395                         // Will be activated in a later step
396                         $networks = Protocol::FEDERATED;
397                 } else {
398                         // For now only send to these contacts:
399                         $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
400                 }
401
402                 $data = ['to' => [], 'cc' => [], 'bcc' => []];
403
404                 if ($item['gravity'] == GRAVITY_PARENT) {
405                         $actor_profile = APContact::getByURL($item['owner-link']);
406                 } else {
407                         $actor_profile = APContact::getByURL($item['author-link']);
408                 }
409
410                 $terms = Tag::ArrayFromURIId($item['uri-id'], [Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION]);
411
412                 if ($item['private'] != Item::PRIVATE) {
413                         // Directly mention the original author upon a quoted reshare.
414                         // Else just ensure that the original author receives the reshare.
415                         $announce = self::getAnnounceArray($item);
416                         if (!empty($announce['comment'])) {
417                                 $data['to'][] = $announce['actor']['url'];
418                         } elseif (!empty($announce)) {
419                                 $data['cc'][] = $announce['actor']['url'];
420                         }
421
422                         $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
423
424                         // Check if the item is completely public or unlisted
425                         if ($item['private'] == Item::PUBLIC) {
426                                 $data['to'][] = ActivityPub::PUBLIC_COLLECTION;
427                         } else {
428                                 $data['cc'][] = ActivityPub::PUBLIC_COLLECTION;
429                         }
430
431                         foreach ($terms as $term) {
432                                 $profile = APContact::getByURL($term['url'], false);
433                                 if (!empty($profile)) {
434                                         $data['to'][] = $profile['url'];
435                                 }
436                         }
437                 } else {
438                         $receiver_list = Item::enumeratePermissions($item, true);
439
440                         foreach ($terms as $term) {
441                                 $cid = Contact::getIdForURL($term['url'], $item['uid']);
442                                 if (!empty($cid) && in_array($cid, $receiver_list)) {
443                                         $contact = DBA::selectFirst('contact', ['url', 'network', 'protocol'], ['id' => $cid]);
444                                         if (!DBA::isResult($contact) || (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB))) {
445                                                 continue;
446                                         }
447
448                                         if (!empty($profile = APContact::getByURL($contact['url'], false))) {
449                                                 $data['to'][] = $profile['url'];
450                                         }
451                                 }
452                         }
453
454                         foreach ($receiver_list as $receiver) {
455                                 $contact = DBA::selectFirst('contact', ['url', 'hidden', 'network', 'protocol'], ['id' => $receiver]);
456                                 if (!DBA::isResult($contact) || (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB))) {
457                                         continue;
458                                 }
459
460                                 if (!empty($profile = APContact::getByURL($contact['url'], false))) {
461                                         if ($contact['hidden'] || $always_bcc) {
462                                                 $data['bcc'][] = $profile['url'];
463                                         } else {
464                                                 $data['cc'][] = $profile['url'];
465                                         }
466                                 }
467                         }
468                 }
469
470                 if (!empty($item['parent'])) {
471                         $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], ['parent' => $item['parent']]);
472                         while ($parent = Item::fetch($parents)) {
473                                 if ($parent['gravity'] == GRAVITY_PARENT) {
474                                         $profile = APContact::getByURL($parent['owner-link'], false);
475                                         if (!empty($profile)) {
476                                                 if ($item['gravity'] != GRAVITY_PARENT) {
477                                                         // Comments to forums are directed to the forum
478                                                         // But comments to forums aren't directed to the followers collection
479                                                         if ($profile['type'] == 'Group') {
480                                                                 $data['to'][] = $profile['url'];
481                                                         } else {
482                                                                 $data['cc'][] = $profile['url'];
483                                                                 if (($item['private'] != Item::PRIVATE) && !empty($actor_profile['followers'])) {
484                                                                         $data['cc'][] = $actor_profile['followers'];
485                                                                 }
486                                                         }
487                                                 } else {
488                                                         // Public thread parent post always are directed to the followers
489                                                         if (($item['private'] != Item::PRIVATE) && !$forum_mode) {
490                                                                 $data['cc'][] = $actor_profile['followers'];
491                                                         }
492                                                 }
493                                         }
494                                 }
495
496                                 // Don't include data from future posts
497                                 if ($parent['id'] >= $last_id) {
498                                         continue;
499                                 }
500
501                                 $profile = APContact::getByURL($parent['author-link'], false);
502                                 if (!empty($profile)) {
503                                         if (($profile['type'] == 'Group') || ($parent['uri'] == $item['thr-parent'])) {
504                                                 $data['to'][] = $profile['url'];
505                                         } else {
506                                                 $data['cc'][] = $profile['url'];
507                                         }
508                                 }
509                         }
510                         DBA::close($parents);
511                 }
512
513                 $data['to'] = array_unique($data['to']);
514                 $data['cc'] = array_unique($data['cc']);
515                 $data['bcc'] = array_unique($data['bcc']);
516
517                 if (($key = array_search($item['author-link'], $data['to'])) !== false) {
518                         unset($data['to'][$key]);
519                 }
520
521                 if (($key = array_search($item['author-link'], $data['cc'])) !== false) {
522                         unset($data['cc'][$key]);
523                 }
524
525                 if (($key = array_search($item['author-link'], $data['bcc'])) !== false) {
526                         unset($data['bcc'][$key]);
527                 }
528
529                 foreach ($data['to'] as $to) {
530                         if (($key = array_search($to, $data['cc'])) !== false) {
531                                 unset($data['cc'][$key]);
532                         }
533
534                         if (($key = array_search($to, $data['bcc'])) !== false) {
535                                 unset($data['bcc'][$key]);
536                         }
537                 }
538
539                 foreach ($data['cc'] as $cc) {
540                         if (($key = array_search($cc, $data['bcc'])) !== false) {
541                                 unset($data['bcc'][$key]);
542                         }
543                 }
544
545                 $receivers = ['to' => array_values($data['to']), 'cc' => array_values($data['cc']), 'bcc' => array_values($data['bcc'])];
546
547                 if (!$blindcopy) {
548                         unset($receivers['bcc']);
549                 }
550
551                 return $receivers;
552         }
553
554         /**
555          * Check if an inbox is archived
556          *
557          * @param string $url Inbox url
558          *
559          * @return boolean "true" if inbox is archived
560          */
561         private static function archivedInbox($url)
562         {
563                 return DBA::exists('inbox-status', ['url' => $url, 'archive' => true]);
564         }
565
566         /**
567          * Fetches a list of inboxes of followers of a given user
568          *
569          * @param integer $uid      User ID
570          * @param boolean $personal fetch personal inboxes
571          *
572          * @return array of follower inboxes
573          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
574          * @throws \ImagickException
575          */
576         public static function fetchTargetInboxesforUser($uid, $personal = false)
577         {
578                 $inboxes = [];
579
580                 if (DI::config()->get('debug', 'total_ap_delivery')) {
581                         // Will be activated in a later step
582                         $networks = Protocol::FEDERATED;
583                 } else {
584                         // For now only send to these contacts:
585                         $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
586                 }
587
588                 $condition = ['uid' => $uid, 'archive' => false, 'pending' => false];
589
590                 if (!empty($uid)) {
591                         $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
592                 }
593
594                 $contacts = DBA::select('contact', ['url', 'network', 'protocol'], $condition);
595                 while ($contact = DBA::fetch($contacts)) {
596                         if (Contact::isLocal($contact['url'])) {
597                                 continue;
598                         }
599
600                         if (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB)) {
601                                 continue;
602                         }
603
604                         if (Network::isUrlBlocked($contact['url'])) {
605                                 continue;
606                         }
607
608                         $profile = APContact::getByURL($contact['url'], false);
609                         if (!empty($profile)) {
610                                 if (empty($profile['sharedinbox']) || $personal) {
611                                         $target = $profile['inbox'];
612                                 } else {
613                                         $target = $profile['sharedinbox'];
614                                 }
615                                 if (!self::archivedInbox($target)) {
616                                         $inboxes[$target] = $target;
617                                 }
618                         }
619                 }
620                 DBA::close($contacts);
621
622                 return $inboxes;
623         }
624
625         /**
626          * Fetches an array of inboxes for the given item and user
627          *
628          * @param array   $item       Item array
629          * @param integer $uid        User ID
630          * @param boolean $personal   fetch personal inboxes
631          * @param integer $last_id    Last item id for adding receivers
632          * @param boolean $forum_mode "true" means that we are sending content to a forum
633          * @return array with inboxes
634          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
635          * @throws \ImagickException
636          */
637         public static function fetchTargetInboxes($item, $uid, $personal = false, $last_id = 0, $forum_mode = false)
638         {
639                 $permissions = self::createPermissionBlockForItem($item, true, $last_id, $forum_mode);
640                 if (empty($permissions)) {
641                         return [];
642                 }
643
644                 $inboxes = [];
645
646                 if ($item['gravity'] == GRAVITY_ACTIVITY) {
647                         $item_profile = APContact::getByURL($item['author-link'], false);
648                 } else {
649                         $item_profile = APContact::getByURL($item['owner-link'], false);
650                 }
651
652                 foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
653                         if (empty($permissions[$element])) {
654                                 continue;
655                         }
656
657                         $blindcopy = in_array($element, ['bto', 'bcc']);
658
659                         foreach ($permissions[$element] as $receiver) {
660                                 if (empty($receiver) || Network::isUrlBlocked($receiver)) {
661                                         continue;
662                                 }
663
664                                 if ($receiver == $item_profile['followers']) {
665                                         $inboxes = array_merge($inboxes, self::fetchTargetInboxesforUser($uid, $personal));
666                                 } else {
667                                         if (Contact::isLocal($receiver)) {
668                                                 continue;
669                                         }
670
671                                         $profile = APContact::getByURL($receiver, false);
672                                         if (!empty($profile)) {
673                                                 if (empty($profile['sharedinbox']) || $personal || $blindcopy) {
674                                                         $target = $profile['inbox'];
675                                                 } else {
676                                                         $target = $profile['sharedinbox'];
677                                                 }
678                                                 if (!self::archivedInbox($target)) {
679                                                         $inboxes[$target] = $target;
680                                                 }
681                                         }
682                                 }
683                         }
684                 }
685
686                 return $inboxes;
687         }
688
689         /**
690          * Creates an array in the structure of the item table for a given mail id
691          *
692          * @param integer $mail_id
693          *
694          * @return array
695          * @throws \Exception
696          */
697         public static function ItemArrayFromMail($mail_id)
698         {
699                 $mail = DBA::selectFirst('mail', [], ['id' => $mail_id]);
700                 if (!DBA::isResult($mail)) {
701                         return [];
702                 }
703
704                 $reply = DBA::selectFirst('mail', ['uri'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
705
706                 // Making the post more compatible for Mastodon by:
707                 // - Making it a note and not an article (no title)
708                 // - Moving the title into the "summary" field that is used as a "content warning"
709                 $mail['body'] = '[abstract]' . $mail['title'] . "[/abstract]\n" . $mail['body'];
710                 $mail['title'] = '';
711
712                 $mail['author-link'] = $mail['owner-link'] = $mail['from-url'];
713                 $mail['allow_cid'] = '<'.$mail['contact-id'].'>';
714                 $mail['allow_gid'] = '';
715                 $mail['deny_cid'] = '';
716                 $mail['deny_gid'] = '';
717                 $mail['private'] = true;
718                 $mail['deleted'] = false;
719                 $mail['edited'] = $mail['created'];
720                 $mail['plink'] = $mail['uri'];
721                 $mail['thr-parent'] = $reply['uri'];
722                 $mail['gravity'] = ($mail['reply'] ? GRAVITY_COMMENT: GRAVITY_PARENT);
723
724                 $mail['event-type'] = '';
725                 $mail['attach'] = '';
726
727                 $mail['parent'] = 0;
728
729                 return $mail;
730         }
731
732         /**
733          * Creates an activity array for a given mail id
734          *
735          * @param integer $mail_id
736          * @param boolean $object_mode Is the activity item is used inside another object?
737          *
738          * @return array of activity
739          * @throws \Exception
740          */
741         public static function createActivityFromMail($mail_id, $object_mode = false)
742         {
743                 $mail = self::ItemArrayFromMail($mail_id);
744                 $object = self::createNote($mail);
745
746                 if (!$object_mode) {
747                         $data = ['@context' => ActivityPub::CONTEXT];
748                 } else {
749                         $data = [];
750                 }
751
752                 $data['id'] = $mail['uri'] . '#Create';
753                 $data['type'] = 'Create';
754                 $data['actor'] = $mail['author-link'];
755                 $data['published'] = DateTimeFormat::utc($mail['created'] . '+00:00', DateTimeFormat::ATOM);
756                 $data['instrument'] = self::getService();
757                 $data = array_merge($data, self::createPermissionBlockForItem($mail, true));
758
759                 if (empty($data['to']) && !empty($data['cc'])) {
760                         $data['to'] = $data['cc'];
761                 }
762
763                 if (empty($data['to']) && !empty($data['bcc'])) {
764                         $data['to'] = $data['bcc'];
765                 }
766
767                 unset($data['cc']);
768                 unset($data['bcc']);
769
770                 $object['to'] = $data['to'];
771                 $object['tag'] = [['type' => 'Mention', 'href' => $object['to'][0], 'name' => '']];
772
773                 unset($object['cc']);
774                 unset($object['bcc']);
775
776                 $data['directMessage'] = true;
777
778                 $data['object'] = $object;
779
780                 $owner = User::getOwnerDataById($mail['uid']);
781
782                 if (!$object_mode && !empty($owner)) {
783                         return LDSignature::sign($data, $owner);
784                 } else {
785                         return $data;
786                 }
787         }
788
789         /**
790          * Returns the activity type of a given item
791          *
792          * @param array $item
793          *
794          * @return string with activity type
795          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
796          * @throws \ImagickException
797          */
798         private static function getTypeOfItem($item)
799         {
800                 $reshared = false;
801
802                 // Only check for a reshare, if it is a real reshare and no quoted reshare
803                 if (strpos($item['body'], "[share") === 0) {
804                         $announce = self::getAnnounceArray($item);
805                         $reshared = !empty($announce);
806                 }
807
808                 if ($reshared) {
809                         $type = 'Announce';
810                 } elseif ($item['verb'] == Activity::POST) {
811                         if ($item['created'] == $item['edited']) {
812                                 $type = 'Create';
813                         } else {
814                                 $type = 'Update';
815                         }
816                 } elseif ($item['verb'] == Activity::LIKE) {
817                         $type = 'Like';
818                 } elseif ($item['verb'] == Activity::DISLIKE) {
819                         $type = 'Dislike';
820                 } elseif ($item['verb'] == Activity::ATTEND) {
821                         $type = 'Accept';
822                 } elseif ($item['verb'] == Activity::ATTENDNO) {
823                         $type = 'Reject';
824                 } elseif ($item['verb'] == Activity::ATTENDMAYBE) {
825                         $type = 'TentativeAccept';
826                 } elseif ($item['verb'] == Activity::FOLLOW) {
827                         $type = 'Follow';
828                 } elseif ($item['verb'] == Activity::TAG) {
829                         $type = 'Add';
830                 } else {
831                         $type = '';
832                 }
833
834                 return $type;
835         }
836
837         /**
838          * Creates the activity or fetches it from the cache
839          *
840          * @param integer $item_id
841          * @param boolean $force Force new cache entry
842          *
843          * @return array with the activity
844          * @throws \Exception
845          */
846         public static function createCachedActivityFromItem($item_id, $force = false)
847         {
848                 $cachekey = 'APDelivery:createActivity:' . $item_id;
849
850                 if (!$force) {
851                         $data = DI::cache()->get($cachekey);
852                         if (!is_null($data)) {
853                                 return $data;
854                         }
855                 }
856
857                 $data = ActivityPub\Transmitter::createActivityFromItem($item_id);
858
859                 DI::cache()->set($cachekey, $data, Duration::QUARTER_HOUR);
860                 return $data;
861         }
862
863         /**
864          * Creates an activity array for a given item id
865          *
866          * @param integer $item_id
867          * @param boolean $object_mode Is the activity item is used inside another object?
868          *
869          * @return array of activity
870          * @throws \Exception
871          */
872         public static function createActivityFromItem($item_id, $object_mode = false)
873         {
874                 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
875
876                 if (!DBA::isResult($item)) {
877                         return false;
878                 }
879
880                 if ($item['wall'] && ($item['uri'] == $item['parent-uri'])) {
881                         $owner = User::getOwnerDataById($item['uid']);
882                         if (($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) && ($item['author-link'] != $owner['url'])) {
883                                 $type = 'Announce';
884
885                                 // Disguise forum posts as reshares. Will later be converted to a real announce
886                                 $item['body'] = share_header($item['author-name'], $item['author-link'], $item['author-avatar'],
887                                         $item['guid'], $item['created'], $item['plink']) . $item['body'] . '[/share]';
888                         }
889                 }
890
891                 if (empty($type)) {
892                         $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
893                         $conversation = DBA::selectFirst('conversation', ['source'], $condition);
894                         if (DBA::isResult($conversation)) {
895                                 $data = json_decode($conversation['source'], true);
896                                 if (!empty($data)) {
897                                         return $data;
898                                 }
899                         }
900
901                         $type = self::getTypeOfItem($item);
902                 }
903
904                 if (!$object_mode) {
905                         $data = ['@context' => ActivityPub::CONTEXT];
906
907                         if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
908                                 $type = 'Undo';
909                         } elseif ($item['deleted']) {
910                                 $type = 'Delete';
911                         }
912                 } else {
913                         $data = [];
914                 }
915
916                 $data['id'] = $item['uri'] . '#' . $type;
917                 $data['type'] = $type;
918
919                 if (Item::isForumPost($item) && ($type != 'Announce')) {
920                         $data['actor'] = $item['author-link'];
921                 } else {
922                         $data['actor'] = $item['owner-link'];
923                 }
924
925                 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
926
927                 $data['instrument'] = self::getService();
928
929                 $data = array_merge($data, self::createPermissionBlockForItem($item, false));
930
931                 if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
932                         $data['object'] = self::createNote($item);
933                 } elseif ($data['type'] == 'Add') {
934                         $data = self::createAddTag($item, $data);
935                 } elseif ($data['type'] == 'Announce') {
936                         $data = self::createAnnounce($item, $data);
937                 } elseif ($data['type'] == 'Follow') {
938                         $data['object'] = $item['parent-uri'];
939                 } elseif ($data['type'] == 'Undo') {
940                         $data['object'] = self::createActivityFromItem($item_id, true);
941                 } else {
942                         $data['diaspora:guid'] = $item['guid'];
943                         if (!empty($item['signed_text'])) {
944                                 $data['diaspora:like'] = $item['signed_text'];
945                         }
946                         $data['object'] = $item['thr-parent'];
947                 }
948
949                 if (!empty($item['contact-uid'])) {
950                         $uid = $item['contact-uid'];
951                 } else {
952                         $uid = $item['uid'];
953                 }
954
955                 $owner = User::getOwnerDataById($uid);
956
957                 if (!$object_mode && !empty($owner)) {
958                         return LDSignature::sign($data, $owner);
959                 } else {
960                         return $data;
961                 }
962
963                 /// @todo Create "conversation" entry
964         }
965
966         /**
967          * Creates a location entry for a given item array
968          *
969          * @param array $item
970          *
971          * @return array with location array
972          */
973         private static function createLocation($item)
974         {
975                 $location = ['type' => 'Place'];
976
977                 if (!empty($item['location'])) {
978                         $location['name'] = $item['location'];
979                 }
980
981                 $coord = [];
982
983                 if (empty($item['coord'])) {
984                         $coord = Map::getCoordinates($item['location']);
985                 } else {
986                         $coords = explode(' ', $item['coord']);
987                         if (count($coords) == 2) {
988                                 $coord = ['lat' => $coords[0], 'lon' => $coords[1]];
989                         }
990                 }
991
992                 if (!empty($coord['lat']) && !empty($coord['lon'])) {
993                         $location['latitude'] = $coord['lat'];
994                         $location['longitude'] = $coord['lon'];
995                 }
996
997                 return $location;
998         }
999
1000         /**
1001          * Returns a tag array for a given item array
1002          *
1003          * @param array $item
1004          *
1005          * @return array of tags
1006          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1007          */
1008         private static function createTagList($item)
1009         {
1010                 $tags = [];
1011
1012                 $terms = Tag::ArrayFromURIId($item['uri-id'], [Tag::HASHTAG, Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION]);
1013                 foreach ($terms as $term) {
1014                         if ($term['type'] == Tag::HASHTAG) {
1015                                 $url = DI::baseUrl() . '/search?tag=' . urlencode($term['term']);
1016                                 $tags[] = ['type' => 'Hashtag', 'href' => $url, 'name' => '#' . $term['term']];
1017                         } else {
1018                                 $contact = Contact::getDetailsByURL($term['url']);
1019                                 if (!empty($contact['addr'])) {
1020                                         $mention = '@' . $contact['addr'];
1021                                 } else {
1022                                         $mention = '@' . $term['url'];
1023                                 }
1024
1025                                 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
1026                         }
1027                 }
1028
1029                 $announce = self::getAnnounceArray($item);
1030                 // Mention the original author upon commented reshares
1031                 if (!empty($announce['comment'])) {
1032                         $tags[] = ['type' => 'Mention', 'href' => $announce['actor']['url'], 'name' => '@' . $announce['actor']['addr']];
1033                 }
1034
1035                 return $tags;
1036         }
1037
1038         /**
1039          * Adds attachment data to the JSON document
1040          *
1041          * @param array  $item Data of the item that is to be posted
1042          * @param string $type Object type
1043          *
1044          * @return array with attachment data
1045          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1046          */
1047         private static function createAttachmentList($item, $type)
1048         {
1049                 $attachments = [];
1050
1051                 // Currently deactivated, since it creates side effects on Mastodon and Pleroma.
1052                 // It will be reactivated, once this cleared.
1053                 /*
1054                 $attach_data = BBCode::getAttachmentData($item['body']);
1055                 if (!empty($attach_data['url'])) {
1056                         $attachment = ['type' => 'Page',
1057                                 'mediaType' => 'text/html',
1058                                 'url' => $attach_data['url']];
1059
1060                         if (!empty($attach_data['title'])) {
1061                                 $attachment['name'] = $attach_data['title'];
1062                         }
1063
1064                         if (!empty($attach_data['description'])) {
1065                                 $attachment['summary'] = $attach_data['description'];
1066                         }
1067
1068                         if (!empty($attach_data['image'])) {
1069                                 $imgdata = Images::getInfoFromURLCached($attach_data['image']);
1070                                 if ($imgdata) {
1071                                         $attachment['icon'] = ['type' => 'Image',
1072                                                 'mediaType' => $imgdata['mime'],
1073                                                 'width' => $imgdata[0],
1074                                                 'height' => $imgdata[1],
1075                                                 'url' => $attach_data['image']];
1076                                 }
1077                         }
1078
1079                         $attachments[] = $attachment;
1080                 }
1081                 */
1082                 $arr = explode('[/attach],', $item['attach']);
1083                 if (count($arr)) {
1084                         foreach ($arr as $r) {
1085                                 $matches = false;
1086                                 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches);
1087                                 if ($cnt) {
1088                                         $attributes = ['type' => 'Document',
1089                                                         'mediaType' => $matches[3],
1090                                                         'url' => $matches[1],
1091                                                         'name' => null];
1092
1093                                         if (trim($matches[4]) != '') {
1094                                                 $attributes['name'] = trim($matches[4]);
1095                                         }
1096
1097                                         $attachments[] = $attributes;
1098                                 }
1099                         }
1100                 }
1101
1102                 if ($type != 'Note') {
1103                         return $attachments;
1104                 }
1105
1106                 // Simplify image codes
1107                 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $item['body']);
1108
1109                 // Grab all pictures without alternative descriptions and create attachments out of them
1110                 if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures)) {
1111                         foreach ($pictures[1] as $picture) {
1112                                 $imgdata = Images::getInfoFromURLCached($picture);
1113                                 if ($imgdata) {
1114                                         $attachments[] = ['type' => 'Document',
1115                                                 'mediaType' => $imgdata['mime'],
1116                                                 'url' => $picture,
1117                                                 'name' => null];
1118                                 }
1119                         }
1120                 }
1121
1122                 // Grab all pictures with alternative description and create attachments out of them
1123                 if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
1124                         foreach ($pictures as $picture) {
1125                                 $imgdata = Images::getInfoFromURLCached($picture[1]);
1126                                 if ($imgdata) {
1127                                         $attachments[] = ['type' => 'Document',
1128                                                 'mediaType' => $imgdata['mime'],
1129                                                 'url' => $picture[1],
1130                                                 'name' => $picture[2]];
1131                                 }
1132                         }
1133                 }
1134
1135                 return $attachments;
1136         }
1137
1138         /**
1139          * Callback function to replace a Friendica style mention in a mention that is used on AP
1140          *
1141          * @param array $match Matching values for the callback
1142          * @return string Replaced mention
1143          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1144          */
1145         private static function mentionCallback($match)
1146         {
1147                 if (empty($match[1])) {
1148                         return '';
1149                 }
1150
1151                 $data = Contact::getDetailsByURL($match[1]);
1152                 if (empty($data['nick'])) {
1153                         return $match[0];
1154                 }
1155
1156                 return '@[url=' . $data['url'] . ']' . $data['nick'] . '[/url]';
1157         }
1158
1159         /**
1160          * Remove image elements since they are added as attachment
1161          *
1162          * @param string $body
1163          *
1164          * @return string with removed images
1165          */
1166         private static function removePictures($body)
1167         {
1168                 // Simplify image codes
1169                 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
1170                 $body = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", '[img]$1[/img]', $body);
1171
1172                 // Now remove local links
1173                 $body = preg_replace_callback(
1174                         '/\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]/Usi',
1175                         function ($match) {
1176                                 // We remove the link when it is a link to a local photo page
1177                                 if (Photo::isLocalPage($match[1])) {
1178                                         return '';
1179                                 }
1180                                 // otherwise we just return the link
1181                                 return '[url]' . $match[1] . '[/url]';
1182                         },
1183                         $body
1184                 );
1185
1186                 // Remove all pictures
1187                 $body = preg_replace("/\[img\]([^\[\]]*)\[\/img\]/Usi", '', $body);
1188
1189                 return $body;
1190         }
1191
1192         /**
1193          * Fetches the "context" value for a givem item array from the "conversation" table
1194          *
1195          * @param array $item
1196          *
1197          * @return string with context url
1198          * @throws \Exception
1199          */
1200         private static function fetchContextURLForItem($item)
1201         {
1202                 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
1203                 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
1204                         $context_uri = $conversation['conversation-href'];
1205                 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
1206                         $context_uri = $conversation['conversation-uri'];
1207                 } else {
1208                         $context_uri = $item['parent-uri'] . '#context';
1209                 }
1210                 return $context_uri;
1211         }
1212
1213         /**
1214          * Returns if the post contains sensitive content ("nsfw")
1215          *
1216          * @param integer $uri_id
1217          *
1218          * @return boolean
1219          * @throws \Exception
1220          */
1221         private static function isSensitive($uri_id)
1222         {
1223                 return DBA::exists('tag-view', ['uri-id' => $uri_id, 'name' => 'nsfw']);
1224         }
1225
1226         /**
1227          * Creates event data
1228          *
1229          * @param array $item
1230          *
1231          * @return array with the event data
1232          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1233          */
1234         public static function createEvent($item)
1235         {
1236                 $event = [];
1237                 $event['name'] = $item['event-summary'];
1238                 $event['content'] = BBCode::convert($item['event-desc'], false, 9);
1239                 $event['startTime'] = DateTimeFormat::utc($item['event-start'] . '+00:00', DateTimeFormat::ATOM);
1240
1241                 if (!$item['event-nofinish']) {
1242                         $event['endTime'] = DateTimeFormat::utc($item['event-finish'] . '+00:00', DateTimeFormat::ATOM);
1243                 }
1244
1245                 if (!empty($item['event-location'])) {
1246                         $item['location'] = $item['event-location'];
1247                         $event['location'] = self::createLocation($item);
1248                 }
1249
1250                 return $event;
1251         }
1252
1253         /**
1254          * Creates a note/article object array
1255          *
1256          * @param array $item
1257          *
1258          * @return array with the object data
1259          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1260          * @throws \ImagickException
1261          */
1262         public static function createNote($item)
1263         {
1264                 if (empty($item)) {
1265                         return [];
1266                 }
1267
1268                 if ($item['event-type'] == 'event') {
1269                         $type = 'Event';
1270                 } elseif (!empty($item['title'])) {
1271                         $type = 'Article';
1272                 } else {
1273                         $type = 'Note';
1274                 }
1275
1276                 if ($item['deleted']) {
1277                         $type = 'Tombstone';
1278                 }
1279
1280                 $data = [];
1281                 $data['id'] = $item['uri'];
1282                 $data['type'] = $type;
1283
1284                 if ($item['deleted']) {
1285                         return $data;
1286                 }
1287
1288                 $data['summary'] = BBCode::toPlaintext(BBCode::getAbstract($item['body'], Protocol::ACTIVITYPUB));
1289
1290                 if ($item['uri'] != $item['thr-parent']) {
1291                         $data['inReplyTo'] = $item['thr-parent'];
1292                 } else {
1293                         $data['inReplyTo'] = null;
1294                 }
1295
1296                 $data['diaspora:guid'] = $item['guid'];
1297                 $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
1298
1299                 if ($item['created'] != $item['edited']) {
1300                         $data['updated'] = DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM);
1301                 }
1302
1303                 $data['url'] = $item['plink'];
1304                 $data['attributedTo'] = $item['author-link'];
1305                 $data['sensitive'] = self::isSensitive($item['uri-id']);
1306                 $data['context'] = self::fetchContextURLForItem($item);
1307
1308                 if (!empty($item['title'])) {
1309                         $data['name'] = BBCode::toPlaintext($item['title'], false);
1310                 }
1311
1312                 $permission_block = self::createPermissionBlockForItem($item, false);
1313
1314                 $body = $item['body'];
1315
1316                 if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) {
1317                         $body = self::prependMentions($body, $permission_block);
1318                 }
1319
1320                 if ($type == 'Note') {
1321                         $body = self::removePictures($body);
1322                 } elseif (($type == 'Article') && empty($data['summary'])) {
1323                         $data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($body), 1000));
1324                 }
1325
1326                 if ($type == 'Event') {
1327                         $data = array_merge($data, self::createEvent($item));
1328                 } else {
1329                         $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
1330                         $body = preg_replace_callback($regexp, ['self', 'mentionCallback'], $body);
1331
1332                         $data['content'] = BBCode::convert($body, false, 9);
1333                 }
1334
1335                 // The regular "content" field does contain a minimized HTML. This is done since systems like
1336                 // Mastodon has got problems with - for example - embedded pictures.
1337                 // The contentMap does contain the unmodified HTML.
1338                 $language = self::getLanguage($item);
1339                 if (!empty($language)) {
1340                         $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
1341                         $richbody = preg_replace_callback($regexp, ['self', 'mentionCallback'], $item['body']);
1342                         $richbody = BBCode::removeAttachment($richbody);
1343
1344                         $data['contentMap'][$language] = BBCode::convert($richbody, false);
1345                 }
1346
1347                 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
1348
1349                 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
1350                         $data['diaspora:comment'] = $item['signed_text'];
1351                 }
1352
1353                 $data['attachment'] = self::createAttachmentList($item, $type);
1354                 $data['tag'] = self::createTagList($item);
1355
1356                 if (empty($data['location']) && (!empty($item['coord']) || !empty($item['location']))) {
1357                         $data['location'] = self::createLocation($item);
1358                 }
1359
1360                 if (!empty($item['app'])) {
1361                         $data['generator'] = ['type' => 'Application', 'name' => $item['app']];
1362                 }
1363
1364                 $data = array_merge($data, $permission_block);
1365
1366                 return $data;
1367         }
1368
1369         /**
1370          * Fetches the language from the post, the user or the system.
1371          *
1372          * @param array $item
1373          *
1374          * @return string language string
1375          */
1376         private static function getLanguage(array $item)
1377         {
1378                 // Try to fetch the language from the post itself
1379                 if (!empty($item['language'])) {
1380                         $languages = array_keys(json_decode($item['language'], true));
1381                         if (!empty($languages[0])) {
1382                                 return $languages[0];
1383                         }
1384                 }
1385
1386                 // Otherwise use the user's language
1387                 if (!empty($item['uid'])) {
1388                         $user = DBA::selectFirst('user', ['language'], ['uid' => $item['uid']]);
1389                         if (!empty($user['language'])) {
1390                                 return $user['language'];
1391                         }
1392                 }
1393
1394                 // And finally just use the system language
1395                 return DI::config()->get('system', 'language');
1396         }
1397
1398         /**
1399          * Creates an an "add tag" entry
1400          *
1401          * @param array $item
1402          * @param array $data activity data
1403          *
1404          * @return array with activity data for adding tags
1405          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1406          * @throws \ImagickException
1407          */
1408         private static function createAddTag($item, $data)
1409         {
1410                 $object = XML::parseString($item['object']);
1411                 $target = XML::parseString($item["target"]);
1412
1413                 $data['diaspora:guid'] = $item['guid'];
1414                 $data['actor'] = $item['author-link'];
1415                 $data['target'] = (string)$target->id;
1416                 $data['summary'] = BBCode::toPlaintext($item['body']);
1417                 $data['object'] = ['id' => (string)$object->id, 'type' => 'tag', 'name' => (string)$object->title, 'content' => (string)$object->content];
1418
1419                 return $data;
1420         }
1421
1422         /**
1423          * Creates an announce object entry
1424          *
1425          * @param array $item
1426          * @param array $data activity data
1427          *
1428          * @return array with activity data
1429          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1430          * @throws \ImagickException
1431          */
1432         private static function createAnnounce($item, $data)
1433         {
1434                 $orig_body = $item['body'];
1435                 $announce = self::getAnnounceArray($item);
1436                 if (empty($announce)) {
1437                         $data['type'] = 'Create';
1438                         $data['object'] = self::createNote($item);
1439                         return $data;
1440                 }
1441
1442                 if (empty($announce['comment'])) {
1443                         // Pure announce, without a quote
1444                         $data['type'] = 'Announce';
1445                         $data['object'] = $announce['object']['uri'];
1446                         return $data;
1447                 }
1448
1449                 // Quote
1450                 $data['type'] = 'Create';
1451                 $item['body'] = $announce['comment'] . "\n" . $announce['object']['plink'];
1452                 $data['object'] = self::createNote($item);
1453
1454                 /// @todo Finally descide how to implement this in AP. This is a possible way:
1455                 $data['object']['attachment'][] = self::createNote($announce['object']);
1456
1457                 $data['object']['source']['content'] = $orig_body;
1458                 return $data;
1459         }
1460
1461         /**
1462          * Return announce related data if the item is an annunce
1463          *
1464          * @param array $item
1465          *
1466          * @return array
1467          */
1468         public static function getAnnounceArray($item)
1469         {
1470                 $reshared = Item::getShareArray($item);
1471                 if (empty($reshared['guid'])) {
1472                         return [];
1473                 }
1474
1475                 $reshared_item = Item::selectFirst([], ['guid' => $reshared['guid']]);
1476                 if (!DBA::isResult($reshared_item)) {
1477                         return [];
1478                 }
1479
1480                 if (!in_array($reshared_item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
1481                         return [];
1482                 }
1483
1484                 $profile = APContact::getByURL($reshared_item['author-link'], false);
1485                 if (empty($profile)) {
1486                         return [];
1487                 }
1488
1489                 return ['object' => $reshared_item, 'actor' => $profile, 'comment' => $reshared['comment']];
1490         }
1491
1492         /**
1493          * Checks if the provided item array is an announce
1494          *
1495          * @param array $item
1496          *
1497          * @return boolean
1498          */
1499         public static function isAnnounce($item)
1500         {
1501                 $announce = self::getAnnounceArray($item);
1502                 if (empty($announce)) {
1503                         return false;
1504                 }
1505
1506                 return empty($announce['comment']);
1507         }
1508
1509         /**
1510          * Creates an activity id for a given contact id
1511          *
1512          * @param integer $cid Contact ID of target
1513          *
1514          * @return bool|string activity id
1515          */
1516         public static function activityIDFromContact($cid)
1517         {
1518                 $contact = DBA::selectFirst('contact', ['uid', 'id', 'created'], ['id' => $cid]);
1519                 if (!DBA::isResult($contact)) {
1520                         return false;
1521                 }
1522
1523                 $hash = hash('ripemd128', $contact['uid'].'-'.$contact['id'].'-'.$contact['created']);
1524                 $uuid = substr($hash, 0, 8). '-' . substr($hash, 8, 4) . '-' . substr($hash, 12, 4) . '-' . substr($hash, 16, 4) . '-' . substr($hash, 20, 12);
1525                 return DI::baseUrl() . '/activity/' . $uuid;
1526         }
1527
1528         /**
1529          * Transmits a contact suggestion to a given inbox
1530          *
1531          * @param integer $uid           User ID
1532          * @param string  $inbox         Target inbox
1533          * @param integer $suggestion_id Suggestion ID
1534          *
1535          * @return boolean was the transmission successful?
1536          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1537          */
1538         public static function sendContactSuggestion($uid, $inbox, $suggestion_id)
1539         {
1540                 $owner = User::getOwnerDataById($uid);
1541
1542                 $suggestion = DI::fsuggest()->getById($suggestion_id);
1543
1544                 $data = ['@context' => ActivityPub::CONTEXT,
1545                         'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1546                         'type' => 'Announce',
1547                         'actor' => $owner['url'],
1548                         'object' => $suggestion->url,
1549                         'content' => $suggestion->note,
1550                         'instrument' => self::getService(),
1551                         'to' => [ActivityPub::PUBLIC_COLLECTION],
1552                         'cc' => []];
1553
1554                 $signed = LDSignature::sign($data, $owner);
1555
1556                 Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1557                 return HTTPSignature::transmit($signed, $inbox, $uid);
1558         }
1559
1560         /**
1561          * Transmits a profile relocation to a given inbox
1562          *
1563          * @param integer $uid   User ID
1564          * @param string  $inbox Target inbox
1565          *
1566          * @return boolean was the transmission successful?
1567          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1568          */
1569         public static function sendProfileRelocation($uid, $inbox)
1570         {
1571                 $owner = User::getOwnerDataById($uid);
1572
1573                 $data = ['@context' => ActivityPub::CONTEXT,
1574                         'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1575                         'type' => 'dfrn:relocate',
1576                         'actor' => $owner['url'],
1577                         'object' => $owner['url'],
1578                         'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1579                         'instrument' => self::getService(),
1580                         'to' => [ActivityPub::PUBLIC_COLLECTION],
1581                         'cc' => []];
1582
1583                 $signed = LDSignature::sign($data, $owner);
1584
1585                 Logger::log('Deliver profile relocation for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1586                 return HTTPSignature::transmit($signed, $inbox, $uid);
1587         }
1588
1589         /**
1590          * Transmits a profile deletion to a given inbox
1591          *
1592          * @param integer $uid   User ID
1593          * @param string  $inbox Target inbox
1594          *
1595          * @return boolean was the transmission successful?
1596          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1597          */
1598         public static function sendProfileDeletion($uid, $inbox)
1599         {
1600                 $owner = User::getOwnerDataById($uid);
1601
1602                 if (empty($owner)) {
1603                         Logger::error('No owner data found, the deletion message cannot be processed.', ['user' => $uid]);
1604                         return false;
1605                 }
1606
1607                 if (empty($owner['uprvkey'])) {
1608                         Logger::error('No private key for owner found, the deletion message cannot be processed.', ['user' => $uid]);
1609                         return false;
1610                 }
1611
1612                 $data = ['@context' => ActivityPub::CONTEXT,
1613                         'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1614                         'type' => 'Delete',
1615                         'actor' => $owner['url'],
1616                         'object' => $owner['url'],
1617                         'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1618                         'instrument' => self::getService(),
1619                         'to' => [ActivityPub::PUBLIC_COLLECTION],
1620                         'cc' => []];
1621
1622                 $signed = LDSignature::sign($data, $owner);
1623
1624                 Logger::log('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1625                 return HTTPSignature::transmit($signed, $inbox, $uid);
1626         }
1627
1628         /**
1629          * Transmits a profile change to a given inbox
1630          *
1631          * @param integer $uid   User ID
1632          * @param string  $inbox Target inbox
1633          *
1634          * @return boolean was the transmission successful?
1635          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1636          * @throws \ImagickException
1637          */
1638         public static function sendProfileUpdate($uid, $inbox)
1639         {
1640                 $owner = User::getOwnerDataById($uid);
1641                 $profile = APContact::getByURL($owner['url']);
1642
1643                 $data = ['@context' => ActivityPub::CONTEXT,
1644                         'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1645                         'type' => 'Update',
1646                         'actor' => $owner['url'],
1647                         'object' => self::getProfile($uid),
1648                         'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
1649                         'instrument' => self::getService(),
1650                         'to' => [$profile['followers']],
1651                         'cc' => []];
1652
1653                 $signed = LDSignature::sign($data, $owner);
1654
1655                 Logger::log('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', Logger::DEBUG);
1656                 return HTTPSignature::transmit($signed, $inbox, $uid);
1657         }
1658
1659         /**
1660          * Transmits a given activity to a target
1661          *
1662          * @param string  $activity Type name
1663          * @param string  $target   Target profile
1664          * @param integer $uid      User ID
1665          * @return bool
1666          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1667          * @throws \ImagickException
1668          * @throws \Exception
1669          */
1670         public static function sendActivity($activity, $target, $uid, $id = '')
1671         {
1672                 $profile = APContact::getByURL($target);
1673                 if (empty($profile['inbox'])) {
1674                         Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1675                         return;
1676                 }
1677
1678                 $owner = User::getOwnerDataById($uid);
1679
1680                 if (empty($id)) {
1681                         $id = DI::baseUrl() . '/activity/' . System::createGUID();
1682                 }
1683
1684                 $data = ['@context' => ActivityPub::CONTEXT,
1685                         'id' => $id,
1686                         'type' => $activity,
1687                         'actor' => $owner['url'],
1688                         'object' => $profile['url'],
1689                         'instrument' => self::getService(),
1690                         'to' => [$profile['url']]];
1691
1692                 Logger::log('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
1693
1694                 $signed = LDSignature::sign($data, $owner);
1695                 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1696         }
1697
1698         /**
1699          * Transmits a "follow object" activity to a target
1700          * This is a preparation for sending automated "follow" requests when receiving "Announce" messages
1701          *
1702          * @param string  $object Object URL
1703          * @param string  $target Target profile
1704          * @param integer $uid    User ID
1705          * @return bool
1706          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1707          * @throws \ImagickException
1708          * @throws \Exception
1709          */
1710         public static function sendFollowObject($object, $target, $uid = 0)
1711         {
1712                 $profile = APContact::getByURL($target);
1713                 if (empty($profile['inbox'])) {
1714                         Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1715                         return;
1716                 }
1717
1718                 if (empty($uid)) {
1719                         // Fetch the list of administrators
1720                         $admin_mail = explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email')));
1721
1722                         // We need to use some user as a sender. It doesn't care who it will send. We will use an administrator account.
1723                         $condition = ['verified' => true, 'blocked' => false, 'account_removed' => false, 'account_expired' => false, 'email' => $admin_mail];
1724                         $first_user = DBA::selectFirst('user', ['uid'], $condition);
1725                         $uid = $first_user['uid'];
1726                 }
1727
1728                 $condition = ['verb' => Activity::FOLLOW, 'uid' => 0, 'parent-uri' => $object,
1729                         'author-id' => Contact::getPublicIdByUserId($uid)];
1730                 if (Item::exists($condition)) {
1731                         Logger::log('Follow for ' . $object . ' for user ' . $uid . ' does already exist.', Logger::DEBUG);
1732                         return false;
1733                 }
1734
1735                 $owner = User::getOwnerDataById($uid);
1736
1737                 $data = ['@context' => ActivityPub::CONTEXT,
1738                         'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1739                         'type' => 'Follow',
1740                         'actor' => $owner['url'],
1741                         'object' => $object,
1742                         'instrument' => self::getService(),
1743                         'to' => [$profile['url']]];
1744
1745                 Logger::log('Sending follow ' . $object . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
1746
1747                 $signed = LDSignature::sign($data, $owner);
1748                 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1749         }
1750
1751         /**
1752          * Transmit a message that the contact request had been accepted
1753          *
1754          * @param string  $target Target profile
1755          * @param         $id
1756          * @param integer $uid    User ID
1757          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1758          * @throws \ImagickException
1759          */
1760         public static function sendContactAccept($target, $id, $uid)
1761         {
1762                 $profile = APContact::getByURL($target);
1763                 if (empty($profile['inbox'])) {
1764                         Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1765                         return;
1766                 }
1767
1768                 $owner = User::getOwnerDataById($uid);
1769                 $data = ['@context' => ActivityPub::CONTEXT,
1770                         'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1771                         'type' => 'Accept',
1772                         'actor' => $owner['url'],
1773                         'object' => [
1774                                 'id' => (string)$id,
1775                                 'type' => 'Follow',
1776                                 'actor' => $profile['url'],
1777                                 'object' => $owner['url']
1778                         ],
1779                         'instrument' => self::getService(),
1780                         'to' => [$profile['url']]];
1781
1782                 Logger::debug('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id);
1783
1784                 $signed = LDSignature::sign($data, $owner);
1785                 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1786         }
1787
1788         /**
1789          * Reject a contact request or terminates the contact relation
1790          *
1791          * @param string  $target Target profile
1792          * @param         $id
1793          * @param integer $uid    User ID
1794          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1795          * @throws \ImagickException
1796          */
1797         public static function sendContactReject($target, $id, $uid)
1798         {
1799                 $profile = APContact::getByURL($target);
1800                 if (empty($profile['inbox'])) {
1801                         Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1802                         return;
1803                 }
1804
1805                 $owner = User::getOwnerDataById($uid);
1806                 $data = ['@context' => ActivityPub::CONTEXT,
1807                         'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
1808                         'type' => 'Reject',
1809                         'actor' => $owner['url'],
1810                         'object' => [
1811                                 'id' => (string)$id,
1812                                 'type' => 'Follow',
1813                                 'actor' => $profile['url'],
1814                                 'object' => $owner['url']
1815                         ],
1816                         'instrument' => self::getService(),
1817                         'to' => [$profile['url']]];
1818
1819                 Logger::debug('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id);
1820
1821                 $signed = LDSignature::sign($data, $owner);
1822                 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1823         }
1824
1825         /**
1826          * Transmits a message that we don't want to follow this contact anymore
1827          *
1828          * @param string  $target Target profile
1829          * @param integer $uid    User ID
1830          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1831          * @throws \ImagickException
1832          * @throws \Exception
1833          */
1834         public static function sendContactUndo($target, $cid, $uid)
1835         {
1836                 $profile = APContact::getByURL($target);
1837                 if (empty($profile['inbox'])) {
1838                         Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
1839                         return;
1840                 }
1841
1842                 $object_id = self::activityIDFromContact($cid);
1843                 if (empty($object_id)) {
1844                         return;
1845                 }
1846
1847                 $id = DI::baseUrl() . '/activity/' . System::createGUID();
1848
1849                 $owner = User::getOwnerDataById($uid);
1850                 $data = ['@context' => ActivityPub::CONTEXT,
1851                         'id' => $id,
1852                         'type' => 'Undo',
1853                         'actor' => $owner['url'],
1854                         'object' => ['id' => $object_id, 'type' => 'Follow',
1855                                 'actor' => $owner['url'],
1856                                 'object' => $profile['url']],
1857                         'instrument' => self::getService(),
1858                         'to' => [$profile['url']]];
1859
1860                 Logger::log('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, Logger::DEBUG);
1861
1862                 $signed = LDSignature::sign($data, $owner);
1863                 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
1864         }
1865
1866         private static function prependMentions($body, array $permission_block)
1867         {
1868                 if (DI::config()->get('system', 'disable_implicit_mentions')) {
1869                         return $body;
1870                 }
1871
1872                 $mentions = [];
1873
1874                 foreach ($permission_block['to'] as $profile_url) {
1875                         $profile = Contact::getDetailsByURL($profile_url);
1876                         if (!empty($profile['addr'])
1877                                 && $profile['contact-type'] != Contact::TYPE_COMMUNITY
1878                                 && !strstr($body, $profile['addr'])
1879                                 && !strstr($body, $profile_url)
1880                         ) {
1881                                 $mentions[] = '@[url=' . $profile_url . ']' . $profile['nick'] . '[/url]';
1882                         }
1883                 }
1884
1885                 $mentions[] = $body;
1886
1887                 return implode(' ', $mentions);
1888         }
1889 }