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 (empty($_SERVER['CONTENT_TYPE']) || $_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 (!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 // TODO : Insert new $xml -> notice code
81 if (Event::handle('StartHandleSalmon', array($this->activity))) {
82 switch ($this->act->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();
110 throw new ClientException(_("Unimplemented."));
112 Event::handle('EndHandleSalmon', array($this->activity));
116 function handlePost()
118 throw new ClientException(_("Unimplemented!"));
121 function handleFollow()
123 throw new ClientException(_("Unimplemented!"));
126 function handleUnfollow()
128 throw new ClientException(_("Unimplemented!"));
131 function handleFavorite()
133 throw new ClientException(_("Unimplemented!"));
137 * Remote user doesn't like one of our posts after all!
138 * Confirm the post is ours, and delete a local favorite event.
141 function handleUnfavorite()
143 throw new ClientException(_("Unimplemented!"));
149 function handleShare()
151 throw new ClientException(_("Unimplemented!"));
157 function handleJoin()
159 throw new ClientException(_("Unimplemented!"));
165 function handleLeave()
167 throw new ClientException(_("Unimplemented!"));
171 * @return Ostatus_profile
173 function ensureProfile()
175 $actor = $this->act->actor;
176 if (empty($actor->id)) {
177 common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
178 common_log(LOG_ERR, "activity with no actor: " . var_export($this->act, true));
179 throw new Exception("Received a salmon slap from unidentified actor.");
182 return Ostatus_profile::ensureActivityObjectProfile($actor);
185 function saveNotice()
187 $oprofile = $this->ensureProfile();
189 // Get (safe!) HTML and text versions of the content
191 require_once(INSTALLDIR.'/extlib/HTMLPurifier/HTMLPurifier.auto.php');
193 $html = $this->act->object->content;
195 $purifier = new HTMLPurifier();
197 $rendered = $purifier->purify($html);
199 $content = html_entity_decode(strip_tags($rendered));
201 $options = array('is_local' => Notice::REMOTE_OMB,
202 'uri' => $this->act->object->id,
203 'url' => $this->act->object->link,
204 'rendered' => $rendered,
205 'replies' => $this->act->context->attention);
207 if (!empty($this->act->context->location)) {
208 $options['lat'] = $location->lat;
209 $options['lon'] = $location->lon;
210 if ($location->location_id) {
211 $options['location_ns'] = $location->location_ns;
212 $options['location_id'] = $location->location_id;
216 if (!empty($this->act->context->replyToID)) {
217 $orig = Notice::staticGet('uri',
218 $this->act->context->replyToID);
220 $options['reply_to'] = $orig->id;
224 if (!empty($this->act->time)) {
225 $options['created'] = common_sql_date($this->act->time);
228 $saved = Notice::saveNew($oprofile->profile_id,
233 // Record that this was saved through a validated Salmon source
234 // @fixme actually do the signature validation!
235 Ostatus_source::saveNew($saved, $oprofile, 'salmon');