forked from SAAR-IITP/SAAR-IITP
-
Notifications
You must be signed in to change notification settings - Fork 13
/
post.php
331 lines (301 loc) · 15.5 KB
/
post.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
<?php
session_start();
if(!isset($_GET['q'])){
header("location: chat.php");
}
?>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SAAR | Disscussion Forum</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="apple-touch-icon" href="apple-touch-icon.png">
<link rel="stylesheet" href="css/fontAwesome.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800" rel="stylesheet">
<link rel="stylesheet" href="css/chat.css" />
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<style>
.nav-item {
margin: 5px 10px;
}
</style>
<script type="text/javascript">
var $_GET = {};
if(document.location.toString().indexOf('?') !== -1) {
var query = document.location
.toString()
// get the query string
.replace(/^.*?\?/, '')
// and remove any existing hash string (thanks, @vrijdenker)
.replace(/#.*$/, '')
.split('&');
for(var i=0, l=query.length; i<l; i++) {
var aux = decodeURIComponent(query[i]).split('=');
$_GET[aux[0]] = aux[1];
}
}
$(document).ready(function(){
$.ajax({
type: "GET",
url: "postdata.php",
data: {
'post_id': $_GET['q']
},
success: function(data){
$('#result').html(data);
}
});
$(document).on('click','#upvote_post',function(e) {
let user_id = $('#user_id').val();
let post_id = $_GET['q'];
vote(post_id,user_id,1);
});
$(document).on('click','#downvote_post',function(e) {
let user_id = $('#user_id').val();
let post_id = $_GET['q'];
vote(post_id,user_id,-1);
});
$(document).on('click','#delete_post',function(e){
let user_id = $('#user_id').val();
let access_token = $('#access_token').val();
let post_id = $_GET['q'];
document.location.href = "https://saar.iitp.ac.in/chat.php";
$.ajax({
type: "POST",
url: "./api/deletepost.php",
data: {
'post_id': $_GET['q'],
'user_id': user_id,
'access_token': access_token
},
success: function(data){
data = JSON.parse(data);
$('#msg').html(`<div class="alert alert-warning alert-dismissible fade show" style="position: fixed;top: 30px;left: 45%;z-index:10;" role="alert">
${data['messages'][0]}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>`);
}
});
});
$(document).on('click','#delete_comment',function(e){
let user_id = $('#user_id').val();
let post_id = $_GET['q'];
let comment_id = this.dataset.id;
let access_token = $('#access_token').val();
$.ajax({
type: "POST",
url: "./api/deletereply.php",
data: {
'post_id': $_GET['q'],
'comment_id': comment_id,
'user_id': user_id,
'access_token':access_token
},
success: function(data){
data = JSON.parse(data);
$('#msg').html(`<div class="alert alert-warning alert-dismissible fade show" style="position: fixed;top: 30px;left: 45%;z-index:10;" role="alert">
${data['messages'][0]}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>`);
}
});
});
$(document).on('click','#upvote_comment',function(){
let comment_id = this.dataset.id;
let user_id = $('#user_id').val();
vote_comment(comment_id,user_id,1);
});
$(document).on('click','#downvote_comment',function(){
let comment_id = this.dataset.id;
let user_id = $('#user_id').val();
vote_comment(comment_id,user_id,-1);
});
$(document).on("click","#refresh", function(){
$.ajax({
type: "GET",
url: "postdata.php",
data: {
'post_id': $_GET['q']
},
success: function(data){
$('#result').html(data);
$('#msg').html(`<div class="alert alert-warning alert-dismissible fade show" style="position: fixed;top: 30px;left: 45%;z-index:10;" role="alert">
Refreshed
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>`);
}
});
});
$("#add_comment").on("click",function(){
let bodyval = $('#comment_body').val();
let user_name = $('#user_name').val();
let user_img = $('#user_img').val();
let user_id = $('#user_id').val();
$('#comment_body').val('');
$.ajax({
type: "POST",
// url: "./api/createReply.php",
url: "./api/createReply.php",
data: {
"user_id": user_id,
"user_name": user_name,
"user_img": user_img,
"post_id":$_GET['q'],
"body": bodyval
},
success: function(data){
data = JSON.parse(data);
$('#msg').html(`<div class="alert alert-warning alert-dismissible fade show" style="position: fixed;top: 30px;left: 45%;z-index:10;" role="alert">
${data['messages'][0]}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>`);
}
});
});
});
function vote(post_id,user_id,upordown){
$.ajax({
type: "POST",
// url: "./api/postvote.php",
url: "./api/postvote.php",
data: {
'post_id': post_id,
'user_id': user_id,
'upordown': upordown,
'what': "post"
},
success: function(data){
data = JSON.parse(data);
$('#msg').html(`<div class="alert alert-warning alert-dismissible fade show" style="position: fixed;top: 30px;left: 45%;z-index:10;" role="alert">
${data['messages'][0]}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>`);
}
});
}
function vote_comment(comment_id,user_id,upordown){
$.ajax({
type: "POST",
// url: "./api/postvote.php",
url: "./api/postvote.php",
data: {
'comment_id': comment_id,
'user_id': user_id,
'upordown': upordown,
'what': "comment"
},
success: function(data){
data = JSON.parse(data);
$('#msg').html(`<div class="alert alert-warning alert-dismissible fade show" style="position: fixed;top: 30px;left: 45%;z-index:10;" role="alert">
${data['messages'][0]}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>`);
}
});
}
function autoRefresh_div()
{
$.ajax({
type: "GET",
url: "postdata.php",
data: {
'post_id': $_GET['q']
},
success: function(data){
$('#result').html(data);
}
});// a function which will load data from other file after x seconds
// $("#result").append("hil<br>");
}
setInterval('autoRefresh_div()', 2000);
</script>
</head>
<body style="height:100%;width:100%">
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container">
<a class="navbar-brand" href="#"><img src="img/logo1.png" alt="SAAR" style="height: 90px;"></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto" style="margin: 10px auto;">
<li class="nav-item active">
<a class="nav-link" href="./index.php">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a href="./chat.php"><button type="button" class="btn btn-primary" >
All Posts
</button></a>
</li>
<li class="nav-item">
<?php if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true ){ ?>
<a href="./portal.php"><button type="button" class="btn btn-primary">
Profile
</button></a>
<?php }else{ ?>
<a href="./signin.php"><button type="button" class="btn btn-primary">
Login
</button></a>
<?php } ?>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div id="msg">
</div>
<div id="result" style="padding-top:15px;">
<div style="padding: 20px; width:100%;" class="text-center">Loading..</div>
</div>
<div class="jumbotron">
<?php if(isset($_SESSION['loggedin']) && $_SESSION['loggedin']== true){ ?>
<div class="row">
<div class="col-lg-1 col-sm-2 res">
<img src="<?php echo $_SESSION['img_url'];?>" class="profile_image" ></img>
<div class="username"><strong><?php echo $_SESSION['fname'].' '.$_SESSION['lname'];?></strong></div>
</div>
<div class="col-lg-11 col-sm-10">
<input type="text" class="form-control mb-3" placeholder="Add a comment..." id="comment_body">
<input type="text" value="<?php echo $_SESSION['fname'].' '.$_SESSION['lname'];?>" id="user_name" hidden>
<input type="text" value="<?php echo $_SESSION['img_url'];?>" id="user_img" hidden>
<input type="text" value="<?php echo $_SESSION['user_id'];?>" id="user_id" hidden>
<input type="text" value="<?php echo $_SESSION['access_token'];?>" id="access_token" hidden>
<span class="input-group-btn">
<button class="btn btn-info " type="button" id="add_comment">POST</button>
</span>
<hr style="margin-top:10px">
</div>
</div>
<?php }else{ ?>
<a href="signup.php"><button class="btn btn-success">Sign Up</button></a> to join this conversation. Already have an account? <a href="signin.php" style="color: #007bff;">Sign In to comment</a>
<?php } ?>
</div>
<br><br>
</div>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<script src="jquery.min.js"></script>
<script src="jquery-ui.js"></script>
</body>
</html>