]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GroupPrivateMessage/GroupPrivateMessagePlugin.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / plugins / GroupPrivateMessage / GroupPrivateMessagePlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Private groups for StatusNet 0.9.x
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  Privacy
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2011 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  * Private groups
39  *
40  * This plugin allows users to send private messages to a group.
41  *
42  * @category  Privacy
43  * @package   StatusNet
44  * @author    Evan Prodromou <evan@status.net>
45  * @copyright 2011 StatusNet, Inc.
46  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
47  * @link      http://status.net/
48  */
49 class GroupPrivateMessagePlugin extends Plugin
50 {
51     /**
52      * Database schema setup
53      *
54      * @see Schema
55      * @see ColumnDef
56      *
57      * @return boolean hook value
58      */
59     function onCheckSchema()
60     {
61         $schema = Schema::get();
62
63         // For storing user-submitted flags on profiles
64         $schema->ensureTable('group_privacy_settings', Group_privacy_settings::schemaDef());
65         $schema->ensureTable('group_message', Group_message::schemaDef());
66         $schema->ensureTable('group_message_profile', Group_message_profile::schemaDef());
67         return true;
68     }
69
70     /**
71      * Map URLs to actions
72      *
73      * @param URLMapper $m path-to-action mapper
74      *
75      * @return boolean hook value
76      */
77     public function onRouterInitialized(URLMapper $m)
78     {
79         $m->connect('group/:nickname/inbox',
80                     array('action' => 'groupinbox'),
81                     array('nickname' => Nickname::DISPLAY_FMT));
82
83         $m->connect('group/message/:id',
84                     array('action' => 'showgroupmessage'),
85                     array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
86
87         $m->connect('group/:nickname/message/new',
88                     array('action' => 'newgroupmessage'),
89                     array('nickname' => Nickname::DISPLAY_FMT));
90
91         return true;
92     }
93
94     /**
95      * Add group inbox to the menu
96      *
97      * @param Action $action The current action handler. Use this to
98      *                       do any output.
99      *
100      * @return boolean hook value; true means continue processing, false means stop.
101      *
102      * @see Action
103      */
104     function onEndGroupGroupNav(Menu $groupnav)
105     {
106         $action = $groupnav->action;
107         $group  = $groupnav->group;
108
109         $action->menuItem(common_local_url('groupinbox',
110                                            array('nickname' => $group->nickname)),
111                           // TRANS: Menu item in group page.
112                           _m('MENU','Inbox'),
113                           // TRANS: Menu title in group page.
114                           _m('Private messages for this group.'),
115                           $action->trimmed('action') == 'groupinbox',
116                           'nav_group_inbox');
117         return true;
118     }
119
120     /**
121      * Create default group privacy settings at group create time
122      *
123      * @param User_group $group Group that was just created
124      *
125      * @result boolean hook value
126      */
127     function onEndGroupSave($group)
128     {
129         $gps = new Group_privacy_settings();
130
131         $gps->group_id      = $group->id;
132         $gps->allow_privacy = Group_privacy_settings::SOMETIMES;
133         $gps->allow_sender  = Group_privacy_settings::MEMBER;
134         $gps->created       = common_sql_now();
135         $gps->modified      = $gps->created;
136
137         // This will throw an exception on error
138
139         $gps->insert();
140
141         return true;
142     }
143
144     /**
145      * Show group privacy controls on group edit form
146      *
147      * @param GroupEditForm $form form being shown
148      */
149     function onEndGroupEditFormData(GroupEditForm $form)
150     {
151         $gps = null;
152
153         if (!empty($form->group)) {
154             $gps = Group_privacy_settings::getKV('group_id', $form->group->id);
155         }
156
157         $form->out->elementStart('li');
158         $form->out->dropdown('allow_privacy',
159                              // TRANS: Dropdown label in group settings page for if group allows private messages.
160                              _m('Private messages'),
161                              // TRANS: Dropdown option in group settings page for allowing private messages.
162                              array(Group_privacy_settings::SOMETIMES => _m('Sometimes'),
163                                    // TRANS: Dropdown option in group settings page for allowing private messages.
164                                    Group_privacy_settings::ALWAYS => _m('Always'),
165                                    // TRANS: Dropdown option in group settings page for allowing private messages.
166                                    Group_privacy_settings::NEVER => _m('Never')),
167                              // TRANS: Dropdown title in group settings page for if group allows private messages.
168                              _m('Whether to allow private messages to this group.'),
169                              false,
170                              (empty($gps)) ? Group_privacy_settings::SOMETIMES : $gps->allow_privacy);
171         $form->out->elementEnd('li');
172         $form->out->elementStart('li');
173         $form->out->dropdown('allow_sender',
174                              // TRANS: Dropdown label in group settings page for who can send private messages to the group.
175                              _m('Private senders'),
176                              // TRANS: Dropdown option in group settings page for who can send private messages.
177                              array(Group_privacy_settings::EVERYONE => _m('Everyone'),
178                                    // TRANS: Dropdown option in group settings page for who can send private messages.
179                                    Group_privacy_settings::MEMBER => _m('Member'),
180                                    // TRANS: Dropdown option in group settings page for who can send private messages.
181                                    Group_privacy_settings::ADMIN => _m('Admin')),
182                              // TRANS: Dropdown title in group settings page for who can send private messages to the group.
183                              _m('Who can send private messages to the group.'),
184                              false,
185                              (empty($gps)) ? Group_privacy_settings::MEMBER : $gps->allow_sender);
186         $form->out->elementEnd('li');
187         return true;
188     }
189
190     function onEndGroupSaveForm(Action $action)
191     {
192         // The Action class must contain this method
193         assert(is_callable(array($action, 'getGroup')));
194
195         $gps = null;
196
197         if ($action->getGroup() instanceof User_group) {
198             $gps = Group_privacy_settings::getKV('group_id', $action->getGroup()->id);
199         }
200
201         $orig = null;
202
203         if (empty($gps)) {
204             $gps = new Group_privacy_settings();
205             $gps->group_id = $action->getGroup()->id;
206         } else {
207             $orig = clone($gps);
208         }
209
210         $gps->allow_privacy = $action->trimmed('allow_privacy');
211         $gps->allow_sender  = $action->trimmed('allow_sender');
212
213         if (empty($orig)) {
214             $gps->created = common_sql_now();
215             $gps->insert();
216         } else {
217             $gps->update($orig);
218         }
219
220         return true;
221     }
222
223     /**
224      * Overload 'd' command to send private messages to groups.
225      *
226      * 'd !group word word word' will send the private message
227      * 'word word word' to the group 'group'.
228      *
229      * @param string  $cmd     Command being run
230      * @param string  $arg     Rest of the message (including address)
231      * @param User    $user    User sending the message
232      * @param Command &$result The resulting command object to be run.
233      *
234      * @return boolean hook value
235      */
236     function onStartInterpretCommand($cmd, $arg, User $user, &$result)
237     {
238         if ($cmd == 'd' || $cmd == 'dm') {
239
240             $this->debug('Got a d command');
241
242             // Break off the first word as the address
243
244             $pieces = explode(' ', $arg, 2);
245
246             if (count($pieces) == 1) {
247                 $pieces[] = null;
248             }
249
250             list($addr, $msg) = $pieces;
251
252             if (!empty($addr) && $addr[0] == '!') {
253                 $result = new GroupMessageCommand($user, substr($addr, 1), $msg);
254                 Event::handle('EndInterpretCommand', array($cmd, $arg, $user, $result));
255                 return false;
256             }
257         }
258
259         return true;
260     }
261
262     /**
263      * To add a "Message" button to the group profile page
264      *
265      * @param Widget     $widget The showgroup action being shown
266      * @param User_group $group  The current group
267      *
268      * @return boolean hook value
269      */
270     function onEndGroupActionsList(Widget $widget, User_group $group)
271     {
272         $cur = common_current_user();
273         $action = $widget->out;
274
275         if (empty($cur)) {
276             return true;
277         }
278
279         try {
280             Group_privacy_settings::ensurePost($cur, $group);
281         } catch (Exception $e) {
282             return true;
283         }
284
285         $action->elementStart('li', 'entity_send-a-message');
286         $action->element('a', array('href' => common_local_url('newgroupmessage', array('nickname' => $group->nickname)),
287                                     // TRANS: Title for action in group actions list.
288                                     'title' => _m('Send a direct message to this group.')),
289                          // TRANS: Link text for action in group actions list to send a private message to a group.
290                          _m('LINKTEXT','Message'));
291         // $form = new GroupMessageForm($action, $group);
292         // $form->hidden = true;
293         // $form->show();
294         $action->elementEnd('li');
295         return true;
296     }
297
298     /**
299      * When saving a notice, check its groups. If any of them has
300      * privacy == always, force a group private message to all mentioned groups.
301      * If any of the groups disallows private messages, skip it.
302      *
303      * @param
304      */
305     function onStartNoticeSave(Notice &$notice) {
306         // Look for group tags
307         // FIXME: won't work for remote groups
308         // @fixme if Notice::saveNew is refactored so we can just pull its list
309         // of groups between processing and saving, make use of it
310
311         $count = preg_match_all('/(?:^|\s)!(' . Nickname::DISPLAY_FMT . ')/',
312                                 strtolower($notice->content),
313                                 $match);
314
315         $groups = array();
316         $ignored = array();
317
318         $forcePrivate = false;
319         $profile = $notice->getProfile();
320
321         if ($count > 0) {
322             /* Add them to the database */
323
324             foreach (array_unique($match[1]) as $nickname) {
325                 $group = User_group::getForNickname($nickname, $profile);
326
327                 if (empty($group)) {
328                     continue;
329                 }
330
331                 $gps = Group_privacy_settings::forGroup($group);
332
333                 switch ($gps->allow_privacy) {
334                 case Group_privacy_settings::ALWAYS:
335                     $forcePrivate = true;
336                     // fall through
337                 case Group_privacy_settings::SOMETIMES:
338                     $groups[] = $group;
339                     break;
340                 case Group_privacy_settings::NEVER:
341                     $ignored[] = $group;
342                     break;
343                 }
344             }
345
346             if ($forcePrivate) {
347                 foreach ($ignored as $group) {
348                     common_log(LOG_NOTICE,
349                                "Notice forced to group direct message ".
350                                "but group ".$group->nickname." does not allow them.");
351                 }
352
353                 $user = User::getKV('id', $notice->profile_id);
354
355                 if (empty($user)) {
356                     common_log(LOG_WARNING,
357                                "Notice forced to group direct message ".
358                                "but profile ".$notice->profile_id." is not a local user.");
359                 } else {
360                     foreach ($groups as $group) {
361                         Group_message::send($user, $group, $notice->content);
362                     }
363                 }
364
365                 // Don't save the notice!
366                 // FIXME: this is probably cheating.
367                 // TRANS: Client exception thrown when a private group message has to be forced.
368                 throw new ClientException(sprintf(_m('Forced notice to private group message.')),
369                                           200);
370             }
371         }
372
373         return true;
374     }
375
376     /**
377      * Show an indicator that the group is (essentially) private on the group page
378      *
379      * @param Action     $action The action being shown
380      * @param User_group $group  The group being shown
381      *
382      * @return boolean hook value
383      */
384     function onEndGroupProfileElements(Action $action, User_group $group)
385     {
386         $gps = Group_privacy_settings::forGroup($group);
387
388         if ($gps->allow_privacy == Group_privacy_settings::ALWAYS) {
389             // TRANS: Indicator on the group page that the group is (essentially) private.
390             $action->element('p', 'privategroupindicator', _m('Private'));
391         }
392
393         return true;
394     }
395
396     function onStartShowExportData(Action $action)
397     {
398         if ($action instanceof ShowgroupAction) {
399             $gps = Group_privacy_settings::forGroup($action->getGroup());
400
401             if ($gps->allow_privacy == Group_privacy_settings::ALWAYS) {
402                 return false;
403             }
404         }
405         return true;
406     }
407
408     function onPluginVersion(array &$versions)
409     {
410         $versions[] = array('name' => 'GroupPrivateMessage',
411                             'version' => GNUSOCIAL_VERSION,
412                             'author' => 'Evan Prodromou',
413                             'homepage' => 'http://status.net/wiki/Plugin:GroupPrivateMessage',
414                             'rawdescription' =>
415                             // TRANS: Plugin description.
416                             _m('Allow posting private messages to groups.'));
417         return true;
418     }
419 }