ActivityPubFetchService.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Http;
  4. use App\Profile;
  5. use App\Util\ActivityPub\Helpers;
  6. use App\Util\ActivityPub\HttpSignature;
  7. use Illuminate\Http\Client\ConnectionException;
  8. use Illuminate\Http\Client\RequestException;
  9. class ActivityPubFetchService
  10. {
  11. public static function get($url, $validateUrl = true)
  12. {
  13. if($validateUrl === true) {
  14. if(!Helpers::validateUrl($url)) {
  15. return 0;
  16. }
  17. }
  18. $baseHeaders = [
  19. 'Accept' => 'application/activity+json, application/ld+json',
  20. ];
  21. $headers = HttpSignature::instanceActorSign($url, false, $baseHeaders, 'get');
  22. $headers['Accept'] = 'application/activity+json, application/ld+json';
  23. $headers['User-Agent'] = 'PixelFedBot/1.0.0 (Pixelfed/'.config('pixelfed.version').'; +'.config('app.url').')';
  24. try {
  25. $res = Http::withOptions(['allow_redirects' => false])->withHeaders($headers)
  26. ->timeout(30)
  27. ->connectTimeout(5)
  28. ->retry(3, 500)
  29. ->get($url);
  30. } catch (RequestException $e) {
  31. return;
  32. } catch (ConnectionException $e) {
  33. return;
  34. } catch (Exception $e) {
  35. return;
  36. }
  37. if(!$res->ok()) {
  38. return;
  39. }
  40. return $res->body();
  41. }
  42. }