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