-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
270 lines (237 loc) · 10.8 KB
/
functions.php
File metadata and controls
270 lines (237 loc) · 10.8 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
<?php
function get_loggedin_username(){
include 'connection.php';
$query = "SELECT p.picture_path, u.user_fname , u.user_lname, u.user_nname FROM pictures as p inner join users as u on u.user_profile_picture = p.picture_id WHERE u.user_id = '".$_SESSION['user_id']."'";
$result = mysqli_query($link , $query);
$row = mysqli_fetch_array($result);
$loggedin_user[0] = 'images/'.$row[0];
$loggedin_user[1] = $row[1].' '.$row[2];
$loggedin_user[2] = $row[0];
$loggedin_user[3] = $row[3];
return $loggedin_user;
}
function posting(){
include 'connection.php';
$posttext = "";
$posttext = $_POST['posttext'];
$privacy = $_POST['privacy'];
if($privacy == 'public' || $privacy == ""){
if ($_FILES['postimage']['name'] != "") {
$path = $_FILES['postimage']['name'];
$path = check_picture_name($path,'postimage');
$target = 'images/'.basename($_FILES['postimage']['name']);
if($posttext != ""){
$query = "INSERT INTO pictures (picture_path) VALUES ('".$path."')";
mysqli_query($link,$query);
$timestamp = date("Y-m-d H:i:s");
$query = "INSERT INTO `posts`(`user_id`, `post_timestamp`, `post_text`,`post_picture_id` , `is_image`) VALUES ('".$_SESSION['user_id']."','".$timestamp."','".$posttext."' ,'".mysqli_insert_id($link)."','1')";
mysqli_query($link,$query);
}else{
$query = "INSERT INTO pictures (picture_path) VALUES ('".$path."')";
//mysqli_query($link,$query);
$timestamp = date("Y-m-d H:i:s");
$query = "INSERT INTO `posts`(`user_id`, `post_timestamp`,`post_picture_id` , `is_image`, `is_text`) VALUES ('".$_SESSION['user_id']."','".$timestamp."' ,'".mysqli_insert_id($link)."','1' ,'0')";
mysqli_query($link,$query);
}
move_uploaded_file($_FILES['postimage']['tmp_name'], $target);
}else{
$timestamp = date("Y-m-d H:i:s");
echo "here111";
if ($posttext != ""){
echo "here5";
$query = "INSERT INTO `posts`(`user_id`, `post_timestamp`, `post_text`) VALUES ('".$_SESSION['user_id']."','".$timestamp."','".$posttext."' )";
mysqli_query($link,$query);
}else{
}
}
}else{
if ($_FILES['postimage']['name'] != "") {
$path = $_FILES['profilepic']['name'];
$path = check_picture_name($path,'profilepic');
$target = 'images/'.basename($_FILES['postimage']['name']);
if($posttext != ""){
$query = "INSERT INTO pictures (picture_path) VALUES ('".$path."')";
mysqli_query($link,$query);
$timestamp = date("Y-m-d H:i:s");
$query = "INSERT INTO `posts`(`user_id`, `post_timestamp`, `post_text`,`post_picture_id` , `is_image`, `post_privacy`) VALUES ('".$_SESSION['user_id']."','".$timestamp."','".$posttext."' ,'".mysqli_insert_id($link)."','1', '0')";
mysqli_query($link,$query);
}else{
$query = "INSERT INTO pictures (picture_path) VALUES ('".$path."')";
//mysqli_query($link,$query);
$timestamp = date("Y-m-d H:i:s");
$query = "INSERT INTO `posts`(`user_id`, `post_timestamp`,`post_picture_id` , `is_image`, `is_text` , `post_privacy`) VALUES ('".$_SESSION['user_id']."','".$timestamp."' ,'".mysqli_insert_id($link)."','1' ,'0' , '0')";
mysqli_query($link,$query);
}
move_uploaded_file($_FILES['postimage']['tmp_name'], $target);
}else{
$timestamp = date("Y-m-d H:i:s");
if ($posttext != ""){
$query = "INSERT INTO `posts`(`user_id`, `post_timestamp`, `post_text` , `post_privacy`) VALUES ('".$_SESSION['user_id']."','".$timestamp."','".$posttext."' , '0' )";
mysqli_query($link,$query);
}else{
}
}
}
}
function check_user_profile_picture(){
include 'connection.php';
$query = "SELECT user_profile_picture from users WHERE user_id = '".$_SESSION['user_id']."'";
$result = mysqli_query($link,$query);
$row = mysqli_fetch_array($result);
if ($row['user_profile_picture'] == NULL) {
$query = "UPDATE users SET user_profile_picture = user_gender where user_id = '".$_SESSION['user_id']."'";
mysqli_query($link,$query);
}
}
function check_picture_name($path, $arg){
include 'connection.php';
$counter = 0;
$query = "SELECT * from pictures where picture_path = '".$path."'";
$result = mysqli_query($link,$query);
while( mysqli_num_rows($result) > 0){
$counter = 1;
if ($counter != 0) {
$query = "SELECT * from pictures where picture_path = '".$path."'";
$result = mysqli_query($link,$query);
}
$random = rand(10,1000);
$temp = explode(".", $path);
$ext = pathinfo($path, PATHINFO_EXTENSION);
$_FILES[$arg]['name'] = $temp[0].$random.'.'.$ext;
$path = $_FILES[$arg]['name'];
}
return $path;
}
function upload_profile_picture(){
include 'connection.php';
if ($_FILES['profilepic']['name'] != "") {
$loggedin_user = get_loggedin_username();
//$counter = $counter + 1;
$path = $_FILES['profilepic']['name'];
$path = check_picture_name($path,'profilepic');
$target = 'images/'.basename($_FILES['profilepic']['name']);
$query = "INSERT INTO pictures (picture_path) VALUES ('".$path."')";
mysqli_query($link,$query);
$pictid = mysqli_insert_id($link);
move_uploaded_file($_FILES['profilepic']['tmp_name'], $target);
$query = "UPDATE users SET user_profile_picture='".mysqli_insert_id($link)."' where user_id = '".$_SESSION['user_id']."'";
mysqli_query($link,$query);
if ($loggedin_user[2] != 'defaultmale.png' || $loggedin_user[2] != 'defaultfemale.png') {
$query = "DELETE from pictures where picture_path = '".$loggedin_user[2]."'";
mysqli_query($link,$query);
}
$post_text = $loggedin_user[1] .' has changed his/her profile picture.';
$query = "SELECT post_id from posts where post_text = '".$post_text."' and post_privacy = 0";
$result = mysqli_query($link,$query);
$num = mysqli_num_rows($result);
if ($num > 0) {
$timestamp = date("Y-m-d H:i:s");
$query = "UPDATE posts SET post_picture_id = '".$pictid."' , post_timestamp = '".$timestamp."' where post_text = '".$post_text."' and user_id = '".$_SESSION["user_id"]."'";
mysqli_query($link,$query);
}else{
$query = "INSERT INTO posts (user_id,post_privacy,is_image , is_text , post_text , post_picture_id) VALUES ('".$_SESSION["user_id"]."' , '0','1','1','".$post_text."' , '".$pictid."')";
mysqli_query($link,$query);
}
}
}
function browse_friends(){
include 'connection.php';
//$friends = array();
//$query = "SELECT u.user_fname , u.user_lname , u.user_nname,pi.picture_path , f.user_id , f.friend_id from users as u inner join friendships as f on u.user_id = f.friend_id inner join pictures as pi on u.user_profile_picture = pi.picture_id WHERE f.user_id = '".$_SESSION['user_id']."' and f.friend_type = 1";
$myid = $_SESSION['user_id'];
$queryf = "SELECT friend_id,user_id FROM `friendships`
WHERE friend_type = 1
AND (user_id = '".$_SESSION['user_id']."' OR friend_id = '".$_SESSION['user_id']."')";
$resultf = mysqli_query($link, $queryf);
$friends = array();
$counter = 0;
while($rowf = mysqli_fetch_array($resultf)){
if($rowf[0] == $myid OR $rowf[1] != $myid){
$friends[$counter] = $rowf[1];
}
else if($rowf[1] == $myid){
$friends[$counter] = $rowf[0];
}
$counter = $counter + 1;
}
$friends_array = array();
if (!empty($friends)) {
# code...
$query = "SELECT DISTINCT u.user_fname , u.user_lname , u.user_nname,pi.picture_path , u.user_id , f.friend_id from users as u inner join friendships as f on u.user_id = f.friend_id inner join pictures as pi on u.user_profile_picture = pi.picture_id WHERE u.user_id IN (" . implode(',', $friends) . ")";
$result = mysqli_query($link , $query);
while($row = mysqli_fetch_array($result)){
$friends_array[] = array(
'user_id' => $row['user_id'],
'friend_id' => $row['friend_id'],
'user_fname' => $row['user_fname'],
'user_lname' => $row['user_lname'],
'user_nname' => $row['user_nname'],
'picture_path' => $row['picture_path']
);
}
}
return $friends_array;
}
function show_posts(){
include 'connection.php';
$myid = $_SESSION['user_id'];
$queryf = "SELECT friend_id,user_id FROM `friendships`
WHERE friend_type = 1
AND (user_id = '".$myid."' OR friend_id = '".$myid."')";
$resultf = mysqli_query($link, $queryf);
$friends = array();
$counter = 0;
while($rowf = mysqli_fetch_array($resultf)){
if($rowf[0] == $myid OR $rowf[1] != $myid){
$friends[$counter] = $rowf[1];
}
else if($rowf[1] == $myid){
$friends[$counter] = $rowf[0];
}
$counter = $counter + 1;
}
if (!empty($friends)) {
$query = "SELECT * from posts as p left join pictures as pi on pi.picture_id = p.post_picture_id where ( p.user_id IN(" . implode(',', $friends) . ") and p.post_privacy = 0) OR p.post_privacy = 1 OR (p.post_privacy = 0 AND p.user_id = '".$myid."') ORDER BY p.post_timestamp DESC";
}else{
$query = "SELECT * from posts as p left join pictures as pi on pi.picture_id = p.post_picture_id where p.post_privacy = 1 OR (p.post_privacy = 0 AND p.user_id = '".$myid."') ORDER BY p.post_timestamp DESC";
}
$posts = array();
$results = mysqli_query($link,$query);
while ( $row = mysqli_fetch_array($results) ) {
$posts[] = array(
'post_id' => $row['post_id'],
'user_id' => $row['user_id'],
'post_privacy' => $row['post_privacy'],
'post_timestamp' => $row['post_timestamp'],
'number_likes' => $row['number_likes'],
'is_image' => $row['is_image'],
'is_text' => $row['is_text'],
'post_text' => $row['post_text'],
'post_picture_id' => $row['post_picture_id'],
'picture_id' => $row['picture_id'],
'picture_path' => $row['picture_path']
);
}
return $posts;
}
function show_comments($post_id){
include 'connection.php';
$comments = array();
$query = "SELECT * FROM `comments` as c inner join users as u on u.user_id = c.user_id
inner join pictures as pi on pi.picture_id = u.user_profile_picture WHERE post_id = '".$post_id."' ORDER BY c.comment_timestamp DESC";
$results = mysqli_query($link,$query);
while ( $row = mysqli_fetch_array($results) ) {
$comments[] = array(
'comment_id' => $row['comment_id'],
'user_id' => $row['user_id'],
'user_fname' => $row['user_fname'],
'user_lname' => $row['user_lname'],
'user_nname' => $row['user_nname'],
'comment_timestamp' => $row['comment_timestamp'],
'comment_text' => $row['comment_text'],
'picture_id' => $row['user_profile_picture'],
'picture_path' => $row['picture_path']
);
}
return $comments;
}