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 $profile = Profile::fromUri($this->activity->actor->id);
56 assert($profile instanceof Profile);
57 } catch (Exception $e) {
58 common_debug('Salmon envelope parsing failed with: '.$e->getMessage());
59 $this->clientError($e->getMessage());
62 // Cryptographic verification test
63 if (!$magic_env->verify($profile)) {
64 common_log(LOG_DEBUG, "Salmon signature verification failed.");
65 // TRANS: Client error.
66 $this->clientError(_m('Salmon signature verification failed.'));
69 $this->oprofile = $this->ensureProfile();
70 $this->actor = $this->oprofile->localProfile();
76 * Check the posted activity type and break out to appropriate processing.
79 protected function handle()
83 common_log(LOG_DEBUG, "Got a " . $this->activity->verb);
85 if (Event::handle('StartHandleSalmonTarget', array($this->activity, $this->target)) &&
86 Event::handle('StartHandleSalmon', array($this->activity))) {
87 switch ($this->activity->verb) {
88 case ActivityVerb::POST:
91 case ActivityVerb::SHARE:
94 case ActivityVerb::FOLLOW:
95 case ActivityVerb::FRIEND:
96 $this->handleFollow();
98 case ActivityVerb::UNFOLLOW:
99 $this->handleUnfollow();
101 case ActivityVerb::JOIN:
104 case ActivityVerb::LEAVE:
105 $this->handleLeave();
107 case ActivityVerb::TAG:
110 case ActivityVerb::UNTAG:
111 $this->handleUntag();
113 case ActivityVerb::UPDATE_PROFILE:
114 $this->handleUpdateProfile();
117 // TRANS: Client exception.
118 throw new ClientException(_m('Unrecognized activity type.'));
120 Event::handle('EndHandleSalmon', array($this->activity));
121 Event::handle('EndHandleSalmonTarget', array($this->activity, $this->target));
123 } catch (AlreadyFulfilledException $e) {
124 // The action's results are already fulfilled. Maybe it was a
125 // duplicate? Maybe someone's database is out of sync?
126 // Let's just accept it and move on.
127 common_log(LOG_INFO, 'Salmon slap carried an event which had already been fulfilled.');
131 function handlePost()
133 // TRANS: Client exception.
134 throw new ClientException(_m('This target does not understand posts.'));
137 function handleFollow()
139 // TRANS: Client exception.
140 throw new ClientException(_m('This target does not understand follows.'));
143 function handleUnfollow()
145 // TRANS: Client exception.
146 throw new ClientException(_m('This target does not understand unfollows.'));
149 function handleShare()
151 // TRANS: Client exception.
152 throw new ClientException(_m('This target does not understand share events.'));
155 function handleJoin()
157 // TRANS: Client exception.
158 throw new ClientException(_m('This target does not understand joins.'));
161 function handleLeave()
163 // TRANS: Client exception.
164 throw new ClientException(_m('This target does not understand leave events.'));
169 // TRANS: Client exception.
170 throw new ClientException(_m('This target does not understand list events.'));
173 function handleUntag()
175 // TRANS: Client exception.
176 throw new ClientException(_m('This target does not understand unlist events.'));
180 * Remote user sent us an update to their profile.
181 * If we already know them, accept the updates.
183 function handleUpdateProfile()
185 $oprofile = Ostatus_profile::getActorProfile($this->activity);
186 if ($oprofile instanceof Ostatus_profile) {
187 common_log(LOG_INFO, "Got a profile-update ping from $oprofile->uri");
188 $oprofile->updateFromActivityObject($this->activity->actor);
190 common_log(LOG_INFO, "Ignoring profile-update ping from unknown " . $this->activity->actor->id);
195 * @return Ostatus_profile
197 function ensureProfile()
199 $actor = $this->activity->actor;
200 if (empty($actor->id)) {
201 common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
202 common_log(LOG_ERR, "activity with no actor: " . var_export($this->activity, true));
204 throw new Exception(_m('Received a salmon slap from unidentified actor.'));
207 // ensureActivityObjectProfile throws exception on failure
208 return Ostatus_profile::ensureActivityObjectProfile($actor);
211 function saveNotice()
213 $oprofile = $this->ensureProfile();
214 return $oprofile->processPost($this->activity, 'salmon');