Skip to content

Commit 586eeab

Browse files
committed
Do not show deleted comments in the total
They are not sent at all with the API request.
1 parent dd8dfd1 commit 586eeab

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

forplay.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@
193193

194194
$get_comments_sql = "SELECT for_comments.comment_id
195195
FROM for_comments
196-
WHERE for_comments.article_id = {$article['article_id']};";
196+
WHERE for_comments.article_id = {$article['article_id']}
197+
AND for_comments.deleted = 0;";
197198

198199
$get_comments_result = mysqli_query($link, $get_comments_sql);
199200

forsecure/comment.php

+70-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
SET `read` = now()
115115
WHERE profile_id = {$profile['profile_id']};";
116116

117-
//$comment_result = mysqli_query($link, $comment_sql);
117+
$comment_result = mysqli_query($link, $comment_sql);
118118

119119
echo json_encode(
120120
array(
@@ -131,8 +131,9 @@
131131

132132
if (getenv('REQUEST_METHOD') == 'GET' && ! isset($_GET['profileId'])) {
133133
$get_comments_sql = isset($_GET['articleId']) ? "'{$_GET ['articleId']}'" : "ANY (SELECT article_id FROM for_comments)";
134-
$order_sql = isset($_GET['origin']) ? "banned ASC, flags DESC, updated DESC, created DESC LIMIT 1000" : "path";
134+
$order_sql = isset($_GET['origin']) ? "banned ASC, flags DESC, IF (updated > created, updated, created) DESC LIMIT 1000" : "path";
135135
$profile_id_sql = isset($profile) ? "{$profile['profile_id']}" : "null";
136+
$deleted_sql = isset($_GET['origin']) ? "" : "AND for_comments.deleted = 0";
136137

137138
$comments_sql = "SELECT for_comments.*,
138139
for_profiles.email,
@@ -157,6 +158,7 @@
157158
ON for_comments.comment_id = for_likes.comment_id
158159
AND for_likes.profile_id = $profile_id_sql
159160
WHERE for_comments.article_id = $get_comments_sql
161+
$deleted_sql
160162
ORDER BY $order_sql;";
161163

162164
$comments_result = mysqli_query($link, $comments_sql);
@@ -208,6 +210,72 @@
208210
$comment_sql = false;
209211
$like_sql = false;
210212

213+
/**
214+
* Strip HTML tags, attributes and new lines at the end;
215+
* Some tags and attributes are allowed.
216+
*/
217+
218+
if (isset($post_comment['comment'])) {
219+
$commentHtml = html_entity_decode($post_comment['comment']);
220+
$commentHtml = strip_tags($commentHtml,
221+
'<img><a><i><b><em><strong><br>');
222+
223+
$dom = new DOMDocument();
224+
225+
/**
226+
* Some stupid error for invalid content.
227+
* Diable it temporary.
228+
* http://stackoverflow.com/questions/1685277/warning-domdocumentloadhtml-htmlparseentityref-expecting-in-entity
229+
*/
230+
231+
$internalErrors = libxml_use_internal_errors(true);
232+
233+
/**
234+
* Need to force utf-8 for bulgarian characters.
235+
*/
236+
237+
$dom->loadHTML('<?xml encoding="utf-8" ?>' . $commentHtml);
238+
239+
/**
240+
* Restore internal errors.
241+
*/
242+
243+
libxml_use_internal_errors($internalErrors);
244+
245+
$xpath = new DOMXPath($dom);
246+
$elements = $xpath->query("//*");
247+
248+
foreach ($elements as $element) {
249+
for ($i = $element->attributes->length; -- $i >= 0;) {
250+
$name = $element->attributes->item($i)->name;
251+
252+
if (('img' === $element->nodeName && 'src' === $name) ||
253+
('img' === $element->nodeName && 'alt' === $name) ||
254+
('a' === $element->nodeName && 'href' === $name) ||
255+
('a' === $element->nodeName && 'target' === $name)) {
256+
257+
continue;
258+
}
259+
260+
$element->removeAttribute($name);
261+
}
262+
}
263+
264+
$commentHtml = $dom->saveHTML();
265+
266+
/**
267+
* This one above will wrap everything in paragraph.
268+
*/
269+
270+
$commentHtml = strip_tags($commentHtml,
271+
'<img><a><i><b><em><strong><br>');
272+
273+
$post_comment['comment'] = preg_replace(
274+
'#(( ){0,}<br( {0,})(/{0,1})>){1,}$#i', '', $commentHtml);
275+
$post_comment['comment'] = mysqli_real_escape_string($link,
276+
$post_comment['comment']);
277+
}
278+
211279
if (isset($post_comment['parentCommentId'])) {
212280
$get_parent_comment_sql = "SELECT * FROM for_comments
213281
WHERE comment_id = {$post_comment['parentCommentId']}

0 commit comments

Comments
 (0)