2018-05-05 00:54:41 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* GNU social - a federating social network
|
|
|
|
*
|
2018-07-09 23:43:34 +01:00
|
|
|
* ActivityPubPlugin implementation for GNU Social
|
2018-05-05 00:54:41 +01:00
|
|
|
*
|
|
|
|
* LICENCE: This program 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @category Plugin
|
|
|
|
* @package GNUsocial
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @author Daniel Supernault <danielsupernault@gmail.com>
|
2018-07-09 23:43:34 +01:00
|
|
|
* @copyright 2018 Free Software Foundation http://fsf.org
|
2018-05-05 00:54:41 +01:00
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
2018-07-09 23:43:34 +01:00
|
|
|
* @link https://www.gnu.org/software/social/
|
2018-05-05 00:54:41 +01:00
|
|
|
*/
|
2018-07-26 22:12:13 +01:00
|
|
|
if (!defined('GNUSOCIAL')) {
|
|
|
|
exit(1);
|
2018-07-09 23:43:34 +01:00
|
|
|
}
|
2018-05-05 00:54:41 +01:00
|
|
|
|
2018-07-09 23:43:34 +01:00
|
|
|
/**
|
2018-07-13 12:32:27 +01:00
|
|
|
* ActivityPub Profile
|
|
|
|
*
|
2018-07-09 23:43:34 +01:00
|
|
|
* @category Plugin
|
|
|
|
* @package GNUsocial
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
|
|
|
* @link http://www.gnu.org/software/social/
|
|
|
|
*/
|
2018-07-28 15:52:47 +01:00
|
|
|
class Activitypub_profile extends Managed_DataObject
|
2018-05-05 00:54:41 +01:00
|
|
|
{
|
2018-07-26 22:12:13 +01:00
|
|
|
public $__table = 'Activitypub_profile';
|
2018-07-29 02:35:04 +01:00
|
|
|
public $uri; // text() not_null
|
|
|
|
public $profile_id; // int(4) primary_key not_null
|
|
|
|
public $inboxuri; // text() not_null
|
|
|
|
public $sharedInboxuri; // text()
|
|
|
|
public $nickname; // varchar(64) multiple_key not_null
|
|
|
|
public $fullname; // text()
|
|
|
|
public $profileurl; // text()
|
|
|
|
public $homepage; // text()
|
|
|
|
public $bio; // text() multiple_key
|
|
|
|
public $location; // text()
|
|
|
|
public $created; // datetime() not_null
|
|
|
|
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
|
2018-07-26 22:12:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return table definition for Schema setup and DB_DataObject usage.
|
|
|
|
*
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @return array array of column definitions
|
|
|
|
*/
|
|
|
|
public static function schemaDef()
|
|
|
|
{
|
2018-07-28 15:52:47 +01:00
|
|
|
return [
|
|
|
|
'fields' => [
|
|
|
|
'uri' => ['type' => 'text', 'not null' => true],
|
|
|
|
'profile_id' => ['type' => 'integer'],
|
|
|
|
'inboxuri' => ['type' => 'text', 'not null' => true],
|
|
|
|
'sharedInboxuri' => ['type' => 'text'],
|
|
|
|
'created' => ['type' => 'datetime', 'not null' => true],
|
|
|
|
'modified' => ['type' => 'datetime', 'not null' => true],
|
|
|
|
],
|
|
|
|
'primary key' => ['profile_id'],
|
|
|
|
'unique keys' => [
|
|
|
|
'Activitypub_profile_profile_id_key' => ['profile_id'],
|
|
|
|
],
|
|
|
|
'foreign keys' => [
|
|
|
|
'Activitypub_profile_profile_id_fkey' => ['profile', ['profile_id' => 'id']],
|
|
|
|
],
|
|
|
|
];
|
2018-07-26 22:12:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a pretty profile from a Profile object
|
|
|
|
*
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @param Profile $profile
|
|
|
|
* @return pretty array to be used in a response
|
|
|
|
*/
|
|
|
|
public static function profile_to_array($profile)
|
|
|
|
{
|
|
|
|
$uri = ActivityPubPlugin::actor_uri($profile);
|
|
|
|
$id = $profile->getID();
|
2018-07-28 02:11:58 +01:00
|
|
|
$rsa = new Activitypub_rsa();
|
|
|
|
$public_key = $rsa->ensure_public_key($profile);
|
|
|
|
unset($rsa);
|
2018-07-26 22:12:13 +01:00
|
|
|
$res = [
|
2018-07-27 21:45:43 +01:00
|
|
|
'@context' => [
|
|
|
|
"https://www.w3.org/ns/activitystreams",
|
2018-07-31 20:09:47 +01:00
|
|
|
"https://w3id.org/security/v1"
|
2018-07-27 21:45:43 +01:00
|
|
|
],
|
|
|
|
'id' => $uri,
|
|
|
|
'type' => 'Person',
|
|
|
|
'following' => common_local_url("apActorFollowing", array("id" => $id)),
|
|
|
|
'followers' => common_local_url("apActorFollowers", array("id" => $id)),
|
|
|
|
'liked' => common_local_url("apActorLiked", array("id" => $id)),
|
|
|
|
'inbox' => common_local_url("apActorInbox", array("id" => $id)),
|
|
|
|
'preferredUsername' => $profile->getNickname(),
|
2018-07-29 02:35:04 +01:00
|
|
|
'name' => $profile->getBestName(),
|
2018-07-27 21:45:43 +01:00
|
|
|
'summary' => ($desc = $profile->getDescription()) == null ? "" : $desc,
|
|
|
|
'url' => $profile->getUrl(),
|
|
|
|
'manuallyApprovesFollowers' => false,
|
2018-07-28 02:11:58 +01:00
|
|
|
'publicKey' => [
|
2018-07-31 20:09:47 +01:00
|
|
|
'id' => $uri."#public-key",
|
2018-07-28 02:11:58 +01:00
|
|
|
'owner' => $uri,
|
|
|
|
'publicKeyPem' => $public_key
|
|
|
|
],
|
2018-07-27 21:45:43 +01:00
|
|
|
'tag' => [],
|
|
|
|
'attachment' => [],
|
|
|
|
'icon' => [
|
|
|
|
'type' => 'Image',
|
2018-08-01 21:24:36 +01:00
|
|
|
'mediaType' => 'image/png',
|
|
|
|
'height' => AVATAR_PROFILE_SIZE,
|
|
|
|
'width' => AVATAR_PROFILE_SIZE,
|
|
|
|
'url' => $profile->avatarUrl(AVATAR_PROFILE_SIZE)
|
2018-07-27 21:45:43 +01:00
|
|
|
]
|
|
|
|
];
|
2018-07-26 22:12:13 +01:00
|
|
|
|
|
|
|
if ($profile->isLocal()) {
|
2018-07-28 02:11:58 +01:00
|
|
|
$res['endpoints']['sharedInbox'] = common_local_url('apSharedInbox');
|
2018-07-26 22:12:13 +01:00
|
|
|
} else {
|
|
|
|
$aprofile = new Activitypub_profile();
|
|
|
|
$aprofile = $aprofile->from_profile($profile);
|
2018-07-27 21:45:43 +01:00
|
|
|
$res['endpoints']['sharedInbox'] = $aprofile->sharedInboxuri;
|
2018-07-09 23:43:34 +01:00
|
|
|
}
|
2018-07-13 00:20:18 +01:00
|
|
|
|
2018-07-26 22:12:13 +01:00
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-28 02:11:58 +01:00
|
|
|
* Insert the current object variables into the database
|
2018-07-26 22:12:13 +01:00
|
|
|
*
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @access public
|
|
|
|
* @throws ServerException
|
|
|
|
*/
|
|
|
|
public function do_insert()
|
|
|
|
{
|
|
|
|
$profile = new Profile();
|
|
|
|
|
|
|
|
$profile->created = $this->created = $this->modified = common_sql_now();
|
|
|
|
|
|
|
|
$fields = [
|
|
|
|
'uri' => 'profileurl',
|
|
|
|
'nickname' => 'nickname',
|
|
|
|
'fullname' => 'fullname',
|
|
|
|
'bio' => 'bio'
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($fields as $af => $pf) {
|
|
|
|
$profile->$pf = $this->$af;
|
2018-07-13 00:20:18 +01:00
|
|
|
}
|
|
|
|
|
2018-07-26 22:12:13 +01:00
|
|
|
$this->profile_id = $profile->insert();
|
|
|
|
if ($this->profile_id === false) {
|
|
|
|
$profile->query('ROLLBACK');
|
|
|
|
throw new ServerException('Profile insertion failed.');
|
2018-07-13 00:20:18 +01:00
|
|
|
}
|
|
|
|
|
2018-07-26 22:12:13 +01:00
|
|
|
$ok = $this->insert();
|
2018-07-13 00:20:18 +01:00
|
|
|
|
2018-07-26 22:12:13 +01:00
|
|
|
if ($ok === false) {
|
|
|
|
$profile->query('ROLLBACK');
|
2018-07-28 15:52:47 +01:00
|
|
|
$this->query('ROLLBACK');
|
2018-07-26 22:12:13 +01:00
|
|
|
throw new ServerException('Cannot save ActivityPub profile.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the locally stored profile for this Activitypub_profile
|
|
|
|
*
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @return Profile
|
|
|
|
* @throws NoProfileException if it was not found
|
|
|
|
*/
|
|
|
|
public function local_profile()
|
|
|
|
{
|
|
|
|
$profile = Profile::getKV('id', $this->profile_id);
|
|
|
|
if (!$profile instanceof Profile) {
|
|
|
|
throw new NoProfileException($this->profile_id);
|
|
|
|
}
|
|
|
|
return $profile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates an Activitypub_profile from a Profile
|
|
|
|
*
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @param Profile $profile
|
|
|
|
* @return Activitypub_profile
|
|
|
|
* @throws Exception if no Activitypub_profile exists for given Profile
|
|
|
|
*/
|
|
|
|
public static function from_profile(Profile $profile)
|
|
|
|
{
|
|
|
|
$profile_id = $profile->getID();
|
|
|
|
|
|
|
|
$aprofile = self::getKV('profile_id', $profile_id);
|
|
|
|
if (!$aprofile instanceof Activitypub_profile) {
|
|
|
|
// No Activitypub_profile for this profile_id,
|
|
|
|
if (!$profile->isLocal()) {
|
|
|
|
// create one!
|
|
|
|
$aprofile = self::create_from_local_profile($profile);
|
|
|
|
} else {
|
|
|
|
throw new Exception('No Activitypub_profile for Profile ID: '.$profile_id. ', this is a local user.');
|
|
|
|
}
|
2018-07-13 00:20:18 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 15:52:47 +01:00
|
|
|
$fields = [
|
|
|
|
'uri' => 'profileurl',
|
|
|
|
'nickname' => 'nickname',
|
|
|
|
'fullname' => 'fullname',
|
|
|
|
'bio' => 'bio'
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($fields as $af => $pf) {
|
|
|
|
$aprofile->$af = $profile->$pf;
|
2018-07-15 02:13:46 +01:00
|
|
|
}
|
|
|
|
|
2018-07-26 22:12:13 +01:00
|
|
|
return $aprofile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given an existent local profile creates an ActivityPub profile.
|
|
|
|
* One must be careful not to give a user profile to this function
|
|
|
|
* as only remote users have ActivityPub_profiles on local instance
|
|
|
|
*
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @param Profile $profile
|
|
|
|
* @return Activitypub_profile
|
|
|
|
*/
|
|
|
|
private static function create_from_local_profile(Profile $profile)
|
|
|
|
{
|
|
|
|
$url = $profile->getURL();
|
|
|
|
$inboxes = Activitypub_explorer::get_actor_inboxes_uri($url);
|
|
|
|
|
|
|
|
$aprofile->created = $aprofile->modified = common_sql_now();
|
|
|
|
|
|
|
|
$aprofile = new Activitypub_profile;
|
|
|
|
$aprofile->profile_id = $profile->getID();
|
|
|
|
$aprofile->uri = $url;
|
|
|
|
$aprofile->nickname = $profile->getNickname();
|
|
|
|
$aprofile->fullname = $profile->getFullname();
|
|
|
|
$aprofile->bio = substr($profile->getDescription(), 0, 1000);
|
|
|
|
$aprofile->inboxuri = $inboxes["inbox"];
|
|
|
|
$aprofile->sharedInboxuri = $inboxes["sharedInbox"];
|
|
|
|
|
|
|
|
$aprofile->insert();
|
|
|
|
|
|
|
|
return $aprofile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns sharedInbox if possible, inbox otherwise
|
|
|
|
*
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @return string Inbox URL
|
|
|
|
*/
|
|
|
|
public function get_inbox()
|
|
|
|
{
|
|
|
|
if (is_null($this->sharedInboxuri)) {
|
|
|
|
return $this->inboxuri;
|
2018-07-13 00:20:18 +01:00
|
|
|
}
|
2018-07-20 16:04:17 +01:00
|
|
|
|
2018-07-26 22:12:13 +01:00
|
|
|
return $this->sharedInboxuri;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter for uri property
|
|
|
|
*
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @return string URI
|
|
|
|
*/
|
2018-07-29 02:35:04 +01:00
|
|
|
public function getUri()
|
2018-07-26 22:12:13 +01:00
|
|
|
{
|
|
|
|
return $this->uri;
|
|
|
|
}
|
|
|
|
|
2018-07-29 02:35:04 +01:00
|
|
|
/**
|
|
|
|
* Getter for url property
|
|
|
|
*
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @return string URL
|
|
|
|
*/
|
|
|
|
public function getUrl()
|
|
|
|
{
|
|
|
|
return $this->getUri();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter for id property
|
|
|
|
*
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @return int32
|
|
|
|
*/
|
|
|
|
public function getID()
|
|
|
|
{
|
|
|
|
return $this->profile_id;
|
|
|
|
}
|
|
|
|
|
2018-07-26 22:12:13 +01:00
|
|
|
/**
|
|
|
|
* Ensures a valid Activitypub_profile when provided with a valid URI.
|
|
|
|
*
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @param string $url
|
|
|
|
* @return Activitypub_profile
|
|
|
|
* @throws Exception if it isn't possible to return an Activitypub_profile
|
|
|
|
*/
|
2018-07-29 02:35:04 +01:00
|
|
|
public static function fromUri($url)
|
2018-07-26 22:12:13 +01:00
|
|
|
{
|
|
|
|
$explorer = new Activitypub_explorer();
|
|
|
|
$profiles_found = $explorer->lookup($url);
|
|
|
|
if (!empty($profiles_found)) {
|
|
|
|
return self::from_profile($profiles_found[0]);
|
|
|
|
} else {
|
|
|
|
throw new Exception('No valid ActivityPub profile found for given URI.');
|
2018-07-20 16:04:17 +01:00
|
|
|
}
|
2018-07-26 22:12:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Look up, and if necessary create, an Activitypub_profile for the remote
|
|
|
|
* entity with the given webfinger address.
|
|
|
|
* This should never return null -- you will either get an object or
|
|
|
|
* an exception will be thrown.
|
|
|
|
*
|
|
|
|
* @author GNU Social
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @param string $addr webfinger address
|
|
|
|
* @return Activitypub_profile
|
|
|
|
* @throws Exception on error conditions
|
|
|
|
*/
|
|
|
|
public static function ensure_web_finger($addr)
|
|
|
|
{
|
|
|
|
// Normalize $addr, i.e. add 'acct:' if missing
|
|
|
|
$addr = Discovery::normalize($addr);
|
|
|
|
|
|
|
|
// Try the cache
|
|
|
|
$uri = self::cacheGet(sprintf('activitypub_profile:webfinger:%s', $addr));
|
|
|
|
|
|
|
|
if ($uri !== false) {
|
|
|
|
if (is_null($uri)) {
|
|
|
|
// Negative cache entry
|
|
|
|
// TRANS: Exception.
|
|
|
|
throw new Exception(_m('Not a valid webfinger address (via cache).'));
|
|
|
|
}
|
|
|
|
try {
|
2018-07-29 02:35:04 +01:00
|
|
|
return self::fromUri($uri);
|
2018-07-26 22:12:13 +01:00
|
|
|
} catch (Exception $e) {
|
|
|
|
common_log(LOG_ERR, sprintf(__METHOD__ . ': Webfinger address cache inconsistent with database, did not find Activitypub_profile uri==%s', $uri));
|
|
|
|
self::cacheSet(sprintf('activitypub_profile:webfinger:%s', $addr), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, try some discovery
|
|
|
|
|
|
|
|
$disco = new Discovery();
|
2018-07-20 16:04:17 +01:00
|
|
|
|
2018-07-26 22:12:13 +01:00
|
|
|
try {
|
|
|
|
$xrd = $disco->lookup($addr);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// Save negative cache entry so we don't waste time looking it up again.
|
|
|
|
// @todo FIXME: Distinguish temporary failures?
|
|
|
|
self::cacheSet(sprintf('activitypub_profile:webfinger:%s', $addr), null);
|
|
|
|
// TRANS: Exception.
|
|
|
|
throw new Exception(_m('Not a valid webfinger address.'));
|
2018-07-20 16:04:17 +01:00
|
|
|
}
|
|
|
|
|
2018-07-26 22:12:13 +01:00
|
|
|
$hints = array_merge(
|
|
|
|
array('webfinger' => $addr),
|
|
|
|
DiscoveryHints::fromXRD($xrd)
|
|
|
|
);
|
|
|
|
|
|
|
|
// If there's an Hcard, let's grab its info
|
|
|
|
if (array_key_exists('hcard', $hints)) {
|
|
|
|
if (!array_key_exists('profileurl', $hints) ||
|
2018-07-20 16:04:17 +01:00
|
|
|
$hints['hcard'] != $hints['profileurl']) {
|
2018-07-26 22:12:13 +01:00
|
|
|
$hcardHints = DiscoveryHints::fromHcardUrl($hints['hcard']);
|
|
|
|
$hints = array_merge($hcardHints, $hints);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got a profile page, try that!
|
|
|
|
$profileUrl = null;
|
|
|
|
if (array_key_exists('profileurl', $hints)) {
|
|
|
|
$profileUrl = $hints['profileurl'];
|
|
|
|
try {
|
|
|
|
common_log(LOG_INFO, "Discovery on acct:$addr with profile URL $profileUrl");
|
2018-07-29 02:35:04 +01:00
|
|
|
$aprofile = self::fromUri($hints['profileurl']);
|
|
|
|
self::cacheSet(sprintf('activitypub_profile:webfinger:%s', $addr), $aprofile->getUri());
|
2018-07-26 22:12:13 +01:00
|
|
|
return $aprofile;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
common_log(LOG_WARNING, "Failed creating profile from profile URL '$profileUrl': " . $e->getMessage());
|
|
|
|
// keep looking
|
2018-07-20 16:04:17 +01:00
|
|
|
//
|
|
|
|
// @todo FIXME: This means an error discovering from profile page
|
|
|
|
// may give us a corrupt entry using the webfinger URI, which
|
|
|
|
// will obscure the correct page-keyed profile later on.
|
2018-07-26 22:12:13 +01:00
|
|
|
}
|
|
|
|
}
|
2018-07-20 16:04:17 +01:00
|
|
|
|
2018-07-26 22:12:13 +01:00
|
|
|
// XXX: try hcard
|
|
|
|
// XXX: try FOAF
|
2018-07-20 16:04:17 +01:00
|
|
|
|
2018-07-26 22:12:13 +01:00
|
|
|
// TRANS: Exception. %s is a webfinger address.
|
|
|
|
throw new Exception(sprintf(_m('Could not find a valid profile for "%s".'), $addr));
|
|
|
|
}
|
2018-05-05 00:54:41 +01:00
|
|
|
}
|