-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.php
400 lines (380 loc) · 15.4 KB
/
action.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
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
<?php
include('db_connection.php');
session_start();
$output='';
if(isset($_POST['action'])){
if($_POST['action']=="insert"){
$data=array(
':user_id' => $_SESSION["user_id"],
':post_content' => $_POST["post_content"],
':post_datetime' => date("Y-m-d") . ' ' . date("H:i:s",strtotime(date('h:i:sa')))
);
$query ="INSERT INTO post_table(user_id,post_content,post_datetime)
VALUES(:user_id,:post_content,:post_datetime)
";
$statement = $connect->prepare($query);
$statement->execute($data);
$ntf_query = "
SELECT receiver_id FROM follow_table
WHERE sender_id = '".$_SESSION["user_id"]."'
";
$statement = $connect->prepare($ntf_query);
$statement->execute();
$ntf_result =$statement->fetchAll();
foreach($ntf_result as $ntf_row){
$ntf_text =' <b>'.get_username($connect,$_SESSION["user_id"]).'</b> has shared new Post';
$insert_query ="
INSERT INTO ntf_table(ntf_rcv_id,ntf_text,read_ntf)
VALUES('".$ntf_row["receiver_id"]."','".$ntf_text."','no')
";
$statement = $connect->prepare($insert_query);
$statement->execute();
}
}
if($_POST['action']=='fetch_post'){
$query ="SELECT * FROM post_table
INNER JOIN user_table ON user_table.user_id = post_table.user_id
LEFT JOIN follow_table ON follow_table.sender_id=post_table.user_id
WHERE follow_table.receiver_id = '".$_SESSION["user_id"]."' OR post_table.user_id=
'".$_SESSION["user_id"]."'
GROUP BY post_table.post_id
ORDER BY post_table.post_id DESC ";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$total_row = $statement->rowCount();
if($total_row >0){
foreach($result as $row){
$profile_image='';
if($row['profile_image']!=''){
$profile_image= '<img src="images/'.$row["profile_image"].'"
class="img-thumbnail img-responsive">';
}
else{
$profile_image ='<img src="images/user.jpg"
class="img-thumbnail img-responsive">';
}
$repost='disabled';
if($row['user_id'] != $_SESSION['user_id']){
$repost='';
}
$output .='<div class="jumbotron" syle="padding:24px 30px 24px 30px">
<div class="row">
<div class="col-md-2">
'.$profile_image.'
</div>
<div class="col-md-8">
<h3><b>@'.$row["username"].'</b></h3>
<p>'.$row["post_content"].'
<button type="button" class="btn btn-link post_comment"
id="'.$row["post_id"].'" data-user_id="'.$row["user_id"].'"
>'.count_comment($connect,$row["post_id"]). ' Comment</button>
<button type="button" class="btn btn-danger repost"
data-post_id="'.$row["post_id"].'" '.$repost.'>
<span class="glyphicon glyphicon-retweet"></span>
'.count_repost($connect,$row["post_id"]).'
</button>
<button type="button" class="btn btn-link like_button"
data-post_id="'.$row["post_id"].'">
<span class="glyphicon glyphicon-thumbs-up"> Like</span>
'.count_like($connect,$row["post_id"]).'
</button>
</p>
<div id="comment_form'.$row["post_id"].'"
style="display:none;">
<span id="old_comment'.$row["post_id"].'">
</span>
<div class="form-group">
<textarea name="comment" class="form-control" id="comment'
.$row["post_id"].'">
</textarea>
</div>
<div class="form-group" align="right">
<button type="button" name="submit_comment" class="btn btn-primary
btn-xs submit_comment">Comment
</button>
</div>
</div>
</div>
</div></div>';
}
}
else{
$output ='<h4>No Post Found</h4>';
}
echo $output;
}
if($_POST['action']=='fetch_user'){
$query ="SELECT * FROM user_table WHERE user_id !='".$_SESSION["user_id"]."'
ORDER BY user_id DESC
";
$statement =$connect->prepare($query);
$statement->execute();
$result=$statement->fetchAll();
foreach($result as $row){
$profile_image='';
if($row['profile_image']!=''){
$profile_image= '<img src="images/'.$row["profile_image"].'"
class="img-thumbnail img-responsive">';
}
else{
$profile_image ='<img src="images/user.jpg"
class="img-thumbnail img-responsive">';
}
$output .='
<div class="row">
<div class="col-md-4">
'.$profile_image.'
</div>
<div class="col-md-8">
<h4><b>@'.$row["username"].'</b></h4>
'.create_follow_button($connect,$row["user_id"],$_SESSION["user_id"]).'
<span class="label label-success">
'.$row["follower_number"].'
Followers</span>
</div>
</div>
<hr>
';
}
echo $output;
}
if($_POST['action']=='follow'){
$query = "
INSERT INTO follow_table(sender_id,receiver_id)
VALUES('".$_POST["sender_id"]."','".$_SESSION["user_id"]."')
";
$statement = $connect->prepare($query);
if($statement->execute()){
$sub_query = "
UPDATE user_table SET follower_number=follower_number+1 WHERE user_id
='".$_POST["sender_id"]."'
";
$statement=$connect->prepare($sub_query);
$statement->execute();
$ntf_text ='<b>'.get_username($connect,$_SESSION["user_id"]).'</b> has
followed you';
$insert_query ="
INSERT INTO ntf_table(ntf_rcv_id,ntf_text,read_ntf)
VALUES('".$_POST["sender_id"]."','".$ntf_text."','no')
";
$statement = $connect->prepare($insert_query);
$statement->execute();
}
}
if($_POST['action']=='unfollow'){
$query="
DELETE FROM follow_table WHERE sender_id ='".$_POST["sender_id"]."'
AND receiver_id='".$_SESSION["user_id"]."'
";
$statement = $connect->prepare($query);
if($statement->execute()){
$sub_query = "UPDATE user_table SET follower_number=follower_number-1 WHERE user_id
='".$_POST["sender_id"]."'
";
$statement=$connect->prepare($sub_query);
$statement->execute();
}
}
if($_POST["action"]=='submit_comment'){
$data=array(
':post_id' => $_POST["post_id"],
':user_id' => $_SESSION["user_id"],
':comment' => $_POST["comment"],
':timestamp' => date("Y-m-d") . ' ' . date("H:i:s",strtotime(date('h:i:sa')))
);
$query="INSERT INTO comment_table (post_id,user_id,comment,timestamp)
VALUES(:post_id,:user_id,:comment,:timestamp)
";
$statement=$connect->prepare($query);
$statement->execute($data);
$ntf_query="
SELECT user_id,post_content FROM post_table
WHERE post_id = '".$_POST["post_id"]."'
";
$statement = $connect->prepare($ntf_query);
$statement->execute();
$ntf_result =$statement->fetchAll();
foreach($ntf_result as $ntf_row){
$ntf_text ='<b>'.get_username($connect,$_SESSION["user_id"]).'</b> has
commented on your Post- "'.strip_tags(substr($ntf_row["post_content"],0,30)).'..."';
$insert_query ="
INSERT INTO ntf_table(ntf_rcv_id,ntf_text,read_ntf)
VALUES('".$ntf_row["user_id"]."','".$ntf_text."','no')
";
$statement = $connect->prepare($insert_query);
$statement->execute();
}
}
if($_POST["action"]=="fetch_comment"){
$query = "SELECT * FROM comment_table INNER JOIN user_table
ON user_table.user_id = comment_table.user_id
WHERE post_id = '".$_POST["post_id"]."'
ORDER BY comment_id ASC
";
$statement=$connect->prepare($query);
$output='';
if($statement->execute()){
$result= $statement->fetchAll();
foreach($result as $row){
$profile_image='';
if($row['profile_image']!=''){
$profile_image= '<img src="images/'.$row["profile_image"].'"
class="img-thumbnail img-responsive img-circle">';
}
else{
$profile_image ='<img src="images/user.jpg"
class="img-thumbnail img-responsive img-circle">';
}
$output .= '
<div class="row">
<div class="col-md-2">
'.$profile_image.'
</div>
<div class="col-md-10" style="margin-top:16px;
padding-left:0">
<small><b>@'.$row["username"].'</b><br>
'.$row["comment"].'
</small>
</div>
</div>
';
}
}
echo $output;
}
if($_POST['action']=='repost'){
$query="
SELECT * FROM repost_table WHERE post_id = '".$_POST["post_id"]."'
AND user_id = '".$_SESSION["user_id"]."'
";
$statement = $connect->prepare($query);
$statement->execute();
$total_row = $statement->rowCount();
if($total_row>0){
echo 'You have already repost this';
}
else{
$query0="
INSERT INTO repost_table(post_id,user_id)
VALUES('".$_POST["post_id"]."','".$_SESSION["user_id"]."')
";
$statement = $connect->prepare($query0);
if($statement->execute()){
$query1="
SELECT * FROM post_table WHERE post_id ='".$_POST["post_id"]."'
";
$statement = $connect->prepare($query1);
if($statement->execute()){
$result=$statement->fetchAll();
$post_content ='';
foreach($result as $row){
$post_content = $row['post_content'];
}
$query2="
INSERT INTO post_table(user_id,post_content,post_datetime)
VALUES('".$_SESSION["user_id"]."',
'".$post_content."','".date("Y-m-d") . ' ' . date("H:i:s",strtotime(date('h:i:sa')))."')
";
$statement = $connect->prepare($query2);
if($statement->execute()){
$ntf_query="
SELECT user_id,post_content FROM post_table
WHERE post_id = '".$_POST["post_id"]."'
";
$statement = $connect->prepare($ntf_query);
$statement->execute();
$ntf_result =$statement->fetchAll();
foreach($ntf_result as $ntf_row){
$ntf_text ='<b>'.get_username($connect,$_SESSION["user_id"]).'</b> has
has reposted your Post-
"'.strip_tags(substr($ntf_row["post_content"],0,30)).'..."';
$insert_query ="
INSERT INTO ntf_table(ntf_rcv_id,ntf_text,read_ntf)
VALUES('".$ntf_row["user_id"]."','".$ntf_text."','no')
";
$statement = $connect->prepare($insert_query);
$statement->execute();
}
echo 'Repost Done Successfully';
}
}
}
}
}
if($_POST["action"]== 'like'){
$query="SELECT * FROM like_table WHERE post_id = '".$_POST["post_id"]."'
AND user_id = '".$_SESSION["user_id"]."'
";
$statement = $connect->prepare($query);
$statement->execute();
$total_row = $statement->rowCount();
if ($total_row>0) {
echo 'You have already like this Post';
} else {
$queryInsert="
INSERT INTO like_table(post_id,user_id)
VALUES('".$_POST["post_id"]."','".$_SESSION["user_id"]."')
";
$statement = $connect->prepare($queryInsert);
$statement->execute();
$ntf_query="
SELECT user_id,post_content FROM post_table
WHERE post_id = '".$_POST["post_id"]."'
";
$statement = $connect->prepare($ntf_query);
$statement->execute();
$ntf_result =$statement->fetchAll();
foreach($ntf_result as $ntf_row){
$ntf_text ='<b>'.get_username($connect,$_SESSION["user_id"]).'</b> has
liked your Post- "'.strip_tags(substr($ntf_row["post_content"],0,30)).'..."';
$insert_query ="
INSERT INTO ntf_table(ntf_rcv_id,ntf_text,read_ntf)
VALUES('".$ntf_row["user_id"]."','".$ntf_text."','no')
";
$statement = $connect->prepare($insert_query);
$statement->execute();
}
echo 'Liked by you';
}
}
if($_POST["action"]=='user_like_list'){
$query= "SELECT * FROM like_table INNER JOIN
user_table ON user_table.user_id = like_table.user_id
WHERE like_table.post_id ='".$_POST["post_id"]."'
";
$statement = $connect->prepare($query);
$statement->execute();
$result=$statement->fetchAll();
foreach($result as $row){
$output .='<p>@'.$row["username"].'</p>';
}
echo $output;
}
if($_POST["action"]== "fetch_link_content"){
echo file_get_contents($_POST["url"][0]);
}
if($_POST["action"]=="update_ntf_sts"){
$query="
UPDATE ntf_table SET read_ntf='yes'
WHERE ntf_rcv_id ='".$_SESSION["user_id"]."'
";
$statement = $connect->prepare($query);
$statement->execute();
}
if($_POST["action"]=="search_user"){
$query ="
SELECT username,profile_image FROM user_table
WHERE username LIKE '%".$_POST["query"]."%'
AND user_id != '".$_SESSION["user_id"]."'
";
$statement =$connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row){
$data[] = $row["username"];
}
echo json_encode($data);
}
}
?>