-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.php
More file actions
618 lines (540 loc) · 33.9 KB
/
Copy pathprofile.php
File metadata and controls
618 lines (540 loc) · 33.9 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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
<?php
date_default_timezone_set("America/New_York");
$timestamp = date("m/d/Y h:i:sa");
//Get logged in user. If no user is logged in, redirect to login page.
session_start();
if (!isset($_SESSION["loggedInUser"])){
header("Location:login.php");}
else{
$loggedInUser = $_SESSION["loggedInUser"];}
//Connect to database
$serverName = "localhost\sqlexpress";
$connectionInfo = array("Database"=>"social_network", "UID"=>"ben", "PWD"=>"password123");
$conn = sqlsrv_connect($serverName, $connectionInfo);
//Get userID of loggedinUser
$getCurrentUserIDQuery = "SELECT id FROM users WHERE username = '$loggedInUser'";
$getCurrentUserID = sqlsrv_query($conn, $getCurrentUserIDQuery, array());
$currentUserID = sqlsrv_fetch_array($getCurrentUserID);
$currentUserID = $currentUserID[0];
//Check if user was selected (on this page, user should always be selected)
if (isset($_GET["selectedUser"])){
$selectedUser = $_GET["selectedUser"];
//Get userID and profile pic of selectedUser
$getSelectedUserIDQuery = "SELECT id, profile_pic FROM users WHERE username = '$selectedUser'";
$getSelectedUserID = sqlsrv_query($conn, $getSelectedUserIDQuery, array());
$selectedUserInfo = sqlsrv_fetch_array($getSelectedUserID);
$selectedUserID = $selectedUserInfo[0];
$selectedUserProfilePic = $selectedUserInfo[1];
}
//If user sent a friend invite
if (isset($_GET["addFriend"])){
$addFriendQuery = "INSERT INTO friends VALUES ('$currentUserID', '$selectedUserID', 'False')";
$addFriend = sqlsrv_query($conn, $addFriendQuery);
}
//If user accepted a friend invite
if (isset($_GET["acceptFriend"])){
$acceptFriendQuery = "UPDATE friends SET accepted = 'True' WHERE userid = '$selectedUserID' AND friendid = '$currentUserID'";
$acceptFriend = sqlsrv_query($conn, $acceptFriendQuery);
}
//If user just unfriended $selectedUser
if (isset($_GET["unfriend"])){
$unfriendQuery = "DELETE FROM friends WHERE (userid = '$currentUserID' AND friendid = '$selectedUserID') OR (userid = '$selectedUserID' AND friendid = '$currentUserID')";
$unfriend = sqlsrv_query($conn, $unfriendQuery);
}
//Get friends of loggedInUser
$getCurrentUserFriendsQuery = "SELECT userid, friendid FROM friends WHERE (accepted = 'True' AND userid = '$currentUserID') OR (accepted = 'True' AND friendid = '$currentUserID') ";
$currentUserFriends = sqlsrv_query($conn, $getCurrentUserFriendsQuery, array(), array( "Scrollable" => 'static' ));
$currentUserFriendsCount = sqlsrv_num_rows($currentUserFriends); //Count friends for loop
//Convert friends of loggedInUser to array of user IDs
$currentUserFriendsArray = array();
for ($x = 1; $x < $currentUserFriendsCount + 1; $x++){
$currentUserFriendsRow = sqlsrv_fetch_array($currentUserFriends, SQLSRV_FETCH_NUMERIC); //Select next row
array_push($currentUserFriendsArray, $currentUserFriendsRow[0]);
array_push($currentUserFriendsArray, $currentUserFriendsRow[1]);}
$currentUserFriendsArray = array_unique($currentUserFriendsArray);
$currentUserFriendsArray = array_values($currentUserFriendsArray);
$currentUserFriendsCount = count($currentUserFriendsArray);
//Get sent pending friend invites of loggedInUser
$getCurrentUserPendingInvitesQuery = "SELECT friendid FROM friends WHERE userid = '$currentUserID' AND accepted = 'False'";
$currentUserPendingInvites = sqlsrv_query($conn, $getCurrentUserPendingInvitesQuery, array(), array( "Scrollable" => 'static' ));
$currentUserPendingInvitesCount = sqlsrv_num_rows($currentUserPendingInvites); //Count friends for loop
//Convert sent pending friend invites of loggedInUser to array of user IDs
$currentUserPendingInvitesArray = array();
for ($x = 1; $x < $currentUserPendingInvitesCount + 1; $x++){
$currentUserPendingInvitesRow = sqlsrv_fetch_array($currentUserPendingInvites, SQLSRV_FETCH_NUMERIC); //Select next row
array_push($currentUserPendingInvitesArray, $currentUserPendingInvitesRow[0]);}
//Get received pending friend invites of loggedInUser
$getCurrentUserReceivedPendingInvitesQuery = "SELECT userid FROM friends WHERE friendid = '$currentUserID' AND accepted = 'False'";
$currentUserReceivedPendingInvites = sqlsrv_query($conn, $getCurrentUserReceivedPendingInvitesQuery, array(), array( "Scrollable" => 'static' ));
$pendingRequestsCount = sqlsrv_num_rows($currentUserReceivedPendingInvites); //Count pending invites
//Convert pending friend invites of loggedInUser to array of user IDs
$currentUserReceivedPendingInvitesArray = array();
for ($x = 1; $x < $pendingRequestsCount + 1; $x++){
$currentUserReceivedPendingInvitesRow = sqlsrv_fetch_array($currentUserReceivedPendingInvites, SQLSRV_FETCH_NUMERIC); //Select next row
array_push($currentUserReceivedPendingInvitesArray, $currentUserReceivedPendingInvitesRow[0]);}
//
//For friends list of selectedUser in sidebar
//
//Get friends of selectedUser
$getSelectedUserFriendsQuery = "SELECT userid, friendid FROM friends WHERE (accepted = 'True' AND userid = '$selectedUserID') OR (accepted = 'True' AND friendid = '$selectedUserID') ";
$selectedUserFriends = sqlsrv_query($conn, $getSelectedUserFriendsQuery, array(), array( "Scrollable" => 'static' ));
$selectedUserFriendsCount2 = sqlsrv_num_rows($selectedUserFriends); //Count friends for loop
//Convert friends of loggedInUser to array of user IDs
$selectedUserFriendsArray = array();
for ($x = 1; $x < $selectedUserFriendsCount2 + 1; $x++){
$selectedUserFriendsRow = sqlsrv_fetch_array($selectedUserFriends, SQLSRV_FETCH_NUMERIC); //Select next row
array_push($selectedUserFriendsArray, $selectedUserFriendsRow[0]);
array_push($selectedUserFriendsArray, $selectedUserFriendsRow[1]);}
$selectedUserFriendsArray = array_unique($selectedUserFriendsArray);
$selectedUserFriendsArray = array_values($selectedUserFriendsArray);
$selectedUserFriendsCount = count($selectedUserFriendsArray);
//Get unseen Likes
$likesNotificationCountQuery = "SELECT * FROM likes WHERE post_author = '$loggedInUser' AND notified = 'False'";
$getLikesNotificationCount = sqlsrv_query($conn, $likesNotificationCountQuery, array(), array( "Scrollable" => 'static' ));
$likesNotificationCount = sqlsrv_num_rows($getLikesNotificationCount);
//Convert selectedUserFriendsArray from user IDs to usernames
foreach ($selectedUserFriendsArray as &$value){
$convertQuery = " SELECT username FROM users WHERE id = '$value' ";
$convert = sqlsrv_query($conn, $convertQuery);
$convert = sqlsrv_fetch_array($convert);
$value = $convert[0];
}
function calculateNewPostID(){
//To calculate new post ID, count number of rows in database and add 1
global $conn;
$countExistingPosts = sqlsrv_query($conn, "SELECT * FROM posts", array(), array( "Scrollable" => 'static' ));
$postsCount = sqlsrv_num_rows($countExistingPosts);
$newPostID = $postsCount + 1;
return $newPostID;
}
function setFilename(){
global $newPostID;
//Set filename of image to new post ID
$path = $_FILES['file']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);
$target_file = "images/" . $newPostID . "." . $ext;
return $target_file;
}
function verifyImage(){
global $conn;
global $newPostID;
global $target_file;
$uploadError = "";
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["file"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
}
else {
$uploadError = "File is not an image.";
return $uploadError;
}
}
// Check file size
if ($_FILES["file"]["size"] > 500000) {
$uploadError = "Sorry, your file is too large.";
return $uploadError;}
// Allow certain file formats
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
$uploadError = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
return $uploadError;}
// if everything is ok, try to upload file
if ($uploadError == "") {
if (!move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
$uploadError = "Sorry, there was an error uploading your file.";
return $uploadError;}
}
}
///////////////////////////////////////////////////
//If $loggedInUser posts a status to their own Wall
if (isset($_POST["new_status"])){
$newStatus = $_POST["new_status"];
$newPostID = calculateNewPostID();
$newPostSubmit = sqlsrv_query($conn, "INSERT INTO posts VALUES ('$newPostID', '$newStatus', '$loggedInUser', '$timestamp', ' ', '$timestamp', '0', '$currentUserID', '$currentUserID', '$loggedInUser', null)");
if (!$newPostSubmit){
print_r(sqlsrv_errors());}
}
////////////////////////////////////////////////////
//If $loggedInUser posts a status to a friend's Wall
//Still need to add permission check
if (isset($_POST["newWallPost"])){
$newWallPost = $_POST["newWallPost"];
$newPostID = calculateNewPostID();
$newPostSubmit = sqlsrv_query($conn, "INSERT INTO posts VALUES ('$newPostID', '$newWallPost', '$loggedInUser', '$timestamp', ' ', '$timestamp', '0', '$selectedUserID', '$currentUserID', '$selectedUser', null)");
if (!$newPostSubmit){
print_r(sqlsrv_errors());}
}
//////////////////////
//If user liked a post
//Still need to add permission check
if (isset($_GET["likePost"])){
$likedPostID = $_GET["likePost"];
$author = $_GET["author"];
$likePostQuery = "INSERT INTO likes VALUES ('$likedPostID', '$author', '$loggedInUser', 'false')";
$likePost = sqlsrv_query($conn, $likePostQuery);
}
////////////////////////
//If user unliked a post
//Still need to add permission check
if (isset($_GET["unLikePost"])){
$unLikedPostID = $_GET["unLikePost"];
$likePostQuery = "DELETE FROM likes WHERE post_id = '$unLikedPostID' AND liked_by = '$loggedInUser'";
$likePost = sqlsrv_query($conn, $likePostQuery);
}
/////////////////////////////////////////////
//If user uploaded an image to their own wall
$uploadError = "";
if (isset($_POST["postImage"])){
$newPostID = calculateNewPostID();
$target_file = setFileName();
$uploadError = verifyImage();
if ($uploadError == "")
$newPostSubmit = sqlsrv_query($conn, "INSERT INTO posts VALUES ('$newPostID', '', '$loggedInUser', '$timestamp', '$target_file', '$timestamp', '0', '$currentUserID', '$currentUserID', '$loggedInUser', null) ");
if (!$newPostSubmit){
print_r(sqlsrv_errors());
}
}
//////////////////////////////////////////////
//If user uploaded an image to a friend's Wall
if (isset($_POST["postImageFriend"])){
$newPostID = calculateNewPostID();
$target_file = setFileName();
$uploadError = verifyImage();
if ($uploadError == "")
$newPostSubmit = sqlsrv_query($conn, "INSERT INTO posts VALUES ('$newPostID', '', '$loggedInUser', '$timestamp', '$target_file', '$timestamp', '0', '$selectedUserID', '$currentUserID', '$selectedUser', null) ");
if (!$newPostSubmit){
print_r(sqlsrv_errors());
}
}
///////////////////////////////////////
//If user changed their profile picture
if (isset($_POST["submitProfilePic"])){
$newPostID = calculateNewPostID();
$target_file = setFileName();
$uploadError = verifyImage();
if ($uploadError == "")
$newPostSubmit = sqlsrv_query($conn, "INSERT INTO posts VALUES ('$newPostID', '', '$loggedInUser', '$timestamp', '$target_file', '$timestamp', '0', '$selectedUserID', '$currentUserID', '$selectedUser') ");
$target_file = substr($target_file, 7);
$newProfilePicSubmit = sqlsrv_query($conn, "UPDATE users SET profile_pic = '$target_file' WHERE username = '$loggedInUser'");
if (!$newPostSubmit){
print_r(sqlsrv_errors());
}
}
///////////////////////////////////////
//If user submitted a comment on a post
if (isset($_POST["submitComment"])){
$commentOn = $_GET["commentOn"];
$newPostID = calculateNewPostID();
$commentText = $_POST['comment'];
$newPostQuery = "INSERT INTO posts VALUES ('$newPostID', '$commentText', '$loggedInUser', '$timestamp', '', '$timestamp', '0', '0', '$currentUserID', 'NULL', $commentOn) ";
$newPostSubmit = sqlsrv_query($conn, $newPostQuery);
if (!$newPostSubmit){
print_r(sqlsrv_errors());}
}
///////////////////////////
//If not viewing first page
if (isset($_GET["page"])){
$page_number = $_GET["page"];
}
else {
$page_number = 0;
}
?>
<html>
<head>
<title>Welcome to Social Network</title>
<link rel="stylesheet" type="text/css" href="default.css">
</head>
<body>
<center>
<!--Logo-->
<div class="header" id="header">
<h1><a href="index.php">Social Network</a></h1>
</div>
<!--Options bar-->
<div id="options">
<span id="search">
<?php
//Search bar
echo '<form action="search.php" method="post" style="display: inline;"><input type="text" name="search" placeholder="Search"><input type="submit" value="Submit" name="submitSearch"></form>';
//Pending friend requests notification
if ($pendingRequestsCount > 0){
echo ' <a href="requests.php"><font color="red">'.$pendingRequestsCount.' new friend requests</font></a>';
}
//New likes notification
if ($likesNotificationCount > 0){
echo ' <a href="likes.php"><font color="red">'.$likesNotificationCount.' new likes</font></a>';
}
?>
</span>
<span id="userOptions">
<?php if (!isset($_SESSION["loggedInUser"])){echo '<a href="register.php">Register</a> ';} ?>
<?php if (!isset($_SESSION["loggedInUser"])){echo '<a href="login.php"44>Log in</a> ';} ?>
<?php if (isset($_SESSION["loggedInUser"])){echo '<a href="logout.php">Log out</a>';} ?>
<?php if (isset($_SESSION["loggedInUser"])){echo 'Welcome, <a href="profile.php?selectedUser=' . $loggedInUser . '">' .$loggedInUser. '</a>';}
?>
</span>
</div>
<div class="main">
<span class="wall" id="wall">
<!--Profile pic-->
<?php
//If profile pic exists, display it. Else, display default profile pic.
//Get userID and profile pic of selectedUser
$getProfilePicQuery = "SELECT profile_pic FROM users WHERE username = '$selectedUser'";
$getProfilePic = sqlsrv_query($conn, $getProfilePicQuery, array());
$selectedUserProfilePic = sqlsrv_fetch_array($getProfilePic);
$selectedUserProfilePic = $selectedUserProfilePic[0];
if ($selectedUserProfilePic != null){
//Need to add code to create thumbnails on profile pic upload
echo"<a href='view_image.php?selectedImage=" . $selectedUserProfilePic . "&imageType=profile'><img src='images/" .$selectedUserProfilePic. "' height='128' width='128'></a><br>";
}
else {
echo "<img src='images\default_profile_picture_128.jpg'></a><br>";
}
if ($selectedUser == $loggedInUser){
//Post an image on your Wall
if (!isset($_GET["changeProfilePic"])){
echo "<a href='?selectedUser=" . $selectedUser . "&changeProfilePic'>Change profile picture</a><br><br>";}
else {
echo "<form action='?selectedUser=" . $selectedUser . "' method='post' enctype='multipart/form-data'>".
"Select image to upload:<br>".
"<input type='file' name='file' id='file'><br>".
"<input type='submit' value='Submit' name='submitProfilePic'>".
"</form>";
}
echo '<div class="error">' . $uploadError . '</div><br>';
}
else echo "<br>"
?>
<!-- Wall -->
<?php
if ($selectedUser == $loggedInUser){
echo "Your Wall";
}
else {
echo nl2br($selectedUser."'s Wall");
}
if ($selectedUser == $loggedInUser){
//Post a status on your Wall
echo nl2br('
<form action="profile.php?selectedUser='.$selectedUser.'" method="post">'.
'<textarea name="new_status" rows="1" cols="40" placeholder="Post a status"></textarea><input type="submit" value="Submit" name="submit_status"><br>'.
'<div class="error" id="status_error"></div>'.
'</form>');
//Post an image on your Wall
if (!isset($_GET["postImage"])){
echo "<a href='?selectedUser=" . $selectedUser . "&postImage'>Post an image</a><br><br>";}
else {
echo "<form action='?selectedUser=" . $selectedUser . "' method='post' enctype='multipart/form-data'>".
"Select image to post:<br>".
"<input type='file' name='file' id='file'><br>".
"<input type='submit' value='Post' name='postImage'>".
"</form>";
}
echo '<div class="error">' . $uploadError . '</div><br>';
}
elseif (in_array($selectedUserID, $currentUserFriendsArray)){ //If users are friends
//Post a status on $selectedUser's Wall
echo nl2br('
<form action="profile.php?selectedUser='.$selectedUser.'" method="post">'.
'<textarea name="newWallPost" rows="1" cols="40" placeholder="Post on '.$selectedUser.'\'s Wall"></textarea><input type="submit" value="Submit" name="submitWallPost"><br>'.
'<div class="error" id="status_error"></div>'.
'</form>');
//Post an image on $selectedUser's Wall
if (!isset($_GET["postImage"])){
echo "<a href='?selectedUser=" . $selectedUser . "&postImage'>Post an image</a><br><br>";}
else {
echo "<form action='?selectedUser=" . $selectedUser . "' method='post' enctype='multipart/form-data'>".
"Select image to post:<br>".
"<input type='file' name='file' id='file'><br>".
"<input type='submit' value='Post' name='postImageFriend'>".
"</form>";
}
echo '<div class="error">' . $uploadError . '</div><br>';
}
else { //If users are not friends, do not display status entry
echo "<br><br>";
}
//If users are friends, display posts on selectedUser's Wall
if ($selectedUserID == $currentUserID || in_array($selectedUserID, $currentUserFriendsArray)){
//Count how many comments are in the the thread
$query = "SELECT * FROM posts WHERE wall = '$selectedUserID' AND comment_of IS NULL";
$posts_array = sqlsrv_query($conn, $query, array(), array( "Scrollable" => 'static'));
$posts_count = sqlsrv_num_rows($posts_array);
//Query 10 posts for page
$offset = $page_number * 10;
$query = "SELECT * FROM posts WHERE wall = '$selectedUserID' AND comment_of IS NULL ORDER BY date_submitted DESC OFFSET $offset ROWS FETCH NEXT 10 ROWS ONLY";
$posts_array = sqlsrv_query($conn, $query, array(), array( "Scrollable" => 'static'));
$anchor = 0;
for ($x = 1; $x < 11; $x++){
$posts_array_row = sqlsrv_fetch_array($posts_array, SQLSRV_FETCH_NUMERIC); //Select next row in $query
$anchor++;
echo "<a id='" . $anchor . "'></a>"; //Direct link to post
//Display a post
if (isset($posts_array_row[0])){
echo "<div class='status'>";
echo "<span class='profileThumb'>";
echo "<a href='profile.php?selectedUser=" . $posts_array_row[2] . "'><img src='";
//Get profile pic or null
$getProfilePicQuery = "SELECT profile_pic FROM users WHERE username = '$posts_array_row[2]'";
$getProfilePic = sqlsrv_query($conn, $getProfilePicQuery, array());
$profilePic = sqlsrv_fetch_array($getProfilePic);
//If profile pic exists, display it. Else, display default profile pic.
if ($profilePic[0] != null){echo "images/" . $profilePic[0];}
else {echo "images\default_profile_picture_32.jpg";}
echo "'></a>";
echo "</span>";
echo "<span class='statusContent'>";
//Username of post author
echo "<font color='#0080ff'><b><a href='profile.php?selectedUser=" . $posts_array_row[2] . "'>" . $posts_array_row[2]. "</a>" . "</b></font> ";
//If post is not on author's wall, display username of that user
if ($posts_array_row[7] != $posts_array_row[8]){echo "<font color='#0080ff'><b></a>" . " > " . "<a href='profile.php?selectedUser=" . $posts_array_row[9] . "'>" . $posts_array_row[9] . "</a>" . "</b></font></br> ";}
else {echo "<br>";}
//Date posted
echo "<font color='gray' size='2'>" . date_format($posts_array_row[3], "m/d/Y h:ia") . "</font><br>";
//Post content
if ($posts_array_row[4] != " "){echo "<a href='view_image.php?selectedImage=" . substr(strval($posts_array_row[4]), 7) . "'><img src='" . $posts_array_row[4] . "'></a><br><font size='2'>";}
else {echo $posts_array_row[1] . "<br><font size='2'>";}
//Get number of likes
$getLikesQuery = "SELECT * FROM likes WHERE post_id = '$posts_array_row[0]' ";
$getLikes = sqlsrv_query($conn, $getLikesQuery, array(), array( "Scrollable" => 'static' ));
$likesCount = sqlsrv_num_rows($getLikes);
//Convert users who liked current post to array
$likesArray = array();
for ($y = 1; $y < $likesCount + 1; $y++){
$likesRow = sqlsrv_fetch_array($getLikes, SQLSRV_FETCH_NUMERIC); //Select next row
array_push($likesArray, $likesRow[2]);}
if ($likesCount == 1) {echo "<div class='tooltip'>1 like<span class='tooltiptext'>" . implode(" ,", $likesArray) . "</span></div> ";}
else if ($likesCount > 1) {echo "<div class='tooltip'>" . $likesCount . " likes<span class='tooltiptext'>" . implode(", ", $likesArray) . "</span></div> ";}
//Like/unlike button
if (!in_array($loggedInUser, $likesArray)){echo "<a href='?selectedUser=" . $selectedUser . "&likePost=" . $posts_array_row[0] . "&author=" . $posts_array_row[2] . "&page=" . $page_number . "#" . $anchor . "'>Like</a> ";}
else {echo "<a href='?selectedUser=" . $selectedUser . "&unLikePost=" . $posts_array_row[0] . "&page=" . $page_number . "#" . $anchor . "'>Unlike</a> ";}
//Comment button/box
if (isset($_GET["commentOn"]) && $_GET["commentOn"] == $posts_array_row[0]) { echo
"<form action='?selectedUser=" . $selectedUser . "&commentOn=" . $posts_array_row[0] . "&page=" . $page_number . "#" . $anchor . "' method='post'>" .
"<input type='text' name='comment' placeholder='Add a comment'>" .
"<input type='submit' value='Submit' name='submitComment'> <a href='?selectedUser=" . $selectedUser . "&page=" . $page_number . "#" . $anchor . "'>Cancel</a><br>" .
"</form>";
}
else {echo "<a href='?selectedUser=" . $selectedUser . "&commentOn=" . $posts_array_row[0] . "&page=" . $page_number . "#" . $anchor . "'>Comment</a>";}
echo "</font>";
echo "</div><br>";
//Count how many comments the post has
$comments_array = sqlsrv_query($conn, "SELECT * FROM posts WHERE comment_of = '$posts_array_row[0]' ", array(), array( "Scrollable" => 'static'));
$comments_count = sqlsrv_num_rows($comments_array);
if ($comments_count > 0){
for ($z = 1; $z < $comments_count + 1; $z++){
$comments_array_row = sqlsrv_fetch_array($comments_array, SQLSRV_FETCH_NUMERIC); //Select next row in $query
$anchor++;
echo "<a id='" . $anchor . "'></a>"; //Direct link to comment
//Display comments on post
echo "<div class='statusComment'>";
echo "<span class='commentProfileThumb'>";
echo "<a href='profile.php?selectedUser=" . $comments_array_row[2] . "'><img src='";
//Get profile pic or null
$getProfilePicQuery2 = "SELECT profile_pic FROM users WHERE username = '$comments_array_row[2]'";
$getProfilePic2 = sqlsrv_query($conn, $getProfilePicQuery2, array());
$profilePic2 = sqlsrv_fetch_array($getProfilePic2);
//If profile pic exists, display it. Else, display default profile pic.
if ($profilePic2[0] != null){echo "images/" . $profilePic2[0];}
else {echo "images\default_profile_picture_32.jpg";}
echo "'></a>";
echo "</span>";
echo "<span class='commentContent'>";
//Username of post author
echo "<font color='#0080ff'><b><a href='profile.php?selectedUser=" . $comments_array_row[2] . "'>" . $comments_array_row[2]. "</a>" . "</b></font><br> ";
//Date posted
echo "<font color='gray' size='2'>" . date_format($comments_array_row[3], "m/d/Y h:ia") . "</font><br>";
//Post content
if ($comments_array_row[4] != ""){echo "<a href='view_image.php?selectedImage=" . substr(strval($comments_array_row[4]), 7) . "'><img src='" . $comments_array_row[4] . "'></a><br>";}
else {echo $comments_array_row[1] . "<br>";}
//Get number of likes
$getLikesQuery2 = "SELECT * FROM likes WHERE post_id = '$comments_array_row[0]'";
$getLikes2 = sqlsrv_query($conn, $getLikesQuery2, array(), array( "Scrollable" => 'static' ));
$likesCount2 = sqlsrv_num_rows($getLikes2);
//Convert users who liked current post to array
$likesArray2 = array();
for ($a = 1; $a < $likesCount2 + 1; $a++){
$likesRow2 = sqlsrv_fetch_array($getLikes2, SQLSRV_FETCH_NUMERIC); //Select next row
array_push($likesArray2, $likesRow2[2]);}
echo "<font size='2'>";
if ($likesCount2 == 1) {echo "<div class='tooltip'>1 like<span class='tooltiptext'>" . implode(" ,", $likesArray2) . "</span></div> ";}
else if ($likesCount2 > 1) {echo "<div class='tooltip'>" . $likesCount2 . " likes<span class='tooltiptext'>" . implode(", ", $likesArray2) . "</span></div> ";}
//Like/unlike button
if (!in_array($loggedInUser, $likesArray2)){echo "<a href='?selectedUser=" . $selectedUser . "&likePost=" . $comments_array_row[0] . "&page=" . $page_number . "#" . $anchor . "'>Like</a> ";}
else {echo "<a href='?selectedUser=" . $selectedUser . "&unLikePost=" . $comments_array_row[0] . "&page=" . $page_number . "#" . $anchor . "'>Unlike</a> ";}
echo "</font>";
echo "<font size='1'><br><br></font>";
echo "</span>";
echo "</div>";
}
}
}
}
}
//If users are not friends, do not display any posts
else {
echo "You are not friends with ".$selectedUser;
}
//Page navigation links
if ($selectedUserID == $currentUserID || in_array($selectedUserID, $currentUserFriendsArray)){ //If users are friends (otherwise there are no posts visible)
if ($posts_count > 10 && isset($posts_array_row[0]) && $page_number == 0) { //First of multiple pages
echo "Showing posts " . (($page_number * 10) + 1) . "-" . (($page_number + 1) * 10) . " of " . $posts_count . " <a href=?selectedUser=" . $selectedUser . "&page=" . ($page_number + 1) . ">Next page</a> ";
}
else if ($posts_count > 10 && isset($posts_array_row[0]) && $page_number > 0 && ((($page_number + 1) * 10) < $posts_count)) { //Multiple pages, not first or last
echo "Showing posts " . (($page_number * 10) + 1) . "-" . (($page_number + 1) * 10) . " of " . $posts_count . " <a href=?selectedUser=" . $selectedUser . "&page=" . ($page_number + 1) . ">Next page</a> <a href=?selectedUser=" . $selectedUser . "&page=" . ($page_number - 1) . ">Previous page</a>";
}
else if ($posts_count > 10 && isset($posts_array_row[0]) && $page_number > 0) { //Last of multiple pages, with no posts left over
echo "Showing posts " . (($page_number * 10) + 1) . "-" . $posts_count . " of " . $posts_count . " <a href=?selectedUser=" . $selectedUser . "&page=" . ($page_number - 1) . ">Previous page</a>";
}
else if ($posts_count <= 10){ //Only page
echo "Showing posts 1-" . $posts_count . " of " . $posts_count;
}
else if (!isset($posts_array_row[0])){ //Last page, with <10 posts left
echo "Showing posts " . (($page_number * 10) + 1) . "-" . $posts_count . " of " . $posts_count . " <a href=?selectedUser=" . $selectedUser . "&page=" . ($page_number - 1) . ">Previous page</a>";
}
}
?>
</span>
<span class="sidebar">
<?php
if ($selectedUser == $loggedInUser){
echo nl2br("Viewing your profile\n\nYour friends(".($currentUserFriendsCount - 1)."): ");
}
else{
echo nl2br("Viewing ".$selectedUser."'s profile\n\n");
}
if ($selectedUserID == $currentUserID){
}
elseif (in_array($selectedUserID, $currentUserFriendsArray)){
echo "You are friends<br><a href='?selectedUser=" . $selectedUser . "&unfriend'>Unfriend</a><br><br>".$selectedUser."'s friends(".($selectedUserFriendsCount - 1)."): <br>";
}
elseif (in_array($selectedUserID, $currentUserReceivedPendingInvitesArray)){
echo nl2br("<a href='profile.php?acceptFriend&selectedUser=".$selectedUser."'>Accept friend request</a>\n\n");
}
elseif (in_array($selectedUserID, $currentUserPendingInvitesArray)){
echo nl2br("Friend request sent\n\n");
}
elseif (!in_array($selectedUserID, $currentUserFriendsArray)){
echo nl2br("<a href='profile.php?addFriend&selectedUser=".$selectedUser."'>Send friend request</a>\n\n");
}
if ($selectedUserFriendsCount > 0){
for ($x = 0; $x < $selectedUserFriendsCount; $x++){
if ($selectedUserFriendsArray[$x] != $selectedUser){
//Display a user
echo nl2br("<font color='#0080ff'><b><a href='profile.php?selectedUser=".$selectedUserFriendsArray[$x]."'>".$selectedUserFriendsArray[$x]."</a></b></font>");
if ($x < $selectedUserFriendsCount - 1){
echo nl2br(", ");
}
}
}
}
?>
</span>
</div>
</center>
</body>
</html>