Skip to content

Commit

Permalink
Fix(UI): Support new line in comments (sbpp#994)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rushaway authored Oct 16, 2024
1 parent 42d1e68 commit 0429e14
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 24 deletions.
2 changes: 1 addition & 1 deletion web/configs/version.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.8.0",
"git": "1411",
"git": "1420",
"dev": true
}
20 changes: 20 additions & 0 deletions web/includes/system-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,23 @@ function compareSanitizedString(string $str1, string $str2)
{
return (bool)(strcmp(filter_var($str1, FILTER_SANITIZE_SPECIAL_CHARS), filter_var($str2, FILTER_SANITIZE_SPECIAL_CHARS)) === 0);
}

/**
* @param string $text
* @return string
*/
function encodePreservingBr($text) {
// Split the text at <br> tags, preserving the tags in the result
$parts = preg_split('/(<br\s*\/?>)/i', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
$result = '';

foreach ($parts as $part) {
if (preg_match('/^<br\s*\/?>$/i', $part)) {
$result .= "\n"; // Replace <br /> with newline
} else {
$result .= htmlspecialchars($part, ENT_QUOTES, 'UTF-8'); // Encode the rest
}
}

return nl2br($result); // Convert newlines back to <br /> for HTML
}
26 changes: 18 additions & 8 deletions web/pages/admin.bans.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,10 @@

$cdata['comname'] = $commentres->fields['comname'];
$cdata['added'] = Config::time($commentres->fields['added']);
$cdata['commenttxt'] = htmlspecialchars($commentres->fields['commenttxt']);
$cdata['commenttxt'] = str_replace("\n", "<br />", $cdata['commenttxt']);
$commentText = html_entity_decode($commentres->fields['commenttxt'], ENT_QUOTES | ENT_HTML5, 'UTF-8');
$commentText = encodePreservingBr($commentText);
$commentText = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="\$1" target="_blank">\$1</a>', $commentText);
$cdata['commenttxt'] = $commentText;

if (!empty($commentres->fields['edittime'])) {
$cdata['edittime'] = Config::time($commentres->fields['edittime']);
Expand Down Expand Up @@ -400,8 +402,10 @@

$cdata['comname'] = $commentres->fields['comname'];
$cdata['added'] = Config::time($commentres->fields['added']);
$cdata['commenttxt'] = htmlspecialchars($commentres->fields['commenttxt']);
$cdata['commenttxt'] = str_replace("\n", "<br />", $cdata['commenttxt']);
$commentText = html_entity_decode($commentres->fields['commenttxt'], ENT_QUOTES | ENT_HTML5, 'UTF-8');
$commentText = encodePreservingBr($commentText);
$commentText = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="\$1" target="_blank">\$1</a>', $commentText);
$cdata['commenttxt'] = $commentText;

if (!empty($commentres->fields['edittime'])) {
$cdata['edittime'] = Config::time($commentres->fields['edittime']);
Expand Down Expand Up @@ -554,8 +558,11 @@

$cdata['comname'] = $commentres->fields['comname'];
$cdata['added'] = Config::time($commentres->fields['added']);
$cdata['commenttxt'] = htmlspecialchars($commentres->fields['commenttxt']);
$cdata['commenttxt'] = str_replace("\n", "<br />", $cdata['commenttxt']);
$commentText = html_entity_decode($commentres->fields['commenttxt'], ENT_QUOTES | ENT_HTML5, 'UTF-8');
$commentText = encodePreservingBr($commentText);
// Parse links and wrap them in a <a href=""></a> tag to be easily clickable
$commentText = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="\$1" target="_blank">\$1</a>', $commentText);
$cdata['commenttxt'] = $commentText;

if (!empty($commentres->fields['edittime'])) {
$cdata['edittime'] = Config::time($commentres->fields['edittime']);
Expand Down Expand Up @@ -694,8 +701,11 @@

$cdata['comname'] = $commentres->fields['comname'];
$cdata['added'] = Config::time($commentres->fields['added']);
$cdata['commenttxt'] = htmlspecialchars($commentres->fields['commenttxt']);
$cdata['commenttxt'] = str_replace("\n", "<br />", $cdata['commenttxt']);
$commentText = html_entity_decode($commentres->fields['commenttxt'], ENT_QUOTES | ENT_HTML5, 'UTF-8');
$commentText = encodePreservingBr($commentText);
// Parse links and wrap them in a <a href=""></a> tag to be easily clickable
$commentText = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="\$1" target="_blank">\$1</a>', $commentText);
$cdata['commenttxt'] = $commentText;

if (!empty($commentres->fields['edittime'])) {
$cdata['edittime'] = Config::time($commentres->fields['edittime']);
Expand Down
20 changes: 12 additions & 8 deletions web/pages/page.banlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -647,10 +647,11 @@ function setPostKey()

$cdata['comname'] = $commentres->fields['comname'];
$cdata['added'] = Config::time($commentres->fields['added']);
$cdata['commenttxt'] = htmlspecialchars($commentres->fields['commenttxt']);
$cdata['commenttxt'] = str_replace("\n", "<br />", $cdata['commenttxt']);
$commentText = html_entity_decode($commentres->fields['commenttxt'], ENT_QUOTES | ENT_HTML5, 'UTF-8');
$commentText = encodePreservingBr($commentText);
// Parse links and wrap them in a <a href=""></a> tag to be easily clickable
$cdata['commenttxt'] = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1" target="_blank">$1</a>', $cdata['commenttxt']);
$commentText = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="\$1" target="_blank">\$1</a>', $commentText);
$cdata['commenttxt'] = $commentText;

if (!empty($commentres->fields['edittime'])) {
$cdata['edittime'] = Config::time($commentres->fields['edittime']);
Expand Down Expand Up @@ -742,7 +743,8 @@ function setPostKey()
if (isset($_GET["cid"])) {
$_GET["cid"] = (int) $_GET["cid"];
$ceditdata = $GLOBALS['db']->GetRow("SELECT * FROM " . DB_PREFIX . "_comments WHERE cid = '" . $_GET["cid"] . "'");
$ctext = htmlspecialchars($ceditdata['commenttxt']);
$ctext = html_entity_decode($ceditdata['commenttxt'], ENT_QUOTES | ENT_HTML5, 'UTF-8');
$ctext = htmlspecialchars($ctext, ENT_QUOTES | ENT_HTML5, 'UTF-8');
$cotherdataedit = " AND cid != '" . $_GET["cid"] . "'";
} else {
$cotherdataedit = "";
Expand All @@ -765,10 +767,12 @@ function setPostKey()
$coment = [];
$coment['comname'] = $cotherdata->fields['comname'];
$coment['added'] = Config::time($cotherdata->fields['added']);
$coment['commenttxt'] = htmlspecialchars($cotherdata->fields['commenttxt']);
$coment['commenttxt'] = str_replace("\n", "<br />", $coment['commenttxt']);
$commentText = html_entity_decode($cotherdata->fields['commenttxt'], ENT_QUOTES | ENT_HTML5, 'UTF-8');
$commentText = encodePreservingBr($commentText);
// Parse links and wrap them in a <a href=""></a> tag to be easily clickable
$coment['commenttxt'] = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1" target="_blank">$1</a>', $coment['commenttxt']);
$commentText = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="\$1" target="_blank">\$1</a>', $commentText);
$coment['commenttxt'] = $commentText;

if ($cotherdata->fields['editname'] != "") {
$coment['edittime'] = Config::time($cotherdata->fields['edittime']);
$coment['editname'] = $cotherdata->fields['editname'];
Expand Down Expand Up @@ -812,4 +816,4 @@ function setPostKey()
$theme->assign('can_delete', $userbank->HasAccess(ADMIN_DELETE_BAN));
$theme->assign('view_bans', ($userbank->HasAccess(ADMIN_OWNER | ADMIN_EDIT_ALL_BANS | ADMIN_EDIT_OWN_BANS | ADMIN_EDIT_GROUP_BANS | ADMIN_UNBAN | ADMIN_UNBAN_OWN_BANS | ADMIN_UNBAN_GROUP_BANS | ADMIN_DELETE_BAN)));
$theme->assign('can_export', ($userbank->HasAccess(ADMIN_OWNER) || Config::getBool('config.exportpublic')));
$theme->display('page_bans.tpl');
$theme->display('page_bans.tpl');
17 changes: 10 additions & 7 deletions web/pages/page.commslist.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,10 +607,11 @@ function setPostKey()

$cdata['comname'] = $commentres->fields['comname'];
$cdata['added'] = Config::time($commentres->fields['added']);
$cdata['commenttxt'] = $commentres->fields['commenttxt'];
$cdata['commenttxt'] = str_replace("\n", "<br />", $cdata['commenttxt']);
$commentText = html_entity_decode($commentres->fields['commenttxt'], ENT_QUOTES | ENT_HTML5, 'UTF-8');
$commentText = encodePreservingBr($commentText);
// Parse links and wrap them in a <a href=""></a> tag to be easily clickable
$cdata['commenttxt'] = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1" target="_blank">$1</a>', $cdata['commenttxt']);
$commentText = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="\$1" target="_blank">\$1</a>', $commentText);
$cdata['commenttxt'] = $commentText;

if (!empty($commentres->fields['edittime'])) {
$cdata['edittime'] = Config::time($commentres->fields['edittime']);
Expand Down Expand Up @@ -700,7 +701,8 @@ function setPostKey()
$theme->assign('commenttype', (isset($_GET["cid"]) ? "Edit" : "Add"));
if (isset($_GET["cid"])) {
$ceditdata = $GLOBALS['db']->GetRow("SELECT * FROM " . DB_PREFIX . "_comments WHERE cid = '" . (int) $_GET["cid"] . "'");
$ctext = $ceditdata['commenttxt'];
$ctext = html_entity_decode($ceditdata['commenttxt'], ENT_QUOTES | ENT_HTML5, 'UTF-8');
$ctext = htmlspecialchars($ctext, ENT_QUOTES | ENT_HTML5, 'UTF-8');
$cotherdataedit = " AND cid != '" . (int) $_GET["cid"] . "'";
} else {
$cotherdataedit = "";
Expand All @@ -720,9 +722,10 @@ function setPostKey()
$coment = [];
$coment['comname'] = $cotherdata->fields['comname'];
$coment['added'] = Config::time($cotherdata->fields['added']);
$coment['commenttxt'] = str_replace("\n", "<br />", $cotherdata->fields['commenttxt']);
// Parse links and wrap them in a <a href=""></a> tag to be easily clickable
$coment['commenttxt'] = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1" target="_blank">$1</a>', $coment['commenttxt']);
$commentText = html_entity_decode($cotherdata->fields['commenttxt'], ENT_QUOTES | ENT_HTML5, 'UTF-8');
$commentText = encodePreservingBr($commentText);
$commentText = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="\$1" target="_blank">\$1</a>', $commentText);
$coment['commenttxt'] = $commentText;
if ($cotherdata->fields['editname'] != "") {
$coment['edittime'] = Config::time($cotherdata->fields['edittime']);
$coment['editname'] = $cotherdata->fields['editname'];
Expand Down

0 comments on commit 0429e14

Please sign in to comment.