PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/OStatus/lib/salmonaction.php

https://gitlab.com/piratas/tortuga
PHP | 293 lines | 191 code | 32 blank | 70 comment | 16 complexity | a4be3d00640d7974ca4afbbf9326b76c MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, AGPL-3.0
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * @package OStatusPlugin
  21. * @author James Walker <james@status.net>
  22. */
  23. if (!defined('GNUSOCIAL')) { exit(1); }
  24. class SalmonAction extends Action
  25. {
  26. protected $needPost = true;
  27. protected $oprofile = null; // Ostatus_profile of the actor
  28. protected $actor = null; // Profile object of the actor
  29. var $xml = null;
  30. var $activity = null;
  31. var $target = null;
  32. protected function prepare(array $args=array())
  33. {
  34. GNUsocial::setApi(true); // Send smaller error pages
  35. parent::prepare($args);
  36. if (!isset($_SERVER['CONTENT_TYPE'])) {
  37. // TRANS: Client error. Do not translate "Content-type"
  38. $this->clientError(_m('Salmon requires a Content-type header.'));
  39. }
  40. $envxml = null;
  41. switch ($_SERVER['CONTENT_TYPE']) {
  42. case 'application/magic-envelope+xml':
  43. $envxml = file_get_contents('php://input');
  44. break;
  45. case 'application/x-www-form-urlencoded':
  46. $envxml = Magicsig::base64_url_decode($this->trimmed('xml'));
  47. break;
  48. default:
  49. // TRANS: Client error. Do not translate the quoted "application/[type]" strings.
  50. $this->clientError(_m('Salmon requires "application/magic-envelope+xml". For Diaspora we also accept "application/x-www-form-urlencoded" with an "xml" parameter.', 415));
  51. }
  52. try {
  53. if (empty($envxml)) {
  54. throw new ClientException('No magic envelope supplied in POST.');
  55. }
  56. $magic_env = new MagicEnvelope($envxml); // parse incoming XML as a MagicEnvelope
  57. $entry = $magic_env->getPayload(); // Not cryptographically verified yet!
  58. $this->activity = new Activity($entry->documentElement);
  59. if (empty($this->activity->actor->id)) {
  60. common_log(LOG_ERR, "broken actor: " . var_export($this->activity->actor->id, true));
  61. common_log(LOG_ERR, "activity with no actor: " . var_export($this->activity, true));
  62. // TRANS: Exception.
  63. throw new Exception(_m('Received a salmon slap from unidentified actor.'));
  64. }
  65. // ensureProfiles sets $this->actor and $this->oprofile
  66. $this->ensureProfiles();
  67. } catch (Exception $e) {
  68. common_debug('Salmon envelope parsing failed with: '.$e->getMessage());
  69. $this->clientError($e->getMessage());
  70. }
  71. // Cryptographic verification test
  72. if (!$magic_env->verify($this->actor)) {
  73. common_log(LOG_DEBUG, "Salmon signature verification failed.");
  74. // TRANS: Client error.
  75. $this->clientError(_m('Salmon signature verification failed.'));
  76. }
  77. return true;
  78. }
  79. /**
  80. * Check the posted activity type and break out to appropriate processing.
  81. */
  82. protected function handle()
  83. {
  84. parent::handle();
  85. common_log(LOG_DEBUG, "Got a " . $this->activity->verb);
  86. try {
  87. if (Event::handle('StartHandleSalmonTarget', array($this->activity, $this->target)) &&
  88. Event::handle('StartHandleSalmon', array($this->activity))) {
  89. switch ($this->activity->verb) {
  90. case ActivityVerb::POST:
  91. $this->handlePost();
  92. break;
  93. case ActivityVerb::SHARE:
  94. $this->handleShare();
  95. break;
  96. case ActivityVerb::FOLLOW:
  97. case ActivityVerb::FRIEND:
  98. $this->handleFollow();
  99. break;
  100. case ActivityVerb::UNFOLLOW:
  101. $this->handleUnfollow();
  102. break;
  103. case ActivityVerb::JOIN:
  104. $this->handleJoin();
  105. break;
  106. case ActivityVerb::LEAVE:
  107. $this->handleLeave();
  108. break;
  109. case ActivityVerb::TAG:
  110. $this->handleTag();
  111. break;
  112. case ActivityVerb::UNTAG:
  113. $this->handleUntag();
  114. break;
  115. case ActivityVerb::UPDATE_PROFILE:
  116. $this->handleUpdateProfile();
  117. break;
  118. default:
  119. // TRANS: Client exception.
  120. throw new ClientException(_m('Unrecognized activity type.'));
  121. }
  122. Event::handle('EndHandleSalmon', array($this->activity));
  123. Event::handle('EndHandleSalmonTarget', array($this->activity, $this->target));
  124. }
  125. } catch (AlreadyFulfilledException $e) {
  126. // The action's results are already fulfilled. Maybe it was a
  127. // duplicate? Maybe someone's database is out of sync?
  128. // Let's just accept it and move on.
  129. common_log(LOG_INFO, 'Salmon slap carried an event which had already been fulfilled.');
  130. }
  131. }
  132. function handlePost()
  133. {
  134. // TRANS: Client exception.
  135. throw new ClientException(_m('This target does not understand posts.'));
  136. }
  137. function handleFollow()
  138. {
  139. // TRANS: Client exception.
  140. throw new ClientException(_m('This target does not understand follows.'));
  141. }
  142. function handleUnfollow()
  143. {
  144. // TRANS: Client exception.
  145. throw new ClientException(_m('This target does not understand unfollows.'));
  146. }
  147. function handleShare()
  148. {
  149. // TRANS: Client exception.
  150. throw new ClientException(_m('This target does not understand share events.'));
  151. }
  152. function handleJoin()
  153. {
  154. // TRANS: Client exception.
  155. throw new ClientException(_m('This target does not understand joins.'));
  156. }
  157. function handleLeave()
  158. {
  159. // TRANS: Client exception.
  160. throw new ClientException(_m('This target does not understand leave events.'));
  161. }
  162. function handleTag()
  163. {
  164. // TRANS: Client exception.
  165. throw new ClientException(_m('This target does not understand list events.'));
  166. }
  167. function handleUntag()
  168. {
  169. // TRANS: Client exception.
  170. throw new ClientException(_m('This target does not understand unlist events.'));
  171. }
  172. /**
  173. * Remote user sent us an update to their profile.
  174. * If we already know them, accept the updates.
  175. */
  176. function handleUpdateProfile()
  177. {
  178. $oprofile = Ostatus_profile::getActorProfile($this->activity);
  179. if ($oprofile instanceof Ostatus_profile) {
  180. common_log(LOG_INFO, "Got a profile-update ping from $oprofile->uri");
  181. $oprofile->updateFromActivityObject($this->activity->actor);
  182. } else {
  183. common_log(LOG_INFO, "Ignoring profile-update ping from unknown " . $this->activity->actor->id);
  184. }
  185. }
  186. function ensureProfiles()
  187. {
  188. try {
  189. $this->oprofile = Ostatus_profile::getActorProfile($this->activity);
  190. if (!$this->oprofile instanceof Ostatus_profile) {
  191. throw new UnknownUriException($this->activity->actor->id);
  192. }
  193. } catch (UnknownUriException $e) {
  194. // Apparently we didn't find the Profile object based on our URI,
  195. // so OStatus doesn't have it with this URI in ostatus_profile.
  196. // Try to look it up again, remote side may have changed from http to https
  197. // or maybe publish an acct: URI now instead of an http: URL.
  198. //
  199. // Steps:
  200. // 1. Check the newly received URI. Who does it say it is?
  201. // 2. Compare these alleged identities to our local database.
  202. // 3. If we found any locally stored identities, ask it about its aliases.
  203. // 4. Do any of the aliases from our known identity match the recently introduced one?
  204. //
  205. // Example: We have stored http://example.com/user/1 but this URI says https://example.com/user/1
  206. common_debug('No local Profile object found for a magicsigned activity author URI: '.$e->object_uri);
  207. $disco = new Discovery();
  208. $xrd = $disco->lookup($e->object_uri);
  209. // Step 1: We got a bunch of discovery data for https://example.com/user/1 which includes
  210. // aliases https://example.com/user and hopefully our original http://example.com/user/1 too
  211. $all_ids = array_merge(array($xrd->subject), $xrd->aliases);
  212. if (!in_array($e->object_uri, $all_ids)) {
  213. common_debug('The activity author URI we got was not listed itself when doing discovery on it.');
  214. throw $e;
  215. }
  216. // Go through each reported alias from lookup to see if we know this already
  217. foreach ($all_ids as $aliased_uri) {
  218. $oprofile = Ostatus_profile::getKV('uri', $aliased_uri);
  219. if (!$oprofile instanceof Ostatus_profile) {
  220. continue; // unknown locally, check the next alias
  221. }
  222. // Step 2: We found the alleged http://example.com/user/1 URI in our local database,
  223. // but this can't be trusted yet because anyone can publish any alias.
  224. common_debug('Found a local Ostatus_profile for "'.$e->object_uri.'" with this URI: '.$aliased_uri);
  225. // We found an existing OStatus profile, but is it really the same? Do a callback to the URI's origin
  226. // Step 3: lookup our previously known http://example.com/user/1 webfinger etc.
  227. $xrd = $disco->lookup($oprofile->getUri()); // getUri returns ->uri, which we filtered on earlier
  228. $doublecheck_aliases = array_merge(array($xrd->subject), $xrd->aliases);
  229. common_debug('Trying to match known "'.$aliased_uri.'" against its returned aliases: '.implode(' ', $doublecheck_aliases));
  230. // if we find our original URI here, it is a legitimate alias
  231. // Step 4: Is the newly introduced https://example.com/user/1 URI in the list of aliases
  232. // presented by http://example.com/user/1 (i.e. do they both say they are the same identity?)
  233. if (in_array($e->object_uri, $doublecheck_aliases)) {
  234. common_debug('URIFIX These identities both say they are each other: "'.$aliased_uri.'" and "'.$e->object_uri.'"');
  235. $orig = clone($oprofile);
  236. $oprofile->uri = $e->object_uri;
  237. common_debug('URIFIX Updating Ostatus_profile URI for '.$aliased_uri.' to '.$oprofile->uri);
  238. $oprofile->updateWithKeys($orig, 'uri'); // 'uri' is the primary key column
  239. unset($orig);
  240. $this->oprofile = $oprofile;
  241. break; // don't iterate through aliases anymore
  242. }
  243. }
  244. // We might end up here after $all_ids is iterated through without a $this->oprofile value,
  245. if (!$this->oprofile instanceof Ostatus_profile) {
  246. common_debug("We do not have a local profile to connect to this activity's author. Let's create one.");
  247. // ensureActivityObjectProfile throws exception on failure
  248. $this->oprofile = Ostatus_profile::ensureActivityObjectProfile($this->activity->actor);
  249. }
  250. }
  251. assert($this->oprofile instanceof Ostatus_profile);
  252. $this->actor = $this->oprofile->localProfile();
  253. }
  254. function saveNotice()
  255. {
  256. if (!$this->oprofile instanceof Ostatus_profile) {
  257. common_debug('Ostatus_profile missing in ' . get_class(). ' profile: '.var_export($this->profile));
  258. }
  259. return $this->oprofile->processPost($this->activity, 'salmon');
  260. }
  261. }