From 087e26ca6a950f9daa5f255f2c716a149dc1245f Mon Sep 17 00:00:00 2001 From: Retro_Guy Date: Thu, 2 May 2024 04:22:16 -0700 Subject: [PATCH] Apply wordrap in post to hopefully work properly with multibyte characters. --- Rocksolid_Light/rocksolid/newsportal.php | 57 ++++++++++++++++++++++++ Rocksolid_Light/rocksolid/post.php | 4 +- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/Rocksolid_Light/rocksolid/newsportal.php b/Rocksolid_Light/rocksolid/newsportal.php index cd40ba3..670e54e 100644 --- a/Rocksolid_Light/rocksolid/newsportal.php +++ b/Rocksolid_Light/rocksolid/newsportal.php @@ -2050,6 +2050,63 @@ function verify_gpg_signature($res, $signed_text) } } +function mb_wordwrap($string, $width = 75, $break = "\n", $cut = false) { + $string = (string) $string; + if ($string === '') { + return ''; + } + $break = (string) $break; + if ($break === '') { + trigger_error('Break string cannot be empty', E_USER_ERROR); + } + $width = (int) $width; + if ($width === 0 && $cut) { + trigger_error('Cannot force cut when width is zero', E_USER_ERROR); + } + if (strlen($string) === mb_strlen($string)) { + return wordwrap($string, $width, $break, $cut); + } + $stringWidth = mb_strlen($string); + $breakWidth = mb_strlen($break); + $result = ''; + $lastStart = $lastSpace = 0; + for ($current = 0; $current < $stringWidth; $current++) { + $char = mb_substr($string, $current, 1); + $possibleBreak = $char; + if ($breakWidth !== 1) { + $possibleBreak = mb_substr($string, $current, $breakWidth); + } + if ($possibleBreak === $break) { + $result .= mb_substr($string, $lastStart, $current - $lastStart + $breakWidth); + $current += $breakWidth - 1; + $lastStart = $lastSpace = $current + 1; + continue; + } + if ($char === ' ') { + if ($current - $lastStart >= $width) { + $result .= mb_substr($string, $lastStart, $current - $lastStart) . $break; + $lastStart = $current + 1; + } + $lastSpace = $current; + continue; + } + if ($current - $lastStart >= $width && $cut && $lastStart >= $lastSpace) { + $result .= mb_substr($string, $lastStart, $current - $lastStart) . $break; + $lastStart = $lastSpace = $current; + continue; + } + if ($current - $lastStart >= $width && $lastStart < $lastSpace) { + $result .= mb_substr($string, $lastStart, $lastSpace - $lastStart) . $break; + $lastStart = $lastSpace = $lastSpace + 1; + continue; + } + } + if ($lastStart !== $current) { + $result .= mb_substr($string, $lastStart, $current - $lastStart); + } + return $result; +} + function is_moderated($newsgroups) { global $CONFIG, $OVERRIDES, $spooldir; diff --git a/Rocksolid_Light/rocksolid/post.php b/Rocksolid_Light/rocksolid/post.php index 9784923..f8874f2 100644 --- a/Rocksolid_Light/rocksolid/post.php +++ b/Rocksolid_Light/rocksolid/post.php @@ -254,9 +254,9 @@ if ($type == "post") { if (isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0) { $_FILES['photo']['name'] = preg_replace('/[^a-zA-Z0-9\.]/', '_', $_FILES['photo']['name']); // There is an attachment to handle - $message = message_post(quoted_printable_encode($subject), $nemail . " (" . quoted_printable_encode($name) . ")", $newsgroups, $references_array, addslashes(wordwrap($body, 75)), $_POST['encryptthis'], $_POST['encryptto'], strtolower($name), $_POST['fromname'], null, true); + $message = message_post(quoted_printable_encode($subject), $nemail . " (" . quoted_printable_encode($name) . ")", $newsgroups, $references_array, addslashes(mb_wordwrap($body, 75)), $_POST['encryptthis'], $_POST['encryptto'], strtolower($name), $_POST['fromname'], null, true); } else { - $message = message_post(quoted_printable_encode($subject), $nemail . " (" . quoted_printable_encode($name) . ")", $newsgroups, $references_array, addslashes(wordwrap($body, 75)), $_POST['encryptthis'], $_POST['encryptto'], strtolower($name), $_POST['fromname']); + $message = message_post(quoted_printable_encode($subject), $nemail . " (" . quoted_printable_encode($name) . ")", $newsgroups, $references_array, addslashes(mb_wordwrap($body, 75)), $_POST['encryptthis'], $_POST['encryptto'], strtolower($name), $_POST['fromname']); } // Article sent without errors, or duplicate? if ((substr($message, 0, 3) == "240") || (substr($message, 0, 7) == "441 435")) {