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;
20 * @brief ActivityPub Protocol class
21 * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
22 * https://www.w3.org/TR/activitypub/
23 * https://www.w3.org/TR/activitystreams-core/
24 * https://www.w3.org/TR/activitystreams-vocabulary/
26 * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
27 * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
29 * Digest: https://tools.ietf.org/html/rfc5843
30 * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
34 const PUBLIC = 'https://www.w3.org/ns/activitystreams#Public';
36 public static function transmit($content, $target, $uid)
38 $owner = User::getOwnerDataById($uid);
44 $host = parse_url($target, PHP_URL_HOST);
45 $path = parse_url($target, PHP_URL_PATH);
48 $headers = ['Host: ' . $host, 'Date: ' . $date];
50 $signed_data = "(request-target): post " . $path . "\nhost: " . $host . "\ndate: " . $date;
52 $signature = base64_encode(Crypto::rsaSign($signed_data, $owner['uprvkey'], 'sha256'));
54 $headers[] = 'Signature: keyId="' . $owner['url'] . '#main-key' . '",headers="(request-target) host date",signature="' . $signature . '"';
55 $headers[] = 'Content-Type: application/activity+json';
59 // $headers = HTTPSignature::createSig('', $headers, $owner['uprvkey'], $owner['url'] . '#main-key', false, false, 'sha256');
61 Network::post($target, $content, $headers);
62 $return_code = BaseObject::getApp()->get_curl_code();
63 echo $return_code."\n";
64 print_r(BaseObject::getApp()->get_curl_headers());
69 * Return the ActivityPub profile of the given user
71 * @param integer $uid User ID
74 public static function profile($uid)
76 $accounttype = ['Person', 'Organization', 'Service', 'Group', 'Application'];
78 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
79 'account_removed' => false, 'verified' => true];
80 $fields = ['guid', 'nickname', 'pubkey', 'account-type'];
81 $user = DBA::selectFirst('user', $fields, $condition);
82 if (!DBA::isResult($user)) {
86 $fields = ['locality', 'region', 'country-name'];
87 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
88 if (!DBA::isResult($profile)) {
92 $fields = ['name', 'url', 'location', 'about', 'avatar'];
93 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
94 if (!DBA::isResult($contact)) {
98 $data = ['@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
99 ['uuid' => 'http://schema.org/identifier', 'sensitive' => 'as:sensitive',
100 'vcard' => 'http://www.w3.org/2006/vcard/ns#']]];
102 $data['id'] = $contact['url'];
103 $data['uuid'] = $user['guid'];
104 $data['type'] = $accounttype[$user['account-type']];
105 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
106 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
107 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
108 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
109 $data['preferredUsername'] = $user['nickname'];
110 $data['name'] = $contact['name'];
111 $data['vcard:hasAddress'] = ['@type' => 'Home', 'vcard:country-name' => $profile['country-name'],
112 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
113 $data['summary'] = $contact['about'];
114 $data['url'] = $contact['url'];
115 $data['manuallyApprovesFollowers'] = false;
116 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
117 'owner' => $contact['url'],
118 'publicKeyPem' => $user['pubkey']];
119 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
120 $data['icon'] = ['type' => 'Image',
121 'url' => $contact['avatar']];
123 // tags: https://kitty.town/@inmysocks/100656097926961126.json
127 public static function createActivityFromItem($item_id)
129 $item = Item::selectFirst([], ['id' => $item_id]);
131 $data = ['@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
132 ['Emoji' => 'toot:Emoji', 'Hashtag' => 'as:Hashtag', 'atomUri' => 'ostatus:atomUri',
133 'conversation' => 'ostatus:conversation', 'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri',
134 'ostatus' => 'http://ostatus.org#', 'sensitive' => 'as:sensitive',
135 'toot' => 'http://joinmastodon.org/ns#']]];
137 $data['type'] = 'Create';
138 $data['id'] = $item['plink'];
139 $data['actor'] = $item['author-link'];
140 $data['to'] = 'https://www.w3.org/ns/activitystreams#Public';
141 $data['object'] = self::createNote($item);
147 public static function createNote($item)
150 $data['type'] = 'Note';
151 $data['id'] = $item['plink'];
152 //$data['context'] = $data['conversation'] = $item['parent-uri'];
153 $data['actor'] = $item['author-link'];
154 // if (!$item['private']) {
156 // $data['to'][] = '"https://pleroma.soykaf.com/users/heluecht"';
157 $data['to'] = 'https://www.w3.org/ns/activitystreams#Public';
158 // $data['cc'] = 'https://pleroma.soykaf.com/users/heluecht';
160 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
161 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
162 $data['attributedTo'] = $item['author-link'];
163 $data['title'] = BBCode::convert($item['title'], false, 7);
164 $data['content'] = BBCode::convert($item['body'], false, 7);
165 //$data['summary'] = '';
166 //$data['sensitive'] = false;
167 //$data['emoji'] = [];
169 //$data['attachment'] = [];
174 * Fetches ActivityPub content from the given url
176 * @param string $url content url
179 public static function fetchContent($url)
181 $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json']);
183 if (!$ret['success'] || empty($ret['body'])) {
187 return json_decode($ret['body'], true);
191 * Resolves the profile url from the address by using webfinger
193 * @param string $addr profile address (user@domain.tld)
196 private static function addrToUrl($addr)
198 $addr_parts = explode('@', $addr);
199 if (count($addr_parts) != 2) {
203 $webfinger = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
205 $ret = Network::curl($webfinger, false, $redirects, ['accept_content' => 'application/jrd+json,application/json']);
206 if (!$ret['success'] || empty($ret['body'])) {
210 $data = json_decode($ret['body'], true);
212 if (empty($data['links'])) {
216 foreach ($data['links'] as $link) {
217 if (empty($link['href']) || empty($link['rel']) || empty($link['type'])) {
221 if (($link['rel'] == 'self') && ($link['type'] == 'application/activity+json')) {
222 return $link['href'];
230 * Fetches a profile from the given url
232 * @param string $url profile url
235 public static function fetchProfile($url)
237 if (empty(parse_url($url, PHP_URL_SCHEME))) {
238 $url = self::addrToUrl($url);
244 $data = self::fetchContent($url);
246 if (empty($data) || empty($data['id']) || empty($data['inbox'])) {
250 $profile = ['network' => Protocol::ACTIVITYPUB];
251 $profile['nick'] = $data['preferredUsername'];
252 $profile['name'] = defaults($data, 'name', $profile['nick']);
253 $profile['guid'] = defaults($data, 'uuid', null);
254 $profile['url'] = $data['id'];
255 $profile['alias'] = self::processElement($data, 'url', 'href');
257 $parts = parse_url($profile['url']);
258 unset($parts['scheme']);
259 unset($parts['path']);
260 $profile['addr'] = $profile['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
262 $profile['photo'] = self::processElement($data, 'icon', 'url');
263 $profile['about'] = defaults($data, 'summary', '');
264 $profile['batch'] = self::processElement($data, 'endpoints', 'sharedInbox');
265 $profile['pubkey'] = self::processElement($data, 'publicKey', 'publicKeyPem');
266 $profile['notify'] = $data['inbox'];
267 $profile['poll'] = $data['outbox'];
269 // Check if the address is resolvable
270 if (self::addrToUrl($profile['addr']) == $profile['url']) {
271 $parts = parse_url($profile['url']);
272 unset($parts['path']);
273 $profile['baseurl'] = Network::unparseURL($parts);
275 unset($profile['addr']);
278 if ($profile['url'] == $profile['alias']) {
279 unset($profile['alias']);
282 // Remove all "null" fields
283 foreach ($profile as $field => $content) {
284 if (is_null($content)) {
285 unset($profile[$field]);
291 unset($data['inbox']);
292 unset($data['outbox']);
293 unset($data['preferredUsername']);
294 unset($data['name']);
295 unset($data['summary']);
297 unset($data['publicKey']);
298 unset($data['endpoints']);
299 unset($data['icon']);
300 unset($data['uuid']);
303 unset($data['type']);
304 unset($data['manuallyApprovesFollowers']);
307 unset($data['@context']);
309 unset($data['attachment']);
310 unset($data['image']);
311 unset($data['nomadicLocations']);
312 unset($data['signature']);
313 unset($data['following']);
314 unset($data['followers']);
315 unset($data['featured']);
316 unset($data['movedTo']);
317 unset($data['liked']);
318 unset($data['sharedInbox']); // Misskey
319 unset($data['isCat']); // Misskey
320 unset($data['kroeg:blocks']); // Kroeg
321 unset($data['updated']); // Kroeg
323 /* if (!empty($data)) {
331 public static function fetchOutbox($url)
333 $data = self::fetchContent($url);
338 if (!empty($data['orderedItems'])) {
339 $items = $data['orderedItems'];
340 } elseif (!empty($data['first']['orderedItems'])) {
341 $items = $data['first']['orderedItems'];
342 } elseif (!empty($data['first'])) {
343 self::fetchOutbox($data['first']);
349 foreach ($items as $activity) {
350 self::processActivity($activity, $url);
354 function processActivity($activity, $url)
356 if (empty($activity['type'])) {
360 if (empty($activity['object'])) {
364 if (empty($activity['actor'])) {
368 $actor = self::processElement($activity, 'actor', 'id');
373 if (is_string($activity['object'])) {
374 $object_url = $activity['object'];
375 } elseif (!empty($activity['object']['id'])) {
376 $object_url = $activity['object']['id'];
381 $receivers = self::getReceivers($activity);
382 if (empty($receivers)) {
386 // ----------------------------------
388 unset($activity['@context']);
389 unset($activity['id']);
392 unset($activity['title']);
393 unset($activity['atomUri']);
394 unset($activity['context_id']);
395 unset($activity['statusnetConversationId']);
397 $structure = $activity;
400 unset($activity['context']);
401 unset($activity['location']);
404 unset($activity['to']);
405 unset($activity['cc']);
406 unset($activity['bto']);
407 unset($activity['bcc']);
408 unset($activity['type']);
409 unset($activity['actor']);
410 unset($activity['object']);
411 unset($activity['published']);
412 unset($activity['updated']);
413 unset($activity['instrument']);
414 unset($activity['inReplyTo']);
416 if (!empty($activity)) {
422 $activity = $structure;
423 // ----------------------------------
425 $item = self::fetchObject($object_url, $url);
430 $item = self::addActivityFields($item, $activity);
432 $item['owner'] = $actor;
434 $item['receiver'] = array_merge($item['receiver'], $receivers);
436 switch ($activity['type']) {
439 self::createItem($item);
443 self::announceItem($item);
448 self::activityItem($item);
455 echo "Unknown activity: ".$activity['type']."\n";
462 private static function getReceivers($activity)
466 $elements = ['to', 'cc', 'bto', 'bcc'];
467 foreach ($elements as $element) {
468 if (empty($activity[$element])) {
472 // The receiver can be an arror or a string
473 if (is_string($activity[$element])) {
474 $activity[$element] = [$activity[$element]];
477 foreach ($activity[$element] as $receiver) {
478 if ($receiver == self::PUBLIC) {
479 $receivers[$receiver] = 0;
482 $condition = ['self' => true, 'nurl' => normalise_link($receiver)];
483 $contact = DBA::selectFirst('contact', ['id'], $condition);
484 if (!DBA::isResult($contact)) {
487 $receivers[$receiver] = $contact['id'];
493 private static function addActivityFields($item, $activity)
495 if (!empty($activity['published']) && empty($item['published'])) {
496 $item['published'] = $activity['published'];
499 if (!empty($activity['updated']) && empty($item['updated'])) {
500 $item['updated'] = $activity['updated'];
503 if (!empty($activity['inReplyTo']) && empty($item['parent-uri'])) {
504 $item['parent-uri'] = self::processElement($activity, 'inReplyTo', 'id');
507 if (!empty($activity['instrument'])) {
508 $item['service'] = self::processElement($activity, 'instrument', 'name', 'Service');
511 // Remove all "null" fields
512 foreach ($item as $field => $content) {
513 if (is_null($content)) {
514 unset($item[$field]);
521 private static function fetchObject($object_url, $url)
523 $data = self::fetchContent($object_url);
528 if (empty($data['type'])) {
531 $type = $data['type'];
534 if (in_array($type, ['Note', 'Article', 'Video'])) {
535 $common = self::processCommonData($data, $url);
540 return array_merge($common, self::processNote($data, $url));
542 return array_merge($common, self::processArticle($data, $url));
544 return array_merge($common, self::processVideo($data, $url));
547 if (empty($data['object'])) {
550 return self::fetchObject($data['object'], $url);
557 echo "Unknown object type: ".$data['type']."\n";
564 private static function processCommonData(&$object, $url)
566 if (empty($object['id']) || empty($object['attributedTo'])) {
571 $item['uri'] = $object['id'];
573 if (!empty($object['inReplyTo'])) {
574 $item['reply-to-uri'] = self::processElement($object, 'inReplyTo', 'id');
576 $item['reply-to-uri'] = $item['uri'];
579 $item['published'] = defaults($object, 'published', null);
580 $item['updated'] = defaults($object, 'updated', $item['published']);
582 if (empty($item['published']) && !empty($item['updated'])) {
583 $item['published'] = $item['updated'];
586 $item['uuid'] = defaults($object, 'uuid', null);
587 $item['owner'] = $item['author'] = self::processElement($object, 'attributedTo', 'id');
588 $item['context'] = defaults($object, 'context', null);
589 $item['conversation'] = defaults($object, 'conversation', null);
590 $item['sensitive'] = defaults($object, 'sensitive', null);
591 $item['name'] = defaults($object, 'name', null);
592 $item['title'] = defaults($object, 'title', null);
593 $item['content'] = defaults($object, 'content', null);
594 $item['summary'] = defaults($object, 'summary', null);
595 $item['location'] = self::processElement($object, 'location', 'name', 'Place');
596 $item['attachments'] = defaults($object, 'attachment', null);
597 $item['tags'] = defaults($object, 'tag', null);
598 $item['service'] = self::processElement($object, 'instrument', 'name', 'Service');
599 $item['alternate-url'] = self::processElement($object, 'url', 'href');
600 $item['receiver'] = self::getReceivers($object);
603 unset($object['id']);
604 unset($object['inReplyTo']);
605 unset($object['published']);
606 unset($object['updated']);
607 unset($object['uuid']);
608 unset($object['attributedTo']);
609 unset($object['context']);
610 unset($object['conversation']);
611 unset($object['sensitive']);
612 unset($object['name']);
613 unset($object['title']);
614 unset($object['content']);
615 unset($object['summary']);
616 unset($object['location']);
617 unset($object['attachment']);
618 unset($object['tag']);
619 unset($object['instrument']);
620 unset($object['url']);
621 unset($object['to']);
622 unset($object['cc']);
623 unset($object['bto']);
624 unset($object['bcc']);
627 unset($object['source']);
630 unset($object['@context']);
631 unset($object['type']);
632 unset($object['actor']);
633 unset($object['signature']);
634 unset($object['mediaType']);
635 unset($object['duration']);
636 unset($object['replies']);
637 unset($object['icon']);
640 audience, preview, endTime, startTime, generator, image
646 private static function processNote($object, $url)
651 unset($object['emoji']);
652 unset($object['atomUri']);
653 unset($object['inReplyToAtomUri']);
656 unset($object['contentMap']);
657 unset($object['announcement_count']);
658 unset($object['announcements']);
659 unset($object['context_id']);
660 unset($object['likes']);
661 unset($object['like_count']);
662 unset($object['inReplyToStatusId']);
663 unset($object['shares']);
664 unset($object['quoteUrl']);
665 unset($object['statusnetConversationId']);
670 echo "Unknown Note\n";
678 private static function processArticle($object, $url)
685 echo "Unknown Article\n";
693 private static function processVideo($object, $url)
698 unset($object['category']);
699 unset($object['licence']);
700 unset($object['language']);
701 unset($object['commentsEnabled']);
704 unset($object['views']);
705 unset($object['waitTranscoding']);
706 unset($object['state']);
707 unset($object['support']);
708 unset($object['subtitleLanguage']);
709 unset($object['likes']);
710 unset($object['dislikes']);
711 unset($object['shares']);
712 unset($object['comments']);
717 echo "Unknown Video\n";
725 private static function processElement($array, $element, $key, $type = null)
731 if (empty($array[$element])) {
735 if (is_string($array[$element])) {
736 return $array[$element];
739 if (is_null($type)) {
740 if (!empty($array[$element][$key])) {
741 return $array[$element][$key];
744 if (!empty($array[$element][0][$key])) {
745 return $array[$element][0][$key];
751 if (!empty($array[$element][$key]) && !empty($array[$element]['type']) && ($array[$element]['type'] == $type)) {
752 return $array[$element][$key];
755 /// @todo Add array search
760 private static function createItem($item)
765 private static function announceItem($item)
770 private static function activityItem($item)