-
Notifications
You must be signed in to change notification settings - Fork 0
/
friend.php
298 lines (269 loc) · 9 KB
/
friend.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
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
<?php
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2008 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
// stuff related to friends
require_once("../inc/forum_db.inc");
require_once("../inc/forum.inc");
require_once("../inc/notification.inc");
check_get_args(array("target_userid", "userid", "action"));
text_counter_script();
// see if there's already a request,
// and whether the notification record is there
//
function check_pending($user, $destuser) {
$friend = BoincFriend::lookup($user->id, $destuser->id);
if ($friend) {
if ($friend->reciprocated) {
error_page(tra("Already friends"));
}
$notify = BoincNotify::lookup($destuser->id, NOTIFY_FRIEND_REQ, $user->id);
if ($notify) {
page_head(tra("Request pending"));
$t = date_str($friend->create_time);
echo tra("You requested friendship with %1 on %2.", $destuser->name,$t) . "
<p>" .
tra("This request is pending.");
page_tail();
exit();
}
BoincFriend::delete($user->id, $destuser->id);
}
}
function check_ignoring($srcuser, $destuser) {
BoincForumPrefs::lookup($destuser);
if (is_ignoring($destuser, $srcuser)) {
error_page(tra("%1 is not accepting friendship requests from you",$destuser->name));
}
}
// user has clicked "add to friends". Ask them if they really mean it.
//
function handle_add($user) {
$destid = get_int('userid');
if ($destid == $user->id) {
error_page(tra("You can't be friends with yourself"));
}
$destuser = BoincUser::lookup_id($destid);
if (!$destuser) error_page("No such user");
check_pending($user, $destuser);
check_ignoring($user, $destuser);
page_head(tra("Send friend request to $destuser->name"));
echo "
<form method=post action=friend.php>
<input type=hidden name=userid value=$destid>
<input type=hidden name=action value=add_confirm>" .
tra("Add an optional message:") ."
<p><p>
".textarea_with_counter("message", 250, "")."
<p>
<input class=\"btn btn-primary\" type=submit value=\"".tra("OK")."\">
</form>
";
page_tail();
}
// User really means it. Make DB entry and send notification
//
function handle_add_confirm($user) {
$destid = post_int('userid');
$destuser = BoincUser::lookup_id($destid);
if (!$destuser) error_page("No such user");
check_pending($user, $destuser);
check_ignoring($user, $destuser);
$msg = post_str('message', true);
if ($msg) $msg = sanitize_tags(BoincDb::escape_string($msg));
$now = time();
$ret = BoincFriend::replace(
"user_src=$user->id, user_dest=$destid, message='$msg', create_time=$now, reciprocated=0"
);
if (!$ret) {
error_page(tra("Database error"));
}
$now = time();
$type = NOTIFY_FRIEND_REQ;
BoincNotify::replace(
"userid=$destid, create_time=$now, type=$type, opaque=$user->id, sent_by_email=0"
);
email_if_immediate($destuser);
page_head(tra("Friend request sent"));
echo tra("We have notified %1 of your request.","<b>".$destuser->name."</b>");
page_tail();
}
// Show destination user the details of request, ask if they accept
//
function handle_query($user) {
$target_userid = get_int('target_userid', true);
if ($target_userid && $target_userid != $user->id) {
$target_user = BoincUser::lookup_id($target_userid);
page_head(tra("Please log in as %1", $target_user->name));
echo tra("You must log in as %1 to view this friend request",
$target_user->name
);
page_tail();
exit;
}
$srcid = get_int('userid');
$srcuser = BoincUser::lookup_id($srcid);
if (!$srcuser) error_page("No such user");
$friend = BoincFriend::lookup($srcid, $user->id);
if (!$friend) error_page("Request not found");
page_head(tra("Friend request"));
echo time_str($friend->create_time)."<p>\n";
$x = user_links($srcuser, BADGE_HEIGHT_MEDIUM);
echo tra("%1 has requested friendship with you.", $x);
if (strlen($friend->message)) {
echo "<p>".tra("%1 says: %2", $srcuser->name, $friend->message)."</p>";
}
echo "<p>";
show_button(
"friend.php?action=accept&userid=".$srcid, tra("Accept friendship"),
tra("Click accept if %1 is in fact a friend",
$srcuser->name)
);
echo " ";
show_button(
"friend.php?action=ignore&userid=".$srcid, tra("Decline"),
tra("Click decline if %1 is not a friend",
$srcuser->name),
"btn-sm btn-warning"
);
echo " <p>
";
page_tail();
}
// Here if they accepted
//
function handle_accept($user) {
$srcid = get_int('userid');
$srcuser = BoincUser::lookup_id($srcid);
if (!$srcuser) error_page("No such user");
$friend = BoincFriend::lookup($srcid, $user->id);
if (!$friend) {
error_page("No request");
}
$friend->update("reciprocated=1");
// "accept message" not implemented in interface yet
$msg = post_str('message', true);
if ($msg) $msg = sanitize_tags(BoincDb::escape_string($msg));
$now = time();
$ret = BoincFriend::replace("user_src=$user->id, user_dest=$srcid, message='$msg', create_time=$now, reciprocated=1");
if (!$ret) {
error_page(tra("Database error"));
}
$type = NOTIFY_FRIEND_ACCEPT;
BoincNotify::replace(
"userid=$srcid, create_time=$now, type=$type, opaque=$user->id, sent_by_email=0"
);
email_if_immediate($srcuser);
$notify = BoincNotify::lookup($user->id, NOTIFY_FRIEND_REQ, $srcid);
if ($notify) {
$notify->delete();
}
page_head(tra("Friendship confirmed"));
echo tra("Your friendship with %1 has been confirmed.","<b>" . $srcuser->name ."</b>");
page_tail();
}
// Here if they declined
//
function handle_ignore($user) {
$srcid = get_int('userid');
$srcuser = BoincUser::lookup_id($srcid);
if (!$srcuser) error_page("No such user");
$friend = BoincFriend::lookup($srcid, $user->id);
if (!$friend) {
error_page("No request");
}
$notify = BoincNotify::lookup($user->id, NOTIFY_FRIEND_REQ, $srcid);
if ($notify) {
$notify->delete();
}
page_head(tra("Friendship declined"));
echo tra("You have declined friendship with %1","<b>".$srcuser->name."</b>");
page_tail();
}
// Here if initiator clicked on "confirmed" notification.
// Delete notification
//
function handle_accepted($user) {
$destid = get_int('userid');
$destuser = BoincUser::lookup_id($destid);
if (!$destuser) error_page("No such user");
$notify = BoincNotify::lookup($user->id, NOTIFY_FRIEND_ACCEPT, $destid);
if ($notify) {
$notify->delete();
}
page_head(tra("Friend confirmed"));
echo tra("You are now friends with %1.",$destuser->name);
page_tail();
}
function handle_cancel_confirm($user) {
$destid = get_int('userid');
$destuser = BoincUser::lookup_id($destid);
if (!$destuser) error_page("No such user");
page_head(tra("Cancel friendship?"));
echo
tra("Are you sure you want to cancel your friendship with %1?",
$destuser->name
) ."<p>\n"
;
show_button("friend.php?action=cancel&userid=$destid", tra("Yes"), tra("Cancel friendship"));
show_button(USER_HOME, tra("No"), tra("Stay friends"));
echo "</ul>";
page_tail();
}
function handle_cancel($user) {
$destid = get_int('userid');
$destuser = BoincUser::lookup_id($destid);
if (!$destuser) error_page("No such user");
BoincFriend::delete($user->id, $destid);
page_head(tra("Friendship cancelled"));
echo tra("Your friendship with %1 has been cancelled.",$destuser->name);
page_tail();
}
// "home page" has Requests area
// (icon) N friend request(s)
$user = get_logged_in_user();
update_visit_time($user);
$action = get_str('action', true);
if (!$action) $action = post_str('action');
switch ($action) {
case 'add':
handle_add($user);
break;
case 'add_confirm':
handle_add_confirm($user);
break;
case 'query':
handle_query($user);
break;
case 'accept':
handle_accept($user);
break;
case 'accepted':
handle_accepted($user);
break;
case 'ignore':
handle_ignore($user);
break;
case 'cancel_confirm':
handle_cancel_confirm($user);
break;
case 'cancel':
handle_cancel($user);
break;
default:
error_page("Unknown action");
}
?>