From 2d6735a6168bcd69cb6867f612b50f136817e9d7 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 1 Oct 2022 16:37:32 +0000 Subject: [PATCH 1/2] Issue 11953: Split a message in parts --- src/Content/Text/Plaintext.php | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/Content/Text/Plaintext.php b/src/Content/Text/Plaintext.php index ab8b712d89..75fdf2b2bb 100644 --- a/src/Content/Text/Plaintext.php +++ b/src/Content/Text/Plaintext.php @@ -179,6 +179,8 @@ class Plaintext $msg = HTML::toPlaintext($html, 0, true); $msg = trim(html_entity_decode($msg, ENT_QUOTES, 'UTF-8')); + $complete_msg = $msg; + $link = ''; if ($includedlinks) { if ($post['type'] == 'link') { @@ -221,6 +223,8 @@ class Plaintext unset($post['url']); } } + } elseif ($link != '') { + $complete_msg .= "\n" . $link; } } @@ -251,8 +255,61 @@ class Plaintext } } + if ($limit > 0) { + $post['parts'] = self::getParts(trim($complete_msg), $limit); + } + $post['text'] = trim($msg); return $post; } + + /** + * Split the message in parts + * + * @param string $message + * @param integer $limit + * @return array + */ + private static function getParts(string $message, int $limit): array + { + $parts = []; + $part = ''; + + while (trim($message)) { + $pos1 = strpos($message, ' '); + $pos2 = strpos($message, "\n"); + + if (($pos1 !== false) && ($pos2 !== false)) { + $pos = min($pos1, $pos2) + 1; + } elseif ($pos1 !== false) { + $pos = $pos1 + 1; + } elseif ($pos2 !== false) { + $pos = $pos2 + 1; + } else { + $word = $message; + $message = ''; + } + + if (trim($message)) { + $word = substr($message, 0, $pos); + $message = trim(substr($message, $pos)); + } + + if (strlen($part . $word) > ($limit - 8)) { + $parts[] = trim($part); + $part = ''; + } + $part .= $word; + } + $parts[] = $part; + + if (count($parts) > 1) { + foreach ($parts as $key => $part) { + $parts[$key] .= ' (' . ($key + 1) . '/' . count($parts) . ')'; + } + } + + return $parts; + } } From 1e29c6f7053813af933d42faa07ebce50687919c Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 1 Oct 2022 16:54:11 +0000 Subject: [PATCH 2/2] Ensure not to add the plink --- src/Content/Text/Plaintext.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Content/Text/Plaintext.php b/src/Content/Text/Plaintext.php index 75fdf2b2bb..1e76a557bc 100644 --- a/src/Content/Text/Plaintext.php +++ b/src/Content/Text/Plaintext.php @@ -223,8 +223,6 @@ class Plaintext unset($post['url']); } } - } elseif ($link != '') { - $complete_msg .= "\n" . $link; } } @@ -240,6 +238,12 @@ class Plaintext $limit = $limit - 23; } + if (!in_array($link, ['', $item['plink']]) && ($post['type'] != 'photo')) { + $complete_msg .= "\n" . $link; + } + + $post['parts'] = self::getParts(trim($complete_msg), $limit); + if (iconv_strlen($msg, 'UTF-8') > $limit) { if (($post['type'] == 'text') && isset($post['url'])) { $post['url'] = $item['plink']; @@ -255,10 +259,6 @@ class Plaintext } } - if ($limit > 0) { - $post['parts'] = self::getParts(trim($complete_msg), $limit); - } - $post['text'] = trim($msg); return $post;