2019-05-11 20:27:21 +09:00
|
|
|
<?php
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ActivityPub implementation for GNU social
|
|
|
|
*
|
|
|
|
* @package GNUsocial
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
|
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
* @link http://www.gnu.org/software/social/
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined('GNUSOCIAL') || die();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ActivityPub Inbox Handler
|
|
|
|
*
|
|
|
|
* @category Plugin
|
|
|
|
* @package GNUsocial
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
|
|
|
class Activitypub_inbox_handler
|
|
|
|
{
|
|
|
|
private $activity;
|
|
|
|
private $actor;
|
|
|
|
private $object;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a Inbox Handler to receive something from someone.
|
|
|
|
*
|
|
|
|
* @param array $activity Activity we are receiving
|
|
|
|
* @param Profile $actor_profile Actor originating the activity
|
|
|
|
* @throws Exception
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
|
|
|
public function __construct($activity, $actor_profile = null)
|
|
|
|
{
|
|
|
|
$this->activity = $activity;
|
|
|
|
$this->object = $activity['object'];
|
|
|
|
|
|
|
|
// Validate Activity
|
2019-10-12 03:09:08 +09:00
|
|
|
if (!$this->validate_activity()) {
|
|
|
|
return; // Just ignore
|
|
|
|
}
|
2019-05-11 20:27:21 +09:00
|
|
|
|
|
|
|
// Get Actor's Profile
|
|
|
|
if (!is_null($actor_profile)) {
|
|
|
|
$this->actor = $actor_profile;
|
|
|
|
} else {
|
|
|
|
$this->actor = ActivityPub_explorer::get_profile_from_url($this->activity['actor']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the Activity
|
|
|
|
$this->process();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates if a given Activity is valid. Throws exception if not.
|
|
|
|
*
|
2019-10-12 03:09:08 +09:00
|
|
|
* @throws Exception if invalid
|
|
|
|
* @return bool true if valid and acceptable, false if unsupported
|
2019-10-12 01:18:29 +09:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
2019-05-11 20:27:21 +09:00
|
|
|
*/
|
2019-10-12 03:09:08 +09:00
|
|
|
private function validate_activity(): bool
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
|
|
|
// Activity validation
|
|
|
|
// Validate data
|
|
|
|
if (!(isset($this->activity['type']))) {
|
|
|
|
throw new Exception('Activity Validation Failed: Type was not specified.');
|
|
|
|
}
|
|
|
|
if (!isset($this->activity['actor'])) {
|
|
|
|
throw new Exception('Activity Validation Failed: Actor was not specified.');
|
|
|
|
}
|
|
|
|
if (!isset($this->activity['object'])) {
|
|
|
|
throw new Exception('Activity Validation Failed: Object was not specified.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Object validation
|
2019-10-12 03:09:08 +09:00
|
|
|
$valid = true;
|
2019-05-11 20:27:21 +09:00
|
|
|
switch ($this->activity['type']) {
|
|
|
|
case 'Accept':
|
2019-10-18 04:25:00 +09:00
|
|
|
$valid = Activitypub_accept::validate_object($this->object);
|
2019-05-11 20:27:21 +09:00
|
|
|
break;
|
|
|
|
case 'Create':
|
2019-10-18 04:25:00 +09:00
|
|
|
$valid = Activitypub_create::validate_object($this->object);
|
2019-05-11 20:27:21 +09:00
|
|
|
break;
|
|
|
|
case 'Delete':
|
2019-10-18 04:25:00 +09:00
|
|
|
$valid = Activitypub_delete::validate_object($this->object);
|
2019-08-09 01:21:56 +09:00
|
|
|
break;
|
2019-05-11 20:27:21 +09:00
|
|
|
case 'Follow':
|
|
|
|
case 'Like':
|
|
|
|
case 'Announce':
|
|
|
|
if (!filter_var($this->object, FILTER_VALIDATE_URL)) {
|
|
|
|
throw new Exception('Object is not a valid Object URI for Activity.');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'Undo':
|
2019-10-18 04:25:00 +09:00
|
|
|
$valid = Activitypub_undo::validate_object($this->object);
|
2019-05-11 20:27:21 +09:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception('Unknown Activity Type.');
|
|
|
|
}
|
2019-10-12 03:09:08 +09:00
|
|
|
|
|
|
|
return $valid;
|
2019-05-11 20:27:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends the Activity to proper handler in order to be processed.
|
|
|
|
*
|
2019-10-12 01:18:29 +09:00
|
|
|
* @throws AlreadyFulfilledException
|
|
|
|
* @throws HTTP_Request2_Exception
|
|
|
|
* @throws NoProfileException
|
|
|
|
* @throws ServerException
|
|
|
|
* @throws Exception
|
2019-05-11 20:27:21 +09:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
|
|
|
private function process()
|
|
|
|
{
|
|
|
|
switch ($this->activity['type']) {
|
|
|
|
case 'Accept':
|
2019-08-13 08:05:51 +09:00
|
|
|
$this->handle_accept();
|
2019-05-11 20:27:21 +09:00
|
|
|
break;
|
|
|
|
case 'Create':
|
2019-08-13 08:05:51 +09:00
|
|
|
$this->handle_create();
|
2019-05-11 20:27:21 +09:00
|
|
|
break;
|
|
|
|
case 'Delete':
|
2019-08-13 08:05:51 +09:00
|
|
|
$this->handle_delete();
|
2019-05-11 20:27:21 +09:00
|
|
|
break;
|
|
|
|
case 'Follow':
|
2019-08-13 08:05:51 +09:00
|
|
|
$this->handle_follow();
|
2019-05-11 20:27:21 +09:00
|
|
|
break;
|
|
|
|
case 'Like':
|
2019-08-13 08:05:51 +09:00
|
|
|
$this->handle_like();
|
2019-05-11 20:27:21 +09:00
|
|
|
break;
|
|
|
|
case 'Undo':
|
2019-08-13 08:05:51 +09:00
|
|
|
$this->handle_undo();
|
2019-05-11 20:27:21 +09:00
|
|
|
break;
|
|
|
|
case 'Announce':
|
2019-08-13 08:05:51 +09:00
|
|
|
$this->handle_announce();
|
2019-05-11 20:27:21 +09:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles an Accept Activity received by our inbox.
|
|
|
|
*
|
|
|
|
* @throws HTTP_Request2_Exception
|
|
|
|
* @throws NoProfileException
|
|
|
|
* @throws ServerException
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
2019-08-13 08:05:51 +09:00
|
|
|
private function handle_accept()
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
2019-08-13 08:05:51 +09:00
|
|
|
switch ($this->object['type']) {
|
2019-05-11 20:27:21 +09:00
|
|
|
case 'Follow':
|
2019-08-13 08:05:51 +09:00
|
|
|
$this->handle_accept_follow();
|
2019-05-11 20:27:21 +09:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles an Accept Follow Activity received by our inbox.
|
|
|
|
*
|
|
|
|
* @throws HTTP_Request2_Exception
|
|
|
|
* @throws NoProfileException
|
|
|
|
* @throws ServerException
|
2019-10-12 01:18:29 +09:00
|
|
|
* @throws Exception
|
2019-05-11 20:27:21 +09:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
2019-08-13 08:05:51 +09:00
|
|
|
private function handle_accept_follow()
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
|
|
|
// Get valid Object profile
|
2019-07-09 03:23:48 +09:00
|
|
|
// Note that, since this an accept_follow, the $object
|
|
|
|
// profile is actually the actor that followed someone
|
2019-05-11 20:27:21 +09:00
|
|
|
$object_profile = new Activitypub_explorer;
|
2019-08-13 08:05:51 +09:00
|
|
|
$object_profile = $object_profile->lookup($this->object['object'])[0];
|
2019-05-11 20:27:21 +09:00
|
|
|
|
2019-08-13 08:05:51 +09:00
|
|
|
Activitypub_profile::subscribeCacheUpdate($object_profile, $this->actor);
|
2019-07-09 03:23:48 +09:00
|
|
|
|
2019-08-13 08:05:51 +09:00
|
|
|
$pending_list = new Activitypub_pending_follow_requests($object_profile->getID(), $this->actor->getID());
|
2019-05-11 20:27:21 +09:00
|
|
|
$pending_list->remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a Create Activity received by our inbox.
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
2019-08-13 08:05:51 +09:00
|
|
|
private function handle_create()
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
2019-08-13 08:05:51 +09:00
|
|
|
switch ($this->object['type']) {
|
2019-05-11 20:27:21 +09:00
|
|
|
case 'Note':
|
2019-08-13 08:05:51 +09:00
|
|
|
$this->handle_create_note();
|
2019-05-11 20:27:21 +09:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 08:05:51 +09:00
|
|
|
/**
|
|
|
|
* Handle a Create Note Activity received by our inbox.
|
|
|
|
*
|
2019-10-12 01:18:29 +09:00
|
|
|
* @throws Exception
|
2019-08-13 08:05:51 +09:00
|
|
|
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
|
|
|
|
*/
|
|
|
|
private function handle_create_note()
|
|
|
|
{
|
2019-08-20 07:33:18 +09:00
|
|
|
if (Activitypub_create::isPrivateNote($this->activity)) {
|
|
|
|
Activitypub_message::create_message($this->object, $this->actor);
|
2019-08-13 08:05:51 +09:00
|
|
|
} else {
|
|
|
|
Activitypub_notice::create_notice($this->object, $this->actor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 20:27:21 +09:00
|
|
|
/**
|
|
|
|
* Handles a Delete Activity received by our inbox.
|
|
|
|
*
|
2019-10-12 01:18:29 +09:00
|
|
|
* @throws NoProfileException
|
2019-10-12 01:41:41 +09:00
|
|
|
* @throws Exception
|
|
|
|
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
|
2020-07-05 10:25:51 +09:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
2019-05-11 20:27:21 +09:00
|
|
|
*/
|
2019-08-13 08:05:51 +09:00
|
|
|
private function handle_delete()
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
2019-08-13 08:05:51 +09:00
|
|
|
$object = $this->object;
|
2020-07-05 10:25:51 +09:00
|
|
|
if (is_string($object)) {
|
|
|
|
$client = new HTTPClient();
|
|
|
|
$response = $client->get($object, ACTIVITYPUB_HTTP_CLIENT_HEADERS);
|
|
|
|
$gone = !$response->isOk();
|
|
|
|
if (!$gone) { // It's not gone, we're updating it.
|
|
|
|
$object = json_decode($response->getBody(), true);
|
|
|
|
switch ($object['type']) {
|
|
|
|
case 'Person':
|
|
|
|
try {
|
|
|
|
// Update profile if we already have a copy of it
|
|
|
|
$aprofile = Activitypub_profile::fromUri($object['id'], false);
|
|
|
|
Activitypub_profile::update_profile($aprofile, $object);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// Import profile if we don't
|
|
|
|
Activitypub_explorer::get_profile_from_url($object['id']);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'Tombstone':
|
|
|
|
try {
|
|
|
|
$notice = ActivityPubPlugin::grab_notice_from_url($object, false);
|
|
|
|
if ($notice instanceof Notice) {
|
|
|
|
$notice->delete();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// either already deleted or not an object at all
|
|
|
|
// nothing to do..
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'Note':
|
|
|
|
// XXX: We do not support updating a note's contents so, we'll ignore it for now...
|
|
|
|
default:
|
|
|
|
common_log(LOG_INFO, "Ignoring Delete activity, we do not understand for {$object['type']}.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We don't know the type of the deleted object :(
|
|
|
|
// Nor if it's gone or not.
|
|
|
|
try {
|
2020-08-06 01:33:16 +09:00
|
|
|
if (is_array($object)) {
|
|
|
|
$object = $object['id'];
|
|
|
|
}
|
2020-07-05 10:25:51 +09:00
|
|
|
$aprofile = Activitypub_profile::fromUri($object, false);
|
|
|
|
$res = Activitypub_explorer::get_remote_user_activity($object);
|
|
|
|
Activitypub_profile::update_profile($aprofile, $res);
|
|
|
|
return;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// Means this wasn't a profile
|
|
|
|
}
|
2019-10-12 01:18:29 +09:00
|
|
|
|
2020-07-05 10:25:51 +09:00
|
|
|
try {
|
|
|
|
$client = new HTTPClient();
|
|
|
|
$response = $client->get($object, ACTIVITYPUB_HTTP_CLIENT_HEADERS);
|
|
|
|
// If it was deleted
|
|
|
|
if ($response->getStatus() == 410) {
|
|
|
|
$notice = ActivityPubPlugin::grab_notice_from_url($object, false);
|
|
|
|
if ($notice instanceof Notice) {
|
|
|
|
$notice->delete();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We can't update a note's contents so, we'll ignore it for now...
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// Means we didn't have this note already
|
2019-08-27 23:37:01 +09:00
|
|
|
}
|
2020-07-05 10:25:51 +09:00
|
|
|
|
2019-08-27 23:37:01 +09:00
|
|
|
return;
|
2019-07-25 11:29:11 +09:00
|
|
|
}
|
2019-05-11 20:27:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a Follow Activity received by our inbox.
|
|
|
|
*
|
|
|
|
* @throws AlreadyFulfilledException
|
|
|
|
* @throws HTTP_Request2_Exception
|
|
|
|
* @throws NoProfileException
|
|
|
|
* @throws ServerException
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
2019-08-13 08:05:51 +09:00
|
|
|
private function handle_follow()
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
2019-08-13 08:05:51 +09:00
|
|
|
Activitypub_follow::follow($this->actor, $this->object, $this->activity['id']);
|
2019-05-11 20:27:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a Like Activity received by our inbox.
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
2019-08-13 08:05:51 +09:00
|
|
|
private function handle_like()
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
2019-08-13 08:05:51 +09:00
|
|
|
$notice = ActivityPubPlugin::grab_notice_from_url($this->object);
|
2020-08-28 09:12:40 +09:00
|
|
|
Activitypub_like::addNew($this->activity['id'], $this->actor, $notice);
|
2019-05-11 20:27:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a Undo Activity received by our inbox.
|
|
|
|
*
|
|
|
|
* @throws AlreadyFulfilledException
|
|
|
|
* @throws HTTP_Request2_Exception
|
|
|
|
* @throws NoProfileException
|
|
|
|
* @throws ServerException
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
2019-08-13 08:05:51 +09:00
|
|
|
private function handle_undo()
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
2019-08-13 08:05:51 +09:00
|
|
|
switch ($this->object['type']) {
|
2019-05-11 20:27:21 +09:00
|
|
|
case 'Follow':
|
2019-08-13 08:05:51 +09:00
|
|
|
$this->handle_undo_follow();
|
2019-05-11 20:27:21 +09:00
|
|
|
break;
|
|
|
|
case 'Like':
|
2019-08-13 08:05:51 +09:00
|
|
|
$this->handle_undo_like();
|
2019-05-11 20:27:21 +09:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a Undo Follow Activity received by our inbox.
|
|
|
|
*
|
|
|
|
* @throws AlreadyFulfilledException
|
|
|
|
* @throws HTTP_Request2_Exception
|
|
|
|
* @throws NoProfileException
|
|
|
|
* @throws ServerException
|
2019-10-12 01:18:29 +09:00
|
|
|
* @throws Exception
|
2019-08-13 08:05:51 +09:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
2019-05-11 20:27:21 +09:00
|
|
|
*/
|
2019-08-13 08:05:51 +09:00
|
|
|
private function handle_undo_follow()
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
|
|
|
// Get Object profile
|
|
|
|
$object_profile = new Activitypub_explorer;
|
2019-08-13 08:05:51 +09:00
|
|
|
$object_profile = $object_profile->lookup($this->object['object'])[0];
|
2019-05-11 20:27:21 +09:00
|
|
|
|
2019-08-13 08:05:51 +09:00
|
|
|
if (Subscription::exists($this->actor, $object_profile)) {
|
|
|
|
Subscription::cancel($this->actor, $object_profile);
|
2019-07-09 03:23:48 +09:00
|
|
|
// You are no longer following this person.
|
2019-08-13 08:05:51 +09:00
|
|
|
Activitypub_profile::unsubscribeCacheUpdate($this->actor, $object_profile);
|
2019-10-12 01:18:29 +09:00
|
|
|
} /*else {
|
|
|
|
// 409: You already aren't following this person.
|
|
|
|
}*/
|
2019-05-11 20:27:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-13 08:05:51 +09:00
|
|
|
* Handles a Undo Like Activity received by our inbox.
|
2019-05-11 20:27:21 +09:00
|
|
|
*
|
2019-08-13 08:05:51 +09:00
|
|
|
* @throws AlreadyFulfilledException
|
|
|
|
* @throws ServerException
|
2019-10-12 01:18:29 +09:00
|
|
|
* @throws Exception
|
2019-05-11 20:27:21 +09:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
2019-08-13 08:05:51 +09:00
|
|
|
*/
|
|
|
|
private function handle_undo_like()
|
|
|
|
{
|
2020-08-28 09:12:40 +09:00
|
|
|
$notice = ActivityPubPlugin::grab_notice_from_url($this->activity['id']);
|
2019-08-13 08:05:51 +09:00
|
|
|
Fave::removeEntry($this->actor, $notice);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a Announce Activity received by our inbox.
|
|
|
|
*
|
2019-05-11 20:27:21 +09:00
|
|
|
* @throws Exception
|
2019-08-13 08:05:51 +09:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
2019-05-11 20:27:21 +09:00
|
|
|
*/
|
2019-08-13 08:05:51 +09:00
|
|
|
private function handle_announce()
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
2020-08-28 09:12:40 +09:00
|
|
|
$notice = ActivityPubPlugin::grab_notice_from_url($this->object);
|
|
|
|
Activitypub_announce::repeat($this->activity['id'], $this->actor, $notice);
|
2019-05-11 20:27:21 +09:00
|
|
|
}
|
|
|
|
}
|