forked from WWBN/AVideo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomment.php
333 lines (292 loc) · 11.3 KB
/
comment.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
<?php
global $global, $config;
if(!isset($global['systemRootPath'])){
require_once '../videos/configuration.php';
}
require_once $global['systemRootPath'] . 'objects/bootGrid.php';
require_once $global['systemRootPath'] . 'objects/user.php';
require_once $global['systemRootPath'] . 'objects/video.php';
class Comment {
protected $id;
protected $comment;
protected $videos_id;
protected $users_id;
protected $comments_id_pai;
function __construct($comment, $videos_id, $id = 0) {
if (empty($id)) {
// get the comment data from comment
$this->comment = xss_esc($comment);
$this->videos_id = $videos_id;
$this->users_id = User::getId();
} else {
// get data from id
$this->load($id);
}
}
function getId() {
return $this->id;
}
function setComments_id_pai($comments_id_pai) {
$this->comments_id_pai = $comments_id_pai;
}
function getComments_id_pai() {
return $this->comments_id_pai;
}
function getUsers_id() {
return $this->users_id;
}
function setComment($comment) {
$this->comment = xss_esc($comment);
}
function getVideos_id() {
return $this->videos_id;
}
private function load($id) {
$row = $this->getComment($id);
if (empty($row))
return false;
foreach ($row as $key => $value) {
$this->$key = $value;
}
return true;
}
function save() {
global $global;
if(!User::isLogged()){
header('Content-Type: application/json');
die('{"error":"'.__("Permission denied").'"}');
}
//$this->comment = htmlentities($this->comment);
$this->comment = $global['mysqli']->real_escape_string($this->comment);
if(empty($this->comment)){
return false;
}
if(empty($this->comments_id_pai)){
$this->comments_id_pai = 'NULL';
}
if(empty($this->videos_id) && !empty($this->comments_id_pai)){
$comment = new Comment("", 0, $this->comments_id_pai);
$this->videos_id = $comment->getVideos_id();
}
if (!empty($this->id) ) {
if(!self::userCanAdminComment($this->id)){
return false;
}
$sql = "UPDATE comments SET "
. " comment = ?, modified = now() WHERE id = ?";
$resp = sqlDAL::writeSql($sql,"si",array(xss_esc($this->comment),$this->id));
} else {
$id = User::getId();
$sql = "INSERT INTO comments ( comment,users_id, videos_id, comments_id_pai, created, modified) VALUES "
. " (?, ?, ?, {$this->comments_id_pai}, now(), now())";
$resp = sqlDAL::writeSql($sql,"sii",array(xss_esc($this->comment),$id,$this->videos_id));
}
if((empty($resp))&&($global['mysqli']->errno!=0)){
die('Error (comment save) : (' . $global['mysqli']->errno . ') ' . $global['mysqli']->error);
}
if (empty($this->id)) {
// log_error("note: insert_id works? ".$global['mysqli']->insert_id); // success!
$id = $global['mysqli']->insert_id;
$this->id = $id;
} else {
$id = $this->id;
}
if(empty($this->comments_id_pai) || $this->comments_id_pai== 'NULL'){
YouPHPTubePlugin::afterNewComment($this->id);
}else{
YouPHPTubePlugin::afterNewResponse($this->id);
}
return $resp;
}
function delete() {
if(!self::userCanAdminComment($this->id)){
return false;
}
global $global;
if (!empty($this->id)) {
$sql = "DELETE FROM comments WHERE id = ?";
} else {
return false;
}
return sqlDAL::writeSql($sql,"i",array($this->id));
}
private function getComment($id) {
global $global;
$id = intval($id);
$sql = "SELECT * FROM comments WHERE id = ? LIMIT 1";
$res = sqlDAL::readSql($sql,"i",array($id));
$result = sqlDAL::fetchAssoc($res);
sqlDAL::close($res);
return ($res!=false) ? $result : false;
}
static function getAllComments($videoId = 0, $comments_id_pai = 'NULL') {
global $global;
$format = "";
$values = array();
$sql = "SELECT c.*, u.name as name, u.user as user, "
. " (SELECT count(id) FROM comments_likes as l where l.comments_id = c.id AND `like` = 1 ) as likes, "
. " (SELECT count(id) FROM comments_likes as l where l.comments_id = c.id AND `like` = -1 ) as dislikes ";
if (User::isLogged()) {
$sql .= ", (SELECT `like` FROM comments_likes as l where l.comments_id = c.id AND users_id = ? ) as myVote ";
$format .= "i";
$values[]=User::getId();
} else {
$sql .= ", 0 as myVote ";
}
$sql .= " FROM comments c LEFT JOIN users as u ON u.id = users_id LEFT JOIN videos as v ON v.id = videos_id WHERE 1=1 ";
if (!empty($videoId)) {
$sql .= " AND videos_id = ? ";
$format .= "i";
$values[] = $videoId;
}else if(!User::isAdmin() && empty ($comments_id_pai)){
if(!User::isLogged()){
die("can not see comments");
}
$users_id = User::getId();
$sql .= " AND (v.users_id = ? OR c.users_id = ?) ";
$format .= "ii";
$values[]=$users_id;
$values[]=$users_id;
}
if($comments_id_pai==='NULL' || empty ($comments_id_pai)){
$sql .= " AND (comments_id_pai IS NULL ";
if(empty($videoId) && User::isLogged()){
$users_id = User::getId();
$sql .= " OR c.users_id = ? ";
$format .= "i";
$values[]=$users_id;
}
$sql .= ") ";
}else{
$sql .= " AND comments_id_pai = ? ";
$format .= "s";
$values[]=$comments_id_pai;
}
$sql .= BootGrid::getSqlFromPost(array('name'));
$res = sqlDAL::readSql($sql,$format,$values);
$allData = sqlDAL::fetchAllAssoc($res);
sqlDAL::close($res);
$comment = array();
if ($res!=false) {
foreach ($allData as $row) {
$row['comment'] = str_replace('\n', "\n", $row['comment']);
$row['commentPlain'] = xss_esc_back($row['comment']);
$row['commentHTML'] = nl2br($row['commentPlain']);
$comment[] = $row;
}
//$comment = $res->fetch_all(MYSQLI_ASSOC);
} else {
$comment = false;
die($sql.'\nError : (' . $global['mysqli']->errno . ') ' . $global['mysqli']->error);
}
return $comment;
}
static function getTotalComments($videoId = 0, $comments_id_pai = 'NULL', $video_owner_users_id=0) {
global $global;
$format = "";
$values = array();
$sql = "SELECT c.id FROM comments c LEFT JOIN users as u ON u.id = users_id LEFT JOIN videos as v ON v.id = videos_id WHERE 1=1 ";
if (!empty($videoId)) {
$sql .= " AND videos_id = ? ";
$format .= "i";
$values[] = $videoId;
}else if(!User::isAdmin() && empty ($comments_id_pai)){
if(!User::isLogged()){
die("can not see comments");
}
$users_id = User::getId();
$sql .= " AND (v.users_id = ? OR c.users_id = ?) ";
$format .= "ii";
$values[] = $users_id;
$values[] = $users_id;
}
if($comments_id_pai==='NULL' || empty ($comments_id_pai)){
$sql .= " AND (comments_id_pai IS NULL ";
if(empty($videoId) && User::isLogged()){
$users_id = User::getId();
$sql .= " OR c.users_id = ? ";
$format .= "i";
$values[] = $users_id;
}
$sql .= ") ";
}else{
$sql .= " AND comments_id_pai = ? ";
$format .= "s";
$values[] = $comments_id_pai;
}
if(!empty($video_owner_users_id)){
$sql .= " AND v.users_id = ? ";
$format .= "i";
$values[] = $video_owner_users_id;
}
$sql .= BootGrid::getSqlSearchFromPost(array('name'));
$res = sqlDAL::readSql($sql,$format,$values);
$countRow = sqlDAL::num_rows($res);
sqlDAL::close($res);
return $countRow;
}
static function userCanAdminComment($comments_id){
if(!User::isLogged()){
return false;
}
if(User::isAdmin()){
return true;
}
$obj = new Comment("", 0, $comments_id);
if($obj->users_id == User::getId()){
return true;
}
$video = new Video("", "", $obj->videos_id);
if($video->getUsers_id() == User::getId()){
return true;
}
return false;
}
static function getTotalCommentsThumbsUpFromUser($users_id, $startDate, $endDate) {
global $global;
$sql = "SELECT id from comments WHERE users_id = ?";
$res = sqlDAL::readSql($sql,"i",array($users_id));
$fullData = sqlDAL::fetchAllAssoc($res);
sqlDAL::close($res);
$r = array('thumbsUp'=>0, 'thumbsDown'=>0 );
if ($res!=false) {
foreach ($fullData as $row) {
$format = "i";
$values = array($row['id']);
$sql = "SELECT id from comments_likes WHERE comments_id = ? AND `like` = 1 ";
if (!empty($startDate)) {
$sql .= " AND `created` >= ? ";
$format .= "s";
$values[] = $startDate;
}
if (!empty($endDate)) {
$sql .= " AND `created` <= ? ";
$format .= "s";
$values[] = $endDate;
}
$res = sqlDAL::readSql($sql,$format,$values);
$countRow = sqlDAL::num_rows($res);
sqlDAL::close($res);
$r['thumbsUp']+=$countRow;
$format = "i";
$values = array($row['id']);
$sql = "SELECT id from comments_likes WHERE comments_id = ? AND `like` = -1 ";
if (!empty($startDate)) {
$sql .= " AND `created` >= ? ";
$format .= "s";
$values[] = $startDate;
}
if (!empty($endDate)) {
$sql .= " AND `created` <= ? ";
$format .= "s";
$values[] = $endDate;
}
$res = sqlDAL::readSql($sql,$format,$values);
$countRow = sqlDAL::num_rows($res);
sqlDAL::close($res);
$r['thumbsDown']+=$countRow;
}
}
return $r;
}
}