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