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');
50 // Check the signature
52 if (!$salmon->verifyMagicEnv($xml)) {
53 common_log(LOG_DEBUG, "Salmon signature verification failed.");
54 $this->clientError(_m('Salmon signature verification failed.'));
56 $magic_env = new MagicEnvelope();
57 $env = $magic_env->parse($xml);
58 $xml = $magic_env->unfold($env);
61 $dom = DOMDocument::loadXML($xml);
62 if ($dom->documentElement->namespaceURI != Activity::ATOM ||
63 $dom->documentElement->localName != 'entry') {
64 common_log(LOG_DEBUG, "Got invalid Salmon post: $xml");
65 $this->clientError(_m('Salmon post must be an Atom entry.'));
68 $this->activity = new Activity($dom->documentElement);
73 * Check the posted activity type and break out to appropriate processing.
76 function handle($args)
78 StatusNet::setApi(true); // Send smaller error pages
80 common_log(LOG_DEBUG, "Got a " . $this->activity->verb);
81 if (Event::handle('StartHandleSalmon', array($this->activity))) {
82 switch ($this->activity->verb)
84 case ActivityVerb::POST:
87 case ActivityVerb::SHARE:
90 case ActivityVerb::FAVORITE:
91 $this->handleFavorite();
93 case ActivityVerb::UNFAVORITE:
94 $this->handleUnfavorite();
96 case ActivityVerb::FOLLOW:
97 case ActivityVerb::FRIEND:
98 $this->handleFollow();
100 case ActivityVerb::UNFOLLOW:
101 $this->handleUnfollow();
103 case ActivityVerb::JOIN:
106 case ActivityVerb::LEAVE:
107 $this->handleLeave();
109 case ActivityVerb::UPDATE_PROFILE:
110 $this->handleUpdateProfile();
113 throw new ClientException(_m("Unrecognized activity type."));
115 Event::handle('EndHandleSalmon', array($this->activity));
119 function handlePost()
121 throw new ClientException(_m("This target doesn't understand posts."));
124 function handleFollow()
126 throw new ClientException(_m("This target doesn't understand follows."));
129 function handleUnfollow()
131 throw new ClientException(_m("This target doesn't understand unfollows."));
134 function handleFavorite()
136 throw new ClientException(_m("This target doesn't understand favorites."));
139 function handleUnfavorite()
141 throw new ClientException(_m("This target doesn't understand unfavorites."));
144 function handleShare()
146 throw new ClientException(_m("This target doesn't understand share events."));
149 function handleJoin()
151 throw new ClientException(_m("This target doesn't understand joins."));
154 function handleLeave()
156 throw new ClientException(_m("This target doesn't understand leave events."));
160 * Remote user sent us an update to their profile.
161 * If we already know them, accept the updates.
163 function handleUpdateProfile()
165 $oprofile = Ostatus_profile::getActorProfile($this->activity);
167 common_log(LOG_INFO, "Got a profile-update ping from $oprofile->uri");
168 $oprofile->updateFromActivityObject($this->activity->actor);
170 common_log(LOG_INFO, "Ignoring profile-update ping from unknown " . $this->activity->actor->id);
175 * @return Ostatus_profile
177 function ensureProfile()
179 $actor = $this->activity->actor;
180 if (empty($actor->id)) {
181 common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
182 common_log(LOG_ERR, "activity with no actor: " . var_export($this->activity, true));
183 throw new Exception("Received a salmon slap from unidentified actor.");
186 return Ostatus_profile::ensureActivityObjectProfile($actor);
189 function saveNotice()
191 $oprofile = $this->ensureProfile();
192 return $oprofile->processPost($this->activity, 'salmon');