[ActivityPub] Consistent headers in explorer requests

Minor indentation fixes
This commit is contained in:
Diogo Cordeiro 2019-10-11 16:00:11 +01:00
parent f1717bde51
commit d9b5ef1cee
2 changed files with 70 additions and 83 deletions

View File

@ -424,7 +424,7 @@ class ActivityPubPlugin extends Plugin
if ($aprofile instanceof Activitypub_profile) { if ($aprofile instanceof Activitypub_profile) {
$url = common_local_url('userbyid', ['id' => $aprofile->getID()], null, null, false); $url = common_local_url('userbyid', ['id' => $aprofile->getID()], null, null, false);
common_redirect($url, 303); common_redirect($url, 303);
return true; return false;
} }
} elseif (filter_var($query, FILTER_VALIDATE_URL)) { // URL found! } elseif (filter_var($query, FILTER_VALIDATE_URL)) { // URL found!
/* Is this an ActivityPub notice? */ /* Is this an ActivityPub notice? */

View File

@ -40,6 +40,8 @@ class Activitypub_explorer
{ {
private $discovered_actor_profiles = []; private $discovered_actor_profiles = [];
private $temp_res; // global variable to hold a temporary http response private $temp_res; // global variable to hold a temporary http response
private static $headers = ['Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
'User-Agent: GNUsocialBot ' . GNUSOCIAL_VERSION . ' - https://gnusocial.network'];
/** /**
* Shortcut function to get a single profile from its URL. * Shortcut function to get a single profile from its URL.
@ -84,7 +86,7 @@ class Activitypub_explorer
return []; return [];
} }
common_debug('ActivityPub Explorer: Started now looking for '.$url); common_debug('ActivityPub Explorer: Started now looking for ' . $url);
$this->discovered_actor_profiles = []; $this->discovered_actor_profiles = [];
return $this->_lookup($url, $grab_online); return $this->_lookup($url, $grab_online);
@ -120,24 +122,21 @@ class Activitypub_explorer
/** /**
* This ensures that we are using a valid ActivityPub URI * This ensures that we are using a valid ActivityPub URI
* *
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @param string $url * @param string $url
* @return bool success state (related to the response) * @return bool success state (related to the response)
* @throws Exception (If the HTTP request fails) * @throws Exception (If the HTTP request fails)
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/ */
private function ensure_proper_remote_uri($url) private function ensure_proper_remote_uri($url)
{ {
$client = new HTTPClient(); $client = new HTTPClient();
$headers = []; $response = $client->get($url, self::$headers);
$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);
$res = json_decode($response->getBody(), true); $res = json_decode($response->getBody(), true);
if (self::validate_remote_response($res)) { if (self::validate_remote_response($res)) {
$this->temp_res = $res; $this->temp_res = $res;
return true; return true;
} else { } else {
common_debug('ActivityPub Explorer: Invalid potential remote actor while ensuring URI: '.$url. '. He returned the following: '.json_encode($res, JSON_UNESCAPED_SLASHES)); common_debug('ActivityPub Explorer: Invalid potential remote actor while ensuring URI: ' . $url . '. He returned the following: ' . json_encode($res, JSON_UNESCAPED_SLASHES));
} }
return false; return false;
@ -157,9 +156,9 @@ class Activitypub_explorer
private function grab_local_user($uri, $online = false) private function grab_local_user($uri, $online = false)
{ {
if ($online) { if ($online) {
common_debug('ActivityPub Explorer: Searching locally for '.$uri. ' with online resources.'); common_debug('ActivityPub Explorer: Searching locally for ' . $uri . ' with online resources.');
} else { } else {
common_debug('ActivityPub Explorer: Searching locally for '.$uri. ' offline.'); common_debug('ActivityPub Explorer: Searching locally for ' . $uri . ' offline.');
} }
// Ensure proper remote URI // Ensure proper remote URI
// If an exception occurs here it's better to just leave everything // If an exception occurs here it's better to just leave everything
@ -173,34 +172,34 @@ class Activitypub_explorer
$aprofile = self::get_aprofile_by_url($uri); $aprofile = self::get_aprofile_by_url($uri);
if ($aprofile instanceof Activitypub_profile) { if ($aprofile instanceof Activitypub_profile) {
$profile = $aprofile->local_profile(); $profile = $aprofile->local_profile();
common_debug('ActivityPub Explorer: Found a local Aprofile for '.$uri); common_debug('ActivityPub Explorer: Found a local Aprofile for ' . $uri);
// We found something! // We found something!
$this->discovered_actor_profiles[]= $profile; $this->discovered_actor_profiles[] = $profile;
unset($this->temp_res); // IMPORTANT to avoid _dangerous_ noise in the Explorer system unset($this->temp_res); // IMPORTANT to avoid _dangerous_ noise in the Explorer system
return true; return true;
} else { } else {
common_debug('ActivityPub Explorer: Unable to find a local Aprofile for '.$uri.' - looking for a Profile instead.'); common_debug('ActivityPub Explorer: Unable to find a local Aprofile for ' . $uri . ' - looking for a Profile instead.');
// Well, maybe it is a pure blood? // Well, maybe it is a pure blood?
// Iff, we are in the same instance: // Iff, we are in the same instance:
$ACTIVITYPUB_BASE_ACTOR_URI_length = strlen(ACTIVITYPUB_BASE_ACTOR_URI); $ACTIVITYPUB_BASE_ACTOR_URI_length = strlen(ACTIVITYPUB_BASE_ACTOR_URI);
if (substr($uri, 0, $ACTIVITYPUB_BASE_ACTOR_URI_length) == ACTIVITYPUB_BASE_ACTOR_URI) { if (substr($uri, 0, $ACTIVITYPUB_BASE_ACTOR_URI_length) == ACTIVITYPUB_BASE_ACTOR_URI) {
try { try {
$profile = Profile::getByID(intval(substr($uri, $ACTIVITYPUB_BASE_ACTOR_URI_length))); $profile = Profile::getByID(intval(substr($uri, $ACTIVITYPUB_BASE_ACTOR_URI_length)));
common_debug('ActivityPub Explorer: Found a Profile for '.$uri); common_debug('ActivityPub Explorer: Found a Profile for ' . $uri);
// We found something! // We found something!
$this->discovered_actor_profiles[]= $profile; $this->discovered_actor_profiles[] = $profile;
unset($this->temp_res); // IMPORTANT to avoid _dangerous_ noise in the Explorer system unset($this->temp_res); // IMPORTANT to avoid _dangerous_ noise in the Explorer system
return true; return true;
} catch (Exception $e) { } catch (Exception $e) {
// Let the exception go on its merry way. // Let the exception go on its merry way.
common_debug('ActivityPub Explorer: Unable to find a Profile for '.$uri); common_debug('ActivityPub Explorer: Unable to find a Profile for ' . $uri);
} }
} }
} }
// If offline grabbing failed, attempt again with online resources // If offline grabbing failed, attempt again with online resources
if (!$online) { if (!$online) {
common_debug('ActivityPub Explorer: Will try everything again with online resources against: '.$uri); common_debug('ActivityPub Explorer: Will try everything again with online resources against: ' . $uri);
return $this->grab_local_user($uri, true); return $this->grab_local_user($uri, true);
} }
@ -221,28 +220,25 @@ class Activitypub_explorer
*/ */
private function grab_remote_user($url) private function grab_remote_user($url)
{ {
common_debug('ActivityPub Explorer: Trying to grab a remote actor for '.$url); common_debug('ActivityPub Explorer: Trying to grab a remote actor for ' . $url);
if (!isset($this->temp_res)) { if (!isset($this->temp_res)) {
$client = new HTTPClient(); $client = new HTTPClient();
$headers = []; $response = $client->get($url, self::$headers);
$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);
$res = json_decode($response->getBody(), true); $res = json_decode($response->getBody(), true);
} else { } else {
$res = $this->temp_res; $res = $this->temp_res;
unset($this->temp_res); unset($this->temp_res);
} }
if (isset($res['type']) && $res['type'] === 'OrderedCollection' && isset($res['first'])) { // It's a potential collection of actors!!! 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); common_debug('ActivityPub Explorer: Found a collection of actors for ' . $url);
$this->travel_collection($res['first']); $this->travel_collection($res['first']);
return true; return true;
} elseif (self::validate_remote_response($res)) { } elseif (self::validate_remote_response($res)) {
common_debug('ActivityPub Explorer: Found a valid remote actor for '.$url); common_debug('ActivityPub Explorer: Found a valid remote actor for ' . $url);
$this->discovered_actor_profiles[]= $this->store_profile($res); $this->discovered_actor_profiles[] = $this->store_profile($res);
return true; return true;
} else { } else {
common_debug('ActivityPub Explorer: Invalid potential remote actor while grabbing remotely: '.$url. '. He returned the following: '.json_encode($res, JSON_UNESCAPED_SLASHES)); 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;
@ -284,7 +280,7 @@ class Activitypub_explorer
$this->update_avatar($profile, $res['icon']['url']); $this->update_avatar($profile, $res['icon']['url']);
} catch (Exception $e) { } catch (Exception $e) {
// Let the exception go, it isn't a serious issue // Let the exception go, it isn't a serious issue
common_debug('ActivityPub Explorer: An error ocurred while grabbing remote avatar: '.$e->getMessage()); common_debug('ActivityPub Explorer: An error ocurred while grabbing remote avatar: ' . $e->getMessage());
} }
} }
@ -294,18 +290,18 @@ class Activitypub_explorer
/** /**
* Download and update given avatar image * Download and update given avatar image
* *
* @author GNU social
* @param Profile $profile * @param Profile $profile
* @param string $url * @param string $url
* @return Avatar The Avatar we have on disk. * @return Avatar The Avatar we have on disk.
* @throws Exception in various failure cases * @throws Exception in various failure cases
* @author GNU social
*/ */
public static function update_avatar(Profile $profile, $url) public static function update_avatar(Profile $profile, $url)
{ {
common_debug('ActivityPub Explorer: Started grabbing remote avatar from: '.$url); common_debug('ActivityPub Explorer: Started grabbing remote avatar from: ' . $url);
if (!filter_var($url, FILTER_VALIDATE_URL)) { if (!filter_var($url, FILTER_VALIDATE_URL)) {
// TRANS: Server exception. %s is a URL. // TRANS: Server exception. %s is a URL.
common_debug('ActivityPub Explorer: Failed because it is an invalid url: '.$url); common_debug('ActivityPub Explorer: Failed because it is an invalid url: ' . $url);
throw new ServerException(sprintf('Invalid avatar URL %s.', $url)); throw new ServerException(sprintf('Invalid avatar URL %s.', $url));
} }
@ -316,12 +312,12 @@ class Activitypub_explorer
$imgData = HTTPClient::quickGet($url); $imgData = HTTPClient::quickGet($url);
// Make sure it's at least an image file. ImageFile can do the rest. // Make sure it's at least an image file. ImageFile can do the rest.
if (false === getimagesizefromstring($imgData)) { if (false === getimagesizefromstring($imgData)) {
common_debug('ActivityPub Explorer: Failed because the downloaded avatar: '.$url. 'is not a valid image.'); common_debug('ActivityPub Explorer: Failed because the downloaded avatar: ' . $url . 'is not a valid image.');
throw new UnsupportedMediaException('Downloaded avatar was not an image.'); throw new UnsupportedMediaException('Downloaded avatar was not an image.');
} }
file_put_contents($temp_filename, $imgData); file_put_contents($temp_filename, $imgData);
unset($imgData); // No need to carry this in memory. unset($imgData); // No need to carry this in memory.
common_debug('ActivityPub Explorer: Stored dowloaded avatar in: '.$temp_filename); common_debug('ActivityPub Explorer: Stored dowloaded avatar in: ' . $temp_filename);
$id = $profile->getID(); $id = $profile->getID();
@ -333,9 +329,9 @@ class Activitypub_explorer
common_timestamp() common_timestamp()
); );
rename($temp_filename, Avatar::path($filename)); rename($temp_filename, Avatar::path($filename));
common_debug('ActivityPub Explorer: Moved avatar from: '.$temp_filename.' to '.$filename); common_debug('ActivityPub Explorer: Moved avatar from: ' . $temp_filename . ' to ' . $filename);
} catch (Exception $e) { } catch (Exception $e) {
common_debug('ActivityPub Explorer: Something went wrong while processing the avatar from: '.$url.' details: '.$e->getMessage()); common_debug('ActivityPub Explorer: Something went wrong while processing the avatar from: ' . $url . ' details: ' . $e->getMessage());
unlink($temp_filename); unlink($temp_filename);
throw $e; throw $e;
} }
@ -353,7 +349,7 @@ class Activitypub_explorer
$profile->avatar = $url; $profile->avatar = $url;
$profile->update($orig); $profile->update($orig);
common_debug('ActivityPub Explorer: Seted Avatar from: '.$url.' to profile.'); common_debug('ActivityPub Explorer: Seted Avatar from: ' . $url . ' to profile.');
return Avatar::getUploaded($profile); return Avatar::getUploaded($profile);
} }
@ -361,9 +357,9 @@ class Activitypub_explorer
* Validates a remote response in order to determine whether this * Validates a remote response in order to determine whether this
* response is a valid profile or not * response is a valid profile or not
* *
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @param array $res remote response * @param array $res remote response
* @return bool success state * @return bool success state
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/ */
public static function validate_remote_response($res) public static function validate_remote_response($res)
{ {
@ -380,9 +376,9 @@ class Activitypub_explorer
* potential ActivityPub remote profiles, as so it is important to use * potential ActivityPub remote profiles, as so it is important to use
* this hacky workaround (at least for now) * this hacky workaround (at least for now)
* *
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @param string $v URL * @param string $v URL
* @return bool|Activitypub_profile false if fails | Aprofile object if successful * @return bool|Activitypub_profile false if fails | Aprofile object if successful
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/ */
public static function get_aprofile_by_url($v) public static function get_aprofile_by_url($v)
{ {
@ -412,10 +408,7 @@ class Activitypub_explorer
public static function get_actor_inboxes_uri($url) public static function get_actor_inboxes_uri($url)
{ {
$client = new HTTPClient(); $client = new HTTPClient();
$headers = []; $response = $client->get($url, self::$headers);
$headers[] = 'Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"';
$headers[] = 'User-Agent: GNUSocialBot ' . GNUSOCIAL_VERSION . ' - https://gnu.io/social';
$response = $client->get($url, $headers);
if (!$response->isOk()) { if (!$response->isOk()) {
throw new Exception('Invalid Actor URL.'); throw new Exception('Invalid Actor URL.');
} }
@ -443,10 +436,7 @@ class Activitypub_explorer
private function travel_collection($url) private function travel_collection($url)
{ {
$client = new HTTPClient(); $client = new HTTPClient();
$headers = []; $response = $client->get($url, self::$headers);
$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);
$res = json_decode($response->getBody(), true); $res = json_decode($response->getBody(), true);
if (!isset($res['orderedItems'])) { if (!isset($res['orderedItems'])) {
@ -455,7 +445,7 @@ class Activitypub_explorer
foreach ($res["orderedItems"] as $profile) { foreach ($res["orderedItems"] as $profile) {
if ($this->_lookup($profile) == false) { if ($this->_lookup($profile) == false) {
common_debug('ActivityPub Explorer: Found an invalid actor for '.$profile); common_debug('ActivityPub Explorer: Found an invalid actor for ' . $profile);
// TODO: Invalid actor found, fallback to OStatus // TODO: Invalid actor found, fallback to OStatus
} }
} }
@ -479,13 +469,10 @@ class Activitypub_explorer
public static function get_remote_user_activity($url) public static function get_remote_user_activity($url)
{ {
$client = new HTTPClient(); $client = new HTTPClient();
$headers = []; $response = $client->get($url, self::$headers);
$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);
$res = json_decode($response->getBody(), true); $res = json_decode($response->getBody(), true);
if (Activitypub_explorer::validate_remote_response($res)) { if (Activitypub_explorer::validate_remote_response($res)) {
common_debug('ActivityPub Explorer: Found a valid remote actor for '.$url); common_debug('ActivityPub Explorer: Found a valid remote actor for ' . $url);
return $res; return $res;
} }
throw new Exception('ActivityPub Explorer: Failed to get activity.'); throw new Exception('ActivityPub Explorer: Failed to get activity.');