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(_('This method requires a POST.'));
44 if ($_SERVER['CONTENT_TYPE'] != 'application/atom+xml') {
45 $this->clientError(_('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 (!$salmon->verifyMagicEnv($dom)) {
61 common_log(LOG_DEBUG, "Salmon signature verification failed.");
62 $this->clientError(_m('Salmon signature verification failed.'));
65 $this->act = new Activity($dom->documentElement);
70 * Check the posted activity type and break out to appropriate processing.
73 function handle($args)
75 StatusNet::setApi(true); // Send smaller error pages
77 // TODO : Insert new $xml -> notice code
79 if (Event::handle('StartHandleSalmon', array($this->activity))) {
80 switch ($this->act->verb)
82 case ActivityVerb::POST:
85 case ActivityVerb::SHARE:
88 case ActivityVerb::FAVORITE:
89 $this->handleFavorite();
91 case ActivityVerb::UNFAVORITE:
92 $this->handleUnfavorite();
94 case ActivityVerb::FOLLOW:
95 case ActivityVerb::FRIEND:
96 $this->handleFollow();
98 case ActivityVerb::UNFOLLOW:
99 $this->handleUnfollow();
101 case ActivityVerb::JOIN:
105 throw new ClientException(_("Unimplemented."));
107 Event::handle('EndHandleSalmon', array($this->activity));
111 function handlePost()
113 throw new ClientException(_("Unimplemented!"));
116 function handleFollow()
118 throw new ClientException(_("Unimplemented!"));
121 function handleUnfollow()
123 throw new ClientException(_("Unimplemented!"));
126 function handleFavorite()
128 throw new ClientException(_("Unimplemented!"));
132 * Remote user doesn't like one of our posts after all!
133 * Confirm the post is ours, and delete a local favorite event.
136 function handleUnfavorite()
138 throw new ClientException(_("Unimplemented!"));
144 function handleShare()
146 throw new ClientException(_("Unimplemented!"));
152 function handleJoin()
154 throw new ClientException(_("Unimplemented!"));
158 * @return Ostatus_profile
160 function ensureProfile()
162 $actor = $this->act->actor;
163 if (empty($actor->id)) {
164 common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
165 common_log(LOG_ERR, "activity with no actor: " . var_export($this->act, true));
166 throw new Exception("Received a salmon slap from unidentified actor.");
169 return Ostatus_profile::ensureActivityObjectProfile($actor);
172 function saveNotice()
174 $oprofile = $this->ensureProfile();
176 // Get (safe!) HTML and text versions of the content
178 require_once(INSTALLDIR.'/extlib/HTMLPurifier/HTMLPurifier.auto.php');
180 $html = $this->act->object->content;
182 $purifier = new HTMLPurifier();
184 $rendered = $purifier->purify($html);
186 $content = html_entity_decode(strip_tags($rendered));
188 $options = array('is_local' => Notice::REMOTE_OMB,
189 'uri' => $this->act->object->id,
190 'url' => $this->act->object->link,
191 'rendered' => $rendered,
192 'replies' => $this->act->context->attention);
194 if (!empty($this->act->context->location)) {
195 $options['lat'] = $location->lat;
196 $options['lon'] = $location->lon;
197 if ($location->location_id) {
198 $options['location_ns'] = $location->location_ns;
199 $options['location_id'] = $location->location_id;
203 if (!empty($this->act->context->replyToID)) {
204 $orig = Notice::staticGet('uri',
205 $this->act->context->replyToID);
207 $options['reply_to'] = $orig->id;
211 if (!empty($this->act->time)) {
212 $options['created'] = common_sql_date($this->act->time);
215 $saved = Notice::saveNew($oprofile->profile_id,
220 // Record that this was saved through a validated Salmon source
221 // @fixme actually do the signature validation!
222 Ostatus_source::saveNew($saved, $oprofile, 'salmon');