-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.php
218 lines (209 loc) · 8.81 KB
/
profile.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
<?php
require 'functions/functions.php';
session_start();
ob_start();
// Check whether user is logged on or not
if (!isset($_SESSION['user_id'])) {
header("location:index.php");
}
// Establish Database Connection
$conn = connect();
?>
<?php
if (isset($_GET['id']) && $_GET['id'] != $_SESSION['user_id']) {
$current_id = $_GET['id'];
$flag = 1;
} else {
$current_id = $_SESSION['user_id'];
$flag = 0;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Social Network</title>
<link rel="stylesheet" type="text/css" href="resources/main.css">
<style>
.post {
margin-right: 50px;
float: right;
margin-bottom: 18px;
}
.profile {
margin-left: 50px;
background-color: white;
box-shadow: 0 0 5px #fca66c;
width: 220px;
padding: 20px;
border-radius: 20px;
}
input[type="file"] {
display: none;
}
label.upload {
cursor: pointer;
color: white;
background-color: #90aacb;
padding: 8px 12px;
display: inline-block;
max-width: 80px;
overflow: auto;
}
label.upload:hover {
background-color: #4267b2;
}
.changeprofile {
color: #23385f;
font-family: Fontin SmallCaps;
}
</style>
</head>
<body>
<?php include 'includes/navbar.php'; ?>
<div class="container">
<h1 class="login-signup-header">Profile</h1>
<?php
$postsql;
if ($flag == 0) { // Your Own Profile
$postsql = "SELECT posts.post_caption, posts.post_time, users.user_name,
posts.post_public, users.user_id, users.user_gender, users.user_username,
users.user_birthdate, users.user_about, posts.post_id
FROM posts
JOIN users
ON users.user_id = posts.post_by
WHERE posts.post_by = $current_id
ORDER BY posts.post_time DESC";
$profilesql = "SELECT users.user_id, users.user_gender, users.user_birthdate, users.user_name
FROM users
WHERE users.user_id = $current_id";
$profilequery = mysqli_query($conn, $profilesql);
} else { // Another Profile ---> Retrieve User data and friendship status
$profilesql = "SELECT users.user_id, users.user_gender, users.user_birthdate,
users.user_name, userfriends.friendship_status
FROM users
LEFT JOIN (
SELECT friendship.user1_id AS user_id, friendship.friendship_status
FROM friendship
WHERE friendship.user1_id = $current_id AND friendship.user2_id = {$_SESSION['user_id']}
UNION
SELECT friendship.user2_id AS user_id, friendship.friendship_status
FROM friendship
WHERE friendship.user1_id = {$_SESSION['user_id']} AND friendship.user2_id = $current_id
) userfriends
ON userfriends.user_id = users.user_id
WHERE users.user_id = $current_id";
$profilequery = mysqli_query($conn, $profilesql);
$row = mysqli_fetch_assoc($profilequery);
mysqli_data_seek($profilequery, 0);
if (isset($row['friendship_status'])) { // Either a friend or requested as a friend
if ($row['friendship_status'] == 1) { // Friend
$postsql = "SELECT posts.post_caption, posts.post_time, users.user_name,
posts.post_public, users.user_id, users.user_gender, users.user_username,
users.user_birthdate, users.user_about,
posts.post_id
FROM posts
JOIN users
ON users.user_id = posts.post_by
WHERE posts.post_by = $current_id
ORDER BY posts.post_time DESC";
} else if ($row['friendship_status'] == 0) { // Requested as a Friend
$postsql = "SELECT posts.post_caption, posts.post_time, users.user_name,
posts.post_public, users.user_id, users.user_gender, users.user_username,
users.user_birthdate, users.user_about,
posts.post_id
FROM posts
JOIN users
ON users.user_id = posts.post_by
WHERE posts.post_by = $current_id AND posts.post_public = 'Y'
ORDER BY posts.post_time DESC";
}
} else { // Not a friend
$postsql = "SELECT posts.post_caption, posts.post_time, users.user_name,
posts.post_public, users.user_id, users.user_gender, users.user_username,
users.user_birthdate, users.user_about,
posts.post_id
FROM posts
JOIN users
ON users.user_id = posts.post_by
WHERE posts.post_by = $current_id AND posts.post_public = 'Y'
ORDER BY posts.post_time DESC";
}
}
$postquery = mysqli_query($conn, $postsql);
if ($postquery) {
// Posts
$width = '40px';
$height = '40px';
if (mysqli_num_rows($postquery) == 0) { // No Posts
if ($flag == 0) { // Message shown if it's your own profile
echo '<div class="post">';
echo 'You don\'t have any posts yet';
echo '</div>';
} else { // Message shown if it's another profile other than you.
echo '<div class="post">';
echo 'There is no public posts to show.';
echo '</div>';
}
include 'includes/profile.php';
} else {
while ($row = mysqli_fetch_assoc($postquery)) {
include 'includes/post.php';
}
// Profile Info
include 'includes/profile.php';
?>
<br>
<?php if ($flag == 0) { ?>
<div class="profile">
<center class="changeprofile">Change Profile Picture</center>
<br>
<form action="" method="post" enctype="multipart/form-data">
<center>
<label class="upload" onchange="showPath()">
<span id="path" style="color: white;">... Browse</span>
<input type="file" name="fileUpload" id="selectedFile">
</label>
</center>
<br>
<input type="submit" value="Upload Image" name="profile">
</form>
</div>
<br>
<?php } ?>
<?php
}
}
?>
</div>
</body>
<script>
function showPath() {
var path = document.getElementById("selectedFile").value;
path = path.replace(/^.*\\/, "");
document.getElementById("path").innerHTML = path;
}
</script>
</html>
<?php include 'functions/upload.php'; ?>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') { // A form is posted
if (isset($_POST['request'])) { // Send a Friend Request
$sql3 = "INSERT INTO friendship(user1_id, user2_id, friendship_status)
VALUES ({$_SESSION['user_id']}, $current_id, 0)";
$query3 = mysqli_query($conn, $sql3);
if (!$query3) {
echo mysqli_error($conn);
}
} else if (isset($_POST['remove'])) { // Remove
$sql3 = "DELETE FROM friendship
WHERE ((friendship.user1_id = $current_id AND friendship.user2_id = {$_SESSION['user_id']})
OR (friendship.user1_id = {$_SESSION['user_id']} AND friendship.user2_id = $current_id))
AND friendship.friendship_status = 1";
$query3 = mysqli_query($conn, $sql3);
if (!$query3) {
echo mysqli_error($conn);
}
}
sleep(4);
}
?>