2018-07-06 11:47:28 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* GNU social - a federating social network
|
|
|
|
*
|
2018-07-10 00:17:18 +01:00
|
|
|
* ActivityPubPlugin implementation for GNU Social
|
2018-07-06 11:47:28 +01:00
|
|
|
*
|
|
|
|
* LICENCE: This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2018-07-10 00:17:18 +01:00
|
|
|
* @category Plugin
|
2018-07-06 11:47:28 +01:00
|
|
|
* @package GNUsocial
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
2018-07-10 01:47:52 +01:00
|
|
|
* @author Daniel Supernault <danielsupernault@gmail.com>
|
2018-07-10 00:17:18 +01:00
|
|
|
* @copyright 2018 Free Software Foundation http://fsf.org
|
2018-07-06 11:47:28 +01:00
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
2018-07-10 00:17:18 +01:00
|
|
|
* @link https://www.gnu.org/software/social/
|
2018-07-06 11:47:28 +01:00
|
|
|
*/
|
2018-07-10 00:17:18 +01:00
|
|
|
if (!defined ('GNUSOCIAL')) {
|
2018-07-10 01:47:52 +01:00
|
|
|
exit (1);
|
2018-07-10 00:17:18 +01:00
|
|
|
}
|
2018-07-06 11:47:28 +01:00
|
|
|
|
2018-07-10 00:17:18 +01:00
|
|
|
/**
|
2018-07-13 12:32:27 +01:00
|
|
|
* ActivityPub's own Explorer
|
|
|
|
*
|
|
|
|
* Allows to discovery new (or the same) ActivityPub profiles
|
|
|
|
*
|
2018-07-10 00:17:18 +01:00
|
|
|
* @category Plugin
|
|
|
|
* @package GNUsocial
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
|
|
|
* @link http://www.gnu.org/software/social/
|
|
|
|
*/
|
2018-07-13 00:20:18 +01:00
|
|
|
class Activitypub_explorer
|
2018-07-10 00:17:18 +01:00
|
|
|
{
|
|
|
|
private $discovered_actor_profiles = array ();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2018-07-13 00:20:18 +01:00
|
|
|
* @return array of Profile objects
|
2018-07-10 00:17:18 +01:00
|
|
|
*/
|
|
|
|
public function lookup ($url)
|
|
|
|
{
|
|
|
|
$this->discovered_actor_profiles = array ();
|
|
|
|
|
|
|
|
return $this->_lookup ($url);
|
2018-07-06 11:47:28 +01:00
|
|
|
}
|
2018-07-10 00:17:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2018-07-13 00:20:18 +01:00
|
|
|
* @return array of Profile objects
|
2018-07-10 00:17:18 +01:00
|
|
|
*/
|
|
|
|
private function _lookup ($url)
|
|
|
|
{
|
|
|
|
// First check if we already have it locally and, if so, return it
|
|
|
|
// If the local fetch fails: grab it remotely, store locally and return
|
2018-07-13 00:20:18 +01:00
|
|
|
if (! ($this->grab_local_user ($url) || $this->grab_remote_user ($url))) {
|
|
|
|
throw new Exception ("User not found");
|
|
|
|
}
|
|
|
|
|
2018-07-10 00:17:18 +01:00
|
|
|
|
|
|
|
return $this->discovered_actor_profiles;
|
2018-07-09 00:07:14 +01:00
|
|
|
}
|
2018-07-10 00:17:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a local user profiles from its URL and joins it on
|
|
|
|
* $this->discovered_actor_profiles
|
|
|
|
*
|
|
|
|
* @param string $url User's url
|
|
|
|
* @return boolean success state
|
|
|
|
*/
|
|
|
|
private function grab_local_user ($url)
|
|
|
|
{
|
2018-07-13 00:20:18 +01:00
|
|
|
if (($actor_profile = self::get_profile_by_url ($url)) != false) {
|
2018-07-10 00:17:18 +01:00
|
|
|
$this->discovered_actor_profiles[]= $actor_profile;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
/******************************** XXX: ********************************
|
|
|
|
* Sometimes it is not true that the user is not locally available, *
|
|
|
|
* mostly when it is a local user and URLs slightly changed *
|
|
|
|
* e.g.: GS instance owner changed from standard urls to pretty urls *
|
|
|
|
* (not sure if this is necessary, but anyway) *
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
// Iff we really are in the same instance
|
|
|
|
$root_url_len = strlen (common_root_url ());
|
|
|
|
if (substr ($url, 0, $root_url_len) == common_root_url ()) {
|
|
|
|
// Grab the nickname and try to get the user
|
|
|
|
if (($actor_profile = Profile::getKV ("nickname", substr ($url, $root_url_len))) != false) {
|
|
|
|
$this->discovered_actor_profiles[]= $actor_profile;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-07-09 14:41:53 +01:00
|
|
|
}
|
2018-07-10 00:17:18 +01:00
|
|
|
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
|
|
|
|
* @return boolean success state
|
|
|
|
*/
|
2018-07-13 00:20:18 +01:00
|
|
|
private function grab_remote_user ($url)
|
|
|
|
{
|
2018-07-10 00:17:18 +01:00
|
|
|
$client = new HTTPClient ();
|
|
|
|
$headers = array();
|
|
|
|
$headers[] = 'Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"';
|
|
|
|
$headers[] = 'User-Agent: GNUSocialBot v0.1 - https://gnu.io/social';
|
|
|
|
$response = $client->get ($url, $headers);
|
2018-07-13 00:20:18 +01:00
|
|
|
if (!$response->isOk ()) {
|
|
|
|
throw new Exception ("Invalid Actor URL.");
|
2018-07-10 00:17:18 +01:00
|
|
|
}
|
|
|
|
$res = json_decode ($response->getBody (), JSON_UNESCAPED_SLASHES);
|
|
|
|
if (isset ($res["orderedItems"])) { // It's a potential collection of actors!!!
|
|
|
|
foreach ($res["orderedItems"] as $profile) {
|
|
|
|
if ($this->_lookup ($profile) == false) {
|
|
|
|
// XXX: Invalid actor found, not sure how we handle those
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Go through entire collection
|
|
|
|
if (!is_null ($res["next"])) {
|
|
|
|
$this->_lookup ($res["next"]);
|
|
|
|
}
|
|
|
|
return true;
|
2018-07-15 02:13:46 +01:00
|
|
|
} else if (self::validate_remote_response ($res)) {
|
2018-07-10 00:17:18 +01:00
|
|
|
$this->discovered_actor_profiles[]= $this->store_profile ($res);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2018-07-09 00:07:14 +01:00
|
|
|
}
|
2018-07-10 00:17:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Save remote user profile in local instance
|
|
|
|
*
|
|
|
|
* @param array $res remote response
|
2018-07-13 00:20:18 +01:00
|
|
|
* @return Profile remote Profile object
|
2018-07-10 00:17:18 +01:00
|
|
|
*/
|
2018-07-13 00:20:18 +01:00
|
|
|
private function store_profile ($res)
|
|
|
|
{
|
|
|
|
$aprofile = new Activitypub_profile;
|
|
|
|
$aprofile->uri = $res["url"];
|
|
|
|
$aprofile->nickname = $res["nickname"];
|
|
|
|
$aprofile->fullname = $res["display_name"];
|
|
|
|
$aprofile->bio = substr ($res["summary"], 0, 1000);
|
|
|
|
$aprofile->inboxuri = $res["inbox"];
|
2018-07-15 02:13:46 +01:00
|
|
|
$aprofile->sharedInboxuri = isset ($res["sharedInbox"]) ? $res["sharedInbox"] : $res["inbox"];
|
2018-07-13 00:20:18 +01:00
|
|
|
|
2018-07-14 00:13:28 +01:00
|
|
|
$aprofile->do_insert ();
|
2018-07-13 00:20:18 +01:00
|
|
|
|
2018-07-14 00:13:28 +01:00
|
|
|
return $aprofile->local_profile ();
|
2018-07-10 00:17:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates a remote response in order to determine whether this
|
|
|
|
* response is a valid profile or not
|
|
|
|
*
|
|
|
|
* @param array $res remote response
|
|
|
|
* @return boolean success state
|
|
|
|
*/
|
2018-07-15 02:13:46 +01:00
|
|
|
private static function validate_remote_response ($res)
|
2018-07-13 00:20:18 +01:00
|
|
|
{
|
2018-07-15 02:13:46 +01:00
|
|
|
if (!isset ($res["url"], $res["nickname"], $res["display_name"], $res["summary"], $res["inbox"])) {
|
2018-07-10 00:17:18 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-07-09 14:41:53 +01:00
|
|
|
}
|
2018-07-13 00:20:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a profile from it's profileurl
|
|
|
|
* 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 boolean|Profile false if fails | Profile object if successful
|
|
|
|
*/
|
|
|
|
static function get_profile_by_url ($v)
|
|
|
|
{
|
|
|
|
$i = Managed_DataObject::getcached(Profile, "profileurl", $v);
|
|
|
|
if (empty ($i)) { // false = cache miss
|
|
|
|
$i = new Profile;
|
|
|
|
$result = $i->get ("profileurl", $v);
|
|
|
|
if ($result) {
|
|
|
|
// Hit!
|
|
|
|
$i->encache();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $i;
|
|
|
|
}
|
2018-07-15 02:13:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a valid actor profile url returns its inboxes
|
|
|
|
*
|
|
|
|
* @param string $url of Actor profile
|
|
|
|
* @return boolean|array false if fails | array with inbox and shared inbox if successful
|
|
|
|
*/
|
|
|
|
static function get_actor_inboxes_uri ($url)
|
|
|
|
{
|
|
|
|
$client = new HTTPClient ();
|
|
|
|
$headers = array();
|
|
|
|
$headers[] = 'Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"';
|
|
|
|
$headers[] = 'User-Agent: GNUSocialBot v0.1 - https://gnu.io/social';
|
|
|
|
$response = $client->get ($url, $headers);
|
|
|
|
if (!$response->isOk ()) {
|
|
|
|
throw new Exception ("Invalid Actor URL.");
|
|
|
|
}
|
|
|
|
$res = json_decode ($response->getBody (), JSON_UNESCAPED_SLASHES);
|
|
|
|
if (self::validate_remote_response ($res)) {
|
|
|
|
return array ("inbox" => $res["inbox"],
|
|
|
|
"sharedInbox" => isset ($res["sharedInbox"]) ? $res["sharedInbox"] : $res["inbox"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-06 11:47:28 +01:00
|
|
|
}
|