Skip to content

Commit

Permalink
[Fix] Actor: add missing Server injection in WebFinger discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
landrok committed Apr 5, 2024
1 parent 354b2dd commit 8939581
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 51 deletions.
25 changes: 13 additions & 12 deletions src/ActivityPhp/Server/Actor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

/**
* A server-oriented actor object
*/
*/
class Actor
{
/**
Expand All @@ -36,7 +36,7 @@ class Actor
* Construct an Actor instance based upon a WebFinger discovery if
* an handle-like is provided. Otherwise, it checks an ActivityPhp
* profile id if it's an URL.
*
*
* @param string $handle URL or a WebFinger handle
* @param \ActivityPhp\Server $server
*/
Expand All @@ -45,12 +45,13 @@ public function __construct(string $handle, Server $server)
$this->server = $server;
$url = null;

WebFinger::setServer($this->server);

// Is a valid handle?
if ($this->isHandle($handle)) {
// testing only
$scheme = $this->server->config('instance.debug')
? 'http' : 'https';
WebFinger::setServer($this->server);
$webfinger = WebFinger::get($handle, $scheme);
$url = $webfinger->getProfileId();
// Is an id?
Expand All @@ -69,7 +70,7 @@ public function __construct(string $handle, Server $server)

/**
* Check that a string is a valid handle
*
*
* @param string $handle
* @return bool
*/
Expand All @@ -94,7 +95,7 @@ private function createActor(string $url)

/**
* Get ActivityStream Actor
*
*
* @param null|string $property
* @return \ActivityPhp\Type\Extended\AbstractActor
* | string
Expand All @@ -111,28 +112,28 @@ public function get($property = null)

/**
* Get Actor's public key PEM
*
*
* @return string|null
*/
public function getPublicKeyPem()
{
if (!isset($this->actor->publicKey)
|| !is_array($this->actor->publicKey)
|| !isset($this->actor->publicKey['publicKeyPem'])
if (! isset($this->actor->publicKey)
|| ! is_array($this->actor->publicKey)
|| ! isset($this->actor->publicKey['publicKeyPem'])
) {
$this->server->logger()->info(
'Public key not found',
[$this->actor->toArray()]
);
return false;
return false;
}

return $this->actor->publicKey['publicKeyPem'];
}

/**
* Get WebFinger bound to a profile
*
*
* @return \ActivityPhp\Server\Http\WebFinger
*/
public function webfinger()
Expand All @@ -141,7 +142,7 @@ public function webfinger()
$scheme = $this->server->config('instance.debug')
? 'http' : 'https';

$port = !is_null(parse_url($this->actor->id, PHP_URL_PORT))
$port = ! is_null(parse_url($this->actor->id, PHP_URL_PORT))
? ':' . parse_url($this->actor->id, PHP_URL_PORT)
: '';

Expand Down
23 changes: 11 additions & 12 deletions src/ActivityPhp/Server/Actor/ActorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Exception;

/**
* \ActivityPhp\Server\ActorFactory provides a factory for server-side
* \ActivityPhp\Server\ActorFactory provides a factory for server-side
* actor.
*/
abstract class ActorFactory
Expand All @@ -29,7 +29,7 @@ abstract class ActorFactory

/**
* Create an actor from its profile url
*
*
* @param string $url
* @return \ActivityPhp\Type\Extended\AbstractActor
* @throws \Exception if actor does not exist
Expand All @@ -42,8 +42,7 @@ public static function create(string $url)
) {
return self::createLocalActor($url);
}



$content = json_decode(
(new Request(
self::$server->config('http.timeout'),
Expand All @@ -52,9 +51,9 @@ public static function create(string $url)
true
);

if (!is_array($content)
|| !count($content)
|| !isset($content['type'])
if (! is_array($content)
|| ! count($content)
|| ! isset($content['type'])
) {
throw new Exception('Actor fetching failed');
}
Expand All @@ -66,7 +65,7 @@ public static function create(string $url)
// ActivityPhp profile
foreach (['id', 'preferredUsername'] as $property) {
if ($actor->has($property)
&& !is_null($actor->$property)
&& ! is_null($actor->$property)
) {
continue;
}
Expand All @@ -81,7 +80,7 @@ public static function create(string $url)

/**
* Inject a server instance
*
*
* @param \ActivityPhp\Server $server
*/
public static function setServer(Server $server)
Expand All @@ -91,7 +90,7 @@ public static function setServer(Server $server)

/**
* Create an actor type from a profile id
*
*
* @param string $url
* @return string
*/
Expand All @@ -106,7 +105,7 @@ public static function createLocalActor(string $url)

/**
* Parse an actor handle from a profile id
*
*
* @param string $url
* @return string
*/
Expand All @@ -119,7 +118,7 @@ public static function extractHandle(string $url)
$pattern
);

if (!preg_match("#{$pattern}#", $url, $matches)) {
if (! preg_match("#{$pattern}#", $url, $matches)) {
throw new Exception(
sprintf(
'Failed to extract username from URL "%s", pattern="%s"',
Expand Down
43 changes: 22 additions & 21 deletions src/ActivityPhp/Server/Actor/Outbox.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the ActivityPhp package.
*
Expand All @@ -15,7 +17,6 @@
use ActivityPhp\Server\Activity\HandlerInterface;
use ActivityPhp\Server\Actor;
use ActivityPhp\Server\Helper;
use ActivityPhp\Server\Http\Request as HttpRequest;
use ActivityPhp\Type;
use ActivityPhp\Type\Core\AbstractActivity;
use ActivityPhp\Type\Util;
Expand All @@ -25,12 +26,12 @@

/**
* A server-side outbox
*/
*/
class Outbox extends AbstractBox
{
/**
* Outbox constructor
*
*
* @param \ActivityPhp\Server\Actor $actor An actor
* @param \ActivityPhp\Server $server
*/
Expand All @@ -44,14 +45,14 @@ public function __construct(Actor $actor, Server $server)

/**
* Get items from an outbox
*
*
* @param string $page
* @return array
*/
public function getPage(string $url)
{
$this->server->logger()->info(
$this->actor->webfinger()->getHandle() . ':' . __METHOD__,
$this->actor->webfinger()->getHandle() . ':' . __METHOD__,
[$url]
);

Expand All @@ -60,12 +61,12 @@ public function getPage(string $url)

/**
* Fetch an outbox
*
*
* @return \ActivityPhp\Type\Core\OrderedCollection
*/
public function get()
{
if (!is_null($this->orderedCollection)) {
if (! is_null($this->orderedCollection)) {
return $this->orderedCollection;
}

Expand All @@ -74,25 +75,25 @@ public function get()
);

$url = $this->actor->get('outbox');

if (is_null($url)) {
$this->server->logger()->warning(
$this->actor->webfinger()->getHandle()
$this->actor->webfinger()->getHandle()
. ': Outbox is not defined'
);
return;
}
}

$this->orderedCollection = Type::create(
Helper::fetch($url)
);

return $this->orderedCollection;
}

/**
* Post a message to the world
*
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
Expand All @@ -106,8 +107,8 @@ public function post(Request $request)
);

// Check current actor can post


// Get content
$payload = Util::decodeJson(
(string)$request->getContent()
Expand All @@ -125,10 +126,10 @@ public function post(Request $request)

return new Response('', 400);
}

// Log
$this->getServer()->logger()->debug(
$this->actor->get()->preferredUsername. ':' . __METHOD__ . '(starting)',
$this->actor->get()->preferredUsername. ':' . __METHOD__ . '(starting)',
$activity->toArray()
);

Expand All @@ -137,9 +138,9 @@ public function post(Request $request)
$activity = $this->wrapObject($activity);
}

// Clients submitting the following activities to an outbox MUST
// provide the object property in the activity:
// Create, Update, Delete, Follow,
// Clients submitting the following activities to an outbox MUST
// provide the object property in the activity:
// Create, Update, Delete, Follow,
// Add, Remove, Like, Block, Undo
if (!isset($activity->object)) {
throw new Exception(
Expand Down Expand Up @@ -172,7 +173,7 @@ public function post(Request $request)

// Log
$this->getServer()->logger()->debug(
$this->actor->get()->preferredUsername. ':' . __METHOD__ . '(posted)',
$this->actor->get()->preferredUsername. ':' . __METHOD__ . '(posted)',
$activity->toArray()
);

Expand Down
12 changes: 6 additions & 6 deletions src/ActivityPhp/Server/Http/WebFingerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

/**
* A simple WebFinger discoverer tool
*/
*/
class WebFingerFactory
{
const WEBFINGER_URL = '%s://%s%s/.well-known/webfinger?resource=acct:%s';
Expand All @@ -28,22 +28,22 @@ class WebFingerFactory
protected static $server;

/**
* @var array An array of key => value.
* @var array An array of key => value.
* Keys are handle, values are WebFinger instances.
*/
protected static $webfingers = [];

/**
* Get a profile via WebFinger protocol
*
*
* @param string $handle
* @param string $scheme Only for testing purpose
* @return \ActivityPhp\Server\Http\WebFinger
* @throws \Exception if handle is malformed.
*/
public static function get(string $handle, string $scheme = 'https')
{
if (!preg_match(
if (! preg_match(
'/^@?(?P<user>[\w\-\.]+)@(?P<host>[\w\.\-]+)(?P<port>:[\d]+)?$/',
$handle,
$matches
Expand Down Expand Up @@ -74,7 +74,7 @@ public static function get(string $handle, string $scheme = 'https')
))->get($url)
);

if (!is_array($content) || !count($content)) {
if (! is_array($content) || ! count($content)) {
throw new Exception('WebFinger fetching has failed');
}

Expand All @@ -85,7 +85,7 @@ public static function get(string $handle, string $scheme = 'https')

/**
* Inject a server instance
*
*
* @param \ActivityPhp\Server $server
*/
public static function setServer(Server $server)
Expand Down
5 changes: 5 additions & 0 deletions src/ActivityPhp/Type/Ontology/Mastodon.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ abstract class Mastodon extends OntologyBase
'height',
'focalPoint',
],
'Emoji' => [
'name',
'updated',
'icon',
],
'Hashtag' => [
'href'
],
Expand Down

0 comments on commit 8939581

Please sign in to comment.