3 * @file src/Protocol/ActivityPub.php
5 namespace Friendica\Protocol;
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\Item;
14 use Friendica\Model\User;
15 use Friendica\Util\DateTimeFormat;
16 use Friendica\Util\Crypto;
17 use Friendica\Content\Text\BBCode;
18 use Friendica\Network\Probe;
21 * @brief ActivityPub Protocol class
22 * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
23 * https://www.w3.org/TR/activitypub/
24 * https://www.w3.org/TR/activitystreams-core/
25 * https://www.w3.org/TR/activitystreams-vocabulary/
27 * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
28 * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
30 * Digest: https://tools.ietf.org/html/rfc5843
31 * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
35 const PUBLIC = 'https://www.w3.org/ns/activitystreams#Public';
37 public static function transmit($content, $target, $uid)
39 $owner = User::getOwnerDataById($uid);
45 $host = parse_url($target, PHP_URL_HOST);
46 $path = parse_url($target, PHP_URL_PATH);
49 $headers = ['Host: ' . $host, 'Date: ' . $date];
51 $signed_data = "(request-target): post " . $path . "\nhost: " . $host . "\ndate: " . $date;
53 $signature = base64_encode(Crypto::rsaSign($signed_data, $owner['uprvkey'], 'sha256'));
55 $headers[] = 'Signature: keyId="' . $owner['url'] . '#main-key' . '",headers="(request-target) host date",signature="' . $signature . '"';
56 $headers[] = 'Content-Type: application/activity+json';
58 Network::post($target, $content, $headers);
59 $return_code = BaseObject::getApp()->get_curl_code();
61 echo $return_code."\n";
65 * Return the ActivityPub profile of the given user
67 * @param integer $uid User ID
70 public static function profile($uid)
72 $accounttype = ['Person', 'Organization', 'Service', 'Group', 'Application'];
74 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
75 'account_removed' => false, 'verified' => true];
76 $fields = ['guid', 'nickname', 'pubkey', 'account-type'];
77 $user = DBA::selectFirst('user', $fields, $condition);
78 if (!DBA::isResult($user)) {
82 $fields = ['locality', 'region', 'country-name'];
83 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
84 if (!DBA::isResult($profile)) {
88 $fields = ['name', 'url', 'location', 'about', 'avatar'];
89 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
90 if (!DBA::isResult($contact)) {
94 $data = ['@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
95 ['uuid' => 'http://schema.org/identifier', 'sensitive' => 'as:sensitive',
96 'vcard' => 'http://www.w3.org/2006/vcard/ns#']]];
98 $data['id'] = $contact['url'];
99 $data['uuid'] = $user['guid'];
100 $data['type'] = $accounttype[$user['account-type']];
101 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
102 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
103 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
104 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
105 $data['preferredUsername'] = $user['nickname'];
106 $data['name'] = $contact['name'];
107 $data['vcard:hasAddress'] = ['@type' => 'Home', 'vcard:country-name' => $profile['country-name'],
108 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
109 $data['summary'] = $contact['about'];
110 $data['url'] = $contact['url'];
111 $data['manuallyApprovesFollowers'] = false;
112 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
113 'owner' => $contact['url'],
114 'publicKeyPem' => $user['pubkey']];
115 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
116 $data['icon'] = ['type' => 'Image',
117 'url' => $contact['avatar']];
119 // tags: https://kitty.town/@inmysocks/100656097926961126.json
123 public static function createActivityFromItem($item_id)
125 $item = Item::selectFirst([], ['id' => $item_id]);
127 $data = ['@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
128 ['Emoji' => 'toot:Emoji', 'Hashtag' => 'as:Hashtag', 'atomUri' => 'ostatus:atomUri',
129 'conversation' => 'ostatus:conversation', 'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri',
130 'ostatus' => 'http://ostatus.org#', 'sensitive' => 'as:sensitive',
131 'toot' => 'http://joinmastodon.org/ns#']]];
133 $data['type'] = 'Create';
134 $data['id'] = $item['plink'];
135 $data['actor'] = $item['author-link'];
136 $data['to'] = 'https://www.w3.org/ns/activitystreams#Public';
137 $data['object'] = self::createNote($item);
143 public static function createNote($item)
146 $data['type'] = 'Note';
147 $data['id'] = $item['plink'];
148 //$data['context'] = $data['conversation'] = $item['parent-uri'];
149 $data['actor'] = $item['author-link'];
150 // if (!$item['private']) {
152 // $data['to'][] = '"https://pleroma.soykaf.com/users/heluecht"';
153 $data['to'] = 'https://www.w3.org/ns/activitystreams#Public';
154 // $data['cc'] = 'https://pleroma.soykaf.com/users/heluecht';
156 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
157 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
158 $data['attributedTo'] = $item['author-link'];
159 $data['name'] = BBCode::convert($item['title'], false, 7);
160 $data['content'] = BBCode::convert($item['body'], false, 7);
161 //$data['summary'] = '';
162 //$data['sensitive'] = false;
163 //$data['emoji'] = [];
165 //$data['attachment'] = [];
170 * Fetches ActivityPub content from the given url
172 * @param string $url content url
175 public static function fetchContent($url)
177 $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json']);
179 if (!$ret['success'] || empty($ret['body'])) {
183 return json_decode($ret['body'], true);
187 * Resolves the profile url from the address by using webfinger
189 * @param string $addr profile address (user@domain.tld)
192 private static function addrToUrl($addr)
194 $addr_parts = explode('@', $addr);
195 if (count($addr_parts) != 2) {
199 $webfinger = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
201 $ret = Network::curl($webfinger, false, $redirects, ['accept_content' => 'application/jrd+json,application/json']);
202 if (!$ret['success'] || empty($ret['body'])) {
206 $data = json_decode($ret['body'], true);
208 if (empty($data['links'])) {
212 foreach ($data['links'] as $link) {
213 if (empty($link['href']) || empty($link['rel']) || empty($link['type'])) {
217 if (($link['rel'] == 'self') && ($link['type'] == 'application/activity+json')) {
218 return $link['href'];
225 public static function verifySignature($content, $http_headers)
227 $object = json_decode($content, true);
229 if (empty($object)) {
233 $actor = self::processElement($object, 'actor', 'id');
236 $headers['(request-target)'] = strtolower($http_headers['REQUEST_METHOD']) . ' ' . $http_headers['REQUEST_URI'];
238 // First take every header
239 foreach ($http_headers as $k => $v) {
240 $field = str_replace('_', '-', strtolower($k));
241 $headers[$field] = $v;
244 // Now add every http header
245 foreach ($http_headers as $k => $v) {
246 if (strpos($k, 'HTTP_') === 0) {
247 $field = str_replace('_', '-', strtolower(substr($k, 5)));
248 $headers[$field] = $v;
252 $sig_block = ActivityPub::parseSigHeader($http_headers['HTTP_SIGNATURE']);
254 if (empty($sig_block) || empty($sig_block['headers']) || empty($sig_block['keyId'])) {
259 foreach ($sig_block['headers'] as $h) {
260 if (array_key_exists($h, $headers)) {
261 $signed_data .= $h . ': ' . $headers[$h] . "\n";
264 $signed_data = rtrim($signed_data, "\n");
266 if (empty($signed_data)) {
272 if ($sig_block['algorithm'] === 'rsa-sha256') {
273 $algorithm = 'sha256';
276 if ($sig_block['algorithm'] === 'rsa-sha512') {
277 $algorithm = 'sha512';
280 if (empty($algorithm)) {
284 $key = self::fetchKey($sig_block['keyId'], $actor);
290 if (!Crypto::rsaVerify($signed_data, $sig_block['signature'], $key, $algorithm)) {
294 // Check the digest if it was part of the signed data
295 if (in_array('digest', $sig_block['headers'])) {
296 $digest = explode('=', $headers['digest'], 2);
297 if ($digest[0] === 'SHA-256') {
300 if ($digest[0] === 'SHA-512') {
304 /// @todo addd all hashes from the rfc
306 if (!empty($hashalg) && base64_encode(hash($hashalg, $content, true)) != $digest[1]) {
311 // Check the content-length if it was part of the signed data
312 if (in_array('content-length', $sig_block['headers'])) {
313 if (strlen($content) != $headers['content-length']) {
322 private static function fetchKey($id, $actor)
324 $url = (strpos($id, '#') ? substr($id, 0, strpos($id, '#')) : $id);
326 $profile = Probe::uri($url, Protocol::ACTIVITYPUB);
327 if (!empty($profile)) {
328 return $profile['pubkey'];
329 } elseif ($url != $actor) {
330 $profile = Probe::uri($actor, Protocol::ACTIVITYPUB);
331 if (!empty($profile)) {
332 return $profile['pubkey'];
342 * @param string $header
343 * @return array associate array with
344 * - \e string \b keyID
345 * - \e string \b algorithm
346 * - \e array \b headers
347 * - \e string \b signature
349 private static function parseSigHeader($header)
354 if (preg_match('/keyId="(.*?)"/ism',$header,$matches)) {
355 $ret['keyId'] = $matches[1];
358 if (preg_match('/algorithm="(.*?)"/ism',$header,$matches)) {
359 $ret['algorithm'] = $matches[1];
362 if (preg_match('/headers="(.*?)"/ism',$header,$matches)) {
363 $ret['headers'] = explode(' ', $matches[1]);
366 if (preg_match('/signature="(.*?)"/ism',$header,$matches)) {
367 $ret['signature'] = base64_decode(preg_replace('/\s+/','',$matches[1]));
374 * Fetches a profile from the given url
376 * @param string $url profile url
379 public static function fetchProfile($url)
381 if (empty(parse_url($url, PHP_URL_SCHEME))) {
382 $url = self::addrToUrl($url);
388 $data = self::fetchContent($url);
390 if (empty($data) || empty($data['id']) || empty($data['inbox'])) {
394 $profile = ['network' => Protocol::ACTIVITYPUB];
395 $profile['nick'] = $data['preferredUsername'];
396 $profile['name'] = defaults($data, 'name', $profile['nick']);
397 $profile['guid'] = defaults($data, 'uuid', null);
398 $profile['url'] = $data['id'];
400 $parts = parse_url($profile['url']);
401 unset($parts['scheme']);
402 unset($parts['path']);
403 $profile['addr'] = $profile['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
404 $profile['alias'] = self::processElement($data, 'url', 'href');
405 $profile['photo'] = self::processElement($data, 'icon', 'url');
406 // $profile['community']
407 // $profile['keywords']
408 // $profile['location']
409 $profile['about'] = defaults($data, 'summary', '');
410 $profile['batch'] = self::processElement($data, 'endpoints', 'sharedInbox');
411 $profile['notify'] = $data['inbox'];
412 $profile['poll'] = $data['outbox'];
413 $profile['pubkey'] = self::processElement($data, 'publicKey', 'publicKeyPem');
415 // Check if the address is resolvable
416 if (self::addrToUrl($profile['addr']) == $profile['url']) {
417 $parts = parse_url($profile['url']);
418 unset($parts['path']);
419 $profile['baseurl'] = Network::unparseURL($parts);
421 unset($profile['addr']);
424 if ($profile['url'] == $profile['alias']) {
425 unset($profile['alias']);
428 // Remove all "null" fields
429 foreach ($profile as $field => $content) {
430 if (is_null($content)) {
431 unset($profile[$field]);
437 unset($data['inbox']);
438 unset($data['outbox']);
439 unset($data['preferredUsername']);
440 unset($data['name']);
441 unset($data['summary']);
443 unset($data['publicKey']);
444 unset($data['endpoints']);
445 unset($data['icon']);
446 unset($data['uuid']);
449 unset($data['type']);
450 unset($data['manuallyApprovesFollowers']);
453 unset($data['@context']);
455 unset($data['attachment']);
456 unset($data['image']);
457 unset($data['nomadicLocations']);
458 unset($data['signature']);
459 unset($data['following']);
460 unset($data['followers']);
461 unset($data['featured']);
462 unset($data['movedTo']);
463 unset($data['liked']);
464 unset($data['sharedInbox']); // Misskey
465 unset($data['isCat']); // Misskey
466 unset($data['kroeg:blocks']); // Kroeg
467 unset($data['updated']); // Kroeg
469 /* if (!empty($data)) {
477 public static function fetchOutbox($url)
479 $data = self::fetchContent($url);
484 if (!empty($data['orderedItems'])) {
485 $items = $data['orderedItems'];
486 } elseif (!empty($data['first']['orderedItems'])) {
487 $items = $data['first']['orderedItems'];
488 } elseif (!empty($data['first'])) {
489 self::fetchOutbox($data['first']);
495 foreach ($items as $activity) {
496 self::processActivity($activity, $url);
500 function processActivity($activity, $url)
502 if (empty($activity['type'])) {
506 if (empty($activity['object'])) {
510 if (empty($activity['actor'])) {
514 $actor = self::processElement($activity, 'actor', 'id');
519 if (is_string($activity['object'])) {
520 $object_url = $activity['object'];
521 } elseif (!empty($activity['object']['id'])) {
522 $object_url = $activity['object']['id'];
527 $receivers = self::getReceivers($activity);
528 if (empty($receivers)) {
532 // ----------------------------------
534 unset($activity['@context']);
535 unset($activity['id']);
538 unset($activity['title']);
539 unset($activity['atomUri']);
540 unset($activity['context_id']);
541 unset($activity['statusnetConversationId']);
543 $structure = $activity;
546 unset($activity['context']);
547 unset($activity['location']);
550 unset($activity['to']);
551 unset($activity['cc']);
552 unset($activity['bto']);
553 unset($activity['bcc']);
554 unset($activity['type']);
555 unset($activity['actor']);
556 unset($activity['object']);
557 unset($activity['published']);
558 unset($activity['updated']);
559 unset($activity['instrument']);
560 unset($activity['inReplyTo']);
562 if (!empty($activity)) {
568 $activity = $structure;
569 // ----------------------------------
571 $item = self::fetchObject($object_url, $url);
576 $item = self::addActivityFields($item, $activity);
578 $item['owner'] = $actor;
580 $item['receiver'] = array_merge($item['receiver'], $receivers);
582 switch ($activity['type']) {
585 self::createItem($item);
589 self::announceItem($item);
594 self::activityItem($item);
601 echo "Unknown activity: ".$activity['type']."\n";
608 private static function getReceivers($activity)
612 $elements = ['to', 'cc', 'bto', 'bcc'];
613 foreach ($elements as $element) {
614 if (empty($activity[$element])) {
618 // The receiver can be an arror or a string
619 if (is_string($activity[$element])) {
620 $activity[$element] = [$activity[$element]];
623 foreach ($activity[$element] as $receiver) {
624 if ($receiver == self::PUBLIC) {
625 $receivers[$receiver] = 0;
628 $condition = ['self' => true, 'nurl' => normalise_link($receiver)];
629 $contact = DBA::selectFirst('contact', ['id'], $condition);
630 if (!DBA::isResult($contact)) {
633 $receivers[$receiver] = $contact['id'];
639 private static function addActivityFields($item, $activity)
641 if (!empty($activity['published']) && empty($item['published'])) {
642 $item['published'] = $activity['published'];
645 if (!empty($activity['updated']) && empty($item['updated'])) {
646 $item['updated'] = $activity['updated'];
649 if (!empty($activity['inReplyTo']) && empty($item['parent-uri'])) {
650 $item['parent-uri'] = self::processElement($activity, 'inReplyTo', 'id');
653 if (!empty($activity['instrument'])) {
654 $item['service'] = self::processElement($activity, 'instrument', 'name', 'Service');
657 // Remove all "null" fields
658 foreach ($item as $field => $content) {
659 if (is_null($content)) {
660 unset($item[$field]);
667 private static function fetchObject($object_url, $url)
669 $data = self::fetchContent($object_url);
674 if (empty($data['type'])) {
677 $type = $data['type'];
680 if (in_array($type, ['Note', 'Article', 'Video'])) {
681 $common = self::processCommonData($data, $url);
686 return array_merge($common, self::processNote($data, $url));
688 return array_merge($common, self::processArticle($data, $url));
690 return array_merge($common, self::processVideo($data, $url));
693 if (empty($data['object'])) {
696 return self::fetchObject($data['object'], $url);
703 echo "Unknown object type: ".$data['type']."\n";
710 private static function processCommonData(&$object, $url)
712 if (empty($object['id']) || empty($object['attributedTo'])) {
717 $item['uri'] = $object['id'];
719 if (!empty($object['inReplyTo'])) {
720 $item['reply-to-uri'] = self::processElement($object, 'inReplyTo', 'id');
722 $item['reply-to-uri'] = $item['uri'];
725 $item['published'] = defaults($object, 'published', null);
726 $item['updated'] = defaults($object, 'updated', $item['published']);
728 if (empty($item['published']) && !empty($item['updated'])) {
729 $item['published'] = $item['updated'];
732 $item['uuid'] = defaults($object, 'uuid', null);
733 $item['owner'] = $item['author'] = self::processElement($object, 'attributedTo', 'id');
734 $item['context'] = defaults($object, 'context', null);
735 $item['conversation'] = defaults($object, 'conversation', null);
736 $item['sensitive'] = defaults($object, 'sensitive', null);
737 $item['name'] = defaults($object, 'title', null);
738 $item['name'] = defaults($object, 'name', $item['name']);
739 $item['summary'] = defaults($object, 'summary', null);
740 $item['content'] = defaults($object, 'content', null);
741 $item['location'] = self::processElement($object, 'location', 'name', 'Place');
742 $item['attachments'] = defaults($object, 'attachment', null);
743 $item['tags'] = defaults($object, 'tag', null);
744 $item['service'] = self::processElement($object, 'instrument', 'name', 'Service');
745 $item['alternate-url'] = self::processElement($object, 'url', 'href');
746 $item['receiver'] = self::getReceivers($object);
749 unset($object['id']);
750 unset($object['inReplyTo']);
751 unset($object['published']);
752 unset($object['updated']);
753 unset($object['uuid']);
754 unset($object['attributedTo']);
755 unset($object['context']);
756 unset($object['conversation']);
757 unset($object['sensitive']);
758 unset($object['name']);
759 unset($object['title']);
760 unset($object['content']);
761 unset($object['summary']);
762 unset($object['location']);
763 unset($object['attachment']);
764 unset($object['tag']);
765 unset($object['instrument']);
766 unset($object['url']);
767 unset($object['to']);
768 unset($object['cc']);
769 unset($object['bto']);
770 unset($object['bcc']);
773 unset($object['source']);
776 unset($object['@context']);
777 unset($object['type']);
778 unset($object['actor']);
779 unset($object['signature']);
780 unset($object['mediaType']);
781 unset($object['duration']);
782 unset($object['replies']);
783 unset($object['icon']);
786 audience, preview, endTime, startTime, generator, image
792 private static function processNote($object, $url)
797 unset($object['emoji']);
798 unset($object['atomUri']);
799 unset($object['inReplyToAtomUri']);
802 unset($object['contentMap']);
803 unset($object['announcement_count']);
804 unset($object['announcements']);
805 unset($object['context_id']);
806 unset($object['likes']);
807 unset($object['like_count']);
808 unset($object['inReplyToStatusId']);
809 unset($object['shares']);
810 unset($object['quoteUrl']);
811 unset($object['statusnetConversationId']);
816 echo "Unknown Note\n";
824 private static function processArticle($object, $url)
831 echo "Unknown Article\n";
839 private static function processVideo($object, $url)
844 unset($object['category']);
845 unset($object['licence']);
846 unset($object['language']);
847 unset($object['commentsEnabled']);
850 unset($object['views']);
851 unset($object['waitTranscoding']);
852 unset($object['state']);
853 unset($object['support']);
854 unset($object['subtitleLanguage']);
855 unset($object['likes']);
856 unset($object['dislikes']);
857 unset($object['shares']);
858 unset($object['comments']);
863 echo "Unknown Video\n";
871 private static function processElement($array, $element, $key, $type = null)
877 if (empty($array[$element])) {
881 if (is_string($array[$element])) {
882 return $array[$element];
885 if (is_null($type)) {
886 if (!empty($array[$element][$key])) {
887 return $array[$element][$key];
890 if (!empty($array[$element][0][$key])) {
891 return $array[$element][0][$key];
897 if (!empty($array[$element][$key]) && !empty($array[$element]['type']) && ($array[$element]['type'] == $type)) {
898 return $array[$element][$key];
901 /// @todo Add array search
906 private static function createItem($item)
911 private static function announceItem($item)
916 private static function activityItem($item)