]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/UserFlag/UserFlagPlugin.php
[ROUTES] Allow accept-header specification during router creation
[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 PLUGIN_VERSION = '2.0.0';
46
47     const REVIEWFLAGS = 'UserFlagPlugin::reviewflags';
48     const CLEARFLAGS  = 'UserFlagPlugin::clearflags';
49
50     public $flagOnBlock = true;
51
52     /**
53      * Hook for ensuring our tables are created
54      *
55      * Ensures that the user_flag_profile table exists
56      * and has the right columns.
57      *
58      * @return boolean hook return
59      */
60     function onCheckSchema()
61     {
62         $schema = Schema::get();
63
64         // For storing user-submitted flags on profiles
65         $schema->ensureTable('user_flag_profile', User_flag_profile::schemaDef());
66         return true;
67     }
68
69     /**
70      * Add our actions to the URL router
71      *
72      * @param URLMapper $m URL mapper for this hit
73      *
74      * @return boolean hook return
75      */
76     public function onRouterInitialized(URLMapper $m)
77     {
78         $m->connect('main/flag/profile', ['action' => 'flagprofile']);
79         $m->connect('main/flag/clear', ['action' => 'clearflag']);
80         $m->connect('panel/profile/flag', ['action' => 'adminprofileflag']);
81         return true;
82     }
83
84     /**
85      * Add a 'flag' button to profile page
86      *
87      * @param Action  $action The action being called
88      * @param Profile $profile Profile being shown
89      *
90      * @return boolean hook result
91      */
92     function onEndProfilePageActionsElements($action, $profile)
93     {
94         $this->showFlagButton($action, $profile,
95                               array('action' => 'showstream',
96                                     'nickname' => $profile->nickname));
97
98         return true;
99     }
100
101     /**
102      * Add a 'flag' button to profiles in a list
103      *
104      * @param ProfileListItem $item item being shown
105      *
106      * @return boolean hook result
107      */
108     function onEndProfileListItemActionElements($item)
109     {
110         list($action, $args) = $item->action->returnToArgs();
111         $args['action'] = $action;
112         $this->showFlagButton($item->action, $item->profile, $args);
113
114         return true;
115     }
116
117     /**
118      * Actually output a flag button. If the target profile has already been
119      * flagged by the current user, a null-action faux button is shown.
120      *
121      * @param Action $action
122      * @param Profile $profile
123      * @param array $returnToArgs
124      */
125     protected function showFlagButton($action, $profile, $returnToArgs)
126     {
127         $user = common_current_user();
128
129         if (!empty($user) && ($user->id != $profile->id)) {
130
131             $action->elementStart('li', 'entity_flag');
132
133             if (User_flag_profile::exists($profile->id, $user->id)) {
134                 // @todo FIXME: Add a title explaining what 'flagged' means?
135                 // TRANS: Message added to a profile if it has been flagged for review.
136                 $action->element('p', 'flagged', _m('Flagged'));
137             } else {
138                 $form = new FlagProfileForm($action, $profile, $returnToArgs);
139                 $form->show();
140             }
141
142             $action->elementEnd('li');
143         }
144     }
145
146     /**
147      * Check whether a user has one of our defined rights
148      *
149      * We define extra rights; this function checks to see if a
150      * user has one of them.
151      *
152      * @param User    $user    User being checked
153      * @param string  $right   Right we're checking
154      * @param boolean &$result out, result of the check
155      *
156      * @return boolean hook result
157      */
158     function onUserRightsCheck($user, $right, &$result)
159     {
160         switch ($right) {
161         case self::REVIEWFLAGS:
162         case self::CLEARFLAGS:
163             $result = $user->hasRole('moderator');
164             return false; // done processing!
165         }
166
167         return true; // unchanged!
168     }
169
170     /**
171      * Optionally flag profile when a block happens
172      *
173      * We optionally add a flag when a profile has been blocked
174      *
175      * @param User    $user    User doing the block
176      * @param Profile $profile Profile being blocked
177      *
178      * @return boolean hook result
179      */
180     function onEndBlockProfile($user, $profile)
181     {
182         if ($this->flagOnBlock && !User_flag_profile::exists($profile->id,
183                                                              $user->id)) {
184
185             User_flag_profile::create($user->id, $profile->id);
186         }
187         return true;
188     }
189
190     /**
191      * Ensure that flag entries for a profile are deleted
192      * along with the profile when deleting users.
193      * This prevents breakage of the admin profile flag UI.
194      *
195      * @param Profile $profile
196      * @param array &$related list of related tables; entries
197      *              with matching profile_id will be deleted.
198      *
199      * @return boolean hook result
200      */
201     function onProfileDeleteRelated($profile, &$related)
202     {
203         $related[] = 'user_flag_profile';
204         return true;
205     }
206
207     /**
208      * Ensure that flag entries created by a user are deleted
209      * when that user gets deleted.
210      *
211      * @param User $user
212      * @param array &$related list of related tables; entries
213      *              with matching user_id will be deleted.
214      *
215      * @return boolean hook result
216      */
217     function onUserDeleteRelated($user, &$related)
218     {
219         $related[] = 'user_flag_profile';
220         return true;
221     }
222
223     /**
224      * Provide plugin version information.
225      *
226      * This data is used when showing the version page.
227      *
228      * @param array &$versions array of version data arrays; see EVENTS.txt
229      *
230      * @return boolean hook value
231      */
232     function onPluginVersion(array &$versions)
233     {
234         $url = 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/UserFlag';
235
236         $versions[] = array('name' => 'UserFlag',
237             'version' => self::PLUGIN_VERSION,
238             'author' => 'Evan Prodromou',
239             'homepage' => $url,
240             'rawdescription' =>
241             // TRANS: Plugin description.
242             _m('This plugin allows flagging of profiles for review and reviewing flagged profiles.'));
243
244         return true;
245     }
246 }