initial mastodon support and minor improvements

- reworked plugin handling. Main now registers and keeps all connectors and based on request we select the correct one and pass it (mainly for clean async-request-handling)
- added outbox functionality for mastodon
- changed image-mime-type approach to retrieve mime-type from the url, this way we don't need to store the images on our server/download each image
- added host for connector for better debugging
- minor bug-fixes
This commit is contained in:
Yannis Vogel 2025-04-08 22:03:19 +02:00
parent a21345c3c7
commit 530caa7ea6
No known key found for this signature in database
25 changed files with 614 additions and 102 deletions

1
.gitignore vendored
View file

@ -5,3 +5,4 @@ php-docs
.phpdoc
phpdoc
html
/cache

View file

@ -14,6 +14,8 @@ compiledir = '../cache'
[plugins]
rediscache = 'rediscache.php'
dummy = 'dummyconnector.php'
contentnation = 'contentnation.php'
mastodon = 'mastodon.php'
[maintenance]
username = 'federatoradmin'

6
contentnation.ini Normal file
View file

@ -0,0 +1,6 @@
[contentnation]
service-uri = https://contentnation.net
[userdata]
path = '/home/net/contentnation/userdata/htdocs/' // need to download local copy of image and put img-path here
url = 'https://userdata.contentnation.net'

112
htdocs/index.html Normal file
View file

@ -0,0 +1,112 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>API Request UI</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="flex justify-center items-center min-h-screen bg-gray-100">
<div class="w-full max-w-3xl bg-white shadow-lg rounded-lg p-6 m-6">
<h2 class="text-2xl font-semibold mb-4 text-center">API Request UI</h2>
<div id="request-container">
<!-- Request Form Template -->
<div class="request-box border p-4 rounded-lg mb-4 bg-gray-50">
<label class="block font-medium">API target link</label>
<input type="text" class="target-link-input w-full p-2 border rounded-md mb-2"
placeholder="Enter target link" value="federator/fedusers/grumpydevelop/outbox?page=0">
<label class="block font-medium">Request type</label>
<input type="text" class="request-type-input w-full p-2 border rounded-md mb-2"
placeholder="POST or GET" value="GET">
<label class="block font-medium">X-Session:</label>
<input type="text" class="session-input w-full p-2 border rounded-md mb-2"
placeholder="Enter X-Session token" value="">
<label class="block font-medium">X-Profile:</label>
<input type="text" class="profile-input w-full p-2 border rounded-md mb-2" placeholder="Enter X-Profile"
value="">
<div class="buttonContainer">
<button class="send-btn bg-blue-500 text-white px-4 py-2 rounded-md w-full hover:bg-blue-600">
Send Request
</button>
</div>
<p class="mt-2 text-sm text-gray-700">Response:</p>
<p class="response mt-2 text-sm text-gray-700 font-mono whitespace-pre">Waiting for response</p>
</div>
</div>
<button id="add-request" class="mt-4 bg-green-500 text-white px-4 py-2 rounded-md w-full hover:bg-green-600">
Add Another Request
</button>
</div>
<script>
function sendRequest(button) {
const container = button.parentElement.parentElement;
const targetLink = container.querySelector(".target-link-input").value;
const requestType = container.querySelector(".request-type-input").value;
const session = container.querySelector(".session-input").value;
const profile = container.querySelector(".profile-input").value;
const responseField = container.querySelector(".response");
button.parentElement.style.cursor = "not-allowed";
button.style.pointerEvents = "none";
button.textContent = "Sending...";
responseField.textContent = "Waiting for response";
const headers = {
...(session ? { "X-Session": session } : {}),
...(profile ? { "X-Profile": profile } : {})
};
fetch("http://localhost/api/" + targetLink, {
method: requestType,
headers
})
.then(response => response.text())
.then(data => {
responseField.textContent = data;
button.parentElement.style.cursor = "";
button.style.pointerEvents = "";
button.textContent = "Send Request";
})
.catch(error => {
responseField.textContent = "Error: " + error;
button.parentElement.style.cursor = "";
button.style.pointerEvents = "";
button.textContent = "Send Request";
});
}
document.querySelectorAll(".send-btn").forEach(btn => {
btn.addEventListener("click", function () {
sendRequest(this);
});
});
document.getElementById("add-request").addEventListener("click", function () {
const container = document.getElementById("request-container");
const requestBox = container.firstElementChild.cloneNode(true);
requestBox.querySelector(".target-link-input").value = "federator/v1/dummy/moo";
requestBox.querySelector(".request-type-input").value = "GET";
requestBox.querySelector(".session-input").value = "somethingvalider";
requestBox.querySelector(".profile-input").value = "ihaveone";
requestBox.querySelector(".response").textContent = "";
requestBox.querySelector(".send-btn").addEventListener("click", function () {
sendRequest(this);
});
container.appendChild(requestBox);
});
</script>
</body>
</html>

