Explorer.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. declare(strict_types=1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. /**
  20. * ActivityPub implementation for GNU social
  21. *
  22. * @package GNUsocial
  23. * @category ActivityPub
  24. * @author Diogo Peralta Cordeiro <@diogo.site>
  25. * @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org
  26. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  27. */
  28. namespace Plugin\ActivityPub\Util;
  29. use App\Core\HTTPClient;
  30. use App\Core\Log;
  31. use App\Util\Exception\NoSuchActorException;
  32. use Exception;
  33. use Plugin\ActivityPub\ActivityPub;
  34. use Plugin\ActivityPub\Entity\ActivitypubActor;
  35. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  36. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  37. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  38. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  39. use function in_array;
  40. use function is_null;
  41. use const JSON_UNESCAPED_SLASHES;
  42. /**
  43. * ActivityPub's own Explorer
  44. *
  45. * Allows to discovery new remote actors
  46. *
  47. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  48. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  49. */
  50. class Explorer
  51. {
  52. private array $discovered_activitypub_actor_profiles = [];
  53. /**
  54. * Shortcut function to get a single profile from its URL.
  55. *
  56. * @param string $url
  57. * @param bool $grab_online whether to try online grabbing, defaults to true
  58. *
  59. * @return ActivitypubActor
  60. * @throws ClientExceptionInterface
  61. * @throws NoSuchActorException
  62. * @throws RedirectionExceptionInterface
  63. * @throws ServerExceptionInterface
  64. * @throws TransportExceptionInterface
  65. */
  66. public static function get_profile_from_url(string $url, bool $grab_online = true): ActivitypubActor
  67. {
  68. $discovery = new self();
  69. // Get valid Actor object
  70. $actor_profile = $discovery->lookup($url, $grab_online);
  71. if (!empty($actor_profile)) {
  72. return $actor_profile[0];
  73. }
  74. throw new NoSuchActorException('Invalid Actor.');
  75. }
  76. /**
  77. * Get every profile from the given URL
  78. * This function cleans the $this->discovered_actor_profiles array
  79. * so that there is no erroneous data
  80. *
  81. * @param string $url User's url
  82. * @param bool $grab_online whether to try online grabbing, defaults to true
  83. *
  84. * @throws ClientExceptionInterface
  85. * @throws NoSuchActorException
  86. * @throws RedirectionExceptionInterface
  87. * @throws ServerExceptionInterface
  88. * @throws TransportExceptionInterface
  89. *
  90. * @return array of Actor objects
  91. */
  92. public function lookup(string $url, bool $grab_online = true)
  93. {
  94. if (in_array($url, ActivityPub::PUBLIC_TO)) {
  95. return [];
  96. }
  97. Log::debug('ActivityPub Explorer: Started now looking for ' . $url);
  98. $this->discovered_activitypub_actor_profiles = [];
  99. return $this->_lookup($url, $grab_online);
  100. }
  101. /**
  102. * Get every profile from the given URL
  103. * This is a recursive function that will accumulate the results on
  104. * $discovered_actor_profiles array
  105. *
  106. * @param string $url User's url
  107. * @param bool $grab_online whether to try online grabbing, defaults to true
  108. *
  109. * @throws ClientExceptionInterface
  110. * @throws NoSuchActorException
  111. * @throws RedirectionExceptionInterface
  112. * @throws ServerExceptionInterface
  113. * @throws TransportExceptionInterface
  114. *
  115. * @return array of ActivityPub Actor objects
  116. */
  117. private function _lookup(string $url, bool $grab_online = true): array
  118. {
  119. $grab_known = $this->grab_known_user($url);
  120. // First check if we already have it locally and, if so, return it.
  121. // If the known fetch fails and remote grab is required: store locally and return.
  122. if (!$grab_known && (!$grab_online || !$this->grab_remote_user($url))) {
  123. throw new NoSuchActorException('Actor not found.');
  124. }
  125. return $this->discovered_activitypub_actor_profiles;
  126. }
  127. /**
  128. * Get a known user profile from its URL and joins it on
  129. * $this->discovered_actor_profiles
  130. *
  131. * @param string $uri Actor's uri
  132. *
  133. * @throws Exception
  134. * @throws NoSuchActorException
  135. *
  136. * @return bool success state
  137. */
  138. private function grab_known_user(string $uri): bool
  139. {
  140. Log::debug('ActivityPub Explorer: Searching locally for ' . $uri . ' offline.');
  141. // Try standard ActivityPub route
  142. // Is this a known filthy little mudblood?
  143. $aprofile = self::get_aprofile_by_url($uri);
  144. if ($aprofile instanceof ActivitypubActor) {
  145. Log::debug('ActivityPub Explorer: Found a known Aprofile for ' . $uri);
  146. // We found something!
  147. $this->discovered_activitypub_actor_profiles[] = $aprofile;
  148. return true;
  149. } else {
  150. Log::debug('ActivityPub Explorer: Unable to find a known Aprofile for ' . $uri);
  151. }
  152. return false;
  153. }
  154. /**
  155. * Get a remote user(s) profile(s) from its URL and joins it on
  156. * $this->discovered_actor_profiles
  157. *
  158. * @param string $url User's url
  159. *
  160. * @throws ClientExceptionInterface
  161. * @throws NoSuchActorException
  162. * @throws RedirectionExceptionInterface
  163. * @throws ServerExceptionInterface
  164. * @throws TransportExceptionInterface
  165. *
  166. * @return bool success state
  167. */
  168. private function grab_remote_user(string $url): bool
  169. {
  170. Log::debug('ActivityPub Explorer: Trying to grab a remote actor for ' . $url);
  171. $response = HTTPClient::get($url, ['headers' => ACTIVITYPUB::HTTP_CLIENT_HEADERS]);
  172. $res = json_decode($response->getContent(), true);
  173. if ($response->getStatusCode() == 410) { // If it was deleted
  174. return true; // Nothing to add.
  175. } elseif (!HTTPClient::statusCodeIsOkay($response)) { // If it is unavailable
  176. return false; // Try to add at another time.
  177. }
  178. if (is_null($res)) {
  179. Log::debug('ActivityPub Explorer: Invalid response returned from given Actor URL: ' . $res);
  180. return true; // Nothing to add.
  181. }
  182. if ($res['type'] === 'OrderedCollection') { // It's a potential collection of actors!!!
  183. Log::debug('ActivityPub Explorer: Found a collection of actors for ' . $url);
  184. $this->travel_collection($res['first']);
  185. return true;
  186. } else {
  187. try {
  188. $this->discovered_activitypub_actor_profiles[] = Model\Actor::fromJson(json_encode($res));
  189. return true;
  190. } catch (Exception $e) {
  191. Log::debug(
  192. 'ActivityPub Explorer: Invalid potential remote actor while grabbing remotely: ' . $url
  193. . '. He returned the following: ' . json_encode($res, JSON_UNESCAPED_SLASHES)
  194. . ' and the following exception: ' . $e->getMessage()
  195. );
  196. return false;
  197. }
  198. }
  199. return false;
  200. }
  201. /**
  202. * Get a ActivityPub Profile from it's uri
  203. *
  204. * @param string $v URL
  205. *
  206. * @return ActivitypubActor|bool false if fails | Aprofile object if successful
  207. */
  208. public static function get_aprofile_by_url(string $v): ActivitypubActor|bool
  209. {
  210. $aprofile = ActivitypubActor::getByPK(['uri' => $v]);
  211. return is_null($aprofile) ? false : ActivitypubActor::getByPK(['uri' => $v]);
  212. }
  213. /**
  214. * Allows the Explorer to transverse a collection of persons.
  215. *
  216. * @param string $url
  217. * @return bool
  218. * @throws ClientExceptionInterface
  219. * @throws NoSuchActorException
  220. * @throws RedirectionExceptionInterface
  221. * @throws ServerExceptionInterface
  222. * @throws TransportExceptionInterface
  223. */
  224. private function travel_collection(string $url): bool
  225. {
  226. $response = HTTPClient::get($url, ['headers' => ACTIVITYPUB::HTTP_CLIENT_HEADERS]);
  227. $res = json_decode($response->getContent(), true);
  228. if (!isset($res['orderedItems'])) {
  229. return false;
  230. }
  231. foreach ($res['orderedItems'] as $profile) {
  232. if ($this->_lookup($profile) == false) {
  233. Log::debug('ActivityPub Explorer: Found an invalid actor for ' . $profile);
  234. }
  235. }
  236. // Go through entire collection
  237. if (!is_null($res['next'])) {
  238. $this->travel_collection($res['next']);
  239. }
  240. return true;
  241. }
  242. /**
  243. * Get a remote user array from its URL (this function is only used for
  244. * profile updating and shall not be used for anything else)
  245. *
  246. * @param string $url User's url
  247. *
  248. * @throws ClientExceptionInterface
  249. * @throws RedirectionExceptionInterface
  250. * @throws ServerExceptionInterface
  251. * @throws TransportExceptionInterface
  252. * @throws Exception
  253. *
  254. * @return string|null If it is able to fetch, false if it's gone
  255. * // Exceptions when network issues or unsupported Activity format
  256. */
  257. public static function get_remote_user_activity(string $url): string|null
  258. {
  259. $response = HTTPClient::get($url, ['headers' => ACTIVITYPUB::HTTP_CLIENT_HEADERS]);
  260. // If it was deleted
  261. if ($response->getStatusCode() == 410) {
  262. return null;
  263. } elseif (!HTTPClient::statusCodeIsOkay($response)) { // If it is unavailable
  264. throw new Exception('Non Ok Status Code for given Actor URL.');
  265. }
  266. return $response->getContent();
  267. }
  268. }