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