]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Activity/ActivityPlugin.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / plugins / Activity / ActivityPlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Shows social activities in the output feed
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Activity
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Activity plugin main class
39  *
40  * @category  Activity
41  * @package   StatusNet
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 2010 StatusNet, Inc.
44  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45  * @link      http://status.net/
46  */
47 class ActivityPlugin extends Plugin
48 {
49     const VERSION = '0.1';
50     const SOURCE  = 'activity';
51
52     // Flags to switch off certain activity notices
53     public $StartFollowUser = true;
54     public $StopFollowUser  = false;
55     public $JoinGroup = true;
56     public $LeaveGroup = false;
57     public $StartLike = false;
58     public $StopLike = false;
59
60     function onEndSubscribe(Profile $profile, Profile $other)
61     {
62         // Only do this if config is enabled
63         if(!$this->StartFollowUser) return true;
64
65         if (!$profile->isLocal()) {
66             // can't do anything with remote user anyway
67             return true;
68         }
69
70         $sub = Subscription::pkeyGet(array('subscriber' => $profile->id,
71                                            'subscribed' => $other->id));
72         // TRANS: Text for "started following" item in activity plugin.
73         // TRANS: %1$s is a profile URL, %2$s is a profile name,
74         // TRANS: %3$s is a profile URL, %4$s is a profile name.
75         $rendered = html_sprintf(_m('<a href="%1$s">%2$s</a> started following <a href="%3$s">%4$s</a>.'),
76                             $profile->getUrl(),
77                             $profile->getBestName(),
78                             $other->getUrl(),
79                             $other->getBestName());
80         // TRANS: Text for "started following" item in activity plugin.
81         // TRANS: %1$s is a profile name, %2$s is a profile URL,
82         // TRANS: %3$s is a profile name, %4$s is a profile URL.
83         $content  = sprintf(_m('%1$s (%2$s) started following %3$s (%4$s).'),
84                             $profile->getBestName(),
85                             $profile->getUrl(),
86                             $other->getBestName(),
87                             $other->getUrl());
88
89         $notice = Notice::saveNew($profile->id,
90                                   $content,
91                                   ActivityPlugin::SOURCE,
92                                   array('rendered' => $rendered,
93                                         'urls' => array(),
94                                         'replies' => array($other->getUri()),
95                                         'verb' => ActivityVerb::FOLLOW,
96                                         'object_type' => ActivityObject::PERSON,
97                                         'uri' => $sub->uri));
98         return true;
99     }
100
101     function onEndUnsubscribe(Profile $profile, Profile $other)
102     {
103         // Only do this if config is enabled
104         if(!$this->StopFollowUser) return true;
105
106         if (!$profile->isLocal()) {
107             return true;
108         }
109
110         // TRANS: Text for "stopped following" item in activity plugin.
111         // TRANS: %1$s is a profile URL, %2$s is a profile name,
112         // TRANS: %3$s is a profile URL, %4$s is a profile name.
113         $rendered = html_sprintf(_m('<a href="%1$s">%2$s</a> stopped following <a href="%3$s">%4$s</a>.'),
114                             $profile->getUrl(),
115                             $profile->getBestName(),
116                             $other->getUrl(),
117                             $other->getBestName());
118         // TRANS: Text for "stopped following" item in activity plugin.
119         // TRANS: %1$s is a profile name, %2$s is a profile URL,
120         // TRANS: %3$s is a profile name, %4$s is a profile URL.
121         $content  = sprintf(_m('%1$s (%2$s) stopped following %3$s (%4$s).'),
122                             $profile->getBestName(),
123                             $profile->getUrl(),
124                             $other->getBestName(),
125                             $other->getUrl());
126
127         $uri = TagURI::mint('stop-following:%d:%d:%s',
128                             $profile->id,
129                             $other->id,
130                             common_date_iso8601(common_sql_now()));
131
132         $notice = Notice::saveNew($profile->id,
133                                   $content,
134                                   ActivityPlugin::SOURCE,
135                                   array('rendered' => $rendered,
136                                         'urls' => array(),
137                                         'replies' => array($other->getUri()),
138                                         'uri' => $uri,
139                                         'verb' => ActivityVerb::UNFOLLOW,
140                                         'object_type' => ActivityObject::PERSON));
141
142         return true;
143     }
144
145     function onEndDisfavorNotice($profile, $notice)
146     {
147         // Only do this if config is enabled
148         if(!$this->StopLike) return true;
149
150         if (!$profile->isLocal()) {
151             return true;
152         }
153
154         $author = Profile::getKV('id', $notice->profile_id);
155         // TRANS: Text for "stopped liking" item in activity plugin.
156         // TRANS: %1$s is a profile URL, %2$s is a profile name,
157         // TRANS: %3$s is a notice URL, %4$s is an author name.
158         $rendered = html_sprintf(_m('<a href="%1$s">%2$s</a> stopped liking <a href="%3$s">%4$s\'s update</a>.'),
159                             $profile->getUrl(),
160                             $profile->getBestName(),
161                             $notice->getUrl(),
162                             $author->getBestName());
163         // TRANS: Text for "stopped liking" item in activity plugin.
164         // TRANS: %1$s is a profile name, %2$s is a profile URL,
165         // TRANS: %3$s is an author name, %4$s is a notice URL.
166         $content  = sprintf(_m('%1$s (%2$s) stopped liking %3$s\'s status (%4$s).'),
167                             $profile->getBestName(),
168                             $profile->getUrl(),
169                             $author->getBestName(),
170                             $notice->getUrl());
171
172         $uri = TagURI::mint('unlike:%d:%d:%s',
173                             $profile->id,
174                             $notice->id,
175                             common_date_iso8601(common_sql_now()));
176
177         $notice = Notice::saveNew($profile->id,
178                                   $content,
179                                   ActivityPlugin::SOURCE,
180                                   array('rendered' => $rendered,
181                                         'urls' => array(),
182                                         'replies' => array($author->getUri()),
183                                         'uri' => $uri,
184                                         'verb' => ActivityVerb::UNFAVORITE,
185                                         'object_type' => (($notice->verb == ActivityVerb::POST) ?
186                                                          $notice->object_type : null)));
187
188         return true;
189     }
190
191     function onEndJoinGroup($group, $profile)
192     {
193         // Only do this if config is enabled
194         if(!$this->JoinGroup) return true;
195
196         if (!$profile->isLocal()) {
197             return true;
198         }
199
200         // TRANS: Text for "joined group" item in activity plugin.
201         // TRANS: %1$s is a profile URL, %2$s is a profile name,
202         // TRANS: %3$s is a group URL, %4$s is a group name.
203         $rendered = html_sprintf(_m('<a href="%1$s">%2$s</a> joined the group <a href="%3$s">%4$s</a>.'),
204                             $profile->getUrl(),
205                             $profile->getBestName(),
206                             $group->homeUrl(),
207                             $group->getBestName());
208         // TRANS: Text for "joined group" item in activity plugin.
209         // TRANS: %1$s is a profile name, %2$s is a profile URL,
210         // TRANS: %3$s is a group name, %4$s is a group URL.
211         $content  = sprintf(_m('%1$s (%2$s) joined the group %3$s (%4$s).'),
212                             $profile->getBestName(),
213                             $profile->getUrl(),
214                             $group->getBestName(),
215                             $group->homeUrl());
216
217         $mem = Group_member::pkeyGet(array('group_id' => $group->id,
218                                            'profile_id' => $profile->id));
219
220         $notice = Notice::saveNew($profile->id,
221                                   $content,
222                                   ActivityPlugin::SOURCE,
223                                   array('rendered' => $rendered,
224                                         'urls' => array(),
225                                         'groups' => array($group->id),
226                                         'uri' => $mem->getURI(),
227                                         'verb' => ActivityVerb::JOIN,
228                                         'object_type' => ActivityObject::GROUP));
229         return true;
230     }
231
232     function onEndLeaveGroup($group, $profile)
233     {
234         // Only do this if config is enabled
235         if(!$this->LeaveGroup) return true;
236
237         if (!$profile->isLocal()) {
238             return true;
239         }
240
241         // TRANS: Text for "left group" item in activity plugin.
242         // TRANS: %1$s is a profile URL, %2$s is a profile name,
243         // TRANS: %3$s is a group URL, %4$s is a group name.
244         $rendered = html_sprintf(_m('<a href="%1$s">%2$s</a> left the group <a href="%3$s">%4$s</a>.'),
245                             $profile->getUrl(),
246                             $profile->getBestName(),
247                             $group->homeUrl(),
248                             $group->getBestName());
249         // TRANS: Text for "left group" item in activity plugin.
250         // TRANS: %1$s is a profile name, %2$s is a profile URL,
251         // TRANS: %3$s is a group name, %4$s is a group URL.
252         $content  = sprintf(_m('%1$s (%2$s) left the group %3$s (%4$s).'),
253                             $profile->getBestName(),
254                             $profile->getUrl(),
255                             $group->getBestName(),
256                             $group->homeUrl());
257
258         $uri = TagURI::mint('leave:%d:%d:%s',
259                             $profile->id,
260                             $group->id,
261                             common_date_iso8601(common_sql_now()));
262
263         $notice = Notice::saveNew($profile->id,
264                                   $content,
265                                   ActivityPlugin::SOURCE,
266                                   array('rendered' => $rendered,
267                                         'urls' => array(),
268                                         'groups' => array($group->id),
269                                         'uri' => $uri,
270                                         'verb' => ActivityVerb::LEAVE,
271                                         'object_type' => ActivityObject::GROUP));
272         return true;
273     }
274
275     function onStartShowNoticeItem($nli)
276     {
277         $notice = $nli->notice;
278
279         $adapter = null;
280
281         switch ($notice->verb) {
282         case ActivityVerb::JOIN:
283             $adapter = new JoinListItem($nli);
284             break;
285         case ActivityVerb::LEAVE:
286             $adapter = new LeaveListItem($nli);
287             break;
288         case ActivityVerb::FOLLOW:
289             $adapter = new FollowListItem($nli);
290             break;
291         case ActivityVerb::UNFOLLOW:
292             $adapter = new UnfollowListItem($nli);
293             break;
294         }
295
296         if (!empty($adapter)) {
297             $adapter->showNotice();
298             $adapter->showNoticeAttachments();
299             $adapter->showNoticeInfo();
300             $adapter->showNoticeOptions();
301             return false;
302         }
303
304         return true;
305     }
306
307     public function onEndNoticeAsActivity(Notice $stored, Activity $act, Profile $scoped=null)
308     {
309         switch ($stored->verb) {
310         case ActivityVerb::UNFAVORITE:
311             // FIXME: do something here
312             break;
313         case ActivityVerb::JOIN:
314             $mem = Group_member::getKV('uri', $stored->getUri());
315             if ($mem instanceof Group_member) {
316                 $group = $mem->getGroup();
317                 $act->title = $stored->getTitle();
318                 $act->objects = array(ActivityObject::fromGroup($group));
319             }
320             break;
321         case ActivityVerb::LEAVE:
322             // FIXME: ????
323             break;
324         case ActivityVerb::FOLLOW:
325             $sub = Subscription::getKV('uri', $stored->uri);
326             if ($sub instanceof Subscription) {
327                 $profile = Profile::getKV('id', $sub->subscribed);
328                 if ($profile instanceof Profile) {
329                     $act->title = $stored->getTitle();
330                     $act->objects = array($profile->asActivityObject());
331                 }
332             }
333             break;
334         case ActivityVerb::UNFOLLOW:
335             // FIXME: ????
336             break;
337         }
338
339         return true;
340     }
341
342     function onPluginVersion(array &$versions)
343     {
344         $versions[] = array('name' => 'Activity',
345                             'version' => self::VERSION,
346                             'author' => 'Evan Prodromou',
347                             'homepage' => 'http://status.net/wiki/Plugin:Activity',
348                             'rawdescription' =>
349                             // TRANS: Plugin description.
350                             _m('Emits notices when social activities happen.'));
351         return true;
352     }
353 }