-
Notifications
You must be signed in to change notification settings - Fork 0
/
friends.php
259 lines (246 loc) · 9.04 KB
/
friends.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
<?php
include "session.php";
include "postupdate.php";
?>
<div class="container" >
<div class="row">
<div class="col-md-3">
<h3 class="control-label" for="status">Friend Requests</h3>
<?php
//Friend Array
$friendString = "";
$selectFriendsQuery = mysql_query("SELECT friends FROM users WHERE username='$user'");
$friendRow = mysql_fetch_assoc($selectFriendsQuery);
$friendString = $friendRow['friends'];
//Sent Request Array
$query = mysql_query("SELECT requests_sent from users WHERE username = '$user'", $conn);
$requestsSent = mysql_fetch_assoc($query);
$requestSentString = $requestsSent['requests_sent'];
if($requestSentString != ""){
$requestSentArray = explode(",", $requestSentString);
for($i = 0; $i < count($requestSentArray); $i++){
$mapFriends[$requestSentArray[$i]] = 1;
}
}
//Request Array
$query = mysql_query("SELECT requests from users WHERE username = '$user'", $conn);
$requestRow = mysql_fetch_assoc($query);
$requestString = $requestRow['requests'];
$requestArray = array();
if ($requestString != "") {
$requestArray = explode(",",$requestString);
}
$requestSize = count($requestArray);
for($i = 0; $i < $requestSize; $i++){
$value = $requestArray[$i];
$mapFriends[$value] = 1;
echo "<form method='post' name='requestform'>";
echo $value;
echo " <input type='submit' class = 'btn btn-sm btn-success' name='accept' value='Accept'>";
echo " <input type='submit' class = 'btn btn-sm btn-danger' name='reject' value='Reject'>";
echo "<input type = 'hidden' name='requestname' value ='".$value."'>";
echo "<br><br></form>";
}
if (isset($_POST['accept'])) {
$requestname = $_POST['requestname'];
//adding requested person to user friend list
if($friendString != "")
$friendString = $friendString.",".$requestname;
else
$friendString = $requestname;
$query = mysql_query("UPDATE users SET friends = '$friendString' WHERE username = '$user'");
//adding user to requested person friendlist
$selectuserQuery = mysql_query("SELECT friends FROM users WHERE username='$requestname'");
$userRow = mysql_fetch_assoc($selectuserQuery);
$userString = $userRow['friends'];
if($userString != "")
$userString = $userString.",".$user;
else
$userString = $user;
$query = mysql_query("UPDATE users SET friends = '$userString' WHERE username = '$requestname'");
//removing requested person from users requests
$new_string = "";
for($i = 0; $i < $requestSize; $i++){
if($requestArray[$i] != $requestname){
$new_string = $new_string.$requestArray[$i].",";
}
}
$new_string = substr($new_string, 0, sizeof($new_string) - 2);
echo $new_string;
$query = mysql_query("UPDATE users SET requests = '$new_string' WHERE username = '$user'");
//removing user from requested person's request sent
$query = mysql_query("SELECT requests_sent from users WHERE username = '$requestname'", $conn);
$sentRequestRow = mysql_fetch_assoc($query);
$sentRequestString = $sentRequestRow['requests_sent'];
$sentRequestArray = array();
if ($sentRequestString != "") {
$sentRequestArray = explode(",",$sentRequestString);
$sentRequestSize = count($sentRequestArray);
$new_string = "";
$new_string.substr(string, start);
for($i = 0; $i < $sentRequestSize; $i++){
if($sentRequestArray[$i] != $user){
$new_string = $new_string.$sentRequestArray[$i].",";
}
}
$new_string = substr($new_string, 0, sizeof($new_string) - 2);
echo $new_string;
$query = mysql_query("UPDATE users SET requests_sent = '$new_string' WHERE username = '$requestname'");
}
header("Location: friends.php");
}
if (isset($_POST['reject'])) {
$requestname = $_POST['requestname'];
$new_string = "";
for($i = 0; $i < $requestSize; $i++){
if($requestArray[$i] != $requestname){
$new_string = $new_string.$requestArray[$i].",";
}
}
$new_string = substr($new_string, 0, sizeof($new_string) - 2);
echo $new_string;
$query = mysql_query("UPDATE users SET requests = '$new_string' WHERE username = '$user'");
//removing user from requested person's request sent
$query = mysql_query("SELECT requests_sent from users WHERE username = '$requestname'", $conn);
$sentRequestRow = mysql_fetch_assoc($query);
$sentRequestString = $sentRequestRow['requests_sent'];
$sentRequestArray = array();
if ($sentRequestString != "") {
$sentRequestArray = explode(",",$sentRequestString);
$sentRequestSize = count($sentRequestArray);
$new_string = "";
for($i = 0; $i < $sentRequestSize; $i++){
if($sentRequestArray[$i] != $user){
$new_string = $new_string.$sentRequestArray[$i].",";
}
}
$new_string = substr($new_string, 0, sizeof($new_string) - 2);
echo $new_string;
$query = mysql_query("UPDATE users SET requests_sent = '$new_string' WHERE username = '$requestname'");
}
header("Location: friends.php");
}
?>
</div>
<div class="col-md-3 col-md-offset-1">
<h3 class="control-label" for="removingfriends">Friends</h3>
<?php
$friendArray = array();
if ($friendString != "") {
$friendArray = explode(",",$friendString);
}
$size = count($friendArray);
if($size != 0){
for ($i = 0; $i < $size; $i++) {
$value = $friendArray[$i];
$mapFriends[$value] = 1;
?>
<form method="post" name="unfriendform">
<?php echo $value;
echo " <input type='submit' class = 'btn btn-sm btn-danger' name='remove' value='Unfriend'>";
echo "<input type = 'hidden' name='friendname' value ='".$value."'>"
?>
<br><br>
</form>
<?php
}
}
?>
<?php
if (isset($_POST['remove'])) {
$friendname = $_POST['friendname'];
// remove friend from user friendlist
$new_string = "";
if($friendString != ""){
for($i = 0; $i < $size ; $i++){
if($friendname != $friendArray[$i]){
$new_string = $new_string.$friendArray[$i].",";
}
else{
$mapFriends[$friendname] = NULL;
}
}
$new_string = substr($new_string, 0, sizeof($new_string) - 2);
$query = mysql_query("UPDATE users SET friends = '$new_string' WHERE username = '$user'", $conn);
}
// remove user from friend's friendlist
$userString = "";
$selectuserQuery = mysql_query("SELECT friends FROM users WHERE username='$friendname'");
$userRow = mysql_fetch_assoc($selectuserQuery);
$userString = $userRow['friends'];
$userArray = array();
$new_string = "";
if ($userString != "") {
$friendArray = explode(",",$friendString);
$usersize = count($userArray);
for($i = 0; $i < $usersize ; $i++){
if($user != $userArray[$i]){
$new_string = $new_string.$userArray[$i].",";
}
}
$new_string = substr($new_string, 0, sizeof($new_string) - 2);
$query = mysql_query("UPDATE users SET friends = '$new_string' WHERE username = '$friendname'", $conn);
}
header("Location: friends.php");
}
?>
</div>
<div class="col-md-3 col-md-offset-1">
<h3 class="control-label" for="status">All Users</h3>
<?php
$query = mysql_query("SELECT username from users WHERE username != '$user'", $conn);
while($get_users = mysql_fetch_assoc($query)){
$username = $get_users['username'];
$c = 0;
if (!isset($mapFriends[$username])){
if($c > 20)
break;
echo "<form method='post' name='addfriendform'>";
echo $username;
echo " <input type='submit' class = 'btn btn-sm btn-primary' name='add' value='Add'>";
echo "<input type = 'hidden' name='addfriend' value ='".$username."'>";
echo "<br><br></form>";
$c++;
}
}
if (isset($_POST['add'])) {
$friendname = $_POST['addfriend'];
$mapFriends[$friendname] = 1;
$query = mysql_query("SELECT requests from users WHERE username = '$friendname'", $conn);
if($query){
$requests = mysql_fetch_assoc($query);
$requestString = $requests['requests'];
}
if(strstr($requestString, $user)){
$new_string = $requestString;
}
else if($requestString != "")
$new_string = $requestString.",".$user;
else
$new_string = $user;
$query = mysql_query("UPDATE users SET requests = '$new_string' WHERE username = '$friendname'", $conn);
$flag = 0;
$new_string = "";
for($i = 0; $i < count($requestSentArray); $i++){
$value = $requestSentArray[$i];
if($friendname == $value){
$flag = 1;
$new_string = $requestSentString;
break;
}
}
if($flag == 0){
if($requestSentString != "")
$new_string = $requestSentString.",".$friendname;
else
$new_string = $friendname;
}
$query = mysql_query("UPDATE users SET requests_sent = '$new_string' WHERE username = '$user'", $conn);
header("Location: friends.php");
}
?>
</div>
</div>
</div>
</body>
</html>