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/atom+xml') {
45 $this->clientError(_m('Salmon requires application/atom+xml'));
48 $xml = file_get_contents('php://input');
50 $dom = DOMDocument::loadXML($xml);
52 if ($dom->documentElement->namespaceURI != Activity::ATOM ||
53 $dom->documentElement->localName != 'entry') {
54 common_log(LOG_DEBUG, "Got invalid Salmon post: $xml");
55 $this->clientError(_m('Salmon post must be an Atom entry.'));
58 // Check the signature
60 if (!common_config('ostatus', 'skip_signatures')) {
61 if (!$salmon->verifyMagicEnv($dom)) {
62 common_log(LOG_DEBUG, "Salmon signature verification failed.");
63 $this->clientError(_m('Salmon signature verification failed.'));
67 $this->act = new Activity($dom->documentElement);
72 * Check the posted activity type and break out to appropriate processing.
75 function handle($args)
77 StatusNet::setApi(true); // Send smaller error pages
79 common_log(LOG_DEBUG, "Got a " . $this->act->verb);
80 if (Event::handle('StartHandleSalmon', array($this->activity))) {
81 switch ($this->act->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::UPDATE_PROFILE:
109 $this->handleUpdateProfile();
112 throw new ClientException(_m("Unrecognized activity type."));
114 Event::handle('EndHandleSalmon', array($this->activity));
118 function handlePost()
120 throw new ClientException(_m("This target doesn't understand posts."));
123 function handleFollow()
125 throw new ClientException(_m("This target doesn't understand follows."));
128 function handleUnfollow()
130 throw new ClientException(_m("This target doesn't understand unfollows."));
133 function handleFavorite()
135 throw new ClientException(_m("This target doesn't understand favorites."));
138 function handleUnfavorite()
140 throw new ClientException(_m("This target doesn't understand unfavorites."));
143 function handleShare()
145 throw new ClientException(_m("This target doesn't understand share events."));
148 function handleJoin()
150 throw new ClientException(_m("This target doesn't understand joins."));
153 function handleLeave()
155 throw new ClientException(_m("This target doesn't understand leave events."));
159 * Remote user sent us an update to their profile.
160 * If we already know them, accept the updates.
162 function handleUpdateProfile()
164 $oprofile = Ostatus_profile::getActorProfile($this->act);
166 common_log(LOG_INFO, "Got a profile-update ping from $oprofile->uri");
167 $oprofile->updateFromActivityObject($this->act->actor);
169 common_log(LOG_INFO, "Ignoring profile-update ping from unknown " . $this->act->actor->id);
174 * @return Ostatus_profile
176 function ensureProfile()
178 $actor = $this->act->actor;
179 if (empty($actor->id)) {
180 common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
181 common_log(LOG_ERR, "activity with no actor: " . var_export($this->act, true));
182 throw new Exception("Received a salmon slap from unidentified actor.");
185 return Ostatus_profile::ensureActivityObjectProfile($actor);
188 function saveNotice()
190 $oprofile = $this->ensureProfile();
191 return $oprofile->processPost($this->act, 'salmon');