]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/salmonaction.php
09a042975dd9a3af0660df9d65c3f9c18ef6991e
[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
58         // Check the signature
59         $salmon = new Salmon;
60         if (!$salmon->verifyMagicEnv($dom)) {
61             common_log(LOG_DEBUG, "Salmon signature verification failed.");
62             $this->clientError(_m('Salmon signature verification failed.'));
63         }
64             
65         $this->act = new Activity($dom->documentElement);
66         return true;
67     }
68
69     /**
70      * Check the posted activity type and break out to appropriate processing.
71      */
72
73     function handle($args)
74     {
75         StatusNet::setApi(true); // Send smaller error pages
76
77         // TODO : Insert new $xml -> notice code
78
79         if (Event::handle('StartHandleSalmon', array($this->activity))) {
80             switch ($this->act->verb)
81             {
82             case ActivityVerb::POST:
83                 $this->handlePost();
84                 break;
85             case ActivityVerb::SHARE:
86                 $this->handleShare();
87                 break;
88             case ActivityVerb::FAVORITE:
89                 $this->handleFavorite();
90                 break;
91             case ActivityVerb::UNFAVORITE:
92                 $this->handleUnfavorite();
93                 break;
94             case ActivityVerb::FOLLOW:
95             case ActivityVerb::FRIEND:
96                 $this->handleFollow();
97                 break;
98             case ActivityVerb::UNFOLLOW:
99                 $this->handleUnfollow();
100                 break;
101             case ActivityVerb::JOIN:
102                 $this->handleJoin();
103                 break;
104             default:
105                 throw new ClientException(_("Unimplemented."));
106             }
107             Event::handle('EndHandleSalmon', array($this->activity));
108         }
109     }
110
111     function handlePost()
112     {
113         throw new ClientException(_("Unimplemented!"));
114     }
115
116     function handleFollow()
117     {
118         throw new ClientException(_("Unimplemented!"));
119     }
120
121     function handleUnfollow()
122     {
123         throw new ClientException(_("Unimplemented!"));
124     }
125
126     function handleFavorite()
127     {
128         throw new ClientException(_("Unimplemented!"));
129     }
130
131     /**
132      * Remote user doesn't like one of our posts after all!
133      * Confirm the post is ours, and delete a local favorite event.
134      */
135
136     function handleUnfavorite()
137     {
138         throw new ClientException(_("Unimplemented!"));
139     }
140
141     /**
142      * Hmmmm
143      */
144     function handleShare()
145     {
146         throw new ClientException(_("Unimplemented!"));
147     }
148
149     /**
150      * Hmmmm
151      */
152     function handleJoin()
153     {
154         throw new ClientException(_("Unimplemented!"));
155     }
156
157     /**
158      * @return Ostatus_profile
159      */
160     function ensureProfile()
161     {
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.");
167         }
168
169         return Ostatus_profile::ensureActivityObjectProfile($actor);
170     }
171
172     function saveNotice()
173     {
174         $oprofile = $this->ensureProfile();
175
176         // Get (safe!) HTML and text versions of the content
177
178         require_once(INSTALLDIR.'/extlib/HTMLPurifier/HTMLPurifier.auto.php');
179
180         $html = $this->act->object->content;
181
182         $purifier = new HTMLPurifier();
183
184         $rendered = $purifier->purify($html);
185
186         $content = html_entity_decode(strip_tags($rendered));
187
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);
193
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;
200             }
201         }
202
203         if (!empty($this->act->context->replyToID)) {
204             $orig = Notice::staticGet('uri',
205                                       $this->act->context->replyToID);
206             if (!empty($orig)) {
207                 $options['reply_to'] = $orig->id;
208             }
209         }
210
211         if (!empty($this->act->time)) {
212             $options['created'] = common_sql_date($this->act->time);
213         }
214
215         $saved = Notice::saveNew($oprofile->profile_id,
216                                  $content,
217                                  'ostatus+salmon',
218                                  $options);
219
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');
223         return $saved;
224     }
225 }