diff --git a/plugins/ActivityPub/ActivityPub.php b/plugins/ActivityPub/ActivityPub.php deleted file mode 100644 index 70f7c13da9..0000000000 --- a/plugins/ActivityPub/ActivityPub.php +++ /dev/null @@ -1,1102 +0,0 @@ -. - -// }}} - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @author Hugo Sales - * @copyright 2018-2021 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * - * @see http://www.gnu.org/software/social/ - */ - -namespace Plugin\ActivityPub; - -use App\Core\Module; - -// // Import plugin libs -// foreach (glob(__DIR__ . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . '*.php') as $filename) { -// require_once $filename; -// } -// // Import plugin models -// foreach (glob(__DIR__ . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'models' . DIRECTORY_SEPARATOR . '*.php') as $filename) { -// require_once $filename; -// } - -/** - * Adds ActivityPub support to GNU social when enabled - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class ActivityPub extends Module -{ - const PLUGIN_VERSION = '0.4.0alpha0'; - // So that this isn't hardcoded everywhere - const ACTIVITYPUB_PUBLIC_TO = [ - 'https://www.w3.org/ns/activitystreams#Public', - 'Public', - 'as:Public', - ]; - - const ACTIVITYPUB_HTTP_CLIENT_HEADERS = [ - 'Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"', - 'User-Agent: GNUsocialBot ' . GNUSOCIAL_VERSION . ' - https://gnusocial.network', - ]; - - /** - * Returns a Actor's URI from its local $profile - * Works both for local and remote users. - * This is a discovery event but it seems more logical to have it separated. - * This ensures that Profile->getUri() will always return the intended for a remote AP profile. - * - * @param Profile $profile Actor's local profile - * @param string &$uri I/O Actor's URI - * - * @author Diogo Cordeiro - * - * @return bool event hook - */ - public function onStartGetProfileUri(Profile $profile, &$uri): bool - { - $aprofile = Activitypub_profile::getKV('profile_id', $profile->id); - if ($aprofile instanceof Activitypub_profile) { - $uri = $aprofile->getUri(); - return false; - } - return true; - } - - /** - * Returns a notice from its URL. - * - * @param string $url Notice's URL - * @param bool $grab_online whether to try online grabbing, defaults to true - * - * @throws Exception This function or provides a Notice, null, or fails with exception - * - * @return null|Notice The Notice object - * - * @author Diogo Cordeiro - */ - public static function grab_notice_from_url(string $url, bool $grab_online = true): ?Notice - { - // Offline Grabbing - try { - // Look for a known remote notice - return Notice::getByUri($url); - } catch (Exception $e) { - // Look for a local notice (unfortunately GNU social doesn't - // provide this functionality natively) - try { - $candidate = Notice::getByID((int) substr($url, (strlen(common_local_url('apNotice', ['id' => 0])) - 1))); - if (common_local_url('apNotice', ['id' => $candidate->getID()]) === $url) { // Sanity check - return $candidate; - } else { - common_debug('ActivityPubPlugin Notice Grabber: ' . $candidate->getUrl() . ' is different of ' . $url); - } - } catch (Exception $e) { - common_debug('ActivityPubPlugin Notice Grabber: failed to find: ' . $url . ' offline.'); - } - } - - if ($grab_online) { - // Online Grabbing - $client = new HTTPClient(); - $response = $client->get($url, ACTIVITYPUB_HTTP_CLIENT_HEADERS); - $object = json_decode($response->getBody(), true); - if (Activitypub_notice::validate_note($object)) { - // Okay, we've found a valid note object! - // Now we need to find the Actor who authored it - // The right way would be to grab attributed to and check its outbox - // But that would be outright inefficient - // Hence, let's just compare the domain names... - if (isset($object['attributedTo'])) { - $acclaimed_actor_profile = ActivityPub_explorer::get_profile_from_url($object['attributedTo']); - } elseif (isset($object['actor'])) { - $acclaimed_actor_profile = ActivityPub_explorer::get_profile_from_url($object['actor']); - } else { - throw new Exception("A notice can't be created without an actor."); - } - if (parse_url($acclaimed_actor_profile->getUri(), PHP_URL_HOST) == parse_url($object['id'], PHP_URL_HOST)) { - return Activitypub_notice::create_notice($object, $acclaimed_actor_profile); - } else { - throw new Exception("The acclaimed actor didn't create this note."); - } - } else { - throw new Exception('Valid ActivityPub Notice object but unsupported by GNU social.'); - } - } - - common_debug('ActivityPubPlugin Notice Grabber: failed to find: ' . $url); - return null; - } - - /** - * Route/Reroute urls - * - * @param URLMapper $m - * - * @throws Exception - * - * @return void - */ - public function onRouterInitialized(URLMapper $m) - { - $acceptHeaders = [ - 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' => 0, - 'application/activity+json' => 1, - 'application/json' => 2, - 'application/ld+json' => 3, - ]; - - $m->connect( - 'user/:id', - ['action' => 'apActorProfile'], - ['id' => '[0-9]+'], - true, - $acceptHeaders - ); - - $m->connect( - ':nickname', - ['action' => 'apActorProfile'], - ['nickname' => Nickname::DISPLAY_FMT], - true, - $acceptHeaders - ); - - $m->connect( - ':nickname/', - ['action' => 'apActorProfile'], - ['nickname' => Nickname::DISPLAY_FMT], - true, - $acceptHeaders - ); - - // v3 - $m->connect( - 'activity/:id', - ['action' => 'apNotice'], - ['id' => '[0-9]+'], - ); - - // v2 - $m->connect( - 'notice/:id', - ['action' => 'apNotice'], - ['id' => '[0-9]+'], - true, - $acceptHeaders - ); - - $m->connect( - 'object/note/:id', - ['action' => 'apNotice'], - ['id' => '[0-9]+'], - ); - - $m->connect( - 'user/:id/liked.json', - ['action' => 'apActorLiked'], - ['id' => '[0-9]+'] - ); - - $m->connect( - 'user/:id/followers.json', - ['action' => 'apActorFollowers'], - ['id' => '[0-9]+'] - ); - - $m->connect( - 'user/:id/following.json', - ['action' => 'apActorFollowing'], - ['id' => '[0-9]+'] - ); - - $m->connect( - 'user/:id/inbox.json', - ['action' => 'apInbox'], - ['id' => '[0-9]+'] - ); - - $m->connect( - 'user/:id/outbox.json', - ['action' => 'apActorOutbox'], - ['id' => '[0-9]+'] - ); - - $m->connect( - 'inbox.json', - ['action' => 'apInbox'] - ); - } - - /** - * Plugin version information - * - * @param array $versions - * - * @return bool hook true - */ - public function onPluginVersion(array &$versions): bool - { - $versions[] = [ - 'name' => 'ActivityPub', - 'version' => self::PLUGIN_VERSION, - 'author' => 'Diogo Cordeiro', - 'homepage' => 'https://notabug.org/diogo/gnu-social/src/nightly/plugins/ActivityPub', - // TRANS: Plugin description. - 'rawdescription' => _m('Follow people across social networks that implement ' . - 'ActivityPub.'), - ]; - return true; - } - - /** - * Set up queue handlers for required interactions - * - * @param QueueManager $qm - * - * @return bool event hook return - */ - public function onEndInitializeQueueManager(QueueManager $qm): bool - { - // Notice distribution - $qm->connect('activitypub', 'ActivityPubQueueHandler'); - // Failed Notice distribution - $qm->connect('activitypub_failed', 'ActivityPubFailedQueueHandler'); - return true; - } - - /** - * Enqueue saved notices for distribution - * - * @param Notice $notice notice to be distributed - * @param array &$transports list of transports to queue for - * - * @return bool event hook return - */ - public function onStartEnqueueNotice(Notice $notice, array &$transports): bool - { - try { - $id = $notice->getID(); - - if ($id > 0) { - $transports[] = 'activitypub'; - $this->log(LOG_INFO, "Notice:{$id} queued for distribution"); - } - } catch (Exception $e) { - $this->log(LOG_ERR, 'Invalid notice, not queueing for distribution'); - } - - return true; - } - - /** - * Update notice before saving. - * We'll use this as a hack to maintain replies to unlisted/followers-only - * notices away from the public timelines. - * - * @param Notice &$notice notice to be saved - * - * @return bool event hook return - */ - public function onStartNoticeSave(Notice &$notice): bool - { - if ($notice->reply_to) { - try { - $parent = $notice->getParent(); - $is_local = (int) $parent->is_local; - - // if we're replying unlisted/followers-only notices received by AP - // or replying to replies of such notices, then we make sure to set - // the correct type flag. - if (($parent->source === 'ActivityPub' && $is_local === Notice::GATEWAY) || ($parent->source === 'web' && $is_local === Notice::LOCAL_NONPUBLIC)) { - $this->log(LOG_INFO, 'Enforcing type flag LOCAL_NONPUBLIC for new notice'); - $notice->is_local = Notice::LOCAL_NONPUBLIC; - } - } catch (NoParentNoticeException $e) { - // This is not a reply to something (has no parent) - } - } - - return true; - } - - /** - * Add AP-subscriptions for private messaging - * - * @param User $current current logged user - * @param array &$recipients - * - * @return void - */ - public function onFillDirectMessageRecipients(User $current, array &$recipients): void - { - try { - $subs = Activitypub_profile::getSubscribed($current->getProfile()); - foreach ($subs as $sub) { - if (!$sub->isLocal()) { // AP plugin adds AP users - try { - $value = 'profile:' . $sub->getID(); - $recipients[$value] = substr($sub->getAcctUri(), 5) . " [{$sub->getBestName()}]"; - } catch (ProfileNoAcctUriException $e) { - $recipients[$value] = '[?@?] ' . $e->profile->getBestName(); - } - } - } - } catch (NoResultException $e) { - // let it go - } - } - - /** - * Validate AP-recipients for profile page message action addition - * - * @param Profile $recipient - * - * @return bool hook return value - */ - public function onDirectMessageProfilePageActions(Profile $recipient): bool - { - $to = Activitypub_profile::getKV('profile_id', $recipient->getID()); - if ($to instanceof Activitypub_profile) { - return false; // we can validate this profile, signal it - } - - return true; - } - - /** - * Mark an ap_profile object for deletion - * - * @param Profile profile being deleted - * @param array &$related objects with same profile_id to be deleted - * - * @return void - */ - public function onProfileDeleteRelated(Profile $profile, array &$related): void - { - $related[] = 'Activitypub_profile'; - $related[] = 'Activitypub_rsa'; - - // pending_follow_requests doesn't have a profile_id column, - // so we must handle it manually - $follow = new Activitypub_pending_follow_requests(null, $profile->getID()); - - if ($follow->find()) { - while ($follow->fetch()) { - $follow->delete(); - } - } - } - - /** - * Plugin Nodeinfo information - * - * @param array $protocols - * - * @return bool hook true - */ - public function onNodeInfoProtocols(array &$protocols) - { - $protocols[] = 'activitypub'; - return true; - } - - /** - * Adds an indicator on Remote ActivityPub profiles. - * - * @param HTMLOutputter $out - * @param Profile $profile - * - * @throws Exception - * - * @return bool hook return value - * - * @author Diogo Cordeiro - */ - public function onEndShowAccountProfileBlock(HTMLOutputter $out, Profile $profile) - { - if ($profile->isLocal()) { - return true; - } - - $aprofile = Activitypub_profile::getKV('profile_id', $profile->getID()); - if (!$aprofile instanceof Activitypub_profile) { - // Not a remote ActivityPub_profile! Maybe some other network - // that has imported a non-local user (e.g.: OStatus)? - return true; - } - - $out->elementStart('dl', 'entity_tags activitypub_profile'); - $out->element('dt', null, 'ActivityPub'); - $out->element('dd', null, _m('Remote Profile')); - $out->elementEnd('dl'); - - return true; - } - - /** - * Hack the notice search-box and try to grab remote profiles or notices. - * - * Note that, on successful grabbing, this function will redirect to the - * new profile/notice, so URL searching is directly affected. A good solution - * for this is to store the URLs in the notice text without the https/http - * prefixes. This would change the queries for URL searching and therefore we - * could do both search and grab. - * - * @param string $query search query - * - * @return bool hook - * - * @author Bruno Casteleiro - */ - public function onStartNoticeSearch(string $query): bool - { - if (!common_logged_in()) { - // early return: Only allow logged users to import/search for remote actors or notes - return true; - } - - if (preg_match('!^((?:\w+\.)*\w+@(?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+)$!', $query)) { // WebFinger ID found! - // Try to grab remote actor - $aprofile = self::pull_remote_profile($query); - if ($aprofile instanceof Activitypub_profile) { - $url = common_local_url('userbyid', ['id' => $aprofile->getID()], null, null, false); - common_redirect($url, 303); - return false; - } - } elseif (filter_var($query, FILTER_VALIDATE_URL)) { // URL found! - // Is this an ActivityPub notice? - - // If we already know it, just return - try { - $notice = self::grab_notice_from_url($query, false); // Only check locally - if ($notice instanceof Notice) { - return true; - } - } catch (Exception $e) { - // We will next try online - } - - // Otherwise, try to grab it - try { - $notice = self::grab_notice_from_url($query); // Unfortunately we will be trying locally again - if ($notice instanceof Notice) { - $url = common_local_url('shownotice', ['notice' => $notice->getID()]); - common_redirect($url, 303); - } - } catch (Exception $e) { - // We will next check if this URL is an actor - } - - // Is this an ActivityPub actor? - - // If we already know it, just return - try { - $explorer = new Activitypub_explorer(); - $profile = $explorer->lookup($query, false)[0]; // Only check locally - if ($profile instanceof Profile) { - return true; - } - } catch (Exception $e) { - // We will next try online - } - - // Try to grab remote actor - try { - if (!isset($explorer)) { - $explorer = new Activitypub_explorer(); - } - $profile = $explorer->lookup($query)[0]; // Unfortunately we will be trying locally again - if ($profile instanceof Profile) { - $url = common_local_url('userbyid', ['id' => $profile->getID()], null, null, false); - common_redirect($url, 303); - return true; - } - } catch (Exception $e) { - // Let the search run naturally - } - } - return true; - } - - /** - * Make sure necessary tables are filled out. - * - * @return bool hook true - */ - public function onCheckSchema() - { - $schema = Schema::get(); - $schema->ensureTable('activitypub_profile', Activitypub_profile::schemaDef()); - $schema->ensureTable('activitypub_rsa', Activitypub_rsa::schemaDef()); - $schema->ensureTable('activitypub_pending_follow_requests', Activitypub_pending_follow_requests::schemaDef()); - return true; - } - - // WebFinger Events - - /** - * Get remote user's ActivityPub_profile via a identifier - * - * @param string $arg A remote user identifier - * - * @return null|Activitypub_profile Valid profile in success | null otherwise - * - * @author GNU social - * @author Diogo Cordeiro - */ - public static function pull_remote_profile($arg) - { - if (preg_match('!^((?:\w+\.)*\w+@(?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+)$!', $arg)) { - // webfinger lookup - try { - return Activitypub_profile::ensure_webfinger($arg); - } catch (Exception $e) { - common_log(LOG_ERR, 'Webfinger lookup failed for ' . - $arg . ': ' . $e->getMessage()); - } - } - - // Look for profile URLs, with or without scheme: - $urls = []; - if (preg_match('!^https?://((?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+(?:/\w+)+)$!', $arg)) { - $urls[] = $arg; - } - if (preg_match('!^((?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+(?:/\w+)+)$!', $arg)) { - $schemes = ['http', 'https']; - foreach ($schemes as $scheme) { - $urls[] = "{$scheme}://{$arg}"; - } - } - - foreach ($urls as $url) { - try { - return Activitypub_profile::fromUri($url); - } catch (Exception $e) { - common_log(LOG_ERR, 'Profile lookup failed for ' . - $arg . ': ' . $e->getMessage()); - } - } - return null; - } - - /** - * Webfinger matches: @user@example.com or even @user--one.george_orwell@1984.biz - * - * @author GNU social - * - * @param string $text The text from which to extract webfinger IDs - * @param string $preMention Character(s) that signals a mention ('@', '!'...) - * - * @return array The matching IDs (without $preMention) and each respective position in the given string. - */ - public static function extractWebfingerIds($text, $preMention = '@') - { - $wmatches = []; - $result = preg_match_all( - '/(? - */ - public function onEndWebFingerProfileLinks(XML_XRD $xrd, Managed_DataObject $object) - { - if ($object->isPerson()) { - $link = new XML_XRD_Element_Link( - 'self', - $object->getProfile()->getUri(), - 'application/activity+json' - ); - $xrd->links[] = clone $link; - } - } - - /** - * Find any explicit remote mentions. Accepted forms: - * Webfinger: @user@example.com - * Profile link: - * - * @param Profile $sender - * @param string $text input markup text - * @param $mentions - * - * @throws InvalidUrlException - * - * @return bool hook return value - * - * @author Diogo Cordeiro - * @example.com/mublog/user - * - * @author GNU social - */ - public function onEndFindMentions(Profile $sender, $text, &$mentions) - { - $matches = []; - - foreach (self::extractWebfingerIds($text, '@') as $wmatch) { - list($target, $pos) = $wmatch; - $this->log(LOG_INFO, "Checking webfinger person '{$target}'"); - $profile = null; - try { - $aprofile = Activitypub_profile::ensure_webfinger($target); - $profile = $aprofile->local_profile(); - } catch (Exception $e) { - $this->log(LOG_ERR, 'Webfinger check failed: ' . $e->getMessage()); - continue; - } - assert($profile instanceof Profile); - - $displayName = !empty($profile->nickname) && mb_strlen($profile->nickname) < mb_strlen($target) - ? $profile->getNickname() // TODO: we could do getBestName() or getFullname() here - : $target; - $url = $profile->getUri(); - if (!common_valid_http_url($url)) { - $url = $profile->getUrl(); - } - $matches[$pos] = ['mentioned' => [$profile], - 'type' => 'mention', - 'text' => $displayName, - 'position' => $pos, - 'length' => mb_strlen($target), - 'url' => $url, ]; - } - - foreach (self::extractUrlMentions($text) as $wmatch) { - list($target, $pos) = $wmatch; - $schemes = ['https', 'http']; - foreach ($schemes as $scheme) { - $url = "{$scheme}://{$target}"; - $this->log(LOG_INFO, "Checking profile address '{$url}'"); - try { - $aprofile = Activitypub_profile::fromUri($url); - $profile = $aprofile->local_profile(); - $displayName = !empty($profile->nickname) && mb_strlen($profile->nickname) < mb_strlen($target) ? - $profile->nickname : $target; - $matches[$pos] = ['mentioned' => [$profile], - 'type' => 'mention', - 'text' => $displayName, - 'position' => $pos, - 'length' => mb_strlen($target), - 'url' => $profile->getUrl(), ]; - break; - } catch (Exception $e) { - $this->log(LOG_ERR, 'Profile check failed: ' . $e->getMessage()); - } - } - } - - foreach ($mentions as $i => $other) { - // If we share a common prefix with a local user, override it! - $pos = $other['position']; - if (isset($matches[$pos])) { - $mentions[$i] = $matches[$pos]; - unset($matches[$pos]); - } - } - foreach ($matches as $mention) { - $mentions[] = $mention; - } - - return true; - } - - /** - * Allow remote profile references to be used in commands: - * sub update@status.net - * whois evan@identi.ca - * reply http://identi.ca/evan hey what's up - * - * @param Command $command - * @param string $arg - * @param Profile &$profile - * - * @return bool hook return code - * - * @author GNU social - * @author Diogo Cordeiro - */ - public function onStartCommandGetProfile($command, $arg, &$profile) - { - try { - $aprofile = $this->pull_remote_profile($arg); - $profile = $aprofile->local_profile(); - } catch (Exception $e) { - // No remote ActivityPub profile found - return true; - } - - return false; - } - - // Discovery Events - - /** - * Profile from URI. - * - * @author GNU social - * @author Diogo Cordeiro - * - * @param string $uri - * @param Profile &$profile in/out param: Profile got from URI - * - * @return mixed hook return code - */ - public function onStartGetProfileFromURI($uri, &$profile) - { - try { - $profile = Activitypub_explorer::get_profile_from_url($uri); - return false; - } catch (Exception $e) { - return true; // It's not an ActivityPub profile as far as we know, continue event handling - } - } - - /** - * Try to grab and store the remote profile by the given uri - * - * @param string $uri - * @param Profile &$profile - * - * @return bool - */ - public function onRemoteFollowPullProfile(string $uri, ?Profile &$profile): bool - { - $aprofile = self::pull_remote_profile($uri); - if ($aprofile instanceof Activitypub_profile) { - $profile = $aprofile->local_profile(); - } else { - // No remote ActivityPub profile found - return true; - } - - return is_null($profile); - } - - // Delivery Events - - /** - * Having established a remote subscription, send a notification to the - * remote ActivityPub profile's endpoint. - * - * @param Profile $profile subscriber - * @param Profile $other subscribee - * - * @throws HTTP_Request2_Exception - * - * @return bool return value - * - * @author Diogo Cordeiro - */ - public function onStartSubscribe(Profile $profile, Profile $other) - { - if (!$profile->isLocal()) { - return true; - } - - $other = Activitypub_profile::getKV('profile_id', $other->getID()); - if (!$other instanceof Activitypub_profile) { - return true; - } - - $postman = new Activitypub_postman($profile, [$other]); - $postman->follow(); - - return true; - } - - /** - * Notify remote server on unsubscribe. - * - * @param Profile $profile - * @param Profile $other - * - * @throws HTTP_Request2_Exception - * - * @return bool return value - * - * @author Diogo Cordeiro - */ - public function onStartUnsubscribe(Profile $profile, Profile $other) - { - if (!$profile->isLocal()) { - return true; - } - - $other = Activitypub_profile::getKV('profile_id', $other->getID()); - if (!$other instanceof Activitypub_profile) { - return true; - } - - $postman = new Activitypub_postman($profile, [$other]); - $postman->undo_follow(); - - return true; - } - - /** - * Notify remote users when their notices get de-favourited. - * - * @param Profile $profile of local user doing the de-faving - * @param Notice $notice Notice being favored - * @return bool return value - * @throws HTTP_Request2_Exception - * @throws InvalidUrlException - * @author Diogo Cordeiro - */ - public function onEndDisfavorNotice(Profile $profile, Notice $notice) - { - // Only distribute local users' favor actions, remote users - // will have already distributed theirs. - if (!$profile->isLocal()) { - return true; - } - - $other = []; - - try { - $other[] = Activitypub_profile::from_profile($notice->getProfile()); - } catch (Exception $e) { - // Local user can be ignored - } - - $other = array_merge( - $other, - Activitypub_profile::from_profile_collection( - $notice->getAttentionProfiles() - ) - ); - - if ($notice->reply_to) { - try { - $parent_notice = $notice->getParent(); - - try { - $other[] = Activitypub_profile::from_profile($parent_notice->getProfile()); - } catch (Exception $e) { - // Local user can be ignored - } - - $other = array_merge( - $other, - Activitypub_profile::from_profile_collection( - $parent_notice->getAttentionProfiles() - ) - ); - } catch (NoParentNoticeException $e) { - // This is not a reply to something (has no parent) - } catch (NoResultException $e) { - // Parent author's profile not found! Complain louder? - common_log(LOG_ERR, "Parent notice's author not found: ".$e->getMessage()); - } - } - - $postman = new Activitypub_postman($profile, $other); - $postman->undo_like($notice); - - return true; - } - - /** - * Notify remote followers when a user gets deleted - * - * @param Action $action - * @param User $user user being deleted - */ - public function onEndDeleteUser(Action $action, User $user): void - { - $deleted_profile = $user->getProfile(); - $postman = new Activitypub_postman($deleted_profile); - $postman->delete_profile($deleted_profile); - } - - /** - * Federate private message - * - * @param Notice $message - * - * @return void - */ - public function onSendDirectMessage(Notice $message): void - { - $from = $message->getProfile(); - if (!$from->isLocal()) { - // nothing to do - return; - } - - $to = Activitypub_profile::from_profile_collection( - $message->getAttentionProfiles() - ); - - if (!empty($to)) { - $postman = new Activitypub_postman($from, $to); - $postman->create_direct_note($message); - } - } - - /** - * Override the "from ActivityPub" bit in notice lists to link to the - * original post and show the domain it came from. - * - * @author Diogo Cordeiro - * - * @param $notice - * @param $name - * @param $url - * @param $title - * - * @throws Exception - * - * @return mixed hook return code - */ - public function onStartNoticeSourceLink($notice, &$name, &$url, &$title) - { - // If we don't handle this, keep the event handler going - if (!in_array($notice->source, ['ActivityPub', 'share'])) { - return true; - } - - try { - $url = $notice->getUrl(); - // If getUrl() throws exception, $url is never set - - $bits = parse_url($url); - $domain = $bits['host']; - if (substr($domain, 0, 4) == 'www.') { - $name = substr($domain, 4); - } else { - $name = $domain; - } - - // TRANS: Title. %s is a domain name. - $title = sprintf(_m('Sent from %s via ActivityPub'), $domain); - - // Abort event handler, we have a name and URL! - return false; - } catch (InvalidUrlException $e) { - // This just means we don't have the notice source data - return true; - } - } -} - -/** - * Plugin return handler - */ -class ActivityPubReturn -{ - /** - * Return a valid answer - * - * @param string $res - * @param int $code Status Code - * - * @return void - * - * @author Diogo Cordeiro - */ - public static function answer($res = '', $code = 202) - { - http_response_code($code); - header('Content-Type: application/activity+json'); - echo json_encode($res, JSON_UNESCAPED_SLASHES | (isset($_GET['pretty']) ? JSON_PRETTY_PRINT : null)); - exit; - } - - /** - * Return an error - * - * @param string $m - * @param int $code Status Code - * - * @return void - * - * @author Diogo Cordeiro - */ - public static function error($m, $code = 400) - { - http_response_code($code); - header('Content-Type: application/activity+json'); - $res[] = Activitypub_error::error_message_to_array($m); - echo json_encode($res, JSON_UNESCAPED_SLASHES); - exit; - } -} diff --git a/plugins/ActivityPub/CONTRIBUTING.md b/plugins/ActivityPub/CONTRIBUTING.md deleted file mode 100644 index 3f3b1b3cc7..0000000000 --- a/plugins/ActivityPub/CONTRIBUTING.md +++ /dev/null @@ -1,93 +0,0 @@ -# Contributing - -When contributing to this repository, please first discuss the change you wish to make via issue, -email, or any other method with the owners of this repository before making a change. - -Please note we have a code of conduct, please follow it in all your interactions with the project. - -# Coding Style -- We follow every [PSR-12](https://www.php-fig.org/psr/psr-12/) ... -- ... except camelCase, that's too bad, we use snake_case - -## Merge Request Process - -1. Ensure you strip any trailing spaces off -2. Increase the version numbers in any examples files and the README.md to the new version that this - Pull Request would represent. The versioning scheme we use is [SemVer](http://semver.org/). -3. You may merge the Pull Request in once you have the sign-off of two other developers, or if you - do not have permission to do that, you may request the second reviewer to merge it for you. - -## Code of Conduct - -### Our Pledge - -In the interest of fostering an open and welcoming environment, we as -contributors and maintainers pledge to making participation in our project and -our community a harassment-free experience for everyone, regardless of age, body -size, disability, ethnicity, gender identity and expression, level of experience, -nationality, personal appearance, race, religion, or sexual identity and -orientation. - -### Our Standards - -Examples of behavior that contributes to creating a positive environment -include: - -* Using welcoming and inclusive language -* Being respectful of differing viewpoints and experiences -* Gracefully accepting constructive criticism -* Focusing on what is best for the community -* Showing empathy towards other community members - -Examples of unacceptable behavior by participants include: - -* The use of sexualized language or imagery and unwelcome sexual attention or -advances -* Trolling, insulting/derogatory comments, and personal or political attacks -* Public or private harassment -* Publishing others' private information, such as a physical or electronic - address, without explicit permission -* Other conduct which could reasonably be considered inappropriate in a - professional setting - -### Our Responsibilities - -Project maintainers are responsible for clarifying the standards of acceptable -behavior and are expected to take appropriate and fair corrective action in -response to any instances of unacceptable behavior. - -Project maintainers have the right and responsibility to remove, edit, or -reject comments, commits, code, wiki edits, issues, and other contributions -that are not aligned to this Code of Conduct, or to ban temporarily or -permanently any contributor for other behaviors that they deem inappropriate, -threatening, offensive, or harmful. - -### Scope - -This Code of Conduct applies both within project spaces and in public spaces -when an individual is representing the project or its community. Examples of -representing a project or community include using an official project e-mail -address, posting via an official social media account, or acting as an appointed -representative at an online or offline event. Representation of a project may be -further defined and clarified by project maintainers. - -### Enforcement - -Instances of abusive, harassing, or otherwise unacceptable behavior may be -reported by contacting the project team at diogo@fc.up.pt. All -complaints will be reviewed and investigated and will result in a response that -is deemed necessary and appropriate to the circumstances. The project team is -obligated to maintain confidentiality with regard to the reporter of an incident. -Further details of specific enforcement policies may be posted separately. - -Project maintainers who do not follow or enforce the Code of Conduct in good -faith may face temporary or permanent repercussions as determined by other -members of the project's leadership. - -### Attribution - -This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, -available at [http://contributor-covenant.org/version/1/4][version] - -[homepage]: http://contributor-covenant.org -[version]: http://contributor-covenant.org/version/1/4/ diff --git a/plugins/ActivityPub/COPYING b/plugins/ActivityPub/COPYING deleted file mode 100644 index dba13ed2dd..0000000000 --- a/plugins/ActivityPub/COPYING +++ /dev/null @@ -1,661 +0,0 @@ - GNU AFFERO GENERAL PUBLIC LICENSE - Version 3, 19 November 2007 - - Copyright (C) 2007 Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The GNU Affero General Public License is a free, copyleft license for -software and other kinds of works, specifically designed to ensure -cooperation with the community in the case of network server software. - - The licenses for most software and other practical works are designed -to take away your freedom to share and change the works. By contrast, -our General Public Licenses are intended to guarantee your freedom to -share and change all versions of a program--to make sure it remains free -software for all its users. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -them if you wish), that you receive source code or can get it if you -want it, that you can change the software or use pieces of it in new -free programs, and that you know you can do these things. - - Developers that use our General Public Licenses protect your rights -with two steps: (1) assert copyright on the software, and (2) offer -you this License which gives you legal permission to copy, distribute -and/or modify the software. - - A secondary benefit of defending all users' freedom is that -improvements made in alternate versions of the program, if they -receive widespread use, become available for other developers to -incorporate. Many developers of free software are heartened and -encouraged by the resulting cooperation. However, in the case of -software used on network servers, this result may fail to come about. -The GNU General Public License permits making a modified version and -letting the public access it on a server without ever releasing its -source code to the public. - - The GNU Affero General Public License is designed specifically to -ensure that, in such cases, the modified source code becomes available -to the community. It requires the operator of a network server to -provide the source code of the modified version running there to the -users of that server. Therefore, public use of a modified version, on -a publicly accessible server, gives the public access to the source -code of the modified version. - - An older license, called the Affero General Public License and -published by Affero, was designed to accomplish similar goals. This is -a different license, not a version of the Affero GPL, but Affero has -released a new version of the Affero GPL which permits relicensing under -this license. - - The precise terms and conditions for copying, distribution and -modification follow. - - TERMS AND CONDITIONS - - 0. Definitions. - - "This License" refers to version 3 of the GNU Affero General Public License. - - "Copyright" also means copyright-like laws that apply to other kinds of -works, such as semiconductor masks. - - "The Program" refers to any copyrightable work licensed under this -License. Each licensee is addressed as "you". "Licensees" and -"recipients" may be individuals or organizations. - - To "modify" a work means to copy from or adapt all or part of the work -in a fashion requiring copyright permission, other than the making of an -exact copy. The resulting work is called a "modified version" of the -earlier work or a work "based on" the earlier work. - - A "covered work" means either the unmodified Program or a work based -on the Program. - - To "propagate" a work means to do anything with it that, without -permission, would make you directly or secondarily liable for -infringement under applicable copyright law, except executing it on a -computer or modifying a private copy. Propagation includes copying, -distribution (with or without modification), making available to the -public, and in some countries other activities as well. - - To "convey" a work means any kind of propagation that enables other -parties to make or receive copies. Mere interaction with a user through -a computer network, with no transfer of a copy, is not conveying. - - An interactive user interface displays "Appropriate Legal Notices" -to the extent that it includes a convenient and prominently visible -feature that (1) displays an appropriate copyright notice, and (2) -tells the user that there is no warranty for the work (except to the -extent that warranties are provided), that licensees may convey the -work under this License, and how to view a copy of this License. If -the interface presents a list of user commands or options, such as a -menu, a prominent item in the list meets this criterion. - - 1. Source Code. - - The "source code" for a work means the preferred form of the work -for making modifications to it. "Object code" means any non-source -form of a work. - - A "Standard Interface" means an interface that either is an official -standard defined by a recognized standards body, or, in the case of -interfaces specified for a particular programming language, one that -is widely used among developers working in that language. - - The "System Libraries" of an executable work include anything, other -than the work as a whole, that (a) is included in the normal form of -packaging a Major Component, but which is not part of that Major -Component, and (b) serves only to enable use of the work with that -Major Component, or to implement a Standard Interface for which an -implementation is available to the public in source code form. A -"Major Component", in this context, means a major essential component -(kernel, window system, and so on) of the specific operating system -(if any) on which the executable work runs, or a compiler used to -produce the work, or an object code interpreter used to run it. - - The "Corresponding Source" for a work in object code form means all -the source code needed to generate, install, and (for an executable -work) run the object code and to modify the work, including scripts to -control those activities. However, it does not include the work's -System Libraries, or general-purpose tools or generally available free -programs which are used unmodified in performing those activities but -which are not part of the work. For example, Corresponding Source -includes interface definition files associated with source files for -the work, and the source code for shared libraries and dynamically -linked subprograms that the work is specifically designed to require, -such as by intimate data communication or control flow between those -subprograms and other parts of the work. - - The Corresponding Source need not include anything that users -can regenerate automatically from other parts of the Corresponding -Source. - - The Corresponding Source for a work in source code form is that -same work. - - 2. Basic Permissions. - - All rights granted under this License are granted for the term of -copyright on the Program, and are irrevocable provided the stated -conditions are met. This License explicitly affirms your unlimited -permission to run the unmodified Program. The output from running a -covered work is covered by this License only if the output, given its -content, constitutes a covered work. This License acknowledges your -rights of fair use or other equivalent, as provided by copyright law. - - You may make, run and propagate covered works that you do not -convey, without conditions so long as your license otherwise remains -in force. You may convey covered works to others for the sole purpose -of having them make modifications exclusively for you, or provide you -with facilities for running those works, provided that you comply with -the terms of this License in conveying all material for which you do -not control copyright. Those thus making or running the covered works -for you must do so exclusively on your behalf, under your direction -and control, on terms that prohibit them from making any copies of -your copyrighted material outside their relationship with you. - - Conveying under any other circumstances is permitted solely under -the conditions stated below. Sublicensing is not allowed; section 10 -makes it unnecessary. - - 3. Protecting Users' Legal Rights From Anti-Circumvention Law. - - No covered work shall be deemed part of an effective technological -measure under any applicable law fulfilling obligations under article -11 of the WIPO copyright treaty adopted on 20 December 1996, or -similar laws prohibiting or restricting circumvention of such -measures. - - When you convey a covered work, you waive any legal power to forbid -circumvention of technological measures to the extent such circumvention -is effected by exercising rights under this License with respect to -the covered work, and you disclaim any intention to limit operation or -modification of the work as a means of enforcing, against the work's -users, your or third parties' legal rights to forbid circumvention of -technological measures. - - 4. Conveying Verbatim Copies. - - You may convey verbatim copies of the Program's source code as you -receive it, in any medium, provided that you conspicuously and -appropriately publish on each copy an appropriate copyright notice; -keep intact all notices stating that this License and any -non-permissive terms added in accord with section 7 apply to the code; -keep intact all notices of the absence of any warranty; and give all -recipients a copy of this License along with the Program. - - You may charge any price or no price for each copy that you convey, -and you may offer support or warranty protection for a fee. - - 5. Conveying Modified Source Versions. - - You may convey a work based on the Program, or the modifications to -produce it from the Program, in the form of source code under the -terms of section 4, provided that you also meet all of these conditions: - - a) The work must carry prominent notices stating that you modified - it, and giving a relevant date. - - b) The work must carry prominent notices stating that it is - released under this License and any conditions added under section - 7. This requirement modifies the requirement in section 4 to - "keep intact all notices". - - c) You must license the entire work, as a whole, under this - License to anyone who comes into possession of a copy. This - License will therefore apply, along with any applicable section 7 - additional terms, to the whole of the work, and all its parts, - regardless of how they are packaged. This License gives no - permission to license the work in any other way, but it does not - invalidate such permission if you have separately received it. - - d) If the work has interactive user interfaces, each must display - Appropriate Legal Notices; however, if the Program has interactive - interfaces that do not display Appropriate Legal Notices, your - work need not make them do so. - - A compilation of a covered work with other separate and independent -works, which are not by their nature extensions of the covered work, -and which are not combined with it such as to form a larger program, -in or on a volume of a storage or distribution medium, is called an -"aggregate" if the compilation and its resulting copyright are not -used to limit the access or legal rights of the compilation's users -beyond what the individual works permit. Inclusion of a covered work -in an aggregate does not cause this License to apply to the other -parts of the aggregate. - - 6. Conveying Non-Source Forms. - - You may convey a covered work in object code form under the terms -of sections 4 and 5, provided that you also convey the -machine-readable Corresponding Source under the terms of this License, -in one of these ways: - - a) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by the - Corresponding Source fixed on a durable physical medium - customarily used for software interchange. - - b) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by a - written offer, valid for at least three years and valid for as - long as you offer spare parts or customer support for that product - model, to give anyone who possesses the object code either (1) a - copy of the Corresponding Source for all the software in the - product that is covered by this License, on a durable physical - medium customarily used for software interchange, for a price no - more than your reasonable cost of physically performing this - conveying of source, or (2) access to copy the - Corresponding Source from a network server at no charge. - - c) Convey individual copies of the object code with a copy of the - written offer to provide the Corresponding Source. This - alternative is allowed only occasionally and noncommercially, and - only if you received the object code with such an offer, in accord - with subsection 6b. - - d) Convey the object code by offering access from a designated - place (gratis or for a charge), and offer equivalent access to the - Corresponding Source in the same way through the same place at no - further charge. You need not require recipients to copy the - Corresponding Source along with the object code. If the place to - copy the object code is a network server, the Corresponding Source - may be on a different server (operated by you or a third party) - that supports equivalent copying facilities, provided you maintain - clear directions next to the object code saying where to find the - Corresponding Source. Regardless of what server hosts the - Corresponding Source, you remain obligated to ensure that it is - available for as long as needed to satisfy these requirements. - - e) Convey the object code using peer-to-peer transmission, provided - you inform other peers where the object code and Corresponding - Source of the work are being offered to the general public at no - charge under subsection 6d. - - A separable portion of the object code, whose source code is excluded -from the Corresponding Source as a System Library, need not be -included in conveying the object code work. - - A "User Product" is either (1) a "consumer product", which means any -tangible personal property which is normally used for personal, family, -or household purposes, or (2) anything designed or sold for incorporation -into a dwelling. In determining whether a product is a consumer product, -doubtful cases shall be resolved in favor of coverage. For a particular -product received by a particular user, "normally used" refers to a -typical or common use of that class of product, regardless of the status -of the particular user or of the way in which the particular user -actually uses, or expects or is expected to use, the product. A product -is a consumer product regardless of whether the product has substantial -commercial, industrial or non-consumer uses, unless such uses represent -the only significant mode of use of the product. - - "Installation Information" for a User Product means any methods, -procedures, authorization keys, or other information required to install -and execute modified versions of a covered work in that User Product from -a modified version of its Corresponding Source. The information must -suffice to ensure that the continued functioning of the modified object -code is in no case prevented or interfered with solely because -modification has been made. - - If you convey an object code work under this section in, or with, or -specifically for use in, a User Product, and the conveying occurs as -part of a transaction in which the right of possession and use of the -User Product is transferred to the recipient in perpetuity or for a -fixed term (regardless of how the transaction is characterized), the -Corresponding Source conveyed under this section must be accompanied -by the Installation Information. But this requirement does not apply -if neither you nor any third party retains the ability to install -modified object code on the User Product (for example, the work has -been installed in ROM). - - The requirement to provide Installation Information does not include a -requirement to continue to provide support service, warranty, or updates -for a work that has been modified or installed by the recipient, or for -the User Product in which it has been modified or installed. Access to a -network may be denied when the modification itself materially and -adversely affects the operation of the network or violates the rules and -protocols for communication across the network. - - Corresponding Source conveyed, and Installation Information provided, -in accord with this section must be in a format that is publicly -documented (and with an implementation available to the public in -source code form), and must require no special password or key for -unpacking, reading or copying. - - 7. Additional Terms. - - "Additional permissions" are terms that supplement the terms of this -License by making exceptions from one or more of its conditions. -Additional permissions that are applicable to the entire Program shall -be treated as though they were included in this License, to the extent -that they are valid under applicable law. If additional permissions -apply only to part of the Program, that part may be used separately -under those permissions, but the entire Program remains governed by -this License without regard to the additional permissions. - - When you convey a copy of a covered work, you may at your option -remove any additional permissions from that copy, or from any part of -it. (Additional permissions may be written to require their own -removal in certain cases when you modify the work.) You may place -additional permissions on material, added by you to a covered work, -for which you have or can give appropriate copyright permission. - - Notwithstanding any other provision of this License, for material you -add to a covered work, you may (if authorized by the copyright holders of -that material) supplement the terms of this License with terms: - - a) Disclaiming warranty or limiting liability differently from the - terms of sections 15 and 16 of this License; or - - b) Requiring preservation of specified reasonable legal notices or - author attributions in that material or in the Appropriate Legal - Notices displayed by works containing it; or - - c) Prohibiting misrepresentation of the origin of that material, or - requiring that modified versions of such material be marked in - reasonable ways as different from the original version; or - - d) Limiting the use for publicity purposes of names of licensors or - authors of the material; or - - e) Declining to grant rights under trademark law for use of some - trade names, trademarks, or service marks; or - - f) Requiring indemnification of licensors and authors of that - material by anyone who conveys the material (or modified versions of - it) with contractual assumptions of liability to the recipient, for - any liability that these contractual assumptions directly impose on - those licensors and authors. - - All other non-permissive additional terms are considered "further -restrictions" within the meaning of section 10. If the Program as you -received it, or any part of it, contains a notice stating that it is -governed by this License along with a term that is a further -restriction, you may remove that term. If a license document contains -a further restriction but permits relicensing or conveying under this -License, you may add to a covered work material governed by the terms -of that license document, provided that the further restriction does -not survive such relicensing or conveying. - - If you add terms to a covered work in accord with this section, you -must place, in the relevant source files, a statement of the -additional terms that apply to those files, or a notice indicating -where to find the applicable terms. - - Additional terms, permissive or non-permissive, may be stated in the -form of a separately written license, or stated as exceptions; -the above requirements apply either way. - - 8. Termination. - - You may not propagate or modify a covered work except as expressly -provided under this License. Any attempt otherwise to propagate or -modify it is void, and will automatically terminate your rights under -this License (including any patent licenses granted under the third -paragraph of section 11). - - However, if you cease all violation of this License, then your -license from a particular copyright holder is reinstated (a) -provisionally, unless and until the copyright holder explicitly and -finally terminates your license, and (b) permanently, if the copyright -holder fails to notify you of the violation by some reasonable means -prior to 60 days after the cessation. - - Moreover, your license from a particular copyright holder is -reinstated permanently if the copyright holder notifies you of the -violation by some reasonable means, this is the first time you have -received notice of violation of this License (for any work) from that -copyright holder, and you cure the violation prior to 30 days after -your receipt of the notice. - - Termination of your rights under this section does not terminate the -licenses of parties who have received copies or rights from you under -this License. If your rights have been terminated and not permanently -reinstated, you do not qualify to receive new licenses for the same -material under section 10. - - 9. Acceptance Not Required for Having Copies. - - You are not required to accept this License in order to receive or -run a copy of the Program. Ancillary propagation of a covered work -occurring solely as a consequence of using peer-to-peer transmission -to receive a copy likewise does not require acceptance. However, -nothing other than this License grants you permission to propagate or -modify any covered work. These actions infringe copyright if you do -not accept this License. Therefore, by modifying or propagating a -covered work, you indicate your acceptance of this License to do so. - - 10. Automatic Licensing of Downstream Recipients. - - Each time you convey a covered work, the recipient automatically -receives a license from the original licensors, to run, modify and -propagate that work, subject to this License. You are not responsible -for enforcing compliance by third parties with this License. - - An "entity transaction" is a transaction transferring control of an -organization, or substantially all assets of one, or subdividing an -organization, or merging organizations. If propagation of a covered -work results from an entity transaction, each party to that -transaction who receives a copy of the work also receives whatever -licenses to the work the party's predecessor in interest had or could -give under the previous paragraph, plus a right to possession of the -Corresponding Source of the work from the predecessor in interest, if -the predecessor has it or can get it with reasonable efforts. - - You may not impose any further restrictions on the exercise of the -rights granted or affirmed under this License. For example, you may -not impose a license fee, royalty, or other charge for exercise of -rights granted under this License, and you may not initiate litigation -(including a cross-claim or counterclaim in a lawsuit) alleging that -any patent claim is infringed by making, using, selling, offering for -sale, or importing the Program or any portion of it. - - 11. Patents. - - A "contributor" is a copyright holder who authorizes use under this -License of the Program or a work on which the Program is based. The -work thus licensed is called the contributor's "contributor version". - - A contributor's "essential patent claims" are all patent claims -owned or controlled by the contributor, whether already acquired or -hereafter acquired, that would be infringed by some manner, permitted -by this License, of making, using, or selling its contributor version, -but do not include claims that would be infringed only as a -consequence of further modification of the contributor version. For -purposes of this definition, "control" includes the right to grant -patent sublicenses in a manner consistent with the requirements of -this License. - - Each contributor grants you a non-exclusive, worldwide, royalty-free -patent license under the contributor's essential patent claims, to -make, use, sell, offer for sale, import and otherwise run, modify and -propagate the contents of its contributor version. - - In the following three paragraphs, a "patent license" is any express -agreement or commitment, however denominated, not to enforce a patent -(such as an express permission to practice a patent or covenant not to -sue for patent infringement). To "grant" such a patent license to a -party means to make such an agreement or commitment not to enforce a -patent against the party. - - If you convey a covered work, knowingly relying on a patent license, -and the Corresponding Source of the work is not available for anyone -to copy, free of charge and under the terms of this License, through a -publicly available network server or other readily accessible means, -then you must either (1) cause the Corresponding Source to be so -available, or (2) arrange to deprive yourself of the benefit of the -patent license for this particular work, or (3) arrange, in a manner -consistent with the requirements of this License, to extend the patent -license to downstream recipients. "Knowingly relying" means you have -actual knowledge that, but for the patent license, your conveying the -covered work in a country, or your recipient's use of the covered work -in a country, would infringe one or more identifiable patents in that -country that you have reason to believe are valid. - - If, pursuant to or in connection with a single transaction or -arrangement, you convey, or propagate by procuring conveyance of, a -covered work, and grant a patent license to some of the parties -receiving the covered work authorizing them to use, propagate, modify -or convey a specific copy of the covered work, then the patent license -you grant is automatically extended to all recipients of the covered -work and works based on it. - - A patent license is "discriminatory" if it does not include within -the scope of its coverage, prohibits the exercise of, or is -conditioned on the non-exercise of one or more of the rights that are -specifically granted under this License. You may not convey a covered -work if you are a party to an arrangement with a third party that is -in the business of distributing software, under which you make payment -to the third party based on the extent of your activity of conveying -the work, and under which the third party grants, to any of the -parties who would receive the covered work from you, a discriminatory -patent license (a) in connection with copies of the covered work -conveyed by you (or copies made from those copies), or (b) primarily -for and in connection with specific products or compilations that -contain the covered work, unless you entered into that arrangement, -or that patent license was granted, prior to 28 March 2007. - - Nothing in this License shall be construed as excluding or limiting -any implied license or other defenses to infringement that may -otherwise be available to you under applicable patent law. - - 12. No Surrender of Others' Freedom. - - If conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot convey a -covered work so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you may -not convey it at all. For example, if you agree to terms that obligate you -to collect a royalty for further conveying from those to whom you convey -the Program, the only way you could satisfy both those terms and this -License would be to refrain entirely from conveying the Program. - - 13. Remote Network Interaction; Use with the GNU General Public License. - - Notwithstanding any other provision of this License, if you modify the -Program, your modified version must prominently offer all users -interacting with it remotely through a computer network (if your version -supports such interaction) an opportunity to receive the Corresponding -Source of your version by providing access to the Corresponding Source -from a network server at no charge, through some standard or customary -means of facilitating copying of software. This Corresponding Source -shall include the Corresponding Source for any work covered by version 3 -of the GNU General Public License that is incorporated pursuant to the -following paragraph. - - Notwithstanding any other provision of this License, you have -permission to link or combine any covered work with a work licensed -under version 3 of the GNU General Public License into a single -combined work, and to convey the resulting work. The terms of this -License will continue to apply to the part which is the covered work, -but the work with which it is combined will remain governed by version -3 of the GNU General Public License. - - 14. Revised Versions of this License. - - The Free Software Foundation may publish revised and/or new versions of -the GNU Affero General Public License from time to time. Such new versions -will be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - - Each version is given a distinguishing version number. If the -Program specifies that a certain numbered version of the GNU Affero General -Public License "or any later version" applies to it, you have the -option of following the terms and conditions either of that numbered -version or of any later version published by the Free Software -Foundation. If the Program does not specify a version number of the -GNU Affero General Public License, you may choose any version ever published -by the Free Software Foundation. - - If the Program specifies that a proxy can decide which future -versions of the GNU Affero General Public License can be used, that proxy's -public statement of acceptance of a version permanently authorizes you -to choose that version for the Program. - - Later license versions may give you additional or different -permissions. However, no additional obligations are imposed on any -author or copyright holder as a result of your choosing to follow a -later version. - - 15. Disclaimer of Warranty. - - THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY -APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT -HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY -OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, -THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM -IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF -ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - 16. Limitation of Liability. - - IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS -THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY -GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE -USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF -DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD -PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), -EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF -SUCH DAMAGES. - - 17. Interpretation of Sections 15 and 16. - - If the disclaimer of warranty and limitation of liability provided -above cannot be given local legal effect according to their terms, -reviewing courts shall apply local law that most closely approximates -an absolute waiver of all civil liability in connection with the -Program, unless a warranty or assumption of liability accompanies a -copy of the Program in return for a fee. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -state the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - 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 . - -Also add information on how to contact you by electronic and paper mail. - - If your software can interact with users remotely through a computer -network, you should also make sure that it provides a way for users to -get its source. For example, if your program is a web application, its -interface could display a "Source" link that leads users to an archive -of the code. There are many ways you could offer source, and different -solutions will be better for different programs; see section 13 for the -specific requirements. - - You should also get your employer (if you work as a programmer) or school, -if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU AGPL, see -. diff --git a/plugins/ActivityPub/Entity/ActivityPubActor.php b/plugins/ActivityPub/Entity/ActivityPubActor.php deleted file mode 100644 index 03c5ae2fec..0000000000 --- a/plugins/ActivityPub/Entity/ActivityPubActor.php +++ /dev/null @@ -1,844 +0,0 @@ -. - -// }}} - -/** - * ActivityPub's Remote Actor - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @author Hugo Sales - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ - -namespace Plugin\ActivityPub\Entity; - -class ActivityPubActor -{ - // {{{ Autocode - private string $uri; - private int $profile_id; - private string $inboxuri; - private ?string $sharedInboxuri; - private ?DateTimeInterface $created; - private DateTimeInterface $modified; - - public function setUri(string $uri): self - { - $this->uri = $uri; - return $this; - } - - public function getUri(): string - { - return $this->uri; - } - - public function setProfileId(int $profile_id): self - { - $this->profile_id = $profile_id; - return $this; - } - - public function getProfileId(): int - { - return $this->profile_id; - } - - public function setInboxuri(string $inboxuri): self - { - $this->inboxuri = $inboxuri; - return $this; - } - - public function getInboxuri(): string - { - return $this->inboxuri; - } - - public function setSharedInboxuri(?string $sharedInboxuri): self - { - $this->sharedInboxuri = $sharedInboxuri; - return $this; - } - - public function getSharedInboxuri(): ?string - { - return $this->sharedInboxuri; - } - - public function setCreated(?DateTimeInterface $created): self - { - $this->created = $created; - return $this; - } - - public function getCreated(): ?DateTimeInterface - { - return $this->created; - } - - public function setModified(DateTimeInterface $modified): self - { - $this->modified = $modified; - return $this; - } - - public function getModified(): DateTimeInterface - { - return $this->modified; - } - - // }}} Autocode - - /** - * Generates a pretty profile from a Profile object - * - * @param Profile $profile - * - * @throws InvalidUrlException - * @throws ServerException - * @throws Exception - * - * @return array array to be used in a response - * - * @author Diogo Cordeiro - */ - public static function profile_to_array(Profile $profile): array - { - $uri = $profile->getUri(); - $id = $profile->getID(); - $rsa = new Activitypub_rsa(); - $public_key = $rsa->ensure_public_key($profile); - unset($rsa); - $res = [ - '@context' => [ - 'https://www.w3.org/ns/activitystreams', - 'https://w3id.org/security/v1', - [ - 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers', - ], - ], - 'id' => $uri, - 'type' => 'Person', - 'following' => common_local_url('apActorFollowing', ['id' => $id]), - 'followers' => common_local_url('apActorFollowers', ['id' => $id]), - 'liked' => common_local_url('apActorLiked', ['id' => $id]), - 'inbox' => common_local_url('apInbox', ['id' => $id]), - 'outbox' => common_local_url('apActorOutbox', ['id' => $id]), - 'preferredUsername' => $profile->getNickname(), - 'name' => $profile->getBestName(), - 'summary' => ($desc = $profile->getDescription()) == null ? '' : $desc, - 'url' => $profile->getUrl(), - 'manuallyApprovesFollowers' => false, - 'publicKey' => [ - 'id' => $uri . '#public-key', - 'owner' => $uri, - 'publicKeyPem' => $public_key, - ], - 'tag' => [], - 'attachment' => [], - 'icon' => [ - 'type' => 'Image', - 'mediaType' => 'image/png', - 'height' => AVATAR_PROFILE_SIZE, - 'width' => AVATAR_PROFILE_SIZE, - 'url' => $profile->avatarUrl(AVATAR_PROFILE_SIZE), - ], - ]; - - if ($profile->isLocal()) { - $res['endpoints']['sharedInbox'] = common_local_url('apInbox'); - } else { - $aprofile = new Activitypub_profile(); - $aprofile = $aprofile->from_profile($profile); - $res['endpoints']['sharedInbox'] = $aprofile->sharedInboxuri; - } - - return $res; - } - - /** - * Insert the current object variables into the database - * - * @throws ServerException - * - * @author Diogo Cordeiro - */ - public function do_insert(): void - { - // Does any other protocol have this remote entity we're about to add ? - Event::handle('StartTFNLookup', [$this->uri, get_class($this), &$profile_id]); - if (!is_null($profile_id)) { - // Yes! Avoid creating a new profile - $this->profile_id = $profile_id; - $this->created = $this->modified = common_sql_now(); - - if ($this->insert() === false) { - $this->query('ROLLBACK'); - throw new ServerException('Cannot save ActivityPub profile.'); - } - - // Update existing profile with received data - $profile = Profile::getKV('id', $profile_id); - self::update_local_profile($profile, $this); - - // Ask TFN to handle profile duplication - Event::handle('EndTFNLookup', [get_class($this), $profile_id]); - } else { - // No, create both a new profile and remote profile - $profile = new Profile(); - $profile->created = $this->created = $this->modified = common_sql_now(); - self::update_local_profile($profile, $this); - - $this->profile_id = $profile->insert(); - if ($this->profile_id === false) { - $profile->query('ROLLBACK'); - throw new ServerException('Profile insertion failed.'); - } - - $ok = $this->insert(); - - if ($ok === false) { - $profile->query('ROLLBACK'); - $this->query('ROLLBACK'); - throw new ServerException('Cannot save ActivityPub profile.'); - } - } - } - - /** - * Fetch the locally stored profile for this Activitypub_profile - * - * @throws NoProfileException if it was not found - * - * @return Profile - * - * @author Diogo Cordeiro - */ - public function local_profile(): 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 - * - * @param Profile $profile - * - * @throws Exception if no Activitypub_profile exists for given Profile - * - * @return Activitypub_profile - * - * @author Diogo Cordeiro - */ - public static function from_profile(Profile $profile): Activitypub_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.'); - } - } - - // extend the ap_profile with some information we - // don't store in the database - $fields = [ - 'nickname' => 'nickname', - 'fullname' => 'fullname', - 'bio' => 'bio', - ]; - - foreach ($fields as $af => $pf) { - $aprofile->{$af} = $profile->{$pf}; - } - - return $aprofile; - } - - /** - * Travels an array of Profile and returns an array of Activitypub_profile - * - * @param array of Profile $profiles - * - * @return array of Activitypub_profile - */ - public static function from_profile_collection(array $profiles): array - { - $ap_profiles = []; - - foreach ($profiles as $profile) { - try { - $ap_profiles[] = self::from_profile($profile); - } catch (Exception $e) { - // Don't mind local profiles - } - } - - return $ap_profiles; - } - - /** - * 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 - * - * @param Profile $profile - * - * @throws HTTP_Request2_Exception - * @throws Exception - * @throws Exception - * - * @return Activitypub_profile - * - * @author Diogo Cordeiro - */ - private static function create_from_local_profile(Profile $profile): Activitypub_profile - { - $aprofile = new Activitypub_profile(); - - $url = $profile->getUri(); - $inboxes = Activitypub_explorer::get_actor_inboxes_uri($url); - if ($inboxes === false) { - throw new Exception('This is not an ActivityPub user thus AProfile is politely refusing to proceed.'); - } - - $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 - * - * @return string Inbox URL - * - * @author Diogo Cordeiro - */ - public function get_inbox(): string - { - if (is_null($this->sharedInboxuri)) { - return $this->inboxuri; - } - - return $this->sharedInboxuri; - } - - /** - * Ensures a valid Activitypub_profile when provided with a valid URI. - * - * @param string $url - * @param bool $grab_online whether to try online grabbing, defaults to true - * - * @throws Exception if it isn't possible to return an Activitypub_profile - * - * @return Activitypub_profile - * - * @author Diogo Cordeiro - */ - public static function fromUri(string $url, bool $grab_online = true): Activitypub_profile - { - try { - return self::from_profile(Activitypub_explorer::get_profile_from_url($url, $grab_online)); - } catch (Exception $e) { - throw new Exception('No valid ActivityPub profile found for given URI.'); - } - } - - /** - * 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. - * - * @param string $addr WebFinger address - * - * @throws Exception on error conditions - * - * @return Activitypub_profile - * - * @author Diogo Cordeiro - * @author GNU social - */ - public static function ensure_webfinger(string $addr): Activitypub_profile - { - // 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 { - return self::fromUri($uri); - } 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(); - - 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.')); - } - - $hints = array_merge( - ['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) || $hints['hcard'] != $hints['profileurl']) { - $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}"); - $aprofile = self::fromUri($hints['profileurl']); - self::cacheSet(sprintf('activitypub_profile:webfinger:%s', $addr), $aprofile->getUri()); - return $aprofile; - } catch (Exception $e) { - common_log(LOG_WARNING, "Failed creating profile from profile URL '{$profileUrl}': " . $e->getMessage()); - // keep looking - // - // @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. - } - } - - // XXX: try hcard - // XXX: try FOAF - - // TRANS: Exception. %s is a WebFinger address. - throw new Exception(sprintf(_m('Could not find a valid profile for "%s".'), $addr)); - } - - /** - * Update local profile with info from some AP profile - * - * @param Profile $profile - * @param Activitypub_profile $aprofile - * - * @return void - * - * @author Bruno Casteleiro - * @author Diogo Cordeiro - */ - public static function update_local_profile(Profile $profile, Activitypub_profile $aprofile): void - { - $fields = [ - 'profileurl' => 'profileurl', - 'nickname' => 'nickname', - 'fullname' => 'fullname', - 'bio' => 'bio', - ]; - - $orig = clone $profile; - - foreach ($fields as $af => $pf) { - $profile->{$pf} = $aprofile->{$af}; - } - - if ($profile->id) { - common_debug('Updating local Profile:' . $profile->id . ' from remote ActivityPub profile'); - $profile->modified = common_sql_now(); - $profile->update($orig); - } - } - - /** - * Update remote user profile in local instance - * - * @param Activitypub_profile $aprofile - * @param array|false $res remote response, if array it updates, if false it deletes - * - * @throws NoProfileException - * - * @return Profile remote Profile object - * - * @author Diogo Cordeiro - */ - public static function update_profile(Activitypub_profile $aprofile, $res): Profile - { - if ($res === false) { - $profile = $aprofile->local_profile(); - $id = $profile->getID(); - $profile->delete(); - throw new NoProfileException($id, '410 Gone'); - } - - if (!is_array($res)) { - throw new InvalidArgumentException('TypeError: Argument 2 passed to Activitypub_profile::update_profile() must be of the type array or bool(false).'); - } - - // ActivityPub Profile - $aprofile->uri = $res['id']; - $aprofile->nickname = $res['preferredUsername']; - $aprofile->fullname = $res['name'] ?? null; - $aprofile->bio = isset($res['summary']) ? substr(strip_tags($res['summary']), 0, 1000) : null; - $aprofile->inboxuri = $res['inbox']; - $aprofile->sharedInboxuri = $res['endpoints']['sharedInbox'] ?? $res['inbox']; - $aprofile->profileurl = $res['url'] ?? $aprofile->uri; - $aprofile->modified = common_sql_now(); - - $profile = $aprofile->local_profile(); - - // Profile - self::update_local_profile($profile, $aprofile); - $aprofile->update(); - - // Public Key - Activitypub_rsa::update_public_key($profile, $res['publicKey']['publicKeyPem']); - - // Avatar - if (isset($res['icon']['url'])) { - try { - Activitypub_explorer::update_avatar($profile, $res['icon']['url']); - } catch (Exception $e) { - // Let the exception go, it isn't a serious issue - common_debug('An error ocurred while grabbing remote avatar' . $e->getMessage()); - } - } - - return $profile; - } - - /** - * Update remote user profile URI in local instance - * - * @param string $uri - * - * @throws Exception (if the update fails) - * - * @return void - * - * @author Bruno Casteleiro - */ - public function updateUri(string $uri): void - { - $orig = clone $this; - $this->uri = $uri; - $this->updateWithKeys($orig); - } - - /** - * Getter for the number of subscribers of a - * given local profile - * - * @param Profile $profile profile object - * - * @return int number of subscribers - * - * @author Bruno Casteleiro - */ - public static function subscriberCount(Profile $profile): int - { - $cnt = self::cacheGet(sprintf('activitypub_profile:subscriberCount:%d', $profile->id)); - - if ($cnt !== false && is_int($cnt)) { - return $cnt; - } - - $user_table = common_database_tablename('user'); - $sub = new Subscription(); - $sub->subscribed = $profile->id; - $sub->_join .= "\n" . <<whereAdd('subscriber <> subscribed'); - $cnt = $sub->count('DISTINCT subscriber'); - - self::cacheSet(sprintf('activitypub_profile:subscriberCount:%d', $profile->id), $cnt); - - return $cnt; - } - - /** - * Getter for the number of subscriptions of a - * given local profile - * - * @param Profile $profile profile object - * - * @return int number of subscriptions - * - * @author Bruno Casteleiro - */ - public static function subscriptionCount(Profile $profile): int - { - $cnt = self::cacheGet(sprintf('activitypub_profile:subscriptionCount:%d', $profile->id)); - - if ($cnt !== false && is_int($cnt)) { - return $cnt; - } - - $user_table = common_database_tablename('user'); - $sub = new Subscription(); - $sub->subscriber = $profile->id; - $sub->_join .= "\n" . <<whereAdd('subscriber <> subscribed'); - $cnt = $sub->count('DISTINCT subscribed'); - - self::cacheSet(sprintf('activitypub_profile:subscriptionCount:%d', $profile->id), $cnt); - - return $cnt; - } - - /** - * Increment or decrement subscriber count - * - * @param Profile $profile - * @param $adder - * - * @author Bruno Casteleiro - */ - public static function updateSubscriberCount(Profile $profile, $adder): void - { - $cnt = self::cacheGet(sprintf('activitypub_profile:subscriberCount:%d', $profile->id)); - - if ($cnt !== false && is_int($cnt)) { - self::cacheSet(sprintf('activitypub_profile:subscriberCount:%d', $profile->id), $cnt + $adder); - } - } - - /** - * Increment or decrement subscription count - * - * @param Profile $profile - * @param $adder - * - * @author Bruno Casteleiro - */ - public static function updateSubscriptionCount(Profile $profile, $adder): void - { - $cnt = self::cacheGet(sprintf('activitypub_profile:subscriptionCount:%d', $profile->id)); - - if ($cnt !== false && is_int($cnt)) { - self::cacheSet(sprintf('activitypub_profile:subscriptionCount:%d', $profile->id), $cnt + $adder); - } - } - - /** - * Getter for the subscriber profiles of a - * given local profile - * - * @param Profile $profile profile object - * @param int $offset [optional] index of the starting row to fetch from - * @param null|int $limit [optional] maximum number of rows allowed for fetching. If it is omitted, - * then the sequence will have everything - * from offset up until the end. - * - * @return array subscriber profile objects - * - * @author Bruno Casteleiro - */ - public static function getSubscribers(Profile $profile, int $offset = 0, ?int $limit = null): array - { - $cache = false; - if ($offset + $limit <= Subscription::CACHE_WINDOW) { - $subs = self::cacheGet(sprintf('activitypub_profile:subscriberCollection:%d', $profile->id)); - if ($subs !== false && is_array($subs)) { - return array_slice($subs, $offset, $limit); - } - - $cache = true; - } - - $subs = Subscription::getSubscriberIDs($profile->id, $offset, $limit); - $profiles = []; - - $users = User::multiGet('id', $subs); - foreach ($users->fetchAll() as $user) { - $profiles[$user->id] = $user->getProfile(); - } - - $ap_profiles = Activitypub_profile::multiGet('profile_id', $subs); - foreach ($ap_profiles->fetchAll() as $ap) { - $profiles[$ap->getID()] = $ap->local_profile(); - } - - if ($cache) { - self::cacheSet(sprintf('activitypub_profile:subscriberCollection:%d', $profile->id), $profiles); - } - - return $profiles; - } - - /** - * Getter for the subscribed profiles of a - * given local profile - * - * @param Profile $profile profile object - * @param int $offset index of the starting row to fetch from - * @param null|int $limit maximum number of rows allowed for fetching - * - * @return array subscribed profile objects - * - * @author Bruno Casteleiro - */ - public static function getSubscribed(Profile $profile, int $offset = 0, ?int $limit = null): array - { - $cache = false; - if ($offset + $limit <= Subscription::CACHE_WINDOW) { - $subs = self::cacheGet(sprintf('activitypub_profile:subscribedCollection:%d', $profile->id)); - if (is_array($subs)) { - return array_slice($subs, $offset, $limit); - } - - $cache = true; - } - - $subs = Subscription::getSubscribedIDs($profile->id, $offset, $limit); - - $profiles = []; - - $users = User::multiGet('id', $subs); - foreach ($users->fetchAll() as $user) { - $profiles[$user->id] = $user->getProfile(); - } - - $ap_profiles = Activitypub_profile::multiGet('profile_id', $subs); - foreach ($ap_profiles->fetchAll() as $ap) { - $profiles[$ap->getID()] = $ap->local_profile(); - } - - if ($cache) { - self::cacheSet(sprintf('activitypub_profile:subscribedCollection:%d', $profile->id), $profiles); - } - - return $profiles; - } - - /** - * Update cached values that are relevant to - * the users involved in a subscription - * - * @param Profile $actor subscriber profile object - * @param Profile $other subscribed profile object - * - * @throws Exception - * - * @return void - * - * @author Bruno Casteleiro - */ - public static function subscribeCacheUpdate(Profile $actor, Profile $other): void - { - self::blow('activitypub_profile:subscribedCollection:%d', $actor->getID()); - self::blow('activitypub_profile:subscriberCollection:%d', $other->id); - self::updateSubscriptionCount($actor, +1); - self::updateSubscriberCount($other, +1); - } - - /** - * Update cached values that are relevant to - * the users involved in an unsubscription - * - * @param Profile $actor subscriber profile object - * @param Profile $other subscribed profile object - * - * @throws Exception - * - * @return void - * - * @author Bruno Casteleiro - */ - public static function unsubscribeCacheUpdate(Profile $actor, Profile $other): void - { - self::blow('activitypub_profile:subscribedCollection:%d', $actor->getID()); - self::blow('activitypub_profile:subscriberCollection:%d', $other->id); - self::updateSubscriptionCount($actor, -1); - self::updateSubscriberCount($other, -1); - } - - public static function schemaDef() - { - return [ - 'name' => 'activitypub_actor', - 'description' => 'remote actor profiles', - 'fields' => [ - 'uri' => ['type' => 'text', 'not null' => true], - 'profile_id' => ['type' => 'int', 'not null' => true], - 'inboxuri' => ['type' => 'text', 'not null' => true], - 'sharedInboxuri' => ['type' => 'text'], - 'created' => ['type' => 'datetime', 'description' => 'date this record was created'], - 'modified' => ['type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'], - ], - 'primary key' => ['profile_id'], - 'foreign keys' => [ - 'activitypub_profile_profile_id_fkey' => ['profile', ['profile_id' => 'id']], - ], - ]; - } -} diff --git a/plugins/ActivityPub/Entity/ActivityPubCryptKey.php b/plugins/ActivityPub/Entity/ActivityPubCryptKey.php deleted file mode 100644 index 7b8e39999c..0000000000 --- a/plugins/ActivityPub/Entity/ActivityPubCryptKey.php +++ /dev/null @@ -1,249 +0,0 @@ -. - -// }}} - -/** - * ActivityPub Assymetric Key Storage System - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @author Hugo Sales - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ - -namespace Plugin\ActivityPub\Entity; - -class ActivityPubCryptKey -{ - // {{{ Autocode - private int $gsactor_id; - private ?string $private_key; - private string $public_key; - private ?DateTimeInterface $created; - private DateTimeInterface $modified; - - public function setGSActorId(int $gsactor_id): self - { - $this->gsactor_id = $gsactor_id; - return $this; - } - - public function getGSActorId(): int - { - return $this->gsactor_id; - } - - public function setPrivateKey(?string $private_key): self - { - $this->private_key = $private_key; - return $this; - } - - public function getPrivateKey(): ?string - { - return $this->private_key; - } - - public function setPublicKey(string $public_key): self - { - $this->public_key = $public_key; - return $this; - } - - public function getPublicKey(): string - { - return $this->public_key; - } - - public function setCreated(?DateTimeInterface $created): self - { - $this->created = $created; - return $this; - } - - public function getCreated(): ?DateTimeInterface - { - return $this->created; - } - - public function setModified(DateTimeInterface $modified): self - { - $this->modified = $modified; - return $this; - } - - public function getModified(): DateTimeInterface - { - return $this->modified; - } - - // }}} Autocode - - /** - * Private key getter - * - * @param Profile $profile - * - * @throws Exception Throws exception if tries to fetch a private key of an actor we don't own - * - * @return string The private key - */ - public function get_private_key(Profile $profile): string - { - $this->profile_id = $profile->getID(); - $apRSA = self::getKV('profile_id', $this->profile_id); - if (!$apRSA instanceof Activitypub_rsa) { - // Nonexistent key pair for this profile - if ($profile->isLocal()) { - self::generate_keys($this->private_key, $this->public_key); - $this->store_keys(); - $apRSA->private_key = $this->private_key; - } else { - throw new Exception('This is a remote Profile, there is no Private Key for this Profile.'); - } - } - return $apRSA->private_key; - } - - /** - * Guarantees a Public Key for a given profile. - * - * @param Profile $profile - * @param bool $fetch=true Should attempt to fetch keys from a remote profile? - * - * @throws ServerException It should never occur, but if so, we break everything! - * @throws Exception - * - * @return string The public key - * - * @author Diogo Cordeiro - */ - public function ensure_public_key(Profile $profile, bool $fetch = true): string - { - $this->profile_id = $profile->getID(); - $apRSA = self::getKV('profile_id', $this->profile_id); - if (!$apRSA instanceof Activitypub_rsa) { - // No existing key pair for this profile - if ($profile->isLocal()) { - self::generate_keys($this->private_key, $this->public_key); - $this->store_keys(); - $apRSA->public_key = $this->public_key; - } else { - // ASSERT: This should never happen, but try to recover! - common_log(LOG_ERR, 'Activitypub_rsa: An impossible thing has happened... Please let the devs know that it entered in line 116 at Activitypub_rsa.php'); - if ($fetch) { - $res = Activitypub_explorer::get_remote_user_activity($profile->getUri()); - Activitypub_rsa::update_public_key($profile, $res['publicKey']['publicKeyPem']); - return self::ensure_public_key($profile, false); - } else { - throw new ServerException('Activitypub_rsa: Failed to find keys for given profile. That should have not happened!'); - } - } - } - return $apRSA->public_key; - } - - /** - * Insert the current object variables into the database. - * - * @throws ServerException - * - * @author Diogo Cordeiro - */ - public function store_keys(): void - { - $this->created = $this->modified = common_sql_now(); - $ok = $this->insert(); - if ($ok === false) { - throw new ServerException('Cannot save ActivityPub RSA.'); - } - } - - /** - * Generates a pair of RSA keys. - * - * @param string $private_key out - * @param string $public_key out - * - * @author PHP Manual Contributed Notes - */ - public static function generate_keys(?string &$private_key, ?string &$public_key): void - { - $config = [ - 'digest_alg' => 'sha512', - 'private_key_bits' => 2048, - 'private_key_type' => OPENSSL_KEYTYPE_RSA, - ]; - - // Create the private and public key - $res = openssl_pkey_new($config); - - // Extract the private key from $res to $private_key - openssl_pkey_export($res, $private_key); - - // Extract the public key from $res to $pubKey - $pubKey = openssl_pkey_get_details($res); - $public_key = $pubKey['key']; - unset($pubKey); - } - - /** - * Update public key. - * - * @param Activitypub_profile|Profile $profile - * @param string $public_key - * - * @throws Exception - * - * @author Diogo Cordeiro - */ - public static function update_public_key($profile, string $public_key): void - { - // Public Key - $apRSA = new Activitypub_rsa(); - $apRSA->profile_id = $profile->getID(); - $apRSA->public_key = $public_key; - $apRSA->created = common_sql_now(); - if (!$apRSA->update()) { - $apRSA->insert(); - } - } - - public static function schemaDef() - { - return [ - 'name' => 'activitypub_crypt_key', - 'description' => 'assymetric key storage for activitypub', - 'fields' => [ - 'gsactor_id' => ['type' => 'int', 'not null' => true], - 'private_key' => ['type' => 'text'], - 'public_key' => ['type' => 'text', 'not null' => true], - 'created' => ['type' => 'datetime', 'description' => 'date this record was created'], - 'modified' => ['type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'], - ], - 'primary key' => ['gsactor_id'], - 'foreign keys' => [ - 'activitypub_rsa_gsactor_id_fkey' => ['gsactor', ['gsactor_id' => 'id']], - ], - ]; - } -} diff --git a/plugins/ActivityPub/Entity/ActivityPubFollowRequests.php b/plugins/ActivityPub/Entity/ActivityPubFollowRequests.php deleted file mode 100644 index 397c3f7e0a..0000000000 --- a/plugins/ActivityPub/Entity/ActivityPubFollowRequests.php +++ /dev/null @@ -1,97 +0,0 @@ -. - -// }}} - -/** - * ActivityPub's Pending follow requests - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @author Hugo Sales - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ - -namespace Plugin\ActivityPub\Entity; - -class ActivityPubFollowRequests -{ - // {{{ Autocode - private int $local_gsactor_id; - private int $remote_gsactor_id; - private int $relation_id; - - public function setLocalGSActorId(int $local_gsactor_id): self - { - $this->local_gsactor_id = $local_gsactor_id; - return $this; - } - - public function getLocalGSActorId(): int - { - return $this->local_gsactor_id; - } - - public function setRemoteGSActorId(int $remote_gsactor_id): self - { - $this->remote_gsactor_id = $remote_gsactor_id; - return $this; - } - - public function getRemoteGSActorId(): int - { - return $this->remote_gsactor_id; - } - - public function setRelationId(int $relation_id): self - { - $this->relation_id = $relation_id; - return $this; - } - - public function getRelationId(): int - { - return $this->relation_id; - } - - // }}} Autocode - - public static function schemaDef() - { - return [ - 'name' => 'activitypub_pending_follow_requests', - 'fields' => [ - 'local_gsactor_id' => ['type' => 'int', 'not null' => true], - 'remote_gsactor_id' => ['type' => 'int', 'not null' => true], - 'relation_id' => ['type' => 'serial', 'not null' => true], - ], - 'primary key' => ['relation_id'], - 'foreign keys' => [ - 'activitypub_pending_follow_requests_local_gsactor_id_fkey' => ['gsactor', ['local_gsactor_id' => 'id']], - 'activitypub_pending_follow_requests_remote_gsactor_id_fkey' => ['gsactor', ['remote_gsactor_id' => 'id']], - ], - 'indexes' => [ - 'activitypub_pending_follow_requests_local_gsactor_id_idx' => ['local_gsactor_id'], - 'activitypub_pending_follow_requests_remote_gsactor_id_idx' => ['remote_gsactor_id'], - ], - ]; - } -} diff --git a/plugins/ActivityPub/README.md b/plugins/ActivityPub/README.md deleted file mode 100644 index 94029589dd..0000000000 --- a/plugins/ActivityPub/README.md +++ /dev/null @@ -1,47 +0,0 @@ -# ActivityPub plugin for GNU social -(c) 2018-2019 Free Software Foundation, Inc - -This is the README file for GNU social's ActivityPub plugin. -It includes general information about the plugin. - -## About - -This plugin adds [ActivityPub](https://www.w3.org/TR/activitypub/) support to GNU social. - -## Additional functionality - -The RemoteFollow plugin is recommended as it increases the UX significatively, -it adds a remote follow button to user profiles. - -## Credits - -* **[Diogo Cordeiro](https://www.diogo.site/)** - -## Special thanks - -* **[Daniel Supernault](https://github.com/dansup)** -* **[Mikael Nordfeldth](https://mmn-o.se/)** - -## License - -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, in the file "COPYING". If not, see -. - - IMPORTANT NOTE: The GNU Affero General Public License (AGPL) has - *different requirements* from the "regular" GPL. In particular, if - you make modifications to the plugin source code on your server, - you *MUST MAKE AVAILABLE* the modified version of the source code - to your users under the same license. This is a legal requirement - of using the software, and if you do not wish to share your - modifications, *YOU MAY NOT USE THIS PLUGIN*. diff --git a/plugins/ActivityPub/actions/apactorfollowers.php b/plugins/ActivityPub/actions/apactorfollowers.php deleted file mode 100644 index 1b37b99307..0000000000 --- a/plugins/ActivityPub/actions/apactorfollowers.php +++ /dev/null @@ -1,140 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * - * @see http://www.gnu.org/software/social/ - */ -defined('GNUSOCIAL') || die(); - -/** - * Actor's Followers Collection - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class apActorFollowersAction extends ManagedAction -{ - protected $needLogin = false; - protected $canPost = true; - - /** - * Handle the Followers Collection request - * - * @throws Exception - * - * @return void - * - * @author Diogo Cordeiro - */ - protected function handle() - { - try { - $profile = Profile::getByID($this->trimmed('id')); - $profile_id = $profile->getID(); - } catch (Exception $e) { - ActivityPubReturn::error('Invalid Actor URI.', 404); - } - - if (!$profile->isLocal()) { - ActivityPubReturn::error('This is not a local user.', 403); - } - - if (!isset($_GET['page'])) { - $page = 0; - } else { - $page = (int) ($this->trimmed('page')); - } - - if ($page < 0) { - ActivityPubReturn::error('Invalid page number.'); - } - - $since = ($page - 1) * PROFILES_PER_MINILIST; - $limit = PROFILES_PER_MINILIST; - - // Calculate total items - $total_subs = Activitypub_profile::subscriberCount($profile); - $total_pages = ceil($total_subs / PROFILES_PER_MINILIST); - - $res = [ - '@context' => [ - 'https://www.w3.org/ns/activitystreams', - 'https://w3id.org/security/v1', - ], - 'id' => common_local_url('apActorFollowers', ['id' => $profile_id]) . (($page != 0) ? '?page=' . $page : ''), - 'type' => ($page == 0 ? 'OrderedCollection' : 'OrderedCollectionPage'), - 'totalItems' => $total_subs, - ]; - - if ($page == 0) { - $res['first'] = common_local_url('apActorFollowers', ['id' => $profile_id]) . '?page=1'; - } else { - $res['orderedItems'] = $this->generate_followers($profile, $since, $limit); - $res['partOf'] = common_local_url('apActorFollowers', ['id' => $profile_id]); - - if ($page + 1 < $total_pages) { - $res['next'] = common_local_url('apActorFollowers', ['id' => $profile_id]) . 'page=' . ($page + 1 == 1 ? 2 : $page + 1); - } - - if ($page > 1) { - $res['prev'] = common_local_url('apActorFollowers', ['id' => $profile_id]) . '?page=' . ($page - 1 <= 0 ? 1 : $page - 1); - } - } - - ActivityPubReturn::answer($res); - } - - /** - * Generates a list of stalkers for a given profile. - * - * @param Profile $profile - * @param int $since - * @param int $limit - * - * @throws Exception - * - * @return array of URIs - * - * @author Diogo Cordeiro - */ - public static function generate_followers($profile, $since, $limit) - { - $subs = []; - try { - $sub = Activitypub_profile::getSubscribers($profile, $since, $limit); - - // Get followers' URLs - foreach ($sub as $s) { - $subs[] = $s->getUri(); - } - } catch (NoResultException $e) { - // Just let the exception go on its merry way - } - - return $subs; - } -} diff --git a/plugins/ActivityPub/actions/apactorfollowing.php b/plugins/ActivityPub/actions/apactorfollowing.php deleted file mode 100644 index fea7d2ea05..0000000000 --- a/plugins/ActivityPub/actions/apactorfollowing.php +++ /dev/null @@ -1,140 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * - * @see http://www.gnu.org/software/social/ - */ -defined('GNUSOCIAL') || die(); - -/** - * Actor's Following Collection - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class apActorFollowingAction extends ManagedAction -{ - protected $needLogin = false; - protected $canPost = true; - - /** - * Handle the Following Collection request - * - * @throws Exception - * - * @return void - * - * @author Diogo Cordeiro - */ - protected function handle() - { - try { - $profile = Profile::getByID($this->trimmed('id')); - $profile_id = $profile->getID(); - } catch (Exception $e) { - ActivityPubReturn::error('Invalid Actor URI.', 404); - } - - if (!$profile->isLocal()) { - ActivityPubReturn::error('This is not a local user.', 403); - } - - if (!isset($_GET['page'])) { - $page = 0; - } else { - $page = (int) ($this->trimmed('page')); - } - - if ($page < 0) { - ActivityPubReturn::error('Invalid page number.'); - } - - $since = ($page - 1) * PROFILES_PER_MINILIST; - $limit = PROFILES_PER_MINILIST; - - // Calculate total items - $total_subs = Activitypub_profile::subscriptionCount($profile); - $total_pages = ceil($total_subs / PROFILES_PER_MINILIST); - - $res = [ - '@context' => [ - 'https://www.w3.org/ns/activitystreams', - 'https://w3id.org/security/v1', - ], - 'id' => common_local_url('apActorFollowing', ['id' => $profile_id]) . (($page != 0) ? '?page=' . $page : ''), - 'type' => ($page == 0 ? 'OrderedCollection' : 'OrderedCollectionPage'), - 'totalItems' => $total_subs, - ]; - - if ($page == 0) { - $res['first'] = common_local_url('apActorFollowing', ['id' => $profile_id]) . '?page=1'; - } else { - $res['orderedItems'] = $this->generate_following($profile, $since, $limit); - $res['partOf'] = common_local_url('apActorFollowing', ['id' => $profile_id]); - - if ($page + 1 < $total_pages) { - $res['next'] = common_local_url('apActorFollowing', ['id' => $profile_id]) . 'page=' . ($page + 1 == 1 ? 2 : $page + 1); - } - - if ($page > 1) { - $res['prev'] = common_local_url('apActorFollowing', ['id' => $profile_id]) . '?page=' . ($page - 1 <= 0 ? 1 : $page - 1); - } - } - - ActivityPubReturn::answer($res); - } - - /** - * Generates the list of those a given profile is stalking. - * - * @param Profile $profile - * @param int $since - * @param int $limit - * - * @throws Exception - * - * @return array of URIs - * - * @author Diogo Cordeiro - */ - public function generate_following($profile, $since, $limit) - { - $subs = []; - try { - $sub = Activitypub_profile::getSubscribed($profile, $since, $limit); - - // Get followed' URLs - foreach ($sub as $s) { - $subs[] = $s->getUri(); - } - } catch (NoResultException $e) { - // Just let the exception go on its merry way - } - - return $subs; - } -} diff --git a/plugins/ActivityPub/actions/apactorliked.php b/plugins/ActivityPub/actions/apactorliked.php deleted file mode 100644 index ef50ec675c..0000000000 --- a/plugins/ActivityPub/actions/apactorliked.php +++ /dev/null @@ -1,162 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * - * @see http://www.gnu.org/software/social/ - */ -defined('GNUSOCIAL') || die(); - -/** - * Actor's Liked Collection - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class apActorLikedAction extends ManagedAction -{ - protected $needLogin = false; - protected $canPost = true; - - /** - * Handle the Liked Collection request - * - * @throws EmptyPkeyValueException - * @throws ServerException - * - * @return void - * - * @author Diogo Cordeiro - */ - protected function handle() - { - try { - $profile = Profile::getByID($this->trimmed('id')); - $profile_id = $profile->getID(); - } catch (Exception $e) { - ActivityPubReturn::error('Invalid Actor URI.', 404); - } - - if (!$profile->isLocal()) { - ActivityPubReturn::error('This is not a local user.', 403); - } - - $limit = (int) ($this->trimmed('limit')); - $since_id = (int) ($this->trimmed('since_id')); - $max_id = (int) ($this->trimmed('max_id')); - - $limit = empty($limit) ? 40 : $limit; // Default is 40 - $since_id = empty($since_id) ? null : $since_id; - $max_id = empty($max_id) ? null : $max_id; - - // Max is 80 - if ($limit > 80) { - $limit = 80; - } - - $fave = $this->fetch_faves($profile_id, $limit, $since_id, $max_id); - - $faves = []; - while ($fave->fetch()) { - $faves[] = $this->pretty_fave(clone $fave); - } - - $res = [ - '@context' => [ - 'https://www.w3.org/ns/activitystreams', - 'https://w3id.org/security/v1', - ], - 'id' => common_local_url('apActorLiked', ['id' => $profile_id]), - 'type' => 'OrderedCollection', - 'totalItems' => Fave::countByProfile($profile), - 'orderedItems' => $faves, - ]; - - ActivityPubReturn::answer($res); - } - - /** - * Take a fave object and turns it in a pretty array to be used - * as a plugin answer - * - * @param Fave $fave_object - * - * @throws EmptyPkeyValueException - * @throws ServerException - * - * @return array pretty array representating a Fave - * - * @author Diogo Cordeiro - */ - protected function pretty_fave($fave_object) - { - $res = [ - 'created' => $fave_object->created, - 'object' => Activitypub_notice::notice_to_array(Notice::getByID($fave_object->notice_id)), - ]; - - return $res; - } - - /** - * Fetch faves - * - * @author Diogo Cordeiro - * - * @param int $user_id - * @param int $limit - * @param int $since_id - * @param int $max_id - * - * @return Fave fetchable fave collection - */ - private static function fetch_faves( - $user_id, - $limit = 40, - $since_id = null, - $max_id = null - ) { - $fav = new Fave(); - - $fav->user_id = $user_id; - - $fav->orderBy('modified DESC, notice_id DESC'); - - if ($since_id != null) { - $fav->whereAdd("notice_id > {$since_id}"); - } - - if ($max_id != null) { - $fav->whereAdd("notice_id < {$max_id}"); - } - - $fav->limit($limit); - - $fav->find(); - - return $fav; - } -} diff --git a/plugins/ActivityPub/actions/apactoroutbox.php b/plugins/ActivityPub/actions/apactoroutbox.php deleted file mode 100644 index b7c74e5627..0000000000 --- a/plugins/ActivityPub/actions/apactoroutbox.php +++ /dev/null @@ -1,140 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * - * @see http://www.gnu.org/software/social/ - */ -defined('GNUSOCIAL') || die(); - -/** - * Inbox Request Handler - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class apActorOutboxAction extends ManagedAction -{ - protected $needLogin = false; - protected $canPost = true; - - /** - * Handle the Outbox request - * - * @author Daniel Supernault - */ - protected function handle() - { - try { - $profile = Profile::getByID($this->trimmed('id')); - $profile_id = $profile->getID(); - } catch (Exception $e) { - ActivityPubReturn::error('Invalid Actor URI.', 404); - } - - if (!$profile->isLocal()) { - ActivityPubReturn::error('This is not a local user.', 403); - } - - if (!isset($_GET['page'])) { - $page = 0; - } else { - $page = (int) ($this->trimmed('page')); - } - - if ($page < 0) { - ActivityPubReturn::error('Invalid page number.'); - } - - $since = ($page - 1) * PROFILES_PER_MINILIST; - $limit = (($page - 1) == 0 ? 1 : $page) * PROFILES_PER_MINILIST; - - // Calculate total items - $total_notes = $profile->noticeCount(); - $total_pages = ceil($total_notes / PROFILES_PER_MINILIST); - - $res = [ - '@context' => [ - 'https://www.w3.org/ns/activitystreams', - 'https://w3id.org/security/v1', - ], - 'id' => common_local_url('apActorOutbox', ['id' => $profile_id]) . (($page != 0) ? '?page=' . $page : ''), - 'type' => ($page == 0 ? 'OrderedCollection' : 'OrderedCollectionPage'), - 'totalItems' => $total_notes, - ]; - - if ($page == 0) { - $res['first'] = common_local_url('apActorOutbox', ['id' => $profile_id]) . '?page=1'; - } else { - $res['orderedItems'] = $this->generate_outbox($profile); - $res['partOf'] = common_local_url('apActorOutbox', ['id' => $profile_id]); - - if ($page + 1 < $total_pages) { - $res['next'] = common_local_url('apActorOutbox', ['id' => $profile_id]) . 'page=' . ($page + 1 == 1 ? 2 : $page + 1); - } - - if ($page > 1) { - $res['prev'] = common_local_url('apActorOutbox', ['id' => $profile_id]) . '?page=' . ($page - 1 <= 0 ? 1 : $page - 1); - } - } - - ActivityPubReturn::answer($res); - } - - /** - * Generates a list of people following given profile. - * - * @param Profile $profile - * - * @throws EmptyPkeyValueException - * @throws InvalidUrlException - * @throws ServerException - * - * @return array of Notices - * - * @author Daniel Supernault - */ - public function generate_outbox($profile) - { - // Fetch Notices - $notices = []; - $notice = $profile->getNotices(); - while ($notice->fetch()) { - $note = $notice; - - // TODO: Handle other types - if ($note->object_type == 'http://activitystrea.ms/schema/1.0/note') { - $notices[] = Activitypub_create::create_to_array( - $note->getProfile()->getUri(), - common_local_url('apNotice', ['id' => $note->getID()]), - Activitypub_notice::notice_to_array($note) - ); - } - } - - return $notices; - } -} diff --git a/plugins/ActivityPub/actions/apactorprofile.php b/plugins/ActivityPub/actions/apactorprofile.php deleted file mode 100644 index 7756534dd7..0000000000 --- a/plugins/ActivityPub/actions/apactorprofile.php +++ /dev/null @@ -1,79 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * - * @see http://www.gnu.org/software/social/ - */ -defined('GNUSOCIAL') || die(); - -/** - * Actor's profile (Local users only) - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class apActorProfileAction extends ManagedAction -{ - protected $needLogin = false; - protected $canPost = true; - - /** - * Handle the Actor Profile request - * - * @throws InvalidUrlException - * @throws ServerException - * - * @return void - * - * @author Diogo Cordeiro - */ - protected function handle() - { - if (!empty($id = $this->trimmed('id'))) { - try { - $profile = Profile::getByID($id); - } catch (Exception $e) { - ActivityPubReturn::error('Invalid Actor URI.', 404); - } - unset($id); - } else { - try { - $profile = User::getByNickname($this->trimmed('nickname'))->getProfile(); - } catch (Exception $e) { - ActivityPubReturn::error('Invalid username.', 404); - } - } - - if (!$profile->isLocal()) { - ActivityPubReturn::error('This is not a local user.', 403); - } - - $res = Activitypub_profile::profile_to_array($profile); - - ActivityPubReturn::answer($res, 200); - } -} diff --git a/plugins/ActivityPub/actions/apinbox.php b/plugins/ActivityPub/actions/apinbox.php deleted file mode 100644 index d2cc117d31..0000000000 --- a/plugins/ActivityPub/actions/apinbox.php +++ /dev/null @@ -1,148 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * - * @see http://www.gnu.org/software/social/ - */ -defined('GNUSOCIAL') || die(); - -/** - * Inbox Request Handler - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class apInboxAction extends ManagedAction -{ - protected $needLogin = false; - protected $canPost = true; - - /** - * Handle the Inbox request - * - * @throws ServerException - * - * @return void - * - * @author Diogo Cordeiro - */ - protected function handle() - { - $path = !empty($this->trimmed('id')) ? common_local_url('apInbox', ['id' => $this->trimmed('id')]) : common_local_url('apInbox'); - $path = parse_url($path, PHP_URL_PATH); - - if ($_SERVER['REQUEST_METHOD'] !== 'POST') { - ActivityPubReturn::error('Only POST requests allowed.'); - } - - common_debug('ActivityPub Inbox: Received a POST request.'); - $body = $data = file_get_contents('php://input'); - common_debug('ActivityPub Inbox: Request contents: ' . $data); - $data = json_decode(file_get_contents('php://input'), true); - - if (!isset($data['actor'])) { - ActivityPubReturn::error('Actor not found in the request.'); - } - - try { - $actor = Activitypub_explorer::get_profile_from_url($data['actor']); - } catch (HTTP_Request2_Exception $e) { - ActivityPubReturn::error('Failed to retrieve remote actor information.'); - } catch (NoProfileException $e) { - // Assert: This won't happen. - common_log(LOG_ERR, 'PLEASE REPORT THIS: ActivityPub Inbox Handler failed with NoProfileException while retrieving remote actor information: ' . $e->getMessage()); - ActivityPubReturn::error('An unknown error has occurred. This was logged, please alert the sysadmin.'); - } catch (ServerException $e) { - ActivityPubReturn::error('Could not store this remote actor.'); - } catch (Exception $e) { - ActivityPubReturn::error('Invalid actor.'); - } - try { - $aprofile = Activitypub_profile::from_profile($actor); - } catch (Exception $e) { - // Assert: This won't happen. - common_log(LOG_ERR, 'PLEASE REPORT THIS: ActivityPub Inbox Handler failed while retrieving AProfile from Profile: ' . $e->getMessage()); - ActivityPubReturn::error('An unknown error has occurred. This was logged, please alert the sysadmin.'); - } - - $actor_public_key = new Activitypub_rsa(); - $actor_public_key = $actor_public_key->ensure_public_key($actor); - - common_debug('ActivityPub Inbox: HTTP Signature: Validation will now start!'); - - $headers = getallheaders(); - common_debug('ActivityPub Inbox: Request Headers: ' . print_r($headers, true)); - - if (!isset($headers['Signature'])) { - common_debug('ActivityPub Inbox: HTTP Signature: Missing Signature header.'); - ActivityPubReturn::error('Missing Signature header.', 400); - } - - // Extract the signature properties - $signatureData = HTTPSignature::parseSignatureHeader($headers['Signature']); - common_debug('ActivityPub Inbox: HTTP Signature Data: ' . print_r($signatureData, true)); - if (isset($signatureData['error'])) { - common_debug('ActivityPub Inbox: HTTP Signature: ' . json_encode($signatureData, true)); - ActivityPubReturn::error(json_encode($signatureData, true), 400); - } - - list($verified, /*$headers*/) = HTTPSignature::verify($actor_public_key, $signatureData, $headers, $path, $body); - - // If the signature fails verification the first time, update profile as it might have changed public key - if ($verified !== 1) { - try { - $res = Activitypub_explorer::get_remote_user_activity($aprofile->getUri()); - } catch (Exception $e) { - ActivityPubReturn::error('Invalid remote actor.'); - } - try { - $actor = Activitypub_profile::update_profile($aprofile, $res); - } catch (Exception $e) { - ActivityPubReturn::error('Failed to updated remote actor information.'); - } - $actor_public_key = new Activitypub_rsa(); - $actor_public_key = $actor_public_key->ensure_public_key($actor); - list($verified, /*$headers*/) = HTTPSignature::verify($actor_public_key, $signatureData, $headers, $path, $body); - } - - // If it still failed despite profile update - if ($verified !== 1) { - common_debug('ActivityPub Inbox: HTTP Signature: Invalid signature.'); - ActivityPubReturn::error('Invalid signature.'); - } - - // HTTP signature checked out, make sure the "actor" of the activity matches that of the signature - common_debug('ActivityPub Inbox: HTTP Signature: Authorized request. Will now start the inbox handler.'); - - try { - new Activitypub_inbox_handler($data, $actor); - ActivityPubReturn::answer(); - } catch (Exception $e) { - ActivityPubReturn::error($e->getMessage()); - } - } -} diff --git a/plugins/ActivityPub/actions/apnotice.php b/plugins/ActivityPub/actions/apnotice.php deleted file mode 100644 index c468a0e2c4..0000000000 --- a/plugins/ActivityPub/actions/apnotice.php +++ /dev/null @@ -1,186 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * - * @see http://www.gnu.org/software/social/ - */ -defined('GNUSOCIAL') || die(); - -/** - * Notice (Local notices only) - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class apNoticeAction extends ManagedAction -{ - protected $needLogin = false; - protected $canPost = true; - - /** - * Notice id - * @var int - */ - public $notice_id; - - /** - * Notice object to show - */ - public $notice = null; - - /** - * Profile of the notice object - */ - public $profile = null; - - /** - * Avatar of the profile of the notice object - */ - public $avatar = null; - - /** - * Load attributes based on database arguments - * - * Loads all the DB stuff - * - * @param array $args $_REQUEST array - * - * @return bool success flag - */ - protected function prepare(array $args = []): bool - { - parent::prepare($args); - - $this->notice_id = (int)$this->trimmed('id'); - - try { - $this->notice = $this->getNotice(); - } catch (ClientException $e) { - //ActivityPubReturn::error('Activity deleted.', 410); - ActivityPubReturn::answer(Activitypub_tombstone::tombstone_to_array($this->notice_id), 410); - } - $this->target = $this->notice; - - if (!$this->notice->inScope($this->scoped)) { - // TRANS: Client exception thrown when trying a view a notice the user has no access to. - throw new ClientException(_m('Access restricted.'), 403); - } - - $this->profile = $this->notice->getProfile(); - - if (!$this->profile instanceof Profile) { - // TRANS: Server error displayed trying to show a notice without a connected profile. - $this->serverError(_m('Notice has no profile.'), 500); - } - - try { - $this->avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE); - } catch (Exception $e) { - $this->avatar = null; - } - - return true; - } - - /** - * Is this action read-only? - * - * @return bool true - */ - public function isReadOnly($args): bool - { - return true; - } - - /** - * Last-modified date for page - * - * When was the content of this page last modified? Based on notice, - * profile, avatar. - * - * @return int last-modified date as unix timestamp - */ - public function lastModified(): int - { - return max(strtotime($this->notice->modified), - strtotime($this->profile->modified), - ($this->avatar) ? strtotime($this->avatar->modified) : 0); - } - - /** - * Handle the Notice request - * - * @throws EmptyPkeyValueException - * @throws InvalidUrlException - * @throws ServerException - * - * @return void - * - * @author Diogo Cordeiro - */ - protected function handle(): void - { - if (is_null($this->notice)) { - ActivityPubReturn::error('Invalid Activity URI.', 404); - } - - if (!$notice->isLocal()) { - ActivityPubReturn::error('This is not a local notice.', 403); - } - - $res = Activitypub_notice::notice_to_array($this->notice); - - ActivityPubReturn::answer($res); - } - - /** - * Fetch the notice to show. This may be overridden by child classes to - * customize what we fetch without duplicating all of the prepare() method. - * - * @return null|Notice null if not found - * @throws ClientException If GONE - */ - protected function getNotice(): ?Notice - { - $notice = null; - try { - $notice = Notice::getByID($this->notice_id); - // Alright, got it! - return $notice; - } catch (NoResultException $e) { - // Hm, not found. - $deleted = null; - Event::handle('IsNoticeDeleted', [$this->notice_id, &$deleted]); - if ($deleted === true) { - // TRANS: Client error displayed trying to show a deleted notice. - throw new ClientException(_m('Notice deleted.'), 410); - } - } - // No such notice. - return null; - } -} diff --git a/plugins/ActivityPub/doc/index.html b/plugins/ActivityPub/doc/index.html deleted file mode 100644 index 93a611cc3d..0000000000 --- a/plugins/ActivityPub/doc/index.html +++ /dev/null @@ -1,24 +0,0 @@ - - - - ActivityPub plugin for GNU Social API Documentation - - - - - - - - - - - - - diff --git a/plugins/ActivityPub/doc/objects_and_activities.md b/plugins/ActivityPub/doc/objects_and_activities.md deleted file mode 100644 index a48f20f2de..0000000000 --- a/plugins/ActivityPub/doc/objects_and_activities.md +++ /dev/null @@ -1,101 +0,0 @@ -ActivityPub Plugin for GNU Social Doc -===================================== - -## Contents - -- [Objects](#entities) - - [Attachment](#attachment) - - [Error](#error) - - [Image](#image) - - [Notice](#notice) - - [Profile](#profile) - - [Tag](#tag) -- [Activities](#activities) - - [Create](#create) - - [Announce](#announce) - - [Delete](#delete) - - [Undo](#undo) - - [Accept](#accept) - - [Reject](#reject) - - [Like](#like) - - [Follow](#follow) - -## Objects - -### Attachment - -| Attribute | Description | Nullable | Type | -| ------------------------ | ----------------------------------------------- | -------- | ------- | -| `id` | ID of the attachment | no | int32 | -| `mimetype` | Mimetype | no | string | -| `url` | URL of the locally hosted version of the image | no | string | -| `meta` | See **attachment metadata** below | yes | Array | -| `title` | Attachment title | no | string | - -**Attachment metadata:** - -Images may contain `width`, `height`, `size`. - -### Error - -The most important part of an error response is the HTTP status code. Standard semantics are followed. The body of an error is a JSON object with this structure: - -| Attribute | Description | Nullable | Type | -| ------------------------ | ----------------------------------- | -------- | ------- | -| `error` | A textual description of the error | no | string | - -### Image - -| Attribute | Description | Nullable | Type | -| ------------------------ | --------------- | -------- | ------- | -| `type` | "Image" | no | string | -| `width` | Image's width | no | int32 | -| `height` | Image's height | no | int32 | -| `url` | Image URL | no | string | - -### Notice - -| Attribute | Description | Nullable | Type | -| ------------------------ | ------------------------------------------------- | -------- | ------------------------------------ | -| `id` | Notice's URL | no | string | -| `type` | Notice's Type | no | string | -| `actor` | URL of Notice owner profile page (can be remote) | no | string | -| `published` | DateTime of notice creation | no | datetime | -| `to` | To | no | string | -| `cc` | CC | no | string | -| `content` | Notice's Content in plain text | no | string | -| `url` | Notice's URL | no | string | -| `reply_to` | ID of the notice this replies | yes | int32 | -| `is_local` | Boolean, true if local, false otherwise | no | bool | -| `conversation` | Notice conversation id | no | int32 | -| `attachment` | Array of [Attachments](#attachment) | no | Array of [Attachments](#attachment) | -| `tag` | Array of [Tags](#tag) | no | Array of [Tags](#tag) | - -### Profile - -| Attribute | Description | Nullable | Type | -| ------------------------ | ------------------------------------------------ | -------- | ---------------- | -| `@context` | Standard compliance | no | string | -| `id` | Actor's id | no | int32 | -| `type` | "Person" | no | string | -| `nickname` | Actor's nickname | no | string | -| `is_local` | True if local, false otherwise | no | bool | -| `inbox` | URL to Actor's inbox endpoint | no | string | -| `outbox` | URL to Actor's outbox endpoint | no | string | -| `display_name` | The Actor's display name | no | string | -| `followers` | URL to Actor's followers endpoint | no | string | -| `followers_count` | Total number of followers | no | int32 | -| `following` | URL to Actor's following endpoint | no | string | -| `following_count` | Total number of following | no | int32 | -| `liked` | URL to Actor's Liked collection endpoint | no | string | -| `liked_count` | Total number of favorites | no | int32 | -| `summary` | Actor's biography | no | string | -| `url` | URL of the Actor's profile page (can be remote) | no | string | -| `avatar` | Actor's avatar | no | [Image](#image) | - -### Tag - -| Attribute | Description | Nullable | Type | -| ------------------------ | -------------------------------------------- | -------- | ---------- | -| `name` | The hashtag, not including the preceding `#` | no | string | -| `url` | The URL of the hashtag | no | string | diff --git a/plugins/ActivityPub/doc/openapi.json b/plugins/ActivityPub/doc/openapi.json deleted file mode 100644 index f39d9a344b..0000000000 --- a/plugins/ActivityPub/doc/openapi.json +++ /dev/null @@ -1,673 +0,0 @@ -{ - "openapi": "3.0.0", - "info": { - "description": "## Retrieving objects\nThe HTTP GET method may be dereferenced against an object's `id` property to retrieve the activity.\nThe plugin supports HTTP content negotiation as defined in [RFC7231](https://tools.ietf.org/html/rfc7231) in every endpoint suffixed with .json .\nThe plugin always presents ActivityStreams object representation in response to every request.\nThe client MUST specify an `Accept` header with the `application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"` media type in order to retrieve the activity.\n\n## Selecting ranges\n\nFor most `GET` operations that return arrays, the query parameters `max_id` and `since_id` can be used to specify the range of IDs to return.\nAPI methods that return collections of items can return a `Link` header containing URLs for the `next` and `prev` pages.\nSee the [Link header RFC](https://tools.ietf.org/html/rfc5988) for more information.\n\n## Pretty output\n\nFor most operations if the `pretty` parameter is set a formated output will be generated (useful for learning about the API or debuging purposes).\n\n## Errors\n\nIf the request you make doesn't go through, the plugin will usually respond with an [Error](#error).\n\n___\n\n> **Note:** Some attributes in the payload can have ``null`` value and are marked as _nullable_ on the tables below. Attributes that are not nullable are guaranteed to return a valid value.", - "version": "1.0.0", - "title": "ActivityPub plugin for GNU Social", - "contact": { - "email": "diogo@fc.up.pt" - }, - "license": { - "name": "AGPLv3", - "url": "https://git.gnu.io/gnu/GS-ActivityPub-plugin/blob/COPYING" - } - }, - "externalDocs": { - "description": "For a complete definition of the objects and activities available click here.", - "url": "https://git.gnu.io/gnu/GS-ActivityPub-plugin/doc/objects_and_activities.md" - }, - "paths": { - "/{nickname}": { - "get": { - "summary": "Fetching an Actor's profile", - "description": "Returns a Profile.", - "responses": { - "200": { - "description": "Returns a Profile", - "content": { - "application/json": { - "schema": { - "type": "array", - "items": { - "$ref": "#/components/schemas/Profile" - } - } - } - } - } - }, - "parameters": [ - { - "name": "Content-Type", - "in": "header", - "required": true, - "schema": { - "type": "string", - "default": "application/json" - } - }, - { - "name": "Accept", - "in": "header", - "required": true, - "schema": { - "type": "string", - "default": "application/activity+json" - } - } - ] - } - }, - "/{nickname}/inbox.json": { - "post": { - "summary": "Actor Inbox endpoint", - "description": "Allows the publish of activities with attention to a given Actor", - "parameters": [ - { - "name": "Content-Type", - "in": "header", - "required": true, - "schema": { - "type": "string", - "default": "application/json" - } - }, - { - "name": "Accept", - "in": "header", - "required": true, - "schema": { - "type": "string", - "default": "application/activity+json" - } - } - ], - "responses": { - "200": { - "description": "Returns the same activity it received" - } - } - } - }, - "/{nickname}/liked.json": { - "get": { - "summary": "Liked Collection", - "description": "Getting an Actor's Liked Collection", - "parameters": [ - { - "name": "Content-Type", - "in": "header", - "required": true, - "schema": { - "type": "string", - "default": "application/json" - } - }, - { - "name": "Accept", - "in": "header", - "required": true, - "schema": { - "type": "string", - "default": "application/activity+json" - } - }, - { - "name": "max_id", - "in": "query", - "required": false, - "schema": { - "type": "integer", - "format": "int32" - }, - "description": "Get a list of likes with ID less than this value", - "format": "int32" - }, - { - "name": "since_id", - "in": "query", - "required": false, - "schema": { - "type": "integer", - "format": "int32" - }, - "description": "Get a list of likes with ID greater than this value" - }, - { - "name": "limit", - "in": "query", - "required": false, - "schema": { - "type": "integer", - "format": "int32", - "default": "40" - }, - "description": "Maximum number of likes to get (Max 80)" - } - ], - "responses": { - "200": { - "description": "Returns Actor's Liked Collection", - "content": { - "application/json": { - "schema": { - "type": "array", - "items": { - "$ref": "#/components/schemas/Liked Collection" - } - } - } - } - } - } - } - }, - "/{nickname}/followers.json": { - "get": { - "summary": "Followers Collection", - "description": "Getting an Actor's Followers Collection", - "parameters": [ - { - "name": "Content-Type", - "in": "header", - "required": true, - "schema": { - "type": "string", - "default": "application/json" - } - }, - { - "name": "Accept", - "in": "header", - "required": true, - "schema": { - "type": "string", - "default": "application/activity+json" - } - }, - { - "name": "page", - "in": "query", - "required": false, - "schema": { - "type": "integer", - "format": "int32", - "default": "1" - }, - "description": "Page index (starts in 1)" - } - ], - "responses": { - "200": { - "description": "Returns Actor's Followers Collection", - "content": { - "application/json": { - "schema": { - "type": "array", - "items": { - "$ref": "#/components/schemas/Follow Collection" - } - } - } - } - } - } - } - }, - "/{nickname}/following.json": { - "get": { - "summary": "Following Collection", - "description": "Getting an Actor's Following Collection", - "parameters": [ - { - "name": "Content-Type", - "in": "header", - "required": true, - "schema": { - "type": "string", - "default": "application/json" - } - }, - { - "name": "Accept", - "in": "header", - "required": true, - "schema": { - "type": "string", - "default": "application/activity+json" - } - }, - { - "name": "page", - "in": "query", - "required": false, - "schema": { - "type": "integer", - "format": "int32", - "default": "1" - }, - "description": "Page index (starts in 1)" - } - ], - "responses": { - "200": { - "description": "Returns Actor's Following Collection", - "content": { - "application/json": { - "schema": { - "type": "array", - "items": { - "$ref": "#/components/schemas/Follow Collection" - } - } - } - } - } - } - } - }, - "/inbox.json": { - "post": { - "summary": "SharedInbox endpoint", - "description": "Allows the publish of activities", - "parameters": [ - { - "name": "Content-Type", - "in": "header", - "required": true, - "schema": { - "type": "string", - "default": "application/json" - } - }, - { - "name": "Accept", - "in": "header", - "required": true, - "schema": { - "type": "string", - "default": "application/activity+json" - } - } - ], - "responses": { - "200": { - "description": "Returns the same activity it received" - } - } - } - } - }, - "components": { - "schemas": { - "Note": { - "type": "object", - "required": [ - "@context", - "id", - "type", - "actor", - "published", - "to", - "cc", - "content", - "url", - "reply_to", - "is_local", - "conversation", - "attachment", - "tag" - ], - "properties": { - "@context": { - "type": "string", - "value": "https://www.w3.org/ns/activitystreams", - "default": "https://www.w3.org/ns/activitystreams" - }, - "id": { - "description": "Notice's URI", - "type": "string" - }, - "type": { - "description": "Notice's Type", - "type": "string" - }, - "actor": { - "description": "URL of Notice owner profile page (can be remote)", - "type": "string" - }, - "published": { - "description": "DateTime of notice creation", - "type": "string", - "format": "date-time" - }, - "to": { - "description": "To", - "type": "string" - }, - "cc": { - "description": "CC", - "type": "string" - }, - "content": { - "description": "Notice's Content in plain text", - "type": "string" - }, - "url": { - "description": "Notice's URL", - "type": "string" - }, - "reply_to": { - "description": "ID of the notice this replies", - "type": "string" - }, - "is_local": { - "description": "true if local, false otherwise", - "type": "string" - }, - "conversation": { - "description": "Notice conversation id", - "type": "integer", - "format": "int32" - }, - "attachment": { - "description": "Array of Attachments", - "type": "Array of Attachments" - }, - "tag": { - "description": "Array of Tags", - "type": "Array of Tags" - } - }, - "xml": { - "name": "Note" - } - }, - "Image": { - "type": "object", - "required": [ - "@context", - "type", - "width", - "height", - "url" - ], - "properties": { - "@context": { - "type": "string", - "value": "https://www.w3.org/ns/activitystreams", - "default": "https://www.w3.org/ns/activitystreams" - }, - "type": { - "description": "Image", - "type": "string" - }, - "width": { - "description": "Image's width", - "type": "integer", - "format": "int32" - }, - "height": { - "description": "Image's height", - "type": "integer", - "format": "int32" - }, - "url": { - "description": "Image URL", - "type": "string" - } - }, - "xml": { - "name": "Image" - } - }, - "Attachment": { - "type": "object", - "required": [ - "@context", - "id", - "mimetype", - "url", - "title" - ], - "properties": { - "@context": { - "type": "string", - "value": "https://www.w3.org/ns/activitystreams", - "default": "https://www.w3.org/ns/activitystreams" - }, - "id": { - "type": "integer", - "format": "int32", - "description": "Id of the Attachment" - }, - "mimetype": { - "type": "string", - "description": "Mimetype" - }, - "url": { - "type": "string", - "description": "URL of locally hosted version of the attachment" - }, - "meta": { - "type": "array", - "description": "Attachment metadata:\n\nImages may contain *width*, *height*, *size*.", - "items": {} - }, - "title": { - "type": "string", - "description": "Attachment title" - } - }, - "xml": { - "name": "Attachment" - } - }, - "Profile": { - "type": "object", - "required": [ - "@context", - "id", - "nickname", - "is_local", - "inbox", - "outbox", - "display_name", - "followers", - "followers_count", - "following", - "following_count", - "liked", - "liked_count", - "summary", - "url", - "avatar" - ], - "properties": { - "@context": { - "type": "string", - "value": "https://www.w3.org/ns/activitystreams", - "default": "https://www.w3.org/ns/activitystreams" - }, - "id": { - "type": "string", - "description": "Local URI" - }, - "type": "Person", - "nickname": { - "type": "string", - "description": "Actor's nickname" - }, - "is_local": { - "type": "boolean", - "description": "True if local, false otherwise" - }, - "inbox": { - "type": "string", - "description": "URL to Actor's inbox endpoint" - }, - "sharedInbox": { - "type": "string", - "description": "URL to Actor's sharedInbox endpoint" - }, - "outbox": { - "type": "string", - "description": "URL to Actor's outbox endpoint" - }, - "display_name": { - "type": "string", - "description": "The Actor's display name" - }, - "followers": { - "type": "string", - "description": "URL to Actor's followers collection" - }, - "followers_count": { - "type": "integer", - "format": "int32", - "description": "Total number of followers" - }, - "following": { - "type": "string", - "description": "URL to Actor's following collection" - }, - "following_count": { - "type": "integer", - "format": "int32", - "description": "Total number of following" - }, - "liked": { - "type": "string", - "description": "URL to Actor's Liked collection" - }, - "liked_count": { - "type": "integer", - "format": "int32", - "description": "Total number of favorites" - }, - "summary": { - "type": "string", - "description": "Actor's biography" - }, - "url": { - "type": "string", - "description": "URL of the Actor's profile page (can be remote)" - }, - "avatar": { - "type": "Image", - "description": "Actor's avatar" - } - }, - "xml": { - "name": "Profile" - } - }, - "Tag": { - "type": "object", - "required": [ - "@context", - "name", - "url" - ], - "properties": { - "@context": { - "type": "string", - "value": "https://www.w3.org/ns/activitystreams", - "default": "https://www.w3.org/ns/activitystreams" - }, - "name": { - "type": "string", - "description": "The hashtag, not including the preceding #" - }, - "url": { - "type": "string", - "description": "The URL of the hashtag" - } - }, - "xml": { - "name": "Tag" - } - }, - "Liked Collection": { - "type": "object", - "required": [ - "@context", - "id", - "type", - "totalItems", - "orderedItems" - ], - "properties": { - "@context": { - "type": "string", - "value": "https://www.w3.org/ns/activitystreams", - "default": "https://www.w3.org/ns/activitystreams" - }, - "id": { - "type": "string", - "description": "URL for current endpoint" - }, - "type": { - "type": "string", - "description": "OrderedCollection" - }, - "totalItems": { - "type": "integer", - "format": "int32", - "description": "Total number of favorites" - }, - "orderedItems": { - "type": "Array of Notices", - "description": "Array of Notices" - } - } - }, - "Follow Collection": { - "type": "object", - "required": [ - "@context", - "id", - "type", - "totalItems", - "orderedItems" - ], - "properties": { - "@context": { - "type": "string", - "value": "https://www.w3.org/ns/activitystreams", - "default": "https://www.w3.org/ns/activitystreams" - }, - "id": { - "type": "string", - "description": "URL for current endpoint" - }, - "type": { - "type": "string", - "description": "OrderedCollection" - }, - "totalItems": { - "type": "integer", - "format": "int32", - "description": "Total number of items" - }, - "prev": { - "type": "string", - "description": "Previous page URL" - }, - "next": { - "type": "string", - "description": "Next page URL" - }, - "orderedItems": { - "type": "Array of string", - "description": "The URL of each profile" - } - } - } - }, - "links": {}, - "callbacks": {} - }, - "security": [], - "servers": [] -} diff --git a/plugins/ActivityPub/lib/Activitypub_activityverb2.php b/plugins/ActivityPub/lib/Activitypub_activityverb2.php deleted file mode 100644 index f0af878ff7..0000000000 --- a/plugins/ActivityPub/lib/Activitypub_activityverb2.php +++ /dev/null @@ -1,100 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * - * @see http://www.gnu.org/software/social/ - */ -defined('GNUSOCIAL') || die(); - -/** - * Utility class to hold a bunch of constant defining default verb types - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class Activitypub_activityverb2 extends Managed_DataObject -{ - const FULL_LIST = [ - 'Accept' => 'https://www.w3.org/ns/activitystreams#Accept', - 'TentativeAccept' => 'https://www.w3.org/ns/activitystreams#TentativeAccept', - 'Add' => 'https://www.w3.org/ns/activitystreams#Add', - 'Arrive' => 'https://www.w3.org/ns/activitystreams#Arrive', - 'Create' => 'https://www.w3.org/ns/activitystreams#Create', - 'Delete' => 'https://www.w3.org/ns/activitystreams#Delete', - 'Follow' => 'https://www.w3.org/ns/activitystreams#Follow', - 'Ignore' => 'https://www.w3.org/ns/activitystreams#Ignore', - 'Join' => 'https://www.w3.org/ns/activitystreams#Join', - 'Leave' => 'https://www.w3.org/ns/activitystreams#Leave', - 'Like' => 'https://www.w3.org/ns/activitystreams#Like', - 'Offer' => 'https://www.w3.org/ns/activitystreams#Offer', - 'Invite' => 'https://www.w3.org/ns/activitystreams#Invite', - 'Reject' => 'https://www.w3.org/ns/activitystreams#Reject', - 'TentativeReject' => 'https://www.w3.org/ns/activitystreams#TentativeReject', - 'Remove' => 'https://www.w3.org/ns/activitystreams#Remove', - 'Undo' => 'https://www.w3.org/ns/activitystreams#Undo', - 'Update' => 'https://www.w3.org/ns/activitystreams#Update', - 'View' => 'https://www.w3.org/ns/activitystreams#View', - 'Listen' => 'https://www.w3.org/ns/activitystreams#Listen', - 'Read' => 'https://www.w3.org/ns/activitystreams#Read', - 'Move' => 'https://www.w3.org/ns/activitystreams#Move', - 'Travel' => 'https://www.w3.org/ns/activitystreams#Travel', - 'Announce' => 'https://www.w3.org/ns/activitystreams#Announce', - 'Block' => 'https://www.w3.org/ns/activitystreams#Block', - 'Flag' => 'https://www.w3.org/ns/activitystreams#Flag', - 'Dislike' => 'https://www.w3.org/ns/activitystreams#Dislike', - 'Question' => 'https://www.w3.org/ns/activitystreams#Question', - ]; - - const KNOWN = [ - 'Accept', - 'Create', - 'Delete', - 'Follow', - 'Like', - 'Undo', - 'Announce', - ]; - - /** - * Converts canonical into verb. - * - * @author GNU social - * - * @param string $verb - * - * @return string - */ - public static function canonical($verb) - { - $ns = 'https://www.w3.org/ns/activitystreams#'; - if (substr($verb, 0, mb_strlen($ns)) == $ns) { - return substr($verb, mb_strlen($ns)); - } else { - return $verb; - } - } -} diff --git a/plugins/ActivityPub/lib/activitypubfailedqueuehandler.php b/plugins/ActivityPub/lib/activitypubfailedqueuehandler.php deleted file mode 100644 index 4503fe726f..0000000000 --- a/plugins/ActivityPub/lib/activitypubfailedqueuehandler.php +++ /dev/null @@ -1,158 +0,0 @@ -. - -/** - * ActivityPub queue handler for notice distribution - * - * @package GNUsocial - * @author Diogo Cordeiro - * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ - -defined('GNUSOCIAL') || die(); - -/** - * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class ActivityPubFailedQueueHandler extends QueueHandler -{ - /** - * Getter of the queue transport name. - * - * @return string transport name - */ - public function transport(): string - { - return 'activitypub_failed'; - } - - /** - * Notice distribution handler. - * - * @param array $to_failed [string to, Notice]. - * @return bool true on success, false otherwise - * @throws HTTP_Request2_Exception - * @throws InvalidUrlException - * @throws ServerException - * @author Diogo Cordeiro - */ - public function handle($to_failed): bool - { - [$other, $notice] = $to_failed; - if (!($notice instanceof Notice)) { - common_log(LOG_ERR, 'Got a bogus notice, not distributing'); - return true; - } - - $profile = $notice->getProfile(); - - if (!$profile->isLocal()) { - return true; - } - - if ($notice->source == 'activity') { - common_log(LOG_ERR, "Ignoring distribution of notice:{$notice->id}: activity source"); - return true; - } - - try { - // Handling a Create? - if (ActivityUtils::compareVerbs($notice->verb, [ActivityVerb::POST, ActivityVerb::SHARE])) { - return $this->handle_create($profile, $notice, $other); - } - - // Handling a Like? - if (ActivityUtils::compareVerbs($notice->verb, [ActivityVerb::FAVORITE])) { - return $this->onEndFavorNotice($profile, $notice, $other); - } - - // Handling a Delete Note? - if (ActivityUtils::compareVerbs($notice->verb, [ActivityVerb::DELETE])) { - return $this->onStartDeleteOwnNotice($profile, $notice, $other); - } - } catch (Exception $e) { - // Postman already re-enqueues for us - common_debug('ActivityPub Failed Queue Handler:'.$e->getMessage()); - } - - return true; - } - - private function handle_create($profile, $notice, $other) - { - // Handling an Announce? - if ($notice->isRepeat()) { - $repeated_notice = Notice::getKV('id', $notice->repeat_of); - if ($repeated_notice instanceof Notice) { - // That was it - $postman = new Activitypub_postman($profile, $other); - $postman->announce($notice, $repeated_notice); - } - - // either made the announce or found nothing to repeat - return true; - } - - // That was it - $postman = new Activitypub_postman($profile, $other); - $postman->create_note($notice); - return true; - } - - /** - * Notify remote users when their notices get favourited. - * - * @param Profile $profile of local user doing the faving - * @param Notice $notice_liked Notice being favored - * @return bool return value - * @throws HTTP_Request2_Exception - * @throws InvalidUrlException - * @author Diogo Cordeiro - */ - public function onEndFavorNotice(Profile $profile, Notice $notice, $other) - { - $postman = new Activitypub_postman($profile, $other); - $postman->like($notice); - - return true; - } - - /** - * Notify remote users when their notices get deleted - * - * @param $user - * @param $notice - * @return bool hook flag - * @throws HTTP_Request2_Exception - * @throws InvalidUrlException - * @author Diogo Cordeiro - */ - public function onStartDeleteOwnNotice($profile, $notice, $other) - { - // Handle delete locally either because: - // 1. There's no undo-share logic yet - // 2. The deleting user has privileges to do so (locally) - if ($notice->isRepeat() || ($notice->getProfile()->getID() != $profile->getID())) { - return true; - } - - $postman = new Activitypub_postman($profile, $other); - $postman->delete_note($notice); - return true; - } -} diff --git a/plugins/ActivityPub/lib/activitypubqueuehandler.php b/plugins/ActivityPub/lib/activitypubqueuehandler.php deleted file mode 100644 index 1e6462238d..0000000000 --- a/plugins/ActivityPub/lib/activitypubqueuehandler.php +++ /dev/null @@ -1,320 +0,0 @@ -. - -/** - * ActivityPub queue handler for notice distribution - * - * @package GNUsocial - * - * @author Bruno Casteleiro - * @author Diogo Cordeiro - * @copyright 2019-2020 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -defined('GNUSOCIAL') || die(); - -/** - * @copyright 2019-2020 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class activitypubqueuehandler extends QueueHandler -{ - /** - * Getter of the queue transport name. - * - * @return string transport name - */ - public function transport(): string - { - return 'activitypub'; - } - - /** - * Notice distribution handler. - * - * @param Notice $notice notice to be distributed. - * - * @throws HTTP_Request2_Exception - * @throws InvalidUrlException - * @throws ServerException - * - * @return bool true on success, false otherwise - * - * @author Diogo Cordeiro - */ - public function handle($notice): bool - { - if (!($notice instanceof Notice)) { - common_log(LOG_ERR, 'Got a bogus notice, not distributing'); - return true; - } - - $profile = $notice->getProfile(); - - if (!$profile->isLocal()) { - return true; - } - - if ($notice->source == 'activity') { - common_log(LOG_ERR, "Ignoring distribution of notice:{$notice->id}: activity source"); - return true; - } - - $other = Activitypub_profile::from_profile_collection( - $notice->getAttentionProfiles() - ); - - try { - // Handling a Create? - if (ActivityUtils::compareVerbs($notice->verb, [ActivityVerb::POST, ActivityVerb::SHARE])) { - return $this->handle_create($profile, $notice, $other); - } - - // Handling a Like? - if (ActivityUtils::compareVerbs($notice->verb, [ActivityVerb::FAVORITE])) { - return $this->onEndFavorNotice($profile, $notice, $other); - } - - // Handling a Delete Note? - if (ActivityUtils::compareVerbs($notice->verb, [ActivityVerb::DELETE])) { - return $this->onStartDeleteOwnNotice($profile, $notice, $other); - } - } catch (Exception $e) { - // Postman handles issues with the failed queue - common_debug('ActivityPub Queue Handler:'.$e->getMessage()); - } - - return true; - } - - /** - * Handle notice creation and propagation - * - * @param mixed $profile - * @param mixed $notice - * @param mixed $other - */ - private function handle_create($profile, $notice, $other) - { - // Handling a reply? - if ($notice->reply_to) { - try { - $parent_notice = $notice->getParent(); - - try { - $other[] = Activitypub_profile::from_profile($parent_notice->getProfile()); - } catch (Exception $e) { - // Local user can be ignored - } - - foreach ($parent_notice->getAttentionProfiles() as $mention) { - try { - $other[] = Activitypub_profile::from_profile($mention); - } catch (Exception $e) { - // Local user can be ignored - } - } - } catch (NoParentNoticeException $e) { - // This is not a reply to something (has no parent) - } catch (NoResultException $e) { - // Parent author's profile not found! Complain louder? - common_log( - LOG_ERR, - "Parent notice's author not found: " . $e->getMessage() - ); - } - } - - // Handling an Announce? - if ($notice->isRepeat()) { - $repeated_notice = Notice::getKV('id', $notice->repeat_of); - if ($repeated_notice instanceof Notice) { - $other = array_merge( - $other, - Activitypub_profile::from_profile_collection( - $repeated_notice->getAttentionProfiles() - ) - ); - - try { - $other[] = Activitypub_profile::from_profile( - $repeated_notice->getProfile() - ); - } catch (Exception $e) { - // Local user can be ignored - } - - // That was it - $postman = new Activitypub_postman($profile, $other); - $postman->announce($notice, $repeated_notice); - } - - // either made the announce or found nothing to repeat - return true; - } - - // That was it - $postman = new Activitypub_postman($profile, $other); - $postman->create_note($notice); - return true; - } - - /** - * Notify remote users when their notices get favourited. - * - * @param Profile $profile of local user doing the faving - * @param Notice $notice Notice being favored - * @param mixed $other - * - * @throws HTTP_Request2_Exception - * @throws InvalidUrlException - * - * @return bool return value - * - * @author Diogo Cordeiro - */ - public function onEndFavorNotice(Profile $profile, Notice $notice, $other) - { - $notice_liked = $notice->getParent(); - if ($notice_liked->reply_to) { - try { - $parent_notice = $notice_liked->getParent(); - - try { - $other[] = Activitypub_profile::from_profile($parent_notice->getProfile()); - } catch (Exception $e) { - // Local user can be ignored - } - - $other = array_merge( - $other, - Activitypub_profile::from_profile_collection( - $parent_notice->getAttentionProfiles() - ) - ); - } catch (NoParentNoticeException $e) { - // This is not a reply to something (has no parent) - } catch (NoResultException $e) { - // Parent author's profile not found! Complain louder? - common_log(LOG_ERR, "Parent notice's author not found: " . $e->getMessage()); - } - } - - $postman = new Activitypub_postman($profile, $other); - $postman->like($notice); - - return true; - } - - /** - * Notify remote users when their notices get de-favourited. - * - * @param Profile $profile of local user doing the de-faving - * @param Notice $notice Notice being favored - * @param mixed $other - * - * @throws HTTP_Request2_Exception - * @throws InvalidUrlException - * - * @return bool return value - * - * @author Diogo Cordeiro - */ - public function onEndDisfavorNotice(Profile $profile, Notice $notice, $other) - { - if ($notice->reply_to) { - try { - $parent_notice = $notice->getParent(); - - try { - $other[] = Activitypub_profile::from_profile($parent_notice->getProfile()); - } catch (Exception $e) { - // Local user can be ignored - } - - $other = array_merge( - $other, - Activitypub_profile::from_profile_collection( - $parent_notice->getAttentionProfiles() - ) - ); - } catch (NoParentNoticeException $e) { - // This is not a reply to something (has no parent) - } catch (NoResultException $e) { - // Parent author's profile not found! Complain louder? - common_log(LOG_ERR, "Parent notice's author not found: " . $e->getMessage()); - } - } - - $postman = new Activitypub_postman($profile, $other); - $postman->undo_like($notice); - - return true; - } - - /** - * Notify remote users when their notices get deleted - * - * @param $user - * @param $notice - * @param mixed $profile - * @param mixed $other - * - * @throws HTTP_Request2_Exception - * @throws InvalidUrlException - * - * @return bool hook flag - * - * @author Diogo Cordeiro - */ - public function onStartDeleteOwnNotice($profile, $notice, $other) - { - // Handle delete locally either because: - // 1. There's no undo-share logic yet - // 2. The deleting user has privileges to do so (locally) - if ($notice->isRepeat() || ($notice->getProfile()->getID() != $profile->getID())) { - return true; - } - - if ($notice->reply_to) { - try { - $parent_notice = $notice->getParent(); - - try { - $other[] = Activitypub_profile::from_profile($parent_notice->getProfile()); - } catch (Exception $e) { - // Local user can be ignored - } - - $other = array_merge( - $other, - Activitypub_profile::from_profile_collection( - $parent_notice->getAttentionProfiles() - ) - ); - } catch (NoParentNoticeException $e) { - // This is not a reply to something (has no parent) - } catch (NoResultException $e) { - // Parent author's profile not found! Complain louder? - common_log(LOG_ERR, "Parent notice's author not found: " . $e->getMessage()); - } - } - - $postman = new Activitypub_postman($profile, $other); - $postman->delete_note($notice); - return true; - } -} diff --git a/plugins/ActivityPub/lib/discoveryhints.php b/plugins/ActivityPub/lib/discoveryhints.php deleted file mode 100644 index c5f3642d10..0000000000 --- a/plugins/ActivityPub/lib/discoveryhints.php +++ /dev/null @@ -1,175 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Evan Prodromou - * @author Brion Vibber - * @author James Walker - * @author Siebrand Mazeland - * @author Mikael Nordfeldth - * @author Diogo Cordeiro - * @copyright 2010-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * - * @see http://www.gnu.org/software/social/ - */ -defined('GNUSOCIAL') || die(); - -class discoveryhints -{ - /** - * Find discovery hints in XML XRD (Extensible Resource Descriptor) - */ - public static function fromXRD(XML_XRD $xrd) - { - $hints = []; - - if (Event::handle('StartDiscoveryHintsFromXRD', [$xrd, &$hints])) { - foreach ($xrd->links as $link) { - switch ($link->rel) { - case WebFingerResource_Profile::PROFILEPAGE: - $hints['profileurl'] = $link->href; - break; - case Discovery::UPDATESFROM: - if (empty($link->type) || $link->type == 'application/atom+xml') { - $hints['feedurl'] = $link->href; - } - break; - case Discovery::HCARD: - case Discovery::MF2_HCARD: - $hints['hcard'] = $link->href; - break; - default: - break; - } - } - Event::handle('EndDiscoveryHintsFromXRD', [$xrd, &$hints]); - } - - return $hints; - } - - public static function fromHcardUrl($url) - { - $client = new HTTPClient(); - $client->setHeader('Accept', 'text/html,application/xhtml+xml'); - try { - $response = $client->get($url); - - if (!$response->isOk()) { - return null; - } - } catch (HTTP_Request2_Exception $e) { - // Any HTTPClient error that might've been thrown - common_log(LOG_ERR, __METHOD__ . ':' . $e->getMessage()); - return null; - } - - return self::hcardHints( - $response->getBody(), - $response->getEffectiveUrl() - - ); - } - - /** - * ?????? - * - * @param mixed $body - * @param mixed $url - */ - public static function hcardHints($body, $url) - { - $hcard = self::_hcard($body, $url); - - if (empty($hcard)) { - return []; - } - - $hints = []; - - // XXX: don't copy stuff into an array and then copy it again - - if (array_key_exists('nickname', $hcard) && !empty($hcard['nickname'][0])) { - $hints['nickname'] = $hcard['nickname'][0]; - } - - if (array_key_exists('name', $hcard) && !empty($hcard['name'][0])) { - $hints['fullname'] = $hcard['name'][0]; - } - - if (array_key_exists('photo', $hcard) && count($hcard['photo'])) { - $hints['avatar'] = $hcard['photo'][0]; - } - - if (array_key_exists('note', $hcard) && !empty($hcard['note'][0])) { - $hints['bio'] = $hcard['note'][0]; - } - - if (array_key_exists('adr', $hcard) && !empty($hcard['adr'][0])) { - $hints['location'] = $hcard['adr'][0]['value']; - } - - if (array_key_exists('url', $hcard) && !empty($hcard['url'][0])) { - $hints['homepage'] = $hcard['url'][0]; - } - - return $hints; - } - - /** - * ????????? - * - * @param mixed $body - * @param mixed $url - */ - public static function _hcard($body, $url) - { - $mf2 = new Mf2\Parser($body, $url); - $mf2 = $mf2->parse(); - - if (empty($mf2['items'])) { - return null; - } - - $hcards = []; - - foreach ($mf2['items'] as $item) { - if (!in_array('h-card', $item['type'])) { - continue; - } - - // We found a match, return it immediately - if (isset($item['properties']['url']) && in_array($url, $item['properties']['url'])) { - return $item['properties']; - } - - // Let's keep all the hcards for later, to return one of them at least - $hcards[] = $item['properties']; - } - - // No match immediately for the url we expected, but there were h-cards found - if (count($hcards) > 0) { - return $hcards[0]; - } - - return null; - } -} diff --git a/plugins/ActivityPub/lib/explorer.php b/plugins/ActivityPub/lib/explorer.php deleted file mode 100644 index 8c0b941717..0000000000 --- a/plugins/ActivityPub/lib/explorer.php +++ /dev/null @@ -1,511 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * - * @see http://www.gnu.org/software/social/ - */ -defined('GNUSOCIAL') || die(); - -/** - * ActivityPub's own Explorer - * - * Allows to discovery new (or the same) Profiles (both local or remote) - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class Activitypub_explorer -{ - private $discovered_actor_profiles = []; - - /** - * Shortcut function to get a single profile from its URL. - * - * @param string $url - * @param bool $grab_online whether to try online grabbing, defaults to true - * - * @throws HTTP_Request2_Exception Network issues - * @throws NoProfileException This won't happen - * @throws Exception Invalid request - * @throws ServerException Error storing remote actor - * - * @return Profile - * - * @author Diogo Cordeiro - */ - public static function get_profile_from_url(string $url, bool $grab_online = true): Profile - { - $discovery = new self(); - // Get valid Actor object - $actor_profile = $discovery->lookup($url, $grab_online); - if (!empty($actor_profile)) { - return $actor_profile[0]; - } - throw new Exception('Invalid Actor.'); - } - - /** - * Get every profile from the given URL - * This function cleans the $this->discovered_actor_profiles array - * so that there is no erroneous data - * - * @param string $url User's url - * @param bool $grab_online whether to try online grabbing, defaults to true - * - * @throws HTTP_Request2_Exception - * @throws NoProfileException - * @throws Exception - * @throws ServerException - * - * @return array of Profile objects - * - * @author Diogo Cordeiro - */ - public function lookup(string $url, bool $grab_online = true) - { - if (in_array($url, ACTIVITYPUB_PUBLIC_TO)) { - return []; - } - - common_debug('ActivityPub Explorer: Started now looking for ' . $url); - $this->discovered_actor_profiles = []; - - return $this->_lookup($url, $grab_online); - } - - /** - * Get every profile from the given URL - * This is a recursive function that will accumulate the results on - * $discovered_actor_profiles array - * - * @param string $url User's url - * @param bool $grab_online whether to try online grabbing, defaults to true - * - * @throws HTTP_Request2_Exception - * @throws NoProfileException - * @throws ServerException - * @throws Exception - * - * @return array of Profile objects - * - * @author Diogo Cordeiro - */ - private function _lookup(string $url, bool $grab_online = true): array - { - $grab_local = $this->grab_local_user($url); - - // First check if we already have it locally and, if so, return it. - // If the local fetch fails and remote grab is required: store locally and return. - if (!$grab_local && (!$grab_online || !$this->grab_remote_user($url))) { - throw new Exception('User not found.'); - } - - return $this->discovered_actor_profiles; - } - - /** - * Get a local user profile from its URL and joins it on - * $this->discovered_actor_profiles - * - * @param string $uri Actor's uri - * @param bool $online - * - * @throws NoProfileException - * @throws Exception - * - * @return bool success state - * - * @author Diogo Cordeiro - */ - private function grab_local_user(string $uri, bool $online = false): bool - { - if ($online) { - common_debug('ActivityPub Explorer: Searching locally for ' . $uri . ' with online resources.'); - $all_ids = LRDDPlugin::grab_profile_aliases($uri); - } else { - common_debug('ActivityPub Explorer: Searching locally for ' . $uri . ' offline.'); - $all_ids = [$uri]; - } - - if (is_null($all_ids)) { - common_debug('AcvitityPub Explorer: Unable to find a local profile for ' . $uri); - return false; - } - - foreach ($all_ids as $alias) { - // Try standard ActivityPub route - // Is this a known filthy little mudblood? - $aprofile = self::get_aprofile_by_url($alias); - if ($aprofile instanceof Activitypub_profile) { - common_debug('ActivityPub Explorer: Found a local Aprofile for ' . $alias); - - // double check to confirm this alias as a legitimate one - if ($online) { - common_debug('ActivityPub Explorer: Double-checking ' . $alias . ' to confirm it as a legitimate alias'); - - $disco = new Discovery(); - $xrd = $disco->lookup($aprofile->getUri()); - $doublecheck_aliases = array_merge([$xrd->subject], $xrd->aliases); - - if (in_array($uri, $doublecheck_aliases)) { - // the original URI is present, we're sure now! - // update aprofile's URI and proceed - common_debug('ActivityPub Explorer: ' . $alias . ' is a legitimate alias'); - $aprofile->updateUri($uri); - } else { - common_debug('ActivityPub Explorer: ' . $alias . ' is not an alias we can trust'); - continue; - } - } - - // Assert: This AProfile has a Profile, no try catch. - $profile = $aprofile->local_profile(); - // We found something! - $this->discovered_actor_profiles[] = $profile; - return true; - } else { - common_debug('ActivityPub Explorer: Unable to find a local Aprofile for ' . $alias . ' - looking for a Profile instead.'); - // Well, maybe it is a pure blood? - // Iff, we are in the same instance: - $ACTIVITYPUB_BASE_ACTOR_URI = common_local_url('userbyid', ['id' => null], null, null, false, true); // @FIXME: Could this be too hardcoded? - $ACTIVITYPUB_BASE_ACTOR_URI_length = strlen($ACTIVITYPUB_BASE_ACTOR_URI); - if (substr($alias, 0, $ACTIVITYPUB_BASE_ACTOR_URI_length) === $ACTIVITYPUB_BASE_ACTOR_URI) { - try { - $profile = Profile::getByID((int) substr($alias, $ACTIVITYPUB_BASE_ACTOR_URI_length)); - common_debug('ActivityPub Explorer: Found a Profile for ' . $alias); - // We found something! - $this->discovered_actor_profiles[] = $profile; - return true; - } catch (Exception $e) { - // Let the exception go on its merry way. - common_debug('ActivityPub Explorer: Unable to find a Profile for ' . $alias); - } - } - } - } - - // If offline grabbing failed, attempt again with online resources - if (!$online) { - common_debug('ActivityPub Explorer: Will try everything again with online resources against: ' . $uri); - return $this->grab_local_user($uri, true); - } - - return false; - } - - /** - * Get a remote user(s) profile(s) from its URL and joins it on - * $this->discovered_actor_profiles - * - * @param string $url User's url - * - * @throws HTTP_Request2_Exception - * @throws NoProfileException - * @throws ServerException - * @throws Exception - * - * @return bool success state - * - * @author Diogo Cordeiro - */ - private function grab_remote_user(string $url): bool - { - common_debug('ActivityPub Explorer: Trying to grab a remote actor for ' . $url); - $client = new HTTPClient(); - $response = $client->get($url, ACTIVITYPUB_HTTP_CLIENT_HEADERS); - $res = json_decode($response->getBody(), true); - if ($response->getStatus() == 410) { // If it was deleted - return true; // Nothing to add. - } elseif (!$response->isOk()) { // If it is unavailable - return false; // Try to add at another time. - } - if (is_null($res)) { - common_debug('ActivityPub Explorer: Invalid JSON returned from given Actor URL: ' . $response->getBody()); - return true; // Nothing to add. - } - - if (isset($res['type']) && $res['type'] === 'OrderedCollection' && isset($res['first'])) { // It's a potential collection of actors!!! - common_debug('ActivityPub Explorer: Found a collection of actors for ' . $url); - $this->travel_collection($res['first']); - return true; - } elseif (self::validate_remote_response($res)) { - common_debug('ActivityPub Explorer: Found a valid remote actor for ' . $url); - $this->discovered_actor_profiles[] = $this->store_profile($res); - return true; - } else { - common_debug('ActivityPub Explorer: Invalid potential remote actor while grabbing remotely: ' . $url . '. He returned the following: ' . json_encode($res, JSON_UNESCAPED_SLASHES)); - return false; - } - - return false; - } - - /** - * Save remote user profile in local instance - * - * @param array $res remote response - * - * @throws NoProfileException - * @throws ServerException - * @throws Exception - * - * @return Profile remote Profile object - * - * @author Diogo Cordeiro - */ - private function store_profile(array $res): Profile - { - // ActivityPub Profile - $aprofile = new Activitypub_profile; - $aprofile->uri = $res['id']; - $aprofile->nickname = $res['preferredUsername']; - $aprofile->fullname = $res['name'] ?? null; - $aprofile->bio = isset($res['summary']) ? substr(strip_tags($res['summary']), 0, 1000) : null; - $aprofile->inboxuri = $res['inbox']; - $aprofile->sharedInboxuri = $res['endpoints']['sharedInbox'] ?? $res['inbox']; - $aprofile->profileurl = $res['url'] ?? $aprofile->uri; - - $aprofile->do_insert(); - $profile = $aprofile->local_profile(); - - // Public Key - $apRSA = new Activitypub_rsa(); - $apRSA->profile_id = $profile->getID(); - $apRSA->public_key = $res['publicKey']['publicKeyPem']; - $apRSA->store_keys(); - - // Avatar - if (isset($res['icon']['url'])) { - try { - $this->update_avatar($profile, $res['icon']['url']); - } catch (Exception $e) { - // Let the exception go, it isn't a serious issue - common_debug('ActivityPub Explorer: An error ocurred while grabbing remote avatar: ' . $e->getMessage()); - } - } - - return $profile; - } - - /** - * Validates a remote response in order to determine whether this - * response is a valid profile or not - * - * @param array $res remote response - * - * @return bool success state - * - * @author Diogo Cordeiro - */ - public static function validate_remote_response(array $res): bool - { - if (!isset($res['id'], $res['preferredUsername'], $res['inbox'], $res['publicKey']['publicKeyPem'])) { - return false; - } - - return true; - } - - /** - * Get a ActivityPub Profile from it's uri - * Unfortunately GNU social cache is not truly reliable when handling - * potential ActivityPub remote profiles, as so it is important to use - * this hacky workaround (at least for now) - * - * @param string $v URL - * - * @return Activitypub_profile|bool false if fails | Aprofile object if successful - * - * @author Diogo Cordeiro - */ - public static function get_aprofile_by_url(string $v) - { - $i = Managed_DataObject::getcached('Activitypub_profile', 'uri', $v); - if (empty($i)) { // false = cache miss - $i = new Activitypub_profile; - $result = $i->get('uri', $v); - if ($result) { - // Hit! - $i->encache(); - } else { - return false; - } - } - return $i; - } - - /** - * Given a valid actor profile url returns its inboxes - * - * @param string $url of Actor profile - * - * @throws HTTP_Request2_Exception - * @throws Exception If an irregular error happens (status code, body format or GONE) - * - * @return array|bool false if fails to validate the answer | array with inbox and shared inbox if successful - * - * @author Diogo Cordeiro - */ - public static function get_actor_inboxes_uri(string $url) - { - $client = new HTTPClient(); - $response = $client->get($url, ACTIVITYPUB_HTTP_CLIENT_HEADERS); - if ($response->getStatus() == 410) { // If it was deleted - throw new Exception('This actor is GONE.'); - } elseif (!$response->isOk()) { // If it is unavailable - throw new Exception('Non Ok Status Code for given Actor URL.'); - } - $res = json_decode($response->getBody(), true); - if (is_null($res)) { // If it is in an unexpected format - common_debug('ActivityPub Explorer: Invalid JSON returned from given Actor URL: ' . $response->getBody()); - throw new Exception('Given Actor URL didn\'t return a valid JSON.'); - } - if (self::validate_remote_response($res)) { - return [ - 'inbox' => $res['inbox'], - 'sharedInbox' => isset($res['endpoints']['sharedInbox']) ? $res['endpoints']['sharedInbox'] : $res['inbox'], - ]; - } - - return false; - } - - /** - * Download and update given avatar image - * TODO: Avoid updating an avatar if its URL didn't change. (this is something OStatus already does) - * TODO: Should be in AProfile instead? - * - * @param Profile $profile - * @param string $url - * - * @throws Exception in various failure cases - * - * @return Avatar The Avatar we have on disk. (seldom used) - * - * @author Diogo Cordeiro - */ - public static function update_avatar(Profile $profile, string $url): Avatar - { - common_debug('ActivityPub Explorer: Started grabbing remote avatar from: ' . $url); - // ImageFile throws exception if something goes wrong, which we'll let go on its merry way - $imagefile = ImageFile::fromURL($url); - - $id = $profile->getID(); - - $type = $imagefile->preferredType(); - $filename = Avatar::filename( - $id, - image_type_to_extension($type), - null, - 'tmp' . common_timestamp() - ); - - $filepath = Avatar::path($filename); - /*$imagefile = */$imagefile->copyTo($filepath); - - common_debug('ActivityPub Explorer: Stored avatar in: ' . $filepath); - - // XXX: Do we need this? - chmod($filepath, 0644); - - $profile->setOriginal($filename); - - common_debug('ActivityPub Explorer: Seted Avatar from: ' . $url . ' to profile.'); - return Avatar::getUploaded($profile); - } - - /** - * Allows the Explorer to transverse a collection of persons. - * - * @param string $url - * - * @throws HTTP_Request2_Exception - * @throws NoProfileException - * @throws ServerException - * - * @return bool - * - * @author Diogo Cordeiro - */ - private function travel_collection(string $url): bool - { - $client = new HTTPClient(); - $response = $client->get($url, ACTIVITYPUB_HTTP_CLIENT_HEADERS); - $res = json_decode($response->getBody(), true); - - if (!isset($res['orderedItems'])) { - return false; - } - - foreach ($res['orderedItems'] as $profile) { - if ($this->_lookup($profile) == false) { - common_debug('ActivityPub Explorer: Found an invalid actor for ' . $profile); - // TODO: Invalid actor found, fallback to OStatus - } - } - // Go through entire collection - if (!is_null($res['next'])) { - $this->travel_collection($res['next']); - } - - return true; - } - - /** - * Get a remote user array from its URL (this function is only used for - * profile updating and shall not be used for anything else) - * - * @param string $url User's url - * - * @throws Exception Either network issues or unsupported Activity format - * - * @return array|false If it is able to fetch, false if it's gone - * - * @author Diogo Cordeiro - */ - public static function get_remote_user_activity(string $url) - { - $client = new HTTPClient(); - $response = $client->get($url, ACTIVITYPUB_HTTP_CLIENT_HEADERS); - // If it was deleted - if ($response->getStatus() == 410) { - return false; - } elseif (!$response->isOk()) { // If it is unavailable - throw new Exception('Non Ok Status Code for given Actor URL.'); - } - $res = json_decode($response->getBody(), true); - if (is_null($res)) { - common_debug('ActivityPub Explorer: Invalid JSON returned from given Actor URL: ' . $response->getBody()); - throw new Exception('Given Actor URL didn\'t return a valid JSON.'); - } - if (self::validate_remote_response($res)) { - common_debug('ActivityPub Explorer: Found a valid remote actor for ' . $url); - return $res; - } - throw new Exception('ActivityPub Explorer: Failed to get activity.'); - } -} diff --git a/plugins/ActivityPub/lib/httpsignature.php b/plugins/ActivityPub/lib/httpsignature.php deleted file mode 100644 index 03a8545cd9..0000000000 --- a/plugins/ActivityPub/lib/httpsignature.php +++ /dev/null @@ -1,192 +0,0 @@ - - * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 - * - * @see https://github.com/aaronpk/Nautilus/blob/master/app/ActivityPub/HTTPSignature.php - */ -class httpsignature -{ - /** - * Sign a message with an Actor - * - * @param Profile $user Actor signing - * @param string $url Inbox url - * @param bool|string $body Data to sign (optional) - * @param array $addlHeaders Additional headers (optional) - * - * @throws Exception Attempted to sign something that belongs to an Actor we don't own - * - * @return array Headers to be used in curl - */ - public static function sign(Profile $user, string $url, $body = false, array $addlHeaders = []): array - { - $digest = false; - if ($body) { - $digest = self::_digest($body); - } - $headers = self::_headersToSign($url, $digest); - $headers = array_merge($headers, $addlHeaders); - $stringToSign = self::_headersToSigningString($headers); - $signedHeaders = implode(' ', array_map('strtolower', array_keys($headers))); - $actor_private_key = new Activitypub_rsa(); - // Intentionally unhandled exception, we want this to explode if that happens as it would be a bug - $actor_private_key = $actor_private_key->get_private_key($user); - $key = openssl_pkey_get_private($actor_private_key); - openssl_sign($stringToSign, $signature, $key, OPENSSL_ALGO_SHA256); - $signature = base64_encode($signature); - $signatureHeader = 'keyId="' . $user->getUri() . '#public-key' . '",headers="' . $signedHeaders . '",algorithm="rsa-sha256",signature="' . $signature . '"'; - unset($headers['(request-target)']); - $headers['Signature'] = $signatureHeader; - - return self::_headersToCurlArray($headers); - } - - /** - * @param mixed $body - * - * @return string - */ - private static function _digest($body): string - { - if (is_array($body)) { - $body = json_encode($body); - } - return base64_encode(hash('sha256', $body, true)); - } - - /** - * @param string $url - * @param mixed $digest - * - * @throws Exception - * - * @return array - */ - protected static function _headersToSign(string $url, $digest = false): array - { - $date = new DateTime('UTC'); - - $headers = [ - '(request-target)' => 'post ' . parse_url($url, PHP_URL_PATH), - 'Date' => $date->format('D, d M Y H:i:s \G\M\T'), - 'Host' => parse_url($url, PHP_URL_HOST), - 'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams", application/activity+json, application/json', - 'User-Agent' => 'GNU social ActivityPub Plugin - ' . GNUSOCIAL_ENGINE_URL, - 'Content-Type' => 'application/activity+json', - ]; - - if ($digest) { - $headers['Digest'] = 'SHA-256=' . $digest; - } - - return $headers; - } - - /** - * @param array $headers - * - * @return string - */ - private static function _headersToSigningString(array $headers): string - { - return implode("\n", array_map(function ($k, $v) { - return strtolower($k) . ': ' . $v; - }, array_keys($headers), $headers)); - } - - /** - * @param array $headers - * - * @return array - */ - private static function _headersToCurlArray(array $headers): array - { - return array_map(function ($k, $v) { - return "{$k}: {$v}"; - }, array_keys($headers), $headers); - } - - /** - * @param string $signature - * - * @return array - */ - public static function parseSignatureHeader(string $signature): array - { - $parts = explode(',', $signature); - $signatureData = []; - - foreach ($parts as $part) { - if (preg_match('/(.+)="(.+)"/', $part, $match)) { - $signatureData[$match[1]] = $match[2]; - } - } - - if (!isset($signatureData['keyId'])) { - return [ - 'error' => 'No keyId was found in the signature header. Found: ' . implode(', ', array_keys($signatureData)), - ]; - } - - if (!filter_var($signatureData['keyId'], FILTER_VALIDATE_URL)) { - return [ - 'error' => 'keyId is not a URL: ' . $signatureData['keyId'], - ]; - } - - if (!isset($signatureData['headers']) || !isset($signatureData['signature'])) { - return [ - 'error' => 'Signature is missing headers or signature parts', - ]; - } - - return $signatureData; - } - - /** - * @param $publicKey - * @param $signatureData - * @param $inputHeaders - * @param $path - * @param $body - * - * @return array - */ - public static function verify($publicKey, $signatureData, $inputHeaders, $path, $body): array - { - // We need this because the used Request headers fields specified by Signature are in lower case. - $headersContent = array_change_key_case($inputHeaders, CASE_LOWER); - $digest = 'SHA-256=' . base64_encode(hash('sha256', $body, true)); - $headersToSign = []; - foreach (explode(' ', $signatureData['headers']) as $h) { - if ($h == '(request-target)') { - $headersToSign[$h] = 'post ' . $path; - } elseif ($h == 'digest') { - $headersToSign[$h] = $digest; - } elseif (isset($headersContent[$h][0])) { - $headersToSign[$h] = $headersContent[$h]; - } - } - $signingString = self::_headersToSigningString($headersToSign); - - $verified = openssl_verify($signingString, base64_decode($signatureData['signature']), $publicKey, OPENSSL_ALGO_SHA256); - - return [$verified, $signingString]; - } -} diff --git a/plugins/ActivityPub/lib/inbox_handler.php b/plugins/ActivityPub/lib/inbox_handler.php deleted file mode 100644 index 490da16d0e..0000000000 --- a/plugins/ActivityPub/lib/inbox_handler.php +++ /dev/null @@ -1,450 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * - * @see http://www.gnu.org/software/social/ - */ -defined('GNUSOCIAL') || die(); - -/** - * ActivityPub Inbox Handler - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @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 - */ - 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 - */ - 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 - */ - 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 - */ - 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 - */ - 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 - */ - 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 - */ - 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. - * - * @throws NoProfileException - * @throws Exception - * - * @author Bruno Casteleiro - * @author Diogo Cordeiro - */ - private function handle_delete() - { - $object = $this->object; - 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 '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']}."); - } - } - } else { - // We don't know the type of the deleted object :( - // Nor if it's gone or not. - try { - if (is_array($object)) { - $object = $object['id']; - } - $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 - } - - 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 - } - - // 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 - */ - 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 - */ - 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 - */ - 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 - */ - 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 - */ - 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 - */ - private function handle_announce() - { - $notice = ActivityPubPlugin::grab_notice_from_url($this->object); - Activitypub_announce::repeat($this->activity['id'], $this->actor, $notice); - } -} diff --git a/plugins/ActivityPub/lib/models/Activitypub_accept.php b/plugins/ActivityPub/lib/models/Activitypub_accept.php deleted file mode 100644 index 3cdf3c98a9..0000000000 --- a/plugins/ActivityPub/lib/models/Activitypub_accept.php +++ /dev/null @@ -1,93 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * - * @see http://www.gnu.org/software/social/ - */ -defined('GNUSOCIAL') || die(); - -/** - * ActivityPub error representation - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class Activitypub_accept -{ - /** - * Generates an ActivityPub representation of a Accept - * - * @author Diogo Cordeiro - * - * @param array $object - * - * @return array pretty array to be used in a response - */ - public static function accept_to_array($object) - { - $res = [ - '@context' => 'https://www.w3.org/ns/activitystreams', - 'id' => common_root_url() . 'accept_follow_from_' . urlencode($object['actor']) . '_to_' . urlencode($object['object']), - 'type' => 'Accept', - 'actor' => $object['object'], - 'object' => $object, - ]; - return $res; - } - - /** - * Verifies if a given object is acceptable for an Accept Activity. - * - * @param array $object - * - * @throws Exception - * - * @return bool - * - * @author Diogo Cordeiro - */ - public static function validate_object($object) - { - if (!is_array($object)) { - throw new Exception('Invalid Object Format for Accept Activity.'); - } - if (!isset($object['type'])) { - throw new Exception('Object type was not specified for Accept Activity.'); - } - switch ($object['type']) { - case 'Follow': - // Validate data - if (!filter_var($object['object'], FILTER_VALIDATE_URL)) { - throw new Exception('Object is not a valid Object URI for Activity.'); - } - break; - default: - throw new Exception('This is not a supported Object Type for Accept Activity.'); - } - return true; - } -} diff --git a/plugins/ActivityPub/lib/models/Activitypub_announce.php b/plugins/ActivityPub/lib/models/Activitypub_announce.php deleted file mode 100644 index 1fc0c196fc..0000000000 --- a/plugins/ActivityPub/lib/models/Activitypub_announce.php +++ /dev/null @@ -1,109 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * - * @see http://www.gnu.org/software/social/ - */ -defined('GNUSOCIAL') || die(); - -/** - * ActivityPub error representation - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class Activitypub_announce -{ - /** - * Generates an ActivityPub representation of a Announce - * - * @param Profile $actor - * @param Notice $notice - * - * @return array pretty array to be used in a response - * - * @author Diogo Cordeiro - */ - public static function announce_to_array(Profile $actor, Notice $notice): array - { - $actor_uri = $actor->getUri(); - $notice_url = Activitypub_notice::getUrl($notice); - - $to = [common_local_url('apActorFollowers', ['id' => $actor->getID()])]; - foreach ($notice->getAttentionProfiles() as $to_profile) { - $to[] = $to_profile->getUri(); - } - - $cc[] = 'https://www.w3.org/ns/activitystreams#Public'; - - $res = [ - '@context' => 'https://www.w3.org/ns/activitystreams', - 'id' => common_root_url() . 'share_from_' . urlencode($actor_uri) . '_to_' . urlencode($notice_url), - 'type' => 'Announce', - 'actor' => $actor_uri, - 'object' => $notice_url, - 'to' => $to, - 'cc' => $cc, - ]; - return $res; - } - - /** - * Convenience function for posting a repeat of an existing message. - * - * @param string $uri - * @param Profile $actor Profile which is doing the repeat - * @param Notice $target - * @return Notice - */ - public static function repeat(string $uri, Profile $actor, Notice $target): Notice - { - // TRANS: Message used to repeat a notice. RT is the abbreviation of 'retweet'. - // TRANS: %1$s is the repeated user's name, %2$s is the repeated notice. - $content = sprintf( - _('RT @%1$s %2$s'), - $actor->getNickname(), - $target->getContent() - ); - - $options = [ - 'source' => 'ActivityPub', - 'uri' => $uri, - 'is_local' => ($actor->isLocal() ? Notice::LOCAL_PUBLIC : Notice::REMOTE), - 'repeat_of' => $target->getParent()->getID(), - 'scope' => $target->getScope(), - ]; - - // Scope is same as this one's - return Notice::saveNew( - $actor->getID(), - $content, - 'ActivityPub', - $options - ); - } -} diff --git a/plugins/ActivityPub/lib/models/Activitypub_attachment.php b/plugins/ActivityPub/lib/models/Activitypub_attachment.php deleted file mode 100644 index 5e0737a0f5..0000000000 --- a/plugins/ActivityPub/lib/models/Activitypub_attachment.php +++ /dev/null @@ -1,71 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * - * @see http://www.gnu.org/software/social/ - */ -defined('GNUSOCIAL') || die(); - -/** - * ActivityPub Attachment representation - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class Activitypub_attachment -{ - /** - * Generates a pretty array from an Attachment object - * - * @author Diogo Cordeiro - * - * @param Attachment $attachment - * - * @return array pretty array to be used in a response - */ - public static function attachment_to_array($attachment) - { - $res = [ - '@context' => 'https://www.w3.org/ns/activitystreams', - 'type' => 'Document', - 'mediaType' => $attachment->mimetype, - 'url' => $attachment->getUrl(), - 'size' => $attachment->getSize(), - 'name' => $attachment->getTitle(), - ]; - - // Image - if (substr($res['mediaType'], 0, 5) == 'image') { - $res['meta'] = [ - 'width' => $attachment->width, - 'height' => $attachment->height, - ]; - } - - return $res; - } -} diff --git a/plugins/ActivityPub/lib/models/Activitypub_create.php b/plugins/ActivityPub/lib/models/Activitypub_create.php deleted file mode 100644 index a5c6993841..0000000000 --- a/plugins/ActivityPub/lib/models/Activitypub_create.php +++ /dev/null @@ -1,122 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * - * @see http://www.gnu.org/software/social/ - */ -defined('GNUSOCIAL') || die(); - -/** - * ActivityPub error representation - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class Activitypub_create -{ - /** - * Generates an ActivityPub representation of a Create - * - * @param string $actor - * @param array $object - * @param bool $directMessage whether it is a private Create activity or not - * - * @return array pretty array to be used in a response - * - * @author Diogo Cordeiro - */ - public static function create_to_array(string $actor, string $uri, $object, bool $directMessage = false): array - { - $res = [ - '@context' => 'https://www.w3.org/ns/activitystreams', - 'id' => $object['id'] . '/create', - 'type' => 'Create', - 'directMessage' => $directMessage, - 'to' => $object['to'], - 'cc' => $object['cc'], - 'actor' => $actor, - 'object' => $object, - ]; - return $res; - } - - /** - * Verifies if a given object is acceptable for a Create Activity. - * - * @param array $object - * - * @throws Exception if invalid - * - * @return bool True if acceptable, false if valid but unsupported - * - * @author Diogo Cordeiro - */ - public static function validate_object($object): bool - { - if (!is_array($object)) { - common_debug('ActivityPub Create Validator: Rejected because of invalid Object format.'); - throw new Exception('Invalid Object Format for Create Activity.'); - } - if (!isset($object['type'])) { - common_debug('ActivityPub Create Validator: Rejected because of Type.'); - throw new Exception('Object type was not specified for Create Activity.'); - } - if (isset($object['directMessage']) && !is_bool($object['directMessage'])) { - common_debug('ActivityPub Create Validator: Rejected because Object directMessage is invalid.'); - throw new Exception('Invalid Object directMessage.'); - } - switch ($object['type']) { - case 'Note': - // Validate data - return Activitypub_notice::validate_note($object); - break; - default: - throw new Exception('This is not a supported Object Type for Create Activity.'); - } - return true; - } - - /** - * Verify if received note is private (direct). - * Note that we're conformant with the (yet) non-standard directMessage attribute: - * https://github.com/w3c/activitypub/issues/196#issuecomment-304958984 - * - * @param array $activity received Create-Note activity - * - * @return bool true if note is private, false otherwise - * - * @author Bruno casteleiro - */ - public static function isPrivateNote(array $activity): bool - { - if (isset($activity['directMessage'])) { - return $activity['directMessage']; - } - - return empty($activity['cc']) && !in_array('https://www.w3.org/ns/activitystreams#Public', $activity['to']); - } -} diff --git a/plugins/ActivityPub/lib/models/Activitypub_delete.php b/plugins/ActivityPub/lib/models/Activitypub_delete.php deleted file mode 100644 index 315b547648..0000000000 --- a/plugins/ActivityPub/lib/models/Activitypub_delete.php +++ /dev/null @@ -1,95 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * - * @see http://www.gnu.org/software/social/ - */ -defined('GNUSOCIAL') || die(); - -/** - * ActivityPub delete representation - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class Activitypub_delete -{ - /** - * Generates an ActivityStreams 2.0 representation of a Delete - * - * @param string $actor actor URI - * @param string $object object URI - * - * @return array pretty array to be used in a response - * - * @author Diogo Cordeiro - */ - public static function delete_to_array($object): array - { - $res = [ - '@context' => 'https://www.w3.org/ns/activitystreams', - 'id' => $object . '/delete', - 'type' => 'Delete', - 'to' => ['https://www.w3.org/ns/activitystreams#Public'], - 'actor' => $actor, - 'object' => $object, - ]; - return $res; - } - - /** - * Verifies if a given object is acceptable for a Delete Activity. - * - * @param array|string $object - * - * @throws Exception - * - * @return bool - * - * @author Bruno Casteleiro - */ - public static function validate_object($object): bool - { - if (!is_array($object)) { - if (!filter_var($object, FILTER_VALIDATE_URL)) { - throw new Exception('Object is not a valid Object URI for Activity.'); - } - } else { - if (!isset($object['type'])) { - throw new Exception('Object type was not specified for Delete Activity.'); - } - if ($object['type'] !== 'Tombstone' && $object['type'] !== 'Person') { - throw new Exception('Invalid Object type for Delete Activity.'); - } - if (!isset($object['id'])) { - throw new Exception('Object id was not specified for Delete Activity.'); - } - } - - return true; - } -} diff --git a/plugins/ActivityPub/lib/models/Activitypub_error.php b/plugins/ActivityPub/lib/models/Activitypub_error.php deleted file mode 100644 index 15540ad643..0000000000 --- a/plugins/ActivityPub/lib/models/Activitypub_error.php +++ /dev/null @@ -1,56 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * - * @see http://www.gnu.org/software/social/ - */ -defined('GNUSOCIAL') || die(); - -/** - * ActivityPub error representation - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class Activitypub_error -{ - /** - * Generates a pretty error from a string - * - * @author Diogo Cordeiro - * - * @param string $m - * - * @return array pretty array to be used in a response - */ - public static function error_message_to_array(string $m): array - { - $res = [ - 'error' => $m, - ]; - } -} diff --git a/plugins/ActivityPub/lib/models/Activitypub_follow.php b/plugins/ActivityPub/lib/models/Activitypub_follow.php deleted file mode 100644 index 082503e882..0000000000 --- a/plugins/ActivityPub/lib/models/Activitypub_follow.php +++ /dev/null @@ -1,104 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * - * @see http://www.gnu.org/software/social/ - */ -defined('GNUSOCIAL') || die(); - -/** - * ActivityPub error representation - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class Activitypub_follow -{ - /** - * Generates an ActivityPub representation of a subscription - * - * @author Diogo Cordeiro - * - * @param string $actor - * @param string $object - * @param null|string $id Activity id, to be used when generating for an Accept Activity - * - * @return array pretty array to be used in a response - */ - public static function follow_to_array(string $actor, string $object, ?string $id = null): array - { - if ($id === null) { - $id = common_root_url() . 'follow_from_' . urlencode($actor) . '_to_' . urlencode($object); - } - - $res = [ - '@context' => 'https://www.w3.org/ns/activitystreams', - 'id' => $id, - 'type' => 'Follow', - 'actor' => $actor, - 'object' => $object, - ]; - return $res; - } - - /** - * Handles a Follow Activity received by our inbox. - * - * @param Profile $actor_profile Remote Actor - * @param string $object Local Actor - * @param string $id Activity id - * - * @throws AlreadyFulfilledException - * @throws HTTP_Request2_Exception - * @throws NoProfileException - * @throws ServerException - * - * @author Diogo Cordeiro - */ - public static function follow(Profile $actor_profile, string $object, string $id) - { - // Get Actor's Aprofile - $actor_aprofile = Activitypub_profile::from_profile($actor_profile); - - // Get Object profile - $object_profile = new Activitypub_explorer; - $object_profile = $object_profile->lookup($object)[0]; - - if (!Subscription::exists($actor_profile, $object_profile)) { - Subscription::start($actor_profile, $object_profile); - Activitypub_profile::subscribeCacheUpdate($actor_profile, $object_profile); - common_debug('ActivityPubPlugin: Accepted Follow request from ' . $actor_profile->getUri() . ' to ' . $object); - } else { - common_debug('ActivityPubPlugin: Received a repeated Follow request from ' . $actor_profile->getUri() . ' to ' . $object); - } - - // Notify remote instance that we have accepted their request - common_debug('ActivityPubPlugin: Notifying remote instance that we have accepted their Follow request request from ' . $actor_profile->getUri() . ' to ' . $object); - $postman = new Activitypub_postman($object_profile, [$actor_aprofile]); - $postman->accept_follow($id); - } -} diff --git a/plugins/ActivityPub/lib/models/Activitypub_like.php b/plugins/ActivityPub/lib/models/Activitypub_like.php deleted file mode 100644 index 0080c564d3..0000000000 --- a/plugins/ActivityPub/lib/models/Activitypub_like.php +++ /dev/null @@ -1,116 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * - * @see http://www.gnu.org/software/social/ - */ -defined('GNUSOCIAL') || die(); - -/** - * ActivityPub Like representation - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class Activitypub_like -{ - /** - * Generates an ActivityPub representation of a Like - * - * @author Diogo Cordeiro - * - * @param string $actor Actor URI - * @param string $object Notice URI - * - * @return array pretty array to be used in a response - * @author Diogo Cordeiro - */ - public static function like_to_array(string $actor, Notice $notice): array - { - $res = [ - '@context' => 'https://www.w3.org/ns/activitystreams', - 'id' => common_root_url() . 'like_from_' . urlencode($actor) . '_to_' . urlencode($object), - 'type' => 'Like', - 'actor' => $actor, - 'object' => $object, - ]; - return $res; - } - - /** - * Save a favorite record. - * - * @param string $uri - * @param Profile $actor the local or remote Profile who favorites - * @param Notice $target the notice that is favorited - * @return Notice record on success - * @throws AlreadyFulfilledException - * @throws ClientException - * @throws NoticeSaveException - * @throws ServerException - */ - public static function addNew(string $uri, Profile $actor, Notice $target): Notice - { - if (Fave::existsForProfile($target, $actor)) { - // TRANS: Client error displayed when trying to mark a notice as favorite that already is a favorite. - throw new AlreadyFulfilledException(_m('You have already favorited this!')); - } - - $act = new Activity(); - $act->type = ActivityObject::ACTIVITY; - $act->verb = ActivityVerb::FAVORITE; - $act->time = time(); - $act->id = $uri; - $act->title = _m('Favor'); - // TRANS: Message that is the "content" of a favorite (%1$s is the actor's nickname, %2$ is the favorited - // notice's nickname and %3$s is the content of the favorited notice.) - $act->content = sprintf( - _m('%1$s favorited something by %2$s: %3$s'), - $actor->getNickname(), - $target->getProfile()->getNickname(), - $target->getRendered() - ); - $act->actor = $actor->asActivityObject(); - $act->target = $target->asActivityObject(); - $act->objects = [clone($act->target)]; - - $url = common_local_url('AtomPubShowFavorite', ['profile'=>$actor->id, 'notice'=>$target->id]); - $act->selfLink = $url; - $act->editLink = $url; - - $options = [ - 'source' => 'ActivityPub', - 'uri' => $act->id, - 'url' => $url, - 'is_local' => ($actor->isLocal() ? Notice::LOCAL_PUBLIC : Notice::REMOTE), - 'scope' => $target->getScope(), - ]; - - // saveActivity will in turn also call Fave::saveActivityObject - return Notice::saveActivity($act, $actor, $options); - } -} diff --git a/plugins/ActivityPub/lib/models/Activitypub_mention_tag.php b/plugins/ActivityPub/lib/models/Activitypub_mention_tag.php deleted file mode 100644 index 8e16f9626a..0000000000 --- a/plugins/ActivityPub/lib/models/Activitypub_mention_tag.php +++ /dev/null @@ -1,61 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * - * @see http://www.gnu.org/software/social/ - */ -defined('GNUSOCIAL') || die(); - -/** - * ActivityPub Mention Tag representation - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class Activitypub_mention_tag -{ - /** - * Generates an ActivityPub representation of a Mention Tag - * - * @author Diogo Cordeiro - * - * @param string $href Actor Uri - * @param string $name Mention name - * - * @return array pretty array to be used in a response - */ - public static function mention_tag_to_array_from_values(string $href, string $name): array - { - $res = [ - '@context' => 'https://www.w3.org/ns/activitystreams', - 'type' => 'Mention', - 'href' => $href, - 'name' => $name, - ]; - return $res; - } -} diff --git a/plugins/ActivityPub/lib/models/Activitypub_message.php b/plugins/ActivityPub/lib/models/Activitypub_message.php deleted file mode 100644 index 686c97dbc6..0000000000 --- a/plugins/ActivityPub/lib/models/Activitypub_message.php +++ /dev/null @@ -1,96 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -defined('GNUSOCIAL') || die(); - -/** - * ActivityPub direct note representation - * - * @author Bruno Casteleiro - * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class Activitypub_message -{ - /** - * Generates a pretty message from a Notice object - * - * @param Notice $message - * - * @return array array to be used in a response - * - * @author Bruno Casteleiro - */ - public static function message_to_array(Notice $message): array - { - $from = $message->getProfile(); - - $tags = []; - foreach ($message->getTags() as $tag) { - if ($tag != '') { // Hacky workaround to avoid stupid outputs - $tags[] = Activitypub_tag::tag_to_array($tag); - } - } - - $to = []; - foreach ($message->getAttentionProfiles() as $to_profile) { - $to[] = $href = $to_profile->getUri(); - $tags[] = Activitypub_mention_tag::mention_tag_to_array_from_values($href, $to_profile->getNickname() . '@' . parse_url($href, PHP_URL_HOST)); - } - - $item = [ - '@context' => 'https://www.w3.org/ns/activitystreams', - 'id' => common_local_url('showmessage', ['message' => $message->getID()]), - 'type' => 'Note', - 'published' => str_replace(' ', 'T', $message->created) . 'Z', - 'attributedTo' => $from->getUri(), - 'to' => $to, - 'cc' => [], - 'content' => $message->getRendered(), - 'attachment' => [], - 'tag' => $tags, - ]; - - return $item; - } - - /** - * Create a private Notice via ActivityPub Note Object. - * Returns created Notice. - * - * @author Bruno Casteleiro - * - * @param array $object - * @param Profile $actor_profile - * - * @throws Exception - * - * @return Notice - */ - public static function create_message(array $object, Profile $actor_profile = null): Notice - { - return Activitypub_notice::create_notice($object, $actor_profile, true); - } -} diff --git a/plugins/ActivityPub/lib/models/Activitypub_notice.php b/plugins/ActivityPub/lib/models/Activitypub_notice.php deleted file mode 100644 index d8217d1b32..0000000000 --- a/plugins/ActivityPub/lib/models/Activitypub_notice.php +++ /dev/null @@ -1,356 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * - * @see http://www.gnu.org/software/social/ - */ -defined('GNUSOCIAL') || die(); - -/** - * ActivityPub notice representation - * - * @category Plugin - * @package GNUsocial - * - * @author Diogo Cordeiro - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - */ -class Activitypub_notice -{ - /** - * Generates a pretty notice from a Notice object - * - * @param Notice $notice - * - * @throws EmptyPkeyValueException - * @throws InvalidUrlException - * @throws ServerException - * @throws Exception - * - * @return array array to be used in a response - * - * @author Diogo Cordeiro - */ - public static function notice_to_array(Notice $notice): array - { - $profile = $notice->getProfile(); - $attachments = []; - foreach ($notice->attachments() as $attachment) { - $attachments[] = Activitypub_attachment::attachment_to_array($attachment); - } - - $tags = []; - foreach ($notice->getTags() as $tag) { - if ($tag != '') { // Hacky workaround to avoid stupid outputs - $tags[] = Activitypub_tag::tag_to_array($tag); - } - } - - if ($notice->isPublic()) { - $to = ['https://www.w3.org/ns/activitystreams#Public']; - $cc = [common_local_url('apActorFollowers', ['id' => $profile->getID()])]; - } else { - // Since we currently don't support sending unlisted/followers-only - // notices, arriving here means we're instead answering to that type - // of posts. Not having subscription policy working, its safer to - // always send answers of type unlisted. - $to = []; - $cc = ['https://www.w3.org/ns/activitystreams#Public']; - } - - foreach ($notice->getAttentionProfiles() as $to_profile) { - $to[] = $href = $to_profile->getUri(); - $tags[] = Activitypub_mention_tag::mention_tag_to_array_from_values($href, $to_profile->getNickname() . '@' . parse_url($href, PHP_URL_HOST)); - } - - $item = [ - '@context' => 'https://www.w3.org/ns/activitystreams', - 'id' => self::getUrl($notice), - 'type' => 'Note', - 'published' => str_replace(' ', 'T', $notice->getCreated()) . 'Z', - 'url' => self::getUrl($notice), - 'attributedTo' => $profile->getUri(), - 'to' => $to, - 'cc' => $cc, - 'conversation' => $notice->getConversationUrl(), - 'content' => $notice->getRendered(), - 'isLocal' => $notice->isLocal(), - 'attachment' => $attachments, - 'tag' => $tags, - ]; - - // Is this a reply? - if (!empty($notice->reply_to)) { - $item['inReplyTo'] = self::getUri(Notice::getById($notice->reply_to)); - } - - // Do we have a location for this notice? - try { - $location = Notice_location::locFromStored($notice); - $item['latitude'] = $location->lat; - $item['longitude'] = $location->lon; - } catch (Exception $e) { - // Apparently no. - } - - return $item; - } - - /** - * Create a Notice via ActivityPub Note Object. - * Returns created Notice. - * - * @param array $object - * @param Profile $actor_profile - * @param bool $directMessage - * - * @throws Exception - * - * @return Notice - * - * @author Diogo Cordeiro - */ - public static function create_notice(array $object, Profile $actor_profile, bool $directMessage = false): Notice - { - $id = $object['id']; // int - $url = isset($object['url']) ? $object['url'] : $id; // string - $content = $object['content']; // string - - // possible keys: ['inReplyTo', 'latitude', 'longitude'] - $settings = []; - if (isset($object['inReplyTo'])) { - $settings['inReplyTo'] = $object['inReplyTo']; - } - if (isset($object['latitude'])) { - $settings['latitude'] = $object['latitude']; - } - if (isset($object['longitude'])) { - $settings['longitude'] = $object['longitude']; - } - - $act = new Activity(); - $act->verb = ActivityVerb::POST; - $act->time = time(); - $act->actor = $actor_profile->asActivityObject(); - $act->context = new ActivityContext(); - $options = ['source' => 'ActivityPub', - 'uri' => $id, - 'url' => $url, - 'is_local' => self::getNotePolicyType($object, $actor_profile), ]; - - if ($directMessage) { - $options['scope'] = Notice::MESSAGE_SCOPE; - } - - // Is this a reply? - if (isset($settings['inReplyTo'])) { - try { - $inReplyTo = ActivityPubPlugin::grab_notice_from_url($settings['inReplyTo']); - $act->context->replyToID = $inReplyTo->getUri(); - $act->context->replyToUrl = $inReplyTo->getUrl(); - } catch (Exception $e) { - // It failed to grab, maybe we got this note from another source - // (e.g.: OStatus) that handles this differently or we really - // failed to get it... - // Welp, nothing that we can do about, let's - // just fake we don't have such notice. - } - } else { - $inReplyTo = null; - } - - // Mentions - $mentions = []; - if (isset($object['tag']) && is_array($object['tag'])) { - foreach ($object['tag'] as $tag) { - if (array_key_exists('type', $tag) && $tag['type'] == 'Mention') { - $mentions[] = $tag['href']; - } - } - } - $mentions_profiles = []; - $discovery = new Activitypub_explorer; - foreach ($mentions as $mention) { - try { - $mentioned_profile = $discovery->lookup($mention); - if (!empty($mentioned_profile)) { - $mentions_profiles[] = $mentioned_profile[0]; - } - } catch (Exception $e) { - // Invalid actor found, just let it go, it will eventually be handled by some other federation plugin like OStatus. - } - } - unset($discovery); - - foreach ($mentions_profiles as $mp) { - if (!$mp->hasBlocked($actor_profile)) { - $act->context->attention[$mp->getUri()] = 'http://activitystrea.ms/schema/1.0/person'; - } - } - - // Add location if that is set - if (isset($settings['latitude'], $settings['longitude'])) { - $act->context->location = Location::fromLatLon($settings['latitude'], $settings['longitude']); - } - - // Reject notice if it is too long (without the HTML) - if (Notice::contentTooLong($content)) { - throw new Exception('That\'s too long. Maximum notice size is %d character.'); - } - - // Attachments (first part) - $attachments = []; - if (isset($object['attachment']) && is_array($object['attachment'])) { - foreach ($object['attachment'] as $attachment) { - if (array_key_exists('type', $attachment) - && $attachment['type'] === 'Document' - && array_key_exists('url', $attachment)) { - try { - // throws exception on failure - $attachment = MediaFile::fromUrl($attachment['url'], $actor_profile, $attachment['name']); - $act->enclosures[] = $attachment->getEnclosure(); - $attachments[] = $attachment; - } catch (Exception $e) { - // Whatever. - continue; - } - } - } - } - - $actobj = new ActivityObject(); - $actobj->type = ActivityObject::NOTE; - $actobj->content = strip_tags($content, '