]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub.php
Changed constant name to keep compatible with PHP 5
[friendica.git] / src / Protocol / ActivityPub.php
1 <?php
2 /**
3  * @file src/Protocol/ActivityPub.php
4  */
5 namespace Friendica\Protocol;
6
7 use Friendica\Database\DBA;
8 use Friendica\Core\System;
9 use Friendica\BaseObject;
10 use Friendica\Util\Network;
11 use Friendica\Util\HTTPSignature;
12 use Friendica\Core\Protocol;
13 use Friendica\Model\Conversation;
14 use Friendica\Model\Contact;
15 use Friendica\Model\APContact;
16 use Friendica\Model\Item;
17 use Friendica\Model\Profile;
18 use Friendica\Model\Term;
19 use Friendica\Model\User;
20 use Friendica\Util\DateTimeFormat;
21 use Friendica\Util\Crypto;
22 use Friendica\Content\Text\BBCode;
23 use Friendica\Content\Text\HTML;
24 use Friendica\Util\JsonLD;
25 use Friendica\Util\LDSignature;
26 use Friendica\Core\Config;
27
28 /**
29  * @brief ActivityPub Protocol class
30  * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
31  * https://www.w3.org/TR/activitypub/
32  * https://www.w3.org/TR/activitystreams-core/
33  * https://www.w3.org/TR/activitystreams-vocabulary/
34  *
35  * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
36  * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
37  *
38  * Digest: https://tools.ietf.org/html/rfc5843
39  * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
40  *
41  * Mastodon implementation of supported activities:
42  * https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/activity.rb#L26
43  *
44  * To-do:
45  *
46  * Receiver:
47  * - Update Note
48  * - Delete Note
49  * - Delete Person
50  * - Undo Announce
51  * - Reject Follow
52  * - Undo Accept
53  * - Undo Follow
54  * - Add
55  * - Create Image
56  * - Create Video
57  * - Event
58  * - Remove
59  * - Block
60  * - Flag
61  *
62  * Transmitter:
63  * - Announce
64  * - Undo Announce
65  * - Update Person
66  * - Reject Follow
67  * - Event
68  *
69  * General:
70  * - Attachments
71  * - nsfw (sensitive)
72  * - Queueing unsucessful deliveries
73  * - Polling the outboxes for missing content?
74  * - Possibly using the LD-JSON parser
75  */
76 class ActivityPub
77 {
78         const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
79         const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
80                 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
81                 'diaspora' => 'https://diasporafoundation.org/ns/',
82                 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
83                 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag']];
84
85         /**
86          * @brief Checks if the web request is done for the AP protocol
87          *
88          * @return is it AP?
89          */
90         public static function isRequest()
91         {
92                 return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
93                         stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/ld+json');
94         }
95
96         /**
97          * @brief collects the lost of followers of the given owner
98          *
99          * @param array $owner Owner array
100          * @param integer $page Page number
101          *
102          * @return array of owners
103          */
104         public static function getFollowers($owner, $page = null)
105         {
106                 $condition = ['rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
107                         'self' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
108                 $count = DBA::count('contact', $condition);
109
110                 $data = ['@context' => self::CONTEXT];
111                 $data['id'] = System::baseUrl() . '/followers/' . $owner['nickname'];
112                 $data['type'] = 'OrderedCollection';
113                 $data['totalItems'] = $count;
114
115                 // When we hide our friends we will only show the pure number but don't allow more.
116                 $profile = Profile::getProfileForUser($owner['uid']);
117                 if (!empty($profile['hide-friends'])) {
118                         return $data;
119                 }
120
121                 if (empty($page)) {
122                         $data['first'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=1';
123                 } else {
124                         $list = [];
125
126                         $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
127                         while ($contact = DBA::fetch($contacts)) {
128                                 $list[] = $contact['url'];
129                         }
130
131                         if (!empty($list)) {
132                                 $data['next'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=' . ($page + 1);
133                         }
134
135                         $data['partOf'] = System::baseUrl() . '/followers/' . $owner['nickname'];
136
137                         $data['orderedItems'] = $list;
138                 }
139
140                 return $data;
141         }
142
143         /**
144          * @brief Create list of following contacts
145          *
146          * @param array $owner Owner array
147          * @param integer $page Page numbe
148          *
149          * @return array of following contacts
150          */
151         public static function getFollowing($owner, $page = null)
152         {
153                 $condition = ['rel' => [Contact::SHARING, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
154                         'self' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
155                 $count = DBA::count('contact', $condition);
156
157                 $data = ['@context' => self::CONTEXT];
158                 $data['id'] = System::baseUrl() . '/following/' . $owner['nickname'];
159                 $data['type'] = 'OrderedCollection';
160                 $data['totalItems'] = $count;
161
162                 // When we hide our friends we will only show the pure number but don't allow more.
163                 $profile = Profile::getProfileForUser($owner['uid']);
164                 if (!empty($profile['hide-friends'])) {
165                         return $data;
166                 }
167
168                 if (empty($page)) {
169                         $data['first'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=1';
170                 } else {
171                         $list = [];
172
173                         $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
174                         while ($contact = DBA::fetch($contacts)) {
175                                 $list[] = $contact['url'];
176                         }
177
178                         if (!empty($list)) {
179                                 $data['next'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=' . ($page + 1);
180                         }
181
182                         $data['partOf'] = System::baseUrl() . '/following/' . $owner['nickname'];
183
184                         $data['orderedItems'] = $list;
185                 }
186
187                 return $data;
188         }
189
190         /**
191          * @brief Public posts for the given owner
192          *
193          * @param array $owner Owner array
194          * @param integer $page Page numbe
195          *
196          * @return array of posts
197          */
198         public static function getOutbox($owner, $page = null)
199         {
200                 $public_contact = Contact::getIdForURL($owner['url'], 0, true);
201
202                 $condition = ['uid' => $owner['uid'], 'contact-id' => $owner['id'], 'author-id' => $public_contact,
203                         'wall' => true, 'private' => false, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
204                         'deleted' => false, 'visible' => true];
205                 $count = DBA::count('item', $condition);
206
207                 $data = ['@context' => self::CONTEXT];
208                 $data['id'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
209                 $data['type'] = 'OrderedCollection';
210                 $data['totalItems'] = $count;
211
212                 if (empty($page)) {
213                         $data['first'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=1';
214                 } else {
215                         $list = [];
216
217                         $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
218
219                         $items = Item::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
220                         while ($item = Item::fetch($items)) {
221                                 $object = self::createObjectFromItemID($item['id']);
222                                 unset($object['@context']);
223                                 $list[] = $object;
224                         }
225
226                         if (!empty($list)) {
227                                 $data['next'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=' . ($page + 1);
228                         }
229
230                         $data['partOf'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
231
232                         $data['orderedItems'] = $list;
233                 }
234
235                 return $data;
236         }
237
238         /**
239          * Return the ActivityPub profile of the given user
240          *
241          * @param integer $uid User ID
242          * @return profile array
243          */
244         public static function profile($uid)
245         {
246                 $accounttype = ['Person', 'Organization', 'Service', 'Group', 'Application'];
247                 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
248                         'account_removed' => false, 'verified' => true];
249                 $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
250                 $user = DBA::selectFirst('user', $fields, $condition);
251                 if (!DBA::isResult($user)) {
252                         return [];
253                 }
254
255                 $fields = ['locality', 'region', 'country-name'];
256                 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
257                 if (!DBA::isResult($profile)) {
258                         return [];
259                 }
260
261                 $fields = ['name', 'url', 'location', 'about', 'avatar'];
262                 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
263                 if (!DBA::isResult($contact)) {
264                         return [];
265                 }
266
267                 $data = ['@context' => self::CONTEXT];
268                 $data['id'] = $contact['url'];
269                 $data['diaspora:guid'] = $user['guid'];
270                 $data['type'] = $accounttype[$user['account-type']];
271                 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
272                 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
273                 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
274                 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
275                 $data['preferredUsername'] = $user['nickname'];
276                 $data['name'] = $contact['name'];
277                 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
278                         'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
279                 $data['summary'] = $contact['about'];
280                 $data['url'] = $contact['url'];
281                 $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [Contact::PAGE_NORMAL, Contact::PAGE_PRVGROUP]);
282                 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
283                         'owner' => $contact['url'],
284                         'publicKeyPem' => $user['pubkey']];
285                 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
286                 $data['icon'] = ['type' => 'Image',
287                         'url' => $contact['avatar']];
288
289                 // tags: https://kitty.town/@inmysocks/100656097926961126.json
290                 return $data;
291         }
292
293         /**
294          * @brief Returns an array with permissions of a given item array
295          *
296          * @param array $item
297          *
298          * @return array with permissions
299          */
300         private static function fetchPermissionBlockFromConversation($item)
301         {
302                 if (empty($item['thr-parent'])) {
303                         return [];
304                 }
305
306                 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
307                 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
308                 if (!DBA::isResult($conversation)) {
309                         return [];
310                 }
311
312                 $activity = json_decode($conversation['source'], true);
313
314                 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
315                 $profile = APContact::getProfileByURL($actor);
316
317                 $item_profile = APContact::getProfileByURL($item['author-link']);
318                 $exclude[] = $item['author-link'];
319
320                 if ($item['gravity'] == GRAVITY_PARENT) {
321                         $exclude[] = $item['owner-link'];
322                 }
323
324                 $permissions['to'][] = $actor;
325
326                 $elements = ['to', 'cc', 'bto', 'bcc'];
327                 foreach ($elements as $element) {
328                         if (empty($activity[$element])) {
329                                 continue;
330                         }
331                         if (is_string($activity[$element])) {
332                                 $activity[$element] = [$activity[$element]];
333                         }
334
335                         foreach ($activity[$element] as $receiver) {
336                                 if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
337                                         $receiver = $item_profile['followers'];
338                                 }
339                                 if (!in_array($receiver, $exclude)) {
340                                         $permissions[$element][] = $receiver;
341                                 }
342                         }
343                 }
344                 return $permissions;
345         }
346
347         /**
348          * @brief Creates an array of permissions from an item thread
349          *
350          * @param array $item
351          *
352          * @return permission array
353          */
354         public static function createPermissionBlockForItem($item)
355         {
356                 $data = ['to' => [], 'cc' => []];
357
358                 $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
359
360                 $actor_profile = APContact::getProfileByURL($item['author-link']);
361
362                 $terms = Term::tagArrayFromItemId($item['id']);
363
364                 $contacts[$item['author-link']] = $item['author-link'];
365
366                 if (!$item['private']) {
367                         $data['to'][] = self::PUBLIC_COLLECTION;
368                         if (!empty($actor_profile['followers'])) {
369                                 $data['cc'][] = $actor_profile['followers'];
370                         }
371
372                         foreach ($terms as $term) {
373                                 if ($term['type'] != TERM_MENTION) {
374                                         continue;
375                                 }
376                                 $profile = APContact::getProfileByURL($term['url'], false);
377                                 if (!empty($profile) && empty($contacts[$profile['url']])) {
378                                         $data['cc'][] = $profile['url'];
379                                         $contacts[$profile['url']] = $profile['url'];
380                                 }
381                         }
382                 } else {
383                         $receiver_list = Item::enumeratePermissions($item);
384
385                         $mentioned = [];
386
387                         foreach ($terms as $term) {
388                                 if ($term['type'] != TERM_MENTION) {
389                                         continue;
390                                 }
391                                 $cid = Contact::getIdForURL($term['url'], $item['uid']);
392                                 if (!empty($cid) && in_array($cid, $receiver_list)) {
393                                         $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid, 'network' => Protocol::ACTIVITYPUB]);
394                                         $data['to'][] = $contact['url'];
395                                         $contacts[$contact['url']] = $contact['url'];
396                                 }
397                         }
398
399                         foreach ($receiver_list as $receiver) {
400                                 $contact = DBA::selectFirst('contact', ['url'], ['id' => $receiver, 'network' => Protocol::ACTIVITYPUB]);
401                                 if (empty($contacts[$contact['url']])) {
402                                         $data['cc'][] = $contact['url'];
403                                         $contacts[$contact['url']] = $contact['url'];
404                                 }
405                         }
406                 }
407
408                 $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity'], ['parent' => $item['parent']]);
409                 while ($parent = Item::fetch($parents)) {
410                         // Don't include data from future posts
411                         if ($parent['id'] >= $item['id']) {
412                                 continue;
413                         }
414
415                         $profile = APContact::getProfileByURL($parent['author-link'], false);
416                         if (!empty($profile) && empty($contacts[$profile['url']])) {
417                                 $data['cc'][] = $profile['url'];
418                                 $contacts[$profile['url']] = $profile['url'];
419                         }
420
421                         if ($item['gravity'] != GRAVITY_PARENT) {
422                                 continue;
423                         }
424
425                         $profile = APContact::getProfileByURL($parent['owner-link'], false);
426                         if (!empty($profile) && empty($contacts[$profile['url']])) {
427                                 $data['cc'][] = $profile['url'];
428                                 $contacts[$profile['url']] = $profile['url'];
429                         }
430                 }
431                 DBA::close($parents);
432
433                 if (empty($data['to'])) {
434                         $data['to'] = $data['cc'];
435                         $data['cc'] = [];
436                 }
437
438                 return $data;
439         }
440
441         /**
442          * @brief Fetches an array of inboxes for the given item and user
443          *
444          * @param array $item
445          * @param integer $uid User ID
446          *
447          * @return array with inboxes
448          */
449         public static function fetchTargetInboxes($item, $uid)
450         {
451                 $permissions = self::createPermissionBlockForItem($item);
452                 if (empty($permissions)) {
453                         return [];
454                 }
455
456                 $inboxes = [];
457
458                 if ($item['gravity'] == GRAVITY_ACTIVITY) {
459                         $item_profile = APContact::getProfileByURL($item['author-link']);
460                 } else {
461                         $item_profile = APContact::getProfileByURL($item['owner-link']);
462                 }
463
464                 $elements = ['to', 'cc', 'bto', 'bcc'];
465                 foreach ($elements as $element) {
466                         if (empty($permissions[$element])) {
467                                 continue;
468                         }
469
470                         foreach ($permissions[$element] as $receiver) {
471                                 if ($receiver == $item_profile['followers']) {
472                                         $contacts = DBA::select('contact', ['notify', 'batch'], ['uid' => $uid,
473                                                 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::ACTIVITYPUB,
474                                                 'archive' => false, 'pending' => false]);
475                                         while ($contact = DBA::fetch($contacts)) {
476                                                 $contact = defaults($contact, 'batch', $contact['notify']);
477                                                 $inboxes[$contact] = $contact;
478                                         }
479                                         DBA::close($contacts);
480                                 } else {
481                                         $profile = APContact::getProfileByURL($receiver);
482                                         if (!empty($profile)) {
483                                                 $target = defaults($profile, 'sharedinbox', $profile['inbox']);
484                                                 $inboxes[$target] = $target;
485                                         }
486                                 }
487                         }
488                 }
489
490                 return $inboxes;
491         }
492
493         /**
494          * @brief Returns the activity type of a given item
495          *
496          * @param array $item
497          *
498          * @return activity type
499          */
500         public static function getTypeOfItem($item)
501         {
502                 if ($item['verb'] == ACTIVITY_POST) {
503                         if ($item['created'] == $item['edited']) {
504                                 $type = 'Create';
505                         } else {
506                                 $type = 'Update';
507                         }
508                 } elseif ($item['verb'] == ACTIVITY_LIKE) {
509                         $type = 'Like';
510                 } elseif ($item['verb'] == ACTIVITY_DISLIKE) {
511                         $type = 'Dislike';
512                 } elseif ($item['verb'] == ACTIVITY_ATTEND) {
513                         $type = 'Accept';
514                 } elseif ($item['verb'] == ACTIVITY_ATTENDNO) {
515                         $type = 'Reject';
516                 } elseif ($item['verb'] == ACTIVITY_ATTENDMAYBE) {
517                         $type = 'TentativeAccept';
518                 } else {
519                         $type = '';
520                 }
521
522                 return $type;
523         }
524
525         /**
526          * @brief Creates an activity array for a given item id
527          *
528          * @param integer $item_id
529          * @param boolean $object_mode Is the activity item is used inside another object?
530          *
531          * @return array of activity
532          */
533         public static function createActivityFromItem($item_id, $object_mode = false)
534         {
535                 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
536
537                 if (!DBA::isResult($item)) {
538                         return false;
539                 }
540
541                 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
542                 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
543                 if (DBA::isResult($conversation)) {
544                         $data = json_decode($conversation['source']);
545                         if (!empty($data)) {
546                                 return $data;
547                         }
548                 }
549
550                 $type = self::getTypeOfItem($item);
551
552                 if (!$object_mode) {
553                         $data = ['@context' => self::CONTEXT];
554
555                         if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
556                                 $type = 'Undo';
557                         } elseif ($item['deleted']) {
558                                 $type = 'Delete';
559                         }
560                 } else {
561                         $data = [];
562                 }
563
564                 $data['id'] = $item['uri'] . '#' . $type;
565                 $data['type'] = $type;
566                 $data['actor'] = $item['author-link'];
567
568                 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
569
570                 if ($item["created"] != $item["edited"]) {
571                         $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
572                 }
573
574                 $data['context'] = self::fetchContextURLForItem($item);
575
576                 $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
577
578                 if (in_array($data['type'], ['Create', 'Update', 'Announce', 'Delete'])) {
579                         $data['object'] = self::createNote($item);
580                 } elseif ($data['type'] == 'Undo') {
581                         $data['object'] = self::createActivityFromItem($item_id, true);
582                 } else {
583                         $data['object'] = $item['thr-parent'];
584                 }
585
586                 $owner = User::getOwnerDataById($item['uid']);
587
588                 if (!$object_mode) {
589                         return LDSignature::sign($data, $owner);
590                 } else {
591                         return $data;
592                 }
593         }
594
595         /**
596          * @brief Creates an object array for a given item id
597          *
598          * @param integer $item_id
599          *
600          * @return object array
601          */
602         public static function createObjectFromItemID($item_id)
603         {
604                 $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]);
605
606                 if (!DBA::isResult($item)) {
607                         return false;
608                 }
609
610                 $data = ['@context' => self::CONTEXT];
611                 $data = array_merge($data, self::createNote($item));
612
613                 return $data;
614         }
615
616         /**
617          * @brief Returns a tag array for a given item array
618          *
619          * @param array $item
620          *
621          * @return array of tags
622          */
623         private static function createTagList($item)
624         {
625                 $tags = [];
626
627                 $terms = Term::tagArrayFromItemId($item['id']);
628                 foreach ($terms as $term) {
629                         if ($term['type'] == TERM_MENTION) {
630                                 $contact = Contact::getDetailsByURL($term['url']);
631                                 if (!empty($contact['addr'])) {
632                                         $mention = '@' . $contact['addr'];
633                                 } else {
634                                         $mention = '@' . $term['url'];
635                                 }
636
637                                 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
638                         }
639                 }
640                 return $tags;
641         }
642
643         /**
644          * @brief Fetches the "context" value for a givem item array from the "conversation" table
645          *
646          * @param array $item
647          *
648          * @return string with context url
649          */
650         private static function fetchContextURLForItem($item)
651         {
652                 $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
653                 if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
654                         $context_uri = $conversation['conversation-href'];
655                 } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
656                         $context_uri = $conversation['conversation-uri'];
657                 } else {
658                         $context_uri = str_replace('/object/', '/context/', $item['parent-uri']);
659                 }
660                 return $context_uri;
661         }
662
663         /**
664          * @brief Creates a note/article object array
665          *
666          * @param array $item
667          *
668          * @return object array
669          */
670         private static function createNote($item)
671         {
672                 if (!empty($item['title'])) {
673                         $type = 'Article';
674                 } else {
675                         $type = 'Note';
676                 }
677
678                 if ($item['deleted']) {
679                         $type = 'Tombstone';
680                 }
681
682                 $data = [];
683                 $data['id'] = $item['uri'];
684                 $data['type'] = $type;
685
686                 if ($item['deleted']) {
687                         return $data;
688                 }
689
690                 $data['summary'] = null; // Ignore by now
691
692                 if ($item['uri'] != $item['thr-parent']) {
693                         $data['inReplyTo'] = $item['thr-parent'];
694                 } else {
695                         $data['inReplyTo'] = null;
696                 }
697
698                 $data['diaspora:guid'] = $item['guid'];
699                 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
700
701                 if ($item["created"] != $item["edited"]) {
702                         $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
703                 }
704
705                 $data['url'] = $item['plink'];
706                 $data['attributedTo'] = $item['author-link'];
707                 $data['actor'] = $item['author-link'];
708                 $data['sensitive'] = false; // - Query NSFW
709                 $data['context'] = self::fetchContextURLForItem($item);
710
711                 if (!empty($item['title'])) {
712                         $data['name'] = BBCode::convert($item['title'], false, 7);
713                 }
714
715                 $data['content'] = BBCode::convert($item['body'], false, 7);
716                 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
717
718                 if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
719                         $data['diaspora:comment'] = $item['signed_text'];
720                 }
721
722                 $data['attachment'] = []; // @ToDo
723                 $data['tag'] = self::createTagList($item);
724                 $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
725
726                 return $data;
727         }
728
729         /**
730          * @brief Transmits a given activity to a target
731          *
732          * @param array $activity
733          * @param string $target Target profile
734          * @param integer $uid User ID
735          */
736         public static function transmitActivity($activity, $target, $uid)
737         {
738                 $profile = APContact::getProfileByURL($target);
739
740                 $owner = User::getOwnerDataById($uid);
741
742                 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
743                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
744                         'type' => $activity,
745                         'actor' => $owner['url'],
746                         'object' => $profile['url'],
747                         'to' => $profile['url']];
748
749                 logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
750
751                 $signed = LDSignature::sign($data, $owner);
752                 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
753         }
754
755         /**
756          * @brief Transmit a message that the contact request had been accepted
757          *
758          * @param string $target Target profile
759          * @param $id
760          * @param integer $uid User ID
761          */
762         public static function transmitContactAccept($target, $id, $uid)
763         {
764                 $profile = APContact::getProfileByURL($target);
765
766                 $owner = User::getOwnerDataById($uid);
767                 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
768                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
769                         'type' => 'Accept',
770                         'actor' => $owner['url'],
771                         'object' => ['id' => $id, 'type' => 'Follow',
772                                 'actor' => $profile['url'],
773                                 'object' => $owner['url']],
774                         'to' => $profile['url']];
775
776                 logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
777
778                 $signed = LDSignature::sign($data, $owner);
779                 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
780         }
781
782         /**
783          * @brief 
784          *
785          * @param string $target Target profile
786          * @param $id
787          * @param integer $uid User ID
788          */
789         public static function transmitContactReject($target, $id, $uid)
790         {
791                 $profile = APContact::getProfileByURL($target);
792
793                 $owner = User::getOwnerDataById($uid);
794                 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
795                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
796                         'type' => 'Reject',
797                         'actor' => $owner['url'],
798                         'object' => ['id' => $id, 'type' => 'Follow',
799                                 'actor' => $profile['url'],
800                                 'object' => $owner['url']],
801                         'to' => $profile['url']];
802
803                 logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
804
805                 $signed = LDSignature::sign($data, $owner);
806                 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
807         }
808
809         /**
810          * @brief 
811          *
812          * @param string $target Target profile
813          * @param integer $uid User ID
814          */
815         public static function transmitContactUndo($target, $uid)
816         {
817                 $profile = APContact::getProfileByURL($target);
818
819                 $id = System::baseUrl() . '/activity/' . System::createGUID();
820
821                 $owner = User::getOwnerDataById($uid);
822                 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
823                         'id' => $id,
824                         'type' => 'Undo',
825                         'actor' => $owner['url'],
826                         'object' => ['id' => $id, 'type' => 'Follow',
827                                 'actor' => $owner['url'],
828                                 'object' => $profile['url']],
829                         'to' => $profile['url']];
830
831                 logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
832
833                 $signed = LDSignature::sign($data, $owner);
834                 HTTPSignature::transmit($signed, $profile['inbox'], $uid);
835         }
836
837         /**
838          * Fetches ActivityPub content from the given url
839          *
840          * @param string $url content url
841          * @return array
842          */
843         public static function fetchContent($url)
844         {
845                 $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
846                 if (!$ret['success'] || empty($ret['body'])) {
847                         return;
848                 }
849                 return json_decode($ret['body'], true);
850         }
851
852         /**
853          * Fetches a profile from the given url into an array that is compatible to Probe::uri
854          *
855          * @param string $url profile url
856          * @return array
857          */
858         public static function probeProfile($url)
859         {
860                 $apcontact = APContact::getProfileByURL($url, true);
861                 if (empty($apcontact)) {
862                         return false;
863                 }
864
865                 $profile = ['network' => Protocol::ACTIVITYPUB];
866                 $profile['nick'] = $apcontact['nick'];
867                 $profile['name'] = $apcontact['name'];
868                 $profile['guid'] = $apcontact['uuid'];
869                 $profile['url'] = $apcontact['url'];
870                 $profile['addr'] = $apcontact['addr'];
871                 $profile['alias'] = $apcontact['alias'];
872                 $profile['photo'] = $apcontact['photo'];
873                 // $profile['community']
874                 // $profile['keywords']
875                 // $profile['location']
876                 $profile['about'] = $apcontact['about'];
877                 $profile['batch'] = $apcontact['sharedinbox'];
878                 $profile['notify'] = $apcontact['inbox'];
879                 $profile['poll'] = $apcontact['outbox'];
880                 $profile['pubkey'] = $apcontact['pubkey'];
881                 $profile['baseurl'] = $apcontact['baseurl'];
882
883                 // Remove all "null" fields
884                 foreach ($profile as $field => $content) {
885                         if (is_null($content)) {
886                                 unset($profile[$field]);
887                         }
888                 }
889
890                 return $profile;
891         }
892
893         /**
894          * @brief 
895          *
896          * @param $body
897          * @param $header
898          * @param integer $uid User ID
899          */
900         public static function processInbox($body, $header, $uid)
901         {
902                 $http_signer = HTTPSignature::getSigner($body, $header);
903                 if (empty($http_signer)) {
904                         logger('Invalid HTTP signature, message will be discarded.', LOGGER_DEBUG);
905                         return;
906                 } else {
907                         logger('HTTP signature is signed by ' . $http_signer, LOGGER_DEBUG);
908                 }
909
910                 $activity = json_decode($body, true);
911
912                 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
913                 logger('Message for user ' . $uid . ' is from actor ' . $actor, LOGGER_DEBUG);
914
915                 if (empty($activity)) {
916                         logger('Invalid body.', LOGGER_DEBUG);
917                         return;
918                 }
919
920                 if (LDSignature::isSigned($activity)) {
921                         $ld_signer = LDSignature::getSigner($activity);
922                         if (empty($ld_signer)) {
923                                 logger('Invalid JSON-LD signature from ' . $actor, LOGGER_DEBUG);
924                         }
925                         if (!empty($ld_signer && ($actor == $http_signer))) {
926                                 logger('The HTTP and the JSON-LD signature belong to ' . $ld_signer, LOGGER_DEBUG);
927                                 $trust_source = true;
928                         } elseif (!empty($ld_signer)) {
929                                 logger('JSON-LD signature is signed by ' . $ld_signer, LOGGER_DEBUG);
930                                 $trust_source = true;
931                         } elseif ($actor == $http_signer) {
932                                 logger('Bad JSON-LD signature, but HTTP signer fits the actor.', LOGGER_DEBUG);
933                                 $trust_source = true;
934                         } else {
935                                 logger('Invalid JSON-LD signature and the HTTP signer is different.', LOGGER_DEBUG);
936                                 $trust_source = false;
937                         }
938                 } elseif ($actor == $http_signer) {
939                         logger('Trusting post without JSON-LD signature, The actor fits the HTTP signer.', LOGGER_DEBUG);
940                         $trust_source = true;
941                 } else {
942                         logger('No JSON-LD signature, different actor.', LOGGER_DEBUG);
943                         $trust_source = false;
944                 }
945
946                 self::processActivity($activity, $body, $uid, $trust_source);
947         }
948
949         /**
950          * @brief 
951          *
952          * @param $url
953          * @param integer $uid User ID
954          */
955         public static function fetchOutbox($url, $uid)
956         {
957                 $data = self::fetchContent($url);
958                 if (empty($data)) {
959                         return;
960                 }
961
962                 if (!empty($data['orderedItems'])) {
963                         $items = $data['orderedItems'];
964                 } elseif (!empty($data['first']['orderedItems'])) {
965                         $items = $data['first']['orderedItems'];
966                 } elseif (!empty($data['first'])) {
967                         self::fetchOutbox($data['first'], $uid);
968                         return;
969                 } else {
970                         $items = [];
971                 }
972
973                 foreach ($items as $activity) {
974                         self::processActivity($activity, '', $uid, true);
975                 }
976         }
977
978         /**
979          * @brief 
980          *
981          * @param array $activity
982          * @param integer $uid User ID
983          * @param $trust_source
984          *
985          * @return 
986          */
987         private static function prepareObjectData($activity, $uid, &$trust_source)
988         {
989                 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
990                 if (empty($actor)) {
991                         logger('Empty actor', LOGGER_DEBUG);
992                         return [];
993                 }
994
995                 // Fetch all receivers from to, cc, bto and bcc
996                 $receivers = self::getReceivers($activity, $actor);
997
998                 // When it is a delivery to a personal inbox we add that user to the receivers
999                 if (!empty($uid)) {
1000                         $owner = User::getOwnerDataById($uid);
1001                         $additional = ['uid:' . $uid => $uid];
1002                         $receivers = array_merge($receivers, $additional);
1003                 }
1004
1005                 logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG);
1006
1007                 $object_id = JsonLD::fetchElement($activity, 'object', 'id');
1008                 if (empty($object_id)) {
1009                         logger('No object found', LOGGER_DEBUG);
1010                         return [];
1011                 }
1012
1013                 // Fetch the content only on activities where this matters
1014                 if (in_array($activity['type'], ['Create', 'Announce'])) {
1015                         $object_data = self::fetchObject($object_id, $activity['object'], $trust_source);
1016                         if (empty($object_data)) {
1017                                 logger("Object data couldn't be processed", LOGGER_DEBUG);
1018                                 return [];
1019                         }
1020                         // We had been able to retrieve the object data - so we can trust the source
1021                         $trust_source = true;
1022                 } elseif (in_array($activity['type'], ['Like', 'Dislike'])) {
1023                         // Create a mostly empty array out of the activity data (instead of the object).
1024                         // This way we later don't have to check for the existence of ech individual array element.
1025                         $object_data = self::processObject($activity);
1026                         $object_data['name'] = $activity['type'];
1027                         $object_data['author'] = $activity['actor'];
1028                         $object_data['object'] = $object_id;
1029                         $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
1030                 } else {
1031                         $object_data = [];
1032                         $object_data['id'] = $activity['id'];
1033                         $object_data['object'] = $activity['object'];
1034                         $object_data['object_type'] = JsonLD::fetchElement($activity, 'object', 'type');
1035                 }
1036
1037                 $object_data = self::addActivityFields($object_data, $activity);
1038
1039                 $object_data['type'] = $activity['type'];
1040                 $object_data['owner'] = $actor;
1041                 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
1042
1043                 logger('Processing ' . $object_data['type'] . ' ' . $object_data['object_type'] . ' ' . $object_data['id'], LOGGER_DEBUG);
1044
1045                 return $object_data;
1046         }
1047
1048         /**
1049          * @brief 
1050          *
1051          * @param array $activity
1052          * @param $body
1053          * @param integer $uid User ID
1054          * @param $trust_source
1055          */
1056         private static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
1057         {
1058                 if (empty($activity['type'])) {
1059                         logger('Empty type', LOGGER_DEBUG);
1060                         return;
1061                 }
1062
1063                 if (empty($activity['object'])) {
1064                         logger('Empty object', LOGGER_DEBUG);
1065                         return;
1066                 }
1067
1068                 if (empty($activity['actor'])) {
1069                         logger('Empty actor', LOGGER_DEBUG);
1070                         return;
1071
1072                 }
1073
1074                 // $trust_source is called by reference and is set to true if the content was retrieved successfully
1075                 $object_data = self::prepareObjectData($activity, $uid, $trust_source);
1076                 if (empty($object_data)) {
1077                         logger('No object data found', LOGGER_DEBUG);
1078                         return;
1079                 }
1080
1081                 if (!$trust_source) {
1082                         logger('No trust for activity type "' . $activity['type'] . '", so we quit now.', LOGGER_DEBUG);
1083                 }
1084
1085                 switch ($activity['type']) {
1086                         case 'Create':
1087                         case 'Announce':
1088                                 self::createItem($object_data, $body);
1089                                 break;
1090
1091                         case 'Like':
1092                                 self::likeItem($object_data, $body);
1093                                 break;
1094
1095                         case 'Dislike':
1096                                 self::dislikeItem($object_data, $body);
1097                                 break;
1098
1099                         case 'Update':
1100                                 if (in_array($object_data['object_type'], ['Person', 'Organization', 'Service', 'Group', 'Application'])) {
1101                                         self::updatePerson($object_data, $body);
1102                                 }
1103                                 break;
1104
1105                         case 'Delete':
1106                                 break;
1107
1108                         case 'Follow':
1109                                 self::followUser($object_data);
1110                                 break;
1111
1112                         case 'Accept':
1113                                 if ($object_data['object_type'] == 'Follow') {
1114                                         self::acceptFollowUser($object_data);
1115                                 }
1116                                 break;
1117
1118                         case 'Undo':
1119                                 if ($object_data['object_type'] == 'Follow') {
1120                                         self::undoFollowUser($object_data);
1121                                 } elseif (in_array($object_data['object_type'], ['Like', 'Dislike', 'Accept', 'Reject', 'TentativeAccept'])) {
1122                                         self::undoActivity($object_data);
1123                                 }
1124                                 break;
1125
1126                         default:
1127                                 logger('Unknown activity: ' . $activity['type'], LOGGER_DEBUG);
1128                                 break;
1129                 }
1130         }
1131
1132         /**
1133          * @brief 
1134          *
1135          * @param array $activity
1136          * @param $actor
1137          *
1138          * @return 
1139          */
1140         private static function getReceivers($activity, $actor)
1141         {
1142                 $receivers = [];
1143
1144                 // When it is an answer, we inherite the receivers from the parent
1145                 $replyto = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
1146                 if (!empty($replyto)) {
1147                         $parents = Item::select(['uid'], ['uri' => $replyto]);
1148                         while ($parent = Item::fetch($parents)) {
1149                                 $receivers['uid:' . $parent['uid']] = $parent['uid'];
1150                         }
1151                 }
1152
1153                 if (!empty($actor)) {
1154                         $profile = APContact::getProfileByURL($actor);
1155                         $followers = defaults($profile, 'followers', '');
1156
1157                         logger('Actor: ' . $actor . ' - Followers: ' . $followers, LOGGER_DEBUG);
1158                 } else {
1159                         logger('Empty actor', LOGGER_DEBUG);
1160                         $followers = '';
1161                 }
1162
1163                 $elements = ['to', 'cc', 'bto', 'bcc'];
1164                 foreach ($elements as $element) {
1165                         if (empty($activity[$element])) {
1166                                 continue;
1167                         }
1168
1169                         // The receiver can be an arror or a string
1170                         if (is_string($activity[$element])) {
1171                                 $activity[$element] = [$activity[$element]];
1172                         }
1173
1174                         foreach ($activity[$element] as $receiver) {
1175                                 if ($receiver == self::PUBLIC_COLLECTION) {
1176                                         $receivers['uid:0'] = 0;
1177                                 }
1178
1179                                 if (($receiver == self::PUBLIC_COLLECTION) && !empty($actor)) {
1180                                         // This will most likely catch all OStatus connections to Mastodon
1181                                         $condition = ['alias' => [$actor, normalise_link($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]
1182                                                 , 'archive' => false, 'pending' => false];
1183                                         $contacts = DBA::select('contact', ['uid'], $condition);
1184                                         while ($contact = DBA::fetch($contacts)) {
1185                                                 if ($contact['uid'] != 0) {
1186                                                         $receivers['uid:' . $contact['uid']] = $contact['uid'];
1187                                                 }
1188                                         }
1189                                         DBA::close($contacts);
1190                                 }
1191
1192                                 if (in_array($receiver, [$followers, self::PUBLIC_COLLECTION]) && !empty($actor)) {
1193                                         $condition = ['nurl' => normalise_link($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
1194                                                 'network' => Protocol::ACTIVITYPUB, 'archive' => false, 'pending' => false];
1195                                         $contacts = DBA::select('contact', ['uid'], $condition);
1196                                         while ($contact = DBA::fetch($contacts)) {
1197                                                 if ($contact['uid'] != 0) {
1198                                                         $receivers['uid:' . $contact['uid']] = $contact['uid'];
1199                                                 }
1200                                         }
1201                                         DBA::close($contacts);
1202                                         continue;
1203                                 }
1204
1205                                 $condition = ['self' => true, 'nurl' => normalise_link($receiver)];
1206                                 $contact = DBA::selectFirst('contact', ['uid'], $condition);
1207                                 if (!DBA::isResult($contact)) {
1208                                         continue;
1209                                 }
1210                                 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1211                         }
1212                 }
1213
1214                 self::switchContacts($receivers, $actor);
1215
1216                 return $receivers;
1217         }
1218
1219         /**
1220          * @brief 
1221          *
1222          * @param $cid
1223          * @param integer $uid User ID
1224          * @param $url
1225          */
1226         private static function switchContact($cid, $uid, $url)
1227         {
1228                 $profile = ActivityPub::probeProfile($url);
1229                 if (empty($profile)) {
1230                         return;
1231                 }
1232
1233                 logger('Switch contact ' . $cid . ' (' . $profile['url'] . ') for user ' . $uid . ' from OStatus to ActivityPub');
1234
1235                 $photo = $profile['photo'];
1236                 unset($profile['photo']);
1237                 unset($profile['baseurl']);
1238
1239                 $profile['nurl'] = normalise_link($profile['url']);
1240                 DBA::update('contact', $profile, ['id' => $cid]);
1241
1242                 Contact::updateAvatar($photo, $uid, $cid);
1243         }
1244
1245         /**
1246          * @brief 
1247          *
1248          * @param $receivers
1249          * @param $actor
1250          */
1251         private static function switchContacts($receivers, $actor)
1252         {
1253                 if (empty($actor)) {
1254                         return;
1255                 }
1256
1257                 foreach ($receivers as $receiver) {
1258                         $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'nurl' => normalise_link($actor)]);
1259                         if (DBA::isResult($contact)) {
1260                                 self::switchContact($contact['id'], $receiver, $actor);
1261                         }
1262
1263                         $contact = DBA::selectFirst('contact', ['id'], ['uid' => $receiver, 'network' => Protocol::OSTATUS, 'alias' => [normalise_link($actor), $actor]]);
1264                         if (DBA::isResult($contact)) {
1265                                 self::switchContact($contact['id'], $receiver, $actor);
1266                         }
1267                 }
1268         }
1269
1270         /**
1271          * @brief 
1272          *
1273          * @param $object_data
1274          * @param array $activity
1275          *
1276          * @return 
1277          */
1278         private static function addActivityFields($object_data, $activity)
1279         {
1280                 if (!empty($activity['published']) && empty($object_data['published'])) {
1281                         $object_data['published'] = $activity['published'];
1282                 }
1283
1284                 if (!empty($activity['updated']) && empty($object_data['updated'])) {
1285                         $object_data['updated'] = $activity['updated'];
1286                 }
1287
1288                 if (!empty($activity['inReplyTo']) && empty($object_data['parent-uri'])) {
1289                         $object_data['parent-uri'] = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
1290                 }
1291
1292                 if (!empty($activity['instrument'])) {
1293                         $object_data['service'] = JsonLD::fetchElement($activity, 'instrument', 'name', 'type', 'Service');
1294                 }
1295                 return $object_data;
1296         }
1297
1298         /**
1299          * @brief 
1300          *
1301          * @param $object_id
1302          * @param $object
1303          * @param $trust_source
1304          *
1305          * @return 
1306          */
1307         private static function fetchObject($object_id, $object = [], $trust_source = false)
1308         {
1309                 if (!$trust_source || is_string($object)) {
1310                         $data = self::fetchContent($object_id);
1311                         if (empty($data)) {
1312                                 logger('Empty content for ' . $object_id . ', check if content is available locally.', LOGGER_DEBUG);
1313                                 $data = $object_id;
1314                         } else {
1315                                 logger('Fetched content for ' . $object_id, LOGGER_DEBUG);
1316                         }
1317                 } else {
1318                         logger('Using original object for url ' . $object_id, LOGGER_DEBUG);
1319                         $data = $object;
1320                 }
1321
1322                 if (is_string($data)) {
1323                         $item = Item::selectFirst([], ['uri' => $data]);
1324                         if (!DBA::isResult($item)) {
1325                                 logger('Object with url ' . $data . ' was not found locally.', LOGGER_DEBUG);
1326                                 return false;
1327                         }
1328                         logger('Using already stored item for url ' . $object_id, LOGGER_DEBUG);
1329                         $data = self::createNote($item);
1330                 }
1331
1332                 if (empty($data['type'])) {
1333                         logger('Empty type', LOGGER_DEBUG);
1334                         return false;
1335                 }
1336
1337                 switch ($data['type']) {
1338                         case 'Note':
1339                         case 'Article':
1340                         case 'Video':
1341                                 return self::processObject($data);
1342
1343                         case 'Announce':
1344                                 if (empty($data['object'])) {
1345                                         return false;
1346                                 }
1347                                 return self::fetchObject($data['object']);
1348
1349                         case 'Person':
1350                         case 'Tombstone':
1351                                 break;
1352
1353                         default:
1354                                 logger('Unknown object type: ' . $data['type'], LOGGER_DEBUG);
1355                                 break;
1356                 }
1357         }
1358
1359         /**
1360          * @brief 
1361          *
1362          * @param $object
1363          *
1364          * @return 
1365          */
1366         private static function processObject(&$object)
1367         {
1368                 if (empty($object['id'])) {
1369                         return false;
1370                 }
1371
1372                 $object_data = [];
1373                 $object_data['object_type'] = $object['type'];
1374                 $object_data['id'] = $object['id'];
1375
1376                 if (!empty($object['inReplyTo'])) {
1377                         $object_data['reply-to-id'] = JsonLD::fetchElement($object, 'inReplyTo', 'id');
1378                 } else {
1379                         $object_data['reply-to-id'] = $object_data['id'];
1380                 }
1381
1382                 $object_data['published'] = defaults($object, 'published', null);
1383                 $object_data['updated'] = defaults($object, 'updated', $object_data['published']);
1384
1385                 if (empty($object_data['published']) && !empty($object_data['updated'])) {
1386                         $object_data['published'] = $object_data['updated'];
1387                 }
1388
1389                 $actor = JsonLD::fetchElement($object, 'attributedTo', 'id');
1390                 if (empty($actor)) {
1391                         $actor = defaults($object, 'actor', null);
1392                 }
1393
1394                 $object_data['diaspora:guid'] = defaults($object, 'diaspora:guid', null);
1395                 $object_data['owner'] = $object_data['author'] = $actor;
1396                 $object_data['context'] = defaults($object, 'context', null);
1397                 $object_data['conversation'] = defaults($object, 'conversation', null);
1398                 $object_data['sensitive'] = defaults($object, 'sensitive', null);
1399                 $object_data['name'] = defaults($object, 'title', null);
1400                 $object_data['name'] = defaults($object, 'name', $object_data['name']);
1401                 $object_data['summary'] = defaults($object, 'summary', null);
1402                 $object_data['content'] = defaults($object, 'content', null);
1403                 $object_data['source'] = defaults($object, 'source', null);
1404                 $object_data['location'] = JsonLD::fetchElement($object, 'location', 'name', 'type', 'Place');
1405                 $object_data['attachments'] = defaults($object, 'attachment', null);
1406                 $object_data['tags'] = defaults($object, 'tag', null);
1407                 $object_data['service'] = JsonLD::fetchElement($object, 'instrument', 'name', 'type', 'Service');
1408                 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'url', 'href');
1409                 $object_data['receiver'] = self::getReceivers($object, $object_data['owner']);
1410
1411                 // Common object data:
1412
1413                 // Unhandled
1414                 // @context, type, actor, signature, mediaType, duration, replies, icon
1415
1416                 // Also missing: (Defined in the standard, but currently unused)
1417                 // audience, preview, endTime, startTime, generator, image
1418
1419                 // Data in Notes:
1420
1421                 // Unhandled
1422                 // contentMap, announcement_count, announcements, context_id, likes, like_count
1423                 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
1424
1425                 // Data in video:
1426
1427                 // To-Do?
1428                 // category, licence, language, commentsEnabled
1429
1430                 // Unhandled
1431                 // views, waitTranscoding, state, support, subtitleLanguage
1432                 // likes, dislikes, shares, comments
1433
1434                 return $object_data;
1435         }
1436
1437         /**
1438          * @brief Converts mentions from Pleroma into the Friendica format
1439          *
1440          * @param string $body
1441          *
1442          * @return converted body
1443          */
1444         private static function convertMentions($body)
1445         {
1446                 $URLSearchString = "^\[\]";
1447                 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
1448
1449                 return $body;
1450         }
1451
1452         /**
1453          * @brief Constructs a string with tags for a given tag array
1454          *
1455          * @param array $tags
1456          * @param boolean $sensitive
1457          *
1458          * @return string with tags
1459          */
1460         private static function constructTagList($tags, $sensitive)
1461         {
1462                 if (empty($tags)) {
1463                         return '';
1464                 }
1465
1466                 $tag_text = '';
1467                 foreach ($tags as $tag) {
1468                         if (in_array($tag['type'], ['Mention', 'Hashtag'])) {
1469                                 if (!empty($tag_text)) {
1470                                         $tag_text .= ',';
1471                                 }
1472
1473                                 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
1474                         }
1475                 }
1476
1477                 /// @todo add nsfw for $sensitive
1478
1479                 return $tag_text;
1480         }
1481
1482         /**
1483          * @brief 
1484          *
1485          * @param $attachments
1486          * @param array $item
1487          *
1488          * @return item array
1489          */
1490         private static function constructAttachList($attachments, $item)
1491         {
1492                 if (empty($attachments)) {
1493                         return $item;
1494                 }
1495
1496                 foreach ($attachments as $attach) {
1497                         $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
1498                         if ($filetype == 'image') {
1499                                 $item['body'] .= "\n[img]".$attach['url'].'[/img]';
1500                         } else {
1501                                 if (!empty($item["attach"])) {
1502                                         $item["attach"] .= ',';
1503                                 } else {
1504                                         $item["attach"] = '';
1505                                 }
1506                                 if (!isset($attach['length'])) {
1507                                         $attach['length'] = "0";
1508                                 }
1509                                 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
1510                         }
1511                 }
1512
1513                 return $item;
1514         }
1515
1516         /**
1517          * @brief 
1518          *
1519          * @param array $activity
1520          * @param $body
1521          */
1522         private static function createItem($activity, $body)
1523         {
1524                 $item = [];
1525                 $item['verb'] = ACTIVITY_POST;
1526                 $item['parent-uri'] = $activity['reply-to-id'];
1527
1528                 if ($activity['reply-to-id'] == $activity['id']) {
1529                         $item['gravity'] = GRAVITY_PARENT;
1530                         $item['object-type'] = ACTIVITY_OBJ_NOTE;
1531                 } else {
1532                         $item['gravity'] = GRAVITY_COMMENT;
1533                         $item['object-type'] = ACTIVITY_OBJ_COMMENT;
1534                 }
1535
1536                 if (($activity['id'] != $activity['reply-to-id']) && !Item::exists(['uri' => $activity['reply-to-id']])) {
1537                         logger('Parent ' . $activity['reply-to-id'] . ' not found. Try to refetch it.');
1538                         self::fetchMissingActivity($activity['reply-to-id'], $activity);
1539                 }
1540
1541                 self::postItem($activity, $item, $body);
1542         }
1543
1544         /**
1545          * @brief 
1546          *
1547          * @param array $activity
1548          * @param $body
1549          */
1550         private static function likeItem($activity, $body)
1551         {
1552                 $item = [];
1553                 $item['verb'] = ACTIVITY_LIKE;
1554                 $item['parent-uri'] = $activity['object'];
1555                 $item['gravity'] = GRAVITY_ACTIVITY;
1556                 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1557
1558                 self::postItem($activity, $item, $body);
1559         }
1560
1561         /**
1562          * @brief 
1563          *
1564          * @param array $activity
1565          * @param $body
1566          */
1567         private static function dislikeItem($activity, $body)
1568         {
1569                 $item = [];
1570                 $item['verb'] = ACTIVITY_DISLIKE;
1571                 $item['parent-uri'] = $activity['object'];
1572                 $item['gravity'] = GRAVITY_ACTIVITY;
1573                 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1574
1575                 self::postItem($activity, $item, $body);
1576         }
1577
1578         /**
1579          * @brief 
1580          *
1581          * @param array $activity
1582          * @param array $item
1583          * @param $body
1584          */
1585         private static function postItem($activity, $item, $body)
1586         {
1587                 /// @todo What to do with $activity['context']?
1588
1589                 if (($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['parent-uri']])) {
1590                         logger('Parent ' . $item['parent-uri'] . ' not found, message will be discarded.', LOGGER_DEBUG);
1591                         return;
1592                 }
1593
1594                 $item['network'] = Protocol::ACTIVITYPUB;
1595                 $item['private'] = !in_array(0, $activity['receiver']);
1596                 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
1597                 $item['owner-id'] = Contact::getIdForURL($activity['owner'], 0, true);
1598                 $item['uri'] = $activity['id'];
1599                 $item['created'] = $activity['published'];
1600                 $item['edited'] = $activity['updated'];
1601                 $item['guid'] = $activity['diaspora:guid'];
1602                 $item['title'] = HTML::toBBCode($activity['name']);
1603                 $item['content-warning'] = HTML::toBBCode($activity['summary']);
1604                 $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
1605                 $item['location'] = $activity['location'];
1606                 $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
1607                 $item['app'] = $activity['service'];
1608                 $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
1609
1610                 $item = self::constructAttachList($activity['attachments'], $item);
1611
1612                 $source = JsonLD::fetchElement($activity, 'source', 'content', 'mediaType', 'text/bbcode');
1613                 if (!empty($source)) {
1614                         $item['body'] = $source;
1615                 }
1616
1617                 $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
1618                 $item['source'] = $body;
1619                 $item['conversation-href'] = $activity['context'];
1620                 $item['conversation-uri'] = $activity['conversation'];
1621
1622                 foreach ($activity['receiver'] as $receiver) {
1623                         $item['uid'] = $receiver;
1624                         $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
1625
1626                         if (($receiver != 0) && empty($item['contact-id'])) {
1627                                 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
1628                         }
1629
1630                         $item_id = Item::insert($item);
1631                         logger('Storing for user ' . $item['uid'] . ': ' . $item_id);
1632                 }
1633         }
1634
1635         /**
1636          * @brief 
1637          *
1638          * @param $url
1639          * @param $child
1640          */
1641         private static function fetchMissingActivity($url, $child)
1642         {
1643                 if (Config::get('system', 'ostatus_full_threads')) {
1644                         return;
1645                 }
1646
1647                 $object = ActivityPub::fetchContent($url);
1648                 if (empty($object)) {
1649                         logger('Activity ' . $url . ' was not fetchable, aborting.');
1650                         return;
1651                 }
1652
1653                 $activity = [];
1654                 $activity['@context'] = $object['@context'];
1655                 unset($object['@context']);
1656                 $activity['id'] = $object['id'];
1657                 $activity['to'] = defaults($object, 'to', []);
1658                 $activity['cc'] = defaults($object, 'cc', []);
1659                 $activity['actor'] = $child['author'];
1660                 $activity['object'] = $object;
1661                 $activity['published'] = $object['published'];
1662                 $activity['type'] = 'Create';
1663
1664                 self::processActivity($activity);
1665                 logger('Activity ' . $url . ' had been fetched and processed.');
1666         }
1667
1668         /**
1669          * @brief perform a "follow" request
1670          *
1671          * @param array $activity
1672          */
1673         private static function followUser($activity)
1674         {
1675                 $actor = JsonLD::fetchElement($activity, 'object', 'id');
1676                 $uid = User::getIdForURL($actor);
1677                 if (empty($uid)) {
1678                         return;
1679                 }
1680
1681                 $owner = User::getOwnerDataById($uid);
1682
1683                 $cid = Contact::getIdForURL($activity['owner'], $uid);
1684                 if (!empty($cid)) {
1685                         $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1686                 } else {
1687                         $contact = false;
1688                 }
1689
1690                 $item = ['author-id' => Contact::getIdForURL($activity['owner']),
1691                         'author-link' => $activity['owner']];
1692
1693                 Contact::addRelationship($owner, $contact, $item);
1694                 $cid = Contact::getIdForURL($activity['owner'], $uid);
1695                 if (empty($cid)) {
1696                         return;
1697                 }
1698
1699                 $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid]);
1700                 if ($contact['network'] != Protocol::ACTIVITYPUB) {
1701                         Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
1702                 }
1703
1704                 DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
1705                 logger('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
1706         }
1707
1708         /**
1709          * @brief Update the given profile
1710          *
1711          * @param array $activity
1712          */
1713         private static function updatePerson($activity)
1714         {
1715                 if (empty($activity['object']['id'])) {
1716                         return;
1717                 }
1718
1719                 logger('Updating profile for ' . $activity['object']['id'], LOGGER_DEBUG);
1720                 APContact::getProfileByURL($activity['object']['id'], true);
1721         }
1722
1723         /**
1724          * @brief Accept a follow request
1725          *
1726          * @param array $activity
1727          */
1728         private static function acceptFollowUser($activity)
1729         {
1730                 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
1731                 $uid = User::getIdForURL($actor);
1732                 if (empty($uid)) {
1733                         return;
1734                 }
1735
1736                 $owner = User::getOwnerDataById($uid);
1737
1738                 $cid = Contact::getIdForURL($activity['owner'], $uid);
1739                 if (empty($cid)) {
1740                         logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1741                         return;
1742                 }
1743
1744                 $fields = ['pending' => false];
1745
1746                 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
1747                 if ($contact['rel'] == Contact::FOLLOWER) {
1748                         $fields['rel'] = Contact::FRIEND;
1749                 }
1750
1751                 $condition = ['id' => $cid];
1752                 DBA::update('contact', $fields, $condition);
1753                 logger('Accept contact request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
1754         }
1755
1756         /**
1757          * @brief Undo activity like "like" or "dislike"
1758          *
1759          * @param array $activity
1760          */
1761         private static function undoActivity($activity)
1762         {
1763                 $activity_url = JsonLD::fetchElement($activity, 'object', 'id');
1764                 if (empty($activity_url)) {
1765                         return;
1766                 }
1767
1768                 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
1769                 if (empty($actor)) {
1770                         return;
1771                 }
1772
1773                 $author_id = Contact::getIdForURL($actor);
1774                 if (empty($author_id)) {
1775                         return;
1776                 }
1777
1778                 Item::delete(['uri' => $activity_url, 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
1779         }
1780
1781         /**
1782          * @brief Activity to remove a follower
1783          *
1784          * @param array $activity
1785          */
1786         private static function undoFollowUser($activity)
1787         {
1788                 $object = JsonLD::fetchElement($activity, 'object', 'object');
1789                 $uid = User::getIdForURL($object);
1790                 if (empty($uid)) {
1791                         return;
1792                 }
1793
1794                 $owner = User::getOwnerDataById($uid);
1795
1796                 $cid = Contact::getIdForURL($activity['owner'], $uid);
1797                 if (empty($cid)) {
1798                         logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1799                         return;
1800                 }
1801
1802                 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1803                 if (!DBA::isResult($contact)) {
1804                         return;
1805                 }
1806
1807                 Contact::removeFollower($owner, $contact);
1808                 logger('Undo following request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
1809         }
1810 }