3
htdocs/info.php Normal file
View file

@ -0,0 +1,3 @@
<?php
echo phpinfo();

6
mastodon.ini Normal file
View file

@ -0,0 +1,6 @@
[mastodon]
service-uri = https://mastodon.social
[userdata]
path = '/home/net/contentnation/userdata/htdocs/' // need to download local copy of image and put img-path here
url = 'https://files.mastodon.net'

View file

@ -74,9 +74,21 @@ class Api extends Main
$this->setPath((string) $_REQUEST['_call']);
$this->openDatabase();
$this->loadPlugins();
$host = 'dummy'; // fallback
// Check if path matches something like fedusers/username@domain.tld
if (preg_match("#^fedusers/([^@]+)@([^/]+)$#", $this->path, $matches) === 1) {
$host = strtolower($matches[2]); // extract domain
} else {
$host = 'dummy';
echo "using dummy host in API::run\n";
}
$connector = $this->getConnectorForHost($host);
$retval = "";
$handler = null;
if ($this->connector === null) {
if ($connector === null) {
http_response_code(500);
return;
}
@ -85,7 +97,7 @@ class Api extends Main
$this->dbh,
$_SERVER['HTTP_X_SESSION'],
$_SERVER['HTTP_X_PROFILE'],
$this->connector,
$connector,
$this->cache
);
if ($this->user === false) {
@ -112,7 +124,7 @@ class Api extends Main
$printresponse = true;
if ($handler !== null) {
try {
$printresponse = $handler->exec($this->paths, $this->user);
$printresponse = $handler->exec($this->paths, $this->user, $connector);
if ($printresponse) {
$retval = $handler->toJson();
}

View file

@ -19,9 +19,10 @@ interface APIInterface
* @param array<string> $paths path array split by /
*
* @param \Federator\Data\User|false $user user who is calling us
* @param \Federator\Connector\Connector $connector connector to use
* @return bool true on success
*/
public function exec($paths, $user);
public function exec($paths, $user, $connector);
/**
* get internal represenation as json string

View file

@ -43,9 +43,10 @@ class FedUsers implements APIInterface
*
* @param array<string> $paths path array split by /
* @param \Federator\Data\User|false $user user who is calling us @unused-param
* @param \Federator\Connector\Connector $connector connector to use
* @return bool true on success
*/
public function exec($paths, $user)
public function exec($paths, $user, $connector)
{
$method = $_SERVER["REQUEST_METHOD"];
$handler = null;
@ -53,7 +54,7 @@ class FedUsers implements APIInterface
case 2:
if ($method === 'GET') {
// /users/username or /@username
return $this->returnUserProfile($paths[1]);
return $this->returnUserProfile($paths[1], $connector);
}
break;
case 3:
@ -82,10 +83,10 @@ class FedUsers implements APIInterface
$ret = false;
switch ($method) {
case 'GET':
$ret = $handler->get($paths[1]);
$ret = $handler->get($paths[1], $connector);
break;
case 'POST':
$ret = $handler->post($paths[1]);
$ret = $handler->post($paths[1], $connector);
break;
}
if ($ret !== false) {
@ -102,14 +103,15 @@ class FedUsers implements APIInterface
* return user profile
*
* @param string $_name
* @param \Federator\Connector\Connector $connector connector to use
* @return boolean true on success
*/
private function returnUserProfile($_name)
private function returnUserProfile($_name, $connector)
{
$user = \Federator\DIO\User::getUserByName(
$this->main->getDatabase(),
$_name,
$this->main->getConnector(),
$connector,
$this->main->getCache()
);
if ($user === false || $user->id === null) {
@ -123,7 +125,7 @@ class FedUsers implements APIInterface
'fqdn' => $_SERVER['SERVER_NAME'],
'name' => $user->name,
'username' => $user->id,
'publickey' => str_replace("\n", "\\n", $user->publicKey),
'publickey' => $user->publicKey,
'registered' => gmdate('Y-m-d\TH:i:s\Z', $user->registered), // 2021-03-25T00:00:00Z
'summary' => $user->summary,
'type' => $user->type

View file

@ -14,15 +14,17 @@ interface FedUsersInterface
* get call for user
*
* @param string $_user user to fetch data for
* @param \Federator\Connector\Connector $connector connector to use
* @return string|false response or false in case of error
*/
public function get($_user);
public function get($_user, $connector);
/**
* post call for user
*
* @param string $_user user to add data to
* @param \Federator\Connector\Connector $connector connector to use
* @return string|false response or false in case of error
*/
public function post($_user);
public function post($_user, $connector);
}

View file

@ -33,13 +33,13 @@ class Outbox implements \Federator\Api\FedUsers\FedUsersInterface
* handle get call
*
* @param string $_user user to fetch outbox for
* @param \Federator\Connector\Connector $connector connector to use
* @return string|false response
*/
public function get($_user)
public function get($_user, $connector)
{
$dbh = $this->main->getDatabase();
$cache = $this->main->getCache();
$connector = $this->main->getConnector();
// get user
$user = \Federator\DIO\User::getUserByName(
$dbh,
@ -50,6 +50,7 @@ class Outbox implements \Federator\Api\FedUsers\FedUsersInterface
if ($user->id === null) {
return false;
}
// get posts from user
$outbox = new \Federator\Data\ActivityPub\Common\Outbox();
$min = $this->main->extractFromURI("min", "");
@ -79,16 +80,17 @@ class Outbox implements \Federator\Api\FedUsers\FedUsersInterface
$outbox->setPrev($id . '&min=' . $oldestId);
}
$obj = $outbox->toObject();
return json_encode($obj);
return json_encode($obj, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
}
/**
* handle post call
*
* @param string $_user user to add data to outbox @unused-param
* @param \Federator\Connector\Connector $connector connector to use
* @return string|false response
*/
public function post($_user)
public function post($_user, $connector)
{
return false;
}

View file

@ -23,7 +23,7 @@ class Dummy implements \Federator\Api\APIInterface
/**
* internal message to output
*
* @var Array<string, mixed> $message
* @var array<string, mixed> $message
*/
private $message = [];
@ -42,9 +42,10 @@ class Dummy implements \Federator\Api\APIInterface
*
* @param array<string> $paths path array split by /
* @param \Federator\Data\User|false $user user who is calling us
* @param \Federator\Connector\Connector $connector connector to use
* @return bool true on success
*/
public function exec($paths, $user) : bool
public function exec($paths, $user, $connector) : bool
{
// only for user with the 'publish' permission
if ($user === false || $user->hasPermission('publish') === false) {

View file

@ -57,7 +57,7 @@ class WellKnown implements APIInterface
* @param \Federator\Data\User|false $user user who is calling us @unused-param
* @return bool true on success
*/
public function exec($paths, $user)
public function exec($paths, $user, $connector)
{
$method = $_SERVER["REQUEST_METHOD"];
switch ($method) {
@ -66,14 +66,14 @@ class WellKnown implements APIInterface
case 2:
if ($paths[0] === 'nodeinfo') {
$ni = new WellKnown\NodeInfo($this, $this->main);
return $ni->exec($paths);
return $ni->exec($paths, $connector);
}
switch ($paths[1]) {
case 'host-meta':
return $this->hostMeta();
case 'nodeinfo':
$ni = new WellKnown\NodeInfo($this, $this->main);
return $ni->exec($paths);
return $ni->exec($paths, $connector);
case 'webfinger':
$wf = new WellKnown\WebFinger($this, $this->main);
return $wf->exec();

View file

@ -41,9 +41,10 @@ class NodeInfo
* handle nodeinfo request
*
* @param string[] $paths path of us
* @param \Federator\Connector\Connector $connector connector to use
* @return bool true on success
*/
public function exec($paths)
public function exec($paths, $connector)
{
$data = [
'fqdn' => $_SERVER['SERVER_NAME']
@ -64,7 +65,7 @@ class NodeInfo
default:
$template = 'nodeinfo2.0.json';
}
$stats = \Federator\DIO\Stats::getStats($this->main);
$stats = \Federator\DIO\Stats::getStats($this->main, $connector);
echo "fetch usercount via connector\n";
$data['usercount'] = $stats->userCount;
$data['postcount'] = $stats->postCount;

View file

@ -46,18 +46,18 @@ class WebFinger
{
$_resource = $this->main->extractFromURI('resource');
$matches = [];
$config = $this->main->getConfig();
$domain = $config['generic']['externaldomain'];
if (preg_match("/^acct:([^@]+)@(.*)$/", $_resource, $matches) != 1 || $matches[2] !== $domain) {
if (preg_match("/^acct:([^@]+)@(.*)$/", $_resource, $matches) != 1) {
throw new \Federator\Exceptions\InvalidArgument();
}
$domain = $matches[2];
$user = \Federator\DIO\User::getUserByName(
$this->main->getDatabase(),
$matches[1],
$this->main->getConnector(),
$this->main->getConnectorForHost($domain),
$this->main->getCache()
);
if ($user->id == 0) {
echo "not found";
throw new \Federator\Exceptions\FileNotFound();
}
$data = [

View file

@ -13,6 +13,12 @@ namespace Federator\Connector;
*/
interface Connector
{
/**
* get the host this connector is dedicated to
*
* @return string
*/
public function getHost();
/**
* get posts by given user
*

View file

@ -773,7 +773,7 @@ class APObject implements \JsonSerializable
* {@inheritDoc}
* @see JsonSerializable::jsonSerialize()
*/
public function jsonSerialize()
public function jsonSerialize(): mixed
{
return $this->toObject();
}

View file

@ -145,7 +145,7 @@ class User
*/
public function hasPermission(string $p)
{
return in_array($p, $this->permissions, false);
return in_array(strtolower($p), array_map('strtolower', $this->permissions), true);
}
/**

View file

@ -17,11 +17,11 @@ class Stats
/**
* get remote stats
*
* @param \Federator\Main $main
* main instance
* @param \Federator\Main $main main instance
* @param \Federator\Connector\Connector $connector connector to use
* @return \Federator\Data\Stats
*/
public static function getStats($main)
public static function getStats($main, $connector)
{
$cache = $main->getCache();
// ask cache
@ -31,7 +31,6 @@ class Stats
return $stats;
}
}
$connector = $main->getConnector();
// ask connector for stats
$stats = $connector->getRemoteStats();
if ($cache !== null && $stats !== false) {

View file

@ -145,6 +145,7 @@ class User
public static function getUserByName($dbh, $_name, $connector, $cache)
{
$user = false;
// ask cache
if ($cache !== null) {
$user = $cache->getRemoteUserByName($_name);
@ -179,6 +180,7 @@ class User
$stmt->fetch();
}
$stmt->close();
if ($user->id === null) {
// ask connector for user-id
$ruser = $connector->getRemoteUserByName($_name);
@ -190,7 +192,7 @@ class User
if ($user->id === null && $user->externalid !== null) {
self::addLocalUser($dbh, $user, $_name);
}
$cache->saveRemoteUserByName($_name, $user);
$cache->saveRemoteUserByName($_name, user: $user);
}
return $user;
}

View file

@ -28,11 +28,11 @@ class Main
*/
protected $config;
/**
* remote connector
* remote connectors
*
* @var Connector\Connector $connector
* @var array<string, Connector\Connector> $connectors
*/
protected $connector = null;
protected $connectors = [];
/**
* response content type
*
@ -141,18 +141,20 @@ class Main
}
/**
* get connector
* Get the connector for a given remote host
*
* @return \Federator\Connector\Connector
* @param string $remoteHost The host from the actor URL (e.g. mastodon.social)
* @return Connector\Connector|null
*/
public function getConnector()
public function getConnectorForHost(string $remoteHost): ?Connector\Connector
{
return $this->connector;
$host = strtolower(parse_url($remoteHost, PHP_URL_HOST) ?? $remoteHost);
return $this->connectors[$host] ?? null;
}
/**
* get config
* @return Array<String, Mixed>
* @return array<string, mixed>
*/
public function getConfig()
{
@ -239,11 +241,16 @@ class Main
}
/**
* set connector
* Set a connector for a specific remote host
*
* @param string $remoteURL The remote host (like mastodon.social or contentnation.net)
* @param Connector\Connector $connector The connector instance
*/
public function setConnector(Connector\Connector $connector) : void
public function addConnector(string $remoteURL, Connector\Connector $connector): void
{
$this->connector = $connector;
// Normalize the host (no scheme, lowercase)
$host = strtolower(parse_url($remoteURL, PHP_URL_HOST) ?? $remoteURL);
$this->connectors[$host] = $connector;
}
/**

View file

@ -49,6 +49,15 @@ class ContentNation implements Connector
$this->main = $main;
}
/**
* get the host this connector is dedicated to
*
* @return string
*/
public function getHost() {
return "contentnation.net";
}
/**
* get posts by given user
*
@ -124,18 +133,21 @@ class ContentNation implements Connector
$apArticle->setID($idurl)
->setURL($idurl);
$image = $activity['image'] ?? $activity['profileimg'];
$mediaType = @mime_content_type($imgpath . $activity['profile'] . '/' . $image) | 'text/plain';
// $mediaType = @mime_content_type($imgpath . $activity['profile'] . '/' . $image) | 'text/plain'; // old approach, using local copy of images
$imgUrl = $userdata . '/' . $activity['profile'] . $image;
$mediaType = $this->getRemoteMimeType($imgUrl) ?? 'text/plain';
$img = new \Federator\Data\ActivityPub\Common\Image();
$img->setMediaType($mediaType)
->setName($articleimage)
->setURL($userdata . '/' . $activity['profile'] . $image);
$apArticle->addImage($img);
$create->setObject($apArticle);
$create->setObject(object: $apArticle);
$posts[] = $create;
break; // Article
case 'Comment':
// echo "comment\n";
// print_r($activity);
$comment = new \Federator\Data\ActivityPub\Common\Activity('Comment');
$create->setObject($comment);
$posts[] = $create;
break; // Comment
case 'Vote':
$url = 'https://' . $host . '/' . $activity['articlelang'] . $userId . '/'
@ -189,6 +201,18 @@ class ContentNation implements Connector
return $posts;
}
/**
* Get the MIME type of a remote file by its URL.
*
* @param string $_url The URL of the remote file.
* @return string|false The MIME type if found, or false on failure.
*/
public function getRemoteMimeType($url)
{
$headers = get_headers($url, 1);
return $headers['Content-Type'] ?? 'unknown';
}
/**
* get statistics from remote system
*
@ -220,6 +244,9 @@ class ContentNation implements Connector
*/
public function getRemoteUserByName(string $_name)
{
if (preg_match("#^([^@]+)@([^/]+)$#", $_name, $matches) === 1) {
$_name = $matches[1];
}
// validate name
if (preg_match("/^[a-zA-Z0-9_\-]+$/", $_name) != 1) {
return false;
@ -299,5 +326,5 @@ namespace Federator;
function contentnation_load($main)
{
$cn = new Connector\ContentNation($main);
$main->setConnector($cn);
$main->addConnector($cn->getHost(), $cn);
}

View file

@ -19,6 +19,15 @@ class DummyConnector implements Connector
{
}
/**
* get the host this connector is dedicated to
*
* @return string
*/
public function getHost() {
return "dummy";
}
/**
* get posts by given user
*
@ -87,5 +96,5 @@ namespace Federator;
function dummy_load($main)
{
$dummy = new Connector\DummyConnector();
$main->setConnector($dummy);
$main->addConnector($dummy->getHost(), $dummy);
}

View file

@ -0,0 +1,302 @@
<?php
/**
* SPDX-FileCopyrightText: 2024 Sascha Nitsch (grumpydeveloper) https://contentnation.net/@grumpydevelop
* SPDX-License-Identifier: GPL-3.0-or-later
*
* @author Sascha Nitsch (grumpydeveloper)
**/
namespace Federator\Connector;
/**
* Connector to Mastodon.social
*/
class Mastodon implements Connector
{
/**
* config parameter
*
* @var array<string, mixed> $config
*/
private $config;
/**
* main instance
*
* @var \Federator\Main $main
*/
private $main;
/**
* service-URL
*
* @var string $service
*/
private $service;
/**
* constructor
*
* @param \Federator\Main $main
*/
public function __construct($main)
{
$config = parse_ini_file($_SERVER['DOCUMENT_ROOT'] . '../mastodon.ini', true);
if ($config !== false) {
$this->config = $config;
}
$this->service = $config['mastodon']['service-uri'];
$this->main = $main;
}
/**
* get the host this connector is dedicated to
*
* @return string
*/
public function getHost() {
return "mastodon.social";
}
/**
* get posts by given user
*
* @param string $userId user id
* @param string $min min date
* @param string $max max date
* @return \Federator\Data\ActivityPub\Common\APObject[]|false
*/
public function getRemotePostsByUser($userId, $min = null, $max = null)
{
$remoteURL = $this->service . '/users/' . $userId . '/outbox';
$items = [];
do {
if ($min !== '') {
$remoteURL .= '&minTS=' . urlencode($min);
}
if ($max !== '') {
$remoteURL .= '&maxTS=' . urlencode($max);
}
// Fetch the current page of items (first or subsequent pages)
[$outboxResponse, $outboxInfo] = \Federator\Main::getFromRemote($remoteURL, ['Accept: application/activity+json']);
if ($outboxInfo['http_code'] !== 200) {
echo "aborting";
return false;
}
$outbox = json_decode($outboxResponse, true);
// Extract orderedItems from the current page
if (isset($outbox['orderedItems'])) {
$items = array_merge($items, $outbox['orderedItems']);
}
// Step 4: Use 'last' URL to determine pagination
if (isset($outbox['last'])) {
// The 'last' URL will usually have a query string that includes min_id for the next set of results
$remoteURL = $outbox['last']; // Update to the last URL for the next page of items
} else {
break; // No more pages, exit pagination
}
} while (!empty($outbox['last'])); // Continue fetching until no 'last' URL
$items = [];
// Follow `first` page (or get orderedItems directly)
if (isset($outbox['orderedItems'])) {
$items = $outbox['orderedItems'];
} elseif (isset($outbox['first'])) {
$firstURL = is_array($outbox['first']) ? $outbox['first']['id'] : $outbox['first'];
[$pageResponse, $pageInfo] = \Federator\Main::getFromRemote($firstURL, ['Accept: application/activity+json']);
if ($pageInfo['http_code'] !== 200) {
return false;
}
$page = json_decode($pageResponse, true);
$items = $page['orderedItems'] ?? [];
}
// Convert to internal representation
$posts = [];
foreach ($items as $activity) {
if (!isset($activity['type']) || $activity['type'] !== 'Create' || !isset($activity['object'])) {
continue; // Skip non-Create activities
}
$obj = $activity['object'];
$create = new \Federator\Data\ActivityPub\Common\Create();
$create->setID($activity['id'])
->setPublished(strtotime($activity['published'] ?? $obj['published'] ?? 'now'))
->setAActor($activity['actor'])
->addTo("https://www.w3.org/ns/activitystreams#Public");
// Handle main Note content
if ($obj['type'] === 'Note') {
$apNote = new \Federator\Data\ActivityPub\Common\Note();
$apNote->setID($obj['id'])
->setPublished(strtotime($obj['published'] ?? 'now'))
->setContent($obj['content'] ?? '')
->setAttributedTo($obj['attributedTo'] ?? $activity['actor'])
->addTo("https://www.w3.org/ns/activitystreams#Public");
// Handle attachments
if (!empty($obj['attachment']) && is_array($obj['attachment'])) {
foreach ($obj['attachment'] as $media) {
if (!isset($media['type'], $media['url']))
continue;
$mediaObj = new \Federator\Data\ActivityPub\Common\APObject($media['type']);
$mediaObj->setURL($media['url']);
$apNote->addAttachment($mediaObj);
}
}
$create->setObject($apNote);
}
$posts[] = $create;
}
return $posts;
}
/**
* Get the MIME type of a remote file by its URL.
*
* @param string $_url The URL of the remote file.
* @return string|false The MIME type if found, or false on failure.
*/
public function getRemoteMimeType($url)
{
$headers = get_headers($url, 1);
return $headers['Content-Type'] ?? 'unknown';
}
/**
* get statistics from remote system
*
* @return \Federator\Data\Stats|false
*/
public function getRemoteStats()
{
$remoteURL = $this->service . '/api/stats';
[$response, $info] = \Federator\Main::getFromRemote($remoteURL, []);
if ($info['http_code'] != 200) {
return false;
}
$r = json_decode($response, true);
if ($r === false || $r === null || !is_array($r)) {
return false;
}
$stats = new \Federator\Data\Stats();
$stats->userCount = array_key_exists('userCount', $r) ? $r['userCount'] : 0;
$stats->postCount = array_key_exists('pageCount', $r) ? $r['pageCount'] : 0;
$stats->commentCount = array_key_exists('commentCount', $r) ? $r['commentCount'] : 0;
return $stats;
}
/**
* get remote user by given name
*
* @param string $_name user/profile name
* @return \Federator\Data\User | false
*/
public function getRemoteUserByName(string $_name)
{
// Validate username (Mastodon usernames can include @ and domain parts)
if (preg_match("/^[a-zA-Z0-9_\-@.]+$/", $_name) !== 1) {
return false;
}
// Mastodon lookup API endpoint
$remoteURL = $this->service . '/api/v1/accounts/lookup?acct=' . urlencode($_name);
// Set headers
$headers = ['Accept: application/json'];
// Fetch data from Mastodon instance
[$response, $info] = \Federator\Main::getFromRemote($remoteURL, $headers);
// Handle HTTP errors
if ($info['http_code'] !== 200) {
return false;
}
// Decode response
$r = json_decode($response, true);
if ($r === false || $r === null || !is_array($r)) {
return false;
}
// Map response to User object
$user = new \Federator\Data\User();
$user->externalid = (string) $r['id']; // Mastodon uses numeric IDs
$user->iconMediaType = 'image/png'; // Mastodon doesn't explicitly return this, assume PNG
$user->iconURL = $r['avatar'] ?? null;
$user->imageMediaType = 'image/png';
$user->imageURL = $r['header'] ?? null;
$user->name = $r['display_name'] ?: $r['username'];
$user->summary = $r['note'];
$user->type = 'Person'; // Mastodon profiles are ActivityPub "Person" objects
$user->registered = strtotime($r['created_at']);
return $user;
}
/**
* get remote user by given session
*
* @param string $_session session id
* @param string $_user user or profile name
* @return \Federator\Data\User | false
*/
public function getRemoteUserBySession(string $_session, string $_user)
{
// validate $_session and $user
if (preg_match("/^[a-z0-9]{16}$/", $_session) != 1) {
return false;
}
if (preg_match("/^[a-zA-Z0-9_\-]+$/", $_user) != 1) {
return false;
}
$remoteURL = $this->service . '/api/users/permissions?profile=' . urlencode($_user);
$headers = ['Cookie: session=' . $_session, 'Accept: application/json'];
[$response, $info] = \Federator\Main::getFromRemote($remoteURL, $headers);
if ($info['http_code'] != 200) {
return false;
}
$r = json_decode($response, true);
if ($r === false || !is_array($r) || !array_key_exists($_user, $r)) {
return false;
}
$user = $this->getRemoteUserByName($_user);
if ($user === false) {
return false;
}
// extend with permissions
$user->permissions = [];
$user->session = $_session;
foreach ($r[$_user] as $p) {
$user->permissions[] = $p;
}
return $user;
}
}
namespace Federator;
/**
* Function to initialize plugin
*
* @param \Federator\Main $main main instance
* @return void
*/
function mastodon_load($main)
{
$mast = new Connector\Mastodon($main);
$main->addConnector($mast->getHost(), $mast);
}

View file

@ -53,6 +53,15 @@ class RedisCache implements Cache
}
}
/**
* get the host this connector is dedicated to
*
* @return string
*/
public function getHost() {
return "redis";
}
/**
* connect to redis
* @return void