Completed
Push — work-fleets ( 964a94...15d039 )
by SuperNova.WS
05:22
created

DBStaticMessages::messageDelete()   C

Complexity

Conditions 15
Paths 99

Size

Total Lines 39
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 240

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 26
c 1
b 0
f 0
nc 99
nop 2
dl 0
loc 39
ccs 0
cts 0
cp 0
crap 240
rs 5.0504

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
class DBStaticMessages {
4
  public static $snMessageClassList = array(
5
    MSG_TYPE_NEW       => array(
6
      'name'       => 'new_message',
7
      'switchable' => false,
8
      'email'      => false,
9
    ),
10
    MSG_TYPE_ADMIN     => array(
11
      'name'       => 'msg_admin',
12
      'switchable' => false,
13
      'email'      => true,
14
    ),
15
    MSG_TYPE_PLAYER    => array(
16
      'name'       => 'mnl_joueur',
17
      'switchable' => false,
18
      'email'      => true,
19
    ),
20
    MSG_TYPE_ALLIANCE  => array(
21
      'name'       => 'mnl_alliance',
22
      'switchable' => false,
23
      'email'      => true,
24
    ),
25
    MSG_TYPE_SPY       => array(
26
      'name'       => 'mnl_spy',
27
      'switchable' => true,
28
      'email'      => true,
29
    ),
30
    MSG_TYPE_COMBAT    => array(
31
      'name'       => 'mnl_attaque',
32
      'switchable' => true,
33
      'email'      => true,
34
    ),
35
    MSG_TYPE_TRANSPORT => array(
36
      'name'       => 'mnl_transport',
37
      'switchable' => true,
38
      'email'      => true,
39
    ),
40
    MSG_TYPE_RECYCLE   => array(
41
      'name'       => 'mnl_exploit',
42
      'switchable' => true,
43
      'email'      => true,
44
    ),
45
    MSG_TYPE_EXPLORE   => array(
46
      'name'       => 'mnl_expedition',
47
      'switchable' => true,
48
      'email'      => true,
49
    ),
50
    //     97 => 'mnl_general',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
51
    MSG_TYPE_QUE       => array(
52
      'name'       => 'mnl_buildlist',
53
      'switchable' => true,
54
      'email'      => true,
55
    ),
56
    MSG_TYPE_OUTBOX    => array(
57
      'name'       => 'mnl_outbox',
58
      'switchable' => false,
59
      'email'      => false,
60
    ),
61
  );
62
  public static $snMessageGroup = array(
63
    'switchable' => array(MSG_TYPE_SPY, MSG_TYPE_COMBAT, MSG_TYPE_RECYCLE, MSG_TYPE_TRANSPORT, MSG_TYPE_EXPLORE, MSG_TYPE_QUE),
64
    'email'      => array(MSG_TYPE_SPY, MSG_TYPE_PLAYER, MSG_TYPE_ALLIANCE, MSG_TYPE_COMBAT, MSG_TYPE_RECYCLE, MSG_TYPE_TRANSPORT,
65
      MSG_TYPE_ADMIN, MSG_TYPE_EXPLORE, MSG_TYPE_QUE),
66
  );
67
68
  /**
69
   * @param mixed|array $owners
70
   * @param integer     $sender
71
   * @param integer     $timestamp
72
   * @param integer     $message_type
73
   * @param string      $from
74
   * @param string      $subject
75
   * @param string      $text
76
   * @param bool        $escaped
77
   * @param bool        $force
78
   */
79
  public static function msg_send_simple_message($owners, $sender, $timestamp, $message_type, $from, $subject, $text, $escaped = false, $force = false) {
80
    global $user;
81
82
    if (!$owners) {
83
      return;
84
    }
85
86
    $timestamp = $timestamp ? $timestamp : SN_TIME_NOW;
87
    $sender = intval($sender);
88
    if (!is_array($owners)) {
89
      $owners = array($owners);
90
    }
91
92
    if (!$escaped) {
93
      $from = db_escape($from);
94
      $subject = db_escape($subject);
95
      $text = db_escape($text);
96
    }
97
98
    $text_unescaped = stripslashes(str_replace(array('\r\n', "\r\n"), "<br />", $text));
99
100
    $message_class = static::$snMessageClassList[$message_type];
101
    $message_class_email = $message_class['email'];
102
    $message_class_switchable = $message_class['switchable'];
103
    $message_class_name = $message_class['name'];
104
105
    $message_class_name_total = static::$snMessageClassList[MSG_TYPE_NEW]['name'];
106
107
    if ($owners[0] == '*') {
108
      if ($user['authlevel'] < 3) {
109
        return false;
110
      }
111
      // TODO Добавить $timestamp - рассылка может быть и отсроченной
112
      // TODO Добавить $sender - рассылка может быть и от кого-то
113
      static::db_message_insert_all($message_type, $from, $subject, $text);
114
      $owners = array();
115
    } else {
116
      $insert_values = array();
117
      $insert_template = "('%u'," . str_replace('%', '%%', " '{$sender}', '{$timestamp}', '{$message_type}', '{$from}', '{$subject}', '{$text}')");
118
119
      foreach ($owners as $owner) {
120
        if ($user['id'] != $owner) {
121
          $owner_row = DBStaticUser::db_user_by_id($owner);
122
        } else {
123
          $owner_row = $user;
124
        }
125
        sys_user_options_unpack($owner_row);
126
127
        if ($force || !$message_class_switchable || $owner_row["opt_{$message_class_name}"]) {
128
          $insert_values[] = sprintf($insert_template, $owner);
129
        }
130
131
        if ($message_class_email && classSupernova::$config->game_email_pm && $owner_row["opt_email_{$message_class_name}"]) {
132
          @$result = mymail($owner_row['email'], $subject, $text_unescaped, '', true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
133
        }
134
      }
135
136
      if (empty($insert_values)) {
137
        return;
138
      }
139
140
      static::db_message_insert($insert_values);
141
    }
142
    DBStaticUser::db_user_list_set_mass_mail($owners, "`{$message_class_name}` = `{$message_class_name}` + 1, `{$message_class_name_total}` = `{$message_class_name_total}` + 1");
143
144
    if (in_array($user['id'], $owners) || $owners[0] == '*') {
145
      $user[$message_class_name]++;
146
      $user[$message_class_name_total]++;
147
    }
148
  }
149
150
  public static function msg_ali_send($message, $subject, $ally_rank_id = 0, $ally_id = 0) {
151
    global $user;
152
153
    $ally_id = $ally_id ? $ally_id : $user['ally_id'];
154
155
    $sendList = array();
156
    $list = '';
157
    $query = DBStaticUser::db_user_list(
158
      "ally_id = '{$ally_id}'" . ($ally_rank_id >= 0 ? " AND ally_rank_id = {$ally_rank_id}" : ''),
159
      false, 'id, username');
160
    foreach ($query as $u) {
161
      $sendList[] = $u['id'];
162
      $list .= "<br>{$u['username']} ";
163
    }
164
165
    static::msg_send_simple_message($sendList, $user['id'], SN_TIME_NOW, MSG_TYPE_ALLIANCE, $user['username'], $subject, $message, true);
166
167
    return $list;
168
  }
169
170
171
  /**
172
   * @param mixed|array $owners
173
   * @param string      $subject
174
   * @param string      $text
175
   * @param bool        $escaped
176
   * @param bool        $force
177
   */
178
  public static function msgSendFromAdmin($owners, $subject, $text, $escaped = false, $force = false) {
179
    static::msg_send_simple_message($owners, 0, SN_TIME_NOW, MSG_TYPE_ADMIN, classLocale::$lang['sys_administration'], $subject, $text, $escaped, $force);
180
  }
181
182
  /**
183
   * @param mixed  $recipientId
184
   * @param array  $playerFromRow
185
   * @param string $subject
186
   * @param string $text
187
   */
188
  public static function msgSendFromPlayer($recipientId, $playerFromRow, $subject, $text) {
189
    static::msg_send_simple_message($recipientId, $playerFromRow['id'], SN_TIME_NOW, MSG_TYPE_PLAYER,
190
      "{$playerFromRow['username']} [{$playerFromRow['galaxy']}:{$playerFromRow['system']}:{$playerFromRow['planet']}]",
191
      $subject,
192
      $text,
193
      false, false);
194
  }
195
196
  /**
197
   * @param mixed  $recipientId
198
   * @param array  $playerFromRow
199
   * @param string $localeSubject
200
   * @param string $localeText
201
   */
202
  public static function msgSendFromPlayerBuddy($recipientId, $playerFromRow, $localeSubject, $localeText) {
203
    static::msgSendFromPlayer(
204
      $recipientId,
205
      $playerFromRow,
206
      classLocale::$lang[$localeSubject],
207
      sprintf(classLocale::$lang[$localeText], $playerFromRow['username'])
208
    );
209
  }
210
211
212
  /**
213
   * @param array $player
214
   *
215
   * @return template
216
   */
217
  public static function messageWrite($player) {
218
    $error_list = array();
219
220
    $recipientId = sys_get_param_id('id');
221
    $recipient_name_unescaped = sys_get_param_str_unsafe('recipient_name');
222
    $subject_unsafe = sys_get_param_str_unsafe('subject');
223
224
    if (!empty($recipientId)) {
225
      $recipient_row = DBStaticUser::db_user_by_id($recipientId);
226
    }
227
228
    if (empty($recipient_row)) {
229
      $recipient_row = DBStaticUser::db_user_by_username($recipient_name_unescaped);
230
    }
231
232
    if (!empty($recipient_row)) {
233
      $recipientId = $recipient_row['id'];
234
      $recipient_name_unescaped = $recipient_row['username'];
235
    } else {
236
      $recipientId = 0;
237
      $recipient_name_unescaped = '';
238
    }
239
240 View Code Duplication
    if ($recipientId == $player['id']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
241
      $error_list[] = array('MESSAGE' => classLocale::$lang['msg_err_self_send'], 'STATUS' => ERR_ERROR);
242
    }
243
244
    $re = 0;
245
    while (strpos($subject_unsafe, classLocale::$lang['msg_answer_prefix']) !== false) {
246
      $subject_unsafe = substr($subject_unsafe, strlen(classLocale::$lang['msg_answer_prefix']));
247
      $re++;
248
    }
249
    $re ? $subject_unsafe = classLocale::$lang['msg_answer_prefix'] . $subject_unsafe : false;
250
251
    $subject_unsafe = $subject_unsafe ? $subject_unsafe : classLocale::$lang['msg_subject_default'];
252
253
    $textUnsafe = sys_get_param_str_unsafe('text');
254
    if (sys_get_param_str('msg_send')) {
255 View Code Duplication
      if (!$recipientId) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
256
        $error_list[] = array('MESSAGE' => classLocale::$lang['msg_err_player_not_found'], 'STATUS' => ERR_ERROR);
257
      }
258
259
      if (empty($textUnsafe)) {
260
        $error_list[] = array('MESSAGE' => classLocale::$lang['msg_err_no_text'], 'STATUS' => ERR_ERROR);
261
      }
262
263
      if (empty($error_list)) {
264
        $error_list[] = array('MESSAGE' => classLocale::$lang['msg_not_message_sent'], 'STATUS' => ERR_NONE);
265
266
        static::msgSendFromPlayer($recipientId, $player, $subject_unsafe, $textUnsafe);
267
268
        $textUnsafe = '';
269
      }
270
    }
271
272
    $template = gettemplate('msg_message_compose', true);
273
    $template->assign_vars(array(
274
      'RECIPIENT_ID'   => $recipientId,
275
      'RECIPIENT_NAME' => htmlspecialchars($recipient_name_unescaped),
276
      'SUBJECT'        => htmlspecialchars($subject_unsafe),
277
      'TEXT'           => htmlspecialchars($textUnsafe),
278
    ));
279
280
    foreach ($error_list as $error_message) {
281
      $template->assign_block_vars('result', $error_message);
282
    }
283
284
    $message_query = static::db_message_list_get_last_20($player, $recipientId);
285
    static::messageRenderList(MSG_TYPE_OUTBOX, $template, $message_query);
286
287
    return $template;
288
  }
289
290
291
  /**
292
   * @param array  $player
293
   * @param string $current_class
294
   */
295
  public static function messageDelete($player, $current_class) {
296
    $message_range = sys_get_param_str('message_range');
297
    $marked_message_list = sys_get_param('mark', array());
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
298
299
    $query_add = '';
300
    switch ($message_range) {
301
      case 'unchecked':
302
      case 'checked':
303
        if ($message_range == 'checked' && empty($marked_message_list)) {
304
          break;
305
        }
306
307
        foreach ($marked_message_list as &$messageId) {
0 ignored issues
show
Bug introduced by
The expression $marked_message_list of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
308
          $messageId = idval($messageId);
309
        }
310
311
        $query_add = implode(',', $marked_message_list);
312
        if ($query_add) {
313
          $query_add = "IN ({$query_add})";
314
          if ($message_range == 'unchecked') {
315
            $query_add = "NOT {$query_add}";
316
          }
317
          $query_add = " AND `message_id` {$query_add}";
318
        }
319
320
      case 'class':
321
        if ($current_class != MSG_TYPE_OUTBOX && $current_class != MSG_TYPE_NEW) {
322
          $query_add .= " AND `message_type` = {$current_class}";
323
        }
324
      case 'all':
325
        $query_add = $query_add ? $query_add : true;
326
      break;
327
    }
328
329
    if ($query_add) {
330
      $query_add = $query_add === true ? '' : $query_add;
331
      static::db_message_list_delete($player, $query_add);
332
    }
333
  }
334
335
  /**
336
   * @param array $player
337
   * @param int   $current_class
338
   *
339
   * @return template
340
   */
341
  public static function messageShow(&$player, $current_class) {
342
    if ($current_class == MSG_TYPE_OUTBOX) {
343
      $message_query = static::db_message_list_outbox_by_user_id($player['id']);
344
    } else {
345
      if ($current_class == MSG_TYPE_NEW) {
346
        $SubUpdateQry = array();
347
        foreach (static::$snMessageClassList as $message_class_id => $message_class) {
348
          if ($message_class_id != MSG_TYPE_OUTBOX) {
349
            $SubUpdateQry[] = "`{$message_class['name']}` = '0'";
350
            $player[$message_class['name']] = 0;
351
          }
352
        }
353
        $SubUpdateQry = implode(',', $SubUpdateQry);
354
      } else {
355
        $classFieldNameCurrent = static::$snMessageClassList[$current_class]['name'];
356
        $classFieldNameNew = static::$snMessageClassList[MSG_TYPE_NEW]['name'];
357
        $SubUpdateQry = "`{$classFieldNameCurrent}` = '0', `{$classFieldNameNew}` = `{$classFieldNameNew}` - '{$player[$classFieldNameCurrent]}'";
358
        $SubSelectQry = "AND `message_type` = '{$current_class}'";
359
360
        $player[static::$snMessageClassList[MSG_TYPE_NEW]['name']] -= $player[static::$snMessageClassList[$current_class]['name']];
361
        $player[static::$snMessageClassList[$current_class]['name']] = 0;
362
      }
363
364
      DBStaticUser::db_user_set_by_id($player['id'], $SubUpdateQry);
365
      $message_query = static::db_message_list_by_owner_and_string($player, $SubSelectQry);
0 ignored issues
show
Bug introduced by
The variable $SubSelectQry does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
366
    }
367
368
    if (sys_get_param_int('return')) {
369
      header('Location: messages.php');
370
      die();
371
    }
372
373
    $template = gettemplate('msg_message_list', true);
374
    static::messageRenderList($current_class, $template, $message_query);
375
376
    $current_class_text = classLocale::$lang['msg_class'][$current_class];
377
378
    $template->assign_vars(array(
379
      "MESSAGE_CLASS"      => $current_class,
380
      "MESSAGE_CLASS_TEXT" => $current_class_text,
381
    ));
382
383
    return $template;
384
  }
385
386
387
  /**
388
   * @param int      $current_class
389
   * @param template $template
390
   * @param          $message_query
391
   */
392
  function messageRenderList($current_class, $template, $message_query) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
393
    while ($message_row = db_fetch($message_query)) {
394
      $template->assign_block_vars('messages', array(
395
        'ID'   => $message_row['message_id'],
396
        'DATE' => date(FMT_DATE_TIME, $message_row['message_time'] + SN_CLIENT_TIME_DIFF),
397
        'FROM' => htmlspecialchars($message_row['message_from']),
398
        'SUBJ' => htmlspecialchars($message_row['message_subject']),
399
        'TEXT' => in_array($message_row['message_type'], array(MSG_TYPE_PLAYER, MSG_TYPE_ALLIANCE)) && $message_row['message_sender']
400
          ? nl2br(htmlspecialchars($message_row['message_text']))
401
          : nl2br($message_row['message_text']),
402
403
        'FROM_ID'        => $message_row['message_sender'],
404
        'SUBJ_SANITIZED' => htmlspecialchars($message_row['message_subject']),
405
        'STYLE'          => $current_class == MSG_TYPE_OUTBOX
406
          ? static::$snMessageClassList[MSG_TYPE_OUTBOX]['name']
407
          : static::$snMessageClassList[$message_row['message_type']]['name'],
408
      ));
409
    }
410
  }
