3 * StatusNet, the distributed open-source microblogging tool
5 * Allows users to flag content and accounts as offensive/spam/whatever
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.
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.
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/>.
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/
30 if (!defined('STATUSNET')) {
35 * Allows users to flag content and accounts as offensive/spam/whatever
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/
44 class UserFlagPlugin extends Plugin
46 const REVIEWFLAGS = 'UserFlagPlugin::reviewflags';
47 const CLEARFLAGS = 'UserFlagPlugin::clearflags';
49 public $flagOnBlock = true;
52 * Hook for ensuring our tables are created
54 * Ensures that the user_flag_profile table exists
55 * and has the right columns.
57 * @return boolean hook return
60 function onCheckSchema()
62 $schema = Schema::get();
64 // For storing user-submitted flags on profiles
66 $schema->ensureTable('user_flag_profile',
67 array(new ColumnDef('profile_id', 'integer', null,
69 new ColumnDef('user_id', 'integer', null,
71 new ColumnDef('created', 'datetime', null,
73 new ColumnDef('cleared', 'datetime', null,
80 * Add our actions to the URL router
82 * @param Net_URL_Mapper $m URL mapper for this hit
84 * @return boolean hook return
87 function onRouterInitialized($m)
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'));
96 * Auto-load our classes if called
98 * @param string $cls Class to load
100 * @return boolean hook return
103 function onAutoload($cls)
105 switch (strtolower($cls))
107 case 'flagprofileaction':
108 case 'adminprofileflagaction':
109 case 'clearflagaction':
110 include_once INSTALLDIR.'/plugins/UserFlag/' .
111 strtolower(mb_substr($cls, 0, -6)) . '.php';
113 case 'flagprofileform':
114 case 'clearflagform':
115 include_once INSTALLDIR.'/plugins/UserFlag/' . strtolower($cls . '.php');
117 case 'user_flag_profile':
118 include_once INSTALLDIR.'/plugins/UserFlag/'.ucfirst(strtolower($cls)).'.php';
126 * Add a 'flag' button to profile page
128 * @param Action &$action The action being called
129 * @param Profile $profile Profile being shown
131 * @return boolean hook result
134 function onEndProfilePageActionsElements(&$action, $profile)
136 $user = common_current_user();
138 if (!empty($user) && ($user->id != $profile->id)) {
140 $action->elementStart('li', 'entity_flag');
142 if (User_flag_profile::exists($profile->id, $user->id)) {
143 $action->element('p', 'flagged', _('Flagged'));
145 $form = new FlagProfileForm($action, $profile,
146 array('action' => 'showstream',
147 'nickname' => $profile->nickname));
151 $action->elementEnd('li');
158 * Add a 'flag' button to profiles in a list
160 * @param ProfileListItem $item item being shown
162 * @return boolean hook result
165 function onEndProfileListItemActionElements($item)
167 $user = common_current_user();
171 list($action, $args) = $item->action->returnToArgs();
173 $args['action'] = $action;
175 $form = new FlagProfileForm($item->action, $item->profile, $args);
177 $item->action->elementStart('li', 'entity_flag');
179 $item->action->elementEnd('li');
186 * Initialize any flagging buttons on the page
188 * @param Action $action action being shown
190 * @return boolean hook result
193 function onEndShowScripts($action)
195 $action->inlineScript('if ($(".form_entity_flag").length > 0) { '.
196 '$(".form_entity_flag").bind("click", function() {'.
197 'SN.U.FormXHR($(this)); return false; }); }');
202 * Check whether a user has one of our defined rights
204 * We define extra rights; this function checks to see if a
205 * user has one of them.
207 * @param User $user User being checked
208 * @param string $right Right we're checking
209 * @param boolean &$result out, result of the check
211 * @return boolean hook result
214 function onUserRightsCheck($user, $right, &$result)
217 case self::REVIEWFLAGS:
218 case self::CLEARFLAGS:
219 $result = $user->hasRole('moderator');
220 return false; // done processing!
223 return true; // unchanged!
227 * Optionally flag profile when a block happens
229 * We optionally add a flag when a profile has been blocked
231 * @param User $user User doing the block
232 * @param Profile $profile Profile being blocked
234 * @return boolean hook result
237 function onEndBlockProfile($user, $profile)
239 if ($this->flagOnBlock && !User_flag_profile::exists($profile->id,
242 User_flag_profile::create($user->id, $profile->id);
248 * Ensure that flag entries for a profile are deleted
249 * along with the profile when deleting users.
250 * This prevents breakage of the admin profile flag UI.
252 * @param Profile $profile
253 * @param array &$related list of related tables; entries
254 * with matching profile_id will be deleted.
256 * @return boolean hook result
259 function onProfileDeleteRelated($profile, &$related)
261 $related[] = 'user_flag_profile';
266 * Ensure that flag entries created by a user are deleted
267 * when that user gets deleted.
270 * @param array &$related list of related tables; entries
271 * with matching user_id will be deleted.
273 * @return boolean hook result
276 function onUserDeleteRelated($user, &$related)
278 $related[] = 'user_flag_profile';