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/
43 class UserFlagPlugin extends Plugin
45 const REVIEWFLAGS = 'UserFlagPlugin::reviewflags';
46 const CLEARFLAGS = 'UserFlagPlugin::clearflags';
48 public $flagOnBlock = true;
51 * Hook for ensuring our tables are created
53 * Ensures that the user_flag_profile table exists
54 * and has the right columns.
56 * @return boolean hook return
58 function onCheckSchema()
60 $schema = Schema::get();
62 // For storing user-submitted flags on profiles
64 $schema->ensureTable('user_flag_profile',
65 array(new ColumnDef('profile_id', 'integer', null,
67 new ColumnDef('user_id', 'integer', null,
69 new ColumnDef('created', 'datetime', null,
71 new ColumnDef('cleared', 'datetime', null,
78 * Add our actions to the URL router
80 * @param Net_URL_Mapper $m URL mapper for this hit
82 * @return boolean hook return
84 function onRouterInitialized($m)
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'));
93 * Auto-load our classes if called
95 * @param string $cls Class to load
97 * @return boolean hook return
99 function onAutoload($cls)
101 switch (strtolower($cls))
103 case 'flagprofileaction':
104 case 'adminprofileflagaction':
105 case 'clearflagaction':
106 include_once INSTALLDIR.'/plugins/UserFlag/' .
107 strtolower(mb_substr($cls, 0, -6)) . '.php';
109 case 'flagprofileform':
110 case 'clearflagform':
111 include_once INSTALLDIR.'/plugins/UserFlag/' . strtolower($cls . '.php');
113 case 'user_flag_profile':
114 include_once INSTALLDIR.'/plugins/UserFlag/'.ucfirst(strtolower($cls)).'.php';
122 * Add a 'flag' button to profile page
124 * @param Action &$action The action being called
125 * @param Profile $profile Profile being shown
127 * @return boolean hook result
129 function onEndProfilePageActionsElements(&$action, $profile)
131 $user = common_current_user();
133 if (!empty($user) && ($user->id != $profile->id)) {
135 $action->elementStart('li', 'entity_flag');
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'));
142 $form = new FlagProfileForm($action, $profile,
143 array('action' => 'showstream',
144 'nickname' => $profile->nickname));
148 $action->elementEnd('li');
155 * Add a 'flag' button to profiles in a list
157 * @param ProfileListItem $item item being shown
159 * @return boolean hook result
161 function onEndProfileListItemActionElements($item)
163 $user = common_current_user();
167 list($action, $args) = $item->action->returnToArgs();
169 $args['action'] = $action;
171 $form = new FlagProfileForm($item->action, $item->profile, $args);
173 $item->action->elementStart('li', 'entity_flag');
175 $item->action->elementEnd('li');
182 * Initialize any flagging buttons on the page
184 * @param Action $action action being shown
186 * @return boolean hook result
188 function onEndShowScripts($action)
190 $action->inlineScript('if ($(".form_entity_flag").length > 0) { '.
191 '$(".form_entity_flag").bind("click", function() {'.
192 'SN.U.FormXHR($(this)); return false; }); }');
197 * Check whether a user has one of our defined rights
199 * We define extra rights; this function checks to see if a
200 * user has one of them.
202 * @param User $user User being checked
203 * @param string $right Right we're checking
204 * @param boolean &$result out, result of the check
206 * @return boolean hook result
208 function onUserRightsCheck($user, $right, &$result)
211 case self::REVIEWFLAGS:
212 case self::CLEARFLAGS:
213 $result = $user->hasRole('moderator');
214 return false; // done processing!
217 return true; // unchanged!
221 * Optionally flag profile when a block happens
223 * We optionally add a flag when a profile has been blocked
225 * @param User $user User doing the block
226 * @param Profile $profile Profile being blocked
228 * @return boolean hook result
230 function onEndBlockProfile($user, $profile)
232 if ($this->flagOnBlock && !User_flag_profile::exists($profile->id,
235 User_flag_profile::create($user->id, $profile->id);
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.
245 * @param Profile $profile
246 * @param array &$related list of related tables; entries
247 * with matching profile_id will be deleted.
249 * @return boolean hook result
251 function onProfileDeleteRelated($profile, &$related)
253 $related[] = 'user_flag_profile';
258 * Ensure that flag entries created by a user are deleted
259 * when that user gets deleted.
262 * @param array &$related list of related tables; entries
263 * with matching user_id will be deleted.
265 * @return boolean hook result
267 function onUserDeleteRelated($user, &$related)
269 $related[] = 'user_flag_profile';
274 * Provide plugin version information.
276 * This data is used when showing the version page.
278 * @param array &$versions array of version data arrays; see EVENTS.txt
280 * @return boolean hook value
282 function onPluginVersion(&$versions)
284 $url = 'http://status.net/wiki/Plugin:UserFlag';
286 $versions[] = array('name' => 'UserFlag',
287 'version' => STATUSNET_VERSION,
288 'author' => 'Evan Prodromou',
291 // TRANS: Plugin description.
292 _m('This plugin allows flagging of profiles for review and reviewing flagged profiles.'));