forked from GNUsocial/gnu-social
[FreeNetwork] Port Discovery
This commit is contained in:
parent
3cdaf6671a
commit
5189269e5b
@ -1,158 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types = 1);
|
|
||||||
// {{{ License
|
|
||||||
|
|
||||||
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
||||||
//
|
|
||||||
// GNU social is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Affero General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// GNU social is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU Affero General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
|
||||||
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
// }}}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* WebFinger implementation for GNU social
|
|
||||||
*
|
|
||||||
* @package GNUsocial
|
|
||||||
*
|
|
||||||
* @author Diogo Peralta Cordeiro <mail@diogo.site
|
|
||||||
* @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
|
|
||||||
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Component\FreeNetwork\Entity;
|
|
||||||
|
|
||||||
use App\Core\DB\DB;
|
|
||||||
use App\Core\Entity;
|
|
||||||
use App\Core\Event;
|
|
||||||
use DateTimeInterface;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Table Definition for freenetwork_actor
|
|
||||||
*
|
|
||||||
* @author Diogo Peralta Cordeiro <mail@diogo.site
|
|
||||||
* @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
|
|
||||||
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
||||||
*/
|
|
||||||
class FreenetworkActor extends Entity
|
|
||||||
{
|
|
||||||
// {{{ Autocode
|
|
||||||
// @codeCoverageIgnoreStart
|
|
||||||
private string $profile_page;
|
|
||||||
private int $actor_id;
|
|
||||||
private bool $is_local;
|
|
||||||
private DateTimeInterface $created;
|
|
||||||
private DateTimeInterface $modified;
|
|
||||||
|
|
||||||
public function getProfilepage(): string
|
|
||||||
{
|
|
||||||
return $this->profile_page;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setProfilepage(string $profile_page): void
|
|
||||||
{
|
|
||||||
$this->profile_page = $profile_page;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getSource(): string
|
|
||||||
{
|
|
||||||
return $this->source;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setSource(string $source): void
|
|
||||||
{
|
|
||||||
$this->source = $source;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getActorId(): int
|
|
||||||
{
|
|
||||||
return $this->actor_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setActorId(int $actor_id): void
|
|
||||||
{
|
|
||||||
$this->actor_id = $actor_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getIsLocal(): bool
|
|
||||||
{
|
|
||||||
return $this->is_local;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setIsLocal(bool $is_local): void
|
|
||||||
{
|
|
||||||
$this->is_local = $is_local;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getCreated(): DateTimeInterface
|
|
||||||
{
|
|
||||||
return $this->created;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setCreated(DateTimeInterface $created): void
|
|
||||||
{
|
|
||||||
$this->created = $created;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getModified(): DateTimeInterface
|
|
||||||
{
|
|
||||||
return $this->modified;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setModified(DateTimeInterface $modified): void
|
|
||||||
{
|
|
||||||
$this->modified = $modified;
|
|
||||||
}
|
|
||||||
// @codeCoverageIgnoreEnd
|
|
||||||
// }}} Autocode
|
|
||||||
|
|
||||||
public static function getOrCreateByRemoteUri($actor_uri): self
|
|
||||||
{
|
|
||||||
$fnactor = DB::findBy('freenetwork_actor', ['profile_page' => $actor_uri, 'is_local' => false]);
|
|
||||||
if ($fnactor === []) {
|
|
||||||
// TODO grab with webfinger
|
|
||||||
// If already has for a different protocol and isn't local, update
|
|
||||||
// else create actor and then fnactor
|
|
||||||
$fnactor = self::create([
|
|
||||||
'profile_page' => $actor_uri,
|
|
||||||
'actor_id' => 1,
|
|
||||||
'is_local' => false,
|
|
||||||
]);
|
|
||||||
DB::persist($fnactor);
|
|
||||||
return $fnactor;
|
|
||||||
} else {
|
|
||||||
return $fnactor[0];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function schemaDef()
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'name' => 'freenetwork_actor',
|
|
||||||
'fields' => [
|
|
||||||
'profile_page' => ['type' => 'text', 'not null' => true],
|
|
||||||
'actor_id' => ['type' => 'int', 'not null' => true],
|
|
||||||
'is_local' => ['type' => 'bool', 'not null' => true, 'description' => 'whether this was a locally generated or an imported actor'],
|
|
||||||
'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
|
|
||||||
'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
|
|
||||||
],
|
|
||||||
'primary key' => ['profile_page'],
|
|
||||||
'indexes' => [
|
|
||||||
'freenetwork_profile_page_idx' => ['actor_id'],
|
|
||||||
],
|
|
||||||
'foreign keys' => [
|
|
||||||
'freenetwork_actor_actor_id_fkey' => ['actor', ['actor_id' => 'id']],
|
|
||||||
],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
@ -21,14 +21,12 @@ declare(strict_types = 1);
|
|||||||
|
|
||||||
namespace Component\FreeNetwork;
|
namespace Component\FreeNetwork;
|
||||||
|
|
||||||
use App\Core\DB\DB;
|
|
||||||
use App\Core\Event;
|
use App\Core\Event;
|
||||||
use App\Core\Router\Router;
|
|
||||||
use Component\FreeNetwork\Entity\FreenetworkActor;
|
|
||||||
use function App\Core\I18n\_m;
|
use function App\Core\I18n\_m;
|
||||||
use App\Core\Log;
|
use App\Core\Log;
|
||||||
use App\Core\Modules\Component;
|
use App\Core\Modules\Component;
|
||||||
use App\Core\Router\RouteLoader;
|
use App\Core\Router\RouteLoader;
|
||||||
|
use App\Core\Router\Router;
|
||||||
use App\Entity\Actor;
|
use App\Entity\Actor;
|
||||||
use App\Entity\LocalUser;
|
use App\Entity\LocalUser;
|
||||||
use App\Entity\Note;
|
use App\Entity\Note;
|
||||||
@ -43,7 +41,7 @@ use Component\FreeNetwork\Controller\Webfinger;
|
|||||||
use Component\FreeNetwork\Util\Discovery;
|
use Component\FreeNetwork\Util\Discovery;
|
||||||
use Component\FreeNetwork\Util\WebfingerResource;
|
use Component\FreeNetwork\Util\WebfingerResource;
|
||||||
use Component\FreeNetwork\Util\WebfingerResource\WebfingerResourceActor;
|
use Component\FreeNetwork\Util\WebfingerResource\WebfingerResourceActor;
|
||||||
use Doctrine\ORM\NoResultException;
|
use Component\FreeNetwork\Util\WebfingerResource\WebfingerResourceNote;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Plugin\ActivityPub\Entity\ActivitypubActivity;
|
use Plugin\ActivityPub\Entity\ActivitypubActivity;
|
||||||
use Plugin\ActivityPub\Util\Response\TypeResponse;
|
use Plugin\ActivityPub\Util\Response\TypeResponse;
|
||||||
@ -62,82 +60,46 @@ use XML_XRD_Element_Link;
|
|||||||
*/
|
*/
|
||||||
class FreeNetwork extends Component
|
class FreeNetwork extends Component
|
||||||
{
|
{
|
||||||
const PLUGIN_VERSION = '0.1.0';
|
public const PLUGIN_VERSION = '0.1.0';
|
||||||
|
|
||||||
const OAUTH_ACCESS_TOKEN_REL = 'http://apinamespace.org/oauth/access_token';
|
public const OAUTH_ACCESS_TOKEN_REL = 'http://apinamespace.org/oauth/access_token';
|
||||||
const OAUTH_REQUEST_TOKEN_REL = 'http://apinamespace.org/oauth/request_token';
|
public const OAUTH_REQUEST_TOKEN_REL = 'http://apinamespace.org/oauth/request_token';
|
||||||
const OAUTH_AUTHORIZE_REL = 'http://apinamespace.org/oauth/authorize';
|
public const OAUTH_AUTHORIZE_REL = 'http://apinamespace.org/oauth/authorize';
|
||||||
|
|
||||||
public function onAddRoute(RouteLoader $m): bool
|
public function onAddRoute(RouteLoader $m): bool
|
||||||
{
|
{
|
||||||
$m->connect('freenetwork_hostmeta', '.well-known/host-meta', [HostMeta::class, 'handle']);
|
$m->connect('freenetwork_hostmeta', '.well-known/host-meta', [HostMeta::class, 'handle']);
|
||||||
$m->connect('freenetwork_hostmeta_format', '.well-known/host-meta.:format',
|
$m->connect(
|
||||||
|
'freenetwork_hostmeta_format',
|
||||||
|
'.well-known/host-meta.:format',
|
||||||
[HostMeta::class, 'handle'],
|
[HostMeta::class, 'handle'],
|
||||||
['format' => '(xml|json)']);
|
['format' => '(xml|json)'],
|
||||||
|
);
|
||||||
// the resource GET parameter can be anywhere, so don't mention it here
|
// the resource GET parameter can be anywhere, so don't mention it here
|
||||||
$m->connect('freenetwork_webfinger', '.well-known/webfinger', [Webfinger::class, 'handle']);
|
$m->connect('freenetwork_webfinger', '.well-known/webfinger', [Webfinger::class, 'handle']);
|
||||||
$m->connect('freenetwork_webfinger_format', '.well-known/webfinger.:format',
|
$m->connect(
|
||||||
|
'freenetwork_webfinger_format',
|
||||||
|
'.well-known/webfinger.:format',
|
||||||
[Webfinger::class, 'handle'],
|
[Webfinger::class, 'handle'],
|
||||||
['format' => '(xml|json)']);
|
['format' => '(xml|json)'],
|
||||||
|
);
|
||||||
$m->connect('freenetwork_ownerxrd', 'main/ownerxrd', [OwnerXrd::class, 'handle']);
|
$m->connect('freenetwork_ownerxrd', 'main/ownerxrd', [OwnerXrd::class, 'handle']);
|
||||||
return Event::next;
|
return Event::next;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onLoginAction($action, &$login)
|
public function onStartGetProfileAcctUri(Actor $profile, &$acct): bool
|
||||||
{
|
|
||||||
switch ($action) {
|
|
||||||
case 'hostmeta':
|
|
||||||
case 'webfinger':
|
|
||||||
$login = true;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Actor $actor
|
|
||||||
* @param LocalUser $user
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
// public function onSuccessfulLocalUserRegistration(Actor $actor, LocalUser $user): bool
|
|
||||||
// {
|
|
||||||
// //$profile_page = Router::url(id: 'actor_view_nickname', args: ['nickname' => $actor->getNickname()], type: Router::ABSOLUTE_URL);
|
|
||||||
// $profile_page = $actor->getUrl(Router::ABSOLUTE_URL);
|
|
||||||
// $actor_uri = $actor->getUri(Router::ABSOLUTE_URL);
|
|
||||||
// Event::handle('FreeNetworkSaveProfilePage', [$source = 'user', $actor_id = $actor->getId(), &$profile_page, &$actor_uri]);
|
|
||||||
// $fnactorpp = FreenetworkActor::create([
|
|
||||||
// 'actor_uri' => $profile_page,
|
|
||||||
// 'source' => $source,
|
|
||||||
// 'actor_id' => $actor_id,
|
|
||||||
// 'is_local' => true,
|
|
||||||
// ]);
|
|
||||||
// DB::persist($fnactorpp);
|
|
||||||
// if ($profile_page !== $actor_uri) {
|
|
||||||
// $fnactoruri = FreenetworkActor::create([
|
|
||||||
// 'actor_uri' => $actor_uri,
|
|
||||||
// 'source' => $source,
|
|
||||||
// 'actor_id' => $actor_id,
|
|
||||||
// 'is_local' => true,
|
|
||||||
// ]);
|
|
||||||
// DB::persist($fnactoruri);
|
|
||||||
// }
|
|
||||||
// return Event::next;
|
|
||||||
// }
|
|
||||||
|
|
||||||
public function onStartGetProfileAcctUri(Actor $profile, &$acct)
|
|
||||||
{
|
{
|
||||||
$wfr = new WebFingerResourceActor($profile);
|
$wfr = new WebFingerResourceActor($profile);
|
||||||
try {
|
try {
|
||||||
$acct = $wfr->reconstructAcct();
|
$acct = $wfr->reconstructAcct();
|
||||||
} catch (Exception $e) {
|
} catch (Exception) {
|
||||||
return true;
|
return Event::next;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return Event::stop;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onEndGetWebFingerResource($resource, WebfingerResource &$target = null, array $args = [])
|
public function onEndGetWebFingerResource($resource, ?WebfingerResource &$target = null, array $args = [])
|
||||||
{
|
{
|
||||||
// * Either we didn't find the profile, then we want to make
|
// * Either we didn't find the profile, then we want to make
|
||||||
// the $profile variable null for clarity.
|
// the $profile variable null for clarity.
|
||||||
@ -147,10 +109,10 @@ class FreeNetwork extends Component
|
|||||||
// our search here by discarding the remote profile.
|
// our search here by discarding the remote profile.
|
||||||
$profile = null;
|
$profile = null;
|
||||||
if (Discovery::isAcct($resource)) {
|
if (Discovery::isAcct($resource)) {
|
||||||
$parts = explode('@', substr(urldecode($resource), 5)); // 5 is strlen of 'acct:'
|
$parts = explode('@', mb_substr(urldecode($resource), 5)); // 5 is strlen of 'acct:'
|
||||||
if (count($parts) == 2) {
|
if (\count($parts) == 2) {
|
||||||
list($nick, $domain) = $parts;
|
[$nick, $domain] = $parts;
|
||||||
if ($domain !== $_ENV['SOCIAL_DOMAIN']) {// XXX: Common::config('site', 'server')) {
|
if ($domain !== $_ENV['SOCIAL_DOMAIN']) {
|
||||||
throw new ServerException(_m('Remote profiles not supported via WebFinger yet.'));
|
throw new ServerException(_m('Remote profiles not supported via WebFinger yet.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,43 +123,27 @@ class FreeNetwork extends Component
|
|||||||
}
|
}
|
||||||
$profile = $freenetwork_actor->getActor();
|
$profile = $freenetwork_actor->getActor();
|
||||||
}
|
}
|
||||||
} elseif (!filter_var($resource, FILTER_VALIDATE_URL)) {
|
} else {
|
||||||
// Try the User URI lookup!
|
|
||||||
try {
|
try {
|
||||||
|
if (filter_var($resource, \FILTER_VALIDATE_URL) !== false) {
|
||||||
|
// This means $resource is a valid url
|
||||||
$resource_parts = parse_url($resource);
|
$resource_parts = parse_url($resource);
|
||||||
if ($resource_parts['host'] === $_ENV['SOCIAL_DOMAIN']) {
|
// TODO: Use URLMatcher
|
||||||
$str = parse_url($resource_parts['path']);
|
if ($resource_parts['host'] === $_ENV['SOCIAL_DOMAIN']) { // XXX: Common::config('site', 'server')) {
|
||||||
|
$str = $resource_parts['path'];
|
||||||
// actor_view_nickname
|
// actor_view_nickname
|
||||||
$renick = '/\/@(' . Nickname::DISPLAY_FMT . ')\/?/m';
|
$renick = '/\/@(' . Nickname::DISPLAY_FMT . ')\/?/m';
|
||||||
// actor_view_id
|
// actor_view_id
|
||||||
$reuri = '/\/actor/(\d+)\/?/m';
|
$reuri = '/\/actor\/(\d+)\/?/m';
|
||||||
if (preg_match_all($renick, $str, $matches, PREG_SET_ORDER, 0) === 1) {
|
if (preg_match_all($renick, $str, $matches, \PREG_SET_ORDER, 0) === 1) {
|
||||||
$profile = LocalUser::getWithPK(['nickname' => $matches[1]])->getActor();
|
$profile = LocalUser::getWithPK(['nickname' => $matches[0][1]])->getActor();
|
||||||
} else if (preg_match_all($reuri, $str, $matches, PREG_SET_ORDER, 0) === 1) {
|
} elseif (preg_match_all($reuri, $str, $matches, \PREG_SET_ORDER, 0) === 1) {
|
||||||
$profile = Actor::getById($matches[1]);
|
$profile = Actor::getById((int) $matches[0][1]);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
throw new NoResultException();
|
|
||||||
}
|
}
|
||||||
} catch (NoResultException $e) {
|
}
|
||||||
|
} catch (NoSuchActorException $e) {
|
||||||
// not a User, maybe a Note? we'll try that further down...
|
// not a User, maybe a Note? we'll try that further down...
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// this means $resource is a common_valid_http_url (or https)
|
|
||||||
// First build up a set of alternative resource URLs that we can use.
|
|
||||||
try {
|
|
||||||
Log::debug(__METHOD__ . ': Finding User URI for WebFinger lookup on resource==' . $resource);
|
|
||||||
$freenetwork_actor = FreenetworkActor::getWithPK(['profile_page' => $resource]);
|
|
||||||
if ($freenetwork_actor !== null) {
|
|
||||||
$profile = Actor::getById($freenetwork_actor->getActorId());
|
|
||||||
}
|
|
||||||
unset($freenetwork_actor);
|
|
||||||
} catch (Exception $e) {
|
|
||||||
// Most likely a UserNoProfileException, if it ever happens
|
|
||||||
// and then we need to do some debugging and perhaps fixes.
|
|
||||||
Log::error(get_class($e) . ': ' . $e->getMessage());
|
|
||||||
throw $e;
|
|
||||||
}
|
|
||||||
|
|
||||||
// try {
|
// try {
|
||||||
// Log::debug(__METHOD__ . ': Finding User_group URI for WebFinger lookup on resource==' . $resource);
|
// Log::debug(__METHOD__ . ': Finding User_group URI for WebFinger lookup on resource==' . $resource);
|
||||||
@ -213,43 +159,45 @@ class FreeNetwork extends Component
|
|||||||
// throw $e;
|
// throw $e;
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ($profile instanceof Actor) {
|
if ($profile instanceof Actor) {
|
||||||
Log::debug(__METHOD__ . ': Found Profile with ID==' . $profile->getID() . ' for resource==' . $resource);
|
Log::debug(__METHOD__ . ': Found Profile with ID==' . $profile->getID() . ' for resource==' . $resource);
|
||||||
$target = new WebfingerResourceActor($profile);
|
$target = new WebfingerResourceActor($profile);
|
||||||
return false; // We got our target, stop handler execution
|
return Event::stop; // We got our target, stop handler execution
|
||||||
}
|
}
|
||||||
|
|
||||||
$APNote = ActivitypubActivity::getWithPK(['object_uri' => $resource]);
|
$APNote = ActivitypubActivity::getWithPK(['object_uri' => $resource]);
|
||||||
if ($APNote instanceof ActivitypubActivity) {
|
if ($APNote instanceof ActivitypubActivity) {
|
||||||
$target = new WebfingerResource\WebfingerResourceNote(Note::getWithPK(['id' => $APNote->getObjectId()]));
|
$target = new WebfingerResourceNote(Note::getWithPK(['id' => $APNote->getObjectId()]));
|
||||||
return false;
|
return Event::stop; // We got our target, stop handler execution
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return Event::next;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onStartHostMetaLinks(array &$links)
|
public function onStartHostMetaLinks(array &$links): bool
|
||||||
{
|
{
|
||||||
foreach (Discovery::supportedMimeTypes() as $type) {
|
foreach (Discovery::supportedMimeTypes() as $type) {
|
||||||
$links[] = new XML_XRD_Element_Link(Discovery::LRDD_REL,
|
$links[] = new XML_XRD_Element_Link(
|
||||||
|
Discovery::LRDD_REL,
|
||||||
Router::url(id: 'freenetwork_webfinger', args: [], type: Router::ABSOLUTE_URL) . '?resource={uri}',
|
Router::url(id: 'freenetwork_webfinger', args: [], type: Router::ABSOLUTE_URL) . '?resource={uri}',
|
||||||
$type,
|
$type,
|
||||||
isTemplate: true);
|
isTemplate: true,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO OAuth connections
|
// TODO OAuth connections
|
||||||
//$links[] = new XML_XRD_Element_link(self::OAUTH_ACCESS_TOKEN_REL, common_local_url('ApiOAuthAccessToken'));
|
//$links[] = new XML_XRD_Element_link(self::OAUTH_ACCESS_TOKEN_REL, common_local_url('ApiOAuthAccessToken'));
|
||||||
//$links[] = new XML_XRD_Element_link(self::OAUTH_REQUEST_TOKEN_REL, common_local_url('ApiOAuthRequestToken'));
|
//$links[] = new XML_XRD_Element_link(self::OAUTH_REQUEST_TOKEN_REL, common_local_url('ApiOAuthRequestToken'));
|
||||||
//$links[] = new XML_XRD_Element_link(self::OAUTH_AUTHORIZE_REL, common_local_url('ApiOAuthAuthorize'));
|
//$links[] = new XML_XRD_Element_link(self::OAUTH_AUTHORIZE_REL, common_local_url('ApiOAuthAuthorize'));
|
||||||
|
return Event::next;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a link header for LRDD Discovery
|
* Add a link header for LRDD Discovery
|
||||||
*
|
|
||||||
* @param mixed $action
|
|
||||||
*/
|
*/
|
||||||
public function onStartShowHTML($action)
|
public function onStartShowHTML($action): bool
|
||||||
{
|
{
|
||||||
if ($action instanceof ShowstreamAction) {
|
if ($action instanceof ShowstreamAction) {
|
||||||
$resource = $action->getTarget()->getUri();
|
$resource = $action->getTarget()->getUri();
|
||||||
@ -259,18 +207,21 @@ class FreeNetwork extends Component
|
|||||||
header('Link: <' . $url . '>; rel="' . Discovery::LRDD_REL . '"; type="' . $type . '"', false);
|
header('Link: <' . $url . '>; rel="' . Discovery::LRDD_REL . '"; type="' . $type . '"', false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return Event::next;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onStartDiscoveryMethodRegistration(Discovery $disco)
|
public function onStartDiscoveryMethodRegistration(Discovery $disco): bool
|
||||||
{
|
{
|
||||||
$disco->registerMethod('LRDDMethod_WebFinger');
|
$disco->registerMethod('\Component\FreeNetwork\Util\LrddMethod\LrddMethodWebfinger');
|
||||||
|
return Event::next;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onEndDiscoveryMethodRegistration(Discovery $disco)
|
public function onEndDiscoveryMethodRegistration(Discovery $disco): bool
|
||||||
{
|
{
|
||||||
$disco->registerMethod('LRDDMethod_HostMeta');
|
$disco->registerMethod('\Component\FreeNetwork\Util\LrddMethod\LrddMethodHostMeta');
|
||||||
$disco->registerMethod('LRDDMethod_LinkHeader');
|
$disco->registerMethod('\Component\FreeNetwork\Util\LrddMethod\LrddMethodLinkHeader');
|
||||||
$disco->registerMethod('LRDDMethod_LinkHTML');
|
$disco->registerMethod('\Component\FreeNetwork\Util\LrddMethod\LrddMethodLinkHtml');
|
||||||
|
return Event::next;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -279,7 +230,7 @@ class FreeNetwork extends Component
|
|||||||
*/
|
*/
|
||||||
public function onControllerResponseInFormat(string $route, array $accept_header, array $vars, ?TypeResponse &$response = null): bool
|
public function onControllerResponseInFormat(string $route, array $accept_header, array $vars, ?TypeResponse &$response = null): bool
|
||||||
{
|
{
|
||||||
if (!in_array($route, ['freenetwork_hostmeta', 'freenetwork_hostmeta_format', 'freenetwork_webfinger', 'freenetwork_webfinger_format', 'freenetwork_ownerxrd'])) {
|
if (!\in_array($route, ['freenetwork_hostmeta', 'freenetwork_hostmeta_format', 'freenetwork_webfinger', 'freenetwork_webfinger_format', 'freenetwork_ownerxrd'])) {
|
||||||
return Event::next;
|
return Event::next;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -293,7 +244,7 @@ class FreeNetwork extends Component
|
|||||||
* -- RFC 7033 (WebFinger)
|
* -- RFC 7033 (WebFinger)
|
||||||
* http://tools.ietf.org/html/rfc7033
|
* http://tools.ietf.org/html/rfc7033
|
||||||
*/
|
*/
|
||||||
$mimeType = count($mimeType) !== 0 ? array_pop($mimeType) : $vars['default_mimetype'];
|
$mimeType = \count($mimeType) !== 0 ? array_pop($mimeType) : $vars['default_mimetype'];
|
||||||
|
|
||||||
$headers = [];
|
$headers = [];
|
||||||
|
|
||||||
@ -310,32 +261,6 @@ class FreeNetwork extends Component
|
|||||||
return Event::stop;
|
return Event::stop;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetch all the aliases of some remote profile
|
|
||||||
*
|
|
||||||
* @param string $uri profile's URI
|
|
||||||
*
|
|
||||||
* @throws Exception (If the Discovery's HTTP requests fail)
|
|
||||||
*
|
|
||||||
* @return null|array aliases
|
|
||||||
*
|
|
||||||
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
|
|
||||||
*/
|
|
||||||
public static function grab_profile_aliases(string $uri): ?array
|
|
||||||
{
|
|
||||||
$disco = new Discovery();
|
|
||||||
$xrd = $disco->lookup($uri);
|
|
||||||
|
|
||||||
$all_ids = array_merge([$xrd->subject], $xrd->aliases);
|
|
||||||
|
|
||||||
if (!in_array($uri, $all_ids)) {
|
|
||||||
Log::info('The original URI was not listed itself when doing discovery on it!');
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $all_ids;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function onPluginVersion(array &$versions): bool
|
public function onPluginVersion(array &$versions): bool
|
||||||
{
|
{
|
||||||
$versions[] = [
|
$versions[] = [
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types = 1);
|
||||||
/**
|
/**
|
||||||
* StatusNet - the distributed open-source microblogging tool
|
* StatusNet - the distributed open-source microblogging tool
|
||||||
* Copyright (C) 2010, StatusNet, Inc.
|
* Copyright (C) 2010, StatusNet, Inc.
|
||||||
@ -37,31 +39,32 @@
|
|||||||
namespace Component\FreeNetwork\Util;
|
namespace Component\FreeNetwork\Util;
|
||||||
|
|
||||||
use App\Core\Event;
|
use App\Core\Event;
|
||||||
|
use App\Core\GSFile;
|
||||||
use App\Core\HTTPClient;
|
use App\Core\HTTPClient;
|
||||||
|
use function App\Core\I18n\_m;
|
||||||
use App\Core\Log;
|
use App\Core\Log;
|
||||||
use App\Util\Exception\ClientException;
|
use App\Util\Exception\ClientException;
|
||||||
|
use Exception;
|
||||||
use XML_XRD;
|
use XML_XRD;
|
||||||
use function App\Core\I18n\_m;
|
use XML_XRD_Element_Link;
|
||||||
|
|
||||||
class Discovery
|
class Discovery
|
||||||
{
|
{
|
||||||
const LRDD_REL = 'lrdd';
|
public const LRDD_REL = 'lrdd';
|
||||||
const UPDATESFROM = 'http://schemas.google.com/g/2010#updates-from';
|
public const UPDATESFROM = 'http://schemas.google.com/g/2010#updates-from';
|
||||||
const HCARD = 'http://microformats.org/profile/hcard';
|
public const HCARD = 'http://microformats.org/profile/hcard';
|
||||||
const MF2_HCARD = 'http://microformats.org/profile/h-card'; // microformats2 h-card
|
public const MF2_HCARD = 'http://microformats.org/profile/h-card'; // microformats2 h-card
|
||||||
|
|
||||||
const JRD_MIMETYPE_OLD = 'application/json'; // RFC6415 uses this
|
public const JRD_MIMETYPE_OLD = 'application/json'; // RFC6415 uses this
|
||||||
const JRD_MIMETYPE = 'application/jrd+json';
|
public const JRD_MIMETYPE = 'application/jrd+json';
|
||||||
const XRD_MIMETYPE = 'application/xrd+xml';
|
public const XRD_MIMETYPE = 'application/xrd+xml';
|
||||||
|
|
||||||
public $methods = [];
|
public array $methods = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for a discovery object
|
* Constructor for a discovery object
|
||||||
*
|
*
|
||||||
* Registers different discovery methods.
|
* Registers different discovery methods.
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
@ -70,7 +73,7 @@ class Discovery
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function supportedMimeTypes()
|
public static function supportedMimeTypes(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'json' => self::JRD_MIMETYPE,
|
'json' => self::JRD_MIMETYPE,
|
||||||
@ -83,10 +86,8 @@ class Discovery
|
|||||||
* Register a discovery class
|
* Register a discovery class
|
||||||
*
|
*
|
||||||
* @param string $class Class name
|
* @param string $class Class name
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
public function registerMethod($class)
|
public function registerMethod($class): void
|
||||||
{
|
{
|
||||||
$this->methods[] = $class;
|
$this->methods[] = $class;
|
||||||
}
|
}
|
||||||
@ -98,9 +99,9 @@ class Discovery
|
|||||||
*
|
*
|
||||||
* @return XML_XRD object for the resource descriptor of the id
|
* @return XML_XRD object for the resource descriptor of the id
|
||||||
*/
|
*/
|
||||||
public function lookup($id)
|
public function lookup(string $id): XML_XRD
|
||||||
{
|
{
|
||||||
// Normalize the incoming $id to make sure we have a uri
|
// Normalize the incoming $id to make sure we have an uri
|
||||||
$uri = self::normalize($id);
|
$uri = self::normalize($id);
|
||||||
|
|
||||||
Log::debug(sprintf('Performing discovery for "%s" (normalized "%s")', $id, $uri));
|
Log::debug(sprintf('Performing discovery for "%s" (normalized "%s")', $id, $uri));
|
||||||
@ -123,18 +124,17 @@ class Discovery
|
|||||||
throw new Exception('No resource descriptor URI in link.');
|
throw new Exception('No resource descriptor URI in link.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$client = new HTTPClient();
|
|
||||||
$headers = [];
|
$headers = [];
|
||||||
if (!is_null($link->type)) {
|
if (!\is_null($link->type)) {
|
||||||
$headers[] = "Accept: {$link->type}";
|
$headers['Accept'] = $link->type;
|
||||||
}
|
}
|
||||||
|
|
||||||
$response = $client->get($xrd_uri, $headers);
|
$response = HTTPClient::get($xrd_uri, ['headers' => $headers]);
|
||||||
if ($response->getStatus() != 200) {
|
if ($response->getStatusCode() !== 200) {
|
||||||
throw new Exception('Unexpected HTTP status code.');
|
throw new Exception('Unexpected HTTP status code.');
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (common_bare_mime($response->getHeader('content-type'))) {
|
switch (GSFile::mimetypeBare($response->getHeaders()['content-type'][0])) {
|
||||||
case self::JRD_MIMETYPE_OLD:
|
case self::JRD_MIMETYPE_OLD:
|
||||||
case self::JRD_MIMETYPE:
|
case self::JRD_MIMETYPE:
|
||||||
$type = 'json';
|
$type = 'json';
|
||||||
@ -144,24 +144,24 @@ class Discovery
|
|||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// fall back to letting XML_XRD auto-detect
|
// fall back to letting XML_XRD auto-detect
|
||||||
Log::debug('No recognized content-type header for resource descriptor body on ' . _ve($xrd_uri));
|
Log::debug('No recognized content-type header for resource descriptor body on ' . $xrd_uri);
|
||||||
$type = null;
|
$type = null;
|
||||||
}
|
}
|
||||||
$xrd->loadString($response->getBody(), $type);
|
$xrd->loadString($response->getContent(), $type);
|
||||||
return $xrd;
|
return $xrd;
|
||||||
} catch (ClientException $e) {
|
} catch (ClientException $e) {
|
||||||
if ($e->getCode() === 403) {
|
if ($e->getCode() === 403) {
|
||||||
Log::info(sprintf('%s: Aborting discovery on URL %s: %s', _ve($class), _ve($uri), _ve($e->getMessage())));
|
Log::info(sprintf('%s: Aborting discovery on URL %s: %s', $class, $uri, $e->getMessage()));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Log::info(sprintf('%s: Failed for %s: %s', _ve($class), _ve($uri), _ve($e->getMessage())));
|
Log::info(sprintf('%s: Failed for %s: %s', $class, $uri, $e->getMessage()));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TRANS: Exception. %s is an ID.
|
// TRANS: Exception. %s is an ID.
|
||||||
throw new Exception(sprintf(_('Unable to find services for %s.'), $id));
|
throw new Exception(sprintf(_m('Unable to find services for %s.'), $id));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -170,9 +170,9 @@ class Discovery
|
|||||||
* @param array $links Links to check (as instances of XML_XRD_Element_Link)
|
* @param array $links Links to check (as instances of XML_XRD_Element_Link)
|
||||||
* @param string $service Service to find
|
* @param string $service Service to find
|
||||||
*
|
*
|
||||||
* @return array $link assoc array representing the link
|
* @return XML_XRD_Element_Link $link
|
||||||
*/
|
*/
|
||||||
public static function getService(array $links, $service)
|
public static function getService(array $links, $service): XML_XRD_Element_Link
|
||||||
{
|
{
|
||||||
foreach ($links as $link) {
|
foreach ($links as $link) {
|
||||||
if ($link->rel === $service) {
|
if ($link->rel === $service) {
|
||||||
@ -187,17 +187,12 @@ class Discovery
|
|||||||
/**
|
/**
|
||||||
* Given a "user id" make sure it's normalized to an acct: uri
|
* Given a "user id" make sure it's normalized to an acct: uri
|
||||||
*
|
*
|
||||||
* @param string $user_id User ID to normalize
|
* @param string $uri User ID to normalize
|
||||||
* @param mixed $uri
|
|
||||||
*
|
*
|
||||||
* @return string normalized acct: URI
|
* @return string normalized acct: URI
|
||||||
*/
|
*/
|
||||||
public static function normalize($uri)
|
public static function normalize(string $uri): string
|
||||||
{
|
{
|
||||||
if (is_null($uri) || $uri === '') {
|
|
||||||
throw new Exception(_m('No resource given.'));
|
|
||||||
}
|
|
||||||
|
|
||||||
$parts = parse_url($uri);
|
$parts = parse_url($uri);
|
||||||
// If we don't have a scheme, but the path implies user@host,
|
// If we don't have a scheme, but the path implies user@host,
|
||||||
// though this is far from a perfect matching procedure...
|
// though this is far from a perfect matching procedure...
|
||||||
@ -209,7 +204,7 @@ class Discovery
|
|||||||
return $uri;
|
return $uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function isAcct($uri)
|
public static function isAcct($uri): bool
|
||||||
{
|
{
|
||||||
return mb_strtolower(mb_substr($uri, 0, 5)) == 'acct:';
|
return mb_strtolower(mb_substr($uri, 0, 5)) == 'acct:';
|
||||||
}
|
}
|
||||||
@ -224,10 +219,8 @@ class Discovery
|
|||||||
*
|
*
|
||||||
* @return string replaced values
|
* @return string replaced values
|
||||||
*/
|
*/
|
||||||
public static function applyTemplate($template, $uri)
|
public static function applyTemplate($template, $uri): string
|
||||||
{
|
{
|
||||||
$template = str_replace('{uri}', urlencode($uri), $template);
|
return str_replace('{uri}', urlencode($uri), $template);
|
||||||
|
|
||||||
return $template;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types = 1);
|
||||||
|
|
||||||
namespace Component\FreeNetwork\Util;
|
namespace Component\FreeNetwork\Util;
|
||||||
|
|
||||||
use App\Core\Event;
|
use App\Core\Event;
|
||||||
use App\Core\HTTPClient;
|
use App\Core\HTTPClient;
|
||||||
|
use Exception;
|
||||||
|
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||||
use XML_XRD;
|
use XML_XRD;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -40,26 +44,14 @@ abstract class LrddMethod
|
|||||||
*/
|
*/
|
||||||
abstract public function discover($uri);
|
abstract public function discover($uri);
|
||||||
|
|
||||||
protected function fetchUrl($url, $method = HTTPClient::METHOD_GET)
|
protected function fetchUrl($url, $method = 'get'): ResponseInterface
|
||||||
{
|
{
|
||||||
// If we have a blacklist enabled, let's check against it
|
// If we have a blacklist enabled, let's check against it
|
||||||
Event::handle('UrlBlacklistTest', [$url]);
|
Event::handle('UrlBlacklistTest', [$url]);
|
||||||
|
|
||||||
$client = new HTTPClient();
|
$response = HTTPClient::$method($url);
|
||||||
|
|
||||||
// GAAHHH, this method sucks! How about we make a better HTTPClient interface?
|
if ($response->getStatusCode() !== 200) {
|
||||||
switch ($method) {
|
|
||||||
case HTTPClient::METHOD_GET:
|
|
||||||
$response = $client->get($url);
|
|
||||||
break;
|
|
||||||
case HTTPClient::METHOD_HEAD:
|
|
||||||
$response = $client->head($url);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new Exception('Bad HTTP method.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($response->getStatus() != 200) {
|
|
||||||
throw new Exception('Unexpected HTTP status code.');
|
throw new Exception('Unexpected HTTP status code.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +17,7 @@ namespace Component\FreeNetwork\Util\LrddMethod;
|
|||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
||||||
use App\Core\Log;
|
use App\Core\Log;
|
||||||
|
use Component\FreeNetwork\Util\Discovery;
|
||||||
use Component\FreeNetwork\Util\LrddMethod;
|
use Component\FreeNetwork\Util\LrddMethod;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
@ -72,7 +73,7 @@ class LrddMethodHostMeta extends LRDDMethod
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
$response = self::fetchUrl($url);
|
$response = self::fetchUrl($url);
|
||||||
$this->xrd->loadString($response->getBody());
|
$this->xrd->loadString($response->getContent());
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Log::debug('LRDD could not load resource descriptor: ' . $url . ' (' . $e->getMessage() . ')');
|
Log::debug('LRDD could not load resource descriptor: ' . $url . ' (' . $e->getMessage() . ')');
|
||||||
continue;
|
continue;
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types = 1);
|
||||||
|
|
||||||
namespace Component\FreeNetwork\Util\LrddMethod;
|
namespace Component\FreeNetwork\Util\LrddMethod;
|
||||||
|
|
||||||
use App\Core\HTTPClient;
|
|
||||||
use App\Core\Log;
|
use App\Core\Log;
|
||||||
use Component\FreeNetwork\Util\LinkHeader;
|
use Component\FreeNetwork\Util\LinkHeader;
|
||||||
use Component\FreeNetwork\Util\LrddMethod;
|
use Component\FreeNetwork\Util\LrddMethod;
|
||||||
@ -29,16 +30,13 @@ class LrddMethodLinkHeader extends LRDDMethod
|
|||||||
/**
|
/**
|
||||||
* For HTTP IDs fetch the URL and look for Link headers.
|
* For HTTP IDs fetch the URL and look for Link headers.
|
||||||
*
|
*
|
||||||
* @param mixed $uri
|
|
||||||
*
|
|
||||||
* @todo fail out of WebFinger URIs faster
|
* @todo fail out of WebFinger URIs faster
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public function discover($uri)
|
public function discover($uri)
|
||||||
{
|
{
|
||||||
$response = self::fetchUrl($uri, HTTPClient::METHOD_HEAD);
|
$response = self::fetchUrl($uri, 'head');
|
||||||
|
|
||||||
$link_header = $response->getHeader('Link');
|
$link_header = $response->getHeaders()['link'][0];
|
||||||
if (empty($link_header)) {
|
if (empty($link_header)) {
|
||||||
throw new Exception('No Link header found');
|
throw new Exception('No Link header found');
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ class LrddMethodLinkHtml extends LRDDMethod
|
|||||||
{
|
{
|
||||||
$response = self::fetchUrl($uri);
|
$response = self::fetchUrl($uri);
|
||||||
|
|
||||||
return self::parse($response->getBody());
|
return self::parse($response->getContent());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -265,7 +265,7 @@ parameters:
|
|||||||
proxy_auth_scheme:
|
proxy_auth_scheme:
|
||||||
|
|
||||||
discovery:
|
discovery:
|
||||||
CORS: false
|
cors: false
|
||||||
|
|
||||||
streams:
|
streams:
|
||||||
notes_per_page: 32
|
notes_per_page: 32
|
||||||
|
Loading…
Reference in New Issue
Block a user