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