| Conditions | 49 |
| Paths | > 20000 |
| Total Lines | 370 |
| Code Lines | 204 |
| Lines | 25 |
| Ratio | 6.76 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 67 | function issueWarning($memID) |
||
| 68 | { |
||
| 69 | global $txt, $scripturl, $modSettings, $user_info, $mbname; |
||
| 70 | global $context, $cur_profile, $smcFunc, $sourcedir; |
||
| 71 | |||
| 72 | // Get all the actual settings. |
||
| 73 | list ($modSettings['warning_enable'], $modSettings['user_limit']) = explode(',', $modSettings['warning_settings']); |
||
| 74 | |||
| 75 | // This stores any legitimate errors. |
||
| 76 | $issueErrors = array(); |
||
| 77 | |||
| 78 | // Doesn't hurt to be overly cautious. |
||
| 79 | if (empty($modSettings['warning_enable']) || ($context['user']['is_owner'] && !$cur_profile['warning']) || !allowedTo('issue_warning')) |
||
| 80 | fatal_lang_error('no_access', false); |
||
| 81 | |||
| 82 | // Get the base (errors related) stuff done. |
||
| 83 | loadLanguage('Errors'); |
||
| 84 | $context['custom_error_title'] = $txt['profile_warning_errors_occured']; |
||
| 85 | |||
| 86 | // Make sure things which are disabled stay disabled. |
||
| 87 | $modSettings['warning_watch'] = !empty($modSettings['warning_watch']) ? $modSettings['warning_watch'] : 110; |
||
| 88 | $modSettings['warning_moderate'] = !empty($modSettings['warning_moderate']) && !empty($modSettings['postmod_active']) ? $modSettings['warning_moderate'] : 110; |
||
| 89 | $modSettings['warning_mute'] = !empty($modSettings['warning_mute']) ? $modSettings['warning_mute'] : 110; |
||
| 90 | |||
| 91 | $context['warning_limit'] = allowedTo('admin_forum') ? 0 : $modSettings['user_limit']; |
||
| 92 | $context['member']['warning'] = $cur_profile['warning']; |
||
| 93 | $context['member']['name'] = $cur_profile['real_name']; |
||
| 94 | |||
| 95 | // What are the limits we can apply? |
||
| 96 | $context['min_allowed'] = 0; |
||
| 97 | $context['max_allowed'] = 100; |
||
| 98 | if ($context['warning_limit'] > 0) |
||
| 99 | { |
||
| 100 | // Make sure we cannot go outside of our limit for the day. |
||
| 101 | $request = $smcFunc['db_query']('', ' |
||
| 102 | SELECT SUM(counter) |
||
| 103 | FROM {db_prefix}log_comments |
||
| 104 | WHERE id_recipient = {int:selected_member} |
||
| 105 | AND id_member = {int:current_member} |
||
| 106 | AND comment_type = {string:warning} |
||
| 107 | AND log_time > {int:day_time_period}', |
||
| 108 | array( |
||
| 109 | 'current_member' => $user_info['id'], |
||
| 110 | 'selected_member' => $memID, |
||
| 111 | 'day_time_period' => time() - 86400, |
||
| 112 | 'warning' => 'warning', |
||
| 113 | ) |
||
| 114 | ); |
||
| 115 | list ($current_applied) = $smcFunc['db_fetch_row']($request); |
||
| 116 | $smcFunc['db_free_result']($request); |
||
| 117 | |||
| 118 | $context['min_allowed'] = max(0, $cur_profile['warning'] - $current_applied - $context['warning_limit']); |
||
| 119 | $context['max_allowed'] = min(100, $cur_profile['warning'] - $current_applied + $context['warning_limit']); |
||
| 120 | } |
||
| 121 | |||
| 122 | // Defaults. |
||
| 123 | $context['warning_data'] = array( |
||
| 124 | 'reason' => '', |
||
| 125 | 'notify' => '', |
||
| 126 | 'notify_subject' => '', |
||
| 127 | 'notify_body' => '', |
||
| 128 | ); |
||
| 129 | |||
| 130 | // Are we saving? |
||
| 131 | if (isset($_POST['save'])) |
||
| 132 | { |
||
| 133 | // Security is good here. |
||
| 134 | checkSession(); |
||
| 135 | |||
| 136 | // This cannot be empty! |
||
| 137 | $_POST['warn_reason'] = isset($_POST['warn_reason']) ? trim($_POST['warn_reason']) : ''; |
||
| 138 | if ($_POST['warn_reason'] == '' && !$context['user']['is_owner']) |
||
| 139 | $issueErrors[] = 'warning_no_reason'; |
||
| 140 | $_POST['warn_reason'] = $smcFunc['htmlspecialchars']($_POST['warn_reason']); |
||
| 141 | |||
| 142 | $_POST['warning_level'] = (int) $_POST['warning_level']; |
||
| 143 | $_POST['warning_level'] = max(0, min(100, $_POST['warning_level'])); |
||
| 144 | if ($_POST['warning_level'] < $context['min_allowed']) |
||
| 145 | $_POST['warning_level'] = $context['min_allowed']; |
||
| 146 | elseif ($_POST['warning_level'] > $context['max_allowed']) |
||
| 147 | $_POST['warning_level'] = $context['max_allowed']; |
||
| 148 | |||
| 149 | // Do we actually have to issue them with a PM? |
||
| 150 | $id_notice = 0; |
||
| 151 | if (!empty($_POST['warn_notify']) && empty($issueErrors)) |
||
| 152 | { |
||
| 153 | $_POST['warn_sub'] = trim($_POST['warn_sub']); |
||
| 154 | $_POST['warn_body'] = trim($_POST['warn_body']); |
||
| 155 | if (empty($_POST['warn_sub']) || empty($_POST['warn_body'])) |
||
| 156 | $issueErrors[] = 'warning_notify_blank'; |
||
| 157 | // Send the PM? |
||
| 158 | else |
||
| 159 | { |
||
| 160 | require_once($sourcedir . '/Subs-Post.php'); |
||
| 161 | $from = array( |
||
| 162 | 'id' => 0, |
||
| 163 | 'name' => $context['forum_name_html_safe'], |
||
| 164 | 'username' => $context['forum_name_html_safe'], |
||
| 165 | ); |
||
| 166 | sendpm(array('to' => array($memID), 'bcc' => array()), $_POST['warn_sub'], $_POST['warn_body'], false, $from); |
||
| 167 | |||
| 168 | // Log the notice! |
||
| 169 | $id_notice = $smcFunc['db_insert']('', |
||
| 170 | '{db_prefix}log_member_notices', |
||
| 171 | array( |
||
| 172 | 'subject' => 'string-255', 'body' => 'string-65534', |
||
| 173 | ), |
||
| 174 | array( |
||
| 175 | $smcFunc['htmlspecialchars']($_POST['warn_sub']), $smcFunc['htmlspecialchars']($_POST['warn_body']), |
||
| 176 | ), |
||
| 177 | array('id_notice'), |
||
| 178 | 1 |
||
| 179 | ); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | // Just in case - make sure notice is valid! |
||
| 184 | $id_notice = (int) $id_notice; |
||
| 185 | |||
| 186 | // What have we changed? |
||
| 187 | $level_change = $_POST['warning_level'] - $cur_profile['warning']; |
||
| 188 | |||
| 189 | // No errors? Proceed! Only log if you're not the owner. |
||
| 190 | if (empty($issueErrors)) |
||
| 191 | { |
||
| 192 | // Log what we've done! |
||
| 193 | if (!$context['user']['is_owner']) |
||
| 194 | $smcFunc['db_insert']('', |
||
| 195 | '{db_prefix}log_comments', |
||
| 196 | array( |
||
| 197 | 'id_member' => 'int', 'member_name' => 'string', 'comment_type' => 'string', 'id_recipient' => 'int', 'recipient_name' => 'string-255', |
||
| 198 | 'log_time' => 'int', 'id_notice' => 'int', 'counter' => 'int', 'body' => 'string-65534', |
||
| 199 | ), |
||
| 200 | array( |
||
| 201 | $user_info['id'], $user_info['name'], 'warning', $memID, $cur_profile['real_name'], |
||
| 202 | time(), $id_notice, $level_change, $_POST['warn_reason'], |
||
| 203 | ), |
||
| 204 | array('id_comment') |
||
| 205 | ); |
||
| 206 | |||
| 207 | // Make the change. |
||
| 208 | updateMemberData($memID, array('warning' => $_POST['warning_level'])); |
||
| 209 | |||
| 210 | // Leave a lovely message. |
||
| 211 | $context['profile_updated'] = $context['user']['is_owner'] ? $txt['profile_updated_own'] : $txt['profile_warning_success']; |
||
| 212 | } |
||
| 213 | else |
||
| 214 | { |
||
| 215 | // Try to remember some bits. |
||
| 216 | $context['warning_data'] = array( |
||
| 217 | 'reason' => $_POST['warn_reason'], |
||
| 218 | 'notify' => !empty($_POST['warn_notify']), |
||
| 219 | 'notify_subject' => isset($_POST['warn_sub']) ? $_POST['warn_sub'] : '', |
||
| 220 | 'notify_body' => isset($_POST['warn_body']) ? $_POST['warn_body'] : '', |
||
| 221 | ); |
||
| 222 | } |
||
| 223 | |||
| 224 | // Show the new improved warning level. |
||
| 225 | $context['member']['warning'] = $_POST['warning_level']; |
||
| 226 | } |
||
| 227 | |||
| 228 | if (isset($_POST['preview'])) |
||
| 229 | { |
||
| 230 | $warning_body = !empty($_POST['warn_body']) ? trim(censorText($_POST['warn_body'])) : ''; |
||
| 231 | $context['preview_subject'] = !empty($_POST['warn_sub']) ? trim($smcFunc['htmlspecialchars']($_POST['warn_sub'])) : ''; |
||
| 232 | if (empty($_POST['warn_sub']) || empty($_POST['warn_body'])) |
||
| 233 | $issueErrors[] = 'warning_notify_blank'; |
||
| 234 | |||
| 235 | View Code Duplication | if (!empty($_POST['warn_body'])) |
|
|
|
|||
| 236 | { |
||
| 237 | require_once($sourcedir . '/Subs-Post.php'); |
||
| 238 | |||
| 239 | preparsecode($warning_body); |
||
| 240 | $warning_body = parse_bbc($warning_body, true); |
||
| 241 | } |
||
| 242 | |||
| 243 | // Try to remember some bits. |
||
| 244 | $context['warning_data'] = array( |
||
| 245 | 'reason' => $_POST['warn_reason'], |
||
| 246 | 'notify' => !empty($_POST['warn_notify']), |
||
| 247 | 'notify_subject' => isset($_POST['warn_sub']) ? $_POST['warn_sub'] : '', |
||
| 248 | 'notify_body' => isset($_POST['warn_body']) ? $_POST['warn_body'] : '', |
||
| 249 | 'body_preview' => $warning_body, |
||
| 250 | ); |
||
| 251 | } |
||
| 252 | |||
| 253 | if (!empty($issueErrors)) |
||
| 254 | { |
||
| 255 | // Fill in the suite of errors. |
||
| 256 | $context['post_errors'] = array(); |
||
| 257 | foreach ($issueErrors as $error) |
||
| 258 | $context['post_errors'][] = $txt[$error]; |
||
| 259 | } |
||
| 260 | |||
| 261 | |||
| 262 | $context['page_title'] = $txt['profile_issue_warning']; |
||
| 263 | |||
| 264 | // Let's use a generic list to get all the current warnings |
||
| 265 | require_once($sourcedir . '/Subs-List.php'); |
||
| 266 | |||
| 267 | // Work our the various levels. |
||
| 268 | $context['level_effects'] = array( |
||
| 269 | 0 => $txt['profile_warning_effect_none'], |
||
| 270 | $modSettings['warning_watch'] => $txt['profile_warning_effect_watch'], |
||
| 271 | $modSettings['warning_moderate'] => $txt['profile_warning_effect_moderation'], |
||
| 272 | $modSettings['warning_mute'] => $txt['profile_warning_effect_mute'], |
||
| 273 | ); |
||
| 274 | $context['current_level'] = 0; |
||
| 275 | View Code Duplication | foreach ($context['level_effects'] as $limit => $dummy) |
|
| 276 | if ($context['member']['warning'] >= $limit) |
||
| 277 | $context['current_level'] = $limit; |
||
| 278 | |||
| 279 | $listOptions = array( |
||
| 280 | 'id' => 'view_warnings', |
||
| 281 | 'title' => $txt['profile_viewwarning_previous_warnings'], |
||
| 282 | 'items_per_page' => $modSettings['defaultMaxListItems'], |
||
| 283 | 'no_items_label' => $txt['profile_viewwarning_no_warnings'], |
||
| 284 | 'base_href' => $scripturl . '?action=profile;area=issuewarning;sa=user;u=' . $memID, |
||
| 285 | 'default_sort_col' => 'log_time', |
||
| 286 | 'get_items' => array( |
||
| 287 | 'function' => 'list_getUserWarnings', |
||
| 288 | 'params' => array( |
||
| 289 | $memID, |
||
| 290 | ), |
||
| 291 | ), |
||
| 292 | 'get_count' => array( |
||
| 293 | 'function' => 'list_getUserWarningCount', |
||
| 294 | 'params' => array( |
||
| 295 | $memID, |
||
| 296 | ), |
||
| 297 | ), |
||
| 298 | 'columns' => array( |
||
| 299 | 'issued_by' => array( |
||
| 300 | 'header' => array( |
||
| 301 | 'value' => $txt['profile_warning_previous_issued'], |
||
| 302 | 'style' => 'width: 20%;', |
||
| 303 | ), |
||
| 304 | 'data' => array( |
||
| 305 | 'function' => function($warning) |
||
| 306 | { |
||
| 307 | return $warning['issuer']['link']; |
||
| 308 | }, |
||
| 309 | ), |
||
| 310 | 'sort' => array( |
||
| 311 | 'default' => 'lc.member_name DESC', |
||
| 312 | 'reverse' => 'lc.member_name', |
||
| 313 | ), |
||
| 314 | ), |
||
| 315 | 'log_time' => array( |
||
| 316 | 'header' => array( |
||
| 317 | 'value' => $txt['profile_warning_previous_time'], |
||
| 318 | 'style' => 'width: 30%;', |
||
| 319 | ), |
||
| 320 | 'data' => array( |
||
| 321 | 'db' => 'time', |
||
| 322 | ), |
||
| 323 | 'sort' => array( |
||
| 324 | 'default' => 'lc.log_time DESC', |
||
| 325 | 'reverse' => 'lc.log_time', |
||
| 326 | ), |
||
| 327 | ), |
||
| 328 | 'reason' => array( |
||
| 329 | 'header' => array( |
||
| 330 | 'value' => $txt['profile_warning_previous_reason'], |
||
| 331 | ), |
||
| 332 | 'data' => array( |
||
| 333 | View Code Duplication | 'function' => function($warning) use ($scripturl, $txt) |
|
| 334 | { |
||
| 335 | $ret = ' |
||
| 336 | <div class="floatleft"> |
||
| 337 | ' . $warning['reason'] . ' |
||
| 338 | </div>'; |
||
| 339 | |||
| 340 | if (!empty($warning['id_notice'])) |
||
| 341 | $ret .= ' |
||
| 342 | <div class="floatright"> |
||
| 343 | <a href="' . $scripturl . '?action=moderate;area=notice;nid=' . $warning['id_notice'] . '" onclick="window.open(this.href, \'\', \'scrollbars=yes,resizable=yes,width=400,height=250\');return false;" target="_blank" class="new_win" title="' . $txt['profile_warning_previous_notice'] . '"><span class="generic_icons filter centericon"></span></a> |
||
| 344 | </div>'; |
||
| 345 | |||
| 346 | return $ret; |
||
| 347 | }, |
||
| 348 | ), |
||
| 349 | ), |
||
| 350 | 'level' => array( |
||
| 351 | 'header' => array( |
||
| 352 | 'value' => $txt['profile_warning_previous_level'], |
||
| 353 | 'style' => 'width: 6%;', |
||
| 354 | ), |
||
| 355 | 'data' => array( |
||
| 356 | 'db' => 'counter', |
||
| 357 | ), |
||
| 358 | 'sort' => array( |
||
| 359 | 'default' => 'lc.counter DESC', |
||
| 360 | 'reverse' => 'lc.counter', |
||
| 361 | ), |
||
| 362 | ), |
||
| 363 | ), |
||
| 364 | ); |
||
| 365 | |||
| 366 | // Create the list for viewing. |
||
| 367 | require_once($sourcedir . '/Subs-List.php'); |
||
| 368 | createList($listOptions); |
||
| 369 | |||
| 370 | // Are they warning because of a message? |
||
| 371 | if (isset($_REQUEST['msg']) && 0 < (int) $_REQUEST['msg']) |
||
| 372 | { |
||
| 373 | $request = $smcFunc['db_query']('', ' |
||
| 374 | SELECT subject |
||
| 375 | FROM {db_prefix}messages AS m |
||
| 376 | INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board) |
||
| 377 | WHERE id_msg = {int:message} |
||
| 378 | AND {query_see_board} |
||
| 379 | LIMIT 1', |
||
| 380 | array( |
||
| 381 | 'message' => (int) $_REQUEST['msg'], |
||
| 382 | ) |
||
| 383 | ); |
||
| 384 | if ($smcFunc['db_num_rows']($request) != 0) |
||
| 385 | { |
||
| 386 | $context['warning_for_message'] = (int) $_REQUEST['msg']; |
||
| 387 | list ($context['warned_message_subject']) = $smcFunc['db_fetch_row']($request); |
||
| 388 | } |
||
| 389 | $smcFunc['db_free_result']($request); |
||
| 390 | |||
| 391 | } |
||
| 392 | |||
| 393 | // Didn't find the message? |
||
| 394 | if (empty($context['warning_for_message'])) |
||
| 395 | { |
||
| 396 | $context['warning_for_message'] = 0; |
||
| 397 | $context['warned_message_subject'] = ''; |
||
| 398 | } |
||
| 399 | |||
| 400 | // Any custom templates? |
||
| 401 | $context['notification_templates'] = array(); |
||
| 402 | |||
| 403 | $request = $smcFunc['db_query']('', ' |
||
| 404 | SELECT recipient_name AS template_title, body |
||
| 405 | FROM {db_prefix}log_comments |
||
| 406 | WHERE comment_type = {literal:warntpl} |
||
| 407 | AND (id_recipient = {int:generic} OR id_recipient = {int:current_member})', |
||
| 408 | array( |
||
| 409 | 'generic' => 0, |
||
| 410 | 'current_member' => $user_info['id'], |
||
| 411 | ) |
||
| 412 | ); |
||
| 413 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 414 | { |
||
| 415 | // If we're not warning for a message skip any that are. |
||
| 416 | if (!$context['warning_for_message'] && strpos($row['body'], '{MESSAGE}') !== false) |
||
| 417 | continue; |
||
| 418 | |||
| 419 | $context['notification_templates'][] = array( |
||
| 420 | 'title' => $row['template_title'], |
||
| 421 | 'body' => $row['body'], |
||
| 422 | ); |
||
| 423 | } |
||
| 424 | $smcFunc['db_free_result']($request); |
||
| 425 | |||
| 426 | // Setup the "default" templates. |
||
| 427 | foreach (array('spamming', 'offence', 'insulting') as $type) |
||
| 428 | $context['notification_templates'][] = array( |
||
| 429 | 'title' => $txt['profile_warning_notify_title_' . $type], |
||
| 430 | 'body' => sprintf($txt['profile_warning_notify_template_outline' . (!empty($context['warning_for_message']) ? '_post' : '')], $txt['profile_warning_notify_for_' . $type]), |
||
| 431 | ); |
||
| 432 | |||
| 433 | // Replace all the common variables in the templates. |
||
| 434 | foreach ($context['notification_templates'] as $k => $name) |
||
| 435 | $context['notification_templates'][$k]['body'] = strtr($name['body'], array('{MEMBER}' => un_htmlspecialchars($context['member']['name']), '{MESSAGE}' => '[url=' . $scripturl . '?msg=' . $context['warning_for_message'] . ']' . un_htmlspecialchars($context['warned_message_subject']) . '[/url]', '{SCRIPTURL}' => $scripturl, '{FORUMNAME}' => $mbname, '{REGARDS}' => $txt['regards_team'])); |
||
| 436 | } |
||
| 437 | |||
| 974 | ?> |
||
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.