123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- <?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
- if (!$this->validate_activity()) {
- return; // Just ignore
- }
- // 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.
- *
- * @throws Exception if invalid
- * @return bool true if valid and acceptable, false if unsupported
- * @author Diogo Cordeiro <diogo@fc.up.pt>
- */
- private function validate_activity(): bool
- {
- // 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
- $valid = true;
- switch ($this->activity['type']) {
- case 'Accept':
- $valid = Activitypub_accept::validate_object($this->object);
- break;
- case 'Create':
- $valid = Activitypub_create::validate_object($this->object);
- break;
- case 'Delete':
- $valid = Activitypub_delete::validate_object($this->object);
- break;
- 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':
- $valid = Activitypub_undo::validate_object($this->object);
- break;
- default:
- throw new Exception('Unknown Activity Type.');
- }
- return $valid;
- }
- /**
- * Sends the Activity to proper handler in order to be processed.
- *
- * @throws AlreadyFulfilledException
- * @throws HTTP_Request2_Exception
- * @throws NoProfileException
- * @throws ServerException
- * @throws Exception
- * @author Diogo Cordeiro <diogo@fc.up.pt>
- */
- private function process()
- {
- switch ($this->activity['type']) {
- case 'Accept':
- $this->handle_accept();
- break;
- case 'Create':
- $this->handle_create();
- break;
- case 'Delete':
- $this->handle_delete();
- break;
- case 'Follow':
- $this->handle_follow();
- break;
- case 'Like':
- $this->handle_like();
- break;
- case 'Undo':
- $this->handle_undo();
- break;
- case 'Announce':
- $this->handle_announce();
- break;
- }
- }
- /**
- * Handles an Accept Activity received by our inbox.
- *
- * @throws HTTP_Request2_Exception
- * @throws NoProfileException
- * @throws ServerException
- * @author Diogo Cordeiro <diogo@fc.up.pt>
- */
- private function handle_accept()
- {
- switch ($this->object['type']) {
- case 'Follow':
- $this->handle_accept_follow();
- break;
- }
- }
- /**
- * Handles an Accept Follow Activity received by our inbox.
- *
- * @throws HTTP_Request2_Exception
- * @throws NoProfileException
- * @throws ServerException
- * @throws Exception
- * @author Diogo Cordeiro <diogo@fc.up.pt>
- */
- private function handle_accept_follow()
- {
- // Get valid Object profile
- // Note that, since this an accept_follow, the $object
- // profile is actually the actor that followed someone
- $object_profile = new Activitypub_explorer;
- $object_profile = $object_profile->lookup($this->object['object'])[0];
- Activitypub_profile::subscribeCacheUpdate($object_profile, $this->actor);
- $pending_list = new Activitypub_pending_follow_requests($object_profile->getID(), $this->actor->getID());
- $pending_list->remove();
- }
- /**
- * Handles a Create Activity received by our inbox.
- *
- * @throws Exception
- * @author Diogo Cordeiro <diogo@fc.up.pt>
- */
- private function handle_create()
- {
- switch ($this->object['type']) {
- case 'Note':
- $this->handle_create_note();
- break;
- }
- }
- /**
- * Handle a Create Note Activity received by our inbox.
- *
- * @throws Exception
- * @author Bruno Casteleiro <brunoccast@fc.up.pt>
- */
- private function handle_create_note()
- {
- if (Activitypub_create::isPrivateNote($this->activity)) {
- Activitypub_message::create_message($this->object, $this->actor);
- } else {
- Activitypub_notice::create_notice($this->object, $this->actor);
- }
- }
- /**
- * Handles a Delete Activity received by our inbox.
- *
- * @author Bruno Casteleiro <brunoccast@fc.up.pt>
- * @author Diogo Cordeiro <diogo@fc.up.pt>
- */
- private function handle_delete()
- {
- $object = $this->object;
- if (is_string($object)) {
- $client = new HTTPClient();
- $response = $client->get($object, ACTIVITYPUB_HTTP_CLIENT_HEADERS);
- $not_gone = $response->isOk();
- if ($not_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 'Note': // XXX: We do not support updating a note's contents so, we'll delete and re-fetch for now...
- try {
- $notice = ActivityPubPlugin::grab_notice_from_url($object['id'], false);
- if ($notice instanceof Notice) {
- $notice->delete();
- }
- ActivityPubPlugin::grab_notice_from_url($object['id'], true);
- return;
- } catch (Exception $e) {
- // either already deleted or not an object at all
- // nothing to do..
- }
- break;
- default:
- common_log(LOG_INFO, "Ignoring Delete activity, we do not understand for {$object['type']}.");
- }
- }
- }
- // IFF we reached this point, it either is gone or it's an array
- // If it's gone, we don't know the type of the deleted object, we only have a Tombstone
- // If we were given an array, we don't know if it's Gone or not via status code...
- // In both cases, we will want to fetch the ID and act on that as it is easier than updating the fields
- $object = $object['id'] ?? null;
- if (is_null($object)) {
- return;
- }
- // Was it a profile?
- try {
- $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
- }
- // Was it a note?
- try {
- $client = new HTTPClient();
- /*$response =*/ $client->get($object, ACTIVITYPUB_HTTP_CLIENT_HEADERS);
- // If it were deleted
- //if (!$response->isOk()) { // 410 or 404
- $notice = ActivityPubPlugin::grab_notice_from_url($object, false);
- if ($notice instanceof Notice) {
- $notice->delete();
- }
- // } else
- ActivityPubPlugin::grab_notice_from_url($object, true);
- // XXX: We do not support updating a note's contents so, we'll delete and re-fetch for now...
- } catch (Exception $e) {
- // Means we didn't have this note already
- // Or we had, deleted and it exploded trying to fetch the Tombstone, either way, we're good.
- }
- }
- /**
- * 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>
- */
- private function handle_follow()
- {
- Activitypub_follow::follow($this->actor, $this->object, $this->activity['id']);
- }
- /**
- * Handles a Like Activity received by our inbox.
- *
- * @throws Exception
- * @author Diogo Cordeiro <diogo@fc.up.pt>
- */
- private function handle_like()
- {
- $notice = ActivityPubPlugin::grab_notice_from_url($this->object);
- Activitypub_like::addNew($this->activity['id'], $this->actor, $notice);
- }
- /**
- * 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>
- */
- private function handle_undo()
- {
- switch ($this->object['type']) {
- case 'Follow':
- $this->handle_undo_follow();
- break;
- case 'Like':
- $this->handle_undo_like();
- break;
- }
- }
- /**
- * Handles a Undo Follow Activity received by our inbox.
- *
- * @throws AlreadyFulfilledException
- * @throws HTTP_Request2_Exception
- * @throws NoProfileException
- * @throws ServerException
- * @throws Exception
- * @author Diogo Cordeiro <diogo@fc.up.pt>
- */
- private function handle_undo_follow()
- {
- // Get Object profile
- $object_profile = new Activitypub_explorer;
- $object_profile = $object_profile->lookup($this->object['object'])[0];
- if (Subscription::exists($this->actor, $object_profile)) {
- Subscription::cancel($this->actor, $object_profile);
- // You are no longer following this person.
- Activitypub_profile::unsubscribeCacheUpdate($this->actor, $object_profile);
- } /*else {
- // 409: You already aren't following this person.
- }*/
- }
- /**
- * Handles a Undo Like Activity received by our inbox.
- *
- * @throws AlreadyFulfilledException
- * @throws ServerException
- * @throws Exception
- * @author Diogo Cordeiro <diogo@fc.up.pt>
- */
- private function handle_undo_like()
- {
- $notice = ActivityPubPlugin::grab_notice_from_url($this->activity['id']);
- Fave::removeEntry($this->actor, $notice);
- }
- /**
- * Handles a Announce Activity received by our inbox.
- *
- * @throws Exception
- * @author Diogo Cordeiro <diogo@fc.up.pt>
- */
- private function handle_announce()
- {
- $notice = ActivityPubPlugin::grab_notice_from_url($this->object);
- Activitypub_announce::repeat($this->activity['id'], $this->actor, $notice);
- }
- }
|