]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/UserFlag/UserFlagPlugin.php
Merge branch 'fatals' into 0.9.x
[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 class UserFlagPlugin extends Plugin
44 {
45     const REVIEWFLAGS = 'UserFlagPlugin::reviewflags';
46     const CLEARFLAGS  = 'UserFlagPlugin::clearflags';
47
48     public $flagOnBlock = true;
49
50     /**
51      * Hook for ensuring our tables are created
52      *
53      * Ensures that the user_flag_profile table exists
54      * and has the right columns.
55      *
56      * @return boolean hook return
57      */
58     function onCheckSchema()
59     {
60         $schema = Schema::get();
61
62         // For storing user-submitted flags on profiles
63
64         $schema->ensureTable('user_flag_profile',
65                              array(new ColumnDef('profile_id', 'integer', null,
66                                                  false, 'PRI'),
67                                    new ColumnDef('user_id', 'integer', null,
68                                                  false, 'PRI'),
69                                    new ColumnDef('created', 'datetime', null,
70                                                  false, 'MUL'),
71                                    new ColumnDef('cleared', 'datetime', null,
72                                                  true, 'MUL')));
73
74         return true;
75     }
76
77     /**
78      * Add our actions to the URL router
79      *
80      * @param Net_URL_Mapper $m URL mapper for this hit
81      *
82      * @return boolean hook return
83      */
84     function onRouterInitialized($m)
85     {
86         $m->connect('main/flag/profile', array('action' => 'flagprofile'));
87         $m->connect('main/flag/clear', array('action' => 'clearflag'));
88         $m->connect('admin/profile/flag', array('action' => 'adminprofileflag'));
89         return true;
90     }
91
92     /**
93      * Auto-load our classes if called
94      *
95      * @param string $cls Class to load
96      *
97      * @return boolean hook return
98      */
99     function onAutoload($cls)
100     {
101         switch (strtolower($cls))
102         {
103         case 'flagprofileaction':
104         case 'adminprofileflagaction':
105         case 'clearflagaction':
106             include_once INSTALLDIR.'/plugins/UserFlag/' .
107               strtolower(mb_substr($cls, 0, -6)) . '.php';
108             return false;
109         case 'flagprofileform':
110         case 'clearflagform':
111             include_once INSTALLDIR.'/plugins/UserFlag/' . strtolower($cls . '.php');
112             return false;
113         case 'user_flag_profile':
114             include_once INSTALLDIR.'/plugins/UserFlag/'.ucfirst(strtolower($cls)).'.php';
115             return false;
116         default:
117             return true;
118         }
119     }
120
121     /**
122      * Add a 'flag' button to profile page
123      *
124      * @param Action  &$action The action being called
125      * @param Profile $profile Profile being shown
126      *
127      * @return boolean hook result
128      */
129     function onEndProfilePageActionsElements(&$action, $profile)
130     {
131         $user = common_current_user();
132
133         if (!empty($user) && ($user->id != $profile->id)) {
134
135             $action->elementStart('li', 'entity_flag');
136
137             if (User_flag_profile::exists($profile->id, $user->id)) {
138                 // @todo FIXME: Add a title explaining what 'flagged' means?
139                 // TRANS: Message added to a profile if it has been flagged for review.
140                 $action->element('p', 'flagged', _('Flagged'));
141             } else {
142                 $form = new FlagProfileForm($action, $profile,
143                                             array('action' => 'showstream',
144                                                   'nickname' => $profile->nickname));
145                 $form->show();
146             }
147
148             $action->elementEnd('li');
149         }
150
151         return true;
152     }
153
154     /**
155      * Add a 'flag' button to profiles in a list
156      *
157      * @param ProfileListItem $item item being shown
158      *
159      * @return boolean hook result
160      */
161     function onEndProfileListItemActionElements($item)
162     {
163         $user = common_current_user();
164
165         if (!empty($user)) {
166
167             list($action, $args) = $item->action->returnToArgs();
168
169             $args['action'] = $action;
170
171             $form = new FlagProfileForm($item->action, $item->profile, $args);
172
173             $item->action->elementStart('li', 'entity_flag');
174             $form->show();
175             $item->action->elementEnd('li');
176         }
177
178         return true;
179     }
180
181     /**
182      * Initialize any flagging buttons on the page
183      *
184      * @param Action $action action being shown
185      *
186      * @return boolean hook result
187      */
188     function onEndShowScripts($action)
189     {
190         $action->inlineScript('if ($(".form_entity_flag").length > 0) { '.
191                               '$(".form_entity_flag").bind("click", function() {'.
192                               'SN.U.FormXHR($(this)); return false; }); }');
193         return true;
194     }
195
196     /**
197      * Check whether a user has one of our defined rights
198      *
199      * We define extra rights; this function checks to see if a
200      * user has one of them.
201      *
202      * @param User    $user    User being checked
203      * @param string  $right   Right we're checking
204      * @param boolean &$result out, result of the check
205      *
206      * @return boolean hook result
207      */
208     function onUserRightsCheck($user, $right, &$result)
209     {
210         switch ($right) {
211         case self::REVIEWFLAGS:
212         case self::CLEARFLAGS:
213             $result = $user->hasRole('moderator');
214             return false; // done processing!
215         }
216
217         return true; // unchanged!
218     }
219
220     /**
221      * Optionally flag profile when a block happens
222      *
223      * We optionally add a flag when a profile has been blocked
224      *
225      * @param User    $user    User doing the block
226      * @param Profile $profile Profile being blocked
227      *
228      * @return boolean hook result
229      */
230     function onEndBlockProfile($user, $profile)
231     {
232         if ($this->flagOnBlock && !User_flag_profile::exists($profile->id,
233                                                              $user->id)) {
234
235             User_flag_profile::create($user->id, $profile->id);
236         }
237         return true;
238     }
239
240     /**
241      * Ensure that flag entries for a profile are deleted
242      * along with the profile when deleting users.
243      * This prevents breakage of the admin profile flag UI.
244      *
245      * @param Profile $profile
246      * @param array &$related list of related tables; entries
247      *              with matching profile_id will be deleted.
248      *
249      * @return boolean hook result
250      */
251     function onProfileDeleteRelated($profile, &$related)
252     {
253         $related[] = 'user_flag_profile';
254         return true;
255     }
256
257     /**
258      * Ensure that flag entries created by a user are deleted
259      * when that user gets deleted.
260      *
261      * @param User $user
262      * @param array &$related list of related tables; entries
263      *              with matching user_id will be deleted.
264      *
265      * @return boolean hook result
266      */
267     function onUserDeleteRelated($user, &$related)
268     {
269         $related[] = 'user_flag_profile';
270         return true;
271     }
272
273     /**
274      * Provide plugin version information.
275      *
276      * This data is used when showing the version page.
277      *
278      * @param array &$versions array of version data arrays; see EVENTS.txt
279      *
280      * @return boolean hook value
281      */
282     function onPluginVersion(&$versions)
283     {
284         $url = 'http://status.net/wiki/Plugin:UserFlag';
285
286         $versions[] = array('name' => 'UserFlag',
287             'version' => STATUSNET_VERSION,
288             'author' => 'Evan Prodromou',
289             'homepage' => $url,
290             'rawdescription' =>
291             // TRANS: Plugin description.
292             _m('This plugin allows flagging of profiles for review and reviewing flagged profiles.'));
293
294         return true;
295     }
296 }