inbox_handler.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * GNU social - a federating social network
  4. *
  5. * ActivityPubPlugin implementation for GNU Social
  6. *
  7. * LICENCE: This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @category Plugin
  21. * @package GNUsocial
  22. * @author Diogo Cordeiro <diogo@fc.up.pt>
  23. * @copyright 2018 Free Software Foundation http://fsf.org
  24. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  25. * @link https://www.gnu.org/software/social/
  26. */
  27. if (!defined('GNUSOCIAL')) {
  28. exit(1);
  29. }
  30. /**
  31. * ActivityPub Inbox Handler
  32. *
  33. * @category Plugin
  34. * @package GNUsocial
  35. * @author Diogo Cordeiro <diogo@fc.up.pt>
  36. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  37. * @link http://www.gnu.org/software/social/
  38. */
  39. class Activitypub_inbox_handler
  40. {
  41. private $activity;
  42. private $actor;
  43. private $object;
  44. /**
  45. * Create a Inbox Handler to receive something from someone.
  46. *
  47. * @author Diogo Cordeiro <diogo@fc.up.pt>
  48. * @param Array $activity Activity we are receiving
  49. * @param Profile $actor_profile Actor originating the activity
  50. */
  51. public function __construct($activity, $actor_profile = null)
  52. {
  53. $this->activity = $activity;
  54. $this->object = $activity['object'];
  55. // Validate Activity
  56. $this->validate_activity();
  57. // Get Actor's Profile
  58. if (!is_null($actor_profile)) {
  59. $this->actor = $actor_profile;
  60. } else {
  61. $this->actor = ActivityPub_explorer::get_profile_from_url($this->activity['actor']);
  62. }
  63. // Handle the Activity
  64. $this->process();
  65. }
  66. /**
  67. * Validates if a given Activity is valid. Throws exception if not.
  68. *
  69. * @author Diogo Cordeiro <diogo@fc.upt.pt>
  70. * @throws Exception
  71. */
  72. private function validate_activity()
  73. {
  74. // Activity validation
  75. // Validate data
  76. if (!(isset($this->activity['type']))) {
  77. throw new Exception('Activity Validation Failed: Type was not specified.');
  78. }
  79. if (!isset($this->activity['actor'])) {
  80. throw new Exception('Activity Validation Failed: Actor was not specified.');
  81. }
  82. if (!isset($this->activity['object'])) {
  83. throw new Exception('Activity Validation Failed: Object was not specified.');
  84. }
  85. // Object validation
  86. switch ($this->activity['type']) {
  87. case 'Accept':
  88. Activitypub_accept::validate_object($this->object);
  89. break;
  90. case 'Create':
  91. Activitypub_create::validate_object($this->object);
  92. break;
  93. case 'Delete':
  94. case 'Follow':
  95. case 'Like':
  96. case 'Announce':
  97. if (!filter_var($this->object, FILTER_VALIDATE_URL)) {
  98. throw new Exception('Object is not a valid Object URI for Activity.');
  99. }
  100. break;
  101. case 'Undo':
  102. Activitypub_undo::validate_object($this->object);
  103. break;
  104. default:
  105. throw new Exception('Unknown Activity Type.');
  106. }
  107. }
  108. /**
  109. * Sends the Activity to proper handler in order to be processed.
  110. *
  111. * @author Diogo Cordeiro <diogo@fc.upt.pt>
  112. */
  113. private function process()
  114. {
  115. switch ($this->activity['type']) {
  116. case 'Accept':
  117. $this->handle_accept($this->actor, $this->object);
  118. break;
  119. case 'Create':
  120. $this->handle_create($this->actor, $this->object);
  121. break;
  122. case 'Delete':
  123. $this->handle_delete($this->actor, $this->object);
  124. break;
  125. case 'Follow':
  126. $this->handle_follow($this->actor, $this->object);
  127. break;
  128. case 'Like':
  129. $this->handle_like($this->actor, $this->object);
  130. break;
  131. case 'Undo':
  132. $this->handle_undo($this->actor, $this->object);
  133. break;
  134. case 'Announce':
  135. $this->handle_announce($this->actor, $this->object);
  136. break;
  137. }
  138. }
  139. /**
  140. * Handles an Accept Activity received by our inbox.
  141. *
  142. * @author Diogo Cordeiro <diogo@fc.upt.pt>
  143. * @param Profile $actor Actor
  144. * @param Array $object Activity
  145. */
  146. private function handle_accept($actor, $object)
  147. {
  148. switch ($object['type']) {
  149. case 'Follow':
  150. $this->handle_accept_follow($actor, $object);
  151. break;
  152. }
  153. }
  154. /**
  155. * Handles an Accept Follow Activity received by our inbox.
  156. *
  157. * @author Diogo Cordeiro <diogo@fc.upt.pt>
  158. * @param Profile $actor Actor
  159. * @param Array $object Activity
  160. */
  161. private function handle_accept_follow($actor, $object)
  162. {
  163. // Get valid Object profile
  164. $object_profile = new Activitypub_explorer;
  165. $object_profile = $object_profile->lookup($object['object'])[0];
  166. $pending_list = new Activitypub_pending_follow_requests($actor->getID(), $object_profile->getID());
  167. $pending_list->remove();
  168. }
  169. /**
  170. * Handles a Create Activity received by our inbox.
  171. *
  172. * @author Diogo Cordeiro <diogo@fc.upt.pt>
  173. * @param Profile $actor Actor
  174. * @param Array $object Activity
  175. */
  176. private function handle_create($actor, $object)
  177. {
  178. switch ($object['type']) {
  179. case 'Note':
  180. Activitypub_notice::create_notice($object, $actor);
  181. break;
  182. }
  183. }
  184. /**
  185. * Handles a Delete Activity received by our inbox.
  186. *
  187. * @author Diogo Cordeiro <diogo@fc.upt.pt>
  188. * @param Profile $actor Actor
  189. * @param Array $object Activity
  190. */
  191. private function handle_delete($actor, $object)
  192. {
  193. $notice = ActivityPubPlugin::grab_notice_from_url($object['object']);
  194. $notice->deleteAs($actor);
  195. }
  196. /**
  197. * Handles a Follow Activity received by our inbox.
  198. *
  199. * @author Diogo Cordeiro <diogo@fc.upt.pt>
  200. * @param Profile $actor Actor
  201. * @param Array $object Activity
  202. */
  203. private function handle_follow($actor, $object)
  204. {
  205. Activitypub_follow::follow($actor, $object);
  206. }
  207. /**
  208. * Handles a Like Activity received by our inbox.
  209. *
  210. * @author Diogo Cordeiro <diogo@fc.upt.pt>
  211. * @param Profile $actor Actor
  212. * @param Array $object Activity
  213. */
  214. private function handle_like($actor, $object)
  215. {
  216. $notice = ActivityPubPlugin::grab_notice_from_url($object);
  217. Fave::addNew($actor, $notice);
  218. }
  219. /**
  220. * Handles a Undo Activity received by our inbox.
  221. *
  222. * @author Diogo Cordeiro <diogo@fc.upt.pt>
  223. * @param Profile $actor Actor
  224. * @param Array $object Activity
  225. */
  226. private function handle_undo($actor, $object)
  227. {
  228. switch ($object['type']) {
  229. case 'Follow':
  230. $this->handle_undo_follow($actor, $object['object']);
  231. break;
  232. case 'Like':
  233. $this->handle_undo_like($actor, $object['object']);
  234. break;
  235. }
  236. }
  237. /**
  238. * Handles a Undo Like Activity received by our inbox.
  239. *
  240. * @author Diogo Cordeiro <diogo@fc.upt.pt>
  241. * @param Profile $actor Actor
  242. * @param Array $object Activity
  243. */
  244. private function handle_undo_like($actor, $object)
  245. {
  246. $notice = ActivityPubPlugin::grab_notice_from_url($object);
  247. Fave::removeEntry($actor, $notice);
  248. }
  249. /**
  250. * Handles a Undo Follow Activity received by our inbox.
  251. *
  252. * @author Diogo Cordeiro <diogo@fc.upt.pt>
  253. * @param Profile $actor Actor
  254. * @param Array $object Activity
  255. */
  256. private function handle_undo_follow($actor, $object)
  257. {
  258. // Get Object profile
  259. $object_profile = new Activitypub_explorer;
  260. $object_profile = $object_profile->lookup($object)[0];
  261. if (Subscription::exists($actor, $object_profile)) {
  262. Subscription::cancel($actor, $object_profile);
  263. // You are no longer following this person.
  264. } else {
  265. // 409: You are not following this person already.
  266. }
  267. }
  268. /**
  269. * Handles a Announce Activity received by our inbox.
  270. *
  271. * @author Diogo Cordeiro <diogo@fc.upt.pt>
  272. * @param Profile $actor Actor
  273. * @param Array $object Activity
  274. */
  275. private function handle_announce($actor, $object)
  276. {
  277. $object_notice = ActivityPubPlugin::grab_notice_from_url($object);
  278. $object_notice->repeat($actor, 'ActivityPub');
  279. }
  280. }