salmonaction.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. StatusNet::setApi(true); // Send smaller error pages
  35. parent::prepare($args);
  36. if (!isset($_SERVER['CONTENT_TYPE']) || $_SERVER['CONTENT_TYPE'] != 'application/magic-envelope+xml') {
  37. // TRANS: Client error. Do not translate "application/magic-envelope+xml".
  38. $this->clientError(_m('Salmon requires "application/magic-envelope+xml".'));
  39. }
  40. try {
  41. $envxml = file_get_contents('php://input');
  42. $magic_env = new MagicEnvelope($envxml); // parse incoming XML as a MagicEnvelope
  43. $entry = $magic_env->getPayload(); // Not cryptographically verified yet!
  44. $this->activity = new Activity($entry->documentElement);
  45. if (empty($this->activity->actor->id)) {
  46. common_log(LOG_ERR, "broken actor: " . var_export($this->activity->actor->id, true));
  47. common_log(LOG_ERR, "activity with no actor: " . var_export($this->activity, true));
  48. // TRANS: Exception.
  49. throw new Exception(_m('Received a salmon slap from unidentified actor.'));
  50. }
  51. // ensureProfiles sets $this->actor and $this->oprofile
  52. $this->ensureProfiles();
  53. } catch (Exception $e) {
  54. common_debug('Salmon envelope parsing failed with: '.$e->getMessage());
  55. $this->clientError($e->getMessage());
  56. }
  57. // Cryptographic verification test
  58. if (!$magic_env->verify($this->actor)) {
  59. common_log(LOG_DEBUG, "Salmon signature verification failed.");
  60. // TRANS: Client error.
  61. $this->clientError(_m('Salmon signature verification failed.'));
  62. }
  63. return true;
  64. }
  65. /**
  66. * Check the posted activity type and break out to appropriate processing.
  67. */
  68. protected function handle()
  69. {
  70. parent::handle();
  71. common_log(LOG_DEBUG, "Got a " . $this->activity->verb);
  72. try {
  73. if (Event::handle('StartHandleSalmonTarget', array($this->activity, $this->target)) &&
  74. Event::handle('StartHandleSalmon', array($this->activity))) {
  75. switch ($this->activity->verb) {
  76. case ActivityVerb::POST:
  77. $this->handlePost();
  78. break;
  79. case ActivityVerb::SHARE:
  80. $this->handleShare();
  81. break;
  82. case ActivityVerb::FOLLOW:
  83. case ActivityVerb::FRIEND:
  84. $this->handleFollow();
  85. break;
  86. case ActivityVerb::UNFOLLOW:
  87. $this->handleUnfollow();
  88. break;
  89. case ActivityVerb::JOIN:
  90. $this->handleJoin();
  91. break;
  92. case ActivityVerb::LEAVE:
  93. $this->handleLeave();
  94. break;
  95. case ActivityVerb::TAG:
  96. $this->handleTag();
  97. break;
  98. case ActivityVerb::UNTAG:
  99. $this->handleUntag();
  100. break;
  101. case ActivityVerb::UPDATE_PROFILE:
  102. $this->handleUpdateProfile();
  103. break;
  104. default:
  105. // TRANS: Client exception.
  106. throw new ClientException(_m('Unrecognized activity type.'));
  107. }
  108. Event::handle('EndHandleSalmon', array($this->activity));
  109. Event::handle('EndHandleSalmonTarget', array($this->activity, $this->target));
  110. }
  111. } catch (AlreadyFulfilledException $e) {
  112. // The action's results are already fulfilled. Maybe it was a
  113. // duplicate? Maybe someone's database is out of sync?
  114. // Let's just accept it and move on.
  115. common_log(LOG_INFO, 'Salmon slap carried an event which had already been fulfilled.');
  116. }
  117. }
  118. function handlePost()
  119. {
  120. // TRANS: Client exception.
  121. throw new ClientException(_m('This target does not understand posts.'));
  122. }
  123. function handleFollow()
  124. {
  125. // TRANS: Client exception.
  126. throw new ClientException(_m('This target does not understand follows.'));
  127. }
  128. function handleUnfollow()
  129. {
  130. // TRANS: Client exception.
  131. throw new ClientException(_m('This target does not understand unfollows.'));
  132. }
  133. function handleShare()
  134. {
  135. // TRANS: Client exception.
  136. throw new ClientException(_m('This target does not understand share events.'));
  137. }
  138. function handleJoin()
  139. {
  140. // TRANS: Client exception.
  141. throw new ClientException(_m('This target does not understand joins.'));
  142. }
  143. function handleLeave()
  144. {
  145. // TRANS: Client exception.
  146. throw new ClientException(_m('This target does not understand leave events.'));
  147. }
  148. function handleTag()
  149. {
  150. // TRANS: Client exception.
  151. throw new ClientException(_m('This target does not understand list events.'));
  152. }
  153. function handleUntag()
  154. {
  155. // TRANS: Client exception.
  156. throw new ClientException(_m('This target does not understand unlist events.'));
  157. }
  158. /**
  159. * Remote user sent us an update to their profile.
  160. * If we already know them, accept the updates.
  161. */
  162. function handleUpdateProfile()
  163. {
  164. $oprofile = Ostatus_profile::getActorProfile($this->activity);
  165. if ($oprofile instanceof Ostatus_profile) {
  166. common_log(LOG_INFO, "Got a profile-update ping from $oprofile->uri");
  167. $oprofile->updateFromActivityObject($this->activity->actor);
  168. } else {
  169. common_log(LOG_INFO, "Ignoring profile-update ping from unknown " . $this->activity->actor->id);
  170. }
  171. }
  172. function ensureProfiles()
  173. {
  174. try {
  175. $this->oprofile = Ostatus_profile::getActorProfile($this->activity);
  176. if (!$this->oprofile instanceof Ostatus_profile) {
  177. throw new UnknownUriException($this->activity->actor->id);
  178. }
  179. } catch (UnknownUriException $e) {
  180. // Apparently we didn't find the Profile object based on our URI,
  181. // so OStatus doesn't have it with this URI in ostatus_profile.
  182. // Try to look it up again, remote side may have changed from http to https
  183. // or maybe publish an acct: URI now instead of an http: URL.
  184. //
  185. // Steps:
  186. // 1. Check the newly received URI. Who does it say it is?
  187. // 2. Compare these alleged identities to our local database.
  188. // 3. If we found any locally stored identities, ask it about its aliases.
  189. // 4. Do any of the aliases from our known identity match the recently introduced one?
  190. //
  191. // Example: We have stored http://example.com/user/1 but this URI says https://example.com/user/1
  192. common_debug('No local Profile object found for a magicsigned activity author URI: '.$e->object_uri);
  193. $disco = new Discovery();
  194. $xrd = $disco->lookup($e->object_uri);
  195. // Step 1: We got a bunch of discovery data for https://example.com/user/1 which includes
  196. // aliases https://example.com/user and hopefully our original http://example.com/user/1 too
  197. $all_ids = array_merge(array($xrd->subject), $xrd->aliases);
  198. if (!in_array($e->object_uri, $all_ids)) {
  199. common_debug('The activity author URI we got was not listed itself when doing discovery on it.');
  200. throw $e;
  201. }
  202. // Go through each reported alias from lookup to see if we know this already
  203. foreach ($all_ids as $aliased_uri) {
  204. $oprofile = Ostatus_profile::getKV('uri', $aliased_uri);
  205. if (!$oprofile instanceof Ostatus_profile) {
  206. continue; // unknown locally, check the next alias
  207. }
  208. // Step 2: We found the alleged http://example.com/user/1 URI in our local database,
  209. // but this can't be trusted yet because anyone can publish any alias.
  210. common_debug('Found a local Ostatus_profile for "'.$e->object_uri.'" with this URI: '.$aliased_uri);
  211. // We found an existing OStatus profile, but is it really the same? Do a callback to the URI's origin
  212. // Step 3: lookup our previously known http://example.com/user/1 webfinger etc.
  213. $xrd = $disco->lookup($oprofile->getUri()); // getUri returns ->uri, which we filtered on earlier
  214. $doublecheck_aliases = array_merge(array($xrd->subject), $xrd->aliases);
  215. common_debug('Trying to match known "'.$aliased_uri.'" against its returned aliases: '.implode(' ', $doublecheck_aliases));
  216. // if we find our original URI here, it is a legitimate alias
  217. // Step 4: Is the newly introduced https://example.com/user/1 URI in the list of aliases
  218. // presented by http://example.com/user/1 (i.e. do they both say they are the same identity?)
  219. if (in_array($e->object_uri, $doublecheck_aliases)) {
  220. common_debug('These identities both say they are each other: "'.$aliased_uri.'" and "'.$e->object_uri);
  221. $this->oprofile = $oprofile;
  222. break; // don't iterate through aliases anymore
  223. }
  224. }
  225. // We might end up here after $all_ids is iterated through without a $this->oprofile value,
  226. if (!$this->oprofile instanceof Ostatus_profile) {
  227. common_debug("We do not have a local profile to connect to this activity's author. Let's create one.");
  228. // ensureActivityObjectProfile throws exception on failure
  229. $this->oprofile = Ostatus_profile::ensureActivityObjectProfile($this->activity->actor);
  230. }
  231. }
  232. assert($this->oprofile instanceof Ostatus_profile);
  233. $this->actor = $this->oprofile->localProfile();
  234. }
  235. function saveNotice()
  236. {
  237. if (!$this->oprofile instanceof Ostatus_profile) {
  238. common_debug('Ostatus_profile missing in ' . get_class(). ' profile: '.var_export($this->profile));
  239. }
  240. return $this->oprofile->processPost($this->activity, 'salmon');
  241. }
  242. }