2020-04-27 05:43:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2021-09-20 06:21:16 +00:00
|
|
|
use Illuminate\Support\Facades\Http;
|
2020-04-27 05:43:54 +00:00
|
|
|
use App\Profile;
|
|
|
|
use App\Util\ActivityPub\Helpers;
|
|
|
|
use App\Util\ActivityPub\HttpSignature;
|
2022-12-05 07:46:55 +00:00
|
|
|
use Illuminate\Http\Client\ConnectionException;
|
2023-04-20 00:57:40 +00:00
|
|
|
use Illuminate\Http\Client\RequestException;
|
2020-04-27 05:43:54 +00:00
|
|
|
|
|
|
|
class ActivityPubFetchService
|
|
|
|
{
|
2024-02-16 04:22:41 +00:00
|
|
|
public static function get($url, $validateUrl = true)
|
|
|
|
{
|
2023-08-01 09:16:08 +00:00
|
|
|
if($validateUrl === true) {
|
2024-02-16 04:22:41 +00:00
|
|
|
if(!Helpers::validateUrl($url)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2023-08-01 09:16:08 +00:00
|
|
|
}
|
2021-01-26 04:44:07 +00:00
|
|
|
|
2024-02-16 04:22:41 +00:00
|
|
|
$baseHeaders = [
|
|
|
|
'Accept' => 'application/activity+json, application/ld+json',
|
|
|
|
];
|
|
|
|
|
|
|
|
$headers = HttpSignature::instanceActorSign($url, false, $baseHeaders, 'get');
|
|
|
|
$headers['Accept'] = 'application/activity+json, application/ld+json';
|
|
|
|
$headers['User-Agent'] = 'PixelFedBot/1.0.0 (Pixelfed/'.config('pixelfed.version').'; +'.config('app.url').')';
|
|
|
|
|
|
|
|
try {
|
|
|
|
$res = Http::withOptions(['allow_redirects' => false])
|
|
|
|
->withHeaders($headers)
|
|
|
|
->timeout(30)
|
|
|
|
->connectTimeout(5)
|
|
|
|
->retry(3, 500)
|
|
|
|
->get($url);
|
|
|
|
} catch (RequestException $e) {
|
|
|
|
return;
|
|
|
|
} catch (ConnectionException $e) {
|
|
|
|
return;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$res->ok()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$res->hasHeader('Content-Type')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$acceptedTypes = [
|
|
|
|
'application/activity+json; charset=utf-8',
|
|
|
|
'application/activity+json',
|
|
|
|
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
|
|
|
|
];
|
|
|
|
|
|
|
|
$contentType = $res->getHeader('Content-Type')[0];
|
|
|
|
|
|
|
|
if(!$contentType) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!in_array($contentType, $acceptedTypes)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $res->body();
|
|
|
|
}
|
2021-09-20 06:21:16 +00:00
|
|
|
}
|