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