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 $env = MagicEnvelope::parse($xml);
58 $xml = MagicEnvelope::unfold($env);
62 $dom = DOMDocument::loadXML($xml);
63 if ($dom->documentElement->namespaceURI != Activity::ATOM ||
64 $dom->documentElement->localName != 'entry') {
65 common_log(LOG_DEBUG, "Got invalid Salmon post: $xml");
66 $this->clientError(_m('Salmon post must be an Atom entry.'));
69 $this->act = new Activity($dom->documentElement);
74 * Check the posted activity type and break out to appropriate processing.
77 function handle($args)
79 StatusNet::setApi(true); // Send smaller error pages
81 common_log(LOG_DEBUG, "Got a " . $this->act->verb);
82 if (Event::handle('StartHandleSalmon', array($this->activity))) {
83 switch ($this->act->verb)
85 case ActivityVerb::POST:
88 case ActivityVerb::SHARE:
91 case ActivityVerb::FAVORITE:
92 $this->handleFavorite();
94 case ActivityVerb::UNFAVORITE:
95 $this->handleUnfavorite();
97 case ActivityVerb::FOLLOW:
98 case ActivityVerb::FRIEND:
99 $this->handleFollow();
101 case ActivityVerb::UNFOLLOW:
102 $this->handleUnfollow();
104 case ActivityVerb::JOIN:
107 case ActivityVerb::LEAVE:
108 $this->handleLeave();
110 case ActivityVerb::UPDATE_PROFILE:
111 $this->handleUpdateProfile();
114 throw new ClientException(_m("Unrecognized activity type."));
116 Event::handle('EndHandleSalmon', array($this->activity));
120 function handlePost()
122 throw new ClientException(_m("This target doesn't understand posts."));
125 function handleFollow()
127 throw new ClientException(_m("This target doesn't understand follows."));
130 function handleUnfollow()
132 throw new ClientException(_m("This target doesn't understand unfollows."));
135 function handleFavorite()
137 throw new ClientException(_m("This target doesn't understand favorites."));
140 function handleUnfavorite()
142 throw new ClientException(_m("This target doesn't understand unfavorites."));
145 function handleShare()
147 throw new ClientException(_m("This target doesn't understand share events."));
150 function handleJoin()
152 throw new ClientException(_m("This target doesn't understand joins."));
155 function handleLeave()
157 throw new ClientException(_m("This target doesn't understand leave events."));
161 * Remote user sent us an update to their profile.
162 * If we already know them, accept the updates.
164 function handleUpdateProfile()
166 $oprofile = Ostatus_profile::getActorProfile($this->act);
168 common_log(LOG_INFO, "Got a profile-update ping from $oprofile->uri");
169 $oprofile->updateFromActivityObject($this->act->actor);
171 common_log(LOG_INFO, "Ignoring profile-update ping from unknown " . $this->act->actor->id);
176 * @return Ostatus_profile
178 function ensureProfile()
180 $actor = $this->act->actor;
181 if (empty($actor->id)) {
182 common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
183 common_log(LOG_ERR, "activity with no actor: " . var_export($this->act, true));
184 throw new Exception("Received a salmon slap from unidentified actor.");
187 return Ostatus_profile::ensureActivityObjectProfile($actor);
190 function saveNotice()
192 $oprofile = $this->ensureProfile();
193 return $oprofile->processPost($this->act, 'salmon');