Explorer.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. *
  25. * @author Diogo Peralta Cordeiro <@diogo.site>
  26. * @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org
  27. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  28. */
  29. namespace Plugin\ActivityPub\Util;
  30. use App\Core\DB;
  31. use App\Core\HTTPClient;
  32. use App\Core\Log;
  33. use App\Entity\Actor;
  34. use App\Entity\LocalUser;
  35. use App\Entity\Note;
  36. use App\Util\Common;
  37. use App\Util\Exception\NoSuchActorException;
  38. use App\Util\Nickname;
  39. use Exception;
  40. use InvalidArgumentException;
  41. use const JSON_UNESCAPED_SLASHES;
  42. use Plugin\ActivityPub\ActivityPub;
  43. use Plugin\ActivityPub\Entity\ActivitypubActor;
  44. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  45. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  46. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  47. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  48. use Symfony\Contracts\HttpClient\ResponseInterface;
  49. /**
  50. * ActivityPub's own Explorer
  51. *
  52. * Allows to discovery new remote actors
  53. *
  54. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  55. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  56. */
  57. class Explorer
  58. {
  59. private array $discovered_actors = [];
  60. /**
  61. * Shortcut function to get a single profile from its URL.
  62. *
  63. * @param bool $try_online whether to try online grabbing, defaults to true
  64. * @param Actor $on_behalf_of AP Actor on behalf of whom any remote lookups are to be performed, defaults to null.
  65. * If null, outgoing GET request(s) will not be http signed.
  66. *
  67. * @throws ClientExceptionInterface
  68. * @throws NoSuchActorException
  69. * @throws RedirectionExceptionInterface
  70. * @throws ServerExceptionInterface
  71. * @throws TransportExceptionInterface
  72. */
  73. public static function getOneFromUri(string $uri, bool $try_online = true, ?Actor $on_behalf_of = null): Actor
  74. {
  75. $actors = (new self())->lookup($uri, $try_online, $on_behalf_of);
  76. switch (\count($actors)) {
  77. case 1:
  78. return $actors[0];
  79. case 0:
  80. throw new NoSuchActorException('Invalid Actor.');
  81. default:
  82. throw new InvalidArgumentException('More than one actor found for this URI.');
  83. }
  84. }
  85. /**
  86. * Get every profile from the given URL
  87. * This function cleans the $this->discovered_actor_profiles array
  88. * so that there is no erroneous data
  89. *
  90. * @param string $uri User's url
  91. * @param bool $try_online whether to try online grabbing, defaults to true
  92. * @param Actor $on_behalf_of AP Actor on behalf of whom the lookup is being performed, defaults to null.
  93. * If null, outgoing GET request(s) will not be http signed.
  94. *
  95. * @throws ClientExceptionInterface
  96. * @throws NoSuchActorException
  97. * @throws RedirectionExceptionInterface
  98. * @throws ServerExceptionInterface
  99. * @throws TransportExceptionInterface
  100. *
  101. * @return array of Actor objects
  102. */
  103. public function lookup(string $uri, bool $try_online = true, ?Actor $on_behalf_of = null): array
  104. {
  105. if (\in_array($uri, ActivityPub::PUBLIC_TO)) {
  106. return [];
  107. }
  108. Log::debug('ActivityPub Explorer: Started now looking for ' . $uri);
  109. $this->discovered_actors = [];
  110. return $this->_lookup($uri, $try_online, $on_behalf_of);
  111. }
  112. /**
  113. * Get every profile from the given URL
  114. * This is a recursive function that will accumulate the results on
  115. * $discovered_actor_profiles array
  116. *
  117. * @param string $uri User's url
  118. * @param bool $try_online whether to try online grabbing, defaults to true
  119. * @param Actor $on_behalf_of Actor on behalf of whom the lookup is being performed, defaults to null.
  120. * If null, outgoing GET request(s) will not be http signed.
  121. *
  122. * @throws ClientExceptionInterface
  123. * @throws NoSuchActorException
  124. * @throws RedirectionExceptionInterface
  125. * @throws ServerExceptionInterface
  126. * @throws TransportExceptionInterface
  127. *
  128. * @return array of Actor objects
  129. */
  130. private function _lookup(string $uri, bool $try_online = true, ?Actor $on_behalf_of = null): array
  131. {
  132. $grab_known = $this->grabKnownActor($uri);
  133. // First check if we already have it locally and, if so, return it.
  134. // If the known fetch fails and remote grab is required: store locally and return.
  135. if (!$grab_known && (!$try_online || !$this->grabRemoteActor($uri, $on_behalf_of))) {
  136. throw new NoSuchActorException('Actor not found.');
  137. }
  138. return $this->discovered_actors;
  139. }
  140. /**
  141. * Get a known user profile from its URL and joins it on
  142. * $this->discovered_actor_profiles
  143. *
  144. * @param string $uri Actor's uri
  145. *
  146. * @throws Exception
  147. * @throws NoSuchActorException
  148. *
  149. * @return bool success state
  150. */
  151. private function grabKnownActor(string $uri): bool
  152. {
  153. Log::debug('ActivityPub Explorer: Searching locally for ' . $uri . ' offline.');
  154. if (!Common::isValidHttpUrl($uri)) {
  155. Log::debug('ActivityPub Explorer: URI ' . $uri . ' was not a valid http url.');
  156. return false;
  157. }
  158. // Check if uri corresponds to local actor
  159. $resource_parts = parse_url($uri);
  160. if ($resource_parts['host'] === Common::config('site', 'server')) {
  161. $actor = $this::getLocalActorForPath($resource_parts['path']);
  162. if (!\is_null($actor)) {
  163. Log::debug('ActivityPub Explorer: Found local ActivityPub Actor for ' . $uri);
  164. $this->discovered_actors[] = $actor;
  165. return true;
  166. } else {
  167. Log::debug('ActivityPub Explorer: Unable to find a known local ActivityPub Actor for ' . $uri);
  168. }
  169. }
  170. // URI isn't for a local actor, try to get by URI more generally
  171. $aprofile = DB::findOneBy(ActivitypubActor::class, ['uri' => $uri], return_null: true);
  172. if (!\is_null($aprofile)) {
  173. Log::debug('ActivityPub Explorer: Found a known ActivityPub Actor for ' . $uri);
  174. $this->discovered_actors[] = $aprofile->getActor();
  175. return true;
  176. } else {
  177. Log::debug('ActivityPub Explorer: Unable to find a known ActivityPub Actor for ' . $uri);
  178. }
  179. return false;
  180. }
  181. /**
  182. * Get a remote user(s) profile(s) from its URL and joins it on
  183. * $this->discovered_actor_profiles
  184. *
  185. * @param string $uri User's url
  186. * @param Actor $on_behalf_of Actor on behalf of whom http GET requests are to be made, defaults to null.
  187. * If null, outgoing GET request(s) will not be http signed.
  188. *
  189. * @throws ClientExceptionInterface
  190. * @throws NoSuchActorException
  191. * @throws RedirectionExceptionInterface
  192. * @throws ServerExceptionInterface
  193. * @throws TransportExceptionInterface
  194. *
  195. * @return bool success state
  196. */
  197. private function grabRemoteActor(string $uri, ?Actor $on_behalf_of = null): bool
  198. {
  199. $response = $this->get($uri, $on_behalf_of);
  200. $res = json_decode($response->getContent(), true);
  201. if ($response->getStatusCode() == 410) { // If it was deleted
  202. return true; // Nothing to add.
  203. } elseif (!HTTPClient::statusCodeIsOkay($response)) { // If it is unavailable
  204. return false; // Try to add at another time.
  205. }
  206. if (\is_null($res)) {
  207. Log::debug('ActivityPub Explorer: Invalid response returned from given Actor URL: ' . $res);
  208. return true; // Nothing to add.
  209. }
  210. if ($res['type'] === 'OrderedCollection') { // It's a potential collection of actors!!!
  211. Log::debug('ActivityPub Explorer: Found a collection of actors for ' . $uri);
  212. $this->travelCollection($res['first'], $on_behalf_of);
  213. return true;
  214. } else {
  215. try {
  216. $this->discovered_actors[] = DB::wrapInTransaction(fn () => Model\Actor::fromJson(json_encode($res)))->getActor();
  217. return true;
  218. } catch (Exception $e) {
  219. Log::debug(
  220. 'ActivityPub Explorer: Invalid potential remote actor while grabbing remotely: ' . $uri
  221. . '. He returned the following: ' . json_encode($res, JSON_UNESCAPED_SLASHES)
  222. . ' and the following exception: ' . $e->getMessage(),
  223. );
  224. return false;
  225. }
  226. }
  227. return false;
  228. }
  229. /**
  230. * Allows the Explorer to transverse a collection of persons.
  231. *
  232. * @param Actor $on_behalf_of Actor on behalf of whom http GET requests are to be made, defaults to null.
  233. * If null, outgoing GET request(s) will not be http signed.
  234. * @param string $uri Collection's url
  235. *
  236. * @throws ClientExceptionInterface
  237. * @throws NoSuchActorException
  238. * @throws RedirectionExceptionInterface
  239. * @throws ServerExceptionInterface
  240. * @throws TransportExceptionInterface
  241. */
  242. private function travelCollection(string $uri, ?Actor $on_behalf_of = null): bool
  243. {
  244. $response = $this->get($uri, $on_behalf_of);
  245. $res = json_decode($response->getContent(), true);
  246. if (!isset($res['orderedItems'])) {
  247. return false;
  248. }
  249. // Accumulate findings
  250. foreach ($res['orderedItems'] as $actor_uri) {
  251. $this->_lookup($actor_uri, true, $on_behalf_of);
  252. }
  253. // Go through entire collection
  254. if (!\is_null($res['next'])) {
  255. $this->travelCollection($res['next'], $on_behalf_of);
  256. }
  257. return true;
  258. }
  259. /**
  260. * Perform an http GET request to the given uri. Will be http-signed on behalf of given Actor, if provided.
  261. *
  262. * @param Actor $on_behalf_of Actor on behalf of whom http GET requests are to be made, defaults to null.
  263. * If null, outgoing GET request(s) will not be http signed.
  264. * @param string $uri uri of remote resource, expected to return an Activity/Object of some kind.
  265. *
  266. * @return ResponseInterface The http response, for further processing.
  267. */
  268. public static function get(string $uri, ?Actor $on_behalf_of = null): ResponseInterface
  269. {
  270. $headers = [];
  271. if (!\is_null($on_behalf_of)) {
  272. // sign the http GET request
  273. $headers = HTTPSignature::sign($on_behalf_of, $uri, body: false, addlHeaders: [], method: 'get');
  274. } else {
  275. // just do a bare request
  276. $headers = ACTIVITYPUB::HTTP_CLIENT_HEADERS;
  277. }
  278. return HTTPClient::get($uri, ['headers' => $headers]);
  279. }
  280. /**
  281. * Get a remote user array from its URL (this function is only used for
  282. * profile updating and shall not be used for anything else)
  283. *
  284. * @param string $uri User's url
  285. * @param Actor $on_behalf_of Actor on behalf of whom http GET requests are to be made, defaults to null.
  286. * If null, outgoing GET request(s) will not be http signed.
  287. *
  288. * @throws ClientExceptionInterface
  289. * @throws Exception
  290. * @throws RedirectionExceptionInterface
  291. * @throws ServerExceptionInterface
  292. * @throws TransportExceptionInterface
  293. *
  294. * @return null|string If it is able to fetch, false if it's gone
  295. * // Exceptions when network issues or unsupported Activity format
  296. */
  297. public static function getRemoteActorActivity(string $uri, ?Actor $on_behalf_of = null): string|null
  298. {
  299. $response = Explorer::get($uri, $on_behalf_of);
  300. // If it was deleted
  301. if ($response->getStatusCode() == 410) {
  302. return null;
  303. } elseif (!HTTPClient::statusCodeIsOkay($response)) { // If it is unavailable
  304. throw new Exception('Non Ok Status Code for given Actor URL.');
  305. }
  306. return $response->getContent();
  307. }
  308. /**
  309. * Parse the given path and return the actor it corresponds to.
  310. *
  311. * @param String $path Path on *this instance*. Will be parsed with regular expressions.
  312. * Something like `/actor/1` or `/object/note/1`.
  313. *
  314. * @return Actor|null The actor corresponding to/owning the given uri, null if not found.
  315. */
  316. public static function getLocalActorForPath(string $path): Actor|null
  317. {
  318. // TODO: Use URLMatcher
  319. // actor_view_nickname
  320. $renick = '/\/@(' . Nickname::DISPLAY_FMT . ')\/?/';
  321. if (preg_match_all($renick, $path, $matches, \PREG_SET_ORDER, 0) === 1) {
  322. return DB::findOneBy(LocalUser::class, ['nickname' => $matches[0][1]])->getActor();
  323. }
  324. // actor_view_id
  325. $reuri = '/\/actor\/(\d+)\/?/';
  326. if (preg_match_all($reuri, $path, $matches, \PREG_SET_ORDER, 0) === 1) {
  327. return Actor::getById((int) $matches[0][1]);
  328. }
  329. // note / page / article match
  330. $renote = '/\/object\/(?:note|page|article)\/(\d+)\/?/';
  331. if (preg_match_all($renote, $path, $matches, \PREG_SET_ORDER, 0) === 1) {
  332. return Note::getById((int) $matches[0][1])->getActor();
  333. }
  334. return null;
  335. }
  336. }