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