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