inbox_handler.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 Inbox Handler
  28. *
  29. * @category Plugin
  30. * @package GNUsocial
  31. * @author Diogo Cordeiro <diogo@fc.up.pt>
  32. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  33. */
  34. class Activitypub_inbox_handler
  35. {
  36. private $activity;
  37. private $actor;
  38. private $object;
  39. /**
  40. * Create a Inbox Handler to receive something from someone.
  41. *
  42. * @param array $activity Activity we are receiving
  43. * @param Profile $actor_profile Actor originating the activity
  44. * @throws Exception
  45. * @author Diogo Cordeiro <diogo@fc.up.pt>
  46. */
  47. public function __construct($activity, $actor_profile = null)
  48. {
  49. $this->activity = $activity;
  50. $this->object = $activity['object'];
  51. // Validate Activity
  52. if (!$this->validate_activity()) {
  53. return; // Just ignore
  54. }
  55. // Get Actor's Profile
  56. if (!is_null($actor_profile)) {
  57. $this->actor = $actor_profile;
  58. } else {
  59. $this->actor = ActivityPub_explorer::get_profile_from_url($this->activity['actor']);
  60. }
  61. // Handle the Activity
  62. $this->process();
  63. }
  64. /**
  65. * Validates if a given Activity is valid. Throws exception if not.
  66. *
  67. * @throws Exception if invalid
  68. * @return bool true if valid and acceptable, false if unsupported
  69. * @author Diogo Cordeiro <diogo@fc.up.pt>
  70. */
  71. private function validate_activity(): bool
  72. {
  73. // Activity validation
  74. // Validate data
  75. if (!(isset($this->activity['type']))) {
  76. throw new Exception('Activity Validation Failed: Type was not specified.');
  77. }
  78. if (!isset($this->activity['actor'])) {
  79. throw new Exception('Activity Validation Failed: Actor was not specified.');
  80. }
  81. if (!isset($this->activity['object'])) {
  82. throw new Exception('Activity Validation Failed: Object was not specified.');
  83. }
  84. // Object validation
  85. $valid = true;
  86. switch ($this->activity['type']) {
  87. case 'Accept':
  88. $valid = Activitypub_accept::validate_object($this->object);
  89. break;
  90. case 'Create':
  91. $valid = Activitypub_create::validate_object($this->object);
  92. break;
  93. case 'Delete':
  94. $valid = Activitypub_delete::validate_object($this->object);
  95. break;
  96. case 'Follow':
  97. case 'Like':
  98. case 'Announce':
  99. if (!filter_var($this->object, FILTER_VALIDATE_URL)) {
  100. throw new Exception('Object is not a valid Object URI for Activity.');
  101. }
  102. break;
  103. case 'Undo':
  104. $valid = Activitypub_undo::validate_object($this->object);
  105. break;
  106. default:
  107. throw new Exception('Unknown Activity Type.');
  108. }
  109. return $valid;
  110. }
  111. /**
  112. * Sends the Activity to proper handler in order to be processed.
  113. *
  114. * @throws AlreadyFulfilledException
  115. * @throws HTTP_Request2_Exception
  116. * @throws NoProfileException
  117. * @throws ServerException
  118. * @throws Exception
  119. * @author Diogo Cordeiro <diogo@fc.up.pt>
  120. */
  121. private function process()
  122. {
  123. switch ($this->activity['type']) {
  124. case 'Accept':
  125. $this->handle_accept();
  126. break;
  127. case 'Create':
  128. $this->handle_create();
  129. break;
  130. case 'Delete':
  131. $this->handle_delete();
  132. break;
  133. case 'Follow':
  134. $this->handle_follow();
  135. break;
  136. case 'Like':
  137. $this->handle_like();
  138. break;
  139. case 'Undo':
  140. $this->handle_undo();
  141. break;
  142. case 'Announce':
  143. $this->handle_announce();
  144. break;
  145. }
  146. }
  147. /**
  148. * Handles an Accept Activity received by our inbox.
  149. *
  150. * @throws HTTP_Request2_Exception
  151. * @throws NoProfileException
  152. * @throws ServerException
  153. * @author Diogo Cordeiro <diogo@fc.up.pt>
  154. */
  155. private function handle_accept()
  156. {
  157. switch ($this->object['type']) {
  158. case 'Follow':
  159. $this->handle_accept_follow();
  160. break;
  161. }
  162. }
  163. /**
  164. * Handles an Accept Follow Activity received by our inbox.
  165. *
  166. * @throws HTTP_Request2_Exception
  167. * @throws NoProfileException
  168. * @throws ServerException
  169. * @throws Exception
  170. * @author Diogo Cordeiro <diogo@fc.up.pt>
  171. */
  172. private function handle_accept_follow()
  173. {
  174. // Get valid Object profile
  175. // Note that, since this an accept_follow, the $object
  176. // profile is actually the actor that followed someone
  177. $object_profile = new Activitypub_explorer;
  178. $object_profile = $object_profile->lookup($this->object['object'])[0];
  179. Activitypub_profile::subscribeCacheUpdate($object_profile, $this->actor);
  180. $pending_list = new Activitypub_pending_follow_requests($object_profile->getID(), $this->actor->getID());
  181. $pending_list->remove();
  182. }
  183. /**
  184. * Handles a Create Activity received by our inbox.
  185. *
  186. * @throws Exception
  187. * @author Diogo Cordeiro <diogo@fc.up.pt>
  188. */
  189. private function handle_create()
  190. {
  191. switch ($this->object['type']) {
  192. case 'Note':
  193. $this->handle_create_note();
  194. break;
  195. }
  196. }
  197. /**
  198. * Handle a Create Note Activity received by our inbox.
  199. *
  200. * @throws Exception
  201. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  202. */
  203. private function handle_create_note()
  204. {
  205. if (Activitypub_create::isPrivateNote($this->activity)) {
  206. Activitypub_message::create_message($this->object, $this->actor);
  207. } else {
  208. Activitypub_notice::create_notice($this->object, $this->actor);
  209. }
  210. }
  211. /**
  212. * Handles a Delete Activity received by our inbox.
  213. *
  214. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  215. * @author Diogo Cordeiro <diogo@fc.up.pt>
  216. */
  217. private function handle_delete()
  218. {
  219. $object = $this->object;
  220. if (is_string($object)) {
  221. $client = new HTTPClient();
  222. $response = $client->get($object, ACTIVITYPUB_HTTP_CLIENT_HEADERS);
  223. $not_gone = $response->isOk();
  224. if ($not_gone) { // It's not gone, we're updating it.
  225. $object = json_decode($response->getBody(), true);
  226. switch ($object['type']) {
  227. case 'Person':
  228. try {
  229. // Update profile if we already have a copy of it
  230. $aprofile = Activitypub_profile::fromUri($object['id'], false);
  231. Activitypub_profile::update_profile($aprofile, $object);
  232. } catch (Exception $e) {
  233. // Import profile if we don't
  234. Activitypub_explorer::get_profile_from_url($object['id']);
  235. }
  236. break;
  237. case 'Note': // XXX: We do not support updating a note's contents so, we'll delete and re-fetch for now...
  238. try {
  239. $notice = ActivityPubPlugin::grab_notice_from_url($object['id'], false);
  240. if ($notice instanceof Notice) {
  241. $notice->delete();
  242. }
  243. ActivityPubPlugin::grab_notice_from_url($object['id'], true);
  244. return;
  245. } catch (Exception $e) {
  246. // either already deleted or not an object at all
  247. // nothing to do..
  248. }
  249. break;
  250. default:
  251. common_log(LOG_INFO, "Ignoring Delete activity, we do not understand for {$object['type']}.");
  252. }
  253. }
  254. }
  255. // IFF we reached this point, it either is gone or it's an array
  256. // If it's gone, we don't know the type of the deleted object, we only have a Tombstone
  257. // If we were given an array, we don't know if it's Gone or not via status code...
  258. // In both cases, we will want to fetch the ID and act on that as it is easier than updating the fields
  259. $object = $object['id'] ?? null;
  260. if (is_null($object)) {
  261. return;
  262. }
  263. // Was it a profile?
  264. try {
  265. $aprofile = Activitypub_profile::fromUri($object, false);
  266. $res = Activitypub_explorer::get_remote_user_activity($object);
  267. Activitypub_profile::update_profile($aprofile, $res);
  268. return;
  269. } catch (Exception $e) {
  270. // Means this wasn't a profile
  271. }
  272. // Was it a note?
  273. try {
  274. $client = new HTTPClient();
  275. /*$response =*/ $client->get($object, ACTIVITYPUB_HTTP_CLIENT_HEADERS);
  276. // If it were deleted
  277. //if (!$response->isOk()) { // 410 or 404
  278. $notice = ActivityPubPlugin::grab_notice_from_url($object, false);
  279. if ($notice instanceof Notice) {
  280. $notice->delete();
  281. }
  282. // } else
  283. ActivityPubPlugin::grab_notice_from_url($object, true);
  284. // XXX: We do not support updating a note's contents so, we'll delete and re-fetch for now...
  285. } catch (Exception $e) {
  286. // Means we didn't have this note already
  287. // Or we had, deleted and it exploded trying to fetch the Tombstone, either way, we're good.
  288. }
  289. }
  290. /**
  291. * Handles a Follow Activity received by our inbox.
  292. *
  293. * @throws AlreadyFulfilledException
  294. * @throws HTTP_Request2_Exception
  295. * @throws NoProfileException
  296. * @throws ServerException
  297. * @author Diogo Cordeiro <diogo@fc.up.pt>
  298. */
  299. private function handle_follow()
  300. {
  301. Activitypub_follow::follow($this->actor, $this->object, $this->activity['id']);
  302. }
  303. /**
  304. * Handles a Like Activity received by our inbox.
  305. *
  306. * @throws Exception
  307. * @author Diogo Cordeiro <diogo@fc.up.pt>
  308. */
  309. private function handle_like()
  310. {
  311. $notice = ActivityPubPlugin::grab_notice_from_url($this->object);
  312. Activitypub_like::addNew($this->activity['id'], $this->actor, $notice);
  313. }
  314. /**
  315. * Handles a Undo Activity received by our inbox.
  316. *
  317. * @throws AlreadyFulfilledException
  318. * @throws HTTP_Request2_Exception
  319. * @throws NoProfileException
  320. * @throws ServerException
  321. * @author Diogo Cordeiro <diogo@fc.up.pt>
  322. */
  323. private function handle_undo()
  324. {
  325. switch ($this->object['type']) {
  326. case 'Follow':
  327. $this->handle_undo_follow();
  328. break;
  329. case 'Like':
  330. $this->handle_undo_like();
  331. break;
  332. }
  333. }
  334. /**
  335. * Handles a Undo Follow Activity received by our inbox.
  336. *
  337. * @throws AlreadyFulfilledException
  338. * @throws HTTP_Request2_Exception
  339. * @throws NoProfileException
  340. * @throws ServerException
  341. * @throws Exception
  342. * @author Diogo Cordeiro <diogo@fc.up.pt>
  343. */
  344. private function handle_undo_follow()
  345. {
  346. // Get Object profile
  347. $object_profile = new Activitypub_explorer;
  348. $object_profile = $object_profile->lookup($this->object['object'])[0];
  349. if (Subscription::exists($this->actor, $object_profile)) {
  350. Subscription::cancel($this->actor, $object_profile);
  351. // You are no longer following this person.
  352. Activitypub_profile::unsubscribeCacheUpdate($this->actor, $object_profile);
  353. } /*else {
  354. // 409: You already aren't following this person.
  355. }*/
  356. }
  357. /**
  358. * Handles a Undo Like Activity received by our inbox.
  359. *
  360. * @throws AlreadyFulfilledException
  361. * @throws ServerException
  362. * @throws Exception
  363. * @author Diogo Cordeiro <diogo@fc.up.pt>
  364. */
  365. private function handle_undo_like()
  366. {
  367. $notice = ActivityPubPlugin::grab_notice_from_url($this->activity['id']);
  368. Fave::removeEntry($this->actor, $notice);
  369. }
  370. /**
  371. * Handles a Announce Activity received by our inbox.
  372. *
  373. * @throws Exception
  374. * @author Diogo Cordeiro <diogo@fc.up.pt>
  375. */
  376. private function handle_announce()
  377. {
  378. $notice = ActivityPubPlugin::grab_notice_from_url($this->object);
  379. Activitypub_announce::repeat($this->activity['id'], $this->actor, $notice);
  380. }
  381. }