explorer.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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
  47. * @throws NoProfileException
  48. * @throws Exception
  49. * @throws ServerException
  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. * @author Diogo Cordeiro <diogo@fc.up.pt>
  113. * @param string $url
  114. * @return bool success state (related to the response)
  115. * @throws Exception (If the HTTP request fails)
  116. */
  117. private function ensure_proper_remote_uri($url)
  118. {
  119. $client = new HTTPClient();
  120. $headers = [];
  121. $headers[] = 'Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"';
  122. $headers[] = 'User-Agent: GNUSocialBot v0.1 - https://gnu.io/social';
  123. $response = $client->get($url, $headers);
  124. $res = json_decode($response->getBody(), true);
  125. if (self::validate_remote_response($res)) {
  126. $this->temp_res = $res;
  127. return true;
  128. } else {
  129. common_debug('ActivityPub Explorer: Invalid potential remote actor while ensuring URI: '.$url. '. He returned the following: '.json_encode($res, JSON_UNESCAPED_SLASHES));
  130. }
  131. return false;
  132. }
  133. /**
  134. * Get a local user profile from its URL and joins it on
  135. * $this->discovered_actor_profiles
  136. *
  137. * @param string $uri Actor's uri
  138. * @param bool $online
  139. * @return bool success state
  140. * @throws NoProfileException
  141. * @throws Exception
  142. * @author Diogo Cordeiro <diogo@fc.up.pt>
  143. */
  144. private function grab_local_user($uri, $online = false)
  145. {
  146. if ($online) {
  147. common_debug('ActivityPub Explorer: Searching locally for '.$uri. ' with online resources.');
  148. } else {
  149. common_debug('ActivityPub Explorer: Searching locally for '.$uri. ' offline.');
  150. }
  151. // Ensure proper remote URI
  152. // If an exception occurs here it's better to just leave everything
  153. // break than to continue processing
  154. if ($online && $this->ensure_proper_remote_uri($uri)) {
  155. $uri = $this->temp_res["id"];
  156. }
  157. // Try standard ActivityPub route
  158. // Is this a known filthy little mudblood?
  159. $aprofile = self::get_aprofile_by_url($uri);
  160. if ($aprofile instanceof Activitypub_profile) {
  161. $profile = $aprofile->local_profile();
  162. common_debug('ActivityPub Explorer: Found a local Aprofile for '.$uri);
  163. // We found something!
  164. $this->discovered_actor_profiles[]= $profile;
  165. unset($this->temp_res); // IMPORTANT to avoid _dangerous_ noise in the Explorer system
  166. return true;
  167. } else {
  168. common_debug('ActivityPub Explorer: Unable to find a local Aprofile for '.$uri.' - looking for a Profile instead.');
  169. // Well, maybe it is a pure blood?
  170. // Iff, we are in the same instance:
  171. $ACTIVITYPUB_BASE_ACTOR_URI_length = strlen(ACTIVITYPUB_BASE_ACTOR_URI);
  172. if (substr($uri, 0, $ACTIVITYPUB_BASE_ACTOR_URI_length) == ACTIVITYPUB_BASE_ACTOR_URI) {
  173. try {
  174. $profile = Profile::getByID(intval(substr($uri, $ACTIVITYPUB_BASE_ACTOR_URI_length)));
  175. common_debug('ActivityPub Explorer: Found a Profile for '.$uri);
  176. // We found something!
  177. $this->discovered_actor_profiles[]= $profile;
  178. unset($this->temp_res); // IMPORTANT to avoid _dangerous_ noise in the Explorer system
  179. return true;
  180. } catch (Exception $e) {
  181. // Let the exception go on its merry way.
  182. common_debug('ActivityPub Explorer: Unable to find a Profile for '.$uri);
  183. }
  184. }
  185. }
  186. // If offline grabbing failed, attempt again with online resources
  187. if (!$online) {
  188. common_debug('ActivityPub Explorer: Will try everything again with online resources against: '.$uri);
  189. return $this->grab_local_user($uri, true);
  190. }
  191. return false;
  192. }
  193. /**
  194. * Get a remote user(s) profile(s) from its URL and joins it on
  195. * $this->discovered_actor_profiles
  196. *
  197. * @param string $url User's url
  198. * @return bool success state
  199. * @throws HTTP_Request2_Exception
  200. * @throws NoProfileException
  201. * @throws ServerException
  202. * @throws Exception
  203. * @author Diogo Cordeiro <diogo@fc.up.pt>
  204. */
  205. private function grab_remote_user($url)
  206. {
  207. common_debug('ActivityPub Explorer: Trying to grab a remote actor for '.$url);
  208. if (!isset($this->temp_res)) {
  209. $client = new HTTPClient();
  210. $headers = [];
  211. $headers[] = 'Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"';
  212. $headers[] = 'User-Agent: GNUSocialBot v0.1 - https://gnu.io/social';
  213. $response = $client->get($url, $headers);
  214. $res = json_decode($response->getBody(), true);
  215. } else {
  216. $res = $this->temp_res;
  217. unset($this->temp_res);
  218. }
  219. if (isset($res['type']) && $res['type'] === 'OrderedCollection' && isset($res['first'])) { // It's a potential collection of actors!!!
  220. common_debug('ActivityPub Explorer: Found a collection of actors for '.$url);
  221. $this->travel_collection($res['first']);
  222. return true;
  223. } elseif (self::validate_remote_response($res)) {
  224. common_debug('ActivityPub Explorer: Found a valid remote actor for '.$url);
  225. $this->discovered_actor_profiles[]= $this->store_profile($res);
  226. return true;
  227. } else {
  228. common_debug('ActivityPub Explorer: Invalid potential remote actor while grabbing remotely: '.$url. '. He returned the following: '.json_encode($res, JSON_UNESCAPED_SLASHES));
  229. }
  230. return false;
  231. }
  232. /**
  233. * Save remote user profile in local instance
  234. *
  235. * @param array $res remote response
  236. * @return Profile remote Profile object
  237. * @throws NoProfileException
  238. * @throws ServerException
  239. * @throws Exception
  240. * @author Diogo Cordeiro <diogo@fc.up.pt>
  241. */
  242. private function store_profile($res)
  243. {
  244. // ActivityPub Profile
  245. $aprofile = new Activitypub_profile;
  246. $aprofile->uri = $res['id'];
  247. $aprofile->nickname = $res['preferredUsername'];
  248. $aprofile->fullname = isset($res['name']) ? $res['name'] : null;
  249. $aprofile->bio = isset($res['summary']) ? substr(strip_tags($res['summary']), 0, 1000) : null;
  250. $aprofile->inboxuri = $res['inbox'];
  251. $aprofile->sharedInboxuri = isset($res['endpoints']['sharedInbox']) ? $res['endpoints']['sharedInbox'] : $res['inbox'];
  252. $aprofile->do_insert();
  253. $profile = $aprofile->local_profile();
  254. // Public Key
  255. $apRSA = new Activitypub_rsa();
  256. $apRSA->profile_id = $profile->getID();
  257. $apRSA->public_key = $res['publicKey']['publicKeyPem'];
  258. $apRSA->store_keys();
  259. // Avatar
  260. if (isset($res['icon']['url'])) {
  261. try {
  262. $this->update_avatar($profile, $res['icon']['url']);
  263. } catch (Exception $e) {
  264. // Let the exception go, it isn't a serious issue
  265. common_debug('ActivityPub Explorer: An error ocurred while grabbing remote avatar: '.$e->getMessage());
  266. }
  267. }
  268. return $profile;
  269. }
  270. /**
  271. * Download and update given avatar image
  272. *
  273. * @author GNU social
  274. * @param Profile $profile
  275. * @param string $url
  276. * @return Avatar The Avatar we have on disk.
  277. * @throws Exception in various failure cases
  278. */
  279. public static function update_avatar(Profile $profile, $url)
  280. {
  281. common_debug('ActivityPub Explorer: Started grabbing remote avatar from: '.$url);
  282. if (!filter_var($url, FILTER_VALIDATE_URL)) {
  283. // TRANS: Server exception. %s is a URL.
  284. common_debug('ActivityPub Explorer: Failed because it is an invalid url: '.$url);
  285. throw new ServerException(sprintf('Invalid avatar URL %s.', $url));
  286. }
  287. // @todo FIXME: This should be better encapsulated
  288. // ripped from oauthstore.php (for old OMB client)
  289. $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
  290. try {
  291. $imgData = HTTPClient::quickGet($url);
  292. // Make sure it's at least an image file. ImageFile can do the rest.
  293. if (false === getimagesizefromstring($imgData)) {
  294. common_debug('ActivityPub Explorer: Failed because the downloaded avatar: '.$url. 'is not a valid image.');
  295. throw new UnsupportedMediaException('Downloaded avatar was not an image.');
  296. }
  297. file_put_contents($temp_filename, $imgData);
  298. unset($imgData); // No need to carry this in memory.
  299. common_debug('ActivityPub Explorer: Stored dowloaded avatar in: '.$temp_filename);
  300. $id = $profile->getID();
  301. $imagefile = new ImageFile(null, $temp_filename);
  302. $filename = Avatar::filename(
  303. $id,
  304. image_type_to_extension($imagefile->type),
  305. null,
  306. common_timestamp()
  307. );
  308. rename($temp_filename, Avatar::path($filename));
  309. common_debug('ActivityPub Explorer: Moved avatar from: '.$temp_filename.' to '.$filename);
  310. } catch (Exception $e) {
  311. common_debug('ActivityPub Explorer: Something went wrong while processing the avatar from: '.$url.' details: '.$e->getMessage());
  312. unlink($temp_filename);
  313. throw $e;
  314. }
  315. // @todo FIXME: Hardcoded chmod is lame, but seems to be necessary to
  316. // keep from accidentally saving images from command-line (queues)
  317. // that can't be read from web server, which causes hard-to-notice
  318. // problems later on:
  319. //
  320. // http://status.net/open-source/issues/2663
  321. chmod(Avatar::path($filename), 0644);
  322. $profile->setOriginal($filename);
  323. $orig = clone($profile);
  324. $profile->avatar = $url;
  325. $profile->update($orig);
  326. common_debug('ActivityPub Explorer: Seted Avatar from: '.$url.' to profile.');
  327. return Avatar::getUploaded($profile);
  328. }
  329. /**
  330. * Validates a remote response in order to determine whether this
  331. * response is a valid profile or not
  332. *
  333. * @author Diogo Cordeiro <diogo@fc.up.pt>
  334. * @param array $res remote response
  335. * @return bool success state
  336. */
  337. public static function validate_remote_response($res)
  338. {
  339. if (!isset($res['id'], $res['preferredUsername'], $res['inbox'], $res['publicKey']['publicKeyPem'])) {
  340. return false;
  341. }
  342. return true;
  343. }
  344. /**
  345. * Get a ActivityPub Profile from it's uri
  346. * Unfortunately GNU social cache is not truly reliable when handling
  347. * potential ActivityPub remote profiles, as so it is important to use
  348. * this hacky workaround (at least for now)
  349. *
  350. * @author Diogo Cordeiro <diogo@fc.up.pt>
  351. * @param string $v URL
  352. * @return bool|Activitypub_profile false if fails | Aprofile object if successful
  353. */
  354. public static function get_aprofile_by_url($v)
  355. {
  356. $i = Managed_DataObject::getcached("Activitypub_profile", "uri", $v);
  357. if (empty($i)) { // false = cache miss
  358. $i = new Activitypub_profile;
  359. $result = $i->get("uri", $v);
  360. if ($result) {
  361. // Hit!
  362. $i->encache();
  363. } else {
  364. return false;
  365. }
  366. }
  367. return $i;
  368. }
  369. /**
  370. * Given a valid actor profile url returns its inboxes
  371. *
  372. * @param string $url of Actor profile
  373. * @return bool|array false if fails | array with inbox and shared inbox if successful
  374. * @throws HTTP_Request2_Exception
  375. * @throws Exception
  376. * @author Diogo Cordeiro <diogo@fc.up.pt>
  377. */
  378. public static function get_actor_inboxes_uri($url)
  379. {
  380. $client = new HTTPClient();
  381. $headers = [];
  382. $headers[] = 'Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"';
  383. $headers[] = 'User-Agent: GNUSocialBot ' . GNUSOCIAL_VERSION . ' - https://gnu.io/social';
  384. $response = $client->get($url, $headers);
  385. if (!$response->isOk()) {
  386. throw new Exception('Invalid Actor URL.');
  387. }
  388. $res = json_decode($response->getBody(), true);
  389. if (self::validate_remote_response($res)) {
  390. return [
  391. 'inbox' => $res['inbox'],
  392. 'sharedInbox' => isset($res['endpoints']['sharedInbox']) ? $res['endpoints']['sharedInbox'] : $res['inbox']
  393. ];
  394. }
  395. return false;
  396. }
  397. /**
  398. * Allows the Explorer to transverse a collection of persons.
  399. *
  400. * @param string $url
  401. * @return bool
  402. * @throws HTTP_Request2_Exception
  403. * @throws NoProfileException
  404. * @throws ServerException
  405. * @author Diogo Cordeiro <diogo@fc.up.pt>
  406. */
  407. private function travel_collection($url)
  408. {
  409. $client = new HTTPClient();
  410. $headers = [];
  411. $headers[] = 'Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"';
  412. $headers[] = 'User-Agent: GNUSocialBot v0.1 - https://gnu.io/social';
  413. $response = $client->get($url, $headers);
  414. $res = json_decode($response->getBody(), true);
  415. if (!isset($res['orderedItems'])) {
  416. return false;
  417. }
  418. foreach ($res["orderedItems"] as $profile) {
  419. if ($this->_lookup($profile) == false) {
  420. common_debug('ActivityPub Explorer: Found an invalid actor for '.$profile);
  421. // TODO: Invalid actor found, fallback to OStatus
  422. }
  423. }
  424. // Go through entire collection
  425. if (!is_null($res["next"])) {
  426. $this->_lookup($res["next"]);
  427. }
  428. return true;
  429. }
  430. /**
  431. * Get a remote user array from its URL (this function is only used for
  432. * profile updating and shall not be used for anything else)
  433. *
  434. * @param string $url User's url
  435. * @return mixed
  436. * @throws Exception
  437. * @author Diogo Cordeiro <diogo@fc.up.pt>
  438. */
  439. public static function get_remote_user_activity($url)
  440. {
  441. $client = new HTTPClient();
  442. $headers = [];
  443. $headers[] = 'Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"';
  444. $headers[] = 'User-Agent: GNUSocialBot v0.1 - https://gnu.io/social';
  445. $response = $client->get($url, $headers);
  446. $res = json_decode($response->getBody(), true);
  447. if (Activitypub_explorer::validate_remote_response($res)) {
  448. common_debug('ActivityPub Explorer: Found a valid remote actor for '.$url);
  449. return $res;
  450. }
  451. throw new Exception('ActivityPub Explorer: Failed to get activity.');
  452. }
  453. }