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