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::FAVORITE:
95 $this->handleFavorite();
97 case ActivityVerb::UNFAVORITE:
98 $this->handleUnfavorite();
100 case ActivityVerb::FOLLOW:
101 case ActivityVerb::FRIEND:
102 $this->handleFollow();
104 case ActivityVerb::UNFOLLOW:
105 $this->handleUnfollow();
107 case ActivityVerb::JOIN:
110 case ActivityVerb::LEAVE:
111 $this->handleLeave();
113 case ActivityVerb::TAG:
116 case ActivityVerb::UNTAG:
117 $this->handleUntag();
119 case ActivityVerb::UPDATE_PROFILE:
120 $this->handleUpdateProfile();
123 // TRANS: Client exception.
124 throw new ClientException(_m('Unrecognized activity type.'));
126 Event::handle('EndHandleSalmon', array($this->activity));
127 Event::handle('EndHandleSalmonTarget', array($this->activity, $this->target));
129 } catch (AlreadyFulfilledException $e) {
130 // The action's results are already fulfilled. Maybe it was a
131 // duplicate? Maybe someone's database is out of sync?
132 // Let's just accept it and move on.
133 common_log(LOG_INFO, 'Salmon slap carried an event which had already been fulfilled.');
137 function handlePost()
139 // TRANS: Client exception.
140 throw new ClientException(_m('This target does not understand posts.'));
143 function handleFollow()
145 // TRANS: Client exception.
146 throw new ClientException(_m('This target does not understand follows.'));
149 function handleUnfollow()
151 // TRANS: Client exception.
152 throw new ClientException(_m('This target does not understand unfollows.'));
155 function handleFavorite()
157 // TRANS: Client exception.
158 throw new ClientException(_m('This target does not understand favorites.'));
161 function handleUnfavorite()
163 // TRANS: Client exception.
164 throw new ClientException(_m('This target does not understand unfavorites.'));
167 function handleShare()
169 // TRANS: Client exception.
170 throw new ClientException(_m('This target does not understand share events.'));
173 function handleJoin()
175 // TRANS: Client exception.
176 throw new ClientException(_m('This target does not understand joins.'));
179 function handleLeave()
181 // TRANS: Client exception.
182 throw new ClientException(_m('This target does not understand leave events.'));
187 // TRANS: Client exception.
188 throw new ClientException(_m('This target does not understand list events.'));
191 function handleUntag()
193 // TRANS: Client exception.
194 throw new ClientException(_m('This target does not understand unlist events.'));
198 * Remote user sent us an update to their profile.
199 * If we already know them, accept the updates.
201 function handleUpdateProfile()
203 $oprofile = Ostatus_profile::getActorProfile($this->activity);
204 if ($oprofile instanceof Ostatus_profile) {
205 common_log(LOG_INFO, "Got a profile-update ping from $oprofile->uri");
206 $oprofile->updateFromActivityObject($this->activity->actor);
208 common_log(LOG_INFO, "Ignoring profile-update ping from unknown " . $this->activity->actor->id);
213 * @return Ostatus_profile
215 function ensureProfile()
217 $actor = $this->activity->actor;
218 if (empty($actor->id)) {
219 common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
220 common_log(LOG_ERR, "activity with no actor: " . var_export($this->activity, true));
222 throw new Exception(_m('Received a salmon slap from unidentified actor.'));
225 // ensureActivityObjectProfile throws exception on failure
226 return Ostatus_profile::ensureActivityObjectProfile($actor);
229 function saveNotice()
231 $oprofile = $this->ensureProfile();
232 return $oprofile->processPost($this->activity, 'salmon');