]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/UserFlag/UserFlagPlugin.php
Merge remote branch 'statusnet/master' into testing
[quix0rs-gnu-social.git] / plugins / UserFlag / UserFlagPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Allows users to flag content and accounts as offensive/spam/whatever
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Plugin
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * Allows users to flag content and accounts as offensive/spam/whatever
36  *
37  * @category Plugin
38  * @package  StatusNet
39  * @author   Evan Prodromou <evan@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://status.net/
42  */
43
44 class UserFlagPlugin extends Plugin
45 {
46     const REVIEWFLAGS = 'UserFlagPlugin::reviewflags';
47     const CLEARFLAGS  = 'UserFlagPlugin::clearflags';
48
49     public $flagOnBlock = true;
50
51     /**
52      * Hook for ensuring our tables are created
53      *
54      * Ensures that the user_flag_profile table exists
55      * and has the right columns.
56      *
57      * @return boolean hook return
58      */
59
60     function onCheckSchema()
61     {
62         $schema = Schema::get();
63
64         // For storing user-submitted flags on profiles
65
66         $schema->ensureTable('user_flag_profile',
67                              array(new ColumnDef('profile_id', 'integer', null,
68                                                  false, 'PRI'),
69                                    new ColumnDef('user_id', 'integer', null,
70                                                  false, 'PRI'),
71                                    new ColumnDef('created', 'datetime', null,
72                                                  false, 'MUL'),
73                                    new ColumnDef('cleared', 'datetime', null,
74                                                  true, 'MUL')));
75
76         return true;
77     }
78
79     /**
80      * Add our actions to the URL router
81      *
82      * @param Net_URL_Mapper $m URL mapper for this hit
83      *
84      * @return boolean hook return
85      */
86
87     function onRouterInitialized($m)
88     {
89         $m->connect('main/flag/profile', array('action' => 'flagprofile'));
90         $m->connect('main/flag/clear', array('action' => 'clearflag'));
91         $m->connect('admin/profile/flag', array('action' => 'adminprofileflag'));
92         return true;
93     }
94
95     /**
96      * Auto-load our classes if called
97      *
98      * @param string $cls Class to load
99      *
100      * @return boolean hook return
101      */
102
103     function onAutoload($cls)
104     {
105         switch (strtolower($cls))
106         {
107         case 'flagprofileaction':
108         case 'adminprofileflagaction':
109         case 'clearflagaction':
110             include_once INSTALLDIR.'/plugins/UserFlag/' .
111               strtolower(mb_substr($cls, 0, -6)) . '.php';
112             return false;
113         case 'flagprofileform':
114         case 'clearflagform':
115             include_once INSTALLDIR.'/plugins/UserFlag/' . strtolower($cls . '.php');
116             return false;
117         case 'user_flag_profile':
118             include_once INSTALLDIR.'/plugins/UserFlag/'.ucfirst(strtolower($cls)).'.php';
119             return false;
120         default:
121             return true;
122         }
123     }
124
125     /**
126      * Add a 'flag' button to profile page
127      *
128      * @param Action  &$action The action being called
129      * @param Profile $profile Profile being shown
130      *
131      * @return boolean hook result
132      */
133
134     function onEndProfilePageActionsElements(&$action, $profile)
135     {
136         $user = common_current_user();
137
138         if (!empty($user) && ($user->id != $profile->id)) {
139
140             $action->elementStart('li', 'entity_flag');
141
142             if (User_flag_profile::exists($profile->id, $user->id)) {
143                 $action->element('p', 'flagged', _('Flagged'));
144             } else {
145                 $form = new FlagProfileForm($action, $profile,
146                                             array('action' => 'showstream',
147                                                   'nickname' => $profile->nickname));
148                 $form->show();
149             }
150
151             $action->elementEnd('li');
152         }
153
154         return true;
155     }
156
157     /**
158      * Add a 'flag' button to profiles in a list
159      *
160      * @param ProfileListItem $item item being shown
161      *
162      * @return boolean hook result
163      */
164
165     function onEndProfileListItemActionElements($item)
166     {
167         $user = common_current_user();
168
169         if (!empty($user)) {
170
171             list($action, $args) = $item->action->returnToArgs();
172
173             $args['action'] = $action;
174
175             $form = new FlagProfileForm($item->action, $item->profile, $args);
176
177             $item->action->elementStart('li', 'entity_flag');
178             $form->show();
179             $item->action->elementEnd('li');
180         }
181
182         return true;
183     }
184
185     /**
186      * Add our plugin's CSS to page output
187      *
188      * @param Action $action action being shown
189      *
190      * @return boolean hook result
191      */
192
193     function onEndShowStatusNetStyles($action)
194     {
195         $action->cssLink(common_path('plugins/UserFlag/userflag.css'),
196                          null, 'screen, projection, tv');
197         return true;
198     }
199
200     /**
201      * Initialize any flagging buttons on the page
202      *
203      * @param Action $action action being shown
204      *
205      * @return boolean hook result
206      */
207
208     function onEndShowScripts($action)
209     {
210         $action->inlineScript('if ($(".form_entity_flag").length > 0) { '.
211                               '$(".form_entity_flag").bind("click", function() {'.
212                               'SN.U.FormXHR($(this)); return false; }); }');
213         return true;
214     }
215
216     /**
217      * Check whether a user has one of our defined rights
218      *
219      * We define extra rights; this function checks to see if a
220      * user has one of them.
221      *
222      * @param User    $user    User being checked
223      * @param string  $right   Right we're checking
224      * @param boolean &$result out, result of the check
225      *
226      * @return boolean hook result
227      */
228
229     function onUserRightsCheck($user, $right, &$result)
230     {
231         switch ($right) {
232         case self::REVIEWFLAGS:
233         case self::CLEARFLAGS:
234             $result = $user->hasRole('moderator');
235             return false; // done processing!
236         }
237
238         return true; // unchanged!
239     }
240
241     /**
242      * Optionally flag profile when a block happens
243      *
244      * We optionally add a flag when a profile has been blocked
245      *
246      * @param User    $user    User doing the block
247      * @param Profile $profile Profile being blocked
248      *
249      * @return boolean hook result
250      */
251
252     function onEndBlockProfile($user, $profile)
253     {
254         if ($this->flagOnBlock && !User_flag_profile::exists($profile->id,
255                                                              $user->id)) {
256
257             User_flag_profile::create($user->id, $profile->id);
258         }
259         return true;
260     }
261
262     /**
263      * Ensure that flag entries for a profile are deleted
264      * along with the profile when deleting users.
265      * This prevents breakage of the admin profile flag UI.
266      *
267      * @param Profile $profile
268      * @param array &$related list of related tables; entries
269      *              with matching profile_id will be deleted.
270      *
271      * @return boolean hook result
272      */
273
274     function onProfileDeleteRelated($profile, &$related)
275     {
276         $related[] = 'user_flag_profile';
277         return true;
278     }
279
280     /**
281      * Ensure that flag entries created by a user are deleted
282      * when that user gets deleted.
283      *
284      * @param User $user
285      * @param array &$related list of related tables; entries
286      *              with matching user_id will be deleted.
287      *
288      * @return boolean hook result
289      */
290
291     function onUserDeleteRelated($user, &$related)
292     {
293         $related[] = 'user_flag_profile';
294         return true;
295     }
296 }