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 $xml = file_get_contents('php://input');
51 // Check the signature
53 if (!$salmon->verifyMagicEnv($xml)) {
54 common_log(LOG_DEBUG, "Salmon signature verification failed.");
55 // TRANS: Client error.
56 $this->clientError(_m('Salmon signature verification failed.'));
58 $magic_env = new MagicEnvelope();
59 $env = $magic_env->parse($xml);
60 $xml = $magic_env->unfold($env);
63 $dom = DOMDocument::loadXML($xml);
64 if ($dom->documentElement->namespaceURI != Activity::ATOM ||
65 $dom->documentElement->localName != 'entry') {
66 common_log(LOG_DEBUG, "Got invalid Salmon post: $xml");
67 // TRANS: Client error.
68 $this->clientError(_m('Salmon post must be an Atom entry.'));
71 $this->activity = new Activity($dom->documentElement);
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);
84 if (Event::handle('StartHandleSalmonTarget', array($this->activity, $this->target)) &&
85 Event::handle('StartHandleSalmon', array($this->activity))) {
86 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));
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 handleFavorite()
151 // TRANS: Client exception.
152 throw new ClientException(_m('This target does not understand favorites.'));
155 function handleUnfavorite()
157 // TRANS: Client exception.
158 throw new ClientException(_m('This target does not understand unfavorites.'));
161 function handleShare()
163 // TRANS: Client exception.
164 throw new ClientException(_m('This target does not understand share events.'));
167 function handleJoin()
169 // TRANS: Client exception.
170 throw new ClientException(_m('This target does not understand joins.'));
173 function handleLeave()
175 // TRANS: Client exception.
176 throw new ClientException(_m('This target does not understand leave events.'));
181 // TRANS: Client exception.
182 throw new ClientException(_m('This target does not understand list events.'));
185 function handleUntag()
187 // TRANS: Client exception.
188 throw new ClientException(_m('This target does not understand unlist events.'));
192 * Remote user sent us an update to their profile.
193 * If we already know them, accept the updates.
195 function handleUpdateProfile()
197 $oprofile = Ostatus_profile::getActorProfile($this->activity);
199 common_log(LOG_INFO, "Got a profile-update ping from $oprofile->uri");
200 $oprofile->updateFromActivityObject($this->activity->actor);
202 common_log(LOG_INFO, "Ignoring profile-update ping from unknown " . $this->activity->actor->id);
207 * @return Ostatus_profile
209 function ensureProfile()
211 $actor = $this->activity->actor;
212 if (empty($actor->id)) {
213 common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
214 common_log(LOG_ERR, "activity with no actor: " . var_export($this->activity, true));
216 throw new Exception(_m('Received a salmon slap from unidentified actor.'));
219 return Ostatus_profile::ensureActivityObjectProfile($actor);
222 function saveNotice()
224 $oprofile = $this->ensureProfile();
225 return $oprofile->processPost($this->activity, 'salmon');