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 $this->clientError(_m('This method requires a POST.'));
44 if (empty($_SERVER['CONTENT_TYPE']) || $_SERVER['CONTENT_TYPE'] != 'application/magic-envelope+xml') {
45 $this->clientError(_m('Salmon requires application/magic-envelope+xml'));
48 $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 $this->clientError(_m('Salmon signature verification failed.'));
57 $magic_env = new MagicEnvelope();
58 $env = $magic_env->parse($xml);
59 $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 $this->clientError(_m('Salmon post must be an Atom entry.'));
70 $this->act = new Activity($dom->documentElement);
75 * Check the posted activity type and break out to appropriate processing.
78 function handle($args)
80 StatusNet::setApi(true); // Send smaller error pages
82 common_log(LOG_DEBUG, "Got a " . $this->act->verb);
83 if (Event::handle('StartHandleSalmon', array($this->activity))) {
84 switch ($this->act->verb)
86 case ActivityVerb::POST:
89 case ActivityVerb::SHARE:
92 case ActivityVerb::FAVORITE:
93 $this->handleFavorite();
95 case ActivityVerb::UNFAVORITE:
96 $this->handleUnfavorite();
98 case ActivityVerb::FOLLOW:
99 case ActivityVerb::FRIEND:
100 $this->handleFollow();
102 case ActivityVerb::UNFOLLOW:
103 $this->handleUnfollow();
105 case ActivityVerb::JOIN:
108 case ActivityVerb::LEAVE:
109 $this->handleLeave();
111 case ActivityVerb::UPDATE_PROFILE:
112 $this->handleUpdateProfile();
115 throw new ClientException(_m("Unrecognized activity type."));
117 Event::handle('EndHandleSalmon', array($this->activity));
121 function handlePost()
123 throw new ClientException(_m("This target doesn't understand posts."));
126 function handleFollow()
128 throw new ClientException(_m("This target doesn't understand follows."));
131 function handleUnfollow()
133 throw new ClientException(_m("This target doesn't understand unfollows."));
136 function handleFavorite()
138 throw new ClientException(_m("This target doesn't understand favorites."));
141 function handleUnfavorite()
143 throw new ClientException(_m("This target doesn't understand unfavorites."));
146 function handleShare()
148 throw new ClientException(_m("This target doesn't understand share events."));
151 function handleJoin()
153 throw new ClientException(_m("This target doesn't understand joins."));
156 function handleLeave()
158 throw new ClientException(_m("This target doesn't understand leave events."));
162 * Remote user sent us an update to their profile.
163 * If we already know them, accept the updates.
165 function handleUpdateProfile()
167 $oprofile = Ostatus_profile::getActorProfile($this->act);
169 common_log(LOG_INFO, "Got a profile-update ping from $oprofile->uri");
170 $oprofile->updateFromActivityObject($this->act->actor);
172 common_log(LOG_INFO, "Ignoring profile-update ping from unknown " . $this->act->actor->id);
177 * @return Ostatus_profile
179 function ensureProfile()
181 $actor = $this->act->actor;
182 if (empty($actor->id)) {
183 common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
184 common_log(LOG_ERR, "activity with no actor: " . var_export($this->act, true));
185 throw new Exception("Received a salmon slap from unidentified actor.");
188 return Ostatus_profile::ensureActivityObjectProfile($actor);
191 function saveNotice()
193 $oprofile = $this->ensureProfile();
194 return $oprofile->processPost($this->act, 'salmon');