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('STATUSNET')) {
29 class SalmonAction extends Action
34 function prepare($args)
36 StatusNet::setApi(true); // Send smaller error pages
38 parent::prepare($args);
40 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
41 // TRANS: Client error. POST is a HTTP command. It should not be translated.
42 $this->clientError(_m('This method requires a POST.'));
45 if (empty($_SERVER['CONTENT_TYPE']) || $_SERVER['CONTENT_TYPE'] != 'application/magic-envelope+xml') {
46 // TRANS: Client error. Do not translate "application/magic-envelope+xml"
47 $this->clientError(_m('Salmon requires "application/magic-envelope+xml".'));
50 $xml = file_get_contents('php://input');
52 // Check the signature
54 if (!$salmon->verifyMagicEnv($xml)) {
55 common_log(LOG_DEBUG, "Salmon signature verification failed.");
56 // TRANS: Client error.
57 $this->clientError(_m('Salmon signature verification failed.'));
59 $magic_env = new MagicEnvelope();
60 $env = $magic_env->parse($xml);
61 $xml = $magic_env->unfold($env);
64 $dom = DOMDocument::loadXML($xml);
65 if ($dom->documentElement->namespaceURI != Activity::ATOM ||
66 $dom->documentElement->localName != 'entry') {
67 common_log(LOG_DEBUG, "Got invalid Salmon post: $xml");
68 // TRANS: Client error.
69 $this->clientError(_m('Salmon post must be an Atom entry.'));
72 $this->activity = new Activity($dom->documentElement);
77 * Check the posted activity type and break out to appropriate processing.
80 function handle($args)
82 StatusNet::setApi(true); // Send smaller error pages
84 common_log(LOG_DEBUG, "Got a " . $this->activity->verb);
85 if (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::UPDATE_PROFILE:
114 $this->handleUpdateProfile();
117 // TRANS: Client exception.
118 throw new ClientException(_m("Unrecognized activity type."));
120 Event::handle('EndHandleSalmon', array($this->activity));
124 function handlePost()
126 // TRANS: Client exception.
127 throw new ClientException(_m("This target doesn't understand posts."));
130 function handleFollow()
132 // TRANS: Client exception.
133 throw new ClientException(_m("This target doesn't understand follows."));
136 function handleUnfollow()
138 // TRANS: Client exception.
139 throw new ClientException(_m("This target doesn't understand unfollows."));
142 function handleFavorite()
144 // TRANS: Client exception.
145 throw new ClientException(_m("This target doesn't understand favorites."));
148 function handleUnfavorite()
150 // TRANS: Client exception.
151 throw new ClientException(_m("This target doesn't understand unfavorites."));
154 function handleShare()
156 // TRANS: Client exception.
157 throw new ClientException(_m("This target doesn't understand share events."));
160 function handleJoin()
162 // TRANS: Client exception.
163 throw new ClientException(_m("This target doesn't understand joins."));
166 function handleLeave()
168 // TRANS: Client exception.
169 throw new ClientException(_m("This target doesn't understand leave events."));
173 * Remote user sent us an update to their profile.
174 * If we already know them, accept the updates.
176 function handleUpdateProfile()
178 $oprofile = Ostatus_profile::getActorProfile($this->activity);
180 common_log(LOG_INFO, "Got a profile-update ping from $oprofile->uri");
181 $oprofile->updateFromActivityObject($this->activity->actor);
183 common_log(LOG_INFO, "Ignoring profile-update ping from unknown " . $this->activity->actor->id);
188 * @return Ostatus_profile
190 function ensureProfile()
192 $actor = $this->activity->actor;
193 if (empty($actor->id)) {
194 common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
195 common_log(LOG_ERR, "activity with no actor: " . var_export($this->activity, true));
197 throw new Exception(_m('Received a salmon slap from unidentified actor.'));
200 return Ostatus_profile::ensureActivityObjectProfile($actor);
203 function saveNotice()
205 $oprofile = $this->ensureProfile();
206 return $oprofile->processPost($this->activity, 'salmon');