inbox_handler.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. * @throws NoProfileException
  215. * @throws Exception
  216. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  217. */
  218. private function handle_delete()
  219. {
  220. $object = $this->object;
  221. if (is_array($object)) {
  222. $object = $object['id'];
  223. }
  224. // profile deletion ?
  225. if ($this->activity['actor'] == $object) {
  226. $aprofile = Activitypub_profile::from_profile($this->actor);
  227. $this->handle_delete_profile($aprofile);
  228. return;
  229. }
  230. // note deletion ?
  231. try {
  232. $notice = ActivityPubPlugin::grab_notice_from_url($object, false);
  233. if ($notice instanceof Notice) {
  234. $this->handle_delete_note($notice);
  235. }
  236. return;
  237. } catch (Exception $e) {
  238. // either already deleted or not an object at all
  239. // nothing to do..
  240. }
  241. common_log(LOG_INFO, "Ignoring Delete activity, nothing that we can/need to handle.");
  242. }
  243. /**
  244. * Handles a Delete-Profile Activity.
  245. *
  246. * Note that the actual ap_profile is deleted during the ProfileDeleteRelated event,
  247. * subscribed by ActivityPubPlugin.
  248. *
  249. * @param Activitypub_profile $aprofile remote user being deleted
  250. * @return void
  251. * @throws NoProfileException
  252. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  253. */
  254. private function handle_delete_profile(Activitypub_profile $aprofile): void
  255. {
  256. $profile = $aprofile->local_profile();
  257. $profile->delete();
  258. }
  259. /**
  260. * Handles a Delete-Note Activity.
  261. *
  262. * @param Notice $note remote note being deleted
  263. * @return void
  264. * @throws AuthorizationException
  265. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  266. */
  267. private function handle_delete_note(Notice $note): void
  268. {
  269. $note->deleteAs($this->actor);
  270. }
  271. /**
  272. * Handles a Follow Activity received by our inbox.
  273. *
  274. * @throws AlreadyFulfilledException
  275. * @throws HTTP_Request2_Exception
  276. * @throws NoProfileException
  277. * @throws ServerException
  278. * @author Diogo Cordeiro <diogo@fc.up.pt>
  279. */
  280. private function handle_follow()
  281. {
  282. Activitypub_follow::follow($this->actor, $this->object, $this->activity['id']);
  283. }
  284. /**
  285. * Handles a Like Activity received by our inbox.
  286. *
  287. * @throws Exception
  288. * @author Diogo Cordeiro <diogo@fc.up.pt>
  289. */
  290. private function handle_like()
  291. {
  292. $notice = ActivityPubPlugin::grab_notice_from_url($this->object);
  293. Fave::addNew($this->actor, $notice);
  294. }
  295. /**
  296. * Handles a Undo Activity received by our inbox.
  297. *
  298. * @throws AlreadyFulfilledException
  299. * @throws HTTP_Request2_Exception
  300. * @throws NoProfileException
  301. * @throws ServerException
  302. * @author Diogo Cordeiro <diogo@fc.up.pt>
  303. */
  304. private function handle_undo()
  305. {
  306. switch ($this->object['type']) {
  307. case 'Follow':
  308. $this->handle_undo_follow();
  309. break;
  310. case 'Like':
  311. $this->handle_undo_like();
  312. break;
  313. }
  314. }
  315. /**
  316. * Handles a Undo Follow Activity received by our inbox.
  317. *
  318. * @throws AlreadyFulfilledException
  319. * @throws HTTP_Request2_Exception
  320. * @throws NoProfileException
  321. * @throws ServerException
  322. * @throws Exception
  323. * @author Diogo Cordeiro <diogo@fc.up.pt>
  324. */
  325. private function handle_undo_follow()
  326. {
  327. // Get Object profile
  328. $object_profile = new Activitypub_explorer;
  329. $object_profile = $object_profile->lookup($this->object['object'])[0];
  330. if (Subscription::exists($this->actor, $object_profile)) {
  331. Subscription::cancel($this->actor, $object_profile);
  332. // You are no longer following this person.
  333. Activitypub_profile::unsubscribeCacheUpdate($this->actor, $object_profile);
  334. } /*else {
  335. // 409: You already aren't following this person.
  336. }*/
  337. }
  338. /**
  339. * Handles a Undo Like Activity received by our inbox.
  340. *
  341. * @throws AlreadyFulfilledException
  342. * @throws ServerException
  343. * @throws Exception
  344. * @author Diogo Cordeiro <diogo@fc.up.pt>
  345. */
  346. private function handle_undo_like()
  347. {
  348. $notice = ActivityPubPlugin::grab_notice_from_url($this->object['object']);
  349. Fave::removeEntry($this->actor, $notice);
  350. }
  351. /**
  352. * Handles a Announce Activity received by our inbox.
  353. *
  354. * @throws Exception
  355. * @author Diogo Cordeiro <diogo@fc.up.pt>
  356. */
  357. private function handle_announce()
  358. {
  359. $object_notice = ActivityPubPlugin::grab_notice_from_url($this->object);
  360. $object_notice->repeat($this->actor, 'ActivityPub');
  361. }
  362. }