1
difenduandada
2024-10-15 7fd2948ee35c8e147ed35ce6d8502f94a98ddd22
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
<?php
 
require('../config.php');
require('../init.php');
 
if(get_setting_value('comments')){
    if(isset($_POST['send']) && $login_user){
        // Deprecated since v165
        // Replaced with new Comment system
        // Still kept for compatibility with the old commenting system
        $conn = open_connection();
        if(isset($_POST['source']) && $_POST['source'] == 'jquery-comments'){
            if(!$_POST['parent']){
                $_POST['parent'] = null;
            }
            $_POST['content'] = trim_string(comment_filtering($_POST['content']));
            $approved = 1;
            if(get_setting_value('moderate_comment') && $login_user->role != 'admin'){
                // Moderate comment is activated
                $approved = 0;
            }
            $sql = 'INSERT INTO comments (parent_id, game_id, comment, sender_id, sender_username, created_date, approved) VALUES (:parent_id, :game_id, :comment, :sender_id, :sender_username, :created_date, :approved)';
            $st = $conn->prepare($sql);
            $st->bindValue(":parent_id", $_POST['parent'], PDO::PARAM_INT);
            $st->bindValue(":game_id", $_POST['game_id'], PDO::PARAM_INT);
            $st->bindValue(":comment", $_POST['content'], PDO::PARAM_STR);
            $st->bindValue(":sender_id", $login_user->id, PDO::PARAM_INT);
            $st->bindValue(":sender_username", $login_user->username, PDO::PARAM_STR);
            $st->bindValue(":created_date", date('Y-m-d H:i:s'), PDO::PARAM_STR);
            $st->bindValue(":approved", $approved, PDO::PARAM_INT);
            $st->execute();
 
            $login_user->add_xp(20);
 
            echo('success');
        }
    }
    if(isset($_POST['load']) && isset($_POST['game_id'])){
        // Deprecated since v165
        // Replaced with new Comment system
        // Still kept for compatibility with the old commenting system
        $conn = open_connection();
        $sql = 'SELECT * FROM comments WHERE game_id = :game_id AND approved = 1 ORDER BY id asc, parent_id asc LIMIT 50';
        $st = $conn->prepare($sql);
        $st->bindValue(":game_id", $_POST['game_id'], PDO::PARAM_INT);
        $st->execute();
        $row = $st->fetchAll(PDO::FETCH_ASSOC);
        $list = array();
        foreach ($row as $item) {
            $item['avatar'] = get_user_avatar($item['sender_username']);
            $list[] = $item;
        }
        echo json_encode((array)$list);
    }
    // New comment system
    if(isset($_POST['send_comment']) && $login_user){
        if(strlen($_POST['content']) < 2){
            echo('too short');
            return;
        }
        $conn = open_connection();
        $parent_id = isset($_POST['parent']) && $_POST['parent'] !== '' ? (int)$_POST['parent'] : null;
        $_POST['content'] = trim_string(comment_filtering($_POST['content']));
        $approved = 1;
        if(get_setting_value('moderate_comment') && $login_user->role != 'admin'){
            // Moderate comment is activated
            $approved = 0;
        }
        $sql = 'INSERT INTO comments (parent_id, game_id, comment, sender_id, sender_username, created_date, approved) VALUES (:parent_id, :game_id, :comment, :sender_id, :sender_username, :created_date, :approved)';
        $st = $conn->prepare($sql);
        if ($parent_id === null) {
            $st->bindValue(":parent_id", $parent_id, PDO::PARAM_NULL);
        } else {
            $st->bindValue(":parent_id", $parent_id, PDO::PARAM_INT);
        }
        $st->bindValue(":game_id", $_POST['game_id'], PDO::PARAM_INT);
        $st->bindValue(":comment", $_POST['content'], PDO::PARAM_STR);
        $st->bindValue(":sender_id", $login_user->id, PDO::PARAM_INT);
        $st->bindValue(":sender_username", $login_user->username, PDO::PARAM_STR);
        $st->bindValue(":created_date", date('Y-m-d H:i:s'), PDO::PARAM_STR);
        $st->bindValue(":approved", $approved, PDO::PARAM_INT);
        $st->execute();
        $login_user->add_xp(20);
        echo('success');
    } elseif(isset($_POST['load_root_comments']) && isset($_POST['game_id']) && isset($_POST['amount'])) {
        $conn = open_connection();
        $offset = isset($_POST['offset']) ? (int)$_POST['offset'] : 0;
        $limit = (int)$_POST['amount'];
        if($limit > 30){
            $limit = 30;
        }
        $sql = 'SELECT c.*, COUNT(r.id) as reply_count 
                FROM comments c
                LEFT JOIN comments r ON c.id = r.parent_id
                WHERE c.game_id = :game_id AND (c.parent_id IS NULL OR c.parent_id = 0) AND c.approved = 1
                GROUP BY c.id
                ORDER BY c.id DESC LIMIT '.$limit.' OFFSET :offset';
        $st = $conn->prepare($sql);
        $st->bindValue(":game_id", $_POST['game_id'], PDO::PARAM_INT);
        $st->bindValue(":offset", $offset, PDO::PARAM_INT);
        $st->execute();
        $row = $st->fetchAll(PDO::FETCH_ASSOC);
        $list = [];
        foreach ($row as $item) {
            $item['avatar'] = get_user_avatar($item['sender_username']);
            $item['has_replies'] = $item['reply_count'] > 0;
            $item['server_date'] = date('Y-m-d H:i:s');
            unset($item['reply_count']); // remove the reply_count as it's not needed anymore
            $list[] = $item;
        }
        echo json_encode((array)$list);
    } elseif(isset($_POST['load_replies']) && isset($_POST['parent_id']) && isset($_POST['amount'])) {
        $conn = open_connection();
        $limit = (int)$_POST['amount'];
        if($limit > 30){
            $limit = 30;
        }
        $sql = 'SELECT * FROM comments WHERE parent_id = :parent_id AND approved = 1 ORDER BY id DESC LIMIT '.$limit;
        $st = $conn->prepare($sql);
        $st->bindValue(":parent_id", $_POST['parent_id'], PDO::PARAM_INT);
        $st->execute();
        $row = $st->fetchAll(PDO::FETCH_ASSOC);
        $list = [];
        foreach ($row as $item) {
            $item['avatar'] = get_user_avatar($item['sender_username']);
            $item['server_date'] = date('Y-m-d H:i:s');
            $list[] = $item;
        }
        echo json_encode((array)$list);
    }
 
}
 
