]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/salmonaction.php
OStatus: record source profile & saving method in ostatus_source table; this allows...
[quix0rs-gnu-social.git] / plugins / OStatus / lib / salmonaction.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 /**
21  * @package OStatusPlugin
22  * @author James Walker <james@status.net>
23  */
24
25 if (!defined('STATUSNET')) {
26     exit(1);
27 }
28
29 class SalmonAction extends Action
30 {
31     var $xml      = null;
32     var $activity = null;
33
34     function prepare($args)
35     {
36         StatusNet::setApi(true); // Send smaller error pages
37
38         parent::prepare($args);
39
40         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
41             $this->clientError(_('This method requires a POST.'));
42         }
43
44         if ($_SERVER['CONTENT_TYPE'] != 'application/atom+xml') {
45             $this->clientError(_('Salmon requires application/atom+xml'));
46         }
47
48         $xml = file_get_contents('php://input');
49
50         $dom = DOMDocument::loadXML($xml);
51
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.'));
56         }
57         // XXX: check the signature
58
59         $this->act = new Activity($dom->documentElement);
60         return true;
61     }
62
63     /**
64      * Check the posted activity type and break out to appropriate processing.
65      */
66
67     function handle($args)
68     {
69         StatusNet::setApi(true); // Send smaller error pages
70
71         // TODO : Insert new $xml -> notice code
72
73         if (Event::handle('StartHandleSalmon', array($this->activity))) {
74             switch ($this->act->verb)
75             {
76             case ActivityVerb::POST:
77                 $this->handlePost();
78                 break;
79             case ActivityVerb::SHARE:
80                 $this->handleShare();
81                 break;
82             case ActivityVerb::FAVORITE:
83                 $this->handleFavorite();
84                 break;
85             case ActivityVerb::UNFAVORITE:
86                 $this->handleUnfavorite();
87                 break;
88             case ActivityVerb::FOLLOW:
89             case ActivityVerb::FRIEND:
90                 $this->handleFollow();
91                 break;
92             case ActivityVerb::UNFOLLOW:
93                 $this->handleUnfollow();
94                 break;
95             case ActivityVerb::JOIN:
96                 $this->handleJoin();
97                 break;
98             default:
99                 throw new ClientException(_("Unimplemented."));
100             }
101             Event::handle('EndHandleSalmon', array($this->activity));
102         }
103     }
104
105     function handlePost()
106     {
107         throw new ClientException(_("Unimplemented!"));
108     }
109
110     function handleFollow()
111     {
112         throw new ClientException(_("Unimplemented!"));
113     }
114
115     function handleUnfollow()
116     {
117         throw new ClientException(_("Unimplemented!"));
118     }
119
120     function handleFavorite()
121     {
122         throw new ClientException(_("Unimplemented!"));
123     }
124
125     /**
126      * Remote user doesn't like one of our posts after all!
127      * Confirm the post is ours, and delete a local favorite event.
128      */
129
130     function handleUnfavorite()
131     {
132         throw new ClientException(_("Unimplemented!"));
133     }
134
135     /**
136      * Hmmmm
137      */
138     function handleShare()
139     {
140         throw new ClientException(_("Unimplemented!"));
141     }
142
143     /**
144      * Hmmmm
145      */
146     function handleJoin()
147     {
148         throw new ClientException(_("Unimplemented!"));
149     }
150
151     /**
152      * @return Ostatus_profile
153      */
154     function ensureProfile()
155     {
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.");
161         }
162
163         return Ostatus_profile::ensureActivityObjectProfile($actor);
164     }
165
166     function saveNotice()
167     {
168         $oprofile = $this->ensureProfile();
169
170         // Get (safe!) HTML and text versions of the content
171
172         require_once(INSTALLDIR.'/extlib/HTMLPurifier/HTMLPurifier.auto.php');
173
174         $html = $this->act->object->content;
175
176         $rendered = HTMLPurifier::purify($html);
177         $content = html_entity_decode(strip_tags($rendered));
178
179         $options = array('is_local' => Notice::REMOTE_OMB,
180                          'uri' => $this->act->object->id,
181                          'url' => $this->act->object->link,
182                          'rendered' => $rendered);
183
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;
190             }
191         }
192
193         if (!empty($this->act->context->replyToID)) {
194             $orig = Notice::staticGet('uri',
195                                       $this->act->context->replyToID);
196             if (!empty($orig)) {
197                 $options['reply_to'] = $orig->id;
198             }
199         }
200
201         if (!empty($this->act->time)) {
202             $options['created'] = common_sql_time($this->act->time);
203         }
204
205         $saved = Notice::saveNew($oprofile->profile_id,
206                                  $content,
207                                  'ostatus+salmon',
208                                  $options);
209
210         // Record that this was saved through a validated Salmon source
211         // @fixme actually do the signature validation!
212         Ostatus_source::saveNew($saved, $oprofile, 'salmon');
213         return $saved;
214     }
215 }