forked from supernova-ws/SuperNova
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buddy.php
179 lines (153 loc) · 6.74 KB
/
buddy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* buddy.php
* Friend system
*
* v3.0 Fully rewrote by Gorlum for http://supernova.ws
* [!] Full rewrote from scratch
*
* Idea from buddy.php Created by Perberos. All rights reversed (C) 2006
* */
include('common.' . substr(strrchr(__FILE__, '.'), 1));
lng_include('buddy');
$result = array();
try
{
sn_db_transaction_start();
if($buddy_id = sys_get_param_id('buddy_id'))
{
$buddy_row = doquery("SELECT BUDDY_SENDER_ID, BUDDY_OWNER_ID, BUDDY_STATUS FROM {{buddy}} WHERE `BUDDY_ID` = {$buddy_id} LIMIT 1 FOR UPDATE;", true);
if(!is_array($buddy_row))
{
throw new exception('buddy_err_not_exist', ERR_ERROR);
}
switch($mode = sys_get_param_str('mode'))
{
case 'accept':
if($buddy_row['BUDDY_SENDER_ID'] == $user['id'])
{
throw new exception('buddy_err_accept_own', ERR_ERROR);
}
if($buddy_row['BUDDY_OWNER_ID'] != $user['id'])
{
throw new exception('buddy_err_accept_alien', ERR_ERROR);
}
if($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_ACTIVE)
{
throw new exception('buddy_err_accept_already', ERR_WARNING);
}
if($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_DENIED)
{
throw new exception('buddy_err_accept_denied', ERR_ERROR);
}
doquery("UPDATE {{buddy}} SET `BUDDY_STATUS` = " . BUDDY_REQUEST_ACTIVE . " WHERE `BUDDY_ID` = {$buddy_id} LIMIT 1;");
if(mysql_affected_rows($link))
{
msg_send_simple_message($buddy_row['BUDDY_SENDER_ID'], $user['id'], $time_now, MSG_TYPE_PLAYER, $user['username'], $lang['buddy_msg_accept_title'],
sprintf($lang['buddy_msg_accept_text'], $user['username']));
sn_db_transaction_commit();
throw new exception('buddy_err_accept_none', ERR_NONE);
}
else
{
throw new exception('buddy_err_accept_internal', ERR_ERROR);
}
break;
case 'delete':
if($buddy_row['BUDDY_SENDER_ID'] != $user['id'] && $buddy_row['BUDDY_OWNER_ID'] != $user['id'])
{
throw new exception('buddy_err_delete_alien', ERR_ERROR);
}
if($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_ACTIVE) // Existing friendship
{
$ex_friend_id = $buddy_row['BUDDY_SENDER_ID'] == $user['id'] ? $buddy_row['BUDDY_OWNER_ID'] : $buddy_row['BUDDY_SENDER_ID'];
msg_send_simple_message($ex_friend_id, $user['id'], $time_now, MSG_TYPE_PLAYER, $user['username'], $lang['buddy_msg_unfriend_title'],
sprintf($lang['buddy_msg_unfriend_text'], $user['username']));
doquery("DELETE FROM {{buddy}} WHERE `BUDDY_ID` = {$buddy_id} LIMIT 1;");
sn_db_transaction_commit();
throw new exception('buddy_err_unfriend_none', ERR_NONE);
}
elseif($buddy_row['BUDDY_SENDER_ID'] == $user['id']) // Player's outcoming request - either denied or waiting
{
doquery("DELETE FROM {{buddy}} WHERE `BUDDY_ID` = {$buddy_id} LIMIT 1;");
sn_db_transaction_commit();
throw new exception('buddy_err_delete_own', ERR_NONE);
}
elseif($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_WAITING) // Deny incoming request
{
msg_send_simple_message($buddy_row['BUDDY_SENDER_ID'], $user['id'], $time_now, MSG_TYPE_PLAYER, $user['username'], $lang['buddy_msg_deny_title'],
sprintf($lang['buddy_msg_deny_text'], $user['username']));
doquery("UPDATE {{buddy}} SET `BUDDY_STATUS` = " . BUDDY_REQUEST_DENIED . " WHERE `BUDDY_ID` = {$buddy_id} LIMIT 1;");
sn_db_transaction_commit();
throw new exception('buddy_err_deny_none', ERR_NONE);
}
break;
}
}
// New request?
// Checking for user ID - in case if it was request from outside buddy system
if($new_friend_id = sys_get_param_id('request_user_id'))
{
$new_friend_row = db_user_by_id($new_friend_id, true, '`id`, `username`');
}
elseif($new_friend_name = sys_get_param_str_unsafe('request_user_name'))
{
$new_friend_row = db_user_by_username($new_friend_name, true, '`id`, `username`');
$new_friend_name = mysql_real_escape_string($new_friend_name);
}
if($new_friend_row['id'] == $user['id'])
{
unset($new_friend_row);
throw new exception('buddy_err_adding_self', ERR_ERROR);
}
// Checking for user name & request text - in case if it was request to adding new request
if(isset($new_friend_row['id']) && ($new_request_text = sys_get_param_str('request_text')))
{
$check_relation = doquery("SELECT `BUDDY_ID` FROM {{buddy}} WHERE
(`BUDDY_SENDER_ID` = {$user['id']} AND `BUDDY_OWNER_ID` = {$new_friend_row['id']})
OR
(`BUDDY_SENDER_ID` = {$new_friend_row['id']} AND `BUDDY_OWNER_ID` = {$user['id']})
LIMIT 1 FOR UPDATE;"
, true);
if(isset($check_relation['BUDDY_ID']))
{
throw new exception('buddy_err_adding_exists', ERR_WARNING);
}
msg_send_simple_message($new_friend_row['id'], $user['id'], $time_now, MSG_TYPE_PLAYER, $user['username'], $lang['buddy_msg_adding_title'],
sprintf($lang['buddy_msg_adding_text'], $user['username']));
doquery($q = "INSERT INTO {{buddy}} SET `BUDDY_SENDER_ID` = {$user['id']}, `BUDDY_OWNER_ID` = {$new_friend_row['id']}, `BUDDY_REQUEST` = '{$new_request_text}';");
sn_db_transaction_commit();
throw new exception('buddy_err_adding_none', ERR_NONE);
}
}
catch(exception $e)
{
$result[] = array(
'STATUS' => in_array($e->getCode(), array(ERR_NONE, ERR_WARNING, ERR_ERROR)) ? $e->getCode() : ERR_ERROR,
'MESSAGE' => $lang[$e->getMessage()],
);
}
// TODO - Это просто заглушка. Дойдут руки - разобраться, в чём проблема
sn_db_transaction_rollback();
$query = db_buddy_list_by_user($user['id']);
while($row = mysql_fetch_assoc($query))
{
$row['BUDDY_REQUEST'] = sys_bbcodeParse($row['BUDDY_REQUEST']);
$row['BUDDY_ACTIVE'] = $row['BUDDY_STATUS'] == BUDDY_REQUEST_ACTIVE;
$row['BUDDY_DENIED'] = $row['BUDDY_STATUS'] == BUDDY_REQUEST_DENIED;
$row['BUDDY_INCOMING'] = $row['BUDDY_OWNER_ID'] == $user['id'];
$row['BUDDY_ONLINE'] = floor((SN_TIME_NOW - $row['onlinetime']) / 60);
$template_result['.']['buddy'][] = $row;
}
$template_result += array(
'PAGE_HEADER' => $lang['buddy_buddies'],
'PAGE_HINT' => $lang['buddy_hint'],
'USER_ID' => $user['id'],
'REQUEST_USER_ID' => isset($new_friend_row['id']) ? $new_friend_row['id'] : 0,
'REQUEST_USER_NAME' => isset($new_friend_row['username']) ? $new_friend_row['username'] : '',
);
$template_result['.']['result'] = is_array($template_result['.']['result']) ? $template_result['.']['result'] : array();
$template_result['.']['result'] += $result;
$template = gettemplate('buddy', true);
$template->assign_recursive($template_result);
display($template);