-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpostvote.php
More file actions
89 lines (83 loc) · 3.84 KB
/
postvote.php
File metadata and controls
89 lines (83 loc) · 3.84 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
<?php
include('init.php');
if($_SERVER['REQUEST_METHOD']=="POST"){
//Initializing the variables
$messages=array();
$response=array();
$user_id = clean($_POST['user_id']);
if(isset($_POST['post_id']))
$id = clean($_POST['post_id']);
else if(isset($_POST['comment_id']))
$id = $_POST['comment_id'];
$upordown = clean($_POST['upordown']);
$what = clean($_POST['what']);
if(empty($user_id)){
$messages[] = "Login to add an upvote or downvote";
$response['status'] = 408;
}else{
if($what == "post"){
$select = "SELECT `upvotes_ids`, `downvotes_ids` FROM `posts` WHERE `id`=$id";
}else if( $what == "comment"){
$select = "SELECT `upvotes_ids`, `downvotes_ids` FROM `threads` WHERE `id`=$id";
}else{
$response['status'] = 408;
$messages[] = "Something went wrong!!";
}
$result = query($select);
if(row_count($result) == 1){
$row = fetch_array($result);
$upvotes = unserialize($row['upvotes_ids']);
$downvotes = unserialize($row['downvotes_ids']);
if($upordown == 1){
if(in_array($user_id,$upvotes)){
unset($upvotes[array_search($user_id, $upvotes)]);
$upvotes = array_values($upvotes);
$messages[] = "Removed Upvote";
}else{
array_push($upvotes,$user_id);
$messages[] = "Added Upvote";
if(in_array($user_id, $downvotes)){
unset($downvotes[array_search($user_id, $downvotes)]);
$downvotes = array_values($downvotes);
}
}
}else if($upordown == -1){
if(in_array($user_id,$downvotes)){
$messages[] = "Removed downvote";
unset($downvotes[array_search($user_id, $downvotes)]);
$downvotes = array_values($downvotes);
}else{
array_push($downvotes,$user_id);
$messages[] = "Added downvote";
if(in_array($user_id, $upvotes)){
unset($upvotes[array_search($user_id, $upvotes)]);
$upvotes = array_values($upvotes);
}
}
}
$upvotes = serialize($upvotes);
$downvotes = serialize($downvotes);
if($what == "post"){
$sql = "UPDATE `posts` SET `upvotes_ids`='$upvotes',`downvotes_ids`='$downvotes' WHERE `id`=$id";
}else if( $what == "comment"){
$sql = "UPDATE `threads` SET `upvotes_ids`='$upvotes',`downvotes_ids`='$downvotes' WHERE `id`=$id";
}else{
$response['status'] = 408;
$messages[] = "Something went wrong!!";
}
$result = query($sql);
if(!$result){
$messages[] = "Query Failed";
$response['status'] = 408;
}else{
$response['status'] = 200;
}
}else{
$messages[] = "Post not found";
$response['status'] = 408;
}
}
$response['messages'] = $messages;
echo json_encode($response);
}
?>