| Conditions | 67 |
| Paths | > 20000 |
| Total Lines | 224 |
| Code Lines | 170 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 345 | protected function _chatAddModel() { |
||
| 346 | defined('IN_AJAX') or define('IN_AJAX', true); |
||
| 347 | |||
| 348 | global $config, $user, $lang; |
||
| 349 | // $chat_commands = get_unit_param(P_CHAT, P_CHAT_COMMANDS); |
||
| 350 | // $chat_aliases = get_unit_param(P_CHAT, P_CHAT_ALIASES); |
||
| 351 | $chat_commands = &$this->_chat_commands; |
||
| 352 | $chat_aliases = &$this->_chat_aliases; |
||
| 353 | |||
| 354 | if ($config->getMode() != classCache::CACHER_NO_CACHE && $config->chat_timeout && SN_TIME_MICRO - $config->array_get('users', $user['id'], 'chat_last_activity') > $config->chat_timeout) { |
||
| 355 | die(); |
||
| 356 | } |
||
| 357 | |||
| 358 | if (($message = sys_get_param_str_unsafe('message')) && $user['username']) { |
||
| 359 | $this->update_chat_activity(); |
||
| 360 | |||
| 361 | $chat_message_sender_id = 'NULL'; |
||
| 362 | $chat_message_sender_name = ''; |
||
| 363 | $chat_message_recipient_id = $user['id']; |
||
| 364 | $chat_message_recipient_name = db_escape($user['username']); |
||
| 365 | $nick = ''; |
||
| 366 | $ally_id = 0; |
||
| 367 | $chat_command_issued = ''; |
||
| 368 | |||
| 369 | $chat_player_row = $this->sn_chat_advanced_get_chat_player_record($user['id']); |
||
| 370 | $chat_player_muted = $chat_player_row['chat_player_muted'] && $chat_player_row['chat_player_muted'] >= SN_TIME_NOW ? $chat_player_row['chat_player_muted'] : false; |
||
| 371 | if (preg_match("#^\/([\w\?]+)\s*({on|off|ID\s*[0-9]+|[а-яёА-ЯЁa-zA-Z0-9\_\-\[\]\(\)\+\{\}]+|\".+\"}*)*\s*(.*)#iu", $message, $chat_command_parsed)) { |
||
| 372 | $chat_command_exists = array_key_exists(strtolower($chat_command_parsed[1]), $chat_aliases) ? true : strtolower($chat_command_parsed[1]); |
||
| 373 | $chat_command_issued = $chat_command_exists === true ? $chat_aliases[$chat_command_parsed[1]] : 'help'; |
||
| 374 | if ($chat_command_accessible = in_array($user['authlevel'], $chat_commands[$chat_command_issued]['access'])) { |
||
| 375 | switch ($chat_command_issued) { |
||
| 376 | case 'invisible': |
||
| 377 | //$chat_player_row = $this->sn_chat_advanced_get_chat_player_record($user['id'], '`chat_player_invisible`', true); |
||
| 378 | $chat_directive = strtolower($chat_command_parsed[2]) == 'on' || $chat_command_parsed[2] == 1 ? 1 : (strtolower($chat_command_parsed[2]) == 'off' || (string)$chat_command_parsed[2] === '0' ? 0 : ''); |
||
| 379 | if ($chat_directive !== '') { |
||
| 380 | doquery("UPDATE {{chat_player}} SET `chat_player_invisible` = {$chat_directive} WHERE `chat_player_player_id` = {$user['id']} LIMIT 1"); |
||
| 381 | } else { |
||
| 382 | $chat_directive = $chat_player_row['chat_player_invisible']; |
||
| 383 | } |
||
| 384 | $message = "[c=lime]{$lang['chat_advanced_visible'][$chat_directive]}[/c]"; |
||
| 385 | break; |
||
| 386 | |||
| 387 | case 'whisper': |
||
| 388 | if ($chat_player_muted) { |
||
| 389 | $chat_command_issued = ''; |
||
| 390 | } elseif ($chat_command_parsed[3] && $chat_command_parsed[2]) { |
||
| 391 | $chat_command_parsed[2] = trim($chat_command_parsed[2], '"'); |
||
| 392 | $recipient_info = db_user_by_username($chat_command_parsed[2]); |
||
| 393 | $chat_command_parsed[2] = db_escape($chat_command_parsed[2]); |
||
| 394 | if ($recipient_info['id']) { |
||
| 395 | $message = $chat_command_parsed[3]; |
||
| 396 | $nick = db_escape(player_nick_compact(player_nick_render_current_to_array($user, array('color' => true, 'icons' => true, 'ally' => false)))); |
||
| 397 | $chat_message_recipient_id = $recipient_info['id']; |
||
| 398 | $chat_message_recipient_name = db_escape($recipient_info['username']); |
||
| 399 | $chat_message_sender_id = $user['id']; |
||
| 400 | $chat_message_sender_name = db_escape($user['username']); |
||
| 401 | } else { |
||
| 402 | $message = "[c=red]{$lang['chat_advanced_err_player_name_unknown']}[/c]"; |
||
| 403 | } |
||
| 404 | } elseif (!$chat_command_parsed[2]) { |
||
| 405 | $message = "[c=red]{$lang['chat_advanced_err_message_player_empty']}[/c]"; |
||
| 406 | } elseif (!$chat_command_parsed[3]) { |
||
| 407 | $message = "[c=red]{$lang['chat_advanced_err_message_empty']}[/c]"; |
||
| 408 | } |
||
| 409 | break; |
||
| 410 | |||
| 411 | case 'mute': |
||
| 412 | case 'ban': |
||
| 413 | case 'unmute': |
||
| 414 | case 'unban': |
||
| 415 | if ($chat_command_parsed[2] && ($chat_command_parsed[3] || $chat_command_issued == 'unmute' || $chat_command_issued == 'unban')) { |
||
| 416 | $chat_command_parsed[2] = strtolower($chat_command_parsed[2]); |
||
| 417 | if (strpos($chat_command_parsed[2], 'id ') !== false && is_id($player_id = substr($chat_command_parsed[2], 3))) { |
||
| 418 | $chat_player_subject = db_user_by_id($player_id, false, '`id`, `authlevel`, `username`'); |
||
| 419 | if ($chat_player_subject) { |
||
| 420 | if ($chat_player_subject['id'] == $user['id']) { |
||
| 421 | $message = "[c=red]{$lang['chat_advanced_err_player_same']}[/c]"; |
||
| 422 | } elseif ($chat_player_subject['authlevel'] >= $user['authlevel']) { |
||
| 423 | $message = "[c=red]{$lang['chat_advanced_err_player_higher']}[/c]"; |
||
| 424 | } else { |
||
| 425 | $chat_message_recipient_id = 'NULL'; |
||
| 426 | $chat_message_recipient_name = ''; |
||
| 427 | if ($chat_command_issued == 'unmute' || $chat_command_issued == 'unban') { |
||
| 428 | $temp = db_escape($chat_command_parsed[3]); |
||
| 429 | if ($chat_command_issued == 'unban') { |
||
| 430 | sys_admin_player_ban_unset($user, $chat_player_subject, $temp); |
||
| 431 | $message = $lang['chat_advanced_command_unban']; |
||
| 432 | } elseif ($chat_command_issued == 'unmute') { |
||
| 433 | doquery("UPDATE {{chat_player}} SET `chat_player_muted` = 0, `chat_player_mute_reason` = '{$temp}' WHERE `chat_player_player_id` = {$chat_player_subject['id']} LIMIT 1"); |
||
| 434 | $message = $lang['chat_advanced_command_unmute']; |
||
| 435 | } else { |
||
| 436 | $message = ''; |
||
| 437 | } |
||
| 438 | |||
| 439 | if ($message) { |
||
| 440 | $message = sprintf($message, $chat_player_subject['username']); |
||
| 441 | $message .= $chat_command_parsed[3] ? sprintf($lang['chat_advanced_command_reason'], $chat_command_parsed[3]) : ''; |
||
| 442 | $message = "[c=lime]{$message}[/c]"; |
||
| 443 | } |
||
| 444 | } elseif (preg_match("#(\d+)(y|m|w|d|h)(\!)?\s*(.*)#iu", $chat_command_parsed[3], $chat_command_parsed_two)) { |
||
| 445 | //TODO Localize [\s\pL\w]* |
||
| 446 | $date_to_timestamp = array( |
||
| 447 | 'y' => PERIOD_YEAR, |
||
| 448 | 'm' => PERIOD_MONTH, |
||
| 449 | 'w' => PERIOD_WEEK, |
||
| 450 | 'd' => PERIOD_DAY, |
||
| 451 | 'h' => PERIOD_HOUR, |
||
| 452 | ); |
||
| 453 | $this->sn_chat_advanced_get_chat_player_record($chat_player_subject['id'], '`chat_player_muted`', false); |
||
| 454 | |||
| 455 | $term = $date_to_timestamp[$chat_command_parsed_two[2]] * $chat_command_parsed_two[1]; |
||
| 456 | $date_compiled = $term + SN_TIME_NOW; |
||
| 457 | $chat_command_parsed_two[4] = db_escape($chat_command_parsed_two[4]); |
||
| 458 | |||
| 459 | doquery("UPDATE {{chat_player}} SET `chat_player_muted` = {$date_compiled}, `chat_player_mute_reason` = '{$chat_command_parsed_two[4]}' WHERE `chat_player_player_id` = {$chat_player_subject['id']} LIMIT 1"); |
||
| 460 | if ($chat_command_issued == 'ban') { |
||
| 461 | sys_admin_player_ban($user, $chat_player_subject, $term, $chat_command_parsed_two[3] != '!', $chat_command_parsed_two[4]); |
||
| 462 | $message = $chat_command_parsed_two[3] == '!' ? $lang['chat_advanced_command_ban_no_vacancy'] : $lang['chat_advanced_command_ban']; |
||
| 463 | } else { |
||
| 464 | $message = $lang['chat_advanced_command_mute']; |
||
| 465 | } |
||
| 466 | // $message = sprintf($message, $chat_player_subject['username'], date(FMT_DATE_TIME, $date_compiled)); |
||
| 467 | // $message .= $chat_command_parsed_two[4] ? sprintf($lang['chat_advanced_command_reason'], $chat_command_parsed_two[4]) : ''; |
||
| 468 | $message = sprintf($message, $chat_player_subject['username'], date(FMT_DATE_TIME, $date_compiled), $chat_command_parsed_two[4] ? sprintf($lang['chat_advanced_command_reason'], $chat_command_parsed_two[4]) : ''); |
||
| 469 | $message = "[c=red]{$message}[/c]"; |
||
| 470 | } else { |
||
| 471 | $message = "[c=red]{$lang['chat_advanced_err_term_wrong']}[/c]"; |
||
| 472 | } |
||
| 473 | } |
||
| 474 | } else { |
||
| 475 | $message = "[c=red]{$lang['chat_advanced_err_player_id_unknown']}[/c]"; |
||
| 476 | } |
||
| 477 | } else { |
||
| 478 | $message = "[c=red]{$lang['chat_advanced_err_player_id_incorrect']}[/c]"; |
||
| 479 | } |
||
| 480 | } elseif (!$chat_command_parsed[2]) { |
||
| 481 | $message = "[c=red]{$lang['chat_advanced_err_player_id_need']}[/c]"; |
||
| 482 | } elseif (!$chat_command_parsed[3]) { |
||
| 483 | $message = "[c=red]{$lang['chat_advanced_err_term_need']}[/c]"; |
||
| 484 | } |
||
| 485 | break; |
||
| 486 | |||
| 487 | default: |
||
| 488 | $message = array(); |
||
| 489 | $chat_command_parsed[2] = strtolower($chat_command_parsed[2]); |
||
| 490 | |||
| 491 | $chat_directive = $chat_command_parsed[2] && array_key_exists($chat_command_parsed[2], $chat_aliases) ? $chat_aliases[$chat_command_parsed[2]] : ''; |
||
| 492 | |||
| 493 | if (!$chat_directive) { |
||
| 494 | $commands_available = array(); |
||
| 495 | $message[] = $lang['chat_advanced_help_description']; |
||
| 496 | foreach ($chat_commands as $chat_command_listed => $chat_command_info) { |
||
| 497 | if (in_array($user['authlevel'], $chat_command_info['access'])) { |
||
| 498 | $commands_available[] = $lang['chat_advanced_help_short'][$chat_command_listed]; |
||
| 499 | } |
||
| 500 | } |
||
| 501 | $message[] = $lang['chat_advanced_help_commands_accessible'] . ' ' . implode(', ', $commands_available); |
||
| 502 | } else { |
||
| 503 | $message[] = sprintf($lang['chat_advanced_help_command'], $chat_directive); |
||
| 504 | $message[] = $lang['chat_advanced_help'][$chat_directive]; |
||
| 505 | $aliases = array(); |
||
| 506 | foreach ($chat_aliases as $chat_command_alias => $chat_command_real) { |
||
| 507 | if ($chat_command_real == $chat_directive) { |
||
| 508 | $aliases[] = '/' . $chat_command_alias; |
||
| 509 | } |
||
| 510 | } |
||
| 511 | $message[] = $lang['chat_advanced_help_command_aliases'] . implode(', ', $aliases); |
||
| 512 | } |
||
| 513 | $message = implode(chr(13) . chr(10), $message); |
||
| 514 | $message = "[c=lime]{$message}[/c]"; |
||
| 515 | |||
| 516 | if ($chat_command_exists !== true) { |
||
| 517 | $message = "[c=red]{$lang['chat_advanced_err_command_unknown']} \"/{$chat_command_exists}\"[/c]" . chr(13) . chr(10) . $message; |
||
| 518 | } |
||
| 519 | break; |
||
| 520 | } |
||
| 521 | } else { |
||
| 522 | $message = "[c=red]{$lang['chat_advanced_err_command_inacessible']}[/c]"; |
||
| 523 | } |
||
| 524 | $message = "[b]{$message}[/b]"; |
||
| 525 | } |
||
| 526 | |||
| 527 | if (!$chat_command_issued && !$chat_player_muted) { |
||
| 528 | $chat_message_sender_id = $user['id']; |
||
| 529 | $chat_message_sender_name = db_escape($user['username']); |
||
| 530 | $chat_message_recipient_id = 'NULL'; |
||
| 531 | $chat_message_recipient_name = ''; |
||
| 532 | $ally_id = sys_get_param('ally') && $user['ally_id'] ? $user['ally_id'] : 0; |
||
| 533 | $nick = db_escape(player_nick_compact(player_nick_render_current_to_array($user, array('color' => true, 'icons' => true, 'ally' => !$ally_id, 'class' => 'class="chat_nick_msg"')))); |
||
| 534 | |||
| 535 | // Replacing news://xxx link with BBCode |
||
| 536 | $message = preg_replace("#news\:\/\/(\d+)#", "[news=$1]", $message); |
||
| 537 | // Replacing news URL with BBCode |
||
| 538 | $message = preg_replace("#(?:https?\:\/\/(?:.+)?\/announce\.php\?id\=(\d+))#", "[news=$1]", $message); |
||
| 539 | $message = preg_replace("#(?:https?\:\/\/(?:.+)?\/index\.php\?page\=battle_report\&cypher\=([0-9a-zA-Z]{32}))#", "[ube=$1]", $message); |
||
| 540 | |||
| 541 | if ($color = sys_get_param_str('color')) { |
||
| 542 | $message = "[c={$color}]{$message}[/c]"; |
||
| 543 | } |
||
| 544 | } elseif (!$chat_command_issued && $chat_player_muted) { |
||
| 545 | $chat_message_recipient_id = $user['id']; |
||
| 546 | $chat_message_recipient_name = db_escape($user['username']); |
||
| 547 | $message = sprintf($lang['chat_advanced_command_mute'], $user['username'], date(FMT_DATE_TIME, $chat_player_muted)) . |
||
| 548 | ($chat_player_row['chat_player_muted_reason'] ? sprintf($lang['chat_advanced_command_mute_reason'], $chat_player_row['chat_player_muted_reason']) : ''); |
||
| 549 | $message = "[c=red]{$message}[/c]"; |
||
| 550 | } |
||
| 551 | $message = db_escape($message); |
||
| 552 | |||
| 553 | doquery( |
||
| 554 | "INSERT INTO |
||
| 555 | {{chat}} |
||
| 556 | SET |
||
| 557 | `user` = '{$nick}', |
||
| 558 | `ally_id` = '{$ally_id}', |
||
| 559 | `message` = '{$message}', |
||
| 560 | `timestamp` = " . SN_TIME_NOW . ", |
||
| 561 | `chat_message_sender_id` = {$chat_message_sender_id}, |
||
| 562 | `chat_message_sender_name` = '{$chat_message_sender_name}', |
||
| 563 | `chat_message_recipient_id` = {$chat_message_recipient_id}, |
||
| 564 | `chat_message_recipient_name` = '{$chat_message_recipient_name}'" |
||
| 565 | ); |
||
| 566 | } |
||
| 567 | |||
| 568 | die(); |
||
| 569 | } |
||
| 602 |