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.'));
57 // XXX: check the signature
59 $this->act = new Activity($dom->documentElement);
64 * Check the posted activity type and break out to appropriate processing.
67 function handle($args)
69 StatusNet::setApi(true); // Send smaller error pages
71 // TODO : Insert new $xml -> notice code
73 if (Event::handle('StartHandleSalmon', array($this->activity))) {
74 switch ($this->act->verb)
76 case ActivityVerb::POST:
79 case ActivityVerb::SHARE:
82 case ActivityVerb::FAVORITE:
83 $this->handleFavorite();
85 case ActivityVerb::UNFAVORITE:
86 $this->handleUnfavorite();
88 case ActivityVerb::FOLLOW:
89 case ActivityVerb::FRIEND:
90 $this->handleFollow();
92 case ActivityVerb::UNFOLLOW:
93 $this->handleUnfollow();
95 case ActivityVerb::JOIN:
99 throw new ClientException(_("Unimplemented."));
101 Event::handle('EndHandleSalmon', array($this->activity));
105 function handlePost()
107 throw new ClientException(_("Unimplemented!"));
110 function handleFollow()
112 throw new ClientException(_("Unimplemented!"));
115 function handleUnfollow()
117 throw new ClientException(_("Unimplemented!"));
120 function handleFavorite()
122 throw new ClientException(_("Unimplemented!"));
126 * Remote user doesn't like one of our posts after all!
127 * Confirm the post is ours, and delete a local favorite event.
130 function handleUnfavorite()
132 throw new ClientException(_("Unimplemented!"));
138 function handleShare()
140 throw new ClientException(_("Unimplemented!"));
146 function handleJoin()
148 throw new ClientException(_("Unimplemented!"));
152 * @return Ostatus_profile
154 function ensureProfile()
156 $actor = $this->act->actor;
157 if (empty($actor->id)) {
158 common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
159 common_log(LOG_ERR, "activity with no actor: " . var_export($this->act, true));
160 throw new Exception("Received a salmon slap from unidentified actor.");
163 return Ostatus_profile::ensureActivityObjectProfile($actor);
166 function saveNotice()
168 $oprofile = $this->ensureProfile();
170 // Get (safe!) HTML and text versions of the content
172 require_once(INSTALLDIR.'/extlib/HTMLPurifier/HTMLPurifier.auto.php');
174 $html = $this->act->object->content;
176 $rendered = HTMLPurifier::purify($html);
177 $content = html_entity_decode(strip_tags($rendered));
179 $options = array('is_local' => Notice::REMOTE_OMB,
180 'uri' => $this->act->object->id,
181 'url' => $this->act->object->link,
182 'rendered' => $rendered);
184 if (!empty($this->act->context->location)) {
185 $options['lat'] = $location->lat;
186 $options['lon'] = $location->lon;
187 if ($location->location_id) {
188 $options['location_ns'] = $location->location_ns;
189 $options['location_id'] = $location->location_id;
193 if (!empty($this->act->context->replyToID)) {
194 $orig = Notice::staticGet('uri',
195 $this->act->context->replyToID);
197 $options['reply_to'] = $orig->id;
201 if (!empty($this->act->time)) {
202 $options['created'] = common_sql_time($this->act->time);
205 return Notice::saveNew($oprofile->profile_id,