2019-06-21 18:30:34 +03:00
|
|
|
<?php
|
|
|
|
|
2022-06-21 02:11:46 +03:00
|
|
|
class MastodonBridge extends BridgeAbstract
|
|
|
|
{
|
|
|
|
// This script attempts to imitiate the behaviour of a read-only ActivityPub server
|
|
|
|
// to read the outbox.
|
2019-06-21 18:30:34 +03:00
|
|
|
|
2022-06-21 02:11:46 +03:00
|
|
|
// Note: Most PixelFed instances have ActivityPub outbox disabled,
|
|
|
|
// so use the official feed: https://pixelfed.instance/users/username.atom (Posts only)
|
|
|
|
|
|
|
|
const MAINTAINER = 'Austin Huang';
|
|
|
|
const NAME = 'ActivityPub Bridge';
|
2019-06-21 18:30:34 +03:00
|
|
|
const CACHE_TIMEOUT = 900; // 15mn
|
2022-06-21 02:11:46 +03:00
|
|
|
const DESCRIPTION = 'Returns recent statuses. Supports Mastodon, Pleroma and Misskey, among others. Access to
|
2022-11-16 20:05:01 +03:00
|
|
|
instances that have Authorized Fetch enabled requires
|
|
|
|
<a href="https://rss-bridge.github.io/rss-bridge/Bridge_Specific/ActivityPub_(Mastodon).html">configuration</a>.';
|
2019-06-21 18:30:34 +03:00
|
|
|
const URI = 'https://mastodon.social';
|
|
|
|
|
2022-06-21 02:11:46 +03:00
|
|
|
// Some Mastodon instances use Secure Mode which requires all requests to be signed.
|
|
|
|
// You do not need this for most instances, but if you want to support every known
|
|
|
|
// instance, then you should configure them.
|
|
|
|
// See also https://docs.joinmastodon.org/spec/security/#http
|
|
|
|
const CONFIGURATION = [
|
|
|
|
'private_key' => [
|
|
|
|
'required' => false,
|
|
|
|
],
|
|
|
|
'key_id' => [
|
|
|
|
'required' => false,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2019-06-21 18:30:34 +03:00
|
|
|
const PARAMETERS = [[
|
|
|
|
'canusername' => [
|
2022-03-24 13:59:34 +03:00
|
|
|
'name' => 'Canonical username',
|
|
|
|
'exampleValue' => '@sebsauvage@framapiaf.org',
|
2019-06-21 18:30:34 +03:00
|
|
|
'required' => true,
|
|
|
|
],
|
2023-08-20 05:37:21 +03:00
|
|
|
'noregular' => [
|
|
|
|
'name' => 'Without regular statuses',
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'title' => 'Hide regular statuses (i.e. non-boosts, replies, etc.)',
|
|
|
|
],
|
2019-06-21 18:30:34 +03:00
|
|
|
'norep' => [
|
|
|
|
'name' => 'Without replies',
|
|
|
|
'type' => 'checkbox',
|
2022-11-16 20:05:01 +03:00
|
|
|
'title' => 'Hide replies, as determined by relations (not mentions).'
|
2019-06-21 18:30:34 +03:00
|
|
|
],
|
|
|
|
'noboost' => [
|
|
|
|
'name' => 'Without boosts',
|
|
|
|
'type' => 'checkbox',
|
2022-11-16 20:05:01 +03:00
|
|
|
'title' => 'Hide boosts. This will reduce loading time as RSS-Bridge fetches the boosted status from other federated instances.'
|
2022-09-05 06:58:18 +03:00
|
|
|
],
|
2022-10-12 22:43:09 +03:00
|
|
|
'signaturetype' => [
|
|
|
|
'type' => 'list',
|
|
|
|
'name' => 'Signature Type',
|
2022-11-16 20:05:01 +03:00
|
|
|
'title' => 'How to sign requests when fetching from instances.
|
|
|
|
Defaults to "nosig" for RSS-Bridge instances that did not set up signatures.',
|
2022-10-12 22:43:09 +03:00
|
|
|
'values' => [
|
|
|
|
'Without Query (Mastodon)' => 'noquery',
|
|
|
|
'With Query (GoToSocial)' => 'query',
|
2022-11-16 20:05:01 +03:00
|
|
|
'Don\'t sign' => 'nosig',
|
2022-10-12 22:43:09 +03:00
|
|
|
],
|
|
|
|
'defaultValue' => 'noquery'
|
|
|
|
],
|
2022-09-05 06:58:18 +03:00
|
|
|
]];
|
2019-06-21 18:30:34 +03:00
|
|
|
|
2022-06-21 02:11:46 +03:00
|
|
|
public function collectData()
|
|
|
|
{
|
2023-08-20 05:37:21 +03:00
|
|
|
if ($this->getInput('norep') && $this->getInput('noboost') && $this->getInput('noregular')) {
|
|
|
|
throw new \Exception('replies, boosts, or regular statuses must be allowed');
|
|
|
|
}
|
|
|
|
|
2022-11-16 20:05:01 +03:00
|
|
|
$user = $this->fetchAP($this->getURI());
|
2023-02-15 23:42:05 +03:00
|
|
|
if (!isset($user['outbox'])) {
|
|
|
|
throw new \Exception('Unable to find the outbox');
|
|
|
|
}
|
2022-11-16 20:05:01 +03:00
|
|
|
$content = $this->fetchAP($user['outbox']);
|
|
|
|
if (is_array($content['first'])) { // mobilizon
|
|
|
|
$content = $content['first'];
|
|
|
|
} else {
|
|
|
|
$content = $this->fetchAP($content['first']);
|
2019-06-21 18:30:34 +03:00
|
|
|
}
|
2022-11-16 20:05:01 +03:00
|
|
|
$items = $content['orderedItems'] ?? $content['items'];
|
|
|
|
foreach ($items as $status) {
|
2023-10-12 23:14:04 +03:00
|
|
|
$item = $this->parseStatus($status);
|
2022-09-05 06:58:18 +03:00
|
|
|
if ($item) {
|
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
|
|
|
}
|
2022-06-21 02:11:46 +03:00
|
|
|
}
|
2019-06-21 18:30:34 +03:00
|
|
|
|
2023-10-12 23:14:04 +03:00
|
|
|
protected function parseStatus($content)
|
2022-06-21 02:11:46 +03:00
|
|
|
{
|
|
|
|
$item = [];
|
|
|
|
switch ($content['type']) {
|
|
|
|
case 'Announce': // boost
|
|
|
|
if ($this->getInput('noboost')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// We fetch the boosted content.
|
|
|
|
try {
|
|
|
|
$rtContent = $this->fetchAP($content['object']);
|
2023-09-11 00:35:40 +03:00
|
|
|
if (!$rtContent) {
|
|
|
|
// Sometimes fetchAP returns null. Someone should figure out why. json_decode failure?
|
|
|
|
break;
|
|
|
|
}
|
2023-09-10 22:50:15 +03:00
|
|
|
$rtUser = $this->loadCacheValue($rtContent['attributedTo']);
|
2024-08-08 18:47:04 +03:00
|
|
|
if (!$rtUser) {
|
2022-06-21 02:11:46 +03:00
|
|
|
// We fetch the author, since we cannot always assume the format of the URL.
|
|
|
|
$user = $this->fetchAP($rtContent['attributedTo']);
|
|
|
|
preg_match('/https?:\/\/([a-z0-9-\.]{0,})\//', $rtContent['attributedTo'], $matches);
|
|
|
|
// We assume that the server name as indicated by the path is the actual server name,
|
|
|
|
// since using webfinger to delegate domains is not officially supported, and it only
|
|
|
|
// seems to work in one way.
|
|
|
|
$rtUser = '@' . $user['preferredUsername'] . '@' . $matches[1];
|
|
|
|
$this->saveCacheValue($rtContent['attributedTo'], $rtUser);
|
|
|
|
}
|
|
|
|
$item['author'] = $rtUser;
|
|
|
|
$item['title'] = 'Shared a status by ' . $rtUser . ': ';
|
|
|
|
$item = $this->parseObject($rtContent, $item);
|
2022-09-05 06:58:18 +03:00
|
|
|
} catch (HttpException $e) {
|
2022-06-21 02:11:46 +03:00
|
|
|
$item['title'] = 'Shared an unreachable status: ' . $content['object'];
|
|
|
|
$item['content'] = $content['object'];
|
|
|
|
$item['uri'] = $content['object'];
|
|
|
|
}
|
|
|
|
break;
|
2022-11-16 20:05:01 +03:00
|
|
|
case 'Note': // frendica posts
|
|
|
|
if ($this->getInput('norep') && isset($content['inReplyTo'])) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-08-20 05:37:21 +03:00
|
|
|
if ($this->getInput('noregular') && !isset($content['inReplyTo'])) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-11-16 20:05:01 +03:00
|
|
|
$item['title'] = '';
|
|
|
|
$item['author'] = $this->getInput('canusername');
|
|
|
|
$item = $this->parseObject($content, $item);
|
|
|
|
break;
|
2022-06-21 02:11:46 +03:00
|
|
|
case 'Create': // posts
|
|
|
|
if ($this->getInput('norep') && isset($content['object']['inReplyTo'])) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-08-20 05:37:21 +03:00
|
|
|
if ($this->getInput('noregular') && !isset($content['object']['inReplyTo'])) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-21 02:11:46 +03:00
|
|
|
$item['title'] = '';
|
2022-11-16 20:05:01 +03:00
|
|
|
$item['author'] = $this->getInput('canusername');
|
2022-06-21 02:11:46 +03:00
|
|
|
$item = $this->parseObject($content['object'], $item);
|
2022-11-16 20:05:01 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return null;
|
2019-06-21 18:30:34 +03:00
|
|
|
}
|
2022-11-16 20:05:01 +03:00
|
|
|
$item['timestamp'] = $content['published'] ?? $item['timestamp'];
|
2022-06-21 02:11:46 +03:00
|
|
|
$item['uid'] = $content['id'];
|
2019-06-21 18:30:34 +03:00
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
2022-06-21 02:11:46 +03:00
|
|
|
protected function parseObject($object, $item)
|
|
|
|
{
|
2022-10-12 22:43:09 +03:00
|
|
|
// If object is a link to another object, fetch it
|
|
|
|
if (is_string($object)) {
|
|
|
|
$object = $this->fetchAP($object);
|
|
|
|
}
|
|
|
|
|
2023-09-26 01:27:45 +03:00
|
|
|
$item['content'] = $object['content'] ?? '';
|
|
|
|
$strippedContent = strip_tags(str_replace('<br>', ' ', $item['content']));
|
2019-06-21 18:30:34 +03:00
|
|
|
|
2022-11-16 20:05:01 +03:00
|
|
|
if (isset($object['name'])) {
|
|
|
|
$item['title'] = $object['name'];
|
2023-08-20 05:37:21 +03:00
|
|
|
} elseif (mb_strlen($strippedContent) > 75) {
|
2022-06-21 02:11:46 +03:00
|
|
|
$contentSubstring = mb_substr($strippedContent, 0, mb_strpos(wordwrap($strippedContent, 75), "\n"));
|
|
|
|
$item['title'] .= $contentSubstring . '...';
|
|
|
|
} else {
|
|
|
|
$item['title'] .= $strippedContent;
|
|
|
|
}
|
|
|
|
$item['uri'] = $object['id'];
|
2022-11-16 20:05:01 +03:00
|
|
|
$item['timestamp'] = $object['published'];
|
|
|
|
|
|
|
|
if (!isset($object['attachment'])) {
|
|
|
|
return $item;
|
|
|
|
}
|
2022-10-12 22:43:09 +03:00
|
|
|
|
|
|
|
if (isset($object['attachment']['url'])) {
|
|
|
|
// Normalize attachment (turn single attachment into array)
|
|
|
|
$object['attachment'] = [$object['attachment']];
|
|
|
|
}
|
|
|
|
|
2022-06-21 02:11:46 +03:00
|
|
|
foreach ($object['attachment'] as $attachment) {
|
|
|
|
// Only process REMOTE pictures (prevent xss)
|
2023-09-26 01:27:45 +03:00
|
|
|
$mediaType = $attachment['mediaType'] ?? null;
|
2022-06-21 02:11:46 +03:00
|
|
|
if (
|
2023-09-26 01:27:45 +03:00
|
|
|
$mediaType
|
|
|
|
&& preg_match('/^image\//', $mediaType, $match)
|
2022-06-21 02:11:46 +03:00
|
|
|
&& preg_match('/^http(s|):\/\//', $attachment['url'], $match)
|
|
|
|
) {
|
|
|
|
$item['content'] = $item['content'] . '<br /><img ';
|
2023-10-13 21:48:08 +03:00
|
|
|
if (isset($attachment['name'])) {
|
2022-06-21 02:11:46 +03:00
|
|
|
$item['content'] .= sprintf('alt="%s" ', $attachment['name']);
|
|
|
|
}
|
|
|
|
$item['content'] .= sprintf('src="%s" />', $attachment['url']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $item;
|
2019-06-21 18:30:34 +03:00
|
|
|
}
|
|
|
|
|
2022-09-05 06:58:18 +03:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
if ($this->getInput('canusername')) {
|
|
|
|
return $this->getInput('canusername');
|
|
|
|
}
|
|
|
|
return parent::getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getInstance()
|
|
|
|
{
|
|
|
|
preg_match('/^@[a-zA-Z0-9_]+@(.+)/', $this->getInput('canusername'), $matches);
|
|
|
|
return $matches[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getUsername()
|
|
|
|
{
|
|
|
|
preg_match('/^@([a-zA-Z_0-9_]+)@.+/', $this->getInput('canusername'), $matches);
|
|
|
|
return $matches[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getURI()
|
|
|
|
{
|
|
|
|
if ($this->getInput('canusername')) {
|
|
|
|
// We parse webfinger to make sure the URL is correct. This is mostly because
|
|
|
|
// MissKey uses user ID instead of the username in the endpoint, domain delegations,
|
|
|
|
// and also to be compatible with future ActivityPub implementations.
|
|
|
|
$resource = 'acct:' . $this->getUsername() . '@' . $this->getInstance();
|
|
|
|
$webfingerUrl = 'https://' . $this->getInstance() . '/.well-known/webfinger?resource=' . $resource;
|
|
|
|
$webfingerHeader = [
|
2022-09-10 08:38:09 +03:00
|
|
|
'Accept: application/jrd+json'
|
2022-09-05 06:58:18 +03:00
|
|
|
];
|
|
|
|
$webfinger = json_decode(getContents($webfingerUrl, $webfingerHeader), true);
|
|
|
|
foreach ($webfinger['links'] as $link) {
|
|
|
|
if ($link['type'] === 'application/activity+json') {
|
|
|
|
return $link['href'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getURI();
|
|
|
|
}
|
|
|
|
|
2022-06-21 02:11:46 +03:00
|
|
|
protected function fetchAP($url)
|
|
|
|
{
|
|
|
|
$d = new DateTime();
|
|
|
|
$d->setTimezone(new DateTimeZone('GMT'));
|
|
|
|
$date = $d->format('D, d M Y H:i:s e');
|
2022-10-12 22:43:09 +03:00
|
|
|
|
|
|
|
// GoToSocial expects the query string to be included when
|
|
|
|
// building the url to sign
|
|
|
|
// @see https://github.com/superseriousbusiness/gotosocial/issues/107#issuecomment-1188289857
|
|
|
|
$regex = [
|
|
|
|
// Include query string when parsing URL
|
|
|
|
'query' => '/https?:\/\/([a-z0-9-\.]{0,})(\/[^#]+)/',
|
|
|
|
|
|
|
|
// Exclude query string when parsing URL
|
|
|
|
'noquery' => '/https?:\/\/([a-z0-9-\.]{0,})(\/[^#?]+)/',
|
2022-11-16 20:05:01 +03:00
|
|
|
'nosig' => '/https?:\/\/([a-z0-9-\.]{0,})(\/[^#?]+)/',
|
2022-10-12 22:43:09 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
preg_match($regex[$this->getInput('signaturetype')], $url, $matches);
|
2022-06-21 02:11:46 +03:00
|
|
|
$headers = [
|
|
|
|
'Accept: application/activity+json',
|
|
|
|
'Host: ' . $matches[1],
|
|
|
|
'Date: ' . $date
|
|
|
|
];
|
|
|
|
$privateKey = $this->getOption('private_key');
|
|
|
|
$keyId = $this->getOption('key_id');
|
2022-11-16 20:05:01 +03:00
|
|
|
if ($privateKey && $keyId && $this->getInput('signaturetype') !== 'nosig') {
|
2022-06-21 02:11:46 +03:00
|
|
|
$pkey = openssl_pkey_get_private('file://' . $privateKey);
|
|
|
|
$toSign = '(request-target): get ' . $matches[2] . "\nhost: " . $matches[1] . "\ndate: " . $date;
|
|
|
|
$result = openssl_sign($toSign, $signature, $pkey, 'RSA-SHA256');
|
|
|
|
if ($result) {
|
2024-08-08 05:31:47 +03:00
|
|
|
$sig = sprintf(
|
|
|
|
'Signature: keyId="%s",headers="(request-target) host date",signature="%s"',
|
|
|
|
$keyId,
|
|
|
|
base64_encode($signature)
|
|
|
|
);
|
|
|
|
|
|
|
|
$headers[] = $sig;
|
2022-06-21 02:11:46 +03:00
|
|
|
}
|
|
|
|
}
|
2023-09-11 00:35:40 +03:00
|
|
|
try {
|
|
|
|
return Json::decode(getContents($url, $headers));
|
|
|
|
} catch (\JsonException $e) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-06-21 18:30:34 +03:00
|
|
|
}
|
|
|
|
}
|