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