411
412
413
// Messages *************************************************************************************************************
414
  public static function db_message_list_get_last_20($user, $recipient_id) {
415
    return doquery(
416
      "SELECT * FROM {{messages}}
417
        WHERE
418
          `message_type` = '" . MSG_TYPE_PLAYER . "' AND
419
          ((`message_owner` = '{$user['id']}' AND `message_sender` = '{$recipient_id}')
420
          OR
421
          (`message_sender` = '{$user['id']}' AND `message_owner` = '{$recipient_id}')) ORDER BY `message_time` DESC LIMIT 20;");
422
  }
423
424
  public static function db_message_list_delete($user, $query_add) {
425
    doquery("DELETE FROM `{{messages}}` WHERE `message_owner` = '{$user['id']}'{$query_add};");
426
  }
427
428 View Code Duplication
  public static function db_message_list_outbox_by_user_id($user_id) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
429
    $user_id = idval($user_id);
430
    if (empty($user_id)) {
431
      return false;
432
    }
433
434
    return doquery("SELECT {{messages}}.message_id, {{messages}}.message_owner, {{users}}.id AS message_sender, {{messages}}.message_time,
435
          {{messages}}.message_type, {{users}}.username AS message_from, {{messages}}.message_subject, {{messages}}.message_text
436
       FROM
437
         {{messages}} LEFT JOIN {{users}} ON {{users}}.id = {{messages}}.message_owner WHERE `message_sender` = '{$user_id}' AND `message_type` = 1
438
       ORDER BY `message_time` DESC;");
439
  }
440
441
  public static function db_message_list_by_owner_and_string($user, $SubSelectQry) {
442
    return doquery("SELECT * FROM {{messages}} WHERE `message_owner` = '{$user['id']}' {$SubSelectQry} ORDER BY `message_time` DESC;");
443
  }
444
445
  public static function db_message_count_by_owner_and_type($user) {
446
    return doquery("SELECT message_owner, message_type, COUNT(message_owner) AS message_count FROM {{messages}} WHERE `message_owner` = {$user['id']} GROUP BY message_owner, message_type ORDER BY message_owner ASC, message_type;");
447
  }
448
449
  public static function db_message_count_outbox($user) {
450
    $row = doquery("SELECT COUNT(message_sender) AS message_count FROM {{messages}} WHERE `message_sender` = '{$user['id']}' AND message_type = 1 GROUP BY message_sender;", true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
451
452
    return intval($row['message_count']);
453
  }
454
455
  public static function db_message_list_admin_by_type($int_type_selected, $StartRec) {
456
    return doquery("SELECT
457
  message_id as `ID`,
458
  message_from as `FROM`,
459
  message_owner as `OWNER_ID`,
460
  u.username as `OWNER_NAME`,
461
  message_text as `TEXT`,
462
  FROM_UNIXTIME(message_time) as `TIME`
463
FROM
464
  {{messages}} AS m
465
  LEFT JOIN {{users}} AS u ON u.id = m.message_owner " .
466
      ($int_type_selected >= 0 ? "WHERE `message_type` = {$int_type_selected} " : '') .
467
      "ORDER BY
468
  `message_id` DESC
469
LIMIT
470
  {$StartRec}, 25;");
471
  }
472
473
  public static function db_message_insert_all($message_type, $from, $subject, $text) {
474
    return doquery($QryInsertMessage = 'INSERT INTO {{messages}} (`message_owner`, `message_sender`, `message_time`, `message_type`, `message_from`, `message_subject`, `message_text`) ' .
475
      "SELECT `id`, 0, unix_timestamp(now()), {$message_type}, '{$from}', '{$subject}', '{$text}' FROM {{users}}");
476
  }
477
478
  /**
479
   * @param $int_type_selected
480
   *
481
   * @return array|bool|mysqli_result|null
482
   */
483
  public static function db_message_count_by_type($int_type_selected) {
484
    $page_max = doquery('SELECT COUNT(*) AS `max` FROM {{messages}}' . ($int_type_selected >= 0 ? " WHERE `message_type` = {$int_type_selected};" : ''), true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
485
486
    return $page_max;
487
  }
488
489
  /**
490
   * @param $message_delete
491
   */
492
  public static function db_message_list_delete_set($message_delete) {
493
    doquery("DELETE FROM {{messages}} WHERE `message_id` in ({$message_delete});");
494
  }
495
496
  /**
497
   * @param $delete_date
498
   * @param $int_type_selected
499
   */
500
  public static function db_message_list_delete_by_date($delete_date, $int_type_selected) {
501
    doquery("DELETE FROM {{messages}} WHERE message_time <= UNIX_TIMESTAMP('{$delete_date}')" . ($int_type_selected >= 0 ? " AND `message_type` = {$int_type_selected}" : ''));
502
  }
503
504
  /**
505
   * @param $insert_values
506
   */
507
  public static function db_message_insert($insert_values) {
508
    doquery('INSERT INTO {{messages}} (`message_owner`, `message_sender`, `message_time`, `message_type`, `message_from`, `message_subject`, `message_text`) ' .
509
      'VALUES ' . implode(',', $insert_values));
510
  }
511
512
}
513