ActivityPubDeliveryService.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Services;
  3. use App\Profile;
  4. use App\Util\ActivityPub\Helpers;
  5. use App\Util\ActivityPub\HttpSignature;
  6. class ActivityPubDeliveryService
  7. {
  8. public $sender;
  9. public $to;
  10. public $payload;
  11. public static function queue()
  12. {
  13. return new self;
  14. }
  15. public function from($profile)
  16. {
  17. $this->sender = $profile;
  18. return $this;
  19. }
  20. public function to(string $url)
  21. {
  22. $this->to = $url;
  23. return $this;
  24. }
  25. public function payload($payload)
  26. {
  27. $this->payload = $payload;
  28. return $this;
  29. }
  30. public function send()
  31. {
  32. return $this->queueDelivery();
  33. }
  34. protected function queueDelivery()
  35. {
  36. abort_if(!$this->sender || !$this->to || !$this->payload, 400);
  37. abort_if(!Helpers::validateUrl($this->to), 400);
  38. abort_if($this->sender->domain != null || $this->sender->status != null, 400);
  39. $body = $this->payload;
  40. $payload = json_encode($body);
  41. $headers = HttpSignature::sign($this->sender, $this->to, $body);
  42. $ch = curl_init($this->to);
  43. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  44. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  45. curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  46. curl_setopt($ch, CURLOPT_HEADER, true);
  47. curl_exec($ch);
  48. }
  49. }