Repository navigation
Expand file tree
/
Copy pathcomments.php
More file actions
365 lines (303 loc) · 12.4 KB
/
Copy pathcomments.php
File metadata and controls
365 lines (303 loc) · 12.4 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
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
<?php
session_start();
//Checking for session
$loggedIn = isset($_SESSION['loggedIn']) ? $_SESSION['loggedIn'] : false;
//Require database connection
require "files/dbconfig.php";
$con = new DBConfig();
//Require class user to get user details
require "files/user.php";
$user = new User();
//Require classs to get post data
require "files/posts.php";
$post = new Posts();
//Require functions file
require "includes/functions.php";
//Require comments class
require 'files/comments.php';
$commentsObj = new Comments();
//Require notification class
require 'files/notificationClass.php';
$notification = new notification();
//Require class authentication
require 'files/authentication.php';
$auth = new authentication;
//Require meta info for creating links
require 'files/meta.php';
$found = 0;
if ($loggedIn == true){
$uid = $_SESSION['uid'];
$userlvl = $user->userLevel($uid);
$permission = $user->userPermissions($userlvl);
$canComment = $permission['comment'];
}
$pid = isset($_POST['pid']) ? $_POST['pid'] : false;
if ($pid == false){
echo '
<div style="color: var(--textHM);"> There was an error </div>
';
exit();
} elseif (!filter_var($pid, FILTER_VALIDATE_INT)){
echo 'Some error occured
';
exit();
}
$postData = $post->getPostsInfo($pid);
if ($postData != -1 && $postData['available'] == 1){
$allowCom = $postData['allow-com'];
$postTitile = $postData['title'];
$postuid = $postData['uid'];
$found = 1;
}
//Add comment to the database after proper validation
if ($loggedIn == true && isset($_POST['action']) && $_POST['action'] == 'add-comment'){
if ($found == 1 && $allowCom != 'disabled' && $canComment == 1){
//setting date
date_default_timezone_set("Asia/Calcutta");
$date = date("Y-m-d H:i:s");
//Getting comment body
$commentBody = validData($_POST['comment']);
$cid = $commentsObj->addComment($uid, $pid, $date, $commentBody);
$auth->lastActive($_SESSION['uid']);
//Setting message for the user
$message = 'Your post <a href="'.$metaInfo['domain'].'/view-post.php?pid='.$pid.'">'.$postTitile.'</a> ' ;
$message .= 'got a new comment "'.$commentBody.'" by <a href="'.$metaInfo['domain'].'/view-profile.php?uid='.$uid.'">';
$message .= $user->userName($_SESSION['uid']).'</a>';
//session Uid must not be same as post uid
if ($uid != $postuid){
$notification->insertNoti($postuid, $message);
}
//get the inserted comment
$showComment = $commentsObj->getComment($cid);
//AUTHOR DETAILS
$authorName = $user->userName($showComment['uid']);
$userlvl = $user->userLevel($showComment['uid']);
$userGroup = $user->userGroup($userlvl);
$profileImage = $user->profileImage($showComment['uid']);
if ($profileImage === 0){
$profileImage = 'profile.png';
}
if ($userlvl == 4 || $userlvl == 5){
$star = '<div class="star-ao">
<span><i class="fa fa-star" aria-hidden="true"></i></span>
</div>';
} else {
$star = NULL;
}
echo '
<div class="c-p-i">
<div class="c-a-info">
<a href="'.$metaInfo['domain'].'/view-profile/'.$user->userName($showComment['uid']).'">
<img src="'.$metaInfo['domain'].'/profileImages/'.$profileImage.'" alt="profileImage">
'.$star.'
<div class="c-a-name">
<a href="'.$metaInfo['domain'].'/view-profile/'.$user->userName($showComment['uid']).'">'.$authorName.'</a>
</div>
<div class="c-a-lvl">
'.$userGroup.'
</div>
</div>
<div class="c-body">
'.nl2br($showComment['cbody']).'
</div>
<div class="c-t-date">
'.$showComment['cdate'].'
</div>
</div>
';
} else {
echo '
<div class="c-p-i">
<p style="color: var(--textHM);">Cannot process the action</p>
</div>
';
}
}
//Show comments after proper validation
if (isset($_POST['action']) && $_POST['action'] == 'show-comments'){
$page = isset($_POST['page']) ? $_POST['page'] : 1;
if (!filter_var($page, FILTER_VALIDATE_INT)){
echo 'Some error occured
';
exit();
}
if ($found == 1 && $pid != false){
$numberOfComments = $post->noComments($pid);
$limit = 10;
$pagesTtl = ceil($numberOfComments / $limit);
if ($page < 1 || $page > $pagesTtl){
$page = 1;
}
if ($numberOfComments > 0){
$comments = $commentsObj->showComments($pid, $page, 10);
foreach ($comments as $commentRow){
//Get user details
$commentUserName = $user->userName($commentRow['uid']);
$commentUserLevel = $user->userLevel($commentRow['uid']);
$commentUserGroup = $user->userGroup($commentUserLevel);
$commentProfileImage = $user->profileImage($commentRow['uid']);
if ($commentUserLevel == 4 || $commentUserLevel == 5){
$star = '<div class="star-ao">
<span><i class="fa fa-star" aria-hidden="true"></i></span>
</div>';
} else {
$star = NULL;
}
$checkAvailableComment = $commentRow['available'];
if ($checkAvailableComment == -1){
$commentRow['cbody'] = '<i>This comment was deleted</i>';
}
if ($loggedIn == true && $checkAvailableComment != -1 && ($user->userLevel($_SESSION['uid']) == 4 || $user->userLevel($_SESSION['uid']) == 5) ){
$editOptions = '
<div class="c-options">
<ul>
<li class="del-com" data-cid="'.$commentRow['cid'].'"><a href="javascript:0">Delete</a></li>
</ul>
</div>
';
} else {
$editOptions = '';
}
echo '
<div class="c-p-i">
<div class="c-a-info">
<a href="'.$metaInfo['domain'].'/view-profile/'.$user->userName($commentRow['uid']).'">
<img src="'.$metaInfo['domain'].'/profileImages/'.$commentProfileImage.'" alt="profile Image">
'.$star.'
</a>
<div class="c-a-name">
<a href="'.$metaInfo['domain'].'/view-profile/'.$user->userName($commentRow['uid']).'">'.$commentUserName.'</a>
</div>
<div class="c-a-lvl">
'.$commentUserGroup.'
</div>
</div>
<div class="c-body">
<p>'.nl2br($commentRow['cbody']).'
</p>
</div>
<div class="c-t-date">
'.$commentRow['cdate'].'
</div>
'.$editOptions.'
</div>
';
}
//Creating pagination
$totalPages = 1;
if ($numberOfComments > 0){
if ($numberOfComments > $limit){
$totalPages = ceil($numberOfComments / $limit);
$previous = $page > 1 ? $page - 1 : '#';
if ($page <= $totalPages){
$next = $page + 1;
} else {
$next = '';
}
}
}
if ($totalPages > 1){
echo '<div class="pagination">
<ul>
<li class="previous number" data-page="'.$previous.'">
<i class="fa fa-angle-double-left" aria-hidden="true"></i>
</li>
';
if ($totalPages > 1 && $totalPages < 6){
for ($i = 1; $i <= $totalPages; $i++){
$active = $page == $i ? 'active' : '';
echo '
<li class="number '.$active.'" data-page="'.$i.'">
'.$i.'
</li>
';
}
}
if ($totalPages > 5){
if ($page == 1 || $page == 2 || $page == 3) {
for($i = 1; $i < 4; $i++){
$active = $page == $i ? 'active' : '';
echo '
<li class="number '.$active.'" data-page="'.$i.'">
'.$i.'
</li>
';
}
echo '
<li class="cont">
...
</li>
';
for($i = $totalPages-1; $i <= $totalPages; $i++){
$active = $page == $i ? 'active' : '';
echo '
<li class="number '.$active.'" data-page="'.$i.'">
'.$i.'
</li>
';
}
} elseif ($page <= $totalPages){
echo '
<li class="number" data-page="1">
1
</li>
<li class="cont">
...
</li>
';
for($i = $totalPages-4; $i <= $totalPages; $i++){
$active = $page == $i ? 'active' : '';
echo '
<li class="number '.$active.'" data-page="'.$i.'">
'.$i.'
</li>
';
}
}
}
echo '
<li class="next number" data-page="'.$next.'">
<i class="fa fa-angle-double-right" aria-hidden="true"></i>
</li>
</div>
';
}
}
}
}
if(isset($_POST['action']) && $_POST['action'] == 'delete-comment'){
$cid = $_POST['cid'];
if (!filter_var($cid, FILTER_VALIDATE_INT)){
echo 'Some error occured
';
exit();
}
$commentDetails = $commentsObj->getComment($cid);
$uidfornotidelete = $commentDetails['uid'];
$commentuserlvldelete = $user->userLevel($uidfornotidelete);
$cbody = $commentDetails['cbody'];
if ($commentDetails != -1){
$sessionUserLevel = $user->userLevel($_SESSION['uid']);
if ($sessionUserLevel == 4 || $sessionUserLevel == 5){
//Setting message for the user
$deletemessage = 'Your comment ';
$deletemessage .= '"'.$cbody.'" was deleted by <a href="'.$metaInfo['domain'].'/view-profile.php?uid='.$_SESSION['uid'].'">';
$deletemessage .= $user->userName($_SESSION['uid']).'</a>';
if ($commentuserlvldelete != 5){
$commentsObj->deleteComment($cid);
$notification->insertNoti($uidfornotidelete, $deletemessage);
echo 1;
} else
if ($commentuserlvldelete == 5){
if ($user->userLevel($_SESSION['uid']) == 5){
$commentsObj->deleteComment($cid);
$notification->insertNoti($uidfornotidelete, $deletemessage);
echo 1;
}
}
else{
echo 0;
}
}
}
}