explorer.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * ActivityPub implementation for GNU social
  18. *
  19. * @package GNUsocial
  20. * @author Diogo Cordeiro <diogo@fc.up.pt>
  21. * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
  22. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  23. * @link http://www.gnu.org/software/social/
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * ActivityPub's own Explorer
  28. *
  29. * Allows to discovery new (or the same) Profiles (both local or remote)
  30. *
  31. * @category Plugin
  32. * @package GNUsocial
  33. * @author Diogo Cordeiro <diogo@fc.up.pt>
  34. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  35. */
  36. class Activitypub_explorer
  37. {
  38. private $discovered_actor_profiles = [];
  39. private $temp_res; // global variable to hold a temporary http response
  40. /**
  41. * Shortcut function to get a single profile from its URL.
  42. *
  43. * @param string $url
  44. * @param bool $grab_online whether to try online grabbing, defaults to true
  45. * @return Profile
  46. * @throws HTTP_Request2_Exception Network issues
  47. * @throws NoProfileException This won't happen
  48. * @throws Exception Invalid request
  49. * @throws ServerException Error storing remote actor
  50. * @author Diogo Cordeiro <diogo@fc.up.pt>
  51. */
  52. public static function get_profile_from_url($url, $grab_online = true)
  53. {
  54. $discovery = new Activitypub_explorer();
  55. // Get valid Actor object
  56. $actor_profile = $discovery->lookup($url, $grab_online);
  57. if (!empty($actor_profile)) {
  58. return $actor_profile[0];
  59. }
  60. throw new Exception('Invalid Actor.');
  61. }
  62. /**
  63. * Get every profile from the given URL
  64. * This function cleans the $this->discovered_actor_profiles array
  65. * so that there is no erroneous data
  66. *
  67. * @param string $url User's url
  68. * @param bool $grab_online whether to try online grabbing, defaults to true
  69. * @return array of Profile objects
  70. * @throws HTTP_Request2_Exception
  71. * @throws NoProfileException
  72. * @throws Exception
  73. * @throws ServerException
  74. * @author Diogo Cordeiro <diogo@fc.up.pt>
  75. */
  76. public function lookup(string $url, bool $grab_online = true)
  77. {
  78. if (in_array($url, ACTIVITYPUB_PUBLIC_TO)) {
  79. return [];
  80. }
  81. common_debug('ActivityPub Explorer: Started now looking for ' . $url);
  82. $this->discovered_actor_profiles = [];
  83. return $this->_lookup($url, $grab_online);
  84. }
  85. /**
  86. * Get every profile from the given URL
  87. * This is a recursive function that will accumulate the results on
  88. * $discovered_actor_profiles array
  89. *
  90. * @param string $url User's url
  91. * @param bool $grab_online whether to try online grabbing, defaults to true
  92. * @return array of Profile objects
  93. * @throws HTTP_Request2_Exception
  94. * @throws NoProfileException
  95. * @throws ServerException
  96. * @throws Exception
  97. * @author Diogo Cordeiro <diogo@fc.up.pt>
  98. */
  99. private function _lookup(string $url, bool $grab_online = true)
  100. {
  101. $grab_local = $this->grab_local_user($url);
  102. // First check if we already have it locally and, if so, return it.
  103. // If the local fetch fails and remote grab is required: store locally and return.
  104. if (!$grab_local && (!$grab_online || !$this->grab_remote_user($url))) {
  105. throw new Exception('User not found.');
  106. }
  107. return $this->discovered_actor_profiles;
  108. }
  109. /**
  110. * This ensures that we are using a valid ActivityPub URI
  111. *
  112. * @param string $url
  113. * @return bool success state (related to the response)
  114. * @throws Exception (If the HTTP request fails)
  115. * @author Diogo Cordeiro <diogo@fc.up.pt>
  116. */
  117. private function ensure_proper_remote_uri($url)
  118. {
  119. $client = new HTTPClient();
  120. $response = $client->get($url, ACTIVITYPUB_HTTP_CLIENT_HEADERS);
  121. $res = json_decode($response->getBody(), true);
  122. if (self::validate_remote_response($res)) {
  123. $this->temp_res = $res;
  124. return true;
  125. } else {
  126. common_debug('ActivityPub Explorer: Invalid potential remote actor while ensuring URI: ' . $url . '. He returned the following: ' . json_encode($res, JSON_UNESCAPED_SLASHES));
  127. }
  128. return false;
  129. }
  130. /**
  131. * Get a local user profile from its URL and joins it on
  132. * $this->discovered_actor_profiles
  133. *
  134. * @param string $uri Actor's uri
  135. * @param bool $online
  136. * @return bool success state
  137. * @throws NoProfileException
  138. * @throws Exception
  139. * @author Diogo Cordeiro <diogo@fc.up.pt>
  140. */
  141. private function grab_local_user($uri, $online = false)
  142. {
  143. if ($online) {
  144. common_debug('ActivityPub Explorer: Searching locally for ' . $uri . ' with online resources.');
  145. } else {
  146. common_debug('ActivityPub Explorer: Searching locally for ' . $uri . ' offline.');
  147. }
  148. // Ensure proper remote URI
  149. // If an exception occurs here it's better to just leave everything
  150. // break than to continue processing
  151. if ($online && $this->ensure_proper_remote_uri($uri)) {
  152. $uri = $this->temp_res["id"];
  153. }
  154. // Try standard ActivityPub route
  155. // Is this a known filthy little mudblood?
  156. $aprofile = self::get_aprofile_by_url($uri);
  157. if ($aprofile instanceof Activitypub_profile) {
  158. // Assert: This AProfile has a Profile, no try catch.
  159. $profile = $aprofile->local_profile();
  160. common_debug('ActivityPub Explorer: Found a local Aprofile for ' . $uri);
  161. // We found something!
  162. $this->discovered_actor_profiles[] = $profile;
  163. unset($this->temp_res); // IMPORTANT to avoid _dangerous_ noise in the Explorer system
  164. return true;
  165. } else {
  166. common_debug('ActivityPub Explorer: Unable to find a local Aprofile for ' . $uri . ' - looking for a Profile instead.');
  167. // Well, maybe it is a pure blood?
  168. // Iff, we are in the same instance:
  169. $ACTIVITYPUB_BASE_ACTOR_URI_length = strlen(ACTIVITYPUB_BASE_ACTOR_URI);
  170. if (substr($uri, 0, $ACTIVITYPUB_BASE_ACTOR_URI_length) == ACTIVITYPUB_BASE_ACTOR_URI) {
  171. try {
  172. $profile = Profile::getByID((int)substr($uri, $ACTIVITYPUB_BASE_ACTOR_URI_length));
  173. common_debug('ActivityPub Explorer: Found a Profile for ' . $uri);
  174. // We found something!
  175. $this->discovered_actor_profiles[] = $profile;
  176. unset($this->temp_res); // IMPORTANT to avoid _dangerous_ noise in the Explorer system
  177. return true;
  178. } catch (Exception $e) {
  179. // Let the exception go on its merry way.
  180. common_debug('ActivityPub Explorer: Unable to find a Profile for ' . $uri);
  181. }
  182. }
  183. }
  184. // If offline grabbing failed, attempt again with online resources
  185. if (!$online) {
  186. common_debug('ActivityPub Explorer: Will try everything again with online resources against: ' . $uri);
  187. return $this->grab_local_user($uri, true);
  188. }
  189. return false;
  190. }
  191. /**
  192. * Get a remote user(s) profile(s) from its URL and joins it on
  193. * $this->discovered_actor_profiles
  194. *
  195. * @param string $url User's url
  196. * @return bool success state
  197. * @throws HTTP_Request2_Exception
  198. * @throws NoProfileException
  199. * @throws ServerException
  200. * @throws Exception
  201. * @author Diogo Cordeiro <diogo@fc.up.pt>
  202. */
  203. private function grab_remote_user($url)
  204. {
  205. common_debug('ActivityPub Explorer: Trying to grab a remote actor for ' . $url);
  206. if (!isset($this->temp_res)) {
  207. $client = new HTTPClient();
  208. $response = $client->get($url, ACTIVITYPUB_HTTP_CLIENT_HEADERS);
  209. $res = json_decode($response->getBody(), true);
  210. } else {
  211. $res = $this->temp_res;
  212. unset($this->temp_res);
  213. }
  214. if (isset($res['type']) && $res['type'] === 'OrderedCollection' && isset($res['first'])) { // It's a potential collection of actors!!!
  215. common_debug('ActivityPub Explorer: Found a collection of actors for ' . $url);
  216. $this->travel_collection($res['first']);
  217. return true;
  218. } elseif (self::validate_remote_response($res)) {
  219. common_debug('ActivityPub Explorer: Found a valid remote actor for ' . $url);
  220. $this->discovered_actor_profiles[] = $this->store_profile($res);
  221. return true;
  222. } else {
  223. common_debug('ActivityPub Explorer: Invalid potential remote actor while grabbing remotely: ' . $url . '. He returned the following: ' . json_encode($res, JSON_UNESCAPED_SLASHES));
  224. }
  225. return false;
  226. }
  227. /**
  228. * Save remote user profile in local instance
  229. *
  230. * @param array $res remote response
  231. * @return Profile remote Profile object
  232. * @throws NoProfileException
  233. * @throws ServerException
  234. * @throws Exception
  235. * @author Diogo Cordeiro <diogo@fc.up.pt>
  236. */
  237. private function store_profile($res)
  238. {
  239. // ActivityPub Profile
  240. $aprofile = new Activitypub_profile;
  241. $aprofile->uri = $res['id'];
  242. $aprofile->nickname = $res['preferredUsername'];
  243. $aprofile->fullname = isset($res['name']) ? $res['name'] : null;
  244. $aprofile->bio = isset($res['summary']) ? substr(strip_tags($res['summary']), 0, 1000) : null;
  245. $aprofile->inboxuri = $res['inbox'];
  246. $aprofile->sharedInboxuri = isset($res['endpoints']['sharedInbox']) ? $res['endpoints']['sharedInbox'] : $res['inbox'];
  247. $aprofile->do_insert();
  248. $profile = $aprofile->local_profile();
  249. // Public Key
  250. $apRSA = new Activitypub_rsa();
  251. $apRSA->profile_id = $profile->getID();
  252. $apRSA->public_key = $res['publicKey']['publicKeyPem'];
  253. $apRSA->store_keys();
  254. // Avatar
  255. if (isset($res['icon']['url'])) {
  256. try {
  257. $this->update_avatar($profile, $res['icon']['url']);
  258. } catch (Exception $e) {
  259. // Let the exception go, it isn't a serious issue
  260. common_debug('ActivityPub Explorer: An error ocurred while grabbing remote avatar: ' . $e->getMessage());
  261. }
  262. }
  263. return $profile;
  264. }
  265. /**
  266. * Download and update given avatar image
  267. *
  268. * @param Profile $profile
  269. * @param string $url
  270. * @return Avatar The Avatar we have on disk.
  271. * @throws Exception in various failure cases
  272. * @author GNU social
  273. */
  274. public static function update_avatar(Profile $profile, $url)
  275. {
  276. common_debug('ActivityPub Explorer: Started grabbing remote avatar from: ' . $url);
  277. if (!filter_var($url, FILTER_VALIDATE_URL)) {
  278. // TRANS: Server exception. %s is a URL.
  279. common_debug('ActivityPub Explorer: Failed because it is an invalid url: ' . $url);
  280. throw new ServerException(sprintf('Invalid avatar URL %s.', $url));
  281. }
  282. // @todo FIXME: This should be better encapsulated
  283. // ripped from oauthstore.php (for old OMB client)
  284. $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
  285. try {
  286. $imgData = HTTPClient::quickGet($url);
  287. // Make sure it's at least an image file. ImageFile can do the rest.
  288. if (false === getimagesizefromstring($imgData)) {
  289. common_debug('ActivityPub Explorer: Failed because the downloaded avatar: ' . $url . 'is not a valid image.');
  290. throw new UnsupportedMediaException('Downloaded avatar was not an image.');
  291. }
  292. file_put_contents($temp_filename, $imgData);
  293. unset($imgData); // No need to carry this in memory.
  294. common_debug('ActivityPub Explorer: Stored dowloaded avatar in: ' . $temp_filename);
  295. $id = $profile->getID();
  296. $imagefile = new ImageFile(null, $temp_filename);
  297. $filename = Avatar::filename(
  298. $id,
  299. image_type_to_extension($imagefile->type),
  300. null,
  301. common_timestamp()
  302. );
  303. rename($temp_filename, Avatar::path($filename));
  304. common_debug('ActivityPub Explorer: Moved avatar from: ' . $temp_filename . ' to ' . $filename);
  305. } catch (Exception $e) {
  306. common_debug('ActivityPub Explorer: Something went wrong while processing the avatar from: ' . $url . ' details: ' . $e->getMessage());
  307. unlink($temp_filename);
  308. throw $e;
  309. }
  310. // @todo FIXME: Hardcoded chmod is lame, but seems to be necessary to
  311. // keep from accidentally saving images from command-line (queues)
  312. // that can't be read from web server, which causes hard-to-notice
  313. // problems later on:
  314. //
  315. // http://status.net/open-source/issues/2663
  316. chmod(Avatar::path($filename), 0644);
  317. $profile->setOriginal($filename);
  318. $orig = clone($profile);
  319. $profile->avatar = $url;
  320. $profile->update($orig);
  321. common_debug('ActivityPub Explorer: Seted Avatar from: ' . $url . ' to profile.');
  322. return Avatar::getUploaded($profile);
  323. }
  324. /**
  325. * Validates a remote response in order to determine whether this
  326. * response is a valid profile or not
  327. *
  328. * @param array $res remote response
  329. * @return bool success state
  330. * @author Diogo Cordeiro <diogo@fc.up.pt>
  331. */
  332. public static function validate_remote_response($res)
  333. {
  334. if (!isset($res['id'], $res['preferredUsername'], $res['inbox'], $res['publicKey']['publicKeyPem'])) {
  335. return false;
  336. }
  337. return true;
  338. }
  339. /**
  340. * Get a ActivityPub Profile from it's uri
  341. * Unfortunately GNU social cache is not truly reliable when handling
  342. * potential ActivityPub remote profiles, as so it is important to use
  343. * this hacky workaround (at least for now)
  344. *
  345. * @param string $v URL
  346. * @return bool|Activitypub_profile false if fails | Aprofile object if successful
  347. * @author Diogo Cordeiro <diogo@fc.up.pt>
  348. */
  349. public static function get_aprofile_by_url($v)
  350. {
  351. $i = Managed_DataObject::getcached("Activitypub_profile", "uri", $v);
  352. if (empty($i)) { // false = cache miss
  353. $i = new Activitypub_profile;
  354. $result = $i->get("uri", $v);
  355. if ($result) {
  356. // Hit!
  357. $i->encache();
  358. } else {
  359. return false;
  360. }
  361. }
  362. return $i;
  363. }
  364. /**
  365. * Given a valid actor profile url returns its inboxes
  366. *
  367. * @param string $url of Actor profile
  368. * @return bool|array false if fails | array with inbox and shared inbox if successful
  369. * @throws HTTP_Request2_Exception
  370. * @throws Exception
  371. * @author Diogo Cordeiro <diogo@fc.up.pt>
  372. */
  373. public static function get_actor_inboxes_uri($url)
  374. {
  375. $client = new HTTPClient();
  376. $response = $client->get($url, ACTIVITYPUB_HTTP_CLIENT_HEADERS);
  377. if (!$response->isOk()) {
  378. throw new Exception('Invalid Actor URL.');
  379. }
  380. $res = json_decode($response->getBody(), true);
  381. if (self::validate_remote_response($res)) {
  382. return [
  383. 'inbox' => $res['inbox'],
  384. 'sharedInbox' => isset($res['endpoints']['sharedInbox']) ? $res['endpoints']['sharedInbox'] : $res['inbox']
  385. ];
  386. }
  387. return false;
  388. }
  389. /**
  390. * Allows the Explorer to transverse a collection of persons.
  391. *
  392. * @param string $url
  393. * @return bool
  394. * @throws HTTP_Request2_Exception
  395. * @throws NoProfileException
  396. * @throws ServerException
  397. * @author Diogo Cordeiro <diogo@fc.up.pt>
  398. */
  399. private function travel_collection($url)
  400. {
  401. $client = new HTTPClient();
  402. $response = $client->get($url, ACTIVITYPUB_HTTP_CLIENT_HEADERS);
  403. $res = json_decode($response->getBody(), true);
  404. if (!isset($res['orderedItems'])) {
  405. return false;
  406. }
  407. foreach ($res["orderedItems"] as $profile) {
  408. if ($this->_lookup($profile) == false) {
  409. common_debug('ActivityPub Explorer: Found an invalid actor for ' . $profile);
  410. // TODO: Invalid actor found, fallback to OStatus
  411. }
  412. }
  413. // Go through entire collection
  414. if (!is_null($res["next"])) {
  415. $this->_lookup($res["next"]);
  416. }
  417. return true;
  418. }
  419. /**
  420. * Get a remote user array from its URL (this function is only used for
  421. * profile updating and shall not be used for anything else)
  422. *
  423. * @param string $url User's url
  424. * @return array
  425. * @throws Exception Either network issues or unsupported Activity format
  426. * @author Diogo Cordeiro <diogo@fc.up.pt>
  427. */
  428. public static function get_remote_user_activity($url)
  429. {
  430. $client = new HTTPClient();
  431. $response = $client->get($url, ACTIVITYPUB_HTTP_CLIENT_HEADERS);
  432. $res = json_decode($response->getBody(), true);
  433. if (Activitypub_explorer::validate_remote_response($res)) {
  434. common_debug('ActivityPub Explorer: Found a valid remote actor for ' . $url);
  435. return $res;
  436. }
  437. throw new Exception('ActivityPub Explorer: Failed to get activity.');
  438. }
  439. }