6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
21 * Handling warning the users
29 function mcp_warn(&$p_master)
31 $this->p_master = &$p_master;
34 function main($id, $mode)
36 global $auth, $db, $user, $template;
37 global $config, $phpbb_root_path, $phpEx;
39 $action = request_var('action', array('' => ''));
41 if (is_array($action))
43 list($action, ) = each($action);
46 $this->page_title = 'MCP_WARN';
48 add_form_key('mcp_warn');
53 $this->mcp_warn_front_view();
54 $this->tpl_name = 'mcp_warn_front';
58 $this->mcp_warn_list_view($action);
59 $this->tpl_name = 'mcp_warn_list';
63 $this->mcp_warn_post_view($action);
64 $this->tpl_name = 'mcp_warn_post';
68 $this->mcp_warn_user_view($action);
69 $this->tpl_name = 'mcp_warn_user';
75 * Generates the summary on the main page of the warning module
77 function mcp_warn_front_view()
79 global $phpEx, $phpbb_root_path, $config;
80 global $template, $db, $user, $auth;
82 $template->assign_vars(array(
83 'U_FIND_USERNAME' => append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&form=mcp&field=username&select_single=true'),
84 'U_POST_ACTION' => append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=warn&mode=warn_user'),
87 // Obtain a list of the 5 naughtiest users....
88 // These are the 5 users with the highest warning count
92 view_warned_users($highest, $count, 5);
94 foreach ($highest as $row)
96 $template->assign_block_vars('highest', array(
97 'U_NOTES' => append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=notes&mode=user_notes&u=' . $row['user_id']),
99 'USERNAME_FULL' => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']),
100 'USERNAME' => $row['username'],
101 'USERNAME_COLOUR' => ($row['user_colour']) ? '#' . $row['user_colour'] : '',
102 'U_USER' => append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=viewprofile&u=' . $row['user_id']),
104 'WARNING_TIME' => $user->format_date($row['user_last_warning']),
105 'WARNINGS' => $row['user_warnings'],
109 // And now the 5 most recent users to get in trouble
110 $sql = 'SELECT u.user_id, u.username, u.username_clean, u.user_colour, u.user_warnings, w.warning_time
111 FROM ' . USERS_TABLE . ' u, ' . WARNINGS_TABLE . ' w
112 WHERE u.user_id = w.user_id
113 ORDER BY w.warning_time DESC';
114 $result = $db->sql_query_limit($sql, 5);
116 while ($row = $db->sql_fetchrow($result))
118 $template->assign_block_vars('latest', array(
119 'U_NOTES' => append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=notes&mode=user_notes&u=' . $row['user_id']),
121 'USERNAME_FULL' => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']),
122 'USERNAME' => $row['username'],
123 'USERNAME_COLOUR' => ($row['user_colour']) ? '#' . $row['user_colour'] : '',
124 'U_USER' => append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=viewprofile&u=' . $row['user_id']),
126 'WARNING_TIME' => $user->format_date($row['warning_time']),
127 'WARNINGS' => $row['user_warnings'],
130 $db->sql_freeresult($result);
134 * Lists all users with warnings
136 function mcp_warn_list_view($action)
138 global $phpEx, $phpbb_root_path, $config;
139 global $template, $db, $user, $auth;
141 $user->add_lang('memberlist');
143 $start = request_var('start', 0);
144 $st = request_var('st', 0);
145 $sk = request_var('sk', 'b');
146 $sd = request_var('sd', 'd');
148 $limit_days = array(0 => $user->lang['ALL_ENTRIES'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 365 => $user->lang['1_YEAR']);
149 $sort_by_text = array('a' => $user->lang['SORT_USERNAME'], 'b' => $user->lang['SORT_DATE'], 'c' => $user->lang['SORT_WARNINGS']);
150 $sort_by_sql = array('a' => 'username_clean', 'b' => 'user_last_warning', 'c' => 'user_warnings');
152 $s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
153 gen_sort_selects($limit_days, $sort_by_text, $st, $sk, $sd, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param);
155 // Define where and sort sql for use in displaying logs
156 $sql_where = ($st) ? (time() - ($st * 86400)) : 0;
157 $sql_sort = $sort_by_sql[$sk] . ' ' . (($sd == 'd') ? 'DESC' : 'ASC');
162 view_warned_users($users, $user_count, $config['topics_per_page'], $start, $sql_where, $sql_sort);
164 foreach ($users as $row)
166 $template->assign_block_vars('user', array(
167 'U_NOTES' => append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=notes&mode=user_notes&u=' . $row['user_id']),
169 'USERNAME_FULL' => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']),
170 'USERNAME' => $row['username'],
171 'USERNAME_COLOUR' => ($row['user_colour']) ? '#' . $row['user_colour'] : '',
172 'U_USER' => append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=viewprofile&u=' . $row['user_id']),
174 'WARNING_TIME' => $user->format_date($row['user_last_warning']),
175 'WARNINGS' => $row['user_warnings'],
179 $template->assign_vars(array(
180 'U_POST_ACTION' => $this->u_action,
181 'S_CLEAR_ALLOWED' => ($auth->acl_get('a_clearlogs')) ? true : false,
182 'S_SELECT_SORT_DIR' => $s_sort_dir,
183 'S_SELECT_SORT_KEY' => $s_sort_key,
184 'S_SELECT_SORT_DAYS' => $s_limit_days,
186 'PAGE_NUMBER' => on_page($user_count, $config['topics_per_page'], $start),
187 'PAGINATION' => generate_pagination(append_sid("{$phpbb_root_path}mcp.$phpEx", "i=warn&mode=list&st=$st&sk=$sk&sd=$sd"), $user_count, $config['topics_per_page'], $start),
188 'TOTAL_USERS' => ($user_count == 1) ? $user->lang['LIST_USER'] : sprintf($user->lang['LIST_USERS'], $user_count),
193 * Handles warning the user when the warning is for a specific post
195 function mcp_warn_post_view($action)
197 global $phpEx, $phpbb_root_path, $config;
198 global $template, $db, $user, $auth;
200 $post_id = request_var('p', 0);
201 $forum_id = request_var('f', 0);
202 $notify = (isset($_REQUEST['notify_user'])) ? true : false;
203 $warning = utf8_normalize_nfc(request_var('warning', '', true));
205 $sql = 'SELECT u.*, p.*
206 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . " u
207 WHERE p.post_id = $post_id
208 AND u.user_id = p.poster_id";
209 $result = $db->sql_query($sql);
210 $user_row = $db->sql_fetchrow($result);
211 $db->sql_freeresult($result);
215 trigger_error('NO_POST');
218 // There is no point issuing a warning to ignored users (ie anonymous and bots)
219 if ($user_row['user_type'] == USER_IGNORE)
221 trigger_error('CANNOT_WARN_ANONYMOUS');
224 // Prevent someone from warning themselves
225 if ($user_row['user_id'] == $user->data['user_id'])
227 trigger_error('CANNOT_WARN_SELF');
230 // Check if there is already a warning for this post to prevent multiple
231 // warnings for the same offence
232 $sql = 'SELECT post_id
233 FROM ' . WARNINGS_TABLE . "
234 WHERE post_id = $post_id";
235 $result = $db->sql_query($sql);
236 $row = $db->sql_fetchrow($result);
237 $db->sql_freeresult($result);
241 trigger_error('ALREADY_WARNED');
244 $user_id = $user_row['user_id'];
246 if (strpos($this->u_action, "&f=$forum_id&p=$post_id") === false)
248 $this->p_master->adjust_url("&f=$forum_id&p=$post_id");
249 $this->u_action .= "&f=$forum_id&p=$post_id";
252 // Check if can send a notification
253 if ($config['allow_privmsg'])
256 $auth2->acl($user_row);
257 $s_can_notify = ($auth2->acl_get('u_readpm')) ? true : false;
262 $s_can_notify = false;
265 // Prevent against clever people
266 if ($notify && !$s_can_notify)
271 if ($warning && $action == 'add_warning')
273 if (check_form_key('mcp_warn'))
275 add_warning($user_row, $warning, $notify, $post_id);
276 $msg = $user->lang['USER_WARNING_ADDED'];
280 $msg = $user->lang['FORM_INVALID'];
282 $redirect = append_sid("{$phpbb_root_path}mcp.$phpEx", "i=notes&mode=user_notes&u=$user_id");
283 meta_refresh(2, $redirect);
284 trigger_error($msg . '<br /><br />' . sprintf($user->lang['RETURN_PAGE'], '<a href="' . $redirect . '">', '</a>'));
287 // OK, they didn't submit a warning so lets build the page for them to do so
289 // We want to make the message available here as a reminder
290 // Parse the message and subject
291 $message = censor_text($user_row['post_text']);
293 // Second parse bbcode here
294 if ($user_row['bbcode_bitfield'])
296 include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx);
298 $bbcode = new bbcode($user_row['bbcode_bitfield']);
299 $bbcode->bbcode_second_pass($message, $user_row['bbcode_uid'], $user_row['bbcode_bitfield']);
302 $message = bbcode_nl2br($message);
303 $message = smiley_text($message);
305 // Generate the appropriate user information for the user we are looking at
306 if (!function_exists('get_user_avatar'))
308 include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
311 $rank_title = $rank_img = '';
312 $avatar_img = get_user_avatar($user_row['user_avatar'], $user_row['user_avatar_type'], $user_row['user_avatar_width'], $user_row['user_avatar_height']);
314 $template->assign_vars(array(
315 'U_POST_ACTION' => $this->u_action,
318 'USERNAME' => $user_row['username'],
319 'USER_COLOR' => (!empty($user_row['user_colour'])) ? $user_row['user_colour'] : '',
320 'RANK_TITLE' => $rank_title,
321 'JOINED' => $user->format_date($user_row['user_regdate']),
322 'POSTS' => ($user_row['user_posts']) ? $user_row['user_posts'] : 0,
323 'WARNINGS' => ($user_row['user_warnings']) ? $user_row['user_warnings'] : 0,
325 'AVATAR_IMG' => $avatar_img,
326 'RANK_IMG' => $rank_img,
328 'L_WARNING_POST_DEFAULT' => sprintf($user->lang['WARNING_POST_DEFAULT'], generate_board_url() . "/viewtopic.$phpEx?f=$forum_id&p=$post_id#p$post_id"),
330 'S_CAN_NOTIFY' => $s_can_notify,
335 * Handles warning the user
337 function mcp_warn_user_view($action)
339 global $phpEx, $phpbb_root_path, $config, $module;
340 global $template, $db, $user, $auth;
342 $user_id = request_var('u', 0);
343 $username = request_var('username', '', true);
344 $notify = (isset($_REQUEST['notify_user'])) ? true : false;
345 $warning = utf8_normalize_nfc(request_var('warning', '', true));
347 $sql_where = ($user_id) ? "user_id = $user_id" : "username_clean = '" . $db->sql_escape(utf8_clean_string($username)) . "'";
350 FROM ' . USERS_TABLE . '
351 WHERE ' . $sql_where;
352 $result = $db->sql_query($sql);
353 $user_row = $db->sql_fetchrow($result);
354 $db->sql_freeresult($result);
358 trigger_error('NO_USER');
361 // Prevent someone from warning themselves
362 if ($user_row['user_id'] == $user->data['user_id'])
364 trigger_error('CANNOT_WARN_SELF');
367 $user_id = $user_row['user_id'];
369 if (strpos($this->u_action, "&u=$user_id") === false)
371 $this->p_master->adjust_url('&u=' . $user_id);
372 $this->u_action .= "&u=$user_id";
375 // Check if can send a notification
376 if ($config['allow_privmsg'])
379 $auth2->acl($user_row);
380 $s_can_notify = ($auth2->acl_get('u_readpm')) ? true : false;
385 $s_can_notify = false;
388 // Prevent against clever people
389 if ($notify && !$s_can_notify)
394 if ($warning && $action == 'add_warning')
396 if (check_form_key('mcp_warn'))
398 add_warning($user_row, $warning, $notify);
399 $msg = $user->lang['USER_WARNING_ADDED'];
403 $msg = $user->lang['FORM_INVALID'];
405 $redirect = append_sid("{$phpbb_root_path}mcp.$phpEx", "i=notes&mode=user_notes&u=$user_id");
406 meta_refresh(2, $redirect);
407 trigger_error($msg . '<br /><br />' . sprintf($user->lang['RETURN_PAGE'], '<a href="' . $redirect . '">', '</a>'));
410 // Generate the appropriate user information for the user we are looking at
411 if (!function_exists('get_user_avatar'))
413 include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
416 $rank_title = $rank_img = '';
417 $avatar_img = get_user_avatar($user_row['user_avatar'], $user_row['user_avatar_type'], $user_row['user_avatar_width'], $user_row['user_avatar_height']);
419 // OK, they didn't submit a warning so lets build the page for them to do so
420 $template->assign_vars(array(
421 'U_POST_ACTION' => $this->u_action,
423 'RANK_TITLE' => $rank_title,
424 'JOINED' => $user->format_date($user_row['user_regdate']),
425 'POSTS' => ($user_row['user_posts']) ? $user_row['user_posts'] : 0,
426 'WARNINGS' => ($user_row['user_warnings']) ? $user_row['user_warnings'] : 0,
428 'USERNAME_FULL' => get_username_string('full', $user_row['user_id'], $user_row['username'], $user_row['user_colour']),
429 'USERNAME_COLOUR' => get_username_string('colour', $user_row['user_id'], $user_row['username'], $user_row['user_colour']),
430 'USERNAME' => get_username_string('username', $user_row['user_id'], $user_row['username'], $user_row['user_colour']),
431 'U_PROFILE' => get_username_string('profile', $user_row['user_id'], $user_row['username'], $user_row['user_colour']),
433 'AVATAR_IMG' => $avatar_img,
434 'RANK_IMG' => $rank_img,
436 'S_CAN_NOTIFY' => $s_can_notify,
444 * Insert the warning into the database
446 function add_warning($user_row, $warning, $send_pm = true, $post_id = 0)
448 global $phpEx, $phpbb_root_path, $config;
449 global $template, $db, $user, $auth;
453 include_once($phpbb_root_path . 'includes/functions_privmsgs.' . $phpEx);
454 include_once($phpbb_root_path . 'includes/message_parser.' . $phpEx);
456 $user_row['user_lang'] = (file_exists($phpbb_root_path . 'language/' . $user_row['user_lang'] . "/mcp.$phpEx")) ? $user_row['user_lang'] : $config['default_lang'];
457 include($phpbb_root_path . 'language/' . basename($user_row['user_lang']) . "/mcp.$phpEx");
459 $message_parser = new parse_message();
461 $message_parser->message = sprintf($lang['WARNING_PM_BODY'], $warning);
462 $message_parser->parse(true, true, true, false, false, true, true);
465 'from_user_id' => $user->data['user_id'],
466 'from_user_ip' => $user->ip,
467 'from_username' => $user->data['username'],
468 'enable_sig' => false,
469 'enable_bbcode' => true,
470 'enable_smilies' => true,
471 'enable_urls' => false,
473 'bbcode_bitfield' => $message_parser->bbcode_bitfield,
474 'bbcode_uid' => $message_parser->bbcode_uid,
475 'message' => $message_parser->message,
476 'address_list' => array('u' => array($user_row['user_id'] => 'to')),
479 submit_pm('post', $lang['WARNING_PM_SUBJECT'], $pm_data, false);
482 add_log('admin', 'LOG_USER_WARNING', $user_row['username']);
483 $log_id = add_log('user', $user_row['user_id'], 'LOG_USER_WARNING_BODY', $warning);
486 'user_id' => $user_row['user_id'],
487 'post_id' => $post_id,
489 'warning_time' => time(),
492 $db->sql_query('INSERT INTO ' . WARNINGS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
494 $sql = 'UPDATE ' . USERS_TABLE . '
495 SET user_warnings = user_warnings + 1,
496 user_last_warning = ' . time() . '
497 WHERE user_id = ' . $user_row['user_id'];
498 $db->sql_query($sql);
500 // We add this to the mod log too for moderators to see that a specific user got warned.
501 $sql = 'SELECT forum_id, topic_id
502 FROM ' . POSTS_TABLE . '
503 WHERE post_id = ' . $post_id;
504 $result = $db->sql_query($sql);
505 $row = $db->sql_fetchrow($result);
506 $db->sql_freeresult($result);
508 add_log('mod', $row['forum_id'], $row['topic_id'], 'LOG_USER_WARNING', $user_row['username']);