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
33 protected function prepare(array $args=array())
35 StatusNet::setApi(true); // Send smaller error pages
37 parent::prepare($args);
39 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
40 // TRANS: Client error. POST is a HTTP command. It should not be translated.
41 $this->clientError(_m('This method requires a POST.'));
44 if (empty($_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".'));
49 $envxml = file_get_contents('php://input');
50 $magic_env = new MagicEnvelope($envxml); // parse incoming XML as a MagicEnvelope
51 if (!$magic_env->verify()) {
52 common_log(LOG_DEBUG, "Salmon signature verification failed.");
53 // TRANS: Client error.
54 $this->clientError(_m('Salmon signature verification failed.'));
57 $xml = $magic_env->unfold(); // return the enveloped XML (the actual data)
58 $dom = DOMDocument::loadXML($xml);
59 if ($dom->documentElement->namespaceURI != Activity::ATOM ||
60 $dom->documentElement->localName != 'entry') {
61 common_log(LOG_DEBUG, "Got invalid Salmon post: $xml");
62 // TRANS: Client error.
63 $this->clientError(_m('Salmon post must be an Atom entry.'));
66 $this->activity = new Activity($dom->documentElement);
71 * Check the posted activity type and break out to appropriate processing.
74 protected function handle()
78 common_log(LOG_DEBUG, "Got a " . $this->activity->verb);
79 if (Event::handle('StartHandleSalmonTarget', array($this->activity, $this->target)) &&
80 Event::handle('StartHandleSalmon', array($this->activity))) {
81 switch ($this->activity->verb)
83 case ActivityVerb::POST:
86 case ActivityVerb::SHARE:
89 case ActivityVerb::FAVORITE:
90 $this->handleFavorite();
92 case ActivityVerb::UNFAVORITE:
93 $this->handleUnfavorite();
95 case ActivityVerb::FOLLOW:
96 case ActivityVerb::FRIEND:
97 $this->handleFollow();
99 case ActivityVerb::UNFOLLOW:
100 $this->handleUnfollow();
102 case ActivityVerb::JOIN:
105 case ActivityVerb::LEAVE:
106 $this->handleLeave();
108 case ActivityVerb::TAG:
111 case ActivityVerb::UNTAG:
112 $this->handleUntag();
114 case ActivityVerb::UPDATE_PROFILE:
115 $this->handleUpdateProfile();
118 // TRANS: Client exception.
119 throw new ClientException(_m('Unrecognized activity type.'));
121 Event::handle('EndHandleSalmon', array($this->activity));
122 Event::handle('EndHandleSalmonTarget', array($this->activity, $this->target));
126 function handlePost()
128 // TRANS: Client exception.
129 throw new ClientException(_m('This target does not understand posts.'));
132 function handleFollow()
134 // TRANS: Client exception.
135 throw new ClientException(_m('This target does not understand follows.'));
138 function handleUnfollow()
140 // TRANS: Client exception.
141 throw new ClientException(_m('This target does not understand unfollows.'));
144 function handleFavorite()
146 // TRANS: Client exception.
147 throw new ClientException(_m('This target does not understand favorites.'));
150 function handleUnfavorite()
152 // TRANS: Client exception.
153 throw new ClientException(_m('This target does not understand unfavorites.'));
156 function handleShare()
158 // TRANS: Client exception.
159 throw new ClientException(_m('This target does not understand share events.'));
162 function handleJoin()
164 // TRANS: Client exception.
165 throw new ClientException(_m('This target does not understand joins.'));
168 function handleLeave()
170 // TRANS: Client exception.
171 throw new ClientException(_m('This target does not understand leave events.'));
176 // TRANS: Client exception.
177 throw new ClientException(_m('This target does not understand list events.'));
180 function handleUntag()
182 // TRANS: Client exception.
183 throw new ClientException(_m('This target does not understand unlist events.'));
187 * Remote user sent us an update to their profile.
188 * If we already know them, accept the updates.
190 function handleUpdateProfile()
192 $oprofile = Ostatus_profile::getActorProfile($this->activity);
193 if ($oprofile instanceof Ostatus_profile) {
194 common_log(LOG_INFO, "Got a profile-update ping from $oprofile->uri");
195 $oprofile->updateFromActivityObject($this->activity->actor);
197 common_log(LOG_INFO, "Ignoring profile-update ping from unknown " . $this->activity->actor->id);
202 * @return Ostatus_profile
204 function ensureProfile()
206 $actor = $this->activity->actor;
207 if (empty($actor->id)) {
208 common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
209 common_log(LOG_ERR, "activity with no actor: " . var_export($this->activity, true));
211 throw new Exception(_m('Received a salmon slap from unidentified actor.'));
214 return Ostatus_profile::ensureActivityObjectProfile($actor);
217 function saveNotice()
219 $oprofile = $this->ensureProfile();
220 return $oprofile->processPost($this->activity, 'salmon');