]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/UserFlag/UserFlagPlugin.php
Merge branch '0.9.x' into 1.0.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('panel/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         $this->showFlagButton($action, $profile,
132                               array('action' => 'showstream',
133                                     'nickname' => $profile->nickname));
134
135         return true;
136     }
137
138     /**
139      * Add a 'flag' button to profiles in a list
140      *
141      * @param ProfileListItem $item item being shown
142      *
143      * @return boolean hook result
144      */
145     function onEndProfileListItemActionElements($item)
146     {
147         list($action, $args) = $item->action->returnToArgs();
148         $args['action'] = $action;
149         $this->showFlagButton($item->action, $item->profile, $args);
150
151         return true;
152     }
153
154     /**
155      * Actually output a flag button. If the target profile has already been
156      * flagged by the current user, a null-action faux button is shown.
157      *
158      * @param Action $action
159      * @param Profile $profile
160      * @param array $returnToArgs
161      */
162     protected function showFlagButton($action, $profile, $returnToArgs)
163     {
164         $user = common_current_user();
165
166         if (!empty($user) && ($user->id != $profile->id)) {
167
168             $action->elementStart('li', 'entity_flag');
169
170             if (User_flag_profile::exists($profile->id, $user->id)) {
171                 // @todo FIXME: Add a title explaining what 'flagged' means?
172                 // TRANS: Message added to a profile if it has been flagged for review.
173                 $action->element('p', 'flagged', _m('Flagged'));
174             } else {
175                 $form = new FlagProfileForm($action, $profile, $returnToArgs);
176                 $form->show();
177             }
178
179             $action->elementEnd('li');
180         }
181     }
182
183     /**
184      * Initialize any flagging buttons on the page
185      *
186      * @param Action $action action being shown
187      *
188      * @return boolean hook result
189      */
190     function onEndShowScripts($action)
191     {
192         $action->inlineScript('if ($(".form_entity_flag").length > 0) { '.
193                               '$(".form_entity_flag").bind("click", function() {'.
194                               'SN.U.FormXHR($(this)); return false; }); }');
195         return true;
196     }
197
198     /**
199      * Check whether a user has one of our defined rights
200      *
201      * We define extra rights; this function checks to see if a
202      * user has one of them.
203      *
204      * @param User    $user    User being checked
205      * @param string  $right   Right we're checking
206      * @param boolean &$result out, result of the check
207      *
208      * @return boolean hook result
209      */
210     function onUserRightsCheck($user, $right, &$result)
211     {
212         switch ($right) {
213         case self::REVIEWFLAGS:
214         case self::CLEARFLAGS:
215             $result = $user->hasRole('moderator');
216             return false; // done processing!
217         }
218
219         return true; // unchanged!
220     }
221
222     /**
223      * Optionally flag profile when a block happens
224      *
225      * We optionally add a flag when a profile has been blocked
226      *
227      * @param User    $user    User doing the block
228      * @param Profile $profile Profile being blocked
229      *
230      * @return boolean hook result
231      */
232     function onEndBlockProfile($user, $profile)
233     {
234         if ($this->flagOnBlock && !User_flag_profile::exists($profile->id,
235                                                              $user->id)) {
236
237             User_flag_profile::create($user->id, $profile->id);
238         }
239         return true;
240     }
241
242     /**
243      * Ensure that flag entries for a profile are deleted
244      * along with the profile when deleting users.
245      * This prevents breakage of the admin profile flag UI.
246      *
247      * @param Profile $profile
248      * @param array &$related list of related tables; entries
249      *              with matching profile_id will be deleted.
250      *
251      * @return boolean hook result
252      */
253     function onProfileDeleteRelated($profile, &$related)
254     {
255         $related[] = 'user_flag_profile';
256         return true;
257     }
258
259     /**
260      * Ensure that flag entries created by a user are deleted
261      * when that user gets deleted.
262      *
263      * @param User $user
264      * @param array &$related list of related tables; entries
265      *              with matching user_id will be deleted.
266      *
267      * @return boolean hook result
268      */
269     function onUserDeleteRelated($user, &$related)
270     {
271         $related[] = 'user_flag_profile';
272         return true;
273     }
274
275     /**
276      * Provide plugin version information.
277      *
278      * This data is used when showing the version page.
279      *
280      * @param array &$versions array of version data arrays; see EVENTS.txt
281      *
282      * @return boolean hook value
283      */
284     function onPluginVersion(&$versions)
285     {
286         $url = 'http://status.net/wiki/Plugin:UserFlag';
287
288         $versions[] = array('name' => 'UserFlag',
289             'version' => STATUSNET_VERSION,
290             'author' => 'Evan Prodromou',
291             'homepage' => $url,
292             'rawdescription' =>
293             // TRANS: Plugin description.
294             _m('This plugin allows flagging of profiles for review and reviewing flagged profiles.'));
295
296         return true;
297     }
298 }