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