2019-05-11 12:27:21 +01: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
|
|
|
|
$this->validate_activity();
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
*
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
private function validate_activity()
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
switch ($this->activity['type']) {
|
|
|
|
case 'Accept':
|
|
|
|
Activitypub_accept::validate_object($this->object);
|
|
|
|
break;
|
|
|
|
case 'Create':
|
|
|
|
Activitypub_create::validate_object($this->object);
|
|
|
|
break;
|
|
|
|
case 'Delete':
|
2019-08-08 17:21:56 +01:00
|
|
|
Activitypub_delete::validate_object($this->object);
|
|
|
|
break;
|
2019-05-11 12:27:21 +01: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':
|
|
|
|
Activitypub_undo::validate_object($this->object);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception('Unknown Activity Type.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends the Activity to proper handler in order to be processed.
|
|
|
|
*
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
|
|
|
private function process()
|
|
|
|
{
|
|
|
|
switch ($this->activity['type']) {
|
|
|
|
case 'Accept':
|
2019-08-13 00:05:51 +01:00
|
|
|
$this->handle_accept();
|
2019-05-11 12:27:21 +01:00
|
|
|
break;
|
|
|
|
case 'Create':
|
2019-08-13 00:05:51 +01:00
|
|
|
$this->handle_create();
|
2019-05-11 12:27:21 +01:00
|
|
|
break;
|
|
|
|
case 'Delete':
|
2019-08-13 00:05:51 +01:00
|
|
|
$this->handle_delete();
|
2019-05-11 12:27:21 +01:00
|
|
|
break;
|
|
|
|
case 'Follow':
|
2019-08-13 00:05:51 +01:00
|
|
|
$this->handle_follow();
|
2019-05-11 12:27:21 +01:00
|
|
|
break;
|
|
|
|
case 'Like':
|
2019-08-13 00:05:51 +01:00
|
|
|
$this->handle_like();
|
2019-05-11 12:27:21 +01:00
|
|
|
break;
|
|
|
|
case 'Undo':
|
2019-08-13 00:05:51 +01:00
|
|
|
$this->handle_undo();
|
2019-05-11 12:27:21 +01:00
|
|
|
break;
|
|
|
|
case 'Announce':
|
2019-08-13 00:05:51 +01:00
|
|
|
$this->handle_announce();
|
2019-05-11 12:27:21 +01: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 00:05:51 +01:00
|
|
|
private function handle_accept()
|
2019-05-11 12:27:21 +01:00
|
|
|
{
|
2019-08-13 00:05:51 +01:00
|
|
|
switch ($this->object['type']) {
|
2019-05-11 12:27:21 +01:00
|
|
|
case 'Follow':
|
2019-08-13 00:05:51 +01:00
|
|
|
$this->handle_accept_follow();
|
2019-05-11 12:27:21 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles an Accept Follow Activity received by our inbox.
|
|
|
|
*
|
|
|
|
* @throws HTTP_Request2_Exception
|
|
|
|
* @throws NoProfileException
|
|
|
|
* @throws ServerException
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
2019-08-13 00:05:51 +01:00
|
|
|
private function handle_accept_follow()
|
2019-05-11 12:27:21 +01:00
|
|
|
{
|
|
|
|
// Get valid Object profile
|
2019-07-08 19:23:48 +01:00
|
|
|
// Note that, since this an accept_follow, the $object
|
|
|
|
// profile is actually the actor that followed someone
|
2019-05-11 12:27:21 +01:00
|
|
|
$object_profile = new Activitypub_explorer;
|
2019-08-13 00:05:51 +01:00
|
|
|
$object_profile = $object_profile->lookup($this->object['object'])[0];
|
2019-05-11 12:27:21 +01:00
|
|
|
|
2019-08-13 00:05:51 +01:00
|
|
|
Activitypub_profile::subscribeCacheUpdate($object_profile, $this->actor);
|
2019-07-08 19:23:48 +01:00
|
|
|
|
2019-08-13 00:05:51 +01:00
|
|
|
$pending_list = new Activitypub_pending_follow_requests($object_profile->getID(), $this->actor->getID());
|
2019-05-11 12:27:21 +01:00
|
|
|
$pending_list->remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a Create Activity received by our inbox.
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
2019-08-13 00:05:51 +01:00
|
|
|
private function handle_create()
|
2019-05-11 12:27:21 +01:00
|
|
|
{
|
2019-08-13 00:05:51 +01:00
|
|
|
switch ($this->object['type']) {
|
2019-05-11 12:27:21 +01:00
|
|
|
case 'Note':
|
2019-08-13 00:05:51 +01:00
|
|
|
$this->handle_create_note();
|
2019-05-11 12:27:21 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 00:05:51 +01:00
|
|
|
/**
|
|
|
|
* Handle a Create Note Activity received by our inbox.
|
|
|
|
*
|
|
|
|
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
|
|
|
|
*/
|
|
|
|
private function handle_create_note()
|
|
|
|
{
|
|
|
|
if (Activitypub_notice::isPrivateNote($this->activity)) {
|
|
|
|
// Plugin DirectMessage must handle this
|
|
|
|
} else {
|
|
|
|
Activitypub_notice::create_notice($this->object, $this->actor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 12:27:21 +01:00
|
|
|
/**
|
|
|
|
* Handles a Delete Activity received by our inbox.
|
|
|
|
*
|
|
|
|
* @throws AuthorizationException
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
2019-08-13 00:05:51 +01:00
|
|
|
private function handle_delete()
|
2019-05-11 12:27:21 +01:00
|
|
|
{
|
2019-08-13 00:05:51 +01:00
|
|
|
$object = $this->object;
|
2019-08-08 17:21:56 +01:00
|
|
|
if (is_array($object)) {
|
|
|
|
$object = $object['id'];
|
|
|
|
}
|
|
|
|
|
2019-07-25 03:29:11 +01:00
|
|
|
// some moderator could already have deleted the
|
|
|
|
// notice, so we test it first
|
|
|
|
try {
|
|
|
|
$found = Deleted_notice::getByUri($object);
|
|
|
|
$deleted = ($found instanceof Deleted_notice);
|
|
|
|
} catch (NoResultException $e) {
|
|
|
|
$deleted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$deleted) {
|
|
|
|
$notice = ActivityPubPlugin::grab_notice_from_url($object);
|
2019-08-13 00:05:51 +01:00
|
|
|
$notice->deleteAs($this->actor);
|
2019-07-25 03:29:11 +01:00
|
|
|
}
|
2019-05-11 12:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a Follow Activity received by our inbox.
|
|
|
|
*
|
|
|
|
* @throws AlreadyFulfilledException
|
|
|
|
* @throws HTTP_Request2_Exception
|
|
|
|
* @throws NoProfileException
|
|
|
|
* @throws ServerException
|
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
|
|
|
* @throws \HttpSignatures\Exception
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
2019-08-13 00:05:51 +01:00
|
|
|
private function handle_follow()
|
2019-05-11 12:27:21 +01:00
|
|
|
{
|
2019-08-13 00:05:51 +01:00
|
|
|
Activitypub_follow::follow($this->actor, $this->object, $this->activity['id']);
|
2019-05-11 12:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a Like Activity received by our inbox.
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
2019-08-13 00:05:51 +01:00
|
|
|
private function handle_like()
|
2019-05-11 12:27:21 +01:00
|
|
|
{
|
2019-08-13 00:05:51 +01:00
|
|
|
$notice = ActivityPubPlugin::grab_notice_from_url($this->object);
|
|
|
|
Fave::addNew($this->actor, $notice);
|
2019-05-11 12:27:21 +01: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 00:05:51 +01:00
|
|
|
private function handle_undo()
|
2019-05-11 12:27:21 +01:00
|
|
|
{
|
2019-08-13 00:05:51 +01:00
|
|
|
switch ($this->object['type']) {
|
2019-05-11 12:27:21 +01:00
|
|
|
case 'Follow':
|
2019-08-13 00:05:51 +01:00
|
|
|
$this->handle_undo_follow();
|
2019-05-11 12:27:21 +01:00
|
|
|
break;
|
|
|
|
case 'Like':
|
2019-08-13 00:05:51 +01:00
|
|
|
$this->handle_undo_like();
|
2019-05-11 12:27:21 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a Undo Follow Activity received by our inbox.
|
|
|
|
*
|
|
|
|
* @throws AlreadyFulfilledException
|
|
|
|
* @throws HTTP_Request2_Exception
|
|
|
|
* @throws NoProfileException
|
|
|
|
* @throws ServerException
|
2019-08-13 00:05:51 +01:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
2019-05-11 12:27:21 +01:00
|
|
|
*/
|
2019-08-13 00:05:51 +01:00
|
|
|
private function handle_undo_follow()
|
2019-05-11 12:27:21 +01:00
|
|
|
{
|
|
|
|
// Get Object profile
|
|
|
|
$object_profile = new Activitypub_explorer;
|
2019-08-13 00:05:51 +01:00
|
|
|
$object_profile = $object_profile->lookup($this->object['object'])[0];
|
2019-05-11 12:27:21 +01:00
|
|
|
|
2019-08-13 00:05:51 +01:00
|
|
|
if (Subscription::exists($this->actor, $object_profile)) {
|
|
|
|
Subscription::cancel($this->actor, $object_profile);
|
2019-07-08 19:23:48 +01:00
|
|
|
// You are no longer following this person.
|
2019-08-13 00:05:51 +01:00
|
|
|
Activitypub_profile::unsubscribeCacheUpdate($this->actor, $object_profile);
|
2019-05-11 12:27:21 +01:00
|
|
|
} else {
|
|
|
|
// 409: You are not following this person already.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-13 00:05:51 +01:00
|
|
|
* Handles a Undo Like Activity received by our inbox.
|
2019-05-11 12:27:21 +01:00
|
|
|
*
|
2019-08-13 00:05:51 +01:00
|
|
|
* @throws AlreadyFulfilledException
|
|
|
|
* @throws ServerException
|
2019-05-11 12:27:21 +01:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
2019-08-13 00:05:51 +01:00
|
|
|
*/
|
|
|
|
private function handle_undo_like()
|
|
|
|
{
|
|
|
|
$notice = ActivityPubPlugin::grab_notice_from_url($this->object['object']);
|
|
|
|
Fave::removeEntry($this->actor, $notice);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a Announce Activity received by our inbox.
|
|
|
|
*
|
2019-05-11 12:27:21 +01:00
|
|
|
* @throws Exception
|
2019-08-13 00:05:51 +01:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
2019-05-11 12:27:21 +01:00
|
|
|
*/
|
2019-08-13 00:05:51 +01:00
|
|
|
private function handle_announce()
|
2019-05-11 12:27:21 +01:00
|
|
|
{
|
2019-08-13 00:05:51 +01:00
|
|
|
$object_notice = ActivityPubPlugin::grab_notice_from_url($this->object);
|
|
|
|
$object_notice->repeat($this->actor, 'ActivityPub');
|
2019-05-11 12:27:21 +01:00
|
|
|
}
|
|
|
|
}
|