explorer.php 18 KB

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