Test Failed
Push — trunk ( d27a51...a828b4 )
by SuperNova.WS
21:37
created

Chat::chatMsgView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
/**
3
 * Created by Gorlum 13.05.2020 13:02
4
 */
5
6
namespace Chat;
7
8
9
use classCache;
10
use Common\Traits\TSingleton;
11
use SN;
12
use SnTemplate;
13
14
class Chat {
15
16
  use TSingleton;
17
18
  protected $_chat_aliases = array(
19
    'h'         => 'help',
20
    '?'         => 'help',
21
    'help'      => 'help',
22
    'w'         => 'whisper',
23
    'whisper'   => 'whisper',
24
    'b'         => 'ban',
25
    'ban'       => 'ban',
26
    'ub'        => 'unban',
27
    'unban'     => 'unban',
28
    'm'         => 'mute',
29
    'mute'      => 'mute',
30
    'um'        => 'unmute',
31
    'unmute'    => 'unmute',
32
    'iv'        => 'invisible',
33
    'invisible' => 'invisible',
34
    // * /i /ignore
35
    // * /ck /kick
36
    // * /ci /invite
37
    // * /cj /join
38
    // * /cc /create
39
  );
40
41
  protected $_chat_commands = array(
42
    'invisible' => array(
43
      'access'  => array(0, 1, 2, 3, 4),
44
      'options' => array(
45
        CHAT_OPTION_SWITCH => CHAT_OPTION_SWITCH,
46
      ),
47
    ),
48
    'whisper'   => array(
49
      'access'  => array(0, 1, 2, 3, 4),
50
      'options' => array(),
51
    ),
52
    'mute'      => array(
53
      'access'  => array(1, 2, 3, 4),
54
      'options' => array(),
55
    ),
56
    'unmute'    => array(
57
      'access'  => array(1, 2, 3, 4),
58
      'options' => array(),
59
    ),
60
    'ban'       => array(
61
      'access'  => array(1, 2, 3, 4),
62
      'options' => array(),
63
    ),
64
    'unban'     => array(
65
      'access'  => array(1, 2, 3, 4),
66
      'options' => array(),
67
    ),
68
    'help'      => array(
69
      'access'  => array(0, 1, 2, 3, 4),
70
      'options' => array(),
71
    ),
72
  );
73
74
75
  public static function chatModel() {
76
    static::me()->_chatModel();
0 ignored issues
show
Bug introduced by
It seems like _chatModel() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
    static::me()->/** @scrutinizer ignore-call */ _chatModel();
Loading history...
77
  }
78
79
  public static function chatAddModel() {
80
    static::me()->_chatAddModel();
0 ignored issues
show
Bug introduced by
It seems like _chatAddModel() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
    static::me()->/** @scrutinizer ignore-call */ _chatAddModel();
Loading history...
81
  }
82
83
  public static function chatView($template = null) {
84
    return static::me()->_chatView($template);
0 ignored issues
show
Bug introduced by
It seems like _chatView() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
    return static::me()->/** @scrutinizer ignore-call */ _chatView($template);
Loading history...
85
  }
86
87
  public static function chatMsgView($template = null) {
88
    static::me()->_chatMsgView($template);
0 ignored issues
show
Bug introduced by
It seems like _chatMsgView() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
    static::me()->/** @scrutinizer ignore-call */ _chatMsgView($template);
Loading history...
89
  }
90
91
  public static function chatFrameView($template = null) {
92
    static::me()->_chatFrameView($template);
0 ignored issues
show
Bug introduced by
It seems like _chatFrameView() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
    static::me()->/** @scrutinizer ignore-call */ _chatFrameView($template);
Loading history...
93
  }
94
95
  protected function _chatView($template = null) {
96
    defined('IN_AJAX') or define('IN_AJAX', true);
97
98
    global $config, $lang;
99
100
    $iframe = sys_get_param_id('iframe');
101
102
//    $template = $this->addModuleTemplate('chat_body', $template);
103
    $template = SnTemplate::gettemplate('chat/chat_body', $template);
104
    $template->assign_var('CHAT_REFRESH_RATE', $config->chat_refresh_rate);
105
    $template->assign_var('CHAT_MODE', $iframe);
106
    $template->assign_var('MENU', !$iframe);
107
    $template->assign_var('NAVBAR', !$iframe);
108
    $template->assign_var('CHAT_IFRAME', $iframe);
109
    foreach ($lang['chat_advanced_command_interval'] as $interval => $locale) {
110
      $template->assign_block_vars('chat_advanced_command_interval', array(
111
        'INTERVAL' => $interval,
112
        'NAME'     => $locale,
113
      ));
114
    }
115
116
    return $template;
117
  }
118
119
  protected function _chatMsgView($template = null) {
120
    defined('IN_AJAX') or define('IN_AJAX', true);
121
122
    global $config, $user, $lang;
123
124
125
    $history = sys_get_param_str('history');
126
    if (!$history) {
127
      $config->array_set('users', $user['id'], 'chat_last_refresh', SN_TIME_MICRO);
128
      // $chat_player_row = $this->sn_chat_advanced_get_chat_player_record($user['id']);
129
      doquery("UPDATE {{chat_player}} SET `chat_player_refresh_last` = " . SN_TIME_NOW . " WHERE `chat_player_player_id` = {$user['id']} LIMIT 1;");
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

129
      /** @scrutinizer ignore-deprecated */ doquery("UPDATE {{chat_player}} SET `chat_player_refresh_last` = " . SN_TIME_NOW . " WHERE `chat_player_player_id` = {$user['id']} LIMIT 1;");
Loading history...
130
    }
131
132
    $page                         = 0;
133
    $last_message                 = '';
134
    $alliance                     = 0;
135
    $template_result['.']['chat'] = array();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$template_result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $template_result = array(); before regardless.
Loading history...
136
    if (!$history && $config->getMode() != classCache::CACHER_NO_CACHE && $config->chat_timeout && SN_TIME_MICRO - $config->array_get('users', $user['id'], 'chat_last_activity') > $config->chat_timeout) {
137
      $result['disable']              = true;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.
Loading history...
138
      $template_result['.']['chat'][] = array(
139
        'TIME'    => date(FMT_DATE_TIME, htmlentities(SN_TIME_NOW + SN_CLIENT_TIME_DIFF, ENT_QUOTES, 'utf-8')),
0 ignored issues
show
Bug introduced by
htmlentities(Chat\SN_TIM...at\ENT_QUOTES, 'utf-8') of type string is incompatible with the type integer expected by parameter $timestamp of date(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

139
        'TIME'    => date(FMT_DATE_TIME, /** @scrutinizer ignore-type */ htmlentities(SN_TIME_NOW + SN_CLIENT_TIME_DIFF, ENT_QUOTES, 'utf-8')),
Loading history...
140
        'DISABLE' => true,
141
      );
142
    } else {
143
      $alliance = sys_get_param_str('ally') && $user['ally_id'] ? $user['ally_id'] : 0;
144
145
      $page_limit = sys_get_param_id('line_per_page', 20); // Chat rows Limit
146
147
      $where_add    = '';
148
      $last_message = 0;
149
      if ($history) {
150
        $rows       = doquery("SELECT count(1) AS CNT
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

150
        $rows       = /** @scrutinizer ignore-deprecated */ doquery("SELECT count(1) AS CNT
Loading history...
151
          FROM {{chat}}
152
          WHERE
153
          (
154
            (ally_id = '{$alliance}' AND `chat_message_recipient_id` IS NULL) OR
155
            (ally_id = 0 AND `chat_message_recipient_id` = {$user['id']}) OR
156
            (ally_id = 0 AND `chat_message_sender_id` = {$user['id']} AND `chat_message_recipient_id` IS NOT NULL) OR
157
            (ally_id = 0 AND `chat_message_sender_id` IS NULL AND `chat_message_recipient_id` IS NULL)
158
          )
159
        ", true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $table of doquery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

159
        ", /** @scrutinizer ignore-type */ true);
Loading history...
160
        $page_count = ceil($rows['CNT'] / $page_limit);
161
162
        for ($i = 1; $i <= $page_count; $i++) {
163
          $template_result['.']['page'][] = array(
164
            'NUMBER' => $i
165
          );
166
        }
167
168
        $page = min($page_count, max(1, sys_get_param_int('sheet')));
169
      } else {
170
        $last_message = sys_get_param_id('last_message');
171
        $where_add    = $last_message ? "AND `messageid` > {$last_message}" : '';
172
        $page         = 1;
173
      }
174
175
      $start_row = ($page - 1) * $page_limit;// OR `chat_message_recipient_id` = {$user['id']}
176
      $start_row = $start_row < 0 ? 0 : $start_row;
177
      $query     = doquery(
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

177
      $query     = /** @scrutinizer ignore-deprecated */ doquery(
Loading history...
178
        "SELECT c.*, u.authlevel
179
        FROM
180
          {{chat}} AS c
181
          LEFT JOIN {{users}} AS u ON u.id = c.chat_message_sender_id
182
        WHERE
183
          (
184
            (c.ally_id = '{$alliance}' AND `chat_message_recipient_id` IS NULL) OR
185
            (c.ally_id = 0 AND `chat_message_recipient_id` = {$user['id']}) OR
186
            (c.ally_id = 0 AND `chat_message_sender_id` = {$user['id']} AND `chat_message_recipient_id` IS NOT NULL) OR
187
            (c.ally_id = 0 AND `chat_message_sender_id` IS NULL AND `chat_message_recipient_id` IS NULL)
188
          )
189
          {$where_add}
190
        ORDER BY messageid DESC
191
        LIMIT {$start_row}, {$page_limit}");
192
      while ($chat_row = db_fetch($query)) {
193
        $chat_row['user']               = player_nick_render_to_html($chat_row['user']);
194
        $nick_stripped                  = htmlentities(strip_tags($chat_row['user']), ENT_QUOTES, 'utf-8');
195
        $template_result['.']['chat'][] = array(
196
          'TIME'                => SN::$gc->bbCodeParser->expandBbCode(date(FMT_DATE_TIME, $chat_row['timestamp'] + SN_CLIENT_TIME_DIFF)),
197
          'NICK'                => $chat_row['user'],
198
          'NICK_STRIPPED'       => $nick_stripped,
199
          'TEXT'                => SN::$gc->bbCodeParser->expandBbCode($chat_row['message'], $chat_row['authlevel']),
200
          'SENDER_ID'           => $chat_row['chat_message_sender_id'],
201
          'SENDER_NAME'         => $safe_name = sys_safe_output($chat_row['chat_message_sender_name']),
202
          'SENDER_NAME_SAFE'    => $safe_name <> $chat_row['chat_message_sender_name'] || strpbrk($safe_name, ' /\\\'') ? '&quot;' . $safe_name . '&quot;' : $safe_name,
203
          'RECIPIENT_ID'        => $chat_row['chat_message_recipient_id'],
204
          'RECIPIENT_NAME'      => $safe_name_recipient = sys_safe_output($chat_row['chat_message_recipient_name']),
205
          'RECIPIENT_NAME_SAFE' => $safe_name_recipient <> $chat_row['chat_message_recipient_name'] || strpbrk($safe_name_recipient, ' /\\\'') ? '&quot;' . $safe_name_recipient . '&quot;' : $safe_name_recipient,
206
        );
207
208
        $last_message = max($last_message, $chat_row['messageid']);
209
      }
210
    }
211
212
    $template_result['.']['chat'] = array_reverse($template_result['.']['chat']);
213
214
    $template_result += array(
215
      'PAGE'        => $page,
216
      'PAGES_TOTAL' => $page_count,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $page_count does not seem to be defined for all execution paths leading up to this point.
Loading history...
217
      'PAGE_NEXT'   => $page < $page_count ? $page + 1 : $page,
218
      'PAGE_PREV'   => $page > 1 ? $page - 1 : $page,
219
      'ALLY'        => $alliance,
220
      'HISTORY'     => $history,
221
      'USER_ID'     => $user['id'],
222
    );
223
224
//    $template = $this->addModuleTemplate('chat_messages', $template);
225
    $template = SnTemplate::gettemplate('chat/chat_messages', $template);
226
    $template->assign_recursive($template_result);
227
228
    if ($history) {
229
      $pageTitle = "{$lang['chat_history']} - {$lang[$alliance ? 'chat_ally' : 'chat_common']}";
230
      SnTemplate::display($template, $pageTitle);
231
    } else {
232
      $result['last_message'] = $last_message;
233
      $result['html']         = SnTemplate::templateRenderToHtml($template);
234
235
//      $template = $this->addModuleTemplate('chat_online', $template);
236
      $template                              = SnTemplate::gettemplate('chat/chat_online', null);
237
//      $template->assign_recursive($template_result);
238
      $chat_online_players                   = 0;
239
      $chat_online_invisibles                = 0;
240
      $chat_player_invisible                 = 0;
241
      $template_result['.']['online_player'] = array();
242
      $ally_add                              = $alliance ? "AND u.ally_id = {$alliance}" : '';
243
244
      $chat_player_list = db_chat_player_list_online($config->chat_refresh_rate, $ally_add);
245
246
      // TODO: Добавить ограничение по бану
247
      while ($chat_player_row = db_fetch($chat_player_list)) {
248
        $chat_online_players++;
249
        if ($chat_player_row['chat_player_invisible']) {
250
          $chat_online_invisibles++;
251
        }
252
253
        if (!$chat_player_row['chat_player_invisible'] || $user['authlevel']) {
254
          $safe_name                               = sys_safe_output($chat_player_row['username']);
255
          $safe_name                               = $chat_player_row['username'] <> $safe_name || strpbrk($safe_name, ' /\\\'') !== false ? '&quot;' . $safe_name . '&quot;' : $safe_name;
256
          $template_result['.']['online_player'][] = array(
257
            'ID'          => $chat_player_row['id'],
258
            'NAME'        => player_nick_render_to_html($chat_player_row, array(
259
              'color' => true,
260
              'icons' => true,
261
              'ally'  => !$alliance,
262
              'class' => 'class="chat_nick_whisper" safe_name="' . $safe_name . '"',
263
            )),
264
            'NAME_SAFE'   => $safe_name,
265
            'MUTED'       => $chat_player_row['chat_player_muted'] && $chat_player_row['chat_player_muted'] >= SN_TIME_NOW ? date(FMT_DATE_TIME, $chat_player_row['chat_player_muted']) : false,
266
            'MUTE_REASON' => htmlentities($chat_player_row['chat_player_mute_reason'], ENT_COMPAT, 'UTF-8'),
267
            'INVISIBLE'   => $chat_player_row['chat_player_invisible'],
268
            'AUTH_LEVEL'  => $chat_player_row['authlevel'],
269
          );
270
        }
271
        if ($user['id'] == $chat_player_row['id']) {
272
          $chat_player_invisible = $chat_player_row['chat_player_invisible'];
273
        }
274
      }
275
276
      $chat_commands   = &$this->_chat_commands;
277
      $template_result = array_merge($template_result, array(
278
        'USER_ID'         => $user['id'],
279
        'USER_AUTHLEVEL'  => $user['authlevel'],
280
        'USER_CAN_BAN'    => in_array($user['authlevel'], $chat_commands['ban']['access']),
281
        'USER_CAN_MUTE'   => in_array($user['authlevel'], $chat_commands['mute']['access']),
282
        'USER_CAN_UNMUTE' => in_array($user['authlevel'], $chat_commands['unmute']['access']),
283
      ));
284
      $template->assign_recursive($template_result);
285
      $result['online'] = SnTemplate::templateRenderToHtml($template);
286
287
      $result['online_players']        = $chat_online_players;
288
      $result['online_invisibles']     = $chat_online_invisibles;
289
      $result['chat_player_invisible'] = $chat_player_invisible;
290
      $result['users_total']           = SN::$config->users_amount;
291
      $result['users_online']          = SN::$config->var_online_user_count;
292
      print(json_encode($result));
293
    }
294
    die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
295
  }
296
297
  protected function _chatFrameView($template = null) {
298
    defined('IN_AJAX') or define('IN_AJAX', true);
299
300
//    $template = $this->addModuleTemplate('chat_frame', $template);
301
    $template = SnTemplate::gettemplate('chat/chat_frame', $template);
302
    require_once($template->files['chat_frame']);
303
    die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
304
  }
305
306
307
  protected function _chatModel() {
308
    global $user, $template_result, $lang;
309
310
    $this->update_chat_activity();
311
312
    $mode = sys_get_param_int('mode');
313
    switch ($mode) {
314
      case CHAT_MODE_ALLY:
315
        $template_result['ALLY']      = intval($user['ally_id']);
316
        $template_result['CHAT_MODE'] = CHAT_MODE_ALLY;
317
        $page_title                   = $lang['chat_ally'];
318
      break;
319
320
      case CHAT_MODE_COMMON:
321
      default:
322
        $page_title                   = $lang['chat_common'];
323
        $template_result['CHAT_MODE'] = CHAT_MODE_COMMON;
324
      break;
325
    }
326
327
    $template_result['PAGE_HEADER'] = $page_title;
328
329
    $template_result['.']['smiles'] = array();
330
331
    foreach (SN::$gc->design->getSmilesList() as $auth_level => $replaces) {
332
      if ($auth_level > $user['authlevel']) {
333
        continue;
334
      }
335
336
      foreach ($replaces as $bbcode => $filename) {
337
        $template_result['.']['smiles'][] = array(
338
          'BBCODE'   => htmlentities($bbcode, ENT_COMPAT, 'UTF-8'),
339
          'FILENAME' => $filename,
340
        );
341
      }
342
    }
343
  }
344
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();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
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'])) {
0 ignored issues
show
Unused Code introduced by
The assignment to $chat_command_accessible is dead and can be removed.
Loading history...
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");
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

380
                /** @scrutinizer ignore-deprecated */ doquery("UPDATE {{chat_player}} SET `chat_player_invisible` = {$chat_directive} WHERE `chat_player_player_id` = {$user['id']} LIMIT 1");
Loading history...
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]);
0 ignored issues
show
Deprecated Code introduced by
The function db_user_by_username() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

392
                $recipient_info         = /** @scrutinizer ignore-deprecated */ db_user_by_username($chat_command_parsed[2]);
Loading history...
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`');
0 ignored issues
show
Bug introduced by
$player_id of type string is incompatible with the type array|integer expected by parameter $user_id_unsafe of db_user_by_id(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

418
                  $chat_player_subject = db_user_by_id(/** @scrutinizer ignore-type */ $player_id, false, '`id`, `authlevel`, `username`');
Loading history...
Deprecated Code introduced by
The function db_user_by_id() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

418
                  $chat_player_subject = /** @scrutinizer ignore-deprecated */ db_user_by_id($player_id, false, '`id`, `authlevel`, `username`');
Loading history...
419
                  if ($chat_player_subject) {
0 ignored issues
show
introduced by
The condition $chat_player_subject is always false.
Loading history...
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");
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

433
                          /** @scrutinizer ignore-deprecated */ 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");
Loading history...
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");
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

459
                        /** @scrutinizer ignore-deprecated */ 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");
Loading history...
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(
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

553
      /** @scrutinizer ignore-deprecated */ doquery(
Loading history...
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();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
569
  }
570
571
572
  protected function update_chat_activity($refresh = false) {
573
    global $config, $user;
574
575
    $config->array_set('users', $user['id'], 'chat_last_activity', SN_TIME_MICRO);
576
    $config->array_set('users', $user['id'], 'chat_last_refresh', $refresh ? 0 : SN_TIME_MICRO);
577
578
    $activity_row = doquery("SELECT `chat_player_id` FROM {{chat_player}} WHERE `chat_player_player_id` = {$user['id']} LIMIT 1", true);
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

578
    $activity_row = /** @scrutinizer ignore-deprecated */ doquery("SELECT `chat_player_id` FROM {{chat_player}} WHERE `chat_player_player_id` = {$user['id']} LIMIT 1", true);
Loading history...
Bug introduced by
true of type true is incompatible with the type string expected by parameter $table of doquery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

578
    $activity_row = doquery("SELECT `chat_player_id` FROM {{chat_player}} WHERE `chat_player_player_id` = {$user['id']} LIMIT 1", /** @scrutinizer ignore-type */ true);
Loading history...
579
    if (!$activity_row) {
580
      doquery("INSERT INTO {{chat_player}} SET `chat_player_player_id` = {$user['id']}");
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

580
      /** @scrutinizer ignore-deprecated */ doquery("INSERT INTO {{chat_player}} SET `chat_player_player_id` = {$user['id']}");
Loading history...
581
    } else {
582
      doquery("UPDATE {{chat_player}} SET `chat_player_activity` = '" . SN::$db->db_escape(SN_TIME_SQL) . "' WHERE `chat_player_player_id` = {$user['id']} LIMIT 1");
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

582
      /** @scrutinizer ignore-deprecated */ doquery("UPDATE {{chat_player}} SET `chat_player_activity` = '" . SN::$db->db_escape(SN_TIME_SQL) . "' WHERE `chat_player_player_id` = {$user['id']} LIMIT 1");
Loading history...
583
    }
584
  }
585
586
  protected function sn_chat_advanced_get_chat_player_record($player_id, $fields = '*', $return_data = true) {
587
    $result = false;
588
    if($player_id) {
589
      if(!($result = doquery("SELECT {$fields} FROM {{chat_player}} WHERE `chat_player_player_id` = {$player_id} LIMIT 1", true))) {
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $table of doquery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

589
      if(!($result = doquery("SELECT {$fields} FROM {{chat_player}} WHERE `chat_player_player_id` = {$player_id} LIMIT 1", /** @scrutinizer ignore-type */ true))) {
Loading history...
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

589
      if(!($result = /** @scrutinizer ignore-deprecated */ doquery("SELECT {$fields} FROM {{chat_player}} WHERE `chat_player_player_id` = {$player_id} LIMIT 1", true))) {
Loading history...
590
        doquery("INSERT INTO {{chat_player}} SET `chat_player_player_id` = {$player_id}");
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

590
        /** @scrutinizer ignore-deprecated */ doquery("INSERT INTO {{chat_player}} SET `chat_player_player_id` = {$player_id}");
Loading history...
591
        if($return_data) {
592
          $result = doquery("SELECT {$fields} FROM {{chat_player}} WHERE `chat_player_player_id` = {$player_id} LIMIT 1", true);
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

592
          $result = /** @scrutinizer ignore-deprecated */ doquery("SELECT {$fields} FROM {{chat_player}} WHERE `chat_player_player_id` = {$player_id} LIMIT 1", true);
Loading history...
593
        }
594
      }
595
    }
596
597
    return $result;
598
  }
599
600
601
}
602