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