Completed
Push — work-fleets ( e04805...964a94 )
by SuperNova.WS
05:41
created

DBStaticMessages   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 197
rs 8.2857
ccs 0
cts 66
cp 0
wmc 39
lcom 1
cbo 4

15 Methods

Rating   Name   Duplication   Size   Complexity  
C msg_send_simple_message() 0 70 18
A msg_ali_send() 0 19 4
A msgSendFromAdmin() 0 3 1
A db_message_list_get_last_20() 0 9 1
A db_message_list_delete() 0 3 1
A db_message_list_outbox_by_user_id() 0 10 2
A db_message_list_by_owner_and_string() 0 3 1
A db_message_count_by_owner_and_type() 0 3 1
A db_message_count_outbox() 0 5 1
A db_message_list_admin_by_type() 0 17 2
A db_message_insert_all() 0 4 1
A db_message_count_by_type() 0 5 2
A db_message_list_delete_set() 0 3 1
A db_message_list_delete_by_date() 0 3 2
A db_message_insert() 0 4 1
1
<?php
2
3
class DBStaticMessages {
4
5
  public static function msg_send_simple_message($owners, $sender, $timestamp, $message_type, $from, $subject, $text, $escaped = false, $force = false) {
6
    global $user, $sn_message_class_list;
7
8
    if (!$owners) {
9
      return;
10
    }
11
12
    $timestamp = $timestamp ? $timestamp : SN_TIME_NOW;
13
    $sender = intval($sender);
14
    if (!is_array($owners)) {
15
      $owners = array($owners);
16
    }
17
18
    if (!$escaped) {
19
      $from = db_escape($from);
20
      $subject = db_escape($subject);
21
      $text = db_escape($text);
22
    }
23
24
    $text_unescaped = stripslashes(str_replace(array('\r\n', "\r\n"), "<br />", $text));
25
26
    $message_class = $sn_message_class_list[$message_type];
27
    $message_class_email = $message_class['email'];
28
    $message_class_switchable = $message_class['switchable'];
29
    $message_class_name = $message_class['name'];
30
31
    $message_class_name_total = $sn_message_class_list[MSG_TYPE_NEW]['name'];
32
33
    if ($owners[0] == '*') {
34
      if ($user['authlevel'] < 3) {
35
        return false;
36
      }
37
      // TODO Добавить $timestamp - рассылка может быть и отсроченной
38
      // TODO Добавить $sender - рассылка может быть и от кого-то
39
      DBStaticMessages::db_message_insert_all($message_type, $from, $subject, $text);
40
      $owners = array();
41
    } else {
42
      $insert_values = array();
43
      $insert_template = "('%u'," . str_replace('%', '%%', " '{$sender}', '{$timestamp}', '{$message_type}', '{$from}', '{$subject}', '{$text}')");
44
45
      foreach ($owners as $owner) {
46
        if ($user['id'] != $owner) {
47
          $owner_row = DBStaticUser::db_user_by_id($owner);
48
        } else {
49
          $owner_row = $user;
50
        }
51
        sys_user_options_unpack($owner_row);
52
53
        if ($force || !$message_class_switchable || $owner_row["opt_{$message_class_name}"]) {
54
          $insert_values[] = sprintf($insert_template, $owner);
55
        }
56
57
        if ($message_class_email && classSupernova::$config->game_email_pm && $owner_row["opt_email_{$message_class_name}"]) {
58
          @$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...
59
        }
60
      }
61
62
      if (empty($insert_values)) {
63
        return;
64
      }
65
66
      DBStaticMessages::db_message_insert($insert_values);
67
    }
68
    DBStaticUser::db_user_list_set_mass_mail($owners, "`{$message_class_name}` = `{$message_class_name}` + 1, `{$message_class_name_total}` = `{$message_class_name_total}` + 1");
69
70
    if (in_array($user['id'], $owners) || $owners[0] == '*') {
71
      $user[$message_class_name]++;
72
      $user[$message_class_name_total]++;
73
    }
74
  }
75
76
  public static function msg_ali_send($message, $subject, $ally_rank_id = 0, $ally_id = 0) {
77
    global $user;
78
79
    $ally_id = $ally_id ? $ally_id : $user['ally_id'];
80
81
    $sendList = array();
82
    $list = '';
83
    $query = DBStaticUser::db_user_list(
84
      "ally_id = '{$ally_id}'" . ($ally_rank_id >= 0 ? " AND ally_rank_id = {$ally_rank_id}" : ''),
85
      false, 'id, username');
86
    foreach ($query as $u) {
87
      $sendList[] = $u['id'];
88
      $list .= "<br>{$u['username']} ";
89
    }
90
91
    static::msg_send_simple_message($sendList, $user['id'], SN_TIME_NOW, MSG_TYPE_ALLIANCE, $user['username'], $subject, $message, true);
92
93
    return $list;
94
  }
95
96
97
  public static function msgSendFromAdmin($owners, $subject, $text, $escaped = false, $force = false) {
98
    static::msg_send_simple_message($owners, 0, SN_TIME_NOW, MSG_TYPE_ADMIN, classLocale::$lang['sys_administration'], $subject, $text, $escaped, $force);
99
  }
100
101
// Messages *************************************************************************************************************
102
  public static function db_message_list_get_last_20($user, $recipient_id) {
103
    return doquery(
104
      "SELECT * FROM {{messages}}
105
        WHERE
106
          `message_type` = '" . MSG_TYPE_PLAYER . "' AND
107
          ((`message_owner` = '{$user['id']}' AND `message_sender` = '{$recipient_id}')
108
          OR
109
          (`message_sender` = '{$user['id']}' AND `message_owner` = '{$recipient_id}')) ORDER BY `message_time` DESC LIMIT 20;");
110
  }
111
112
  public static function db_message_list_delete($user, $query_add) {
113
    doquery("DELETE FROM `{{messages}}` WHERE `message_owner` = '{$user['id']}'{$query_add};");
114
  }
115
116
  public static function db_message_list_outbox_by_user_id($user_id) {
117
    // return ($user_id = intval($user_id))
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% 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...
118
    return ($user_id = idval($user_id))
119
      ? doquery("SELECT {{messages}}.message_id, {{messages}}.message_owner, {{users}}.id AS message_sender, {{messages}}.message_time,
120
          {{messages}}.message_type, {{users}}.username AS message_from, {{messages}}.message_subject, {{messages}}.message_text
121
       FROM
122
         {{messages}} LEFT JOIN {{users}} ON {{users}}.id = {{messages}}.message_owner WHERE `message_sender` = '{$user_id}' AND `message_type` = 1
123
       ORDER BY `message_time` DESC;")
124
      : false;
125
  }
126
127
  public static function db_message_list_by_owner_and_string($user, $SubSelectQry) {
128
    return doquery("SELECT * FROM {{messages}} WHERE `message_owner` = '{$user['id']}' {$SubSelectQry} ORDER BY `message_time` DESC;");
129
  }
130
131
  public static function db_message_count_by_owner_and_type($user) {
132
    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;");
133
  }
134
135
  public static function db_message_count_outbox($user) {
136
    $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...
137
138
    return intval($row['message_count']);
139
  }
140
141
  public static function db_message_list_admin_by_type($int_type_selected, $StartRec) {
142
    return doquery("SELECT
143
  message_id as `ID`,
144
  message_from as `FROM`,
145
  message_owner as `OWNER_ID`,
146
  u.username as `OWNER_NAME`,
147
  message_text as `TEXT`,
148
  FROM_UNIXTIME(message_time) as `TIME`
149
FROM
150
  {{messages}} AS m
151
  LEFT JOIN {{users}} AS u ON u.id = m.message_owner " .
152
      ($int_type_selected >= 0 ? "WHERE `message_type` = {$int_type_selected} " : '') .
153
      "ORDER BY
154
  `message_id` DESC
155
LIMIT
156
  {$StartRec}, 25;");
157
  }
158
159
  public static function db_message_insert_all($message_type, $from, $subject, $text) {
160
    return doquery($QryInsertMessage = 'INSERT INTO {{messages}} (`message_owner`, `message_sender`, `message_time`, `message_type`, `message_from`, `message_subject`, `message_text`) ' .
161
      "SELECT `id`, 0, unix_timestamp(now()), {$message_type}, '{$from}', '{$subject}', '{$text}' FROM {{users}}");
162
  }
163
164
  /**
165
   * @param $int_type_selected
166
   *
167
   * @return array|bool|mysqli_result|null
168
   */
169
  public static function db_message_count_by_type($int_type_selected) {
170
    $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...
171
172
    return $page_max;
173
  }
174
175
  /**
176
   * @param $message_delete
177
   */
178
  public static function db_message_list_delete_set($message_delete) {
179
    doquery("DELETE FROM {{messages}} WHERE `message_id` in ({$message_delete});");
180
  }
181
182
  /**
183
   * @param $delete_date
184
   * @param $int_type_selected
185
   */
186
  public static function db_message_list_delete_by_date($delete_date, $int_type_selected) {
187
    doquery("DELETE FROM {{messages}} WHERE message_time <= UNIX_TIMESTAMP('{$delete_date}')" . ($int_type_selected >= 0 ? " AND `message_type` = {$int_type_selected}" : ''));
188
  }
189
190
  /**
191
   * @param $insert_values
192
   */
193
  public static function db_message_insert($insert_values) {
194
    doquery('INSERT INTO {{messages}} (`message_owner`, `message_sender`, `message_time`, `message_type`, `message_from`, `message_subject`, `message_text`) ' .
195
      'VALUES ' . implode(',', $insert_values));
196
  }
197
198
199
}