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