3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
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.
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.
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/>.
21 * @package OStatusPlugin
22 * @author James Walker <james@status.net>
25 if (!defined('GNUSOCIAL')) { exit(1); }
27 class SalmonAction extends Action
29 protected $needPost = true;
31 protected $oprofile = null; // Ostatus_profile of the actor
32 protected $actor = null; // Profile object of the actor
38 protected function prepare(array $args=array())
40 GNUsocial::setApi(true); // Send smaller error pages
42 parent::prepare($args);
44 if (!isset($_SERVER['CONTENT_TYPE'])) {
45 // TRANS: Client error. Do not translate "Content-type"
46 $this->clientError(_m('Salmon requires a Content-type header.'));
49 switch ($_SERVER['CONTENT_TYPE']) {
50 case 'application/magic-envelope+xml':
51 $envxml = file_get_contents('php://input');
53 case 'application/x-www-form-urlencoded':
54 $envxml = Magicsig::base64_url_decode($this->trimmed('xml'));
57 // TRANS: Client error. Do not translate the quoted "application/[type]" strings.
58 throw new ClientException(_m('Salmon requires "application/magic-envelope+xml". For Diaspora we also accept "application/x-www-form-urlencoded" with an "xml" parameter.', 415));
62 throw new ClientException('No magic envelope supplied in POST.');
65 $magic_env = new MagicEnvelope($envxml); // parse incoming XML as a MagicEnvelope
67 $entry = $magic_env->getPayload(); // Not cryptographically verified yet!
68 $this->activity = new Activity($entry->documentElement);
69 if (empty($this->activity->actor->id)) {
70 common_log(LOG_ERR, "broken actor: " . var_export($this->activity->actor->id, true));
71 common_log(LOG_ERR, "activity with no actor: " . var_export($this->activity, true));
73 throw new ClientException(_m('Activity in salmon slap has no actor id.'));
75 // ensureProfiles sets $this->actor and $this->oprofile
76 $this->ensureProfiles();
77 } catch (Exception $e) {
78 common_debug('Salmon envelope parsing failed with: '.$e->getMessage());
79 // convert exception to ClientException
80 throw new ClientException($e->getMessage());
83 // Cryptographic verification test, throws exception on failure
84 $magic_env->verify($this->actor);
90 * Check the posted activity type and break out to appropriate processing.
93 protected function handle()
97 assert($this->activity instanceof Activity);
98 assert($this->target instanceof Profile);
100 common_log(LOG_DEBUG, "Got a " . $this->activity->verb);
103 $options = [ 'source' => 'ostatus' ];
104 common_debug('Save salmon slap directly with Notice::saveActivity for actor=='.$this->actor->getID());
105 $stored = Notice::saveActivity($this->activity, $this->actor, $options);
106 common_debug('Save salmon slap finished, notice id=='.$stored->getID());
108 } catch (AlreadyFulfilledException $e) {
109 // The action's results are already fulfilled. Maybe it was a
110 // duplicate? Maybe someone's database is out of sync?
111 // Let's just accept it and move on.
112 common_log(LOG_INFO, 'Salmon slap carried an event which had already been fulfilled.');
114 } catch (NoticeSaveException $e) {
115 common_debug('Notice::saveActivity did not save our '._ve($this->activity->verb).' activity, trying old-fashioned salmon saving.');
119 if (Event::handle('StartHandleSalmonTarget', array($this->activity, $this->target)) &&
120 Event::handle('StartHandleSalmon', array($this->activity))) {
121 switch ($this->activity->verb) {
122 case ActivityVerb::POST:
125 case ActivityVerb::SHARE:
126 $this->handleShare();
128 case ActivityVerb::FOLLOW:
129 case ActivityVerb::FRIEND:
130 $this->handleFollow();
132 case ActivityVerb::UNFOLLOW:
133 $this->handleUnfollow();
135 case ActivityVerb::JOIN:
138 case ActivityVerb::LEAVE:
139 $this->handleLeave();
141 case ActivityVerb::TAG:
144 case ActivityVerb::UNTAG:
145 $this->handleUntag();
147 case ActivityVerb::UPDATE_PROFILE:
148 $this->handleUpdateProfile();
151 // TRANS: Client exception.
152 throw new ClientException(_m('Unrecognized activity type.'));
154 Event::handle('EndHandleSalmon', array($this->activity));
155 Event::handle('EndHandleSalmonTarget', array($this->activity, $this->target));
157 } catch (AlreadyFulfilledException $e) {
158 // The action's results are already fulfilled. Maybe it was a
159 // duplicate? Maybe someone's database is out of sync?
160 // Let's just accept it and move on.
161 common_log(LOG_INFO, 'Salmon slap carried an event which had already been fulfilled.');
165 function handlePost()
167 // TRANS: Client exception.
168 throw new ClientException(_m('This target does not understand posts.'));
171 function handleFollow()
173 // TRANS: Client exception.
174 throw new ClientException(_m('This target does not understand follows.'));
177 function handleUnfollow()
179 // TRANS: Client exception.
180 throw new ClientException(_m('This target does not understand unfollows.'));
183 function handleShare()
185 // TRANS: Client exception.
186 throw new ClientException(_m('This target does not understand share events.'));
189 function handleJoin()
191 // TRANS: Client exception.
192 throw new ClientException(_m('This target does not understand joins.'));
195 function handleLeave()
197 // TRANS: Client exception.
198 throw new ClientException(_m('This target does not understand leave events.'));
203 // TRANS: Client exception.
204 throw new ClientException(_m('This target does not understand list events.'));
207 function handleUntag()
209 // TRANS: Client exception.
210 throw new ClientException(_m('This target does not understand unlist events.'));
214 * Remote user sent us an update to their profile.
215 * If we already know them, accept the updates.
217 function handleUpdateProfile()
219 $oprofile = Ostatus_profile::getActorProfile($this->activity);
220 if ($oprofile instanceof Ostatus_profile) {
221 common_log(LOG_INFO, "Got a profile-update ping from $oprofile->uri");
222 $oprofile->updateFromActivityObject($this->activity->actor);
224 common_log(LOG_INFO, "Ignoring profile-update ping from unknown " . $this->activity->actor->id);
228 function ensureProfiles()
231 $this->oprofile = Ostatus_profile::getActorProfile($this->activity);
232 if (!$this->oprofile instanceof Ostatus_profile) {
233 throw new UnknownUriException($this->activity->actor->id);
235 } catch (UnknownUriException $e) {
236 // Apparently we didn't find the Profile object based on our URI,
237 // so OStatus doesn't have it with this URI in ostatus_profile.
238 // Try to look it up again, remote side may have changed from http to https
239 // or maybe publish an acct: URI now instead of an http: URL.
242 // 1. Check the newly received URI. Who does it say it is?
243 // 2. Compare these alleged identities to our local database.
244 // 3. If we found any locally stored identities, ask it about its aliases.
245 // 4. Do any of the aliases from our known identity match the recently introduced one?
247 // Example: We have stored http://example.com/user/1 but this URI says https://example.com/user/1
248 common_debug('No local Profile object found for a magicsigned activity author URI: '.$e->object_uri);
249 $disco = new Discovery();
250 $xrd = $disco->lookup($e->object_uri);
251 // Step 1: We got a bunch of discovery data for https://example.com/user/1 which includes
252 // aliases https://example.com/user and hopefully our original http://example.com/user/1 too
253 $all_ids = array_merge(array($xrd->subject), $xrd->aliases);
255 if (!in_array($e->object_uri, $all_ids)) {
256 common_debug('The activity author URI we got was not listed itself when doing discovery on it.');
260 // Go through each reported alias from lookup to see if we know this already
261 foreach ($all_ids as $aliased_uri) {
262 $oprofile = Ostatus_profile::getKV('uri', $aliased_uri);
263 if (!$oprofile instanceof Ostatus_profile) {
264 continue; // unknown locally, check the next alias
266 // Step 2: We found the alleged http://example.com/user/1 URI in our local database,
267 // but this can't be trusted yet because anyone can publish any alias.
268 common_debug('Found a local Ostatus_profile for "'.$e->object_uri.'" with this URI: '.$aliased_uri);
270 // We found an existing OStatus profile, but is it really the same? Do a callback to the URI's origin
271 // Step 3: lookup our previously known http://example.com/user/1 webfinger etc.
272 $xrd = $disco->lookup($oprofile->getUri()); // getUri returns ->uri, which we filtered on earlier
273 $doublecheck_aliases = array_merge(array($xrd->subject), $xrd->aliases);
274 common_debug('Trying to match known "'.$aliased_uri.'" against its returned aliases: '.implode(' ', $doublecheck_aliases));
275 // if we find our original URI here, it is a legitimate alias
276 // Step 4: Is the newly introduced https://example.com/user/1 URI in the list of aliases
277 // presented by http://example.com/user/1 (i.e. do they both say they are the same identity?)
278 if (in_array($e->object_uri, $doublecheck_aliases)) {
279 $oprofile->updateUriKeys($e->object_uri, DiscoveryHints::fromXRD($xrd));
280 $this->oprofile = $oprofile;
281 break; // don't iterate through aliases anymore
285 // We might end up here after $all_ids is iterated through without a $this->oprofile value,
286 if (!$this->oprofile instanceof Ostatus_profile) {
287 common_debug("We do not have a local profile to connect to this activity's author. Let's create one.");
288 // ensureActivityObjectProfile throws exception on failure
289 $this->oprofile = Ostatus_profile::ensureActivityObjectProfile($this->activity->actor);
293 assert($this->oprofile instanceof Ostatus_profile);
295 $this->actor = $this->oprofile->localProfile();
298 function saveNotice()
300 if (!$this->oprofile instanceof Ostatus_profile) {
301 common_debug('Ostatus_profile missing in ' . get_class(). ' profile: '.var_export($this->profile, true));
303 return $this->oprofile->processPost($this->activity, 'salmon');