forked from RetroAchievements/RAWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_db_friend.php
More file actions
445 lines (400 loc) · 12.2 KB
/
_db_friend.php
File metadata and controls
445 lines (400 loc) · 12.2 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<?php
require_once('db.inc.php');
//////////////////////////////////////////////////////////////////////////////////////////
// Friend Functions
function ChangeFriendStatus( $user, $friend, $action )
{
settype( $action, 'integer' );
$query = "SELECT (f.User = '$user') AS Local, f.Friend, f.Friendship
FROM Friends AS f
WHERE (f.User = '$user' && f.Friend = '$friend')
UNION
SELECT (f.User = '$user') AS Local, f.Friend, f.Friendship
FROM Friends AS f
WHERE (f.User = '$friend' && f.Friend = '$user') ";
$dbResult = s_mysql_query( $query );
if( $dbResult !== FALSE )
{
$localFriendState = NULL;
$remoteFriendState = NULL;
while( $data = mysqli_fetch_assoc( $dbResult ) )
{
//var_dump( $data );
if( $data['Local'] == 1 )
{
$localFriendState = $data['Friendship'];
settype( $localFriendState, 'integer' );
}
else //if( $data['Local'] == 0 )
{
$remoteFriendState = $data['Friendship'];
settype( $remoteFriendState, 'integer' );
}
}
if( !isset( $localFriendState ) )
{
// Entry needs adding afresh:
$query = "INSERT INTO Friends VALUES ( '$user', '$friend', $action )";
log_sql( $query );
$dbResult = s_mysql_query( $query );
if( $dbResult !== FALSE )
{
if( isset( $remoteFriendState ) )
{
// Friend already has an entry about us.
if( $remoteFriendState == 1 )
{
// remote friend is already friends: they sent the request perhaps?
if( getAccountDetails( $friend, $friendData ) )
{
if( ( $friendData['websitePrefs'] & (1<<4) ) !== 0 )
{
// Note reverse user and friend (perspective!)
sendFriendEmail( $friend, $friendData['EmailAddress'], 1, $user );
}
else
{
error_log( __FUNCTION__ . " not sending $friend any email about this friend confirm, they don't want emails." );
}
}
return "friendconfirmed";
}
else if( $remoteFriendState == 0 )
{
// remote friend has an entry for this person, but they are not friends.
return "friendadded";
}
else if( $remoteFriendState == -1 )
{
// remote friend has blocked this user.
return "error";
}
}
else
{
// Remote friend hasn't heard about us yet!
if( $action == 1 )
{
// Notify $friend that $user wants to be their friend
if( getAccountDetails( $friend, $friendData ) )
{
if( ( $friendData['websitePrefs'] & (1<<4) ) !== 0 )
{
// Note reverse user and friend (perspective!)
sendFriendEmail( $friend, $friendData['EmailAddress'], 0, $user );
}
else
{
error_log( __FUNCTION__ . " friend request, $friend has elected not to have email about $user adding them as a friend" );
}
}
else
{
error_log( __FUNCTION__ . " friend request, cannot get friend data!" );
log_sql_fail();
}
return "friendrequested";
}
}
}
else
{
log_sql_fail();
return "issues1";
}
}
else //if( isset( $localFriendState ) )
{
// My entry already exists in some form.
if( $localFriendState == $action )
{
// No change:
return "nochange";
}
else
{
// Entry exists already but needs changing to $action:
$query = "UPDATE Friends AS f SET f.Friendship = $action ";
$query.= "WHERE f.User = '$user' AND f.Friend = '$friend' ";
$dbResult = s_mysql_query( $query );
if( $dbResult !== FALSE )
{
if( $localFriendState == -1 && $action == 0 )
return "userunblocked";
if( $action == 0 )
return "friendremoved";
else if( $action == -1 )
return "friendblocked";
else if( $action == 1 )
{
// Notify $friend that $user confirmed their friend request!
if( isset( $remoteFriendState ) && $remoteFriendState == 1 )
{
return "friendconfirmed"; // again
}
else
{
return "friendrequested"; // again
}
}
}
else
{
log_sql_fail();
return "issues2";
}
}
}
}
else
{
error_log( __FUNCTION__ );
error_log( $query );
log_sql_fail();
return "sqlfail";
}
}
function addFriend( $user, $friendToAdd )
{
$query = "SELECT * FROM Friends WHERE (User='$user' AND Friend='$friendToAdd') OR (User='$friendToAdd' AND Friend='$user')";
$dbResult = s_mysql_query( $query );
if( $dbResult !== FALSE )
{
$numRows = mysqli_num_rows($dbResult);
if( $numRows == 0 )
{
// New friend request
// Add as a confirmed friend for me
$query = "INSERT INTO Friends (User, Friend, Friendship) VALUES ( '$user', '$friendToAdd', '1' )";
log_sql( $query );
$dbResult = s_mysql_query( $query );
if( $dbResult == FALSE )
log_sql_fail();
// Add as a pending friend for him
$query = "INSERT INTO Friends (User, Friend, Friendship) VALUES ( '$friendToAdd', '$user', '0' )";
log_sql( $query );
$dbResult = s_mysql_query( $query );
if( $dbResult == FALSE )
log_sql_fail();
return TRUE;
}
else
{
// Friend request already sent. To simply this, just fail, call "confirmFriend" instead!
error_log( __FUNCTION__ . " failed: friend request already sent from user:$user, friend to add:$friendToAdd (numRows: $numRows)" );
return FALSE;
}
}
else
{
error_log( $query );
error_log( __FUNCTION__ . " failed: friend request query failed:$user, friend to add:$friendToAdd" );
return FALSE;
}
}
function confirmFriend( $user, $friendToConfirm )
{
$query = "SELECT * FROM Friends WHERE User='$user' AND Friend='$friendToConfirm'";
$dbResult = s_mysql_query( $query );
if( $dbResult !== FALSE )
{
$numRows = mysqli_num_rows($dbResult);
if( $numRows > 1 )
error_log( __FUNCTION__ . " warning: something's screwed up, $user has more than 1 request to confirm from $friendToAdd" );
if( $numRows == 1 )
{
$query = "UPDATE Friends SET Friendship='1' WHERE User='$user' AND Friend='$friendToConfirm'";
if( s_mysql_query( $query ) !== FALSE )
{
// Accepted successfully :)
return TRUE;
}
else
{
error_log( $query );
error_log( __FUNCTION__ . "query failed: user:$user, friend:$friendToConfirm" );
return FALSE;
}
}
else
{
error_log( $query );
error_log( __FUNCTION__ . " failed: no friendship to confirm? User:$user Friend:$friendToConfirm" );
return FALSE;
}
}
else
{
error_log( $query );
error_log( __FUNCTION__ . " failed: friend request query failed:$user, friend to add:$friendToAdd" );
return FALSE;
}
}
function blockFriend( $user, $friendToAdd )
{
$query = "SELECT * FROM Friends WHERE User='$user' AND Friend='$friendToConfirm'";
$dbResult = s_mysql_query( $query );
if( $dbResult !== FALSE )
{
$numRows = mysqli_num_rows($dbResult);
if( $numRows > 1 )
error_log( __FUNCTION__ . " warning: something's screwed up, $user has more than 1 entry with $friendToAdd" );
if( $numRows == 1 )
{
$query = "UPDATE Friends SET Friendship='-1' WHERE User='$user' AND Friend='$friendToConfirm'";
if( s_mysql_query( $query ) !== FALSE )
{
// Accepted successfully :)
return TRUE;
}
else
{
error_log( $query );
error_log( __FUNCTION__ . "query failed: user:$user, friend:$friendToConfirm" );
return FALSE;
}
}
else
{
error_log( $query );
error_log( __FUNCTION__ . " failed: no friendship to confirm? User:$user Friend:$friendToConfirm" );
return FALSE;
}
}
else
{
error_log( $query );
error_log( __FUNCTION__ . " failed: friend request query failed:$user, friend to add:$friendToAdd" );
return FALSE;
}
}
function IsFriendsWith( $user, $friend )
{
$query = "SELECT * FROM Friends WHERE User='$user' AND Friend='$friend'";
$dbResult = s_mysql_query( $query );
if( $dbResult == FALSE )
return FALSE;
$data = mysqli_fetch_assoc($dbResult);
if( $data == FALSE )
return FALSE;
return ($data['Friendship'] == '1');
}
function getAllFriendsProgress( $user, $gameID, &$friendScoresOut )
{
$friendScoresOut = Array();
// Subquery one: select all friends this user has added:
// Subquery two: select all achievements associated with this game:
// Manual sanitisation, as we need to call multiple functions (and include semicolons)
settype( $gameID, 'integer' );
if( !ctype_alnum( $user ) )
{
error_log( __FUNCTION__ . " called with dodgy looking user: $user" );
log_email( __FUNCTION__ . "failed... user is $user" );
return 0;
}
//s_mysql_query( "CREATE VIEW _FriendList AS SELECT f.Friend FROM Friends as f WHERE f.User = '$user'" );
//s_mysql_query( "CREATE VIEW _ThisAwarded AS SELECT aw.User, aw.AchievementID, aw.Date FROM Awarded AS aw WHERE aw.AchievementID IN ( SELECT ID FROM Achievements WHERE Achievements.GameID = '$gameID' )" );
//$query = "SELECT aw.User, ua.Motto, SUM( ach.Points ) AS TotalPoints, ua.RAPoints, act.LastUpdate
// FROM _ThisAwarded AS aw
// NATURAL JOIN _FriendList
// LEFT JOIN UserAccounts AS ua ON ua.User = aw.User
// LEFT JOIN Achievements AS ach ON ach.ID = aw.AchievementID
// LEFT JOIN Activity AS act ON act.ID = ua.LastActivityID
// WHERE aw.User = _FriendList.Friend
// GROUP BY aw.User
// ORDER BY TotalPoints DESC, aw.User";
// Concatenated queries:
// $query = "SELECT aw.User, ua.Motto, SUM( ach.Points ) AS TotalPoints, ua.RAPoints, act.LastUpdate
// FROM (
// SELECT aw.User, aw.AchievementID, aw.Date FROM Awarded AS aw WHERE aw.AchievementID IN
// ( SELECT ID FROM Achievements WHERE Achievements.GameID = '$gameID' )
// ) AS aw
// NATURAL JOIN ( SELECT f.Friend FROM Friends as f WHERE f.User = '$user' ) AS _FriendList
// LEFT JOIN UserAccounts AS ua ON ua.User = aw.User
// LEFT JOIN Achievements AS ach ON ach.ID = aw.AchievementID
// LEFT JOIN Activity AS act ON act.ID = ua.LastActivityID
// WHERE aw.User = _FriendList.Friend
// GROUP BY aw.User
// ORDER BY TotalPoints DESC, aw.User ";
// Less dependent subqueries :)
$query = "SELECT aw.User, ua.Motto, SUM( ach.Points ) AS TotalPoints, ua.RAPoints, ua.RichPresenceMsg, act.LastUpdate
FROM
(
SELECT aw.User, aw.AchievementID, aw.Date
FROM Awarded AS aw
RIGHT JOIN
(
SELECT ID
FROM Achievements AS ach
WHERE ach.GameID = '$gameID' AND ach.Flags = 3
) AS Inner1 ON Inner1.ID = aw.AchievementID
) AS aw
NATURAL JOIN
(
SELECT f.Friend
FROM Friends AS f
WHERE f.User = '$user'
) AS _FriendList
LEFT JOIN UserAccounts AS ua ON ua.User = aw.User
LEFT JOIN Achievements AS ach ON ach.ID = aw.AchievementID
LEFT JOIN Activity AS act ON act.ID = ua.LastActivityID
WHERE aw.User = _FriendList.Friend
GROUP BY aw.User
ORDER BY TotalPoints DESC, aw.User";
$dbResult = s_mysql_query( $query );
$numFriends = 0;
if( $dbResult !== FALSE )
{
while( $db_entry = mysqli_fetch_assoc($dbResult) )
{
if( !isset( $friendScoresOut[ $db_entry['User'] ] ) )
$friendScoresOut[ $db_entry['User'] ] = 0;
// Tally up our friend's scores
$friendScoresOut[ $db_entry['User'] ] = $db_entry;
$numFriends++;
}
}
else
{
error_log( $query );
error_log( __FUNCTION__ . " failed3: user:$user gameID:$gameID" );
}
//s_mysql_query( "DROP VIEW _FriendList" );
//s_mysql_query( "DROP VIEW _ThisAwarded" );
return $numFriends;
}
function GetFriendList( $user )
{
$friendList = array();
$query = "SELECT f.Friend, ua.RAPoints, ua.RichPresenceMsg AS LastSeen
FROM Friends AS f
LEFT JOIN UserAccounts AS ua ON ua.User = f.Friend
WHERE f.User='$user'
ORDER BY ua.LastActivityID DESC ";
$dbResult = s_mysql_query( $query );
if( $dbResult == FALSE )
{
error_log( $query );
error_log( __FUNCTION__ . " failed: user:$user" );
}
else
{
while( $db_entry = mysqli_fetch_assoc( $dbResult ) )
{
if( !isset( $db_entry["LastSeen"] ) || $db_entry["LastSeen"] == "" )
$db_entry["LastSeen"] = "_";
$friendList[] = $db_entry;
}
}
return $friendList;
}
// function getLastKnownActivity( $user )
// {
// $query = "SELECT act.ID, act.timestamp, act.activitytype, act.User, act.data, act.data2
// FROM UserAccounts AS ua
// LEFT JOIN Activity AS act ON act.ID = ua.LastActivityID
// WHERE ua.User = '$user'";
// $dbResult = s_mysql_query( $query );
// return mysqli_fetch_assoc( $dbResult );
// }
?>