Issues (1369)

buddy.php (10 issues)

Severity
1
<?php
2
3
/**
4
 * buddy.php
5
 *   Friend system
6
 *
7
 * v3.0 Fully rewrote by Gorlum for http://supernova.ws
8
 *   [!] Full rewrote from scratch
9
 *
10
 * Idea from buddy.php Created by Perberos. All rights reversed (C) 2006
11
 * */
12
13
use DBAL\db_mysql;
14
15
include('common.' . substr(strrchr(__FILE__, '.'), 1));
16
17
lng_include('buddy');
18
19
$result = array();
20
try {
21
  db_mysql::db_transaction_start();
22
23
  if ($buddy_id = sys_get_param_id('buddy_id')) {
24
    $buddy_row = doquery("SELECT BUDDY_SENDER_ID, BUDDY_OWNER_ID, BUDDY_STATUS FROM {{buddy}} WHERE `BUDDY_ID` = {$buddy_id} LIMIT 1 FOR UPDATE;", 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

24
    $buddy_row = /** @scrutinizer ignore-deprecated */ doquery("SELECT BUDDY_SENDER_ID, BUDDY_OWNER_ID, BUDDY_STATUS FROM {{buddy}} WHERE `BUDDY_ID` = {$buddy_id} LIMIT 1 FOR UPDATE;", true);
Loading history...
25
    if (!is_array($buddy_row)) {
26
      throw new exception('buddy_err_not_exist', ERR_ERROR);
27
    }
28
29
    switch ($mode = sys_get_param_str('mode')) {
30
      case 'accept':
31
        if ($buddy_row['BUDDY_SENDER_ID'] == $user['id']) {
32
          throw new exception('buddy_err_accept_own', ERR_ERROR);
33
        }
34
35
        if ($buddy_row['BUDDY_OWNER_ID'] != $user['id']) {
36
          throw new exception('buddy_err_accept_alien', ERR_ERROR);
37
        }
38
39
        if ($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_ACTIVE) {
40
          throw new exception('buddy_err_accept_already', ERR_WARNING);
41
        }
42
43
        if ($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_DENIED) {
44
          throw new exception('buddy_err_accept_denied', ERR_ERROR);
45
        }
46
47
        doquery("UPDATE {{buddy}} SET `BUDDY_STATUS` = " . BUDDY_REQUEST_ACTIVE . " WHERE `BUDDY_ID` = {$buddy_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

47
        /** @scrutinizer ignore-deprecated */ doquery("UPDATE {{buddy}} SET `BUDDY_STATUS` = " . BUDDY_REQUEST_ACTIVE . " WHERE `BUDDY_ID` = {$buddy_id} LIMIT 1;");
Loading history...
48
        if (SN::$db->db_affected_rows()) {
49
          msg_send_simple_message($buddy_row['BUDDY_SENDER_ID'], $user['id'], SN_TIME_NOW, MSG_TYPE_PLAYER, $user['username'], $lang['buddy_msg_accept_title'],
50
            sprintf($lang['buddy_msg_accept_text'], $user['username']));
51
          db_mysql::db_transaction_commit();
52
          throw new exception('buddy_err_accept_none', ERR_NONE);
53
        } else {
54
          throw new exception('buddy_err_accept_internal', ERR_ERROR);
55
        }
56
      break;
57
58
      case 'delete':
59
        if ($buddy_row['BUDDY_SENDER_ID'] != $user['id'] && $buddy_row['BUDDY_OWNER_ID'] != $user['id']) {
60
          throw new exception('buddy_err_delete_alien', ERR_ERROR);
61
        }
62
63
        if ($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_ACTIVE) // Existing friendship
64
        {
65
          $ex_friend_id = $buddy_row['BUDDY_SENDER_ID'] == $user['id'] ? $buddy_row['BUDDY_OWNER_ID'] : $buddy_row['BUDDY_SENDER_ID'];
66
67
          msg_send_simple_message($ex_friend_id, $user['id'], SN_TIME_NOW, MSG_TYPE_PLAYER, $user['username'], $lang['buddy_msg_unfriend_title'],
68
            sprintf($lang['buddy_msg_unfriend_text'], $user['username']));
69
70
          doquery("DELETE FROM {{buddy}} WHERE `BUDDY_ID` = {$buddy_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

70
          /** @scrutinizer ignore-deprecated */ doquery("DELETE FROM {{buddy}} WHERE `BUDDY_ID` = {$buddy_id} LIMIT 1;");
Loading history...
71
          db_mysql::db_transaction_commit();
72
          throw new exception('buddy_err_unfriend_none', ERR_NONE);
73
        } elseif ($buddy_row['BUDDY_SENDER_ID'] == $user['id']) // Player's outcoming request - either denied or waiting
74
        {
75
          doquery("DELETE FROM {{buddy}} WHERE `BUDDY_ID` = {$buddy_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

75
          /** @scrutinizer ignore-deprecated */ doquery("DELETE FROM {{buddy}} WHERE `BUDDY_ID` = {$buddy_id} LIMIT 1;");
Loading history...
76
          db_mysql::db_transaction_commit();
77
          throw new exception('buddy_err_delete_own', ERR_NONE);
78
        } elseif ($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_WAITING) // Deny incoming request
79
        {
80
          msg_send_simple_message($buddy_row['BUDDY_SENDER_ID'], $user['id'], SN_TIME_NOW, MSG_TYPE_PLAYER, $user['username'], $lang['buddy_msg_deny_title'],
81
            sprintf($lang['buddy_msg_deny_text'], $user['username']));
82
83
          doquery("UPDATE {{buddy}} SET `BUDDY_STATUS` = " . BUDDY_REQUEST_DENIED . " WHERE `BUDDY_ID` = {$buddy_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

83
          /** @scrutinizer ignore-deprecated */ doquery("UPDATE {{buddy}} SET `BUDDY_STATUS` = " . BUDDY_REQUEST_DENIED . " WHERE `BUDDY_ID` = {$buddy_id} LIMIT 1;");
Loading history...
84
          db_mysql::db_transaction_commit();
85
          throw new exception('buddy_err_deny_none', ERR_NONE);
86
        }
87
      break;
88
    }
89
  }
90
91
  // New request?
92
  // Checking for user ID - in case if it was request from outside buddy system
93
  if ($new_friend_id = sys_get_param_id('request_user_id')) {
94
    $new_friend_row = db_user_by_id($new_friend_id, true);
0 ignored issues
show
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

94
    $new_friend_row = /** @scrutinizer ignore-deprecated */ db_user_by_id($new_friend_id, true);
Loading history...
95
  } elseif ($new_friend_name = sys_get_param_str_unsafe('request_user_name')) {
96
    $new_friend_row  = db_user_by_username($new_friend_name);
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

96
    $new_friend_row  = /** @scrutinizer ignore-deprecated */ db_user_by_username($new_friend_name);
Loading history...
97
    $new_friend_name = SN::$db->db_escape($new_friend_name);
98
  }
99
100
  if ($new_friend_row['id'] == $user['id']) {
101
    unset($new_friend_row);
102
    throw new exception('buddy_err_adding_self', ERR_ERROR);
103
  }
104
105
  // Checking for user name & request text - in case if it was request to adding new request
106
  if (isset($new_friend_row['id']) && ($new_request_text = sys_get_param_str('request_text'))) {
107
    $check_relation = doquery("SELECT `BUDDY_ID` FROM {{buddy}} WHERE
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

107
    $check_relation = /** @scrutinizer ignore-deprecated */ doquery("SELECT `BUDDY_ID` FROM {{buddy}} WHERE
Loading history...
108
      (`BUDDY_SENDER_ID` = {$user['id']} AND `BUDDY_OWNER_ID` = {$new_friend_row['id']})
109
      OR
110
      (`BUDDY_SENDER_ID` = {$new_friend_row['id']} AND `BUDDY_OWNER_ID` = {$user['id']})
111
      LIMIT 1 FOR UPDATE;"
112
      , true);
113
    if (isset($check_relation['BUDDY_ID'])) {
114
      throw new exception('buddy_err_adding_exists', ERR_WARNING);
115
    }
116
117
    msg_send_simple_message($new_friend_row['id'], $user['id'], SN_TIME_NOW, MSG_TYPE_PLAYER, $user['username'], $lang['buddy_msg_adding_title'],
118
      sprintf($lang['buddy_msg_adding_text'], $user['username']));
119
120
    doquery($q = "INSERT INTO {{buddy}} SET `BUDDY_SENDER_ID` = {$user['id']}, `BUDDY_OWNER_ID` = {$new_friend_row['id']}, `BUDDY_REQUEST` = '{$new_request_text}';");
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

120
    /** @scrutinizer ignore-deprecated */ doquery($q = "INSERT INTO {{buddy}} SET `BUDDY_SENDER_ID` = {$user['id']}, `BUDDY_OWNER_ID` = {$new_friend_row['id']}, `BUDDY_REQUEST` = '{$new_request_text}';");
Loading history...
121
    db_mysql::db_transaction_commit();
122
    throw new exception('buddy_err_adding_none', ERR_NONE);
123
  }
124
} catch (exception $e) {
125
  $result[] = array(
126
    'STATUS'  => in_array($e->getCode(), array(ERR_NONE, ERR_WARNING, ERR_ERROR)) ? $e->getCode() : ERR_ERROR,
127
    'MESSAGE' => $lang[$e->getMessage()],
128
  );
129
}
130
// TODO - Это просто заглушка. Дойдут руки - разобраться, в чём проблема
131
db_mysql::db_transaction_rollback();
132
133
$query = db_buddy_list_by_user($user['id']);
134
while ($row = db_fetch($query)) {
0 ignored issues
show
Deprecated Code introduced by
The function db_fetch() 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

134
while ($row = /** @scrutinizer ignore-deprecated */ db_fetch($query)) {
Loading history...
135
  $row['BUDDY_REQUEST'] = HelperString::nl2br($row['BUDDY_REQUEST']);
136
137
  $row['BUDDY_ACTIVE']   = $row['BUDDY_STATUS'] == BUDDY_REQUEST_ACTIVE;
138
  $row['BUDDY_DENIED']   = $row['BUDDY_STATUS'] == BUDDY_REQUEST_DENIED;
139
  $row['BUDDY_INCOMING'] = $row['BUDDY_OWNER_ID'] == $user['id'];
140
  $row['BUDDY_ONLINE']   = floor((SN_TIME_NOW - $row['onlinetime']) / 60);
141
142
  $template_result['.']['buddy'][] = $row;
143
}
144
145
$template_result += array(
146
  'PAGE_HEADER'       => $lang['buddy_buddies'],
147
  'PAGE_HINT'         => $lang['buddy_hint'],
148
  'USER_ID'           => $user['id'],
149
  'REQUEST_USER_ID'   => isset($new_friend_row['id']) ? $new_friend_row['id'] : 0,
150
  'REQUEST_USER_NAME' => isset($new_friend_row['username']) ? $new_friend_row['username'] : '',
151
);
152
153
$template_result['.']['result'] = is_array($template_result['.']['result']) ? $template_result['.']['result'] : array();
154
$template_result['.']['result'] += $result;
155
156
$template = SnTemplate::gettemplate('buddy', true);
157
$template->assign_recursive($template_result);
158
159
SnTemplate::display($template);
160