if(isset($_POST['delete']) && $login_user){
    $conn = open_connection();
    if( USER_ADMIN && !ADMIN_DEMO){
        $sql = 'DELETE FROM comments WHERE id = :id LIMIT 1';
        $st = $conn->prepare($sql);
        $st->bindValue(":id", $_POST['id'], PDO::PARAM_INT);
        $st->execute();
    } else {
        $sql = 'DELETE FROM comments WHERE sender_id = :sender_id AND id = :id LIMIT 1';
        $st = $conn->prepare($sql);
        $st->bindValue(":sender_id", $login_user->id, PDO::PARAM_INT);
        $st->bindValue(":id", $_POST['id'], PDO::PARAM_INT);
        $st->execute();
    }
    echo 'deleted';
}
 
if(isset($_POST['approve']) && $login_user && USER_ADMIN){
    $conn = open_connection();
    $sql = 'UPDATE comments SET approved = 1 WHERE id = :id LIMIT 1';
    $st = $conn->prepare($sql);
    $st->bindValue(":id", $_POST['id'], PDO::PARAM_INT);
    $st->execute();
    echo 'ok';
}
 
function comment_filtering($comment){
    if(file_exists(ABSPATH.'includes/banned-words-comment.json')){
        $words = json_decode(file_get_contents(ABSPATH.'includes/banned-words-comment.json'), true);
        $comment = str_ireplace($words, '***', $comment);
    }
    return $comment;
}
 
function trim_string($str) {
    if (strlen($str) > 400) {
        return substr($str, 0, 397) . '...';
    }
    return $str;
}
 